www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Libdivide ported to D

reply Tomer Filiba <tomerfiliba gmail.com> writes:
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
Mar 02 2016
parent reply Johan Engelen <j j.nl> writes:
On Wednesday, 2 March 2016 at 08:57:59 UTC, Tomer Filiba wrote:
 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.
Where can we find the code?
Mar 02 2016
parent Tomer Filiba <tomerfiliba gmail.com> writes:
On Wednesday, 2 March 2016 at 09:28:30 UTC, Johan Engelen wrote:
 Where can we find the code?
Oh my, obviously I forgot to put the link. Here you go: https://github.com/tomerfiliba/dlang/blob/master/source/divide.d
Mar 02 2016