DeepCover Embedded Security in IoT: Public-key Secured Data Paths

Dependencies:   MaximInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ieee754.h Source File

ieee754.h

00001 // Tencent is pleased to support the open source community by making RapidJSON available.
00002 // 
00003 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
00004 //
00005 // Licensed under the MIT License (the "License"); you may not use this file except
00006 // in compliance with the License. You may obtain a copy of the License at
00007 //
00008 // http://opensource.org/licenses/MIT
00009 //
00010 // Unless required by applicable law or agreed to in writing, software distributed 
00011 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
00012 // CONDITIONS OF ANY KIND, either express or implied. See the License for the 
00013 // specific language governing permissions and limitations under the License.
00014 
00015 #ifndef RAPIDJSON_IEEE754_
00016 #define RAPIDJSON_IEEE754_
00017 
00018 #include "../rapidjson.h"
00019 
00020 RAPIDJSON_NAMESPACE_BEGIN
00021 namespace internal {
00022 
00023 class Double {
00024 public:
00025     Double() {}
00026     Double(double d) : d_(d) {}
00027     Double(uint64_t u) : u_(u) {}
00028 
00029     double Value() const { return d_; }
00030     uint64_t Uint64Value() const { return u_; }
00031 
00032     double NextPositiveDouble() const {
00033         RAPIDJSON_ASSERT(!Sign());
00034         return Double(u_ + 1).Value();
00035     }
00036 
00037     bool Sign() const { return (u_ & kSignMask) != 0; }
00038     uint64_t Significand() const { return u_ & kSignificandMask; }
00039     int Exponent() const { return static_cast<int>(((u_ & kExponentMask) >> kSignificandSize) - kExponentBias); }
00040 
00041     bool IsNan() const { return (u_ & kExponentMask) == kExponentMask && Significand() != 0; }
00042     bool IsInf() const { return (u_ & kExponentMask) == kExponentMask && Significand() == 0; }
00043     bool IsNanOrInf() const { return (u_ & kExponentMask) == kExponentMask; }
00044     bool IsNormal() const { return (u_ & kExponentMask) != 0 || Significand() == 0; }
00045     bool IsZero() const { return (u_ & (kExponentMask | kSignificandMask)) == 0; }
00046 
00047     uint64_t IntegerSignificand() const { return IsNormal() ? Significand() | kHiddenBit : Significand(); }
00048     int IntegerExponent() const { return (IsNormal() ? Exponent() : kDenormalExponent) - kSignificandSize; }
00049     uint64_t ToBias() const { return (u_ & kSignMask) ? ~u_ + 1 : u_ | kSignMask; }
00050 
00051     static unsigned EffectiveSignificandSize(int order) {
00052         if (order >= -1021)
00053             return 53;
00054         else if (order <= -1074)
00055             return 0;
00056         else
00057             return static_cast<unsigned>(order) + 1074;
00058     }
00059 
00060 private:
00061     static const int kSignificandSize = 52;
00062     static const int kExponentBias = 0x3FF;
00063     static const int kDenormalExponent = 1 - kExponentBias;
00064     static const uint64_t kSignMask = RAPIDJSON_UINT64_C2(0x80000000, 0x00000000);
00065     static const uint64_t kExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000);
00066     static const uint64_t kSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF);
00067     static const uint64_t kHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000);
00068 
00069     union {
00070         double d_;
00071         uint64_t u_;
00072     };
00073 };
00074 
00075 } // namespace internal
00076 RAPIDJSON_NAMESPACE_END
00077 
00078 #endif // RAPIDJSON_IEEE754_