]> git.saurik.com Git - apple/icu.git/blame_incremental - icuSources/i18n/double-conversion-diy-fp.h
ICU-64260.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / double-conversion-diy-fp.h
... / ...
CommitLineData
1// © 2018 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3//
4// From the double-conversion library. Original license:
5//
6// Copyright 2010 the V8 project authors. All rights reserved.
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions are
9// met:
10//
11// * Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// * Redistributions in binary form must reproduce the above
14// copyright notice, this list of conditions and the following
15// disclaimer in the documentation and/or other materials provided
16// with the distribution.
17// * Neither the name of Google Inc. nor the names of its
18// contributors may be used to endorse or promote products derived
19// from this software without specific prior written permission.
20//
21// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33// ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
34#include "unicode/utypes.h"
35#if !UCONFIG_NO_FORMATTING
36
37#ifndef DOUBLE_CONVERSION_DIY_FP_H_
38#define DOUBLE_CONVERSION_DIY_FP_H_
39
40// ICU PATCH: Customize header file paths for ICU.
41
42#include "double-conversion-utils.h"
43
44// ICU PATCH: Wrap in ICU namespace
45U_NAMESPACE_BEGIN
46
47namespace double_conversion {
48
49// This "Do It Yourself Floating Point" class implements a floating-point number
50// with a uint64 significand and an int exponent. Normalized DiyFp numbers will
51// have the most significant bit of the significand set.
52// Multiplication and Subtraction do not normalize their results.
53// DiyFp are not designed to contain special doubles (NaN and Infinity).
54class DiyFp {
55 public:
56 static const int kSignificandSize = 64;
57
58 DiyFp() : f_(0), e_(0) {}
59 DiyFp(uint64_t significand, int exponent) : f_(significand), e_(exponent) {}
60
61 // this = this - other.
62 // The exponents of both numbers must be the same and the significand of this
63 // must be bigger than the significand of other.
64 // The result will not be normalized.
65 void Subtract(const DiyFp& other) {
66 ASSERT(e_ == other.e_);
67 ASSERT(f_ >= other.f_);
68 f_ -= other.f_;
69 }
70
71 // Returns a - b.
72 // The exponents of both numbers must be the same and this must be bigger
73 // than other. The result will not be normalized.
74 static DiyFp Minus(const DiyFp& a, const DiyFp& b) {
75 DiyFp result = a;
76 result.Subtract(b);
77 return result;
78 }
79
80
81 // this = this * other.
82 void Multiply(const DiyFp& other);
83
84 // returns a * b;
85 static DiyFp Times(const DiyFp& a, const DiyFp& b) {
86 DiyFp result = a;
87 result.Multiply(b);
88 return result;
89 }
90
91 void Normalize() {
92 ASSERT(f_ != 0);
93 uint64_t significand = f_;
94 int exponent = e_;
95
96 // This method is mainly called for normalizing boundaries. In general
97 // boundaries need to be shifted by 10 bits. We thus optimize for this case.
98 const uint64_t k10MSBits = UINT64_2PART_C(0xFFC00000, 00000000);
99 while ((significand & k10MSBits) == 0) {
100 significand <<= 10;
101 exponent -= 10;
102 }
103 while ((significand & kUint64MSB) == 0) {
104 significand <<= 1;
105 exponent--;
106 }
107 f_ = significand;
108 e_ = exponent;
109 }
110
111 static DiyFp Normalize(const DiyFp& a) {
112 DiyFp result = a;
113 result.Normalize();
114 return result;
115 }
116
117 uint64_t f() const { return f_; }
118 int e() const { return e_; }
119
120 void set_f(uint64_t new_value) { f_ = new_value; }
121 void set_e(int new_value) { e_ = new_value; }
122
123 private:
124 static const uint64_t kUint64MSB = UINT64_2PART_C(0x80000000, 00000000);
125
126 uint64_t f_;
127 int e_;
128};
129
130} // namespace double_conversion
131
132// ICU PATCH: Close ICU namespace
133U_NAMESPACE_END
134
135#endif // DOUBLE_CONVERSION_DIY_FP_H_
136#endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING