Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 08:27:14

0001 // Copyright 2012 the V8 project authors. All rights reserved.
0002 // Redistribution and use in source and binary forms, with or without
0003 // modification, are permitted provided that the following conditions are
0004 // met:
0005 //
0006 //     * Redistributions of source code must retain the above copyright
0007 //       notice, this list of conditions and the following disclaimer.
0008 //     * Redistributions in binary form must reproduce the above
0009 //       copyright notice, this list of conditions and the following
0010 //       disclaimer in the documentation and/or other materials provided
0011 //       with the distribution.
0012 //     * Neither the name of Google Inc. nor the names of its
0013 //       contributors may be used to endorse or promote products derived
0014 //       from this software without specific prior written permission.
0015 //
0016 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0017 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0018 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0019 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0020 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0021 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0022 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0023 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0024 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0026 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 // We assume that doubles and uint64_t have the same endianness.
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 // Helper functions for doubles.
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;  // Excludes the hidden bit.
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   // The value encoded by this Double must be greater or equal to +0.0.
0062   // It must not be special (infinity, or NaN).
0063   DiyFp AsDiyFp() const {
0064     DOUBLE_CONVERSION_ASSERT(Sign() > 0);
0065     DOUBLE_CONVERSION_ASSERT(!IsSpecial());
0066     return DiyFp(Significand(), Exponent());
0067   }
0068 
0069   // The value encoded by this Double must be strictly greater than 0.
0070   DiyFp AsNormalizedDiyFp() const {
0071     DOUBLE_CONVERSION_ASSERT(value() > 0.0);
0072     uint64_t f = Significand();
0073     int e = Exponent();
0074 
0075     // The current double could be a denormal.
0076     while ((f & kHiddenBit) == 0) {
0077       f <<= 1;
0078       e--;
0079     }
0080     // Do the final shifts in one go.
0081     f <<= DiyFp::kSignificandSize - kSignificandSize;
0082     e -= DiyFp::kSignificandSize - kSignificandSize;
0083     return DiyFp(f, e);
0084   }
0085 
0086   // Returns the double's bit as uint64.
0087   uint64_t AsUint64() const {
0088     return d64_;
0089   }
0090 
0091   // Returns the next greater double. Returns +infinity on input +infinity.
0092   double NextDouble() const {
0093     if (d64_ == kInfinity) return Double(kInfinity).value();
0094     if (Sign() < 0 && Significand() == 0) {
0095       // -0.0
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   // Returns true if the double is a denormal.
0135   bool IsDenormal() const {
0136     uint64_t d64 = AsUint64();
0137     return (d64 & kExponentMask) == 0;
0138   }
0139 
0140   // We consider denormals not to be special.
0141   // Hence only Infinity and NaN are special.
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   // Precondition: the value encoded by this Double must be greater or equal
0182   // than +0.0.
0183   DiyFp UpperBoundary() const {
0184     DOUBLE_CONVERSION_ASSERT(Sign() > 0);
0185     return DiyFp(Significand() * 2 + 1, Exponent() - 1);
0186   }
0187 
0188   // Computes the two boundaries of this.
0189   // The bigger boundary (m_plus) is normalized. The lower boundary has the same
0190   // exponent as m_plus.
0191   // Precondition: the value encoded by this Double must be greater than 0.
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     // The boundary is closer if the significand is of the form f == 2^p-1 then
0210     // the lower boundary is closer.
0211     // Think of v = 1000e10 and v- = 9999e9.
0212     // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
0213     // at a distance of 1e8.
0214     // The only exception is for the smallest normal: the largest denormal is
0215     // at the same distance as its successor.
0216     // Note: denormals have the same exponent as the smallest normals.
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   // Returns the significand size for a given order of magnitude.
0224   // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude.
0225   // This function returns the number of significant binary digits v will have
0226   // once it's encoded into a double. In almost all cases this is equal to
0227   // kSignificandSize. The only exceptions are denormals. They start with
0228   // leading zeroes and their effective significand-size is hence smaller.
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;  // Excludes the hidden bit.
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   // The value encoded by this Single must be greater or equal to +0.0.
0302   // It must not be special (infinity, or NaN).
0303   DiyFp AsDiyFp() const {
0304     DOUBLE_CONVERSION_ASSERT(Sign() > 0);
0305     DOUBLE_CONVERSION_ASSERT(!IsSpecial());
0306     return DiyFp(Significand(), Exponent());
0307   }
0308 
0309   // Returns the single's bit as uint64.
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   // Returns true if the single is a denormal.
0334   bool IsDenormal() const {
0335     uint32_t d32 = AsUint32();
0336     return (d32 & kExponentMask) == 0;
0337   }
0338 
0339   // We consider denormals not to be special.
0340   // Hence only Infinity and NaN are special.
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   // Computes the two boundaries of this.
0381   // The bigger boundary (m_plus) is normalized. The lower boundary has the same
0382   // exponent as m_plus.
0383   // Precondition: the value encoded by this Single must be greater than 0.
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   // Precondition: the value encoded by this Single must be greater or equal
0401   // than +0.0.
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     // The boundary is closer if the significand is of the form f == 2^p-1 then
0409     // the lower boundary is closer.
0410     // Think of v = 1000e10 and v- = 9999e9.
0411     // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
0412     // at a distance of 1e8.
0413     // The only exception is for the smallest normal: the largest denormal is
0414     // at the same distance as its successor.
0415     // Note: denormals have the same exponent as the smallest normals.
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 }  // namespace double_conversion
0447 }  // namespace arrow_vendored
0448 
0449 #endif  // DOUBLE_CONVERSION_DOUBLE_H_