ieee754.h Source File

ieee754.h Source File#

Composable Kernel: ieee754.h Source File
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.
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
21namespace internal {
22
23class Double
24{
25 public:
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 {
36 return Double(u_ + 1).Value();
37 }
38
39 bool Sign() const { return (u_ & kSignMask) != 0; }
40 uint64_t Significand() const { return u_ & kSignificandMask; }
41 int Exponent() const
42 {
43 return static_cast<int>(((u_ & kExponentMask) >> kSignificandSize) - kExponentBias);
44 }
45
46 bool IsNan() const { return (u_ & kExponentMask) == kExponentMask && Significand() != 0; }
47 bool IsInf() const { return (u_ & kExponentMask) == kExponentMask && Significand() == 0; }
48 bool IsNanOrInf() const { return (u_ & kExponentMask) == kExponentMask; }
49 bool IsNormal() const { return (u_ & kExponentMask) != 0 || Significand() == 0; }
50 bool IsZero() const { return (u_ & (kExponentMask | kSignificandMask)) == 0; }
51
53 {
54 return IsNormal() ? Significand() | kHiddenBit : Significand();
55 }
56 int IntegerExponent() const
57 {
58 return (IsNormal() ? Exponent() : kDenormalExponent) - kSignificandSize;
59 }
60 uint64_t ToBias() const { return (u_ & kSignMask) ? ~u_ + 1 : u_ | kSignMask; }
61
62 static int EffectiveSignificandSize(int order)
63 {
64 if(order >= -1021)
65 return 53;
66 else if(order <= -1074)
67 return 0;
68 else
69 return order + 1074;
70 }
71
72 private:
73 static const int kSignificandSize = 52;
74 static const int kExponentBias = 0x3FF;
75 static const int kDenormalExponent = 1 - kExponentBias;
76 static const uint64_t kSignMask = RAPIDJSON_UINT64_C2(0x80000000, 0x00000000);
77 static const uint64_t kExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000);
78 static const uint64_t kSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF);
79 static const uint64_t kHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000);
80
81 union
82 {
83 double d_;
85 };
86};
87
88} // namespace internal
90
91#endif // RAPIDJSON_IEEE754_
uint64_t Uint64Value() const
Definition ieee754.h:31
uint64_t IntegerSignificand() const
Definition ieee754.h:52
Double(uint64_t u)
Definition ieee754.h:28
bool IsNan() const
Definition ieee754.h:46
double Value() const
Definition ieee754.h:30
double NextPositiveDouble() const
Definition ieee754.h:33
bool IsNormal() const
Definition ieee754.h:49
bool IsNanOrInf() const
Definition ieee754.h:48
bool IsZero() const
Definition ieee754.h:50
int IntegerExponent() const
Definition ieee754.h:56
Double()
Definition ieee754.h:26
double d_
Definition ieee754.h:83
static int EffectiveSignificandSize(int order)
Definition ieee754.h:62
bool Sign() const
Definition ieee754.h:39
uint64_t ToBias() const
Definition ieee754.h:60
Double(double d)
Definition ieee754.h:27
uint64_t u_
Definition ieee754.h:84
uint64_t Significand() const
Definition ieee754.h:40
int Exponent() const
Definition ieee754.h:41
bool IsInf() const
Definition ieee754.h:47
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition rapidjson.h:451
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition rapidjson.h:121
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition rapidjson.h:124
Definition allocators.h:459
common definitions and configuration
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition rapidjson.h:326
unsigned __int64 uint64_t
Definition stdint.h:136