• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/base/dtoa_helper.h"
17 #include "ecmascript/base/number_helper.h"
18 
19 #ifndef UINT64_C2
20 #define UINT64_C2(high32, low32) ((static_cast<uint64_t>(high32) << 32) | static_cast<uint64_t>(low32))
21 #endif
22 namespace panda::ecmascript::base {
GetCachedPowerByIndex(size_t index)23 DtoaHelper::DiyFp DtoaHelper::GetCachedPowerByIndex(size_t index)
24 {
25     // 10^-348, 10^-340, ..., 10^340
26     static const uint64_t kCachedPowers_F[] = {
27         UINT64_C2(0xfa8fd5a0, 0x081c0288), UINT64_C2(0xbaaee17f, 0xa23ebf76), UINT64_C2(0x8b16fb20, 0x3055ac76),
28         UINT64_C2(0xcf42894a, 0x5dce35ea), UINT64_C2(0x9a6bb0aa, 0x55653b2d), UINT64_C2(0xe61acf03, 0x3d1a45df),
29         UINT64_C2(0xab70fe17, 0xc79ac6ca), UINT64_C2(0xff77b1fc, 0xbebcdc4f), UINT64_C2(0xbe5691ef, 0x416bd60c),
30         UINT64_C2(0x8dd01fad, 0x907ffc3c), UINT64_C2(0xd3515c28, 0x31559a83), UINT64_C2(0x9d71ac8f, 0xada6c9b5),
31         UINT64_C2(0xea9c2277, 0x23ee8bcb), UINT64_C2(0xaecc4991, 0x4078536d), UINT64_C2(0x823c1279, 0x5db6ce57),
32         UINT64_C2(0xc2109436, 0x4dfb5637), UINT64_C2(0x9096ea6f, 0x3848984f), UINT64_C2(0xd77485cb, 0x25823ac7),
33         UINT64_C2(0xa086cfcd, 0x97bf97f4), UINT64_C2(0xef340a98, 0x172aace5), UINT64_C2(0xb23867fb, 0x2a35b28e),
34         UINT64_C2(0x84c8d4df, 0xd2c63f3b), UINT64_C2(0xc5dd4427, 0x1ad3cdba), UINT64_C2(0x936b9fce, 0xbb25c996),
35         UINT64_C2(0xdbac6c24, 0x7d62a584), UINT64_C2(0xa3ab6658, 0x0d5fdaf6), UINT64_C2(0xf3e2f893, 0xdec3f126),
36         UINT64_C2(0xb5b5ada8, 0xaaff80b8), UINT64_C2(0x87625f05, 0x6c7c4a8b), UINT64_C2(0xc9bcff60, 0x34c13053),
37         UINT64_C2(0x964e858c, 0x91ba2655), UINT64_C2(0xdff97724, 0x70297ebd), UINT64_C2(0xa6dfbd9f, 0xb8e5b88f),
38         UINT64_C2(0xf8a95fcf, 0x88747d94), UINT64_C2(0xb9447093, 0x8fa89bcf), UINT64_C2(0x8a08f0f8, 0xbf0f156b),
39         UINT64_C2(0xcdb02555, 0x653131b6), UINT64_C2(0x993fe2c6, 0xd07b7fac), UINT64_C2(0xe45c10c4, 0x2a2b3b06),
40         UINT64_C2(0xaa242499, 0x697392d3), UINT64_C2(0xfd87b5f2, 0x8300ca0e), UINT64_C2(0xbce50864, 0x92111aeb),
41         UINT64_C2(0x8cbccc09, 0x6f5088cc), UINT64_C2(0xd1b71758, 0xe219652c), UINT64_C2(0x9c400000, 0x00000000),
42         UINT64_C2(0xe8d4a510, 0x00000000), UINT64_C2(0xad78ebc5, 0xac620000), UINT64_C2(0x813f3978, 0xf8940984),
43         UINT64_C2(0xc097ce7b, 0xc90715b3), UINT64_C2(0x8f7e32ce, 0x7bea5c70), UINT64_C2(0xd5d238a4, 0xabe98068),
44         UINT64_C2(0x9f4f2726, 0x179a2245), UINT64_C2(0xed63a231, 0xd4c4fb27), UINT64_C2(0xb0de6538, 0x8cc8ada8),
45         UINT64_C2(0x83c7088e, 0x1aab65db), UINT64_C2(0xc45d1df9, 0x42711d9a), UINT64_C2(0x924d692c, 0xa61be758),
46         UINT64_C2(0xda01ee64, 0x1a708dea), UINT64_C2(0xa26da399, 0x9aef774a), UINT64_C2(0xf209787b, 0xb47d6b85),
47         UINT64_C2(0xb454e4a1, 0x79dd1877), UINT64_C2(0x865b8692, 0x5b9bc5c2), UINT64_C2(0xc83553c5, 0xc8965d3d),
48         UINT64_C2(0x952ab45c, 0xfa97a0b3), UINT64_C2(0xde469fbd, 0x99a05fe3), UINT64_C2(0xa59bc234, 0xdb398c25),
49         UINT64_C2(0xf6c69a72, 0xa3989f5c), UINT64_C2(0xb7dcbf53, 0x54e9bece), UINT64_C2(0x88fcf317, 0xf22241e2),
50         UINT64_C2(0xcc20ce9b, 0xd35c78a5), UINT64_C2(0x98165af3, 0x7b2153df), UINT64_C2(0xe2a0b5dc, 0x971f303a),
51         UINT64_C2(0xa8d9d153, 0x5ce3b396), UINT64_C2(0xfb9b7cd9, 0xa4a7443c), UINT64_C2(0xbb764c4c, 0xa7a44410),
52         UINT64_C2(0x8bab8eef, 0xb6409c1a), UINT64_C2(0xd01fef10, 0xa657842c), UINT64_C2(0x9b10a4e5, 0xe9913129),
53         UINT64_C2(0xe7109bfb, 0xa19c0c9d), UINT64_C2(0xac2820d9, 0x623bf429), UINT64_C2(0x80444b5e, 0x7aa7cf85),
54         UINT64_C2(0xbf21e440, 0x03acdd2d), UINT64_C2(0x8e679c2f, 0x5e44ff8f), UINT64_C2(0xd433179d, 0x9c8cb841),
55         UINT64_C2(0x9e19db92, 0xb4e31ba9), UINT64_C2(0xeb96bf6e, 0xbadf77d9), UINT64_C2(0xaf87023b, 0x9bf0ee6b)};
56     static const int16_t kCachedPowers_E[] = {
57         -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980, -954, -927, -901, -874, -847,
58         -821,  -794,  -768,  -741,  -715,  -688,  -661,  -635,  -608,  -582, -555, -529, -502, -475, -449,
59         -422,  -396,  -369,  -343,  -316,  -289,  -263,  -236,  -210,  -183, -157, -130, -103, -77,  -50,
60         -24,   3,     30,    56,    83,    109,   136,   162,   189,   216,  242,  269,  295,  322,  348,
61         375,   402,   428,   455,   481,   508,   534,   561,   588,   614,  641,  667,  694,  720,  747,
62         774,   800,   827,   853,   880,   907,   933,   960,   986,   1013, 1039, 1066};
63     return DtoaHelper::DiyFp(kCachedPowers_F[index], kCachedPowers_E[index]);
64 }
65 
GrisuRound(char * buffer,int len,uint64_t delta,uint64_t rest,uint64_t tenKappa,uint64_t distance)66 void DtoaHelper::GrisuRound(char *buffer, int len, uint64_t delta, uint64_t rest, uint64_t tenKappa, uint64_t distance)
67 {
68     while (rest < distance && delta - rest >= tenKappa &&
69         (rest + tenKappa < distance || distance - rest > rest + tenKappa - distance)) {
70         buffer[len - 1]--;
71         rest += tenKappa;
72     }
73 }
74 
CountDecimalDigit32(uint32_t n)75 int DtoaHelper::CountDecimalDigit32(uint32_t n)
76 {
77     if (n < TEN) {
78         return 1; // 1: means the decimal digit
79     } else if (n < TEN2POW) {
80         return 2; // 2: means the decimal digit
81     } else if (n < TEN3POW) {
82         return 3; // 3: means the decimal digit
83     } else if (n < TEN4POW) {
84         return 4; // 4: means the decimal digit
85     } else if (n < TEN5POW) {
86         return 5; // 5: means the decimal digit
87     } else if (n < TEN6POW) {
88         return 6; // 6: means the decimal digit
89     } else if (n < TEN7POW) {
90         return 7; // 7: means the decimal digit
91     } else if (n < TEN8POW) {
92         return 8; // 8: means the decimal digit
93     } else {
94         return 9; // 9: means the decimal digit
95     }
96 }
97 
DigitGen(const DiyFp & W,const DiyFp & Mp,uint64_t delta,char * buffer,int * len,int * K)98 void DtoaHelper::DigitGen(const DiyFp &W, const DiyFp &Mp, uint64_t delta, char *buffer, int *len, int *K)
99 {
100     const DiyFp one(uint64_t(1) << -Mp.e, Mp.e);
101     const DiyFp distance = Mp - W;
102     uint32_t p1 = static_cast<uint32_t>(Mp.f >> -one.e);
103     ASSERT(one.f > 0);
104     uint64_t p2 = Mp.f & (one.f - 1);
105     int kappa = CountDecimalDigit32(p1); // kappa in [0, 9]
106     *len = 0;
107     while (kappa > 0) {
108         uint32_t d = 0;
109         switch (kappa) {
110             case 9: // 9: means the decimal digit
111                 d = p1 / TEN8POW;
112                 p1 %= TEN8POW;
113                 break;
114             case 8: // 8: means the decimal digit
115                 d = p1 / TEN7POW;
116                 p1 %= TEN7POW;
117                 break;
118             case 7: // 7: means the decimal digit
119                 d = p1 / TEN6POW;
120                 p1 %= TEN6POW;
121                 break;
122             case 6: // 6: means the decimal digit
123                 d = p1 / TEN5POW;
124                 p1 %= TEN5POW;
125                 break;
126             case 5: // 5: means the decimal digit
127                 d = p1 / TEN4POW;
128                 p1 %= TEN4POW;
129                 break;
130             case 4: // 4: means the decimal digit
131                 d = p1 / TEN3POW;
132                 p1 %= TEN3POW;
133                 break;
134             case 3: // 3: means the decimal digit
135                 d = p1 / TEN2POW;
136                 p1 %= TEN2POW;
137                 break;
138             case 2: // 2: means the decimal digit
139                 d = p1 / TEN;
140                 p1 %= TEN;
141                 break;
142             case 1: // 1: means the decimal digit
143                 d = p1;
144                 p1 = 0;
145                 break;
146             default:;
147         }
148         if (d || *len) {
149             buffer[(*len)++] = static_cast<char>('0' + static_cast<char>(d));
150         }
151         kappa--;
152         uint64_t tmp = (static_cast<uint64_t>(p1) << -one.e) + p2;
153         if (tmp <= delta) {
154             *K += kappa;
155             GrisuRound(buffer, *len, delta, tmp, POW10[kappa] << -one.e, distance.f);
156             return;
157         }
158     }
159 
160     // kappa = 0
161     for (;;) {
162         p2 *= TEN;
163         delta *= TEN;
164         char d = static_cast<char>(p2 >> -one.e);
165         if (d || *len) {
166             buffer[(*len)++] = static_cast<char>('0' + d);
167         }
168         ASSERT(one.f > 0);
169         p2 &= one.f - 1;
170         kappa--;
171         if (p2 < delta) {
172             *K += kappa;
173             int index = -kappa;
174             GrisuRound(buffer, *len, delta, p2, one.f, distance.f * (index < kIndex ? POW10[index] : 0));
175             return;
176         }
177     }
178 }
179 
180 // Grisu2 algorithm use the extra capacity of the used integer type to shorten the produced output
Grisu(double value,char * buffer,int * length,int * K)181 void DtoaHelper::Grisu(double value, char *buffer, int *length, int *K)
182 {
183     const DiyFp v(value);
184     DiyFp mMinus;
185     DiyFp mPlus;
186     v.NormalizedBoundaries(&mMinus, &mPlus);
187 
188     const DiyFp cached = GetCachedPower(mPlus.e, K);
189     const DiyFp W = v.Normalize() * cached;
190     DiyFp wPlus = mPlus * cached;
191     DiyFp wMinus = mMinus * cached;
192     wMinus.f++;
193     wPlus.f--;
194     DigitGen(W, wPlus, wPlus.f - wMinus.f, buffer, length, K);
195 }
196 
Dtoa(double value,char * buffer,int * point,int * length)197 void DtoaHelper::Dtoa(double value, char *buffer, int *point, int *length)
198 {
199     // Exceptional case such as NAN, 0.0, negative... are processed in DoubleToEcmaString
200     // So use Dtoa should avoid Exceptional case.
201     ASSERT(value > 0);
202     int k;
203     Grisu(value, buffer, length, &k);
204     *point = *length + k;
205 }
206 
FillDigits32FixedLength(uint32_t number,int requested_length,BufferVector<char> buffer,int * length)207 void DtoaHelper::FillDigits32FixedLength(uint32_t number, int requested_length,
208                                          BufferVector<char> buffer, int* length)
209 {
210     for (int i = requested_length - 1; i >= 0; --i) {
211         buffer[(*length) + i] = '0' + number % TEN;
212         number /= TEN;
213     }
214     *length += requested_length;
215 }
216 
FillDigits32(uint32_t number,BufferVector<char> buffer,int * length)217 void DtoaHelper::FillDigits32(uint32_t number, BufferVector<char> buffer, int* length)
218 {
219     int number_length = 0;
220     // We fill the digits in reverse order and exchange them afterwards.
221     while (number != 0) {
222         int digit = static_cast<int>(number % TEN);
223         number /= TEN;
224         buffer[(*length) + number_length] = '0' + digit;
225         number_length++;
226     }
227     // Exchange the digits.
228     int i = *length;
229     int j = *length + number_length - 1;
230     while (i < j) {
231         char tmp = buffer[i];
232         buffer[i] = buffer[j];
233         buffer[j] = tmp;
234         i++;
235         j--;
236     }
237     *length += number_length;
238 }
239 
FillDigits64FixedLength(uint64_t number,int requested_length,BufferVector<char> buffer,int * length)240 void DtoaHelper::FillDigits64FixedLength(uint64_t number, [[maybe_unused]] int requested_length,
241                                          BufferVector<char> buffer, int* length)
242 {
243     // For efficiency cut the number into 3 uint32_t parts, and print those.
244     uint32_t part2 = static_cast<uint32_t>(number % TEN7POW);
245     number /= TEN7POW;
246     uint32_t part1 = static_cast<uint32_t>(number % TEN7POW);
247     uint32_t part0 = static_cast<uint32_t>(number / TEN7POW);
248     FillDigits32FixedLength(part0, 3, buffer, length); // 3: parameter
249     FillDigits32FixedLength(part1, 7, buffer, length); // 7: parameter
250     FillDigits32FixedLength(part2, 7, buffer, length); // 7: parameter
251 }
252 
FillDigits64(uint64_t number,BufferVector<char> buffer,int * length)253 void DtoaHelper::FillDigits64(uint64_t number, BufferVector<char> buffer, int* length)
254 {
255     // For efficiency cut the number into 3 uint32_t parts, and print those.
256     uint32_t part2 = static_cast<uint32_t>(number % TEN7POW);
257     number /= TEN7POW;
258     uint32_t part1 = static_cast<uint32_t>(number % TEN7POW);
259     uint32_t part0 = static_cast<uint32_t>(number / TEN7POW);
260     if (part0 != 0) {
261         FillDigits32(part0, buffer, length);
262         FillDigits32FixedLength(part1, 7, buffer, length); // 7: means the decimal digit
263         FillDigits32FixedLength(part2, 7, buffer, length); // 7: means the decimal digit
264     } else if (part1 != 0) {
265         FillDigits32(part1, buffer, length);
266         FillDigits32FixedLength(part2, 7, buffer, length); // 7: means the decimal digit
267     } else {
268         FillDigits32(part2, buffer, length);
269     }
270 }
271 
RoundUp(BufferVector<char> buffer,int * length,int * decimal_point)272 void DtoaHelper::RoundUp(BufferVector<char> buffer, int* length, int* decimal_point)
273 {
274     // An empty buffer represents 0.
275     if (*length == 0) {
276         buffer[0] = '1';
277         *decimal_point = 1;
278         *length = 1;
279         return;
280     }
281     buffer[(*length) - 1]++;
282     for (int i = (*length) - 1; i > 0; --i) {
283         if (buffer[i] != '0' + 10) { // 10: means the decimal digit
284             return;
285         }
286         buffer[i] = '0';
287         buffer[i - 1]++;
288     }
289     if (buffer[0] == '0' + 10) { // 10: means the decimal digit
290         buffer[0] = '1';
291         (*decimal_point)++;
292     }
293 }
294 
FillFractionals(uint64_t fractionals,int exponent,int fractional_count,BufferVector<char> buffer,int * length,int * decimal_point)295 void DtoaHelper::FillFractionals(uint64_t fractionals, int exponent, int fractional_count,
296                                  BufferVector<char> buffer, int* length, int* decimal_point)
297 {
298     ASSERT(NEGATIVE_128BIT <= exponent && exponent <= 0);
299     // 'fractionals' is a fixed-point number, with binary point at bit
300     // (-exponent). Inside the function the non-converted remainder of fractionals
301     // is a fixed-point number, with binary point at bit 'point'.
302     if (-exponent <= EXPONENT_64) {
303         // One 64 bit number is sufficient.
304         ASSERT((fractionals >> 56) == 0); // 56: parameter
305         int point = -exponent;
306         for (int i = 0; i < fractional_count; ++i) {
307             if (fractionals == 0) break;
308             fractionals *= 5; // 5: parameter
309             point--;
310             int digit = static_cast<int>(fractionals >> point);
311             buffer[*length] = '0' + digit;
312             (*length)++;
313             fractionals -= static_cast<uint64_t>(digit) << point;
314         }
315         // If the first bit after the point is set we have to round up.
316         if (point > 0 && ((fractionals >> (point - 1)) & 1) == 1) {
317             RoundUp(buffer, length, decimal_point);
318         }
319     } else {  // We need 128 bits.
320         ASSERT(EXPONENT_64 < -exponent && -exponent <= EXPONENT_128);
321         UInt128 fractionals128 = UInt128(fractionals, 0);
322         fractionals128.Shift(-exponent - EXPONENT_64);
323         int point = 128;
324         for (int i = 0; i < fractional_count; ++i) {
325             if (fractionals128.IsZero()) break;
326             // As before: instead of multiplying by 10 we multiply by 5 and adjust the
327             // point location.
328             // This multiplication will not overflow for the same reasons as before.
329             fractionals128.Multiply(5); // 5: parameter
330             point--;
331             int digit = fractionals128.DivModPowerOf2(point);
332             buffer[*length] = '0' + digit;
333             (*length)++;
334         }
335         if (fractionals128.BitAt(point - 1) == 1) {
336             RoundUp(buffer, length, decimal_point);
337         }
338     }
339 }
340 
341 // Removes leading and trailing zeros.
342 // If leading zeros are removed then the decimal point position is adjusted.
TrimZeros(BufferVector<char> buffer,int * length,int * decimal_point)343 void DtoaHelper::TrimZeros(BufferVector<char> buffer, int* length, int* decimal_point)
344 {
345     while (*length > 0 && buffer[(*length) - 1] == '0') {
346         (*length)--;
347     }
348     int first_non_zero = 0;
349     while (first_non_zero < *length && buffer[first_non_zero] == '0') {
350         first_non_zero++;
351     }
352     if (first_non_zero != 0) {
353         for (int i = first_non_zero; i < *length; ++i) {
354             buffer[i - first_non_zero] = buffer[i];
355         }
356         *length -= first_non_zero;
357         *decimal_point -= first_non_zero;
358     }
359 }
360 
FixedDtoa(double v,int fractional_count,BufferVector<char> buffer,int * length,int * decimal_point)361 bool DtoaHelper::FixedDtoa(double v, int fractional_count, BufferVector<char> buffer,
362                            int* length, int* decimal_point)
363 {
364     if (v == 0) {
365         buffer[0] = '0';
366         buffer[1] = '\0';
367         *length = 1;
368         *decimal_point = 1;
369         return true;
370     }
371     uint64_t significand = NumberHelper::Significand(v);
372     int exponent = NumberHelper::Exponent(v);
373     if (exponent > 20) return false; // 20: max parameter
374     if (fractional_count > 20) return false; // 20: max parameter
375     *length = 0;
376     if (exponent + kDoubleSignificandSize > EXPONENT_64) {
377         const uint64_t kFive17 = 0xB1'A2BC'2EC5;  // 5^17
378         uint64_t divisor = kFive17;
379         int divisor_power = 17;
380         uint64_t dividend = significand;
381         uint32_t quotient;
382         uint64_t remainder;
383         if (exponent > divisor_power) {
384             // We only allow exponents of up to 20 and therefore (17 - e) <= 3
385             dividend <<= exponent - divisor_power;
386             quotient = static_cast<uint32_t>(dividend / divisor);
387             remainder = (dividend % divisor) << divisor_power;
388         } else {
389             divisor <<= divisor_power - exponent;
390             quotient = static_cast<uint32_t>(dividend / divisor);
391             remainder = (dividend % divisor) << exponent;
392         }
393         FillDigits32(quotient, buffer, length);
394         FillDigits64FixedLength(remainder, divisor_power, buffer, length);
395         *decimal_point = *length;
396     } else if (exponent >= 0) {
397         // 0 <= exponent <= 11
398         significand <<= exponent;
399         FillDigits64(significand, buffer, length);
400         *decimal_point = *length;
401     } else if (exponent > -kDoubleSignificandSize) {
402         // We have to cut the number.
403         uint64_t integrals = significand >> -exponent;
404         uint64_t fractionals = significand - (integrals << -exponent);
405         if (integrals > kMaxUInt32) {
406             FillDigits64(integrals, buffer, length);
407         } else {
408             FillDigits32(static_cast<uint32_t>(integrals), buffer, length);
409         }
410         *decimal_point = *length;
411         FillFractionals(fractionals, exponent, fractional_count,
412                         buffer, length, decimal_point);
413     } else if (exponent < NEGATIVE_128BIT) {
414         ASSERT(fractional_count <= 20); // 20: parameter
415         buffer[0] = '\0';
416         *length = 0;
417         *decimal_point = -fractional_count;
418     } else {
419         *decimal_point = 0;
420         FillFractionals(significand, exponent, fractional_count,
421                         buffer, length, decimal_point);
422     }
423     TrimZeros(buffer, length, decimal_point);
424     buffer[*length] = '\0';
425     if ((*length) == 0) {
426         *decimal_point = -fractional_count;
427     }
428     return true;
429 }
430 }