ieee754.h
Go to the documentation of this file.
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. All rights reserved.
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 
21 namespace internal
22 {
23 class Double
24 {
25 public:
27  {
28  }
29  Double(double d) : d_(d)
30  {
31  }
32  Double(uint64_t u) : u_(u)
33  {
34  }
35 
36  double Value() const
37  {
38  return d_;
39  }
41  {
42  return u_;
43  }
44 
45  double NextPositiveDouble() const
46  {
48  return Double(u_ + 1).Value();
49  }
50 
51  bool Sign() const
52  {
53  return (u_ & kSignMask) != 0;
54  }
56  {
57  return u_ & kSignificandMask;
58  }
59  int Exponent() const
60  {
61  return static_cast<int>(((u_ & kExponentMask) >> kSignificandSize) - kExponentBias);
62  }
63 
64  bool IsNan() const
65  {
66  return (u_ & kExponentMask) == kExponentMask && Significand() != 0;
67  }
68  bool IsInf() const
69  {
70  return (u_ & kExponentMask) == kExponentMask && Significand() == 0;
71  }
72  bool IsNanOrInf() const
73  {
74  return (u_ & kExponentMask) == kExponentMask;
75  }
76  bool IsNormal() const
77  {
78  return (u_ & kExponentMask) != 0 || Significand() == 0;
79  }
80  bool IsZero() const
81  {
82  return (u_ & (kExponentMask | kSignificandMask)) == 0;
83  }
84 
86  {
87  return IsNormal() ? Significand() | kHiddenBit : Significand();
88  }
89  int IntegerExponent() const
90  {
92  }
93  uint64_t ToBias() const
94  {
95  return (u_ & kSignMask) ? ~u_ + 1 : u_ | kSignMask;
96  }
97 
98  static int EffectiveSignificandSize(int order)
99  {
100  if (order >= -1021)
101  return 53;
102  else if (order <= -1074)
103  return 0;
104  else
105  return order + 1074;
106  }
107 
108 private:
109  static const int kSignificandSize = 52;
110  static const int kExponentBias = 0x3FF;
111  static const int kDenormalExponent = 1 - kExponentBias;
112  static const uint64_t kSignMask = RAPIDJSON_UINT64_C2(0x80000000, 0x00000000);
113  static const uint64_t kExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000);
114  static const uint64_t kSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF);
115  static const uint64_t kHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000);
116 
117  union
118  {
119  double d_;
121  };
122 };
123 
124 } // namespace internal
126 
127 #endif // RAPIDJSON_IEEE754_
static const int kSignificandSize
Definition: ieee754.h:109
uint64_t u_
Definition: ieee754.h:120
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:416
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:294
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:126
Double(uint64_t u)
Definition: ieee754.h:32
static const uint64_t kExponentMask
Definition: ieee754.h:113
uint64_t Significand() const
Definition: ieee754.h:55
bool IsNanOrInf() const
Definition: ieee754.h:72
static int EffectiveSignificandSize(int order)
Definition: ieee754.h:98
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
static const uint64_t kSignMask
Definition: ieee754.h:112
Double(double d)
Definition: ieee754.h:29
double Value() const
Definition: ieee754.h:36
bool IsInf() const
Definition: ieee754.h:68
bool IsNormal() const
Definition: ieee754.h:76
bool Sign() const
Definition: ieee754.h:51
double NextPositiveDouble() const
Definition: ieee754.h:45
bool IsNan() const
Definition: ieee754.h:64
static const int kDenormalExponent
Definition: ieee754.h:111
bool IsZero() const
Definition: ieee754.h:80
unsigned __int64 uint64_t
Definition: stdint.h:136
uint64_t IntegerSignificand() const
Definition: ieee754.h:85
uint64_t Uint64Value() const
Definition: ieee754.h:40
int IntegerExponent() const
Definition: ieee754.h:89
static const uint64_t kSignificandMask
Definition: ieee754.h:114
static const int kExponentBias
Definition: ieee754.h:110
static const uint64_t kHiddenBit
Definition: ieee754.h:115
uint64_t ToBias() const
Definition: ieee754.h:93
int Exponent() const
Definition: ieee754.h:59


xbot_talker
Author(s): wangxiaoyun
autogenerated on Sat Oct 10 2020 03:27:53