1 //===-- High Precision Decimal ----------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See httpss//llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LIBC_SRC_SUPPORT_HIGH_PRECISION_DECIMAL_H
10 #define LIBC_SRC_SUPPORT_HIGH_PRECISION_DECIMAL_H
12 #include "src/__support/ctype_utils.h"
13 #include "src/__support/str_to_integer.h"
16 namespace __llvm_libc
{
19 struct LShiftTableEntry
{
21 char const *power_of_five
;
24 // This is used in both this file and in the main str_to_float.h.
25 // TODO: Figure out where to put this.
26 enum class RoundDirection
{ Up
, Down
, Nearest
};
28 // This is based on the HPD data structure described as part of the Simple
29 // Decimal Conversion algorithm by Nigel Tao, described at this link:
30 // https://nigeltao.github.io/blog/2020/parse-number-f64-simple.html
31 class HighPrecisionDecimal
{
33 // This precomputed table speeds up left shifts by having the number of new
34 // digits that will be added by multiplying 5^i by 2^i. If the number is less
35 // than 5^i then it will add one fewer digit. There are only 60 entries since
36 // that's the max shift amount.
37 // This table was generated by the script at
38 // libc/utils/mathtools/GenerateHPDConstants.py
39 static constexpr LShiftTableEntry LEFT_SHIFT_DIGIT_TABLE
[] = {
59 {6, "19073486328125"},
60 {7, "95367431640625"},
61 {7, "476837158203125"},
62 {7, "2384185791015625"},
63 {7, "11920928955078125"},
64 {8, "59604644775390625"},
65 {8, "298023223876953125"},
66 {8, "1490116119384765625"},
67 {9, "7450580596923828125"},
68 {9, "37252902984619140625"},
69 {9, "186264514923095703125"},
70 {10, "931322574615478515625"},
71 {10, "4656612873077392578125"},
72 {10, "23283064365386962890625"},
73 {10, "116415321826934814453125"},
74 {11, "582076609134674072265625"},
75 {11, "2910383045673370361328125"},
76 {11, "14551915228366851806640625"},
77 {12, "72759576141834259033203125"},
78 {12, "363797880709171295166015625"},
79 {12, "1818989403545856475830078125"},
80 {13, "9094947017729282379150390625"},
81 {13, "45474735088646411895751953125"},
82 {13, "227373675443232059478759765625"},
83 {13, "1136868377216160297393798828125"},
84 {14, "5684341886080801486968994140625"},
85 {14, "28421709430404007434844970703125"},
86 {14, "142108547152020037174224853515625"},
87 {15, "710542735760100185871124267578125"},
88 {15, "3552713678800500929355621337890625"},
89 {15, "17763568394002504646778106689453125"},
90 {16, "88817841970012523233890533447265625"},
91 {16, "444089209850062616169452667236328125"},
92 {16, "2220446049250313080847263336181640625"},
93 {16, "11102230246251565404236316680908203125"},
94 {17, "55511151231257827021181583404541015625"},
95 {17, "277555756156289135105907917022705078125"},
96 {17, "1387778780781445675529539585113525390625"},
97 {18, "6938893903907228377647697925567626953125"},
98 {18, "34694469519536141888238489627838134765625"},
99 {18, "173472347597680709441192448139190673828125"},
100 {19, "867361737988403547205962240695953369140625"},
103 // The maximum amount we can shift is the number of bits used in the
104 // accumulator, minus the number of bits needed to represent the base (in this
106 static constexpr uint32_t MAX_SHIFT_AMOUNT
= sizeof(uint64_t) - 4;
108 // 800 is an arbitrary number of digits, but should be
109 // large enough for any practical number.
110 static constexpr uint32_t MAX_NUM_DIGITS
= 800;
112 uint32_t num_digits
= 0;
113 int32_t decimal_point
= 0;
114 bool truncated
= false;
115 uint8_t digits
[MAX_NUM_DIGITS
];
118 bool should_round_up(int32_t roundToDigit
, RoundDirection round
) {
119 if (roundToDigit
< 0 ||
120 static_cast<uint32_t>(roundToDigit
) >= this->num_digits
) {
124 // The above condition handles all cases where all of the trailing digits
125 // are zero. In that case, if the rounding mode is up, then this number
126 // should be rounded up. Similarly, if the rounding mode is down, then it
127 // should always round down.
128 if (round
== RoundDirection::Up
) {
130 } else if (round
== RoundDirection::Down
) {
133 // Else round to nearest.
135 // If we're right in the middle and there are no extra digits
136 if (this->digits
[roundToDigit
] == 5 &&
137 static_cast<uint32_t>(roundToDigit
+ 1) == this->num_digits
) {
139 // Round up if we've truncated (since that means the result is slightly
140 // higher than what's represented.)
141 if (this->truncated
) {
145 // If this exactly halfway, round to even.
146 if (roundToDigit
== 0)
147 // When the input is ".5".
149 return this->digits
[roundToDigit
- 1] % 2 != 0;
151 // If there are digits after roundToDigit, they must be non-zero since we
152 // trim trailing zeroes after all operations that change digits.
153 return this->digits
[roundToDigit
] >= 5;
156 // Takes an amount to left shift and returns the number of new digits needed
157 // to store the result based on LEFT_SHIFT_DIGIT_TABLE.
158 uint32_t get_num_new_digits(uint32_t lShiftAmount
) {
159 const char *power_of_five
=
160 LEFT_SHIFT_DIGIT_TABLE
[lShiftAmount
].power_of_five
;
161 uint32_t new_digits
= LEFT_SHIFT_DIGIT_TABLE
[lShiftAmount
].new_digits
;
162 uint32_t digit_index
= 0;
163 while (power_of_five
[digit_index
] != 0) {
164 if (digit_index
>= this->num_digits
) {
165 return new_digits
- 1;
167 if (this->digits
[digit_index
] != power_of_five
[digit_index
] - '0') {
169 ((this->digits
[digit_index
] < power_of_five
[digit_index
] - '0')
178 // Trim all trailing 0s
179 void trim_trailing_zeroes() {
180 while (this->num_digits
> 0 && this->digits
[this->num_digits
- 1] == 0) {
183 if (this->num_digits
== 0) {
184 this->decimal_point
= 0;
188 // Perform a digitwise binary non-rounding right shift on this value by
189 // shiftAmount. The shiftAmount can't be more than MAX_SHIFT_AMOUNT to prevent
191 void right_shift(uint32_t shiftAmount
) {
192 uint32_t read_index
= 0;
193 uint32_t write_index
= 0;
195 uint64_t accumulator
= 0;
197 const uint64_t shift_mask
= (uint64_t(1) << shiftAmount
) - 1;
199 // Warm Up phase: we don't have enough digits to start writing, so just
200 // read them into the accumulator.
201 while (accumulator
>> shiftAmount
== 0) {
202 uint64_t read_digit
= 0;
203 // If there are still digits to read, read the next one, else the digit is
205 if (read_index
< this->num_digits
) {
206 read_digit
= this->digits
[read_index
];
208 accumulator
= accumulator
* 10 + read_digit
;
212 // Shift the decimal point by the number of digits it took to fill the
214 this->decimal_point
-= read_index
- 1;
216 // Middle phase: we have enough digits to write, as well as more digits to
217 // read. Keep reading until we run out of digits.
218 while (read_index
< this->num_digits
) {
219 uint64_t read_digit
= this->digits
[read_index
];
220 uint64_t write_digit
= accumulator
>> shiftAmount
;
221 accumulator
&= shift_mask
;
222 this->digits
[write_index
] = static_cast<uint8_t>(write_digit
);
223 accumulator
= accumulator
* 10 + read_digit
;
228 // Cool Down phase: All of the readable digits have been read, so just write
229 // the remainder, while treating any more digits as 0.
230 while (accumulator
> 0) {
231 uint64_t write_digit
= accumulator
>> shiftAmount
;
232 accumulator
&= shift_mask
;
233 if (write_index
< MAX_NUM_DIGITS
) {
234 this->digits
[write_index
] = static_cast<uint8_t>(write_digit
);
236 } else if (write_digit
> 0) {
237 this->truncated
= true;
239 accumulator
= accumulator
* 10;
241 this->num_digits
= write_index
;
242 this->trim_trailing_zeroes();
245 // Perform a digitwise binary non-rounding left shift on this value by
246 // shiftAmount. The shiftAmount can't be more than MAX_SHIFT_AMOUNT to prevent
248 void left_shift(uint32_t shiftAmount
) {
249 uint32_t new_digits
= this->get_num_new_digits(shiftAmount
);
251 int32_t read_index
= this->num_digits
- 1;
252 uint32_t write_index
= this->num_digits
+ new_digits
;
254 uint64_t accumulator
= 0;
256 // No Warm Up phase. Since we're putting digits in at the top and taking
257 // digits from the bottom we don't have to wait for the accumulator to fill.
259 // Middle phase: while we have more digits to read, keep reading as well as
261 while (read_index
>= 0) {
262 accumulator
+= static_cast<uint64_t>(this->digits
[read_index
])
264 uint64_t next_accumulator
= accumulator
/ 10;
265 uint64_t write_digit
= accumulator
- (10 * next_accumulator
);
267 if (write_index
< MAX_NUM_DIGITS
) {
268 this->digits
[write_index
] = static_cast<uint8_t>(write_digit
);
269 } else if (write_digit
!= 0) {
270 this->truncated
= true;
272 accumulator
= next_accumulator
;
276 // Cool Down phase: there are no more digits to read, so just write the
277 // remaining digits in the accumulator.
278 while (accumulator
> 0) {
279 uint64_t next_accumulator
= accumulator
/ 10;
280 uint64_t write_digit
= accumulator
- (10 * next_accumulator
);
282 if (write_index
< MAX_NUM_DIGITS
) {
283 this->digits
[write_index
] = static_cast<uint8_t>(write_digit
);
284 } else if (write_digit
!= 0) {
285 this->truncated
= true;
287 accumulator
= next_accumulator
;
290 this->num_digits
+= new_digits
;
291 if (this->num_digits
> MAX_NUM_DIGITS
) {
292 this->num_digits
= MAX_NUM_DIGITS
;
294 this->decimal_point
+= new_digits
;
295 this->trim_trailing_zeroes();
299 // numString is assumed to be a string of numeric characters. It doesn't
300 // handle leading spaces.
301 HighPrecisionDecimal(const char *__restrict numString
) {
302 bool saw_dot
= false;
303 // This counts the digits in the number, even if there isn't space to store
305 uint32_t total_digits
= 0;
306 while (isdigit(*numString
) || *numString
== '.') {
307 if (*numString
== '.') {
311 this->decimal_point
= total_digits
;
314 if (*numString
== '0' && this->num_digits
== 0) {
315 --this->decimal_point
;
320 if (this->num_digits
< MAX_NUM_DIGITS
) {
321 this->digits
[this->num_digits
] =
322 static_cast<uint8_t>(*numString
- '0');
324 } else if (*numString
!= '0') {
325 this->truncated
= true;
332 this->decimal_point
= total_digits
;
334 if ((*numString
| 32) == 'e') {
336 if (isdigit(*numString
) || *numString
== '+' || *numString
== '-') {
337 int32_t add_to_exp
= strtointeger
<int32_t>(numString
, 10);
338 if (add_to_exp
> 100000) {
340 } else if (add_to_exp
< -100000) {
341 add_to_exp
= -100000;
343 this->decimal_point
+= add_to_exp
;
347 this->trim_trailing_zeroes();
350 // Binary shift left (shiftAmount > 0) or right (shiftAmount < 0)
351 void shift(int shiftAmount
) {
352 if (shiftAmount
== 0) {
356 else if (shiftAmount
> 0) {
357 while (static_cast<uint32_t>(shiftAmount
) > MAX_SHIFT_AMOUNT
) {
358 this->left_shift(MAX_SHIFT_AMOUNT
);
359 shiftAmount
-= MAX_SHIFT_AMOUNT
;
361 this->left_shift(shiftAmount
);
365 while (static_cast<uint32_t>(shiftAmount
) < -MAX_SHIFT_AMOUNT
) {
366 this->right_shift(MAX_SHIFT_AMOUNT
);
367 shiftAmount
+= MAX_SHIFT_AMOUNT
;
369 this->right_shift(-shiftAmount
);
373 // Round the number represented to the closest value of unsigned int type T.
374 // This is done ignoring overflow.
376 T
round_to_integer_type(RoundDirection round
= RoundDirection::Nearest
) {
378 uint32_t cur_digit
= 0;
380 while (static_cast<int32_t>(cur_digit
) < this->decimal_point
&&
381 cur_digit
< this->num_digits
) {
382 result
= result
* 10 + (this->digits
[cur_digit
]);
386 // If there are implicit 0s at the end of the number, include those.
387 while (static_cast<int32_t>(cur_digit
) < this->decimal_point
) {
391 return result
+ this->should_round_up(this->decimal_point
, round
);
394 // Extra functions for testing.
396 uint8_t *get_digits() { return this->digits
; }
397 uint32_t get_num_digits() { return this->num_digits
; }
398 int32_t get_decimal_point() { return this->decimal_point
; }
399 void set_truncated(bool trunc
) { this->truncated
= trunc
; }
402 } // namespace internal
403 } // namespace __llvm_libc
405 #endif // LIBC_SRC_SUPPORT_HIGH_PRECISION_DECIMAL_H