blob: 52fff7e14560b507f895981eb6d70622f62b74a8 [file] [log] [blame]
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Ben Murdochf87a2032010-10-22 12:50:53 +01004
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005#include "src/cached-powers.h"
6
Ben Murdochf87a2032010-10-22 12:50:53 +01007#include <limits.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include <stdarg.h>
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009#include <stdint.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#include <cmath>
Ben Murdochf87a2032010-10-22 12:50:53 +010011
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012#include "src/base/logging.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013#include "src/globals.h"
Ben Murdochf87a2032010-10-22 12:50:53 +010014
15namespace v8 {
16namespace internal {
17
18struct CachedPower {
19 uint64_t significand;
20 int16_t binary_exponent;
21 int16_t decimal_exponent;
22};
23
24static const CachedPower kCachedPowers[] = {
John Reck59135872010-11-02 12:39:01 -070025 {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},
Ben Murdochf87a2032010-10-22 12:50:53 +010030 {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
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114#ifdef DEBUG
115static const int kCachedPowersLength = arraysize(kCachedPowers);
116#endif
117
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100118static const int kCachedPowersOffset = 348; // -1 * the first decimal_exponent.
Ben Murdochf87a2032010-10-22 12:50:53 +0100119static const double kD_1_LOG2_10 = 0.30102999566398114; // 1 / lg(10)
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100120// Difference between the decimal exponents in the table above.
121const int PowersOfTenCache::kDecimalExponentDistance = 8;
122const int PowersOfTenCache::kMinDecimalExponent = -348;
123const int PowersOfTenCache::kMaxDecimalExponent = 340;
Ben Murdochf87a2032010-10-22 12:50:53 +0100124
John Reck59135872010-11-02 12:39:01 -0700125void PowersOfTenCache::GetCachedPowerForBinaryExponentRange(
126 int min_exponent,
127 int max_exponent,
128 DiyFp* power,
129 int* decimal_exponent) {
130 int kQ = DiyFp::kSignificandSize;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000131 // Some platforms return incorrect sign on 0 result. We can ignore that here,
132 // which means we can avoid depending on platform.h.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133 double k = std::ceil((min_exponent + kQ - 1) * kD_1_LOG2_10);
John Reck59135872010-11-02 12:39:01 -0700134 int foo = kCachedPowersOffset;
135 int index =
136 (foo + static_cast<int>(k) - 1) / kDecimalExponentDistance + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137 DCHECK(0 <= index && index < kCachedPowersLength);
John Reck59135872010-11-02 12:39:01 -0700138 CachedPower cached_power = kCachedPowers[index];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000139 DCHECK(min_exponent <= cached_power.binary_exponent);
140 DCHECK(cached_power.binary_exponent <= max_exponent);
John Reck59135872010-11-02 12:39:01 -0700141 *decimal_exponent = cached_power.decimal_exponent;
142 *power = DiyFp(cached_power.significand, cached_power.binary_exponent);
143}
144
145
146void PowersOfTenCache::GetCachedPowerForDecimalExponent(int requested_exponent,
147 DiyFp* power,
148 int* found_exponent) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149 DCHECK(kMinDecimalExponent <= requested_exponent);
150 DCHECK(requested_exponent < kMaxDecimalExponent + kDecimalExponentDistance);
John Reck59135872010-11-02 12:39:01 -0700151 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;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156 DCHECK(*found_exponent <= requested_exponent);
157 DCHECK(requested_exponent < *found_exponent + kDecimalExponentDistance);
Ben Murdochf87a2032010-10-22 12:50:53 +0100158}
159
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000160} // namespace internal
161} // namespace v8