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

Dependencies:   MaximInterface

The MAXREFDES155# is an internet-of-things (IoT) embedded-security reference design, built to authenticate and control a sensing node using elliptic-curve-based public-key cryptography with control and notification from a web server.

The hardware includes an ARM® mbed™ shield and attached sensor endpoint. The shield contains a DS2476 DeepCover® ECDSA/SHA-2 coprocessor, Wifi communication, LCD push-button controls, and status LEDs. The sensor endpoint is attached to the shield using a 300mm cable and contains a DS28C36 DeepCover ECDSA/SHA-2 authenticator, IR-thermal sensor, and aiming laser for the IR sensor. The MAXREFDES155# is equipped with a standard Arduino® form-factor shield connector for immediate testing using an mbed board such as the MAX32600MBED#. The combination of these two devices represent an IoT device. Communication to the web server is accomplished with the shield Wifi circuitry. Communication from the shield to the attached sensor module is accomplished over I2C . The sensor module represents an IoT endpoint that generates small data with a requirement for message authenticity/integrity and secure on/off operational control.

The design is hierarchical with each mbed platform and shield communicating data from the sensor node to a web server that maintains a centralized log and dispatches notifications as necessary. The simplicity of this design enables rapid integration into any star-topology IoT network to provide security with the low overhead and cost provided by the ECDSA-P256 asymmetric-key and SHA-256 symmetric-key algorithms.

More information about the MAXREFDES155# is available on the Maxim Integrated website.

Committer:
IanBenzMaxim
Date:
Fri Feb 24 11:23:12 2017 -0600
Revision:
0:33d4e66780c0
Initial commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 0:33d4e66780c0 1 // Tencent is pleased to support the open source community by making RapidJSON available.
IanBenzMaxim 0:33d4e66780c0 2 //
IanBenzMaxim 0:33d4e66780c0 3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
IanBenzMaxim 0:33d4e66780c0 4 //
IanBenzMaxim 0:33d4e66780c0 5 // Licensed under the MIT License (the "License"); you may not use this file except
IanBenzMaxim 0:33d4e66780c0 6 // in compliance with the License. You may obtain a copy of the License at
IanBenzMaxim 0:33d4e66780c0 7 //
IanBenzMaxim 0:33d4e66780c0 8 // http://opensource.org/licenses/MIT
IanBenzMaxim 0:33d4e66780c0 9 //
IanBenzMaxim 0:33d4e66780c0 10 // Unless required by applicable law or agreed to in writing, software distributed
IanBenzMaxim 0:33d4e66780c0 11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
IanBenzMaxim 0:33d4e66780c0 12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
IanBenzMaxim 0:33d4e66780c0 13 // specific language governing permissions and limitations under the License.
IanBenzMaxim 0:33d4e66780c0 14
IanBenzMaxim 0:33d4e66780c0 15 // This is a C++ header-only implementation of Grisu2 algorithm from the publication:
IanBenzMaxim 0:33d4e66780c0 16 // Loitsch, Florian. "Printing floating-point numbers quickly and accurately with
IanBenzMaxim 0:33d4e66780c0 17 // integers." ACM Sigplan Notices 45.6 (2010): 233-243.
IanBenzMaxim 0:33d4e66780c0 18
IanBenzMaxim 0:33d4e66780c0 19 #ifndef RAPIDJSON_DIYFP_H_
IanBenzMaxim 0:33d4e66780c0 20 #define RAPIDJSON_DIYFP_H_
IanBenzMaxim 0:33d4e66780c0 21
IanBenzMaxim 0:33d4e66780c0 22 #include "../rapidjson.h"
IanBenzMaxim 0:33d4e66780c0 23
IanBenzMaxim 0:33d4e66780c0 24 #if defined(_MSC_VER) && defined(_M_AMD64)
IanBenzMaxim 0:33d4e66780c0 25 #include <intrin.h>
IanBenzMaxim 0:33d4e66780c0 26 #pragma intrinsic(_BitScanReverse64)
IanBenzMaxim 0:33d4e66780c0 27 #pragma intrinsic(_umul128)
IanBenzMaxim 0:33d4e66780c0 28 #endif
IanBenzMaxim 0:33d4e66780c0 29
IanBenzMaxim 0:33d4e66780c0 30 RAPIDJSON_NAMESPACE_BEGIN
IanBenzMaxim 0:33d4e66780c0 31 namespace internal {
IanBenzMaxim 0:33d4e66780c0 32
IanBenzMaxim 0:33d4e66780c0 33 #ifdef __GNUC__
IanBenzMaxim 0:33d4e66780c0 34 RAPIDJSON_DIAG_PUSH
IanBenzMaxim 0:33d4e66780c0 35 RAPIDJSON_DIAG_OFF(effc++)
IanBenzMaxim 0:33d4e66780c0 36 #endif
IanBenzMaxim 0:33d4e66780c0 37
IanBenzMaxim 0:33d4e66780c0 38 #ifdef __clang__
IanBenzMaxim 0:33d4e66780c0 39 RAPIDJSON_DIAG_PUSH
IanBenzMaxim 0:33d4e66780c0 40 RAPIDJSON_DIAG_OFF(padded)
IanBenzMaxim 0:33d4e66780c0 41 #endif
IanBenzMaxim 0:33d4e66780c0 42
IanBenzMaxim 0:33d4e66780c0 43 struct DiyFp {
IanBenzMaxim 0:33d4e66780c0 44 DiyFp() : f(), e() {}
IanBenzMaxim 0:33d4e66780c0 45
IanBenzMaxim 0:33d4e66780c0 46 DiyFp(uint64_t fp, int exp) : f(fp), e(exp) {}
IanBenzMaxim 0:33d4e66780c0 47
IanBenzMaxim 0:33d4e66780c0 48 explicit DiyFp(double d) {
IanBenzMaxim 0:33d4e66780c0 49 union {
IanBenzMaxim 0:33d4e66780c0 50 double d;
IanBenzMaxim 0:33d4e66780c0 51 uint64_t u64;
IanBenzMaxim 0:33d4e66780c0 52 } u = { d };
IanBenzMaxim 0:33d4e66780c0 53
IanBenzMaxim 0:33d4e66780c0 54 int biased_e = static_cast<int>((u.u64 & kDpExponentMask) >> kDpSignificandSize);
IanBenzMaxim 0:33d4e66780c0 55 uint64_t significand = (u.u64 & kDpSignificandMask);
IanBenzMaxim 0:33d4e66780c0 56 if (biased_e != 0) {
IanBenzMaxim 0:33d4e66780c0 57 f = significand + kDpHiddenBit;
IanBenzMaxim 0:33d4e66780c0 58 e = biased_e - kDpExponentBias;
IanBenzMaxim 0:33d4e66780c0 59 }
IanBenzMaxim 0:33d4e66780c0 60 else {
IanBenzMaxim 0:33d4e66780c0 61 f = significand;
IanBenzMaxim 0:33d4e66780c0 62 e = kDpMinExponent + 1;
IanBenzMaxim 0:33d4e66780c0 63 }
IanBenzMaxim 0:33d4e66780c0 64 }
IanBenzMaxim 0:33d4e66780c0 65
IanBenzMaxim 0:33d4e66780c0 66 DiyFp operator-(const DiyFp& rhs) const {
IanBenzMaxim 0:33d4e66780c0 67 return DiyFp(f - rhs.f, e);
IanBenzMaxim 0:33d4e66780c0 68 }
IanBenzMaxim 0:33d4e66780c0 69
IanBenzMaxim 0:33d4e66780c0 70 DiyFp operator*(const DiyFp& rhs) const {
IanBenzMaxim 0:33d4e66780c0 71 #if defined(_MSC_VER) && defined(_M_AMD64)
IanBenzMaxim 0:33d4e66780c0 72 uint64_t h;
IanBenzMaxim 0:33d4e66780c0 73 uint64_t l = _umul128(f, rhs.f, &h);
IanBenzMaxim 0:33d4e66780c0 74 if (l & (uint64_t(1) << 63)) // rounding
IanBenzMaxim 0:33d4e66780c0 75 h++;
IanBenzMaxim 0:33d4e66780c0 76 return DiyFp(h, e + rhs.e + 64);
IanBenzMaxim 0:33d4e66780c0 77 #elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__)
IanBenzMaxim 0:33d4e66780c0 78 __extension__ typedef unsigned __int128 uint128;
IanBenzMaxim 0:33d4e66780c0 79 uint128 p = static_cast<uint128>(f) * static_cast<uint128>(rhs.f);
IanBenzMaxim 0:33d4e66780c0 80 uint64_t h = static_cast<uint64_t>(p >> 64);
IanBenzMaxim 0:33d4e66780c0 81 uint64_t l = static_cast<uint64_t>(p);
IanBenzMaxim 0:33d4e66780c0 82 if (l & (uint64_t(1) << 63)) // rounding
IanBenzMaxim 0:33d4e66780c0 83 h++;
IanBenzMaxim 0:33d4e66780c0 84 return DiyFp(h, e + rhs.e + 64);
IanBenzMaxim 0:33d4e66780c0 85 #else
IanBenzMaxim 0:33d4e66780c0 86 const uint64_t M32 = 0xFFFFFFFF;
IanBenzMaxim 0:33d4e66780c0 87 const uint64_t a = f >> 32;
IanBenzMaxim 0:33d4e66780c0 88 const uint64_t b = f & M32;
IanBenzMaxim 0:33d4e66780c0 89 const uint64_t c = rhs.f >> 32;
IanBenzMaxim 0:33d4e66780c0 90 const uint64_t d = rhs.f & M32;
IanBenzMaxim 0:33d4e66780c0 91 const uint64_t ac = a * c;
IanBenzMaxim 0:33d4e66780c0 92 const uint64_t bc = b * c;
IanBenzMaxim 0:33d4e66780c0 93 const uint64_t ad = a * d;
IanBenzMaxim 0:33d4e66780c0 94 const uint64_t bd = b * d;
IanBenzMaxim 0:33d4e66780c0 95 uint64_t tmp = (bd >> 32) + (ad & M32) + (bc & M32);
IanBenzMaxim 0:33d4e66780c0 96 tmp += 1U << 31; /// mult_round
IanBenzMaxim 0:33d4e66780c0 97 return DiyFp(ac + (ad >> 32) + (bc >> 32) + (tmp >> 32), e + rhs.e + 64);
IanBenzMaxim 0:33d4e66780c0 98 #endif
IanBenzMaxim 0:33d4e66780c0 99 }
IanBenzMaxim 0:33d4e66780c0 100
IanBenzMaxim 0:33d4e66780c0 101 DiyFp Normalize() const {
IanBenzMaxim 0:33d4e66780c0 102 #if defined(_MSC_VER) && defined(_M_AMD64)
IanBenzMaxim 0:33d4e66780c0 103 unsigned long index;
IanBenzMaxim 0:33d4e66780c0 104 _BitScanReverse64(&index, f);
IanBenzMaxim 0:33d4e66780c0 105 return DiyFp(f << (63 - index), e - (63 - index));
IanBenzMaxim 0:33d4e66780c0 106 #elif defined(__GNUC__) && __GNUC__ >= 4
IanBenzMaxim 0:33d4e66780c0 107 int s = __builtin_clzll(f);
IanBenzMaxim 0:33d4e66780c0 108 return DiyFp(f << s, e - s);
IanBenzMaxim 0:33d4e66780c0 109 #else
IanBenzMaxim 0:33d4e66780c0 110 DiyFp res = *this;
IanBenzMaxim 0:33d4e66780c0 111 while (!(res.f & (static_cast<uint64_t>(1) << 63))) {
IanBenzMaxim 0:33d4e66780c0 112 res.f <<= 1;
IanBenzMaxim 0:33d4e66780c0 113 res.e--;
IanBenzMaxim 0:33d4e66780c0 114 }
IanBenzMaxim 0:33d4e66780c0 115 return res;
IanBenzMaxim 0:33d4e66780c0 116 #endif
IanBenzMaxim 0:33d4e66780c0 117 }
IanBenzMaxim 0:33d4e66780c0 118
IanBenzMaxim 0:33d4e66780c0 119 DiyFp NormalizeBoundary() const {
IanBenzMaxim 0:33d4e66780c0 120 DiyFp res = *this;
IanBenzMaxim 0:33d4e66780c0 121 while (!(res.f & (kDpHiddenBit << 1))) {
IanBenzMaxim 0:33d4e66780c0 122 res.f <<= 1;
IanBenzMaxim 0:33d4e66780c0 123 res.e--;
IanBenzMaxim 0:33d4e66780c0 124 }
IanBenzMaxim 0:33d4e66780c0 125 res.f <<= (kDiySignificandSize - kDpSignificandSize - 2);
IanBenzMaxim 0:33d4e66780c0 126 res.e = res.e - (kDiySignificandSize - kDpSignificandSize - 2);
IanBenzMaxim 0:33d4e66780c0 127 return res;
IanBenzMaxim 0:33d4e66780c0 128 }
IanBenzMaxim 0:33d4e66780c0 129
IanBenzMaxim 0:33d4e66780c0 130 void NormalizedBoundaries(DiyFp* minus, DiyFp* plus) const {
IanBenzMaxim 0:33d4e66780c0 131 DiyFp pl = DiyFp((f << 1) + 1, e - 1).NormalizeBoundary();
IanBenzMaxim 0:33d4e66780c0 132 DiyFp mi = (f == kDpHiddenBit) ? DiyFp((f << 2) - 1, e - 2) : DiyFp((f << 1) - 1, e - 1);
IanBenzMaxim 0:33d4e66780c0 133 mi.f <<= mi.e - pl.e;
IanBenzMaxim 0:33d4e66780c0 134 mi.e = pl.e;
IanBenzMaxim 0:33d4e66780c0 135 *plus = pl;
IanBenzMaxim 0:33d4e66780c0 136 *minus = mi;
IanBenzMaxim 0:33d4e66780c0 137 }
IanBenzMaxim 0:33d4e66780c0 138
IanBenzMaxim 0:33d4e66780c0 139 double ToDouble() const {
IanBenzMaxim 0:33d4e66780c0 140 union {
IanBenzMaxim 0:33d4e66780c0 141 double d;
IanBenzMaxim 0:33d4e66780c0 142 uint64_t u64;
IanBenzMaxim 0:33d4e66780c0 143 }u;
IanBenzMaxim 0:33d4e66780c0 144 const uint64_t be = (e == kDpDenormalExponent && (f & kDpHiddenBit) == 0) ? 0 :
IanBenzMaxim 0:33d4e66780c0 145 static_cast<uint64_t>(e + kDpExponentBias);
IanBenzMaxim 0:33d4e66780c0 146 u.u64 = (f & kDpSignificandMask) | (be << kDpSignificandSize);
IanBenzMaxim 0:33d4e66780c0 147 return u.d;
IanBenzMaxim 0:33d4e66780c0 148 }
IanBenzMaxim 0:33d4e66780c0 149
IanBenzMaxim 0:33d4e66780c0 150 static const int kDiySignificandSize = 64;
IanBenzMaxim 0:33d4e66780c0 151 static const int kDpSignificandSize = 52;
IanBenzMaxim 0:33d4e66780c0 152 static const int kDpExponentBias = 0x3FF + kDpSignificandSize;
IanBenzMaxim 0:33d4e66780c0 153 static const int kDpMaxExponent = 0x7FF - kDpExponentBias;
IanBenzMaxim 0:33d4e66780c0 154 static const int kDpMinExponent = -kDpExponentBias;
IanBenzMaxim 0:33d4e66780c0 155 static const int kDpDenormalExponent = -kDpExponentBias + 1;
IanBenzMaxim 0:33d4e66780c0 156 static const uint64_t kDpExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000);
IanBenzMaxim 0:33d4e66780c0 157 static const uint64_t kDpSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF);
IanBenzMaxim 0:33d4e66780c0 158 static const uint64_t kDpHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000);
IanBenzMaxim 0:33d4e66780c0 159
IanBenzMaxim 0:33d4e66780c0 160 uint64_t f;
IanBenzMaxim 0:33d4e66780c0 161 int e;
IanBenzMaxim 0:33d4e66780c0 162 };
IanBenzMaxim 0:33d4e66780c0 163
IanBenzMaxim 0:33d4e66780c0 164 inline DiyFp GetCachedPowerByIndex(size_t index) {
IanBenzMaxim 0:33d4e66780c0 165 // 10^-348, 10^-340, ..., 10^340
IanBenzMaxim 0:33d4e66780c0 166 static const uint64_t kCachedPowers_F[] = {
IanBenzMaxim 0:33d4e66780c0 167 RAPIDJSON_UINT64_C2(0xfa8fd5a0, 0x081c0288), RAPIDJSON_UINT64_C2(0xbaaee17f, 0xa23ebf76),
IanBenzMaxim 0:33d4e66780c0 168 RAPIDJSON_UINT64_C2(0x8b16fb20, 0x3055ac76), RAPIDJSON_UINT64_C2(0xcf42894a, 0x5dce35ea),
IanBenzMaxim 0:33d4e66780c0 169 RAPIDJSON_UINT64_C2(0x9a6bb0aa, 0x55653b2d), RAPIDJSON_UINT64_C2(0xe61acf03, 0x3d1a45df),
IanBenzMaxim 0:33d4e66780c0 170 RAPIDJSON_UINT64_C2(0xab70fe17, 0xc79ac6ca), RAPIDJSON_UINT64_C2(0xff77b1fc, 0xbebcdc4f),
IanBenzMaxim 0:33d4e66780c0 171 RAPIDJSON_UINT64_C2(0xbe5691ef, 0x416bd60c), RAPIDJSON_UINT64_C2(0x8dd01fad, 0x907ffc3c),
IanBenzMaxim 0:33d4e66780c0 172 RAPIDJSON_UINT64_C2(0xd3515c28, 0x31559a83), RAPIDJSON_UINT64_C2(0x9d71ac8f, 0xada6c9b5),
IanBenzMaxim 0:33d4e66780c0 173 RAPIDJSON_UINT64_C2(0xea9c2277, 0x23ee8bcb), RAPIDJSON_UINT64_C2(0xaecc4991, 0x4078536d),
IanBenzMaxim 0:33d4e66780c0 174 RAPIDJSON_UINT64_C2(0x823c1279, 0x5db6ce57), RAPIDJSON_UINT64_C2(0xc2109436, 0x4dfb5637),
IanBenzMaxim 0:33d4e66780c0 175 RAPIDJSON_UINT64_C2(0x9096ea6f, 0x3848984f), RAPIDJSON_UINT64_C2(0xd77485cb, 0x25823ac7),
IanBenzMaxim 0:33d4e66780c0 176 RAPIDJSON_UINT64_C2(0xa086cfcd, 0x97bf97f4), RAPIDJSON_UINT64_C2(0xef340a98, 0x172aace5),
IanBenzMaxim 0:33d4e66780c0 177 RAPIDJSON_UINT64_C2(0xb23867fb, 0x2a35b28e), RAPIDJSON_UINT64_C2(0x84c8d4df, 0xd2c63f3b),
IanBenzMaxim 0:33d4e66780c0 178 RAPIDJSON_UINT64_C2(0xc5dd4427, 0x1ad3cdba), RAPIDJSON_UINT64_C2(0x936b9fce, 0xbb25c996),
IanBenzMaxim 0:33d4e66780c0 179 RAPIDJSON_UINT64_C2(0xdbac6c24, 0x7d62a584), RAPIDJSON_UINT64_C2(0xa3ab6658, 0x0d5fdaf6),
IanBenzMaxim 0:33d4e66780c0 180 RAPIDJSON_UINT64_C2(0xf3e2f893, 0xdec3f126), RAPIDJSON_UINT64_C2(0xb5b5ada8, 0xaaff80b8),
IanBenzMaxim 0:33d4e66780c0 181 RAPIDJSON_UINT64_C2(0x87625f05, 0x6c7c4a8b), RAPIDJSON_UINT64_C2(0xc9bcff60, 0x34c13053),
IanBenzMaxim 0:33d4e66780c0 182 RAPIDJSON_UINT64_C2(0x964e858c, 0x91ba2655), RAPIDJSON_UINT64_C2(0xdff97724, 0x70297ebd),
IanBenzMaxim 0:33d4e66780c0 183 RAPIDJSON_UINT64_C2(0xa6dfbd9f, 0xb8e5b88f), RAPIDJSON_UINT64_C2(0xf8a95fcf, 0x88747d94),
IanBenzMaxim 0:33d4e66780c0 184 RAPIDJSON_UINT64_C2(0xb9447093, 0x8fa89bcf), RAPIDJSON_UINT64_C2(0x8a08f0f8, 0xbf0f156b),
IanBenzMaxim 0:33d4e66780c0 185 RAPIDJSON_UINT64_C2(0xcdb02555, 0x653131b6), RAPIDJSON_UINT64_C2(0x993fe2c6, 0xd07b7fac),
IanBenzMaxim 0:33d4e66780c0 186 RAPIDJSON_UINT64_C2(0xe45c10c4, 0x2a2b3b06), RAPIDJSON_UINT64_C2(0xaa242499, 0x697392d3),
IanBenzMaxim 0:33d4e66780c0 187 RAPIDJSON_UINT64_C2(0xfd87b5f2, 0x8300ca0e), RAPIDJSON_UINT64_C2(0xbce50864, 0x92111aeb),
IanBenzMaxim 0:33d4e66780c0 188 RAPIDJSON_UINT64_C2(0x8cbccc09, 0x6f5088cc), RAPIDJSON_UINT64_C2(0xd1b71758, 0xe219652c),
IanBenzMaxim 0:33d4e66780c0 189 RAPIDJSON_UINT64_C2(0x9c400000, 0x00000000), RAPIDJSON_UINT64_C2(0xe8d4a510, 0x00000000),
IanBenzMaxim 0:33d4e66780c0 190 RAPIDJSON_UINT64_C2(0xad78ebc5, 0xac620000), RAPIDJSON_UINT64_C2(0x813f3978, 0xf8940984),
IanBenzMaxim 0:33d4e66780c0 191 RAPIDJSON_UINT64_C2(0xc097ce7b, 0xc90715b3), RAPIDJSON_UINT64_C2(0x8f7e32ce, 0x7bea5c70),
IanBenzMaxim 0:33d4e66780c0 192 RAPIDJSON_UINT64_C2(0xd5d238a4, 0xabe98068), RAPIDJSON_UINT64_C2(0x9f4f2726, 0x179a2245),
IanBenzMaxim 0:33d4e66780c0 193 RAPIDJSON_UINT64_C2(0xed63a231, 0xd4c4fb27), RAPIDJSON_UINT64_C2(0xb0de6538, 0x8cc8ada8),
IanBenzMaxim 0:33d4e66780c0 194 RAPIDJSON_UINT64_C2(0x83c7088e, 0x1aab65db), RAPIDJSON_UINT64_C2(0xc45d1df9, 0x42711d9a),
IanBenzMaxim 0:33d4e66780c0 195 RAPIDJSON_UINT64_C2(0x924d692c, 0xa61be758), RAPIDJSON_UINT64_C2(0xda01ee64, 0x1a708dea),
IanBenzMaxim 0:33d4e66780c0 196 RAPIDJSON_UINT64_C2(0xa26da399, 0x9aef774a), RAPIDJSON_UINT64_C2(0xf209787b, 0xb47d6b85),
IanBenzMaxim 0:33d4e66780c0 197 RAPIDJSON_UINT64_C2(0xb454e4a1, 0x79dd1877), RAPIDJSON_UINT64_C2(0x865b8692, 0x5b9bc5c2),
IanBenzMaxim 0:33d4e66780c0 198 RAPIDJSON_UINT64_C2(0xc83553c5, 0xc8965d3d), RAPIDJSON_UINT64_C2(0x952ab45c, 0xfa97a0b3),
IanBenzMaxim 0:33d4e66780c0 199 RAPIDJSON_UINT64_C2(0xde469fbd, 0x99a05fe3), RAPIDJSON_UINT64_C2(0xa59bc234, 0xdb398c25),
IanBenzMaxim 0:33d4e66780c0 200 RAPIDJSON_UINT64_C2(0xf6c69a72, 0xa3989f5c), RAPIDJSON_UINT64_C2(0xb7dcbf53, 0x54e9bece),
IanBenzMaxim 0:33d4e66780c0 201 RAPIDJSON_UINT64_C2(0x88fcf317, 0xf22241e2), RAPIDJSON_UINT64_C2(0xcc20ce9b, 0xd35c78a5),
IanBenzMaxim 0:33d4e66780c0 202 RAPIDJSON_UINT64_C2(0x98165af3, 0x7b2153df), RAPIDJSON_UINT64_C2(0xe2a0b5dc, 0x971f303a),
IanBenzMaxim 0:33d4e66780c0 203 RAPIDJSON_UINT64_C2(0xa8d9d153, 0x5ce3b396), RAPIDJSON_UINT64_C2(0xfb9b7cd9, 0xa4a7443c),
IanBenzMaxim 0:33d4e66780c0 204 RAPIDJSON_UINT64_C2(0xbb764c4c, 0xa7a44410), RAPIDJSON_UINT64_C2(0x8bab8eef, 0xb6409c1a),
IanBenzMaxim 0:33d4e66780c0 205 RAPIDJSON_UINT64_C2(0xd01fef10, 0xa657842c), RAPIDJSON_UINT64_C2(0x9b10a4e5, 0xe9913129),
IanBenzMaxim 0:33d4e66780c0 206 RAPIDJSON_UINT64_C2(0xe7109bfb, 0xa19c0c9d), RAPIDJSON_UINT64_C2(0xac2820d9, 0x623bf429),
IanBenzMaxim 0:33d4e66780c0 207 RAPIDJSON_UINT64_C2(0x80444b5e, 0x7aa7cf85), RAPIDJSON_UINT64_C2(0xbf21e440, 0x03acdd2d),
IanBenzMaxim 0:33d4e66780c0 208 RAPIDJSON_UINT64_C2(0x8e679c2f, 0x5e44ff8f), RAPIDJSON_UINT64_C2(0xd433179d, 0x9c8cb841),
IanBenzMaxim 0:33d4e66780c0 209 RAPIDJSON_UINT64_C2(0x9e19db92, 0xb4e31ba9), RAPIDJSON_UINT64_C2(0xeb96bf6e, 0xbadf77d9),
IanBenzMaxim 0:33d4e66780c0 210 RAPIDJSON_UINT64_C2(0xaf87023b, 0x9bf0ee6b)
IanBenzMaxim 0:33d4e66780c0 211 };
IanBenzMaxim 0:33d4e66780c0 212 static const int16_t kCachedPowers_E[] = {
IanBenzMaxim 0:33d4e66780c0 213 -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980,
IanBenzMaxim 0:33d4e66780c0 214 -954, -927, -901, -874, -847, -821, -794, -768, -741, -715,
IanBenzMaxim 0:33d4e66780c0 215 -688, -661, -635, -608, -582, -555, -529, -502, -475, -449,
IanBenzMaxim 0:33d4e66780c0 216 -422, -396, -369, -343, -316, -289, -263, -236, -210, -183,
IanBenzMaxim 0:33d4e66780c0 217 -157, -130, -103, -77, -50, -24, 3, 30, 56, 83,
IanBenzMaxim 0:33d4e66780c0 218 109, 136, 162, 189, 216, 242, 269, 295, 322, 348,
IanBenzMaxim 0:33d4e66780c0 219 375, 402, 428, 455, 481, 508, 534, 561, 588, 614,
IanBenzMaxim 0:33d4e66780c0 220 641, 667, 694, 720, 747, 774, 800, 827, 853, 880,
IanBenzMaxim 0:33d4e66780c0 221 907, 933, 960, 986, 1013, 1039, 1066
IanBenzMaxim 0:33d4e66780c0 222 };
IanBenzMaxim 0:33d4e66780c0 223 return DiyFp(kCachedPowers_F[index], kCachedPowers_E[index]);
IanBenzMaxim 0:33d4e66780c0 224 }
IanBenzMaxim 0:33d4e66780c0 225
IanBenzMaxim 0:33d4e66780c0 226 inline DiyFp GetCachedPower(int e, int* K) {
IanBenzMaxim 0:33d4e66780c0 227
IanBenzMaxim 0:33d4e66780c0 228 //int k = static_cast<int>(ceil((-61 - e) * 0.30102999566398114)) + 374;
IanBenzMaxim 0:33d4e66780c0 229 double dk = (-61 - e) * 0.30102999566398114 + 347; // dk must be positive, so can do ceiling in positive
IanBenzMaxim 0:33d4e66780c0 230 int k = static_cast<int>(dk);
IanBenzMaxim 0:33d4e66780c0 231 if (dk - k > 0.0)
IanBenzMaxim 0:33d4e66780c0 232 k++;
IanBenzMaxim 0:33d4e66780c0 233
IanBenzMaxim 0:33d4e66780c0 234 unsigned index = static_cast<unsigned>((k >> 3) + 1);
IanBenzMaxim 0:33d4e66780c0 235 *K = -(-348 + static_cast<int>(index << 3)); // decimal exponent no need lookup table
IanBenzMaxim 0:33d4e66780c0 236
IanBenzMaxim 0:33d4e66780c0 237 return GetCachedPowerByIndex(index);
IanBenzMaxim 0:33d4e66780c0 238 }
IanBenzMaxim 0:33d4e66780c0 239
IanBenzMaxim 0:33d4e66780c0 240 inline DiyFp GetCachedPower10(int exp, int *outExp) {
IanBenzMaxim 0:33d4e66780c0 241 unsigned index = (static_cast<unsigned>(exp) + 348u) / 8u;
IanBenzMaxim 0:33d4e66780c0 242 *outExp = -348 + static_cast<int>(index) * 8;
IanBenzMaxim 0:33d4e66780c0 243 return GetCachedPowerByIndex(index);
IanBenzMaxim 0:33d4e66780c0 244 }
IanBenzMaxim 0:33d4e66780c0 245
IanBenzMaxim 0:33d4e66780c0 246 #ifdef __GNUC__
IanBenzMaxim 0:33d4e66780c0 247 RAPIDJSON_DIAG_POP
IanBenzMaxim 0:33d4e66780c0 248 #endif
IanBenzMaxim 0:33d4e66780c0 249
IanBenzMaxim 0:33d4e66780c0 250 #ifdef __clang__
IanBenzMaxim 0:33d4e66780c0 251 RAPIDJSON_DIAG_POP
IanBenzMaxim 0:33d4e66780c0 252 RAPIDJSON_DIAG_OFF(padded)
IanBenzMaxim 0:33d4e66780c0 253 #endif
IanBenzMaxim 0:33d4e66780c0 254
IanBenzMaxim 0:33d4e66780c0 255 } // namespace internal
IanBenzMaxim 0:33d4e66780c0 256 RAPIDJSON_NAMESPACE_END
IanBenzMaxim 0:33d4e66780c0 257
IanBenzMaxim 0:33d4e66780c0 258 #endif // RAPIDJSON_DIYFP_H_