Slides for P3666R1 — Bit-precise integers
- Document number:
- P3869R1
- Date:
2025-02-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/bitint-slides.cow
- →, ↓ : go to the next slide
- ←, ↑ : go to previous slide
Bit-precise integers
P3666R1
Introduction
C23 now has type for N-bit integers (WG14 [N2763], [N2775]):
P3666R1 Overview
-
Core design: copy
from C23 & paste w/ minimal changes:_BitInt -
Library design: copy from C23 & paste unchanged:
,<cmath> ,<stdbit.h> : copy C semantics<stdckdint.h> - lots of extensions to C++ standard library
P3666R1 Points of contention
- fundamental type instead of library class template
- undefined behavior on signed integer overflow
- implicit conversions inherited from C
stays the sameBITINT_MAXWIDTH keyword should exist_BitInt - (SG22: unanimous consent to author position on all)
Library class template vs fundamental type
This was discussed as [P3639R0].
The WG14 delegation to SG22 believes that the C++ type family that deliberately corresponds to _BitInt (perhaps via compatibility macros) should be... (Fundamental/Library)
SF F N L SL 8 1 1 0 0 WG21
SF F N L SL 4 5 0 0 0
- SG6: no opinion
- EWG: consensus to have a fundamental type
UB on signed integer overflow
- Problem: signed integer overflow results in UB
- Idea: well-defined for
, e.g. wrapping & erroneous (Rust-style)_BitInt - ✔️ less UB = more good, could catch bugs
- ❌ inconsistent with C
- ❌ not a general solution (
stays unchanged)int - ❌ well-defined (wrapping) behavior not very useful
- ❌ UB is useful for optimization
- Author position: problem should be approached more generally
Limiting implicit conversions (1/2)
- Problem: lots of bug-prone, questionable implicit conversions
- Idea: limit conversions for
(e.g. narrowing)_BitInt - ❌ almost impossible to specify without "false positives"
- many issues due to lack of "untyped literals" like in Rust
Limiting implicit conversions (2/2)
Author position: at most, pick low-hanging fruits (e.g. → )
Raising the BITINT_MAXWIDTH
- Problem: C only guarantees
/_BitInt ( 64 ) _BitInt ( LLONG_WIDTH ) and wider are not portable_BitInt ( 128 )
- Idea: mandate e.g.
(GCC max.)BITINT_MAXWIDTH >= 65 ' 535 - ❌
depends on system ABI:BITINT_MAXWIDTH - e.g. x86_64 psABI: Clang supports
_BitInt ( 8 ' 388 ' 608 ) - e.g. Basic C ABI for WASM: Clang supports only
_BitInt ( 128 )
- e.g. x86_64 psABI: Clang supports
- ✔️ standardizing high
pressures ecosystemBITINT_MAXWIDTH - Author position: inherit C restrictions, which are still useful
C++ standard library changes (1/2)
- § 5.1. Naming of alias template:
andbit_int proposedbit_uint - § 5.2.
,format (function template),to_chars supportto_string - § 5.3. Preventing
ABI breakranges :: iota_view - § 5.4.
andsize_t allowed to be bit-preciseptrdiff_t - § 5.5. New
overload (function template)abs - § 5.6. bit-precise ints also treated as
indouble <cmath> - § 5.7. no
support (too much design/wording effort)<random> - § 5.8.
support added only for widths of standard integerssimd
C++ standard library changes (2/2)
- § 5.9.
support addedvalarray - § 5.10.
includes bit-precise integersis_integral - § 5.11.
andmake_signed changesmake_unsigned - § 5.12. misc. support is either blanket support, or inherited from C
,<bit> , etc.<numeric> - more design/wording effort in the long run if support is delayed
- § 5.13. one feature-test macro for entire library (and one for core)
- § 5.14. bit-precise integers passed by value, despite potential large size
Questions
- Agreement with author position?
- allow
and0 wb = _BitInt ( 1 ) enum E : _BitInt ( N ) - keep UB on signed integer overflow
- keep all implicit conversions (at least in this paper)
- keep the
from CBITINT_MAXWIDTH - library changes
- allow
- Forward to EWG/LEWG?
References
[P3639R0]
The _BitInt Debate
2025-02-20
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3639r0.html
[P3666R1]
Bit-precise integers
2025-10-01
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3666r1.html
[N1744]
Big Integer Library Proposal for C++0x
2005-01-13
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1744.pdf
[P1889R1]
C++ Numerics Work In Progress
2019-12-27
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1889r1%2epdf
[P3639R0]
The _BitInt Debate
2025-02-20
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3639r0.html
[N2763]
Adding a Fundamental Type for N-bit integers
2021-06-21
https://open-std.org/JTC1/SC22/WG14/www/docs/n2763.pdf
[N2775]
Literal suffixes for bit-precise integers
2021-07-13
https://open-std.org/JTC1/SC22/WG14/www/docs/n2775.pdf