| 329 | using Base::PowerOfTen; |
| 330 | |
| 331 | static Decimal64 RoundedRightShift(const Decimal64& x, int bits) { |
| 332 | if (bits == 0) { |
| 333 | return x; |
| 334 | } |
| 335 | |
| 336 | int64_t result = x.value(); |
| 337 | uint64_t shifted = 0; |
| 338 | if (bits > 0) { |
| 339 | shifted = (static_cast<uint64_t>(result) << (64 - bits)); |
| 340 | result >>= bits; |
| 341 | } |
| 342 | constexpr uint64_t kHalf = 0x8000000000000000ULL; |
| 343 | if (shifted > kHalf) { |
| 344 | // strictly more than half => round up |
| 345 | result += 1; |
| 346 | } else if (shifted == kHalf) { |
| 347 | // exactly half => round to even |
| 348 | if ((result & 1) != 0) { |
| 349 | result += 1; |
| 350 | } |
| 351 | } else { |
| 352 | // strictly less than half => round down |
| 353 | } |
| 354 | return Decimal64(result); |
| 355 | } |
| 356 | |
| 357 | template <typename Real> |
| 358 | static Result<Decimal64> FromPositiveRealApprox(Real real, int32_t precision, |