NDDEM
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 CEREAL_RAPIDJSON_IEEE754_
16 #define CEREAL_RAPIDJSON_IEEE754_
17 
18 #include "../rapidjson.h"
19 
21 namespace internal {
22 
23 class Double {
24 public:
25  Double() {}
26  Double(double d) : d_(d) {}
27  Double(uint64_t u) : u_(u) {}
28 
29  double Value() const { return d_; }
30  uint64_t Uint64Value() const { return u_; }
31 
32  double NextPositiveDouble() const {
34  return Double(u_ + 1).Value();
35  }
36 
37  bool Sign() const { return (u_ & kSignMask) != 0; }
38  uint64_t Significand() const { return u_ & kSignificandMask; }
39  int Exponent() const { return static_cast<int>(((u_ & kExponentMask) >> kSignificandSize) - kExponentBias); }
40 
41  bool IsNan() const { return (u_ & kExponentMask) == kExponentMask && Significand() != 0; }
42  bool IsInf() const { return (u_ & kExponentMask) == kExponentMask && Significand() == 0; }
43  bool IsNanOrInf() const { return (u_ & kExponentMask) == kExponentMask; }
44  bool IsNormal() const { return (u_ & kExponentMask) != 0 || Significand() == 0; }
45  bool IsZero() const { return (u_ & (kExponentMask | kSignificandMask)) == 0; }
46 
49  uint64_t ToBias() const { return (u_ & kSignMask) ? ~u_ + 1 : u_ | kSignMask; }
50 
51  static int EffectiveSignificandSize(int order) {
52  if (order >= -1021)
53  return 53;
54  else if (order <= -1074)
55  return 0;
56  else
57  return order + 1074;
58  }
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 = CEREAL_RAPIDJSON_UINT64_C2(0x80000000, 0x00000000);
65  static const uint64_t kExponentMask = CEREAL_RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000);
66  static const uint64_t kSignificandMask = CEREAL_RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF);
67  static const uint64_t kHiddenBit = CEREAL_RAPIDJSON_UINT64_C2(0x00100000, 0x00000000);
68 
69  union {
70  double d_;
72  };
73 };
74 
75 } // namespace internal
77 
78 #endif // CEREAL_RAPIDJSON_IEEE754_
#define CEREAL_RAPIDJSON_ASSERT(x)
Definition: json.hpp:50
Definition: ieee754.h:23
uint64_t Uint64Value() const
Definition: ieee754.h:30
uint64_t IntegerSignificand() const
Definition: ieee754.h:47
Double(uint64_t u)
Definition: ieee754.h:27
static const int kSignificandSize
Definition: ieee754.h:61
bool IsNan() const
Definition: ieee754.h:41
static const uint64_t kExponentMask
Definition: ieee754.h:65
static const uint64_t kSignMask
Definition: ieee754.h:64
double Value() const
Definition: ieee754.h:29
double NextPositiveDouble() const
Definition: ieee754.h:32
static const int kExponentBias
Definition: ieee754.h:62
bool IsNormal() const
Definition: ieee754.h:44
bool IsNanOrInf() const
Definition: ieee754.h:43
static const int kDenormalExponent
Definition: ieee754.h:63
bool IsZero() const
Definition: ieee754.h:45
static const uint64_t kHiddenBit
Definition: ieee754.h:67
int IntegerExponent() const
Definition: ieee754.h:48
Double()
Definition: ieee754.h:25
double d_
Definition: ieee754.h:70
static const uint64_t kSignificandMask
Definition: ieee754.h:66
static int EffectiveSignificandSize(int order)
Definition: ieee754.h:51
bool Sign() const
Definition: ieee754.h:37
uint64_t ToBias() const
Definition: ieee754.h:49
Double(double d)
Definition: ieee754.h:26
uint64_t u_
Definition: ieee754.h:71
uint64_t Significand() const
Definition: ieee754.h:38
int Exponent() const
Definition: ieee754.h:39
bool IsInf() const
Definition: ieee754.h:42
#define CEREAL_RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
#define CEREAL_RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:124
uint d
Definition: document.h:416
#define CEREAL_RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:289
unsigned __int64 uint64_t
Definition: stdint.h:136