1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/cached-powers.h"
6
7 #include <limits.h>
8 #include <stdarg.h>
9 #include <stdint.h>
10 #include <cmath>
11
12 #include "src/base/logging.h"
13 #include "src/globals.h"
14
15 namespace v8 {
16 namespace internal {
17
18 struct CachedPower {
19 uint64_t significand;
20 int16_t binary_exponent;
21 int16_t decimal_exponent;
22 };
23
24 static const CachedPower kCachedPowers[] = {
25 {V8_2PART_UINT64_C(0xFA8FD5A0, 081C0288), -1220, -348},
26 {V8_2PART_UINT64_C(0xBAAEE17F, A23EBF76), -1193, -340},
27 {V8_2PART_UINT64_C(0x8B16FB20, 3055AC76), -1166, -332},
28 {V8_2PART_UINT64_C(0xCF42894A, 5DCE35EA), -1140, -324},
29 {V8_2PART_UINT64_C(0x9A6BB0AA, 55653B2D), -1113, -316},
30 {V8_2PART_UINT64_C(0xE61ACF03, 3D1A45DF), -1087, -308},
31 {V8_2PART_UINT64_C(0xAB70FE17, C79AC6CA), -1060, -300},
32 {V8_2PART_UINT64_C(0xFF77B1FC, BEBCDC4F), -1034, -292},
33 {V8_2PART_UINT64_C(0xBE5691EF, 416BD60C), -1007, -284},
34 {V8_2PART_UINT64_C(0x8DD01FAD, 907FFC3C), -980, -276},
35 {V8_2PART_UINT64_C(0xD3515C28, 31559A83), -954, -268},
36 {V8_2PART_UINT64_C(0x9D71AC8F, ADA6C9B5), -927, -260},
37 {V8_2PART_UINT64_C(0xEA9C2277, 23EE8BCB), -901, -252},
38 {V8_2PART_UINT64_C(0xAECC4991, 4078536D), -874, -244},
39 {V8_2PART_UINT64_C(0x823C1279, 5DB6CE57), -847, -236},
40 {V8_2PART_UINT64_C(0xC2109436, 4DFB5637), -821, -228},
41 {V8_2PART_UINT64_C(0x9096EA6F, 3848984F), -794, -220},
42 {V8_2PART_UINT64_C(0xD77485CB, 25823AC7), -768, -212},
43 {V8_2PART_UINT64_C(0xA086CFCD, 97BF97F4), -741, -204},
44 {V8_2PART_UINT64_C(0xEF340A98, 172AACE5), -715, -196},
45 {V8_2PART_UINT64_C(0xB23867FB, 2A35B28E), -688, -188},
46 {V8_2PART_UINT64_C(0x84C8D4DF, D2C63F3B), -661, -180},
47 {V8_2PART_UINT64_C(0xC5DD4427, 1AD3CDBA), -635, -172},
48 {V8_2PART_UINT64_C(0x936B9FCE, BB25C996), -608, -164},
49 {V8_2PART_UINT64_C(0xDBAC6C24, 7D62A584), -582, -156},
50 {V8_2PART_UINT64_C(0xA3AB6658, 0D5FDAF6), -555, -148},
51 {V8_2PART_UINT64_C(0xF3E2F893, DEC3F126), -529, -140},
52 {V8_2PART_UINT64_C(0xB5B5ADA8, AAFF80B8), -502, -132},
53 {V8_2PART_UINT64_C(0x87625F05, 6C7C4A8B), -475, -124},
54 {V8_2PART_UINT64_C(0xC9BCFF60, 34C13053), -449, -116},
55 {V8_2PART_UINT64_C(0x964E858C, 91BA2655), -422, -108},
56 {V8_2PART_UINT64_C(0xDFF97724, 70297EBD), -396, -100},
57 {V8_2PART_UINT64_C(0xA6DFBD9F, B8E5B88F), -369, -92},
58 {V8_2PART_UINT64_C(0xF8A95FCF, 88747D94), -343, -84},
59 {V8_2PART_UINT64_C(0xB9447093, 8FA89BCF), -316, -76},
60 {V8_2PART_UINT64_C(0x8A08F0F8, BF0F156B), -289, -68},
61 {V8_2PART_UINT64_C(0xCDB02555, 653131B6), -263, -60},
62 {V8_2PART_UINT64_C(0x993FE2C6, D07B7FAC), -236, -52},
63 {V8_2PART_UINT64_C(0xE45C10C4, 2A2B3B06), -210, -44},
64 {V8_2PART_UINT64_C(0xAA242499, 697392D3), -183, -36},
65 {V8_2PART_UINT64_C(0xFD87B5F2, 8300CA0E), -157, -28},
66 {V8_2PART_UINT64_C(0xBCE50864, 92111AEB), -130, -20},
67 {V8_2PART_UINT64_C(0x8CBCCC09, 6F5088CC), -103, -12},
68 {V8_2PART_UINT64_C(0xD1B71758, E219652C), -77, -4},
69 {V8_2PART_UINT64_C(0x9C400000, 00000000), -50, 4},
70 {V8_2PART_UINT64_C(0xE8D4A510, 00000000), -24, 12},
71 {V8_2PART_UINT64_C(0xAD78EBC5, AC620000), 3, 20},
72 {V8_2PART_UINT64_C(0x813F3978, F8940984), 30, 28},
73 {V8_2PART_UINT64_C(0xC097CE7B, C90715B3), 56, 36},
74 {V8_2PART_UINT64_C(0x8F7E32CE, 7BEA5C70), 83, 44},
75 {V8_2PART_UINT64_C(0xD5D238A4, ABE98068), 109, 52},
76 {V8_2PART_UINT64_C(0x9F4F2726, 179A2245), 136, 60},
77 {V8_2PART_UINT64_C(0xED63A231, D4C4FB27), 162, 68},
78 {V8_2PART_UINT64_C(0xB0DE6538, 8CC8ADA8), 189, 76},
79 {V8_2PART_UINT64_C(0x83C7088E, 1AAB65DB), 216, 84},
80 {V8_2PART_UINT64_C(0xC45D1DF9, 42711D9A), 242, 92},
81 {V8_2PART_UINT64_C(0x924D692C, A61BE758), 269, 100},
82 {V8_2PART_UINT64_C(0xDA01EE64, 1A708DEA), 295, 108},
83 {V8_2PART_UINT64_C(0xA26DA399, 9AEF774A), 322, 116},
84 {V8_2PART_UINT64_C(0xF209787B, B47D6B85), 348, 124},
85 {V8_2PART_UINT64_C(0xB454E4A1, 79DD1877), 375, 132},
86 {V8_2PART_UINT64_C(0x865B8692, 5B9BC5C2), 402, 140},
87 {V8_2PART_UINT64_C(0xC83553C5, C8965D3D), 428, 148},
88 {V8_2PART_UINT64_C(0x952AB45C, FA97A0B3), 455, 156},
89 {V8_2PART_UINT64_C(0xDE469FBD, 99A05FE3), 481, 164},
90 {V8_2PART_UINT64_C(0xA59BC234, DB398C25), 508, 172},
91 {V8_2PART_UINT64_C(0xF6C69A72, A3989F5C), 534, 180},
92 {V8_2PART_UINT64_C(0xB7DCBF53, 54E9BECE), 561, 188},
93 {V8_2PART_UINT64_C(0x88FCF317, F22241E2), 588, 196},
94 {V8_2PART_UINT64_C(0xCC20CE9B, D35C78A5), 614, 204},
95 {V8_2PART_UINT64_C(0x98165AF3, 7B2153DF), 641, 212},
96 {V8_2PART_UINT64_C(0xE2A0B5DC, 971F303A), 667, 220},
97 {V8_2PART_UINT64_C(0xA8D9D153, 5CE3B396), 694, 228},
98 {V8_2PART_UINT64_C(0xFB9B7CD9, A4A7443C), 720, 236},
99 {V8_2PART_UINT64_C(0xBB764C4C, A7A44410), 747, 244},
100 {V8_2PART_UINT64_C(0x8BAB8EEF, B6409C1A), 774, 252},
101 {V8_2PART_UINT64_C(0xD01FEF10, A657842C), 800, 260},
102 {V8_2PART_UINT64_C(0x9B10A4E5, E9913129), 827, 268},
103 {V8_2PART_UINT64_C(0xE7109BFB, A19C0C9D), 853, 276},
104 {V8_2PART_UINT64_C(0xAC2820D9, 623BF429), 880, 284},
105 {V8_2PART_UINT64_C(0x80444B5E, 7AA7CF85), 907, 292},
106 {V8_2PART_UINT64_C(0xBF21E440, 03ACDD2D), 933, 300},
107 {V8_2PART_UINT64_C(0x8E679C2F, 5E44FF8F), 960, 308},
108 {V8_2PART_UINT64_C(0xD433179D, 9C8CB841), 986, 316},
109 {V8_2PART_UINT64_C(0x9E19DB92, B4E31BA9), 1013, 324},
110 {V8_2PART_UINT64_C(0xEB96BF6E, BADF77D9), 1039, 332},
111 {V8_2PART_UINT64_C(0xAF87023B, 9BF0EE6B), 1066, 340},
112 };
113
114 #ifdef DEBUG
115 static const int kCachedPowersLength = arraysize(kCachedPowers);
116 #endif
117
118 static const int kCachedPowersOffset = 348; // -1 * the first decimal_exponent.
119 static const double kD_1_LOG2_10 = 0.30102999566398114; // 1 / lg(10)
120 // Difference between the decimal exponents in the table above.
121 const int PowersOfTenCache::kDecimalExponentDistance = 8;
122 const int PowersOfTenCache::kMinDecimalExponent = -348;
123 const int PowersOfTenCache::kMaxDecimalExponent = 340;
124
GetCachedPowerForBinaryExponentRange(int min_exponent,int max_exponent,DiyFp * power,int * decimal_exponent)125 void PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
126 int min_exponent,
127 int max_exponent,
128 DiyFp* power,
129 int* decimal_exponent) {
130 int kQ = DiyFp::kSignificandSize;
131 // Some platforms return incorrect sign on 0 result. We can ignore that here,
132 // which means we can avoid depending on platform.h.
133 double k = std::ceil((min_exponent + kQ - 1) * kD_1_LOG2_10);
134 int foo = kCachedPowersOffset;
135 int index =
136 (foo + static_cast<int>(k) - 1) / kDecimalExponentDistance + 1;
137 DCHECK(0 <= index && index < kCachedPowersLength);
138 CachedPower cached_power = kCachedPowers[index];
139 DCHECK(min_exponent <= cached_power.binary_exponent);
140 DCHECK(cached_power.binary_exponent <= max_exponent);
141 *decimal_exponent = cached_power.decimal_exponent;
142 *power = DiyFp(cached_power.significand, cached_power.binary_exponent);
143 }
144
145
GetCachedPowerForDecimalExponent(int requested_exponent,DiyFp * power,int * found_exponent)146 void PowersOfTenCache::GetCachedPowerForDecimalExponent(int requested_exponent,
147 DiyFp* power,
148 int* found_exponent) {
149 DCHECK_LE(kMinDecimalExponent, requested_exponent);
150 DCHECK(requested_exponent < kMaxDecimalExponent + kDecimalExponentDistance);
151 int index =
152 (requested_exponent + kCachedPowersOffset) / kDecimalExponentDistance;
153 CachedPower cached_power = kCachedPowers[index];
154 *power = DiyFp(cached_power.significand, cached_power.binary_exponent);
155 *found_exponent = cached_power.decimal_exponent;
156 DCHECK(*found_exponent <= requested_exponent);
157 DCHECK(requested_exponent < *found_exponent + kDecimalExponentDistance);
158 }
159
160 } // namespace internal
161 } // namespace v8
162