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

This document has custom controls:

  • ,  ↓ : go to the next slide
  • ,  ↑ : go to previous slide

Bit-precise integers
P3666R1

Jan Schultke  |  Slides for P3666R1 — Bit-precise integers  |  SG22 Telecon 2025-10-08  |  Slide 1

Introduction

C23 now has _BitInt type for N-bit integers (WG14 [N2763], [N2775]):

// 8-bit unsigned integer initialized with value 255. // The literal suffix wb is unnecessary in this case. unsigned _BitInt(8) x = 0xFFwb;
Jan Schultke  |  Slides for P3666R1 — Bit-precise integers  |  SG22 Telecon 2025-10-08  |  Slide 2

P3666R1 Overview

Jan Schultke  |  Slides for P3666R1 — Bit-precise integers  |  SG22 Telecon 2025-10-08  |  Slide 3

P3666R1 Points of contention

Jan Schultke  |  Slides for P3666R1 — Bit-precise integers  |  SG22 Telecon 2025-10-08  |  Slide 4

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)

SFFNLSL
81100

WG21

SFFNLSL
45000

EWG: consensus to have a fundamental type.

Jan Schultke  |  Slides for P3666R1 — Bit-precise integers  |  SG22 Telecon 2025-10-08  |  Slide 5

UB on signed integer overflow

Jan Schultke  |  Slides for P3666R1 — Bit-precise integers  |  SG22 Telecon 2025-10-08  |  Slide 6

Limiting implicit conversions (1/2)

// Problems with C/C++ interoperable headers: unsigned _BitInt(8) bit = 0; unsigned _BitInt(3) max3u = -1; // Problems with "false rejections" for safe cases: bit &= 0xf; // int → unsigned _BitInt(8)
Jan Schultke  |  Slides for P3666R1 — Bit-precise integers  |  SG22 Telecon 2025-10-08  |  Slide 7

Limiting implicit conversions (2/2)

template<std::integral T> T div_ceil(T x, T y) { // performs integer division, rounding to +∞ // ⚠️ Could be mixed-sign comparison: bool quotient_positive = (x ^ y) >= 0; // ⚠️ Could be mixed-sign comparison bool adjust = x % y != 0 && quotient_positive; // ⚠️ Could be mixed-sign addition between int (0 or 1) // and unsigned _BitInt(N) "x / y": // ⚠️ Could be lossy conversion: int → unsigned _BitInt return x / y + int(adjust); }

Author position: at most, pick low-hanging fruits (e.g. _BitIntbool)

Jan Schultke  |  Slides for P3666R1 — Bit-precise integers  |  SG22 Telecon 2025-10-08  |  Slide 8

Raising the BITINT_MAXWIDTH

Jan Schultke  |  Slides for P3666R1 — Bit-precise integers  |  SG22 Telecon 2025-10-08  |  Slide 9

_BitInt keyword and std::bit_int alias

Jan Schultke  |  Slides for P3666R1 — Bit-precise integers  |  SG22 Telecon 2025-10-08  |  Slide 10

Questions

Jan Schultke  |  Slides for P3666R1 — Bit-precise integers  |  SG22 Telecon 2025-10-08  |  Slide 11

References

[N1744] Michiel Salters. Big Integer Library Proposal for C++0x 2005-01-13 https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1744.pdf
[P1889R1] Alexander Zaitsev et al.. C++ Numerics Work In Progress 2019-12-27 https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1889r1%2epdf
[N2763] Aaron Ballman et al.. Adding a Fundamental Type for N-bit integers 2021-06-21 https://open-std.org/JTC1/SC22/WG14/www/docs/n2763.pdf
[N2775] Aaron Ballman, Melanie Blower. Literal suffixes for bit-precise integers 2021-07-13 https://open-std.org/JTC1/SC22/WG14/www/docs/n2775.pdf
[N3699] Robert C. Seacord. Integer Sets, v3 2025-09-02 https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3699.pdf
[N3705] Phillip Klaus Krause. bit-precise enum 2025-09-05 https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3705.htm