On 5/14/2017 3:39 AM, Tomer Filiba wrote:
https://code.dlang.org/packages/divide
Libdivide (http://libdivide.com/) allows converting the DIV instruction (in
runtime) to a series of shifts and MULs, which is much more efficient in
execution time. It works by taking a number (the divisor or "denominator") and
doing some preprocessing to it, after which dividing by it can be ~8 times
faster (my own measurements). I use it to divide CPU cycles by the CPU frequency
(i.e., two big ugly numbers) to obtain wall time from it.
Of course it only applies to runtime division -- the compiler can do the same if
the divisor is known in compile time.
I hate to say this, but modern compilers already do this for generated runtime
code when dividing by a constant. Here's dmd's version:
https://github.com/dlang/dmd/blob/master/src/ddmd/backend/divcoeff.c