Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:28:08

0001 // Tencent is pleased to support the open source community by making RapidJSON available.
0002 // 
0003 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
0004 //
0005 // Licensed under the MIT License (the "License"); you may not use this file except
0006 // in compliance with the License. You may obtain a copy of the License at
0007 //
0008 // http://opensource.org/licenses/MIT
0009 //
0010 // Unless required by applicable law or agreed to in writing, software distributed 
0011 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
0012 // CONDITIONS OF ANY KIND, either express or implied. See the License for the 
0013 // specific language governing permissions and limitations under the License.
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 } // namespace internal
0076 RAPIDJSON_NAMESPACE_END
0077 
0078 #endif // RAPIDJSON_IEEE754_