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_conv_utils.h" 14 #include <stdint.h> 15 16 namespace __llvm_libc { 17 namespace internal { 18 19 struct LShiftTableEntry { 20 uint32_t newDigits; 21 char const *powerOfFive; 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 HighPrecsisionDecimal { 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 numDigits = 0; 109 int32_t decimalPoint = 0; 110 bool truncated = false; 111 uint8_t digits[MAX_NUM_DIGITS]; 112 113 private: 114 bool shouldRoundUp(uint32_t roundToDigit) { 115 if (roundToDigit < 0 || roundToDigit >= this->numDigits) { 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->numDigits) { 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 getNumNewDigits(uint32_t lShiftAmount) { 140 const char *powerOfFive = LEFT_SHIFT_DIGIT_TABLE[lShiftAmount].powerOfFive; 141 uint32_t newDigits = LEFT_SHIFT_DIGIT_TABLE[lShiftAmount].newDigits; 142 uint32_t digitIndex = 0; 143 while (powerOfFive[digitIndex] != 0) { 144 if (digitIndex >= this->numDigits) { 145 return newDigits - 1; 146 } 147 if (this->digits[digitIndex] != powerOfFive[digitIndex] - '0') { 148 return newDigits - 149 ((this->digits[digitIndex] < powerOfFive[digitIndex] - '0') ? 1 150 : 0); 151 } 152 ++digitIndex; 153 } 154 return newDigits; 155 } 156 157 // Trim all trailing 0s 158 void trimTrailingZeroes() { 159 while (this->numDigits > 0 && this->digits[this->numDigits - 1] == 0) { 160 --this->numDigits; 161 } 162 if (this->numDigits == 0) { 163 this->decimalPoint = 0; 164 } 165 } 166 167 // Perform a digitwise binary non-rounding right shift on this value by 168 // shiftAmount. The shiftAmount can't be more than MAX_SHIFT_AMOUNT to prevent 169 // overflow. 170 void rightShift(uint32_t shiftAmount) { 171 uint32_t readIndex = 0; 172 uint32_t writeIndex = 0; 173 174 uint64_t accumulator = 0; 175 176 const uint64_t shiftMask = (uint64_t(1) << shiftAmount) - 1; 177 178 // Warm Up phase: we don't have enough digits to start writing, so just 179 // read them into the accumulator. 180 while (accumulator >> shiftAmount == 0) { 181 uint64_t readDigit = 0; 182 // If there are still digits to read, read the next one, else the digit is 183 // assumed to be 0. 184 if (readIndex < this->numDigits) { 185 readDigit = this->digits[readIndex]; 186 } 187 accumulator = accumulator * 10 + readDigit; 188 ++readIndex; 189 } 190 191 // Shift the decimal point by the number of digits it took to fill the 192 // accumulator. 193 this->decimalPoint -= readIndex - 1; 194 195 // Middle phase: we have enough digits to write, as well as more digits to 196 // read. Keep reading until we run out of digits. 197 while (readIndex < this->numDigits) { 198 uint64_t readDigit = this->digits[readIndex]; 199 uint64_t writeDigit = accumulator >> shiftAmount; 200 accumulator &= shiftMask; 201 this->digits[writeIndex] = static_cast<uint8_t>(writeDigit); 202 accumulator = accumulator * 10 + readDigit; 203 ++readIndex; 204 ++writeIndex; 205 } 206 207 // Cool Down phase: All of the readable digits have been read, so just write 208 // the remainder, while treating any more digits as 0. 209 while (accumulator > 0) { 210 uint64_t writeDigit = accumulator >> shiftAmount; 211 accumulator &= shiftMask; 212 if (writeIndex < MAX_NUM_DIGITS) { 213 this->digits[writeIndex] = static_cast<uint8_t>(writeDigit); 214 ++writeIndex; 215 } else if (writeDigit > 0) { 216 this->truncated = true; 217 } 218 accumulator = accumulator * 10; 219 } 220 this->numDigits = writeIndex; 221 this->trimTrailingZeroes(); 222 } 223 224 // Perform a digitwise binary non-rounding left shift on this value by 225 // shiftAmount. The shiftAmount can't be more than MAX_SHIFT_AMOUNT to prevent 226 // overflow. 227 void leftShift(uint32_t shiftAmount) { 228 uint32_t newDigits = this->getNumNewDigits(shiftAmount); 229 230 int32_t readIndex = this->numDigits - 1; 231 uint32_t writeIndex = this->numDigits + newDigits; 232 233 uint64_t accumulator = 0; 234 235 // No Warm Up phase. Since we're putting digits in at the top and taking 236 // digits from the bottom we don't have to wait for the accumulator to fill. 237 238 // Middle phase: while we have more digits to read, keep reading as well as 239 // writing. 240 while (readIndex >= 0) { 241 accumulator += static_cast<uint64_t>(this->digits[readIndex]) 242 << shiftAmount; 243 uint64_t nextAccumulator = accumulator / 10; 244 uint64_t writeDigit = accumulator - (10 * nextAccumulator); 245 --writeIndex; 246 if (writeIndex < MAX_NUM_DIGITS) { 247 this->digits[writeIndex] = static_cast<uint8_t>(writeDigit); 248 } else if (writeDigit != 0) { 249 this->truncated = true; 250 } 251 accumulator = nextAccumulator; 252 --readIndex; 253 } 254 255 // Cool Down phase: there are no more digits to read, so just write the 256 // remaining digits in the accumulator. 257 while (accumulator > 0) { 258 uint64_t nextAccumulator = accumulator / 10; 259 uint64_t writeDigit = accumulator - (10 * nextAccumulator); 260 --writeIndex; 261 if (writeIndex < MAX_NUM_DIGITS) { 262 this->digits[writeIndex] = static_cast<uint8_t>(writeDigit); 263 } else if (writeDigit != 0) { 264 this->truncated = true; 265 } 266 accumulator = nextAccumulator; 267 } 268 269 this->numDigits += newDigits; 270 if (this->numDigits > MAX_NUM_DIGITS) { 271 this->numDigits = MAX_NUM_DIGITS; 272 } 273 this->decimalPoint += newDigits; 274 this->trimTrailingZeroes(); 275 } 276 277 public: 278 // numString is assumed to be a string of numeric characters. It doesn't 279 // handle leading spaces. 280 HighPrecsisionDecimal(const char *__restrict numString) { 281 bool sawDot = false; 282 bool sawDigit = false; 283 while (isdigit(*numString) || *numString == '.') { 284 if (*numString == '.') { 285 if (sawDot) { 286 break; 287 } 288 this->decimalPoint = this->numDigits; 289 sawDot = true; 290 } else { 291 sawDigit = true; 292 if (*numString == '0' && this->numDigits == 0) { 293 --this->decimalPoint; 294 ++numString; 295 continue; 296 } 297 if (this->numDigits < MAX_NUM_DIGITS) { 298 this->digits[this->numDigits] = *numString - '0'; 299 ++this->numDigits; 300 } else if (*numString != '0') { 301 this->truncated = true; 302 } 303 } 304 ++numString; 305 } 306 307 if (!sawDot) { 308 this->decimalPoint = this->numDigits; 309 } 310 311 if ((*numString | 32) == 'e') { 312 ++numString; 313 if (isdigit(*numString) || *numString == '+' || *numString == '-') { 314 int32_t addToExp = strtointeger<int32_t>(numString, nullptr, 10); 315 if (addToExp > 100000) { 316 addToExp = 100000; 317 } else if (addToExp < -100000) { 318 addToExp = -100000; 319 } 320 this->decimalPoint += addToExp; 321 } 322 } 323 324 this->trimTrailingZeroes(); 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->leftShift(MAX_SHIFT_AMOUNT); 336 shiftAmount -= MAX_SHIFT_AMOUNT; 337 } 338 this->leftShift(shiftAmount); 339 } 340 // Right 341 else { 342 while (static_cast<uint32_t>(shiftAmount) < -MAX_SHIFT_AMOUNT) { 343 this->rightShift(MAX_SHIFT_AMOUNT); 344 shiftAmount += MAX_SHIFT_AMOUNT; 345 } 346 this->rightShift(-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 roundToIntegerType() { 353 T result = 0; 354 uint32_t curDigit = 0; 355 356 while (static_cast<int32_t>(curDigit) < this->decimalPoint && 357 curDigit < this->numDigits) { 358 result = result * 10 + (this->digits[curDigit]); 359 ++curDigit; 360 } 361 362 // If there are implicit 0s at the end of the number, include those. 363 while (static_cast<int32_t>(curDigit) < this->decimalPoint) { 364 result *= 10; 365 ++curDigit; 366 } 367 if (this->shouldRoundUp(this->decimalPoint)) { 368 ++result; 369 } 370 return result; 371 } 372 373 // Extra functions for testing. 374 375 uint8_t *getDigits() { return this->digits; } 376 uint32_t getNumDigits() { return this->numDigits; } 377 int32_t getDecimalPoint() { return this->decimalPoint; } 378 void setTruncated(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