/src/double-conversion/double-conversion/diy-fp.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2010 the V8 project authors. All rights reserved. |
2 | | // Redistribution and use in source and binary forms, with or without |
3 | | // modification, are permitted provided that the following conditions are |
4 | | // met: |
5 | | // |
6 | | // * Redistributions of source code must retain the above copyright |
7 | | // notice, this list of conditions and the following disclaimer. |
8 | | // * Redistributions in binary form must reproduce the above |
9 | | // copyright notice, this list of conditions and the following |
10 | | // disclaimer in the documentation and/or other materials provided |
11 | | // with the distribution. |
12 | | // * Neither the name of Google Inc. nor the names of its |
13 | | // contributors may be used to endorse or promote products derived |
14 | | // from this software without specific prior written permission. |
15 | | // |
16 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 | | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 | | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 | | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 | | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | | |
28 | | #ifndef DOUBLE_CONVERSION_DIY_FP_H_ |
29 | | #define DOUBLE_CONVERSION_DIY_FP_H_ |
30 | | |
31 | | #include "utils.h" |
32 | | |
33 | | namespace double_conversion { |
34 | | |
35 | | // This "Do It Yourself Floating Point" class implements a floating-point number |
36 | | // with a uint64 significand and an int exponent. Normalized DiyFp numbers will |
37 | | // have the most significant bit of the significand set. |
38 | | // Multiplication and Subtraction do not normalize their results. |
39 | | // DiyFp store only non-negative numbers and are not designed to contain special |
40 | | // doubles (NaN and Infinity). |
41 | | class DiyFp { |
42 | | public: |
43 | | static const int kSignificandSize = 64; |
44 | | |
45 | 2.91k | DiyFp() : f_(0), e_(0) {} |
46 | 7.00k | DiyFp(const uint64_t significand, const int32_t exponent) : f_(significand), e_(exponent) {} |
47 | | |
48 | | // this -= other. |
49 | | // The exponents of both numbers must be the same and the significand of this |
50 | | // must be greater or equal than the significand of other. |
51 | | // The result will not be normalized. |
52 | 0 | void Subtract(const DiyFp& other) { |
53 | 0 | DOUBLE_CONVERSION_ASSERT(e_ == other.e_); |
54 | 0 | DOUBLE_CONVERSION_ASSERT(f_ >= other.f_); |
55 | 0 | f_ -= other.f_; |
56 | 0 | } |
57 | | |
58 | | // Returns a - b. |
59 | | // The exponents of both numbers must be the same and a must be greater |
60 | | // or equal than b. The result will not be normalized. |
61 | 0 | static DiyFp Minus(const DiyFp& a, const DiyFp& b) { |
62 | 0 | DiyFp result = a; |
63 | 0 | result.Subtract(b); |
64 | 0 | return result; |
65 | 0 | } |
66 | | |
67 | | // this *= other. |
68 | 2.78k | void Multiply(const DiyFp& other) { |
69 | | // Simply "emulates" a 128 bit multiplication. |
70 | | // However: the resulting number only contains 64 bits. The least |
71 | | // significant 64 bits are only used for rounding the most significant 64 |
72 | | // bits. |
73 | 2.78k | const uint64_t kM32 = 0xFFFFFFFFU; |
74 | 2.78k | const uint64_t a = f_ >> 32; |
75 | 2.78k | const uint64_t b = f_ & kM32; |
76 | 2.78k | const uint64_t c = other.f_ >> 32; |
77 | 2.78k | const uint64_t d = other.f_ & kM32; |
78 | 2.78k | const uint64_t ac = a * c; |
79 | 2.78k | const uint64_t bc = b * c; |
80 | 2.78k | const uint64_t ad = a * d; |
81 | 2.78k | const uint64_t bd = b * d; |
82 | | // By adding 1U << 31 to tmp we round the final result. |
83 | | // Halfway cases will be rounded up. |
84 | 2.78k | const uint64_t tmp = (bd >> 32) + (ad & kM32) + (bc & kM32) + (1U << 31); |
85 | 2.78k | e_ += other.e_ + 64; |
86 | 2.78k | f_ = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32); |
87 | 2.78k | } |
88 | | |
89 | | // returns a * b; |
90 | 0 | static DiyFp Times(const DiyFp& a, const DiyFp& b) { |
91 | 0 | DiyFp result = a; |
92 | 0 | result.Multiply(b); |
93 | 0 | return result; |
94 | 0 | } |
95 | | |
96 | 2.91k | void Normalize() { |
97 | 2.91k | DOUBLE_CONVERSION_ASSERT(f_ != 0); |
98 | 2.91k | uint64_t significand = f_; |
99 | 2.91k | int32_t exponent = e_; |
100 | | |
101 | | // This method is mainly called for normalizing boundaries. In general, |
102 | | // boundaries need to be shifted by 10 bits, and we optimize for this case. |
103 | 2.91k | const uint64_t k10MSBits = DOUBLE_CONVERSION_UINT64_2PART_C(0xFFC00000, 00000000); |
104 | 6.39k | while ((significand & k10MSBits) == 0) { |
105 | 3.48k | significand <<= 10; |
106 | 3.48k | exponent -= 10; |
107 | 3.48k | } |
108 | 7.47k | while ((significand & kUint64MSB) == 0) { |
109 | 4.56k | significand <<= 1; |
110 | 4.56k | exponent--; |
111 | 4.56k | } |
112 | 2.91k | f_ = significand; |
113 | 2.91k | e_ = exponent; |
114 | 2.91k | } |
115 | | |
116 | 0 | static DiyFp Normalize(const DiyFp& a) { |
117 | 0 | DiyFp result = a; |
118 | 0 | result.Normalize(); |
119 | 0 | return result; |
120 | 0 | } |
121 | | |
122 | 6.06k | uint64_t f() const { return f_; } |
123 | 12.3k | int32_t e() const { return e_; } |
124 | | |
125 | 397 | void set_f(uint64_t new_value) { f_ = new_value; } |
126 | 90 | void set_e(int32_t new_value) { e_ = new_value; } |
127 | | |
128 | | private: |
129 | | static const uint64_t kUint64MSB = DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000); |
130 | | |
131 | | uint64_t f_; |
132 | | int32_t e_; |
133 | | }; |
134 | | |
135 | | } // namespace double_conversion |
136 | | |
137 | | #endif // DOUBLE_CONVERSION_DIY_FP_H_ |