ridiculousfish / libdivide

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

Constant division templates #89

Closed adbancroft closed 2 years ago

adbancroft commented 2 years ago

A set of predefined templates to speed up division by 16-bit constants.

Template equivalent of #76.

ridiculousfish commented 2 years ago

These hpp files are giving the compiler quite a workout, with 32k separate specializations! Those are going to be some fun error messages when it lists all the templates that don't apply!

I think this could be cleaner as a constexpr function instead. With C++14 you could use switch, with C++11 it can still be done with chained ternary operators:

constexpr libdivide_s16_t get_divider(constexpr int16_t v) {
  return v == -16384   ? libdivide_s16_t{0, 142}
         : v == -16383 ? libdivide_s16_t{32765, 205}
         : v == -16382 ? libdivide_s16_t{32763, 205}
         // remaining 65k go here
         : libdivide_s16_t{0, 0}; // unreachable
}

etc. It's up to you whether you implement that, just an idea in case you haven't thought of it.

adbancroft commented 2 years ago

Unfortunately the constexprfunction didn't pan out: it increases compile times to an unacceptable level. I don't know exactly how much, as it took so long that I killed the compiler process after 10 mins (for a build that normally takes seconds).

I've left the relevant code in the generator just in case we could make use of it in the future.