ridiculousfish / libdivide

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

Fix compilation of primitive types #98

Closed ridiculousfish closed 2 years ago

ridiculousfish commented 2 years ago

To restate #96: currently libdivide "dispatches" based on whether its type is uint32_t, uint64_t, etc. However these types are mapped to a primitive type like unsigned int; if say unsigned long has the same width then divider<unsigned long> will fail to compile since there is no specialization for it.

The idea here is to go back to a dispatch based on the size and signedness of the type, so that unsigned long and unsigned int will be handled the same. To detect the signedness we avoid type_traits which is apparently unavailable in AVR; instead we use the following trick:

T(0) > T(-1)

which will be true only for signed types. We can prevent floating point by throwing in a shift:

(T(0) >> 0) > T(-1)

An inelegant hack for an uncivilized language but it does the job. Fixes #96.

ridiculousfish commented 2 years ago

@adbancroft if possible please check whether this works with your AVR setup, thank you

adbancroft commented 2 years ago

@adbancroft if possible please check whether this works with your AVR setup, thank you

The tests passed on both simulator & real hardware (AT Mega 2560)