Live data from Hacker News

How expensive is integer-overflow trapping in C++?

lemire.me

31–40 of 198 posts

Re: How expensive is integer-overflow trapping in C++?

#31
post #26

Earlier quoted context omitted.

I wasn’t aware of this - which instruction do you mean?

There's an integer overflow flag that gets set after operations. My x86 books are all boxed up somewhere so I can't look up the details.

As I understand this is about trapping, which makes overflow checking free for non-overflowing operations.

Flag checking, as opposed to trapping, incurs costs on all operations.

Re: How expensive is integer-overflow trapping in C++?

#32
post #24

The problem with integer overflow, in the general context of UB, isn't that it's expensive to trap. The problem is that the C (and C++) specs insist on overflow being undefined behavior. Then compilers insist on abusing the UB as meaning that they get to do whatever they want. This particular case is only partially the compiler's fault, as the various specs should not be defining well specified behavior as being unde…

Integer overflow should have been implementation-defined behavior from day one, because that's what it actually is.

Re: How expensive is integer-overflow trapping in C++?

#34

It's worth mentioning that x86 CPUs had a single instruction overflow test that would raise a hardware interrupt in response to an integer overflow. This allowed for extremely low cost integer overflow tests. Unfortunately, this capability was deleted from x86-64 and we're now stuck with either accepting the risks of integer overflows or suffering a performance hit to test for them by using multiple instructions. Ano…

I wasn’t aware of this - which instruction do you mean?

INTO.

It signals int 0x04 if the instruction is executed while the overflow flag is set due to a prior signed arithmetic operation wrapping.

It seems I misspoke somewhat; there is somewhat equivalent functionality in x64 in the JO instructions, but they're six bytes rather than one and the extra size could have cache and/or memory bandwidth impacts under demanding circumstances.

Re: How expensive is integer-overflow trapping in C++?

#35
post #21
post #3

From GCC documentation, >The compiler will attempt to use hardware instructions to implement these built-in functions where possible, like conditional jump on overflow after addition, conditional jump on carry etc. I suspect something's not exactly right with compiler flags. Then again, how much of your program is integer arithmetic? 12x slowdown on 0.1% is 1.2%.

What I find annoying is we go back and forth over this because the insistence on clinging to the one true holy int type as on Gods Blessed Machine the PDP 11. Instead of implementing proper variable types like you have with Ada. Seriously considering code can compiled for on 16, 32, and 64 bit machines 'int' is dodgy as hell.

I love typedefs for u8, u16, u32 and their signed brethren, because it's just as short as "int" but way more sane.

Re: How expensive is integer-overflow trapping in C++?

#37
post #28

Earlier quoted context omitted.

I wasn’t aware of this - which instruction do you mean?

I think the GP was probably thinking of INTO(0xCE). https://en.wikipedia.org/wiki/INT_(x86_instruction)#INTO The bounds-checking was MPX, I think. > Another extremely useful instruction deleted from x86-64 tested if an array access was in bounds. https://en.wikipedia.org/wiki/Intel_MPX#Extensions

I was thinking of BOUND[0] rather than MPX. It's a much lighter solution than MPX and didn't require OS support.

[0] https://hjlebbink.github.io/x86doc/html/BOUND.html

Re: How expensive is integer-overflow trapping in C++?

#38

Earlier quoted context omitted.

I wasn’t aware of this - which instruction do you mean?

INTO. It signals int 0x04 if the instruction is executed while the overflow flag is set due to a prior signed arithmetic operation wrapping. It seems I misspoke somewhat; there is somewhat equivalent functionality in x64 in the JO instructions, but they're six bytes rather than one and the extra size could have cache and/or memory bandwidth impacts under demanding circumstances.

Yeah I knew about jo, and I think add-jo is even fused, but without a trap you have to write code everywhere to handle the overflow which you wouldn’t need if you had a trap. And of course jo doesn’t work for address arithmetic. That’s where the real overhead is.

Re: How expensive is integer-overflow trapping in C++?

#39

Earlier quoted context omitted.

I wasn’t aware of this - which instruction do you mean?

INTO. It signals int 0x04 if the instruction is executed while the overflow flag is set due to a prior signed arithmetic operation wrapping. It seems I misspoke somewhat; there is somewhat equivalent functionality in x64 in the JO instructions, but they're six bytes rather than one and the extra size could have cache and/or memory bandwidth impacts under demanding circumstances.

Causing an interrupt is much more expensive than having a jump that won't be taken most of the time

Re: How expensive is integer-overflow trapping in C++?

#40
post #29
post #26

Earlier quoted context omitted.

There's an integer overflow flag that gets set after operations. My x86 books are all boxed up somewhere so I can't look up the details.

That still exists though? add rax, rbx/jo overflow_error is how you do overflow checking on x86-64.

That doesn’t work with a lot of arithmetic, so you have to use slower arithmetic in order to use it.
Post reply on HN