| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#ifndef CEREAL_RAPIDJSON_IEEE754_ |
| 16 |
#define CEREAL_RAPIDJSON_IEEE754_ |
| 17 |
|
| 18 |
#include "../rapidjson.h" |
| 19 |
|
| 20 |
CEREAL_RAPIDJSON_NAMESPACE_BEGIN |
| 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 { |
| 33 |
CEREAL_RAPIDJSON_ASSERT(!Sign()); |
| 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 |
|
| 47 |
uint64_t IntegerSignificand() const { return IsNormal() ? Significand() | kHiddenBit : Significand(); } |
| 48 |
int IntegerExponent() const { return (IsNormal() ? Exponent() : kDenormalExponent) - kSignificandSize; } |
| 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_; |
| 71 |
uint64_t u_; |
| 72 |
}; |
| 73 |
}; |
| 74 |
|
| 75 |
} |
| 76 |
CEREAL_RAPIDJSON_NAMESPACE_END |
| 77 |
|
| 78 |
#endif |