C++ Logo


Advanced search

Re: [std-proposals] Integer overflow arithmetic

From: Tiago Freire <tmiguelf_at_[hidden]>
Date: Sat, 17 Feb 2024 15:00:39 +0000

Double-wide remainders are important for safe modular arithmetic. For
example, std::linear_congruential_generator is commonly implemented
using double-wide arithmetic through __int128. However, it would be
desirable to have a general solution for any integer type.

Your div_wide function does not cover this use case because it has the
precondition is_div_wide_defined. A rem_wide function would have a
wider contract. Only division by zero and the edge case of HUGE / -1
are problematic, but the remainder always fits into a 64-bit number.

> FWIW, for std::linear_congruential_generator the integers involved are unsigned and the remainder is a compile-time constant that can't be zero, so neither of those cases is a problem. That doesn't mean it couldn't be useful in other situations, but isn't needed for std::linear_congruential_generator.

 That was my thought, being a modular math, I had assumed it was unsigned, and for unsigned you can extend the domain of usability for the remainder using the following algorithm:

//assume dividend != 0
div_result<uint64_t> temporary = dive_wide(0, divisor_hi, dividend);
temporary = dive_wide(temporary.remainder, divisor_low, dividend);
return temporary.remainder;

That’s why these are so useful.

Received on 2024-02-17 15:00:43