Libdivide ported to D
Tomer Filiba via Digitalmars-d
digitalmars-d at puremagic.com
Wed Mar 2 00:57:59 PST 2016
I ported libdivide (http://libdivide.com/) to D for my own
purposes, but thought it would be nice to contribute it back to
the community. In short, libdivide does optimized integer
division, it first does some preprocessing of the denominator and
generates something that will divide must fast. I only ported the
following functions:
struct libdivide_s32_t libdivide_s32_gen(int32_t y)
int32_t libdivide_s32_do(int32_t, const struct libdivide_s32_t
*)
struct libdivide_u32_t libdivide_u32_gen(uint32_t y)
uint32_t libdivide_u32_do(uint32_t, const struct libdivide_u32_t
*)
struct libdivide_s64_t libdivide_s64_gen(int64_t y)
int64_t libdivide_s64_do(int64_t, const struct libdivide_s64_t
*)
struct libdivide_u64_t libdivide_u64_gen(uint64_t y)
uint64_t libdivide_u64_do(uint64_t, const struct libdivide_u64_t
*)
And organized them to structs with a simple ctor and
opBinaryRight("/") for intuitive use. Example:
auto denom = denominator(7263217);
auto quot = 587387218 / denom;
when `denom` is obviously given only at runtime, otherwise the
compiler would optimize it too.
On LDC (0.17), my port is ~9 times faster than regular division
On dmd (2.70.1) it's 40% slower :(
It seems logical to include it in the standard library. Anyway, I
hope it serves you well.
-tomer
More information about the Digitalmars-d
mailing list