6 ms·
Normally that would depend on the architecture. For x86, you would get an exception. Of course the compiler could insert a check for 0 and a branch, avoiding t
by tropo 7y ago
Normally that would depend on the architecture.
For x86, you would get an exception. Of course the compiler could insert a check for 0 and a branch, avoiding the exception.
For PowerPC and I believe most RISC, there is no exception. Most processors would give a specific value, such as 0x80000000 for 32-bit. Of course the compiler could insert a check for 0 and a branch, intentionally jumping to code that raises an exception.
On MIPS the convention was exactly that. The compiler would add that extra code so that you'd get an exception even though the hardware wouldn't give one.
This often causes RISC processor emulators compiled for x86 to crash.
It's one of the many architecture-specific things that can poke through the abstraction layers to burn people. Another common group of them, for which I'm curious what rust does, is shift values that are out of range or that shift negative values right. For example: -6 shifted right by 1, 42 shifted left by 4096, 12 shifted right by -2, 88 shifted left by -3, -13 shifted right by 256, etc. (and for the non-negative values, does it matter if they are an unsigned type or not?)
- roblabla 7y agoAll of those would panic in rust. Rust inserts checks for anything that might cause an exception in order to turn it into a safe panic. https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=880243ea93dd07032648abea981a9939 https://play.rust-lang.org/?version=stable&mode=debug&editio...