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