webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
ieee754.h
Go to the documentation of this file.
1 // Copyright (C) 2011 Milo Yip
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef RAPIDJSON_IEEE754_
22 #define RAPIDJSON_IEEE754_
23 
24 #include "../rapidjson.h"
25 
27 namespace internal {
28 
29 class Double {
30 public:
31  Double() {}
32  Double(double d) : d(d) {}
33  Double(uint64_t u) : u(u) {}
34 
35  double Value() const { return d; }
36  uint64_t Uint64Value() const { return u; }
37 
38  double NextPositiveDouble() const {
40  return Double(u + 1).Value();
41  }
42 
43  double PreviousPositiveDouble() const {
45  if (d == 0.0)
46  return 0.0;
47  else
48  return Double(u - 1).Value();
49  }
50 
51  bool Sign() const { return (u & kSignMask) != 0; }
52  uint64_t Significand() const { return u & kSignificandMask; }
53  int Exponent() const { return ((u & kExponentMask) >> kSignificandSize) - kExponentBias; }
54 
55  bool IsNan() const { return (u & kExponentMask) == kExponentMask && Significand() != 0; }
56  bool IsInf() const { return (u & kExponentMask) == kExponentMask && Significand() == 0; }
57  bool IsNormal() const { return (u & kExponentMask) != 0 || Significand() == 0; }
58 
59  uint64_t IntegerSignificand() const { return IsNormal() ? Significand() | kHiddenBit : Significand(); }
60  int IntegerExponent() const { return (IsNormal() ? Exponent() : kDenormalExponent) - kSignificandSize; }
61  uint64_t ToBias() const { return (u & kSignMask) ? ~u + 1 : u | kSignMask; }
62 
63  static unsigned EffectiveSignificandSize(int order) {
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  double d;
84  };
85 };
86 
87 } // namespace internal
89 
90 #endif // RAPIDJSON_IEEE754_
Double(double d)
Definition: ieee754.h:32
double PreviousPositiveDouble() const
Definition: ieee754.h:43
uint64_t Significand() const
Definition: ieee754.h:52
double Value() const
Definition: ieee754.h:35
uint64_t ToBias() const
Definition: ieee754.h:61
unsigned long long uint64_t
Definition: ptypes.h:120
uint64_t Uint64Value() const
Definition: ieee754.h:36
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:232
bool IsNormal() const
Definition: ieee754.h:57
Definition: ieee754.h:29
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:91
bool IsNan() const
Definition: ieee754.h:55
uint64_t u
Definition: ieee754.h:83
Double(uint64_t u)
Definition: ieee754.h:33
double NextPositiveDouble() const
Definition: ieee754.h:38
int IntegerExponent() const
Definition: ieee754.h:60
bool IsInf() const
Definition: ieee754.h:56
double d
Definition: ieee754.h:82
bool Sign() const
Definition: ieee754.h:51
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:94
Double()
Definition: ieee754.h:31
Definition: document.h:393
static unsigned EffectiveSignificandSize(int order)
Definition: ieee754.h:63
int Exponent() const
Definition: ieee754.h:53
uint64_t IntegerSignificand() const
Definition: ieee754.h:59
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:315