| 1 | // © 2018 and later: Unicode, Inc. and others. | 
|---|
| 2 | // License & terms of use: http://www.unicode.org/copyright.html | 
|---|
| 3 | // | 
|---|
| 4 | // From the double-conversion library. Original license: | 
|---|
| 5 | // | 
|---|
| 6 | // Copyright 2006-2008 the V8 project authors. All rights reserved. | 
|---|
| 7 | // Redistribution and use in source and binary forms, with or without | 
|---|
| 8 | // modification, are permitted provided that the following conditions are | 
|---|
| 9 | // met: | 
|---|
| 10 | // | 
|---|
| 11 | //     * Redistributions of source code must retain the above copyright | 
|---|
| 12 | //       notice, this list of conditions and the following disclaimer. | 
|---|
| 13 | //     * Redistributions in binary form must reproduce the above | 
|---|
| 14 | //       copyright notice, this list of conditions and the following | 
|---|
| 15 | //       disclaimer in the documentation and/or other materials provided | 
|---|
| 16 | //       with the distribution. | 
|---|
| 17 | //     * Neither the name of Google Inc. nor the names of its | 
|---|
| 18 | //       contributors may be used to endorse or promote products derived | 
|---|
| 19 | //       from this software without specific prior written permission. | 
|---|
| 20 | // | 
|---|
| 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|---|
| 22 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|---|
| 23 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
|---|
| 24 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
|---|
| 25 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
|---|
| 26 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
|---|
| 27 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 28 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 29 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 30 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
|---|
| 31 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 32 |  | 
|---|
| 33 | // ICU PATCH: ifdef around UCONFIG_NO_FORMATTING | 
|---|
| 34 | #include "unicode/utypes.h" | 
|---|
| 35 | #if !UCONFIG_NO_FORMATTING | 
|---|
| 36 |  | 
|---|
| 37 | #include <climits> | 
|---|
| 38 | #include <cmath> | 
|---|
| 39 | #include <cstdarg> | 
|---|
| 40 |  | 
|---|
| 41 | // ICU PATCH: Customize header file paths for ICU. | 
|---|
| 42 |  | 
|---|
| 43 | #include "double-conversion-utils.h" | 
|---|
| 44 |  | 
|---|
| 45 | #include "double-conversion-cached-powers.h" | 
|---|
| 46 |  | 
|---|
| 47 | // ICU PATCH: Wrap in ICU namespace | 
|---|
| 48 | U_NAMESPACE_BEGIN | 
|---|
| 49 |  | 
|---|
| 50 | namespace double_conversion { | 
|---|
| 51 |  | 
|---|
| 52 | namespace PowersOfTenCache { | 
|---|
| 53 |  | 
|---|
| 54 | struct CachedPower { | 
|---|
| 55 | uint64_t significand; | 
|---|
| 56 | int16_t binary_exponent; | 
|---|
| 57 | int16_t decimal_exponent; | 
|---|
| 58 | }; | 
|---|
| 59 |  | 
|---|
| 60 | static const CachedPower kCachedPowers[] = { | 
|---|
| 61 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xfa8fd5a0, 081c0288), -1220, -348}, | 
|---|
| 62 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xbaaee17f, a23ebf76), -1193, -340}, | 
|---|
| 63 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x8b16fb20, 3055ac76), -1166, -332}, | 
|---|
| 64 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xcf42894a, 5dce35ea), -1140, -324}, | 
|---|
| 65 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x9a6bb0aa, 55653b2d), -1113, -316}, | 
|---|
| 66 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xe61acf03, 3d1a45df), -1087, -308}, | 
|---|
| 67 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xab70fe17, c79ac6ca), -1060, -300}, | 
|---|
| 68 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xff77b1fc, bebcdc4f), -1034, -292}, | 
|---|
| 69 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xbe5691ef, 416bd60c), -1007, -284}, | 
|---|
| 70 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x8dd01fad, 907ffc3c), -980, -276}, | 
|---|
| 71 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xd3515c28, 31559a83), -954, -268}, | 
|---|
| 72 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x9d71ac8f, ada6c9b5), -927, -260}, | 
|---|
| 73 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xea9c2277, 23ee8bcb), -901, -252}, | 
|---|
| 74 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xaecc4991, 4078536d), -874, -244}, | 
|---|
| 75 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x823c1279, 5db6ce57), -847, -236}, | 
|---|
| 76 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xc2109436, 4dfb5637), -821, -228}, | 
|---|
| 77 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x9096ea6f, 3848984f), -794, -220}, | 
|---|
| 78 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xd77485cb, 25823ac7), -768, -212}, | 
|---|
| 79 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xa086cfcd, 97bf97f4), -741, -204}, | 
|---|
| 80 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xef340a98, 172aace5), -715, -196}, | 
|---|
| 81 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xb23867fb, 2a35b28e), -688, -188}, | 
|---|
| 82 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x84c8d4df, d2c63f3b), -661, -180}, | 
|---|
| 83 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xc5dd4427, 1ad3cdba), -635, -172}, | 
|---|
| 84 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x936b9fce, bb25c996), -608, -164}, | 
|---|
| 85 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xdbac6c24, 7d62a584), -582, -156}, | 
|---|
| 86 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xa3ab6658, 0d5fdaf6), -555, -148}, | 
|---|
| 87 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xf3e2f893, dec3f126), -529, -140}, | 
|---|
| 88 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xb5b5ada8, aaff80b8), -502, -132}, | 
|---|
| 89 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x87625f05, 6c7c4a8b), -475, -124}, | 
|---|
| 90 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xc9bcff60, 34c13053), -449, -116}, | 
|---|
| 91 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x964e858c, 91ba2655), -422, -108}, | 
|---|
| 92 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xdff97724, 70297ebd), -396, -100}, | 
|---|
| 93 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xa6dfbd9f, b8e5b88f), -369, -92}, | 
|---|
| 94 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xf8a95fcf, 88747d94), -343, -84}, | 
|---|
| 95 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xb9447093, 8fa89bcf), -316, -76}, | 
|---|
| 96 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x8a08f0f8, bf0f156b), -289, -68}, | 
|---|
| 97 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xcdb02555, 653131b6), -263, -60}, | 
|---|
| 98 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x993fe2c6, d07b7fac), -236, -52}, | 
|---|
| 99 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xe45c10c4, 2a2b3b06), -210, -44}, | 
|---|
| 100 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xaa242499, 697392d3), -183, -36}, | 
|---|
| 101 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xfd87b5f2, 8300ca0e), -157, -28}, | 
|---|
| 102 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xbce50864, 92111aeb), -130, -20}, | 
|---|
| 103 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x8cbccc09, 6f5088cc), -103, -12}, | 
|---|
| 104 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xd1b71758, e219652c), -77, -4}, | 
|---|
| 105 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x9c400000, 00000000), -50, 4}, | 
|---|
| 106 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xe8d4a510, 00000000), -24, 12}, | 
|---|
| 107 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xad78ebc5, ac620000), 3, 20}, | 
|---|
| 108 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x813f3978, f8940984), 30, 28}, | 
|---|
| 109 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xc097ce7b, c90715b3), 56, 36}, | 
|---|
| 110 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x8f7e32ce, 7bea5c70), 83, 44}, | 
|---|
| 111 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xd5d238a4, abe98068), 109, 52}, | 
|---|
| 112 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x9f4f2726, 179a2245), 136, 60}, | 
|---|
| 113 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xed63a231, d4c4fb27), 162, 68}, | 
|---|
| 114 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xb0de6538, 8cc8ada8), 189, 76}, | 
|---|
| 115 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x83c7088e, 1aab65db), 216, 84}, | 
|---|
| 116 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xc45d1df9, 42711d9a), 242, 92}, | 
|---|
| 117 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x924d692c, a61be758), 269, 100}, | 
|---|
| 118 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xda01ee64, 1a708dea), 295, 108}, | 
|---|
| 119 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xa26da399, 9aef774a), 322, 116}, | 
|---|
| 120 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xf209787b, b47d6b85), 348, 124}, | 
|---|
| 121 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xb454e4a1, 79dd1877), 375, 132}, | 
|---|
| 122 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x865b8692, 5b9bc5c2), 402, 140}, | 
|---|
| 123 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xc83553c5, c8965d3d), 428, 148}, | 
|---|
| 124 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x952ab45c, fa97a0b3), 455, 156}, | 
|---|
| 125 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xde469fbd, 99a05fe3), 481, 164}, | 
|---|
| 126 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xa59bc234, db398c25), 508, 172}, | 
|---|
| 127 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xf6c69a72, a3989f5c), 534, 180}, | 
|---|
| 128 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xb7dcbf53, 54e9bece), 561, 188}, | 
|---|
| 129 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x88fcf317, f22241e2), 588, 196}, | 
|---|
| 130 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xcc20ce9b, d35c78a5), 614, 204}, | 
|---|
| 131 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x98165af3, 7b2153df), 641, 212}, | 
|---|
| 132 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xe2a0b5dc, 971f303a), 667, 220}, | 
|---|
| 133 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xa8d9d153, 5ce3b396), 694, 228}, | 
|---|
| 134 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xfb9b7cd9, a4a7443c), 720, 236}, | 
|---|
| 135 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xbb764c4c, a7a44410), 747, 244}, | 
|---|
| 136 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x8bab8eef, b6409c1a), 774, 252}, | 
|---|
| 137 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xd01fef10, a657842c), 800, 260}, | 
|---|
| 138 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x9b10a4e5, e9913129), 827, 268}, | 
|---|
| 139 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xe7109bfb, a19c0c9d), 853, 276}, | 
|---|
| 140 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xac2820d9, 623bf429), 880, 284}, | 
|---|
| 141 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x80444b5e, 7aa7cf85), 907, 292}, | 
|---|
| 142 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xbf21e440, 03acdd2d), 933, 300}, | 
|---|
| 143 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x8e679c2f, 5e44ff8f), 960, 308}, | 
|---|
| 144 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xd433179d, 9c8cb841), 986, 316}, | 
|---|
| 145 | {DOUBLE_CONVERSION_UINT64_2PART_C(0x9e19db92, b4e31ba9), 1013, 324}, | 
|---|
| 146 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xeb96bf6e, badf77d9), 1039, 332}, | 
|---|
| 147 | {DOUBLE_CONVERSION_UINT64_2PART_C(0xaf87023b, 9bf0ee6b), 1066, 340}, | 
|---|
| 148 | }; | 
|---|
| 149 |  | 
|---|
| 150 | static const int kCachedPowersOffset = 348;  // -1 * the first decimal_exponent. | 
|---|
| 151 | static const double kD_1_LOG2_10 = 0.30102999566398114;  //  1 / lg(10) | 
|---|
| 152 |  | 
|---|
| 153 | void GetCachedPowerForBinaryExponentRange( | 
|---|
| 154 | int min_exponent, | 
|---|
| 155 | int max_exponent, | 
|---|
| 156 | DiyFp* power, | 
|---|
| 157 | int* decimal_exponent) { | 
|---|
| 158 | int kQ = DiyFp::kSignificandSize; | 
|---|
| 159 | double k = ceil((min_exponent + kQ - 1) * kD_1_LOG2_10); | 
|---|
| 160 | int foo = kCachedPowersOffset; | 
|---|
| 161 | int index = | 
|---|
| 162 | (foo + static_cast<int>(k) - 1) / kDecimalExponentDistance + 1; | 
|---|
| 163 | DOUBLE_CONVERSION_ASSERT(0 <= index && index < static_cast<int>(DOUBLE_CONVERSION_ARRAY_SIZE(kCachedPowers))); | 
|---|
| 164 | CachedPower cached_power = kCachedPowers[index]; | 
|---|
| 165 | DOUBLE_CONVERSION_ASSERT(min_exponent <= cached_power.binary_exponent); | 
|---|
| 166 | (void) max_exponent;  // Mark variable as used. | 
|---|
| 167 | DOUBLE_CONVERSION_ASSERT(cached_power.binary_exponent <= max_exponent); | 
|---|
| 168 | *decimal_exponent = cached_power.decimal_exponent; | 
|---|
| 169 | *power = DiyFp(cached_power.significand, cached_power.binary_exponent); | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 |  | 
|---|
| 173 | void GetCachedPowerForDecimalExponent(int requested_exponent, | 
|---|
| 174 | DiyFp* power, | 
|---|
| 175 | int* found_exponent) { | 
|---|
| 176 | DOUBLE_CONVERSION_ASSERT(kMinDecimalExponent <= requested_exponent); | 
|---|
| 177 | DOUBLE_CONVERSION_ASSERT(requested_exponent < kMaxDecimalExponent + kDecimalExponentDistance); | 
|---|
| 178 | int index = | 
|---|
| 179 | (requested_exponent + kCachedPowersOffset) / kDecimalExponentDistance; | 
|---|
| 180 | CachedPower cached_power = kCachedPowers[index]; | 
|---|
| 181 | *power = DiyFp(cached_power.significand, cached_power.binary_exponent); | 
|---|
| 182 | *found_exponent = cached_power.decimal_exponent; | 
|---|
| 183 | DOUBLE_CONVERSION_ASSERT(*found_exponent <= requested_exponent); | 
|---|
| 184 | DOUBLE_CONVERSION_ASSERT(requested_exponent < *found_exponent + kDecimalExponentDistance); | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | }  // namespace PowersOfTenCache | 
|---|
| 188 |  | 
|---|
| 189 | }  // namespace double_conversion | 
|---|
| 190 |  | 
|---|
| 191 | // ICU PATCH: Close ICU namespace | 
|---|
| 192 | U_NAMESPACE_END | 
|---|
| 193 | #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING | 
|---|
| 194 |  | 
|---|