• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_NUMBERS_DOUBLE_H_
6 #define V8_NUMBERS_DOUBLE_H_
7 
8 #include "src/base/macros.h"
9 #include "src/numbers/diy-fp.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 // We assume that doubles and uint64_t have the same endianness.
double_to_uint64(double d)15 inline uint64_t double_to_uint64(double d) { return bit_cast<uint64_t>(d); }
uint64_to_double(uint64_t d64)16 inline double uint64_to_double(uint64_t d64) { return bit_cast<double>(d64); }
17 
18 // Helper functions for doubles.
19 class Double {
20  public:
21   static constexpr uint64_t kSignMask = 0x8000'0000'0000'0000;
22   static constexpr uint64_t kExponentMask = 0x7FF0'0000'0000'0000;
23   static constexpr uint64_t kSignificandMask = 0x000F'FFFF'FFFF'FFFF;
24   static constexpr uint64_t kHiddenBit = 0x0010'0000'0000'0000;
25   static constexpr int kPhysicalSignificandSize =
26       52;  // Excludes the hidden bit.
27   static constexpr int kSignificandSize = 53;
28 
Double()29   Double() : d64_(0) {}
Double(double d)30   explicit Double(double d) : d64_(double_to_uint64(d)) {}
Double(uint64_t d64)31   explicit Double(uint64_t d64) : d64_(d64) {}
Double(DiyFp diy_fp)32   explicit Double(DiyFp diy_fp) : d64_(DiyFpToUint64(diy_fp)) {}
33 
34   // The value encoded by this Double must be greater or equal to +0.0.
35   // It must not be special (infinity, or NaN).
AsDiyFp()36   DiyFp AsDiyFp() const {
37     DCHECK_GT(Sign(), 0);
38     DCHECK(!IsSpecial());
39     return DiyFp(Significand(), Exponent());
40   }
41 
42   // The value encoded by this Double must be strictly greater than 0.
AsNormalizedDiyFp()43   DiyFp AsNormalizedDiyFp() const {
44     DCHECK_GT(value(), 0.0);
45     uint64_t f = Significand();
46     int e = Exponent();
47 
48     // The current double could be a denormal.
49     while ((f & kHiddenBit) == 0) {
50       f <<= 1;
51       e--;
52     }
53     // Do the final shifts in one go.
54     f <<= DiyFp::kSignificandSize - kSignificandSize;
55     e -= DiyFp::kSignificandSize - kSignificandSize;
56     return DiyFp(f, e);
57   }
58 
59   // Returns the double's bit as uint64.
AsUint64()60   uint64_t AsUint64() const { return d64_; }
61 
62   // Returns the next greater double. Returns +infinity on input +infinity.
NextDouble()63   double NextDouble() const {
64     if (d64_ == kInfinity) return Double(kInfinity).value();
65     if (Sign() < 0 && Significand() == 0) {
66       // -0.0
67       return 0.0;
68     }
69     if (Sign() < 0) {
70       return Double(d64_ - 1).value();
71     } else {
72       return Double(d64_ + 1).value();
73     }
74   }
75 
Exponent()76   int Exponent() const {
77     if (IsDenormal()) return kDenormalExponent;
78 
79     uint64_t d64 = AsUint64();
80     int biased_e =
81         static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize);
82     return biased_e - kExponentBias;
83   }
84 
Significand()85   uint64_t Significand() const {
86     uint64_t d64 = AsUint64();
87     uint64_t significand = d64 & kSignificandMask;
88     if (!IsDenormal()) {
89       return significand + kHiddenBit;
90     } else {
91       return significand;
92     }
93   }
94 
95   // Returns true if the double is a denormal.
IsDenormal()96   bool IsDenormal() const {
97     uint64_t d64 = AsUint64();
98     return (d64 & kExponentMask) == 0;
99   }
100 
101   // We consider denormals not to be special.
102   // Hence only Infinity and NaN are special.
IsSpecial()103   bool IsSpecial() const {
104     uint64_t d64 = AsUint64();
105     return (d64 & kExponentMask) == kExponentMask;
106   }
107 
IsInfinite()108   bool IsInfinite() const {
109     uint64_t d64 = AsUint64();
110     return ((d64 & kExponentMask) == kExponentMask) &&
111            ((d64 & kSignificandMask) == 0);
112   }
113 
Sign()114   int Sign() const {
115     uint64_t d64 = AsUint64();
116     return (d64 & kSignMask) == 0 ? 1 : -1;
117   }
118 
119   // Precondition: the value encoded by this Double must be greater or equal
120   // than +0.0.
UpperBoundary()121   DiyFp UpperBoundary() const {
122     DCHECK_GT(Sign(), 0);
123     return DiyFp(Significand() * 2 + 1, Exponent() - 1);
124   }
125 
126   // Returns the two boundaries of this.
127   // The bigger boundary (m_plus) is normalized. The lower boundary has the same
128   // exponent as m_plus.
129   // Precondition: the value encoded by this Double must be greater than 0.
NormalizedBoundaries(DiyFp * out_m_minus,DiyFp * out_m_plus)130   void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
131     DCHECK_GT(value(), 0.0);
132     DiyFp v = this->AsDiyFp();
133     bool significand_is_zero = (v.f() == kHiddenBit);
134     DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
135     DiyFp m_minus;
136     if (significand_is_zero && v.e() != kDenormalExponent) {
137       // The boundary is closer. Think of v = 1000e10 and v- = 9999e9.
138       // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
139       // at a distance of 1e8.
140       // The only exception is for the smallest normal: the largest denormal is
141       // at the same distance as its successor.
142       // Note: denormals have the same exponent as the smallest normals.
143       m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
144     } else {
145       m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
146     }
147     m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
148     m_minus.set_e(m_plus.e());
149     *out_m_plus = m_plus;
150     *out_m_minus = m_minus;
151   }
152 
value()153   double value() const { return uint64_to_double(d64_); }
154 
155   // Returns the significand size for a given order of magnitude.
156   // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude.
157   // This function returns the number of significant binary digits v will have
158   // once its encoded into a double. In almost all cases this is equal to
159   // kSignificandSize. The only exception are denormals. They start with leading
160   // zeroes and their effective significand-size is hence smaller.
SignificandSizeForOrderOfMagnitude(int order)161   static int SignificandSizeForOrderOfMagnitude(int order) {
162     if (order >= (kDenormalExponent + kSignificandSize)) {
163       return kSignificandSize;
164     }
165     if (order <= kDenormalExponent) return 0;
166     return order - kDenormalExponent;
167   }
168 
169  private:
170   static constexpr int kExponentBias = 0x3FF + kPhysicalSignificandSize;
171   static constexpr int kDenormalExponent = -kExponentBias + 1;
172   static constexpr int kMaxExponent = 0x7FF - kExponentBias;
173   static constexpr uint64_t kInfinity = 0x7FF0'0000'0000'0000;
174 
175   // The field d64_ is not marked as const to permit the usage of the copy
176   // constructor.
177   uint64_t d64_;
178 
DiyFpToUint64(DiyFp diy_fp)179   static uint64_t DiyFpToUint64(DiyFp diy_fp) {
180     uint64_t significand = diy_fp.f();
181     int exponent = diy_fp.e();
182     while (significand > kHiddenBit + kSignificandMask) {
183       significand >>= 1;
184       exponent++;
185     }
186     if (exponent >= kMaxExponent) {
187       return kInfinity;
188     }
189     if (exponent < kDenormalExponent) {
190       return 0;
191     }
192     while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) {
193       significand <<= 1;
194       exponent--;
195     }
196     uint64_t biased_exponent;
197     if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) {
198       biased_exponent = 0;
199     } else {
200       biased_exponent = static_cast<uint64_t>(exponent + kExponentBias);
201     }
202     return (significand & kSignificandMask) |
203            (biased_exponent << kPhysicalSignificandSize);
204   }
205 };
206 
207 }  // namespace internal
208 }  // namespace v8
209 
210 #endif  // V8_NUMBERS_DOUBLE_H_
211