Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It's a little amusing that x86 has the INTO instruction, a single byte opcode at position CEh, that was designed specifically for this purpose and was there since the 8086, but when AMD designed their 64-bit extensions, it turned into an invalid instruction (and Intel was forced to go along, presumably for compatibility.) A rather shortsighted move, I think; instead of having a possibly useful (but not previously often-used) instruction, that single-byte opcode becomes wasted in 64-bit mode. With it, adding overflow-trapping arithemtic support to a compiler would be trivial and only add 1 extra byte to each arithmetic operation that needs it.

Ditto for BOUND, which is useful for automatic array bounds-checking - it performs both lower and upper bounds checks.

Also, I don't really get why integers wrapping around should be "unexpected". It is only to those who don't understand how integers work. The saying "know your limit, play within it" comes to mind.



Also, the original SPARC chips had TADDCCTV, which would add two 32-bit integers, trapping either if there was overflow or if either operand had a nonzero value in its two low-order bits. This was specifically to support tagged fixnum addition in Lisp.

They dropped this instruction for the 64-bit SPARCs.


Those instructions generate an interrupt; you'd have to define the OS ABI to make those instructions trap back to the application in a catchable way.


There's an existing way to do this already: SIGFPE + the UNIX signal-handling mechanism. Integer overflow is even mentioned as one of the possible causes of this signal. The BSDs have a defined constant FPE_INTOVF_TRAP for it too. On Windows, SEH is an equivalent functionality.


If you put the INTO immediately after the overflowing instruction, the OS could just go back one instruction (harder than it seems, but not impossible). Wouldn't that work?


That'd require the OS to define that as part of the ABI.

Also, you almost certainly don't want to just go back an instruction; you want to catch and handle the overflow.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: