File indexing completed on 2025-01-18 09:55:27
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #ifndef DOUBLE_CONVERSION_DOUBLE_H_
0029 #define DOUBLE_CONVERSION_DOUBLE_H_
0030
0031 #include "diy-fp.h"
0032
0033 namespace double_conversion {
0034
0035
0036 static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); }
0037 static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); }
0038 static uint32_t float_to_uint32(float f) { return BitCast<uint32_t>(f); }
0039 static float uint32_to_float(uint32_t d32) { return BitCast<float>(d32); }
0040
0041
0042 class Double {
0043 public:
0044 static const uint64_t kSignMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000);
0045 static const uint64_t kExponentMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000);
0046 static const uint64_t kSignificandMask = DOUBLE_CONVERSION_UINT64_2PART_C(0x000FFFFF, FFFFFFFF);
0047 static const uint64_t kHiddenBit = DOUBLE_CONVERSION_UINT64_2PART_C(0x00100000, 00000000);
0048 static const uint64_t kQuietNanBit = DOUBLE_CONVERSION_UINT64_2PART_C(0x00080000, 00000000);
0049 static const int kPhysicalSignificandSize = 52;
0050 static const int kSignificandSize = 53;
0051 static const int kExponentBias = 0x3FF + kPhysicalSignificandSize;
0052 static const int kMaxExponent = 0x7FF - kExponentBias;
0053
0054 Double() : d64_(0) {}
0055 explicit Double(double d) : d64_(double_to_uint64(d)) {}
0056 explicit Double(uint64_t d64) : d64_(d64) {}
0057 explicit Double(DiyFp diy_fp)
0058 : d64_(DiyFpToUint64(diy_fp)) {}
0059
0060
0061
0062 DiyFp AsDiyFp() const {
0063 DOUBLE_CONVERSION_ASSERT(Sign() > 0);
0064 DOUBLE_CONVERSION_ASSERT(!IsSpecial());
0065 return DiyFp(Significand(), Exponent());
0066 }
0067
0068
0069 DiyFp AsNormalizedDiyFp() const {
0070 DOUBLE_CONVERSION_ASSERT(value() > 0.0);
0071 uint64_t f = Significand();
0072 int e = Exponent();
0073
0074
0075 while ((f & kHiddenBit) == 0) {
0076 f <<= 1;
0077 e--;
0078 }
0079
0080 f <<= DiyFp::kSignificandSize - kSignificandSize;
0081 e -= DiyFp::kSignificandSize - kSignificandSize;
0082 return DiyFp(f, e);
0083 }
0084
0085
0086 uint64_t AsUint64() const {
0087 return d64_;
0088 }
0089
0090
0091 double NextDouble() const {
0092 if (d64_ == kInfinity) return Double(kInfinity).value();
0093 if (Sign() < 0 && Significand() == 0) {
0094
0095 return 0.0;
0096 }
0097 if (Sign() < 0) {
0098 return Double(d64_ - 1).value();
0099 } else {
0100 return Double(d64_ + 1).value();
0101 }
0102 }
0103
0104 double PreviousDouble() const {
0105 if (d64_ == (kInfinity | kSignMask)) return -Infinity();
0106 if (Sign() < 0) {
0107 return Double(d64_ + 1).value();
0108 } else {
0109 if (Significand() == 0) return -0.0;
0110 return Double(d64_ - 1).value();
0111 }
0112 }
0113
0114 int Exponent() const {
0115 if (IsDenormal()) return kDenormalExponent;
0116
0117 uint64_t d64 = AsUint64();
0118 int biased_e =
0119 static_cast<int>((d64 & kExponentMask) >> kPhysicalSignificandSize);
0120 return biased_e - kExponentBias;
0121 }
0122
0123 uint64_t Significand() const {
0124 uint64_t d64 = AsUint64();
0125 uint64_t significand = d64 & kSignificandMask;
0126 if (!IsDenormal()) {
0127 return significand + kHiddenBit;
0128 } else {
0129 return significand;
0130 }
0131 }
0132
0133
0134 bool IsDenormal() const {
0135 uint64_t d64 = AsUint64();
0136 return (d64 & kExponentMask) == 0;
0137 }
0138
0139
0140
0141 bool IsSpecial() const {
0142 uint64_t d64 = AsUint64();
0143 return (d64 & kExponentMask) == kExponentMask;
0144 }
0145
0146 bool IsNan() const {
0147 uint64_t d64 = AsUint64();
0148 return ((d64 & kExponentMask) == kExponentMask) &&
0149 ((d64 & kSignificandMask) != 0);
0150 }
0151
0152 bool IsQuietNan() const {
0153 #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
0154 return IsNan() && ((AsUint64() & kQuietNanBit) == 0);
0155 #else
0156 return IsNan() && ((AsUint64() & kQuietNanBit) != 0);
0157 #endif
0158 }
0159
0160 bool IsSignalingNan() const {
0161 #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
0162 return IsNan() && ((AsUint64() & kQuietNanBit) != 0);
0163 #else
0164 return IsNan() && ((AsUint64() & kQuietNanBit) == 0);
0165 #endif
0166 }
0167
0168
0169 bool IsInfinite() const {
0170 uint64_t d64 = AsUint64();
0171 return ((d64 & kExponentMask) == kExponentMask) &&
0172 ((d64 & kSignificandMask) == 0);
0173 }
0174
0175 int Sign() const {
0176 uint64_t d64 = AsUint64();
0177 return (d64 & kSignMask) == 0? 1: -1;
0178 }
0179
0180
0181
0182 DiyFp UpperBoundary() const {
0183 DOUBLE_CONVERSION_ASSERT(Sign() > 0);
0184 return DiyFp(Significand() * 2 + 1, Exponent() - 1);
0185 }
0186
0187
0188
0189
0190
0191 void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
0192 DOUBLE_CONVERSION_ASSERT(value() > 0.0);
0193 DiyFp v = this->AsDiyFp();
0194 DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
0195 DiyFp m_minus;
0196 if (LowerBoundaryIsCloser()) {
0197 m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
0198 } else {
0199 m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
0200 }
0201 m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
0202 m_minus.set_e(m_plus.e());
0203 *out_m_plus = m_plus;
0204 *out_m_minus = m_minus;
0205 }
0206
0207 bool LowerBoundaryIsCloser() const {
0208
0209
0210
0211
0212
0213
0214
0215
0216 bool physical_significand_is_zero = ((AsUint64() & kSignificandMask) == 0);
0217 return physical_significand_is_zero && (Exponent() != kDenormalExponent);
0218 }
0219
0220 double value() const { return uint64_to_double(d64_); }
0221
0222
0223
0224
0225
0226
0227
0228 static int SignificandSizeForOrderOfMagnitude(int order) {
0229 if (order >= (kDenormalExponent + kSignificandSize)) {
0230 return kSignificandSize;
0231 }
0232 if (order <= kDenormalExponent) return 0;
0233 return order - kDenormalExponent;
0234 }
0235
0236 static double Infinity() {
0237 return Double(kInfinity).value();
0238 }
0239
0240 static double NaN() {
0241 return Double(kNaN).value();
0242 }
0243
0244 private:
0245 static const int kDenormalExponent = -kExponentBias + 1;
0246 static const uint64_t kInfinity = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF00000, 00000000);
0247 #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
0248 static const uint64_t kNaN = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF7FFFF, FFFFFFFF);
0249 #else
0250 static const uint64_t kNaN = DOUBLE_CONVERSION_UINT64_2PART_C(0x7FF80000, 00000000);
0251 #endif
0252
0253
0254 const uint64_t d64_;
0255
0256 static uint64_t DiyFpToUint64(DiyFp diy_fp) {
0257 uint64_t significand = diy_fp.f();
0258 int exponent = diy_fp.e();
0259 while (significand > kHiddenBit + kSignificandMask) {
0260 significand >>= 1;
0261 exponent++;
0262 }
0263 if (exponent >= kMaxExponent) {
0264 return kInfinity;
0265 }
0266 if (exponent < kDenormalExponent) {
0267 return 0;
0268 }
0269 while (exponent > kDenormalExponent && (significand & kHiddenBit) == 0) {
0270 significand <<= 1;
0271 exponent--;
0272 }
0273 uint64_t biased_exponent;
0274 if (exponent == kDenormalExponent && (significand & kHiddenBit) == 0) {
0275 biased_exponent = 0;
0276 } else {
0277 biased_exponent = static_cast<uint64_t>(exponent + kExponentBias);
0278 }
0279 return (significand & kSignificandMask) |
0280 (biased_exponent << kPhysicalSignificandSize);
0281 }
0282
0283 DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Double);
0284 };
0285
0286 class Single {
0287 public:
0288 static const uint32_t kSignMask = 0x80000000;
0289 static const uint32_t kExponentMask = 0x7F800000;
0290 static const uint32_t kSignificandMask = 0x007FFFFF;
0291 static const uint32_t kHiddenBit = 0x00800000;
0292 static const uint32_t kQuietNanBit = 0x00400000;
0293 static const int kPhysicalSignificandSize = 23;
0294 static const int kSignificandSize = 24;
0295
0296 Single() : d32_(0) {}
0297 explicit Single(float f) : d32_(float_to_uint32(f)) {}
0298 explicit Single(uint32_t d32) : d32_(d32) {}
0299
0300
0301
0302 DiyFp AsDiyFp() const {
0303 DOUBLE_CONVERSION_ASSERT(Sign() > 0);
0304 DOUBLE_CONVERSION_ASSERT(!IsSpecial());
0305 return DiyFp(Significand(), Exponent());
0306 }
0307
0308
0309 uint32_t AsUint32() const {
0310 return d32_;
0311 }
0312
0313 int Exponent() const {
0314 if (IsDenormal()) return kDenormalExponent;
0315
0316 uint32_t d32 = AsUint32();
0317 int biased_e =
0318 static_cast<int>((d32 & kExponentMask) >> kPhysicalSignificandSize);
0319 return biased_e - kExponentBias;
0320 }
0321
0322 uint32_t Significand() const {
0323 uint32_t d32 = AsUint32();
0324 uint32_t significand = d32 & kSignificandMask;
0325 if (!IsDenormal()) {
0326 return significand + kHiddenBit;
0327 } else {
0328 return significand;
0329 }
0330 }
0331
0332
0333 bool IsDenormal() const {
0334 uint32_t d32 = AsUint32();
0335 return (d32 & kExponentMask) == 0;
0336 }
0337
0338
0339
0340 bool IsSpecial() const {
0341 uint32_t d32 = AsUint32();
0342 return (d32 & kExponentMask) == kExponentMask;
0343 }
0344
0345 bool IsNan() const {
0346 uint32_t d32 = AsUint32();
0347 return ((d32 & kExponentMask) == kExponentMask) &&
0348 ((d32 & kSignificandMask) != 0);
0349 }
0350
0351 bool IsQuietNan() const {
0352 #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
0353 return IsNan() && ((AsUint32() & kQuietNanBit) == 0);
0354 #else
0355 return IsNan() && ((AsUint32() & kQuietNanBit) != 0);
0356 #endif
0357 }
0358
0359 bool IsSignalingNan() const {
0360 #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
0361 return IsNan() && ((AsUint32() & kQuietNanBit) != 0);
0362 #else
0363 return IsNan() && ((AsUint32() & kQuietNanBit) == 0);
0364 #endif
0365 }
0366
0367
0368 bool IsInfinite() const {
0369 uint32_t d32 = AsUint32();
0370 return ((d32 & kExponentMask) == kExponentMask) &&
0371 ((d32 & kSignificandMask) == 0);
0372 }
0373
0374 int Sign() const {
0375 uint32_t d32 = AsUint32();
0376 return (d32 & kSignMask) == 0? 1: -1;
0377 }
0378
0379
0380
0381
0382
0383 void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
0384 DOUBLE_CONVERSION_ASSERT(value() > 0.0);
0385 DiyFp v = this->AsDiyFp();
0386 DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
0387 DiyFp m_minus;
0388 if (LowerBoundaryIsCloser()) {
0389 m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
0390 } else {
0391 m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
0392 }
0393 m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
0394 m_minus.set_e(m_plus.e());
0395 *out_m_plus = m_plus;
0396 *out_m_minus = m_minus;
0397 }
0398
0399
0400
0401 DiyFp UpperBoundary() const {
0402 DOUBLE_CONVERSION_ASSERT(Sign() > 0);
0403 return DiyFp(Significand() * 2 + 1, Exponent() - 1);
0404 }
0405
0406 bool LowerBoundaryIsCloser() const {
0407
0408
0409
0410
0411
0412
0413
0414
0415 bool physical_significand_is_zero = ((AsUint32() & kSignificandMask) == 0);
0416 return physical_significand_is_zero && (Exponent() != kDenormalExponent);
0417 }
0418
0419 float value() const { return uint32_to_float(d32_); }
0420
0421 static float Infinity() {
0422 return Single(kInfinity).value();
0423 }
0424
0425 static float NaN() {
0426 return Single(kNaN).value();
0427 }
0428
0429 private:
0430 static const int kExponentBias = 0x7F + kPhysicalSignificandSize;
0431 static const int kDenormalExponent = -kExponentBias + 1;
0432 static const int kMaxExponent = 0xFF - kExponentBias;
0433 static const uint32_t kInfinity = 0x7F800000;
0434 #if (defined(__mips__) && !defined(__mips_nan2008)) || defined(__hppa__)
0435 static const uint32_t kNaN = 0x7FBFFFFF;
0436 #else
0437 static const uint32_t kNaN = 0x7FC00000;
0438 #endif
0439
0440 const uint32_t d32_;
0441
0442 DOUBLE_CONVERSION_DISALLOW_COPY_AND_ASSIGN(Single);
0443 };
0444
0445 }
0446
0447 #endif