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