ieee754.h

Engine/source/persistence/rapidjson/internal/ieee754.h

More...

Classes:

Namespaces:

namespace

Detailed Description

 1
 2// Tencent is pleased to support the open source community by making RapidJSON available.
 3// 
 4// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
 5//
 6// Licensed under the MIT License (the "License"); you may not use this file except
 7// in compliance with the License. You may obtain a copy of the License at
 8//
 9// http://opensource.org/licenses/MIT
10//
11// Unless required by applicable law or agreed to in writing, software distributed 
12// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
13// CONDITIONS OF ANY KIND, either express or implied. See the License for the 
14// specific language governing permissions and limitations under the License.
15
16#ifndef RAPIDJSON_IEEE754_
17#define RAPIDJSON_IEEE754_
18
19#include "../rapidjson.h"
20
21RAPIDJSON_NAMESPACE_BEGIN
22namespace internal {
23
24class Double {
25public:
26    Double() {}
27    Double(double d) : d_(d) {}
28    Double(uint64_t u) : u_(u) {}
29
30    double Value() const { return d_; }
31    uint64_t Uint64Value() const { return u_; }
32
33    double NextPositiveDouble() const {
34        RAPIDJSON_ASSERT(!Sign());
35        return Double(u_ + 1).Value();
36    }
37
38    bool Sign() const { return (u_ & kSignMask) != 0; }
39    uint64_t Significand() const { return u_ & kSignificandMask; }
40    int Exponent() const { return static_cast<int>(((u_ & kExponentMask) >> kSignificandSize) - kExponentBias); }
41
42    bool IsNan() const { return (u_ & kExponentMask) == kExponentMask && Significand() != 0; }
43    bool IsInf() const { return (u_ & kExponentMask) == kExponentMask && Significand() == 0; }
44    bool IsNanOrInf() const { return (u_ & kExponentMask) == kExponentMask; }
45    bool IsNormal() const { return (u_ & kExponentMask) != 0 || Significand() == 0; }
46    bool IsZero() const { return (u_ & (kExponentMask | kSignificandMask)) == 0; }
47
48    uint64_t IntegerSignificand() const { return IsNormal() ? Significand() | kHiddenBit : Significand(); }
49    int IntegerExponent() const { return (IsNormal() ? Exponent() : kDenormalExponent) - kSignificandSize; }
50    uint64_t ToBias() const { return (u_ & kSignMask) ? ~<a href="/coding/class/classinternal_1_1double/#classinternal_1_1double_1ad7523f7fe0c47d6aabe34f68b00a3250">u_</a> + 1 : u_ | kSignMask; }
51
52    static int EffectiveSignificandSize(int order) {
53        if (order >= -1021)
54            return 53;
55        else if (order <= -1074)
56            return 0;
57        else
58            return order + 1074;
59    }
60
61private:
62    static const int kSignificandSize = 52;
63    static const int kExponentBias = 0x3FF;
64    static const int kDenormalExponent = 1 - kExponentBias;
65    static const uint64_t kSignMask = RAPIDJSON_UINT64_C2(0x80000000, 0x00000000);
66    static const uint64_t kExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000);
67    static const uint64_t kSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF);
68    static const uint64_t kHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000);
69
70    union {
71        double d_;
72        uint64_t u_;
73    };
74};
75
76} // namespace internal
77RAPIDJSON_NAMESPACE_END
78
79#endif // RAPIDJSON_IEEE754_
80