Commit Graph

1 Commits

Author SHA1 Message Date
Mateusz Pusz fc7eb739e7 fix: complete double_width_int operator suite so it can stand in for __int128 on MSVC (#795)
`double_width_int` was previously missing a number of operators that generic numerical code
expects from a 128-bit integer.  On platforms with native `__int128` this was harmless, but on
MSVC (where `int128_t` / `uint128_t` alias `double_width_int<(u)int64_t>`) the recent
`UsesIntegerScaling` change wired the synthetic dwint into concept checks, and `compare_quantities`
+ magnitude folding into runtime paths, exposing every gap.

This change rounds out the operator set so `double_width_int` truly behaves as an integer type:

* binary `+`, `-`, `*`, `/` between two `double_width_int`s (alongside the existing narrow-rhs
  overloads).
* unary `~` and binary `&`, `|`, `^`.
* compound assignment for arithmetic, bitwise, and shift operations.
* pre/post `++` and `--`.
* `static_cast<long double>` (and `double`/`float`) with sign-preserving conversion that avoids
  catastrophic cancellation on platforms where `long double == double`.
* `std::numeric_limits<double_width_int<T>>` specialization so generic code probing `::max()`,
  `::min()`, `::digits`, `::is_signed`, etc. gets correct answers (needed by
  `checked_int_pow`, `compute_base_power`, `safe_int::operator-`).
* fields `hi_` / `lo_` and the `(hi, lo)` ctor are public so cross-instance inline friend
  operators can access each other without further friend declarations.

While here, also:
* extract `double_width_int` (and its `std::numeric_limits` specialization) into a dedicated
  header `bits/double_width_int.h`; `fixed_point.h` now just includes it and keeps the
  `int128_t` aliases, `min_width_uint_t` / `double_width_int_for_t` / `wide_product_of`
  helpers, and the `fixed_point` class itself.
* rename a local variable `m` in `wide_product_of` to `mid` to avoid shadowing
  `si::unit_symbols::m` (MSVC C4459).
* rewrite the `lo_ > 0 ? -1 : 0` unary-minus body to use `Tl{0} - lo_` instead of `-lo_`,
  silencing MSVC C4146 about unary minus on an unsigned operand.

Tests:
* new `test/static/double_width_int_test.cpp` pins down each new operator at compile time
  (carry/borrow edges, schoolbook multiplication, narrow/wide division paths, bitwise
  identities, numeric_limits values, long-double round-trips).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 23:19:38 +02:00