Slides for P3724R1 — Integer division
- Document number:
- P3895R0
- Date:
2025-03-11 - Audience:
- SG6
- Project:
- ISO/IEC 14882 Programming Languages — C++, ISO/IEC JTC1/SC22/WG21
- Reply-to:
- Jan Schultke <janschultke@gmail.com>
- Source:
- github.com/Eisenwave/cpp-proposals/blob/master/src/intdiv-slides.cow
- →, ↓ : go to the next slide
- ←, ↑ : go to previous slide
Integer division
P3724R1
Introduction
- C++ supports integer division with rounding towards zero
- many other rounding modes are useful:
-
hardware division always rounds towards zero
- only mode where
doesn't overflowr = x - y * q
- only mode where
- software division in other langs. sometimes supports other modes
Why does this need to be in the standard?
- users need it all the time, and try to implement it
- they fail miserably: almost all attempts on StackOverflow/blogs wrong
Mistake: incrementing negative quotients:
- pulling in third-party library for one division function is silly
Computing remainders is hard
overflows:
-
Conclusion: provide functions for computing quotient and remainder
Which rounding modes to support
📘() |
|
|
📘() |
📘() |
|
📘() |
|
⚖️ |
⚖️📘 |
⚖️ |
⚖️ |
- marked functions are must-have, others fill the gaps
- ⚖️ — unbiased | 📘 — ISO/IEC 60559
- each
function hasdiv_ mode counterpart (24 total)div_rem_ mode
Library interface
- no
because narrow contract (Preconditions:)noexcept - no
constant template- or run-time parameterrounding_mode ismod div_rem_to_neg_inf ( x , y ) . remainder - as in , and as in
in Haskell, Ada, CSS, etc.mod
- as in , and as in
Other design considerations
- no SIMD overloads, but could be added in the future
- branchless implementation possible
- no functions that compute only remainder (except
)mod - these are not usually needed
- undesirable remainder sign for most rounding modes
- all functions are in
<numeric> - feature-test macro:
__cpp_lib_integer_division
Implementation experience
- Appendix A has simplified implementation for educational purposes
eisenwave/integer-division [GitHub] repo has full implementation- all functions can be made branchless and are suitable for SIMD port
and .
Questions
- Agreement with overall design?
- SIMD overloads in another proposal?
- Forward to LEWG?
References
[P3724R1]
Integer division
2025-09-29
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3724r1.html
[StackOverflowCeil]
Fast ceiling of an integer division in C / C++
https://stackoverflow.com/q/2745074/5740428