1 //===-- High Precision Decimal ----------------------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LIBC_SRC_SUPPORT_HIGH_PRECISION_DECIMAL_H 10 #define LIBC_SRC_SUPPORT_HIGH_PRECISION_DECIMAL_H 11 12 #include "src/__support/ctype_utils.h" 13 #include "src/__support/str_to_integer.h" 14 #include <stdint.h> 15 16 namespace __llvm_libc { 17 namespace internal { 18 19 struct LShiftTableEntry { 20 uint32_t new_digits; 21 char const *power_of_five; 22 }; 23 24 // This is based on the HPD data structure described as part of the Simple 25 // Decimal Conversion algorithm by Nigel Tao, described at this link: 26 // https://nigeltao.github.io/blog/2020/parse-number-f64-simple.html 27 class HighPrecisionDecimal { 28 29 // This precomputed table speeds up left shifts by having the number of new 30 // digits that will be added by multiplying 5^i by 2^i. If the number is less 31 // than 5^i then it will add one fewer digit. There are only 60 entries since 32 // that's the max shift amount. 33 // This table was generated by the script at 34 // libc/utils/mathtools/GenerateHPDConstants.py 35 static constexpr LShiftTableEntry LEFT_SHIFT_DIGIT_TABLE[] = { 36 {0, ""}, 37 {1, "5"}, 38 {1, "25"}, 39 {1, "125"}, 40 {2, "625"}, 41 {2, "3125"}, 42 {2, "15625"}, 43 {3, "78125"}, 44 {3, "390625"}, 45 {3, "1953125"}, 46 {4, "9765625"}, 47 {4, "48828125"}, 48 {4, "244140625"}, 49 {4, "1220703125"}, 50 {5, "6103515625"}, 51 {5, "30517578125"}, 52 {5, "152587890625"}, 53 {6, "762939453125"}, 54 {6, "3814697265625"}, 55 {6, "19073486328125"}, 56 {7, "95367431640625"}, 57 {7, "476837158203125"}, 58 {7, "2384185791015625"}, 59 {7, "11920928955078125"}, 60 {8, "59604644775390625"}, 61 {8, "298023223876953125"}, 62 {8, "1490116119384765625"}, 63 {9, "7450580596923828125"}, 64 {9, "37252902984619140625"}, 65 {9, "186264514923095703125"}, 66 {10, "931322574615478515625"}, 67 {10, "4656612873077392578125"}, 68 {10, "23283064365386962890625"}, 69 {10, "116415321826934814453125"}, 70 {11, "582076609134674072265625"}, 71 {11, "2910383045673370361328125"}, 72 {11, "14551915228366851806640625"}, 73 {12, "72759576141834259033203125"}, 74 {12, "363797880709171295166015625"}, 75 {12, "1818989403545856475830078125"}, 76 {13, "9094947017729282379150390625"}, 77 {13, "45474735088646411895751953125"}, 78 {13, "227373675443232059478759765625"}, 79 {13, "1136868377216160297393798828125"}, 80 {14, "5684341886080801486968994140625"}, 81 {14, "28421709430404007434844970703125"}, 82 {14, "142108547152020037174224853515625"}, 83 {15, "710542735760100185871124267578125"}, 84 {15, "3552713678800500929355621337890625"}, 85 {15, "17763568394002504646778106689453125"}, 86 {16, "88817841970012523233890533447265625"}, 87 {16, "444089209850062616169452667236328125"}, 88 {16, "2220446049250313080847263336181640625"}, 89 {16, "11102230246251565404236316680908203125"}, 90 {17, "55511151231257827021181583404541015625"}, 91 {17, "277555756156289135105907917022705078125"}, 92 {17, "1387778780781445675529539585113525390625"}, 93 {18, "6938893903907228377647697925567626953125"}, 94 {18, "34694469519536141888238489627838134765625"}, 95 {18, "173472347597680709441192448139190673828125"}, 96 {19, "867361737988403547205962240695953369140625"}, 97 }; 98 99 // The maximum amount we can shift is the number of bits used in the 100 // accumulator, minus the number of bits needed to represent the base (in this 101 // case 4). 102 static constexpr uint32_t MAX_SHIFT_AMOUNT = sizeof(uint64_t) - 4; 103 104 // 800 is an arbitrary number of digits, but should be 105 // large enough for any practical number. 106 static constexpr uint32_t MAX_NUM_DIGITS = 800; 107 108 uint32_t num_digits = 0; 109 int32_t decimal_point = 0; 110 bool truncated = false; 111 uint8_t digits[MAX_NUM_DIGITS]; 112 113 private: 114 bool should_round_up(uint32_t roundToDigit) { 115 if (roundToDigit < 0 || roundToDigit >= this->num_digits) { 116 return false; 117 } 118 119 // If we're right in the middle and there are no extra digits 120 if (this->digits[roundToDigit] == 5 && 121 roundToDigit + 1 == this->num_digits) { 122 123 // Round up if we've truncated (since that means the result is slightly 124 // higher than what's represented.) 125 if (this->truncated) { 126 return true; 127 } 128 129 // If this exactly halfway, round to even. 130 return this->digits[roundToDigit - 1] % 2 != 0; 131 } 132 // If there are digits after roundToDigit, they must be non-zero since we 133 // trim trailing zeroes after all operations that change digits. 134 return this->digits[roundToDigit] >= 5; 135 } 136 137 // Takes an amount to left shift and returns the number of new digits needed 138 // to store the result based on LEFT_SHIFT_DIGIT_TABLE. 139 uint32_t get_num_new_digits(uint32_t lShiftAmount) { 140 const char *power_of_five = 141 LEFT_SHIFT_DIGIT_TABLE[lShiftAmount].power_of_five; 142 uint32_t new_digits = LEFT_SHIFT_DIGIT_TABLE[lShiftAmount].new_digits; 143 uint32_t digit_index = 0; 144 while (power_of_five[digit_index] != 0) { 145 if (digit_index >= this->num_digits) { 146 return new_digits - 1; 147 } 148 if (this->digits[digit_index] != power_of_five[digit_index] - '0') { 149 return new_digits - 150 ((this->digits[digit_index] < power_of_five[digit_index] - '0') 151 ? 1 152 : 0); 153 } 154 ++digit_index; 155 } 156 return new_digits; 157 } 158 159 // Trim all trailing 0s 160 void trim_trailing_zeroes() { 161 while (this->num_digits > 0 && this->digits[this->num_digits - 1] == 0) { 162 --this->num_digits; 163 } 164 if (this->num_digits == 0) { 165 this->decimal_point = 0; 166 } 167 } 168 169 // Perform a digitwise binary non-rounding right shift on this value by 170 // shiftAmount. The shiftAmount can't be more than MAX_SHIFT_AMOUNT to prevent 171 // overflow. 172 void right_shift(uint32_t shiftAmount) { 173 uint32_t read_index = 0; 174 uint32_t write_index = 0; 175 176 uint64_t accumulator = 0; 177 178 const uint64_t shift_mask = (uint64_t(1) << shiftAmount) - 1; 179 180 // Warm Up phase: we don't have enough digits to start writing, so just 181 // read them into the accumulator. 182 while (accumulator >> shiftAmount == 0) { 183 uint64_t read_digit = 0; 184 // If there are still digits to read, read the next one, else the digit is 185 // assumed to be 0. 186 if (read_index < this->num_digits) { 187 read_digit = this->digits[read_index]; 188 } 189 accumulator = accumulator * 10 + read_digit; 190 ++read_index; 191 } 192 193 // Shift the decimal point by the number of digits it took to fill the 194 // accumulator. 195 this->decimal_point -= read_index - 1; 196 197 // Middle phase: we have enough digits to write, as well as more digits to 198 // read. Keep reading until we run out of digits. 199 while (read_index < this->num_digits) { 200 uint64_t read_digit = this->digits[read_index]; 201 uint64_t write_digit = accumulator >> shiftAmount; 202 accumulator &= shift_mask; 203 this->digits[write_index] = static_cast<uint8_t>(write_digit); 204 accumulator = accumulator * 10 + read_digit; 205 ++read_index; 206 ++write_index; 207 } 208 209 // Cool Down phase: All of the readable digits have been read, so just write 210 // the remainder, while treating any more digits as 0. 211 while (accumulator > 0) { 212 uint64_t write_digit = accumulator >> shiftAmount; 213 accumulator &= shift_mask; 214 if (write_index < MAX_NUM_DIGITS) { 215 this->digits[write_index] = static_cast<uint8_t>(write_digit); 216 ++write_index; 217 } else if (write_digit > 0) { 218 this->truncated = true; 219 } 220 accumulator = accumulator * 10; 221 } 222 this->num_digits = write_index; 223 this->trim_trailing_zeroes(); 224 } 225 226 // Perform a digitwise binary non-rounding left shift on this value by 227 // shiftAmount. The shiftAmount can't be more than MAX_SHIFT_AMOUNT to prevent 228 // overflow. 229 void left_shift(uint32_t shiftAmount) { 230 uint32_t new_digits = this->get_num_new_digits(shiftAmount); 231 232 int32_t read_index = this->num_digits - 1; 233 uint32_t write_index = this->num_digits + new_digits; 234 235 uint64_t accumulator = 0; 236 237 // No Warm Up phase. Since we're putting digits in at the top and taking 238 // digits from the bottom we don't have to wait for the accumulator to fill. 239 240 // Middle phase: while we have more digits to read, keep reading as well as 241 // writing. 242 while (read_index >= 0) { 243 accumulator += static_cast<uint64_t>(this->digits[read_index]) 244 << shiftAmount; 245 uint64_t next_accumulator = accumulator / 10; 246 uint64_t write_digit = accumulator - (10 * next_accumulator); 247 --write_index; 248 if (write_index < MAX_NUM_DIGITS) { 249 this->digits[write_index] = static_cast<uint8_t>(write_digit); 250 } else if (write_digit != 0) { 251 this->truncated = true; 252 } 253 accumulator = next_accumulator; 254 --read_index; 255 } 256 257 // Cool Down phase: there are no more digits to read, so just write the 258 // remaining digits in the accumulator. 259 while (accumulator > 0) { 260 uint64_t next_accumulator = accumulator / 10; 261 uint64_t write_digit = accumulator - (10 * next_accumulator); 262 --write_index; 263 if (write_index < MAX_NUM_DIGITS) { 264 this->digits[write_index] = static_cast<uint8_t>(write_digit); 265 } else if (write_digit != 0) { 266 this->truncated = true; 267 } 268 accumulator = next_accumulator; 269 } 270 271 this->num_digits += new_digits; 272 if (this->num_digits > MAX_NUM_DIGITS) { 273 this->num_digits = MAX_NUM_DIGITS; 274 } 275 this->decimal_point += new_digits; 276 this->trim_trailing_zeroes(); 277 } 278 279 public: 280 // numString is assumed to be a string of numeric characters. It doesn't 281 // handle leading spaces. 282 HighPrecisionDecimal(const char *__restrict numString) { 283 bool saw_dot = false; 284 while (isdigit(*numString) || *numString == '.') { 285 if (*numString == '.') { 286 if (saw_dot) { 287 break; 288 } 289 this->decimal_point = this->num_digits; 290 saw_dot = true; 291 } else { 292 if (*numString == '0' && this->num_digits == 0) { 293 --this->decimal_point; 294 ++numString; 295 continue; 296 } 297 if (this->num_digits < MAX_NUM_DIGITS) { 298 this->digits[this->num_digits] = *numString - '0'; 299 ++this->num_digits; 300 } else if (*numString != '0') { 301 this->truncated = true; 302 } 303 } 304 ++numString; 305 } 306 307 if (!saw_dot) { 308 this->decimal_point = this->num_digits; 309 } 310 311 if ((*numString | 32) == 'e') { 312 ++numString; 313 if (isdigit(*numString) || *numString == '+' || *numString == '-') { 314 int32_t add_to_exp = strtointeger<int32_t>(numString, nullptr, 10); 315 if (add_to_exp > 100000) { 316 add_to_exp = 100000; 317 } else if (add_to_exp < -100000) { 318 add_to_exp = -100000; 319 } 320 this->decimal_point += add_to_exp; 321 } 322 } 323 324 this->trim_trailing_zeroes(); 325 } 326 327 // Binary shift left (shiftAmount > 0) or right (shiftAmount < 0) 328 void shift(int shiftAmount) { 329 if (shiftAmount == 0) { 330 return; 331 } 332 // Left 333 else if (shiftAmount > 0) { 334 while (static_cast<uint32_t>(shiftAmount) > MAX_SHIFT_AMOUNT) { 335 this->left_shift(MAX_SHIFT_AMOUNT); 336 shiftAmount -= MAX_SHIFT_AMOUNT; 337 } 338 this->left_shift(shiftAmount); 339 } 340 // Right 341 else { 342 while (static_cast<uint32_t>(shiftAmount) < -MAX_SHIFT_AMOUNT) { 343 this->right_shift(MAX_SHIFT_AMOUNT); 344 shiftAmount += MAX_SHIFT_AMOUNT; 345 } 346 this->right_shift(-shiftAmount); 347 } 348 } 349 350 // Round the number represented to the closest value of unsigned int type T. 351 // This is done ignoring overflow. 352 template <class T> T round_to_integer_type() { 353 T result = 0; 354 uint32_t cur_digit = 0; 355 356 while (static_cast<int32_t>(cur_digit) < this->decimal_point && 357 cur_digit < this->num_digits) { 358 result = result * 10 + (this->digits[cur_digit]); 359 ++cur_digit; 360 } 361 362 // If there are implicit 0s at the end of the number, include those. 363 while (static_cast<int32_t>(cur_digit) < this->decimal_point) { 364 result *= 10; 365 ++cur_digit; 366 } 367 if (this->should_round_up(this->decimal_point)) { 368 ++result; 369 } 370 return result; 371 } 372 373 // Extra functions for testing. 374 375 uint8_t *get_digits() { return this->digits; } 376 uint32_t get_num_digits() { return this->num_digits; } 377 int32_t get_decimal_point() { return this->decimal_point; } 378 void set_truncated(bool trunc) { this->truncated = trunc; } 379 }; 380 381 } // namespace internal 382 } // namespace __llvm_libc 383 384 #endif // LIBC_SRC_SUPPORT_HIGH_PRECISION_DECIMAL_H 385