Slides for P3666R1 — Bit-precise integers
- Document number:
- P3869R0
- Date:
2025-10-26 - Audience:
- SG22
- 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:
: treat all integers as<cmath> double : allow<stdbit.h> of same width as standard integer_BitInt ( N ) : allow<stdckdint.h> only as target type_BitInt ( N )
P3666R1 Points of contention
- library class template vs fundamental type
- undefined behavior on signed integer overflow
- removing some implicit conversions
- raising the
BITINT_MAXWIDTH keyword and_BitInt alias templatestd :: bit_int - (CWG/LWG: use of
keyword in specification)_BitInt
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
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
_BitInt keyword and std :: bit_int alias
- Problem A:
useful for C-interoperable headers_BitInt andsigned _BitInt unsigned _BitInt - deducing
fromN _BitInt ( N ) -like compat. macro for_Atomic is convolutedbit-int ( N )
- Problem B:
is a better fit for C++ thanstd :: bit_int < N > _BitInt ( N ) - Author position:
- both spellings are useful
should be a keyword_BitInt
Questions
- Agreement with author position?
- fundamental type better than class template
- 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 -
add both a
keyword and alias templates (_BitInt andstd :: bit_int )std :: bit_uint
- Forward to EWG?
References
[P3639R0]
The _BitInt Debate
2025-02-20
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3639r0.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