8 #include "src/base/macros.h" 9 #include "src/diy-fp.h" 15 inline uint64_t double_to_uint64(
double d) {
return bit_cast<uint64_t>(d); }
16 inline double uint64_to_double(uint64_t d64) {
return bit_cast<
double>(d64); }
21 static constexpr uint64_t kSignMask = V8_2PART_UINT64_C(0x80000000, 00000000);
22 static constexpr uint64_t kExponentMask =
23 V8_2PART_UINT64_C(0x7FF00000, 00000000);
24 static constexpr uint64_t kSignificandMask =
25 V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF);
26 static constexpr uint64_t kHiddenBit =
27 V8_2PART_UINT64_C(0x00100000, 00000000);
28 static constexpr
int kPhysicalSignificandSize =
30 static constexpr
int kSignificandSize = 53;
33 explicit Double(
double d) : d64_(double_to_uint64(d)) {}
34 explicit Double(uint64_t d64) : d64_(d64) {}
36 : d64_(DiyFpToUint64(diy_fp)) {}
40 DiyFp AsDiyFp()
const {
43 return DiyFp(Significand(), Exponent());
47 DiyFp AsNormalizedDiyFp()
const {
48 DCHECK_GT(value(), 0.0);
49 uint64_t f = Significand();
53 while ((f & kHiddenBit) == 0) {
58 f <<= DiyFp::kSignificandSize - kSignificandSize;
59 e -= DiyFp::kSignificandSize - kSignificandSize;
64 uint64_t AsUint64()
const {
69 double NextDouble()
const {
70 if (d64_ == kInfinity)
return Double(kInfinity).value();
71 if (Sign() < 0 && Significand() == 0) {
76 return Double(d64_ - 1).value();
78 return Double(d64_ + 1).value();
82 int Exponent()
const {
83 if (IsDenormal())
return kDenormalExponent;
85 uint64_t d64 = AsUint64();
87 static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize);
88 return biased_e - kExponentBias;
91 uint64_t Significand()
const {
92 uint64_t d64 = AsUint64();
93 uint64_t significand = d64 & kSignificandMask;
95 return significand + kHiddenBit;
102 bool IsDenormal()
const {
103 uint64_t d64 = AsUint64();
104 return (d64 & kExponentMask) == 0;
109 bool IsSpecial()
const {
110 uint64_t d64 = AsUint64();
111 return (d64 & kExponentMask) == kExponentMask;
114 bool IsInfinite()
const {
115 uint64_t d64 = AsUint64();
116 return ((d64 & kExponentMask) == kExponentMask) &&
117 ((d64 & kSignificandMask) == 0);
121 uint64_t d64 = AsUint64();
122 return (d64 & kSignMask) == 0? 1: -1;
127 DiyFp UpperBoundary()
const {
128 DCHECK_GT(Sign(), 0);
129 return DiyFp(Significand() * 2 + 1, Exponent() - 1);
136 void NormalizedBoundaries(
DiyFp* out_m_minus,
DiyFp* out_m_plus)
const {
137 DCHECK_GT(value(), 0.0);
138 DiyFp v = this->AsDiyFp();
139 bool significand_is_zero = (v.f() == kHiddenBit);
140 DiyFp m_plus = DiyFp::Normalize(
DiyFp((v.f() << 1) + 1, v.e() - 1));
142 if (significand_is_zero && v.e() != kDenormalExponent) {
149 m_minus =
DiyFp((v.f() << 2) - 1, v.e() - 2);
151 m_minus =
DiyFp((v.f() << 1) - 1, v.e() - 1);
153 m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
154 m_minus.set_e(m_plus.e());
155 *out_m_plus = m_plus;
156 *out_m_minus = m_minus;
159 double value()
const {
return uint64_to_double(d64_); }
167 static int SignificandSizeForOrderOfMagnitude(
int order) {
168 if (order >= (kDenormalExponent + kSignificandSize)) {
169 return kSignificandSize;
171 if (order <= kDenormalExponent)
return 0;
172 return order - kDenormalExponent;
176 static constexpr
int kExponentBias = 0x3FF + kPhysicalSignificandSize;
177 static constexpr
int kDenormalExponent = -kExponentBias + 1;
178 static constexpr
int kMaxExponent = 0x7FF - kExponentBias;
179 static constexpr uint64_t kInfinity = V8_2PART_UINT64_C(0x7FF00000, 00000000);
185 static uint64_t DiyFpToUint64(
DiyFp diy_fp) {
186 uint64_t significand = diy_fp.f();
187 int exponent = diy_fp.e();
188 while (significand > kHiddenBit + kSignificandMask) {
192 if (exponent >= kMaxExponent) {
195 if (exponent < kDenormalExponent) {
198 while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) {
202 uint64_t biased_exponent;
203 if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) {
206 biased_exponent =
static_cast<uint64_t
>(exponent + kExponentBias);
208 return (significand & kSignificandMask) |
209 (biased_exponent << kPhysicalSignificandSize);
216 #endif // V8_DOUBLE_H_