ridiculousfish / libdivide

Official git repository for libdivide: optimized integer division
http://libdivide.com
Other
1.09k stars 77 forks source link

upgrade int types #50

Closed yb303 closed 5 years ago

yb303 commented 5 years ago

Currently gcc is not happy with dividing (some type) by divider. operator/ and co need more overloads or something more concise.

kimwalisch commented 5 years ago

Thanks but I need more information. Can you please share your test program (or use case)?

yb303 commented 5 years ago

Its just for convenience. Like people normally divide any int type by another. Say an int loop iterator divide by uint size of something

yb303 commented 5 years ago

I guess it's not always possible to promote in this case. The dividend type can be promoted to a wider type and change signed/unsigned but the divisor cannot.

kimwalisch commented 5 years ago

Dividing any type by another type usually works like this:

int64_t a  = 12345;
int32_t b = 2;

// 1) b is automatically promoted to int64_t
// 2) the division a / b is calculated
a = a / b;

However this is not easily possible using libdivide because libdivide first has to generate a divider that only works for a given type (e.g. a libdivide unsigned divider cannot divide a signed integer). Hence the only way to implement your requested feature would be using:

int64_t a  = 12345;
int32_t b = 2;

libdivide::divider<int32_t> div(b);

// 1) We need to promote libdivide::divider<int32_t> to libdivide::divider<int64_t>
// 2) Compute the division a / libdivide::divider<int64_t>
a = a / div;

Even if it was possible to automatically promote any libdivide divider to the correct type it would be a very bad idea because generating a libdivide divider is very slow (up to 3 times slower than integer division). For this reason I suggest to leave everything as is and close this issue.

yb303 commented 5 years ago

Yeah, of course promoting the divisor is out of the question. I was actually thinking maybe signed and unsigned division can be merged into one and then sign can be promoted. But this does not help with int64 / int32 so IMHO not worth the effort if it cannot properly cover the entire scope of promotion.