File indexing completed on 2025-12-16 10:28:08
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef RAPIDJSON_IEEE754_
0016 #define RAPIDJSON_IEEE754_
0017
0018 #include "../rapidjson.h"
0019
0020 RAPIDJSON_NAMESPACE_BEGIN
0021 namespace internal {
0022
0023 class Double {
0024 public:
0025 Double() {}
0026 Double(double d) : d_(d) {}
0027 Double(uint64_t u) : u_(u) {}
0028
0029 double Value() const { return d_; }
0030 uint64_t Uint64Value() const { return u_; }
0031
0032 double NextPositiveDouble() const {
0033 RAPIDJSON_ASSERT(!Sign());
0034 return Double(u_ + 1).Value();
0035 }
0036
0037 bool Sign() const { return (u_ & kSignMask) != 0; }
0038 uint64_t Significand() const { return u_ & kSignificandMask; }
0039 int Exponent() const { return static_cast<int>(((u_ & kExponentMask) >> kSignificandSize) - kExponentBias); }
0040
0041 bool IsNan() const { return (u_ & kExponentMask) == kExponentMask && Significand() != 0; }
0042 bool IsInf() const { return (u_ & kExponentMask) == kExponentMask && Significand() == 0; }
0043 bool IsNanOrInf() const { return (u_ & kExponentMask) == kExponentMask; }
0044 bool IsNormal() const { return (u_ & kExponentMask) != 0 || Significand() == 0; }
0045 bool IsZero() const { return (u_ & (kExponentMask | kSignificandMask)) == 0; }
0046
0047 uint64_t IntegerSignificand() const { return IsNormal() ? Significand() | kHiddenBit : Significand(); }
0048 int IntegerExponent() const { return (IsNormal() ? Exponent() : kDenormalExponent) - kSignificandSize; }
0049 uint64_t ToBias() const { return (u_ & kSignMask) ? ~u_ + 1 : u_ | kSignMask; }
0050
0051 static int EffectiveSignificandSize(int order) {
0052 if (order >= -1021)
0053 return 53;
0054 else if (order <= -1074)
0055 return 0;
0056 else
0057 return order + 1074;
0058 }
0059
0060 private:
0061 static const int kSignificandSize = 52;
0062 static const int kExponentBias = 0x3FF;
0063 static const int kDenormalExponent = 1 - kExponentBias;
0064 static const uint64_t kSignMask = RAPIDJSON_UINT64_C2(0x80000000, 0x00000000);
0065 static const uint64_t kExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000);
0066 static const uint64_t kSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF);
0067 static const uint64_t kHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000);
0068
0069 union {
0070 double d_;
0071 uint64_t u_;
0072 };
0073 };
0074
0075 }
0076 RAPIDJSON_NAMESPACE_END
0077
0078 #endif