Live data from Hacker News

Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

bitbucket.org

121–130 of 130 posts

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#121

Earlier quoted context omitted.

Did you read this part: > bounded::integer uses built-in integers as the template parameter to determine its bounds. This means that it cannot store an integer larger than ... They say the limitation arises from using "built-in integers" as template parameters. It is reasonable to assume "built-in integer" means "int". It is more a stretch to assume "built-in integer" means any number of types depending on the evalua…

> It is reasonable to assume "built-in integer" means "int". This strikes me as a little nuts for C but it definitely explains this thread. I just assumed people had already switched over to e.g. sint32/size_t to avoid this problem a long time ago. I couldn’t tell if this was a legitimate complaint or someone’s language lawyering tendencies gone too far.

First of all, we're not talking C. This is a C++ template parameter. The feature doesn't exist in C.

Second, it is not "nuts" to use int, I would argue it's quite a bit more crazy to use rarely-supported 128-bit quantities when you don't need them. Your suggestion of "sint32" (this is not a standardized typedef, you mean int32_t perhaps) would be the same as int on most compilers today, and size_t is likewise very often 32 bits. Your suggestion is basically a no-op in a lot of places. Domain-specific areas like file formats or network protocols are a different story, as those have to be explicit about specifying sizes.

Third:

> someone’s language lawyering tendencies

I suggest you avoid passive-aggressive communication style and just say "you" instead of "someone". If you're going to criticize or give feedback, be direct. Don't vaguely tell me that "somebody" "might" have a problem.

Best regards to you, friend.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#122

Earlier quoted context omitted.

> It is reasonable to assume "built-in integer" means "int". This strikes me as a little nuts for C but it definitely explains this thread. I just assumed people had already switched over to e.g. sint32/size_t to avoid this problem a long time ago. I couldn’t tell if this was a legitimate complaint or someone’s language lawyering tendencies gone too far.

First of all, we're not talking C. This is a C++ template parameter. The feature doesn't exist in C. Second, it is not "nuts" to use int, I would argue it's quite a bit more crazy to use rarely-supported 128-bit quantities when you don't need them. Your suggestion of "sint32" (this is not a standardized typedef, you mean int32_t perhaps) would be the same as int on most compilers today, and size_t is likewise very of…

Hey I was just trying to figure out if you were bringing up anything of value :)

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#123

Earlier quoted context omitted.

You were rude I would like you not to interact with me any more, it's that simple.

What, precisely, did you find rude about “ Ada. It’s not an acronym. ADA is the Americans with Disabilities Act.”? Sincere question. Rudeness was not my intention, and those three sentences appear to be three simple facts completely neutral in nature. I’m not understanding how they could possibly be perceived as rude. “go away” on the other hand, seemed an unnecessarily hostile and rude response to such a neutral cor…

What part of you were rude please do not interact with me any more do you not get?

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#124

Earlier quoted context omitted.

What, precisely, did you find rude about “ Ada. It’s not an acronym. ADA is the Americans with Disabilities Act.”? Sincere question. Rudeness was not my intention, and those three sentences appear to be three simple facts completely neutral in nature. I’m not understanding how they could possibly be perceived as rude. “go away” on the other hand, seemed an unnecessarily hostile and rude response to such a neutral cor…

What part of you were rude please do not interact with me any more do you not get?

What part of ”I’m disinclined to let ‘you were rude to me‘ go unchallenged just because you followed it up with ‘stop interacting with me’” do you not get?

You don’t get to accuse people of things and then summarily declare that they’re not allowed to defend themselves against your daft accusations.

This is a forum. If you don’t want people responding to you, don’t post. You don’t get to play silly little games like “You believe the earth is flat and also I wave my magic wand and forbid you from replying to me, so that claim cannot be challenged”. That’s not how a discussion forum works.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#125
post #67

Earlier quoted context omitted.

If you are doing high-throughput, and you ever allocate anything after startup, you are Doing It Wrong. Any brand of FIFO loses. What you need is a big-ass ring buffer, mmapped on a hugetlbfs, fed by a process on a NOHZ isolcpu core. Readers are separate processes.

When I see you say “any brand of FIFO loses” in the same post as “what you need is a ring buffer”, it shows that there’s a terminology disconnect here: A “ring buffer” used for streaming data is a brand of ”FIFO” :) Therefore, the implementation you’re suggesting is actually not all that different from my current solution, except for a few important details related to the particular problem I’m solving — e.g. handlin…

That is an interesting problem. When I see it, the input streams tend to be ring buffers, and output is a priority queue of their heads, ordered by timestamp.

Obviously a ring buffer is, literally, a first-in first-out medium. The essential difference between your typical FIFO and a ring buffer is the entire lack of interaction between writer and reader. More precisely, readers never have any effect on the writer. This allows any number of readers to be at random places along the sequence. The FIFO queues I find myself replacing tend to allocate a buffer, under a lock, and push it to a queue, under another lock (often these are hardware locks, what is often called "lock-free"), and readers pop a buffer under the same lock, use them for awhile, and then return them to a pool, under ther first lock. All this interaction generates overhead and cache pollution.

The only coupling in a ring buffer is readers checking the current state of the head pointer, done under relaxed semantics. The head pointer lives at all times in the writer's cache.

You probably understand all this, but remarkably many don't.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#126
post #3

> bounded::integer uses built-in integers as the template parameter to determine its bounds. This means that it cannot store an integer larger than an unsigned 64-bit or unsigned 128-bit integer (depending on the platform) or smaller than a signed 64-bit or signed 128-bit integer. This restriction should be removed at some point in the future. So when an overflow happens here I assume it's a compile time error (UB du…

I am not aware of any compiler or platform where int is 128 bits. The most common these days, even on 64 bit platforms, is 32 bits, which they don't even mention. I am not sure why they chose to speculate about sizeof(int) in this way, vs. expressing the limit in terms of something more concrete.

Library author here. I'm sorry you found the wording confusing. I was trying to talk generally about the non-library types: things like int and long. The C++ standard guarantees that there exists an integer type that is at least 64-bits large (`long long` and `unsigned long long`), and many platforms support a type like __int128 that is 128 bits large. Unfortunately, the documentation is slightly out of date. clang has some bugs that cause the compiler to crash if you use __int128 or __uint128 in certain situations (which my library uses), so I had 128-bit support on only in gcc. However, my library is currently making use of C++20 concepts, and only a branch on clang supports that enough, so the master version of my library can be compiled only by clang, so the largest size currently allowed is a 64-bit integer (either signed or unsigned, the library handles that for you).

I'll make sure to update the documentation.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#127

At first glance it seems to be similar to boost::safe_numerics. Are there significant differences? https://github.com/boostorg/safe_numerics

Both libraries do the same thing. Boost lets you set your exception policy and I'm unsure if this Bounded Integer library gives the same level of control.

The bounded::integer types accept three template parameters: `integer`. That final parameter can be bounded::throw_policy, which is itself templated on the exception type thrown. The default exception policy is "overflow is undefined behavior". The other two policies supported out of the box are wrapping / modulo and saturation / clamping.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#128

what about support for older gcc (say 6), older clang (say 5) and old MSVC (say 2015/17)?

Only very old versions of the library are supported by those older compilers. My library is targeting the C++20 standard, so currently the only compile version that can compile it is clang with the concepts branch.

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#129
post #30

At this point, when I see libraries like this all I can think is ”oh good, my compile times aren’t long enough, lets make EVERY INTEGER a template”. Also, while I can reasonably beliveve that most of the overhead goes away at -O2 or -O3, this has to just trash performance for debug builds, which is not unimportant.

Library author here: As I have added more features to the library, compile times have definitely suffered. Fortunately, the latest version of the clang compiler added `-ftime-trace`, which reports where the compiler spends its time. I am currently going through and optimizing the compile-time performance of my library. However, I expect the largest gain to come in the next year or so once compilers + build systems implement C++20 modules so that you don't have to compile everything every time you use it.

For run-time performance, yes, you definitely get a hit in your run time if you have all optimizations off. However, I have found that the practical benefits of things like this library, combined with run-time sanitizers, is worth much more at finding bugs than a debugger, so I typically debug and develop with `-O3 -fsanitize=undefined -fsanitize=address` and release mode is `-O3 -flto=thin -DNDEBUG`. This is an area where it's not possible (yet) to satisfy all use cases. I am hopeful that in the future, it will be much easier to tell your compiler "this is the code I care about debugging, optimize the rest".

Re: Bounded Integer: Header-only C++ library replaces integers, adds explicit bounds

#130
post #84
post #41

Earlier quoted context omitted.

it does not have to. A smart compiler can narrow it down to machine size integers then possible.

> A smart compiler can narrow it down to machine size integers then possible. A smart compiler will be able to narrow it down in a limited set of scenarios, but all we need to do is put an accumulator into a loop to see that bounding an integer is equivalent to the halting problem. Or for another example, should we expect our "smart" compiler to bound y in the following? The bound on x is a freebie. bigint n(uint64_t…

As I said, "when possible". When not possible a smart compiler can give a warning or an error and ask the user to either use arbirary-precision integer or a type with bound checking.
Post reply on HN