r/cpp • WG21 Member • Aug 18 '26

P4444: std::big_int

https://isocpp.org/files/papers/D4444R0.html

Hey folks! Matt Borland, Christopher Kormanyos, and I are working on bringing infinite-precision integers to C++29. We now have a D4444R0 draft of a paper that should be in the next mailing.

We could really use some feedback so that the published R0 is as polished as possible. Any thoughts on the paper and on the reference implementation are greatly appreciated.

It would also be very helpful if you tested out whether our big_int implementation works for you. We're in need of some real deployment experience. If you're currently using Boost.Multiprecision, the library should be a drop-in replacement for cpp_int for the most part.

180 Upvotes

85 comments sorted by

View all comments

78

u/ReDucTor Game Developer | quiz.cpp-perf.com Aug 18 '26 edited Aug 18 '26

Another reason why std::big_int needs to be in the standard library is that it's extremely difficult to implement and optimize, in part because the implementation depends heavily on the platform's hardware capabilities, many of which are not exposed portably in the language.

This stands out more of a reason for it not to be in the standard library, because when the implementation is faulty its harder to fix. Do we need another vector<bool>, memory_order_consume, std:regex, etc.

EDIT: Also if there hardware capabilities and portablity issues that aren't easy to resolve, then fix those in the standard to allow people to implement libraries better rather then trying to make something which is special in standard library code.

20

u/vip17 Aug 18 '26

if there hardware capabilities and portablity issues that aren't easy to resolve, then fix those in the standard to allow people to implement libraries

There's no way to expose hardware features consistently for all big int libraries to use. Each hardware has vastly different features, for example in x86 there's ADX to do two add/mul chains in parallel for big int, and for even bigger values AVX2/AVX-512 can be used. Many other architectures don't have carry flags, some have separate instructions to get the high bits of the multiplication, some have FFT accelerator for FFT big int, some others like ARM SVE2 have SIMD add with carry flag from other lanes. There are already intrinsics that libraries have to use anyway. Replacing the library interface is always a better choice

7

u/styczynski_meow Aug 18 '26 edited Aug 18 '26

std::simd situation 2.0

Committee pleeease can I get just one standard library extension this one time? Pleaaaaaase to provide clean interface for a clean code!

Actually uses it to introduce messy abstraction nobody use like a boss 🗿
/s

31

u/eisenwave WG21 Member Aug 18 '26

I can understand the argument that it's hard to change something once in the standard, and we mention that in https://isocpp.org/files/papers/D4444R0.html#inability-to-change-abi. To be fair, we also have over a decade of experience with Boost.Multiprecision to draw from, and I don't think there's all that much "room for failure" in the design and implementation here.

Also, we've deliberately provided quite a lot of template parameters on std::basic_big_int so that even if the default specialization turns out to be bad, a "fixed one" can always be provided later. Ideally, that wouldn't happen though.

27

u/ReDucTor Game Developer | quiz.cpp-perf.com Aug 18 '26

we also have over a decade of experience with Boost.Multiprecision

I don't think that is a good metric, Boost.regex was around a decade before it got standardized and it was less niche.

39

u/James20k P2005R0 Aug 18 '26

One day I need to dig into how std::regex ended up in such a poor state, it feels like we never get a post mortem of why and how some features ended up as such a disaster

19

u/ReDucTor Game Developer | quiz.cpp-perf.com Aug 18 '26

Please do, that would be a good read and well needed to avoid having the same issue existing, especially when it feels like some proposals come more from wanting bragging rights to say they got something changed in the standard then an actual demand by the users of the language.

4

u/vishal340 Aug 18 '26

The ctre package is really good( probably the best) alternative. It does lot of work compile time. Will have check it out but I think that is one of the big difference

6

u/AlexReinkingYale Aug 18 '26

I wonder if you have read this paper on big int implementation from Daan Leijen (Microsoft Research)

https://www.microsoft.com/en-us/research/wp-content/uploads/2022/07/int.pdf

4

u/eisenwave WG21 Member Aug 18 '26 edited Aug 18 '26

Not yet, but will do!

EDIT: Well, I did now, and I wrote some stuff about it here: https://isocpp.org/files/papers/D4444R0.html#why-not-use-tagged-integers

The paper is excellent and it presents a brilliant optimization technique. I think that technique is great when you don't really touch the "internals" and work with a black box int/bigint in a high-level language, like JavaScript or Python.

The issue for std::big_int is that it conflicts with the desire to provide direct access to the internals via representation() like we do, and it's hard to extend big_int with more operations optimally. std::big_int is a bit like a container for an allocator, for a limb array, and exposes these details with very little abstraction. Tagged integers make that tricky. They're also not really constexpr-friendly.

→ More replies (1)
→ More replies (2)

12

u/Circlejerker_ Aug 18 '26

Whats wrong with memory_order_consume?

Im also not that much of a hater of std::regex - it got a bit of an annoying interface but works for simple cases and its nice not having to pull in 3rd party stuff for small throw-away programs.

23

u/ReDucTor Game Developer | quiz.cpp-perf.com Aug 18 '26

Whats wrong with memory_order_consume?

memory_order_consume is deprecated in C++26, but has essentially been deprecated since it first came out as no compiler really implemented it they just treated it as memory_order_acquire

See Retire the concept of consume operations for more info.

not that much of a hater of std::regex

The performance of std::regex is awful, it's covered in memory allocations, bloats the compiled code, the build times, and because of the ABI it can never be fixed. If I recall even just calling out into Python or some other language to do regex is going to be faster.

5

u/Circlejerker_ Aug 18 '26

So what so bad about the situation with memory_order_consume? Could you write code that was invalid with memory_order_require but valid with memory_order_consume? Or is it simply that its now deprecated that is the issue?

For std::regexp I usually don't care about regexp performance, but simply need to match a couple of strings. Its nice that there is something that can do that without having to pull 3rd party packets. If I have a usecase where i do care about performance then I'm simply in the same situation as I would be by not having it in the standard library.

4

u/ReDucTor Game Developer | quiz.cpp-perf.com Aug 18 '26 edited Aug 18 '26

Could you write code that was invalid with memory_order_require but valid with memory_order_consume?

You could write something like this

std::atomic<bool> g_flag;
int g_value;

// Producer
g_value = 10;
g_flag.store( true, std::memory_order_release );

// Consumer
// std::memory_order_consume - Anything depending on the result will see values from the producer
// std::memory_order_acquire - Anything after the consume will see the values from the producer
bool hasFlag = g_flag.load( std::memory_order_consume );
if (hasFlag)
{
    assert( g_value == 10 );
}

Here the load from g_value does not data depend on g_flag so that assert could trigger, if however you changed hasFlag to be a pointer to g_value and stored it instead and used it for the assert then it would have the data dependency. There are other ways in which dependencies could have been established and hacky approaches to mix them in but you will never have to worry about it as it's long gone.

One of the main potential users of consume was doing things like rcu_dereference, you could think of memory_order_consume more like a memory_order_relax but there is a depenency chain of stores and releases that also link up, not just a general happens-before

9

u/saf_e Aug 18 '26

Having "good enough " standard implementation is a good thing. When you need something advanced you always can use other libs (or custom impl).

If we want std lib be the best, we'll need to deprecate it every release since better impl available. 

20

u/James20k P2005R0 Aug 18 '26

Regex isn't good-enough though, its just bad

6

u/LucyShortForLucas Aug 18 '26 edited Aug 18 '26

This may be naive, but can they really not just add a fixed regex implementation into the standard? I understand that ABI compat is holy, but things like this have happened before where they add a new, better version alongside it (just look at jthread).

It is things like this, where parts of the standard library are basically-but-not-actually deprecated without a replacement that turns people off of C++. The language has been doing lots to get its shit together in recent years, coincidentally alongside the rise of Rust.

The committee has been slowly improving on this front, but they still have long way to go to get off their high horse and just admit fault and fix things when and where needed.

6

u/James20k P2005R0 Aug 18 '26

You'd need a std::regex2, but the real problem is that nobody wants to try and nobody's super interested in proposing a whole new regex

The committee has been slowly improving on this front, but they still have long way to go to get off their high horse and just admit fault and fix things when and where needed.

The language development is driven entirely by individual members' interest, its not a case that the committee is unaware that regex is broken (in fact everyone I spoke to is absolutely painfully aware), its just that its quite literally nobody's job to fix it. Fixing regex is signing up for maybe a decade of incredibly painful tedious work, with the end result that your kids might get to use it

Interest in fixes to standard library components is generally a lot lower than new language features

9

u/unchangeableusername Aug 18 '26

Wasn't std::regex heavily based on Boost.Regex though? In terms of regex search performance (I'm writing a regex library of my own and so have benchmarks to compare), Boost.regex is between 40% and 6800% faster than the Libstdc++ implementation of std::regex (700% faster on average) for the test cases I have.

Surely this is just a quality of implementation issue (that the implementers won't fix because it'll break ABI) and not a problem with the standard itself?

5

u/azswcowboy Aug 18 '26

You’re correct, it’s a quality of implementation issue not a specification problem. std library vendors are very good, but every now and then a non optimal implementation occurs.

→ More replies (1)

8

u/13steinj Aug 18 '26

Forget these massive failures, take a more subtle, recent one: std::format or ranges.

libfmt repeatedly leaves <format> in the dust on build and even runtime, but even on API subtleties/fixes.

ranges-v3 and STL ranges have diverged in functionality which is even worse but there's still valid use of ranges v2 (boost).

Hell there's even valid use of Boost.FileSystem instead of std/STL.

11

u/BarryRevzin Aug 18 '26 edited Aug 18 '26

So it's bad when the standard library provides less functionality than the third party library (std::format doesn't support color or named arguments) and also bad (even worse!) when the standard library provides more functionality than the third party library (std::ranges supports move-only views, exposes a way to write user-defined adapters, and has fewer unnecessary requirements on types)?

That doesn't really leave a lot of wiggle room.

→ More replies (3)

4

u/Infamous-Bed-7535 Aug 18 '26

I kind of agree with you. C++ std libs should include and cover lightweight and or core elements. Compiler vendors, commitee, etc should work on core functionalities of the langugae.

Impelementing libs like json parser or basic graphics, bigint are good to being 3rd party.

16

u/eisenwave WG21 Member Aug 18 '26

Do you think it's a mistake for Java, JavaScript, Go, and others to provide a BigInt in their standard libraries (full list at https://isocpp.org/files/papers/D4444R0.html#infinite-precision-integers-in-other-languages)?

That is, is there something about C++ specifcially that would make big_int unfit for a standard library, or is it about big_int in general?

→ More replies (11)