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

source code of qtbase/src/3rdparty/double-conversion/double-conversion/cached-powers.cc