Removes data from the given significand until it is no more precise than is required for the desired precision.
| 3610 | /// Removes data from the given significand until it is no more |
| 3611 | /// precise than is required for the desired precision. |
| 3612 | void AdjustToPrecision(APInt &significand, |
| 3613 | int &exp, unsigned FormatPrecision) { |
| 3614 | unsigned bits = significand.getActiveBits(); |
| 3615 | |
| 3616 | // 196/59 is a very slight overestimate of lg_2(10). |
| 3617 | unsigned bitsRequired = (FormatPrecision * 196 + 58) / 59; |
| 3618 | |
| 3619 | if (bits <= bitsRequired) return; |
| 3620 | |
| 3621 | unsigned tensRemovable = (bits - bitsRequired) * 59 / 196; |
| 3622 | if (!tensRemovable) return; |
| 3623 | |
| 3624 | exp += tensRemovable; |
| 3625 | |
| 3626 | APInt divisor(significand.getBitWidth(), 1); |
| 3627 | APInt powten(significand.getBitWidth(), 10); |
| 3628 | while (true) { |
| 3629 | if (tensRemovable & 1) |
| 3630 | divisor *= powten; |
| 3631 | tensRemovable >>= 1; |
| 3632 | if (!tensRemovable) break; |
| 3633 | powten *= powten; |
| 3634 | } |
| 3635 | |
| 3636 | significand = significand.udiv(divisor); |
| 3637 | |
| 3638 | // Truncate the significand down to its active bit count. |
| 3639 | significand = significand.trunc(significand.getActiveBits()); |
| 3640 | } |
| 3641 | |
| 3642 | |
| 3643 | void AdjustToPrecision(SmallVectorImpl<char> &buffer, |