4 ms·
I wasn’t aware of this - which instruction do you mean?
by chrisseaton 6y ago
I wasn’t aware of this - which instruction do you mean?
- wmil 6y agoThere'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.
- gsg 6y agoThat still exists though? add rax, rbx/jo overflow_error is how you do overflow checking on x86-64.
- chrisseaton 6y agoThat doesn’t work with a lot of arithmetic, so you have to use slower arithmetic in order to use it.
- gsg 6y agoSure, but that didn't change much between x86 and x86-64. Perhaps it got a little worse because the SIMD instructions aren't overflow check friendly.
- praptak 6y agoAs 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.
- gpderetta 6y agoINTO would still need to check the flags and you would need to invoke it after every operation that could overflow. You can't configure the integer ALU to raise an interrupt automatically after any overflow.
- zeotroph 6y agoIs there a reason to not add a new instruction to x86-64 which does interrupt the program on an integer operation overflow so no branching happens in the program? And can this instruction be made just as fast as the unchecked ones?
- deleted 6y ago[deleted]
- xpressvideoz 6y agoHaving a mode that makes all existing arithmetic instructions interrupt sounds plausible, though there must be something I overlooked.
- tom_mellior 6y agoThat wouldn't work well for C since signed overflow is undefined, but unsigned overflow is well-defined, with wrapping semantics. Making all existing arithmetic instructions trapping would mean paying trapping costs for well-defined unsigned overflow.
- zeotroph 6y agoI think this would be a per-thread state which you toggle, defaulting to "off". Then instead of checking and branching after each operation you flip it on/off depending on the type of arithmetic you want. The trick would be to get to the minimal number of toggles and not do the "safe" thing and set it to on/off for each operation.
- gpderetta 6y agoGenerally this sort of global states are considered a misfeature. They are hard to use and impede cpu design (they are particularly bad for OoO). It is better to have per instruction flags. For exemple while SSE still has an fpu control word, explicit instructions were added for some stuff that historically was handled by the control register (rounding for example).
- deleted 6y ago[deleted]
- gopalv 6y agoI think the GP was probably thinking of INTO(0xCE). https://en.wikipedia.org/wiki/INT_(x86_instruction)#INTO 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 https://en.wikipedia.org/wiki/Intel_MPX#Extensions
- bleepblorp 6y agoI 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 https://hjlebbink.github.io/x86doc/html/BOUND.html
- gsg 6y agoBOUND is pretty slow, and requires an odd start/end pair to be placed in memory. I don't see any reason that it would be better than the usual unsigned comparison + branch that languages with bounds checking tend to use. Besides, much of the difficulty with memory safety in C and C++ is the existence of pointers (or wrappers around them, like iterators), which do not come with an associated length. Length checking machinery can't help with that problem whether special instructions exist or not. In short, it's doubtful that the availability of these old instructions would make any difference to the practicality of bounds checking on modern x86 machines whatsoever.
- pjmlp 6y agoModern approaches take advantage of 64bit addressing modes to handle tagged pointers. The irony that we need a Lisp Machines solution to fix C and its derived languages.
- bleepblorp 6y agoINTO. 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.
- chrisseaton 6y agoYeah 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.
- BeeOnRope 6y agoUnfortunately jo is not fused on Intel chips, which is a bit surprising.
- raverbashing 6y agoCausing an interrupt is much more expensive than having a jump that won't be taken most of the time
- deleted 6y ago[deleted]
- rightbyte 6y agoUsually you don't really care that much about the overflow cost if it is an error right?
- raverbashing 6y agoTrue but do you care for all sums/muls in your code? Might be better to check where exactly can you have the overflows and test in the code for those cases. I think checking for overflows over the entire code is useful for testing/validation purposes but not really useful in production. Or use bigger data types if you're hitting overflows frequently.