Live data from Hacker News

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

bitbucket.org

31–40 of 130 posts

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

#31
post #15

Earlier quoted context omitted.

X86 has also had saturation arithmetic since MMX was introduced.

You should never rely on the underlying CPU architecture's behavior on integer overflow when you write C or C++. It is undefined and not unspecified or implementation defined.

true, but the behavior of unsigned integers is defined such that they can't overflow. the "wrapping" behavior of unsigned ints is defined: you get the arithmetic result modulo the max value for that width.

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

#32

> The built-in integer types in C++ (int, unsigned, long long, etc.) are mostly unusable because of the lax requirements on bounds. This must be some new definition of the word 'unusable.'

On a different platform, these "friendly" types can be larger, typically 2 or 4 times larger which can be devastating for performance, or smaller, leading to overflow. They are only "usable" if you restrict yourself on one version, of one compiler, on one hardware platform.

Does C++ not have int32_t and the like from C?

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

#33
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.

It does not change your existing integers or compilation time. Only where you use the new types the compiler will have more work to do. What would be the alternative in those places? Manual code? Preprocessor macros? Assertions everywhere? A theorem prover?

To me the implementation seems to be a good solution given that you don't want to change the core language.

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

#34
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.

And if the application's run-time safety requirements demand that the debug performance hit be paid, then that audience will view the debug sluggishness as a cost of doing business.

Welcome to engineering tradeoffs.

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

#35

> The built-in integer types in C++ (int, unsigned, long long, etc.) are mostly unusable because of the lax requirements on bounds. This must be some new definition of the word 'unusable.'

On a different platform, these "friendly" types can be larger, typically 2 or 4 times larger which can be devastating for performance, or smaller, leading to overflow. They are only "usable" if you restrict yourself on one version, of one compiler, on one hardware platform.

You can still use the Uint32/Uint8/... types. While performance can vary from system to system, the range should be the same on all reasonable platforms.

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

#36
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.

Have you measured?

Also, what do you mean by "this has to just trash performance for debug builds". Do you mean runtime performance?

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

#37
post #32

Earlier quoted context omitted.

On a different platform, these "friendly" types can be larger, typically 2 or 4 times larger which can be devastating for performance, or smaller, leading to overflow. They are only "usable" if you restrict yourself on one version, of one compiler, on one hardware platform.

Does C++ not have int32_t and the like from C?

It does. I personally stick to those exclusively except for for loop index variables.

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

#38
post #32

Earlier quoted context omitted.

On a different platform, these "friendly" types can be larger, typically 2 or 4 times larger which can be devastating for performance, or smaller, leading to overflow. They are only "usable" if you restrict yourself on one version, of one compiler, on one hardware platform.

Does C++ not have int32_t and the like from C?

Yes

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

#39
post #32

Earlier quoted context omitted.

Does C++ not have int32_t and the like from C?

It does. I personally stick to those exclusively except for for loop index variables.

It's a good choice. Sometimes your best choice for a general purpose int is

    typedef Int int32_t;
for the entire compilation as a whole, and when things get screwy just bump Int up to int64_t and take the performance hit for the extra bit of safety
Post reply on HN