C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Modular integers

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Tue, 13 Jan 2026 18:03:42 +0100
wt., 13 sty 2026 o 17:59 Hans Åberg via Std-Proposals
<std-proposals_at_[hidden]> napisał(a):
>
>
>
> > On 13 Jan 2026, at 16:08, Jan Schultke <janschultke_at_[hidden]> wrote:
> >
> >> > Why would templates be able to be faster than _BitInt()? Is there any
> >> > inherent issue in _BitInt() that doesn't allow compilers to implement it
> >> > efficiently?
> >>
> >> The templates are recursive, so they halve the words until one can replace it with hardware-supported assembler. The template functions have no backward jumps, encouraging pipelining, which is very complicated to write by hand if one were to iterate it.
> >>
> >> Say it is on a 64-bit platform. For division, if there is divq, one can use it; if not, it will recurse down to a 32-bit implementation. If one is on a larger word size, one can use it by specializing an inline function.
> >>
> > None of these seem like "inherent issues". Everything you're describing boils down to the quality of lowering say, a 128-bit operation to the underlying 64-bit or 32-bit operations supported by hardware. That is, purely non-observable performance characteristics of implementation details.
>
> This is a reply to a question about implementation.
>
> > Compilers already do a reasonably good job at lowering _BitInt(128) and _BitInt(4096) operations, and there is no innate reason why they would do a better job at _BitModInt(4096) or mod_int<4096> or whatever you're proposing. I don't see any scenario in which your proposed mod_int wouldn't just be an alias template for unsigned _BitInt.
>
> It is important for pipelining to avoid backward jumps, like in loops. Despite Clang having an efficient 2-word by 1-word 64-bit implementation, its 2-word by 2-word division uses binary division, which is slow. In addition, 2-word by 1-word division can be optimized to only use one multiplication per half-word.
>

And how do you prevent that in your `int_mod<m>` not use the same
exact implementation? Like they implement `int_mod` using `_BitInt`
and call it a day?

> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2026-01-13 17:03:59