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