ridiculousfish / libdivide

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

Idea: libdivide::divider_branchfree<T> #14

Closed kimwalisch closed 6 years ago

kimwalisch commented 8 years ago

The current API for using the branchfree divider in C++ is a little too cumbersome in my opinion:

libdivide::divider<uint64_t, libdivide::BRANCHFREE> my_divider;

I think that:

libdivide::divider_branchfree<uint64_t> my_divider;

would be nicer. Using C++11 this could be implemented using:

#if __cplusplus >= 201103L
    template <typename T>
    using divider_branchfree = divider<T, BRANCHFREE>;
#endif

It would be nice to find a solution for C++98 as well.

otherjason commented 8 years ago

One method of doing this in C++98 would be to derive from the desired type:

template <typename T>
class divider_branchfree : public divider<T, libdivide::BRANCHFREE>
{
    // must duplicate the constructors for `divider<T>` in here
};

Unfortunately, C++98 doesn't support the hoisting of base-class constructors with the using keyword like C++11 does, but this stucture achieves the desired user-level syntax.

kimwalisch commented 8 years ago

One method of doing this in C++98 would be to derive from the desired type:

I tried exactly that a few months ago but I failed. If you can make it work that would be great!

kimwalisch commented 6 years ago

I have now implemented this for C++11 in only 4 lines of code. I won't implement it in C++98 though, I tried to do it in 2016 but the code was a mess and ultimately I could not make it work.

I have actually decided to use libdivide::branchfree_divider<T> instead of libdivide::divider_branchfree<T> as I suggested back in 2016.

I am thinking about documenting the branchfree in README.md because most people do not know this functionality exists in libdivide as it is not documented on http://libdivide.com.