/src/mozilla-central/intl/icu/source/i18n/double-conversion-diy-fp.h
Line | Count | Source (jump to first uncovered line) |
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 |
45 | | U_NAMESPACE_BEGIN |
46 | | |
47 | | namespace 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). |
54 | | class DiyFp { |
55 | | public: |
56 | | static const int kSignificandSize = 64; |
57 | | |
58 | 0 | DiyFp() : f_(0), e_(0) {} |
59 | 0 | 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 | 0 | void Subtract(const DiyFp& other) { |
66 | 0 | ASSERT(e_ == other.e_); |
67 | 0 | ASSERT(f_ >= other.f_); |
68 | 0 | f_ -= other.f_; |
69 | 0 | } |
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 | 0 | static DiyFp Minus(const DiyFp& a, const DiyFp& b) { |
75 | 0 | DiyFp result = a; |
76 | 0 | result.Subtract(b); |
77 | 0 | return result; |
78 | 0 | } |
79 | | |
80 | | |
81 | | // this = this * other. |
82 | | void Multiply(const DiyFp& other); |
83 | | |
84 | | // returns a * b; |
85 | 0 | static DiyFp Times(const DiyFp& a, const DiyFp& b) { |
86 | 0 | DiyFp result = a; |
87 | 0 | result.Multiply(b); |
88 | 0 | return result; |
89 | 0 | } |
90 | | |
91 | 0 | void Normalize() { |
92 | 0 | ASSERT(f_ != 0); |
93 | 0 | uint64_t significand = f_; |
94 | 0 | int exponent = e_; |
95 | 0 |
|
96 | 0 | // This method is mainly called for normalizing boundaries. In general |
97 | 0 | // boundaries need to be shifted by 10 bits. We thus optimize for this case. |
98 | 0 | const uint64_t k10MSBits = UINT64_2PART_C(0xFFC00000, 00000000); |
99 | 0 | while ((significand & k10MSBits) == 0) { |
100 | 0 | significand <<= 10; |
101 | 0 | exponent -= 10; |
102 | 0 | } |
103 | 0 | while ((significand & kUint64MSB) == 0) { |
104 | 0 | significand <<= 1; |
105 | 0 | exponent--; |
106 | 0 | } |
107 | 0 | f_ = significand; |
108 | 0 | e_ = exponent; |
109 | 0 | } |
110 | | |
111 | 0 | static DiyFp Normalize(const DiyFp& a) { |
112 | 0 | DiyFp result = a; |
113 | 0 | result.Normalize(); |
114 | 0 | return result; |
115 | 0 | } |
116 | | |
117 | 0 | uint64_t f() const { return f_; } |
118 | 0 | int e() const { return e_; } |
119 | | |
120 | 0 | void set_f(uint64_t new_value) { f_ = new_value; } |
121 | 0 | 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 |
133 | | U_NAMESPACE_END |
134 | | |
135 | | #endif // DOUBLE_CONVERSION_DIY_FP_H_ |
136 | | #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING |