Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:27

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 double_conversion {
0034 
0035 // We assume that doubles and uint64_t have the same endianness.
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 // Helper functions for doubles.
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;  // Excludes the hidden bit.
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   // The value encoded by this Double must be greater or equal to +0.0.
0061   // It must not be special (infinity, or NaN).
0062   DiyFp AsDiyFp() const {
0063     DOUBLE_CONVERSION_ASSERT(Sign() > 0);
0064     DOUBLE_CONVERSION_ASSERT(!IsSpecial());
0065     return DiyFp(Significand(), Exponent());
0066   }
0067 
0068   // The value encoded by this Double must be strictly greater than 0.
0069   DiyFp AsNormalizedDiyFp() const {
0070     DOUBLE_CONVERSION_ASSERT(value() > 0.0);
0071     uint64_t f = Significand();
0072     int e = Exponent();
0073 
0074     // The current double could be a denormal.
0075     while ((f & kHiddenBit) == 0) {
0076       f <<= 1;
0077       e--;
0078     }
0079     // Do the final shifts in one go.
0080     f <<= DiyFp::kSignificandSize - kSignificandSize;
0081     e -= DiyFp::kSignificandSize - kSignificandSize;
0082     return DiyFp(f, e);
0083   }
0084 
0085   // Returns the double's bit as uint64.
0086   uint64_t AsUint64() const {
0087     return d64_;
0088   }
0089 
0090   // Returns the next greater double. Returns +infinity on input +infinity.
0091   double NextDouble() const {
0092     if (d64_ == kInfinity) return Double(kInfinity).value();
0093     if (Sign() < 0 && Significand() == 0) {
0094       // -0.0
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   // Returns true if the double is a denormal.
0134   bool IsDenormal() const {
0135     uint64_t d64 = AsUint64();
0136     return (d64 & kExponentMask) == 0;
0137   }
0138 
0139   // We consider denormals not to be special.
0140   // Hence only Infinity and NaN are special.
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   // Precondition: the value encoded by this Double must be greater or equal
0181   // than +0.0.
0182   DiyFp UpperBoundary() const {
0183     DOUBLE_CONVERSION_ASSERT(Sign() > 0);
0184     return DiyFp(Significand() * 2 + 1, Exponent() - 1);
0185   }
0186 
0187   // Computes the two boundaries of this.
0188   // The bigger boundary (m_plus) is normalized. The lower boundary has the same
0189   // exponent as m_plus.
0190   // Precondition: the value encoded by this Double must be greater than 0.
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     // The boundary is closer if the significand is of the form f == 2^p-1 then
0209     // the lower boundary is closer.
0210     // Think of v = 1000e10 and v- = 9999e9.
0211     // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
0212     // at a distance of 1e8.
0213     // The only exception is for the smallest normal: the largest denormal is
0214     // at the same distance as its successor.
0215     // Note: denormals have the same exponent as the smallest normals.
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   // Returns the significand size for a given order of magnitude.
0223   // If v = f*2^e with 2^p-1 <= f <= 2^p then p+e is v's order of magnitude.
0224   // This function returns the number of significant binary digits v will have
0225   // once it's encoded into a double. In almost all cases this is equal to
0226   // kSignificandSize. The only exceptions are denormals. They start with
0227   // leading zeroes and their effective significand-size is hence smaller.
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;  // Excludes the hidden bit.
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   // The value encoded by this Single must be greater or equal to +0.0.
0301   // It must not be special (infinity, or NaN).
0302   DiyFp AsDiyFp() const {
0303     DOUBLE_CONVERSION_ASSERT(Sign() > 0);
0304     DOUBLE_CONVERSION_ASSERT(!IsSpecial());
0305     return DiyFp(Significand(), Exponent());
0306   }
0307 
0308   // Returns the single's bit as uint64.
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   // Returns true if the single is a denormal.
0333   bool IsDenormal() const {
0334     uint32_t d32 = AsUint32();
0335     return (d32 & kExponentMask) == 0;
0336   }
0337 
0338   // We consider denormals not to be special.
0339   // Hence only Infinity and NaN are special.
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   // Computes the two boundaries of this.
0380   // The bigger boundary (m_plus) is normalized. The lower boundary has the same
0381   // exponent as m_plus.
0382   // Precondition: the value encoded by this Single must be greater than 0.
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   // Precondition: the value encoded by this Single must be greater or equal
0400   // than +0.0.
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     // The boundary is closer if the significand is of the form f == 2^p-1 then
0408     // the lower boundary is closer.
0409     // Think of v = 1000e10 and v- = 9999e9.
0410     // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
0411     // at a distance of 1e8.
0412     // The only exception is for the smallest normal: the largest denormal is
0413     // at the same distance as its successor.
0414     // Note: denormals have the same exponent as the smallest normals.
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 }  // namespace double_conversion
0446 
0447 #endif  // DOUBLE_CONVERSION_DOUBLE_H_