Finite-guarded, range-clamped double → int64 (llround of NaN/out-of-range is UB).
| 16 | namespace { |
| 17 | // Finite-guarded, range-clamped double → int64 (llround of NaN/out-of-range is UB). |
| 18 | std::int64_t toInt64(double d) { |
| 19 | if (!std::isfinite(d)) { |
| 20 | return 0; |
| 21 | } |
| 22 | if (d >= 9.0e18) { |
| 23 | return std::numeric_limits<std::int64_t>::max(); |
| 24 | } |
| 25 | if (d <= -9.0e18) { |
| 26 | return std::numeric_limits<std::int64_t>::min(); |
| 27 | } |
| 28 | return static_cast<std::int64_t>(std::llround(d)); |
| 29 | } |
| 30 | |
| 31 | // Finite-guarded double → uint64. Negative/non-finite → 0; >= ~2^64 → UINT64_MAX. Avoids the |
| 32 | // llround-overflow-near-2^63 UB by truncating in the high band (values there are already past |