[rust-dev] Division and modulo for signed numbers

Patrick Walton pwalton at mozilla.com
Tue Apr 23 08:02:22 PDT 2013

On 4/23/13 7:48 AM, swede at earthling.net wrote:
> Performance should be about the same when using F-division:
>   * Performance will go up for division by constant powers of two.
>   * Performance will stay the same for division by compile-time constants, since these are transformed by the compiler into multiplies. (I actually think performance will go up slightly in this case, but it's been a while since I looked at the algorithm.)
>   * Performance on ARM will stay the same for divides by variables (not known at compile-time), since ARM does not have a hardware divider.
>   * Performance on x86/x64 for divides by variables will go down slightly, since Intel's idiv instruction implements F-division.
> So one already very slow operation (x86 idiv) gets slightly slower, one fast operation (divide by power-of-two) gets quite a bit faster. It probably nets out near zero.

I worry quite a bit about that last one. Rust language semantics strive 
to mirror the CPU instructions as closely as possible. This is why, for 
example, we were forced to make `<` respect NaN, unlike Haskell--if we 
didn't, we couldn't use the hardware floating point comparison instructions.

I'm also nervous about C interoperability.

Including F-division as a library function sounds fine to me 
though--macros or dot notation may be able to sweeten the syntax.


More information about the Rust-dev mailing list