C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Integer overflow arithmetic with exceptions

From: Hans Åberg <haberg_1_at_[hidden]>
Date: Thu, 26 Jun 2025 14:41:21 +0200
I also implemented the division algorithm one-word optimization in a way that so if one has the functions in your proposal for an unsigned Word, then one can also use that word size for the multiprecision type basic_natural<Word>. The division algorithm is otherwise typically used for the half-word size; for example, on a 64-bit computer, one uses a 32-bit unsigned word for the multiprecision, with some signed 64-bit word for its implementation, as the algorithm from the 1990s is said to be. —Incidentally, I did not see any dramatic improvement with optimization; perhaps the overhead is too great, or the computers have changed since the 1990s.

The C++ does not have a way to indicate the arithmetically optimal word, it seems. So, for example, arm64 does not have the x86_64 divq assembler operations, but curiously both Clang and GCC have a type unsigned __int128 on Mac OS. Suppose this is used to define uint128_t, then it would not be arithmetically optimal, but uint64_t and using uintmax_t would go wrong.

Other things that might be useful in the C++ standard: I implemented the functions in your proposal with templates, making use of a double word if available; otherwise, one can use splitting into half words. For this to work, one must for any given word have templates for the double and half-sized words.

Also, the division algorithm I implemented is arbitrary size. One can also have fixed-size multiprecision types: the division algorithm implementations often have such variations. So there might be some templates for that.

Received on 2025-06-26 12:41:35