ridiculousfish / libdivide

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

Branchfree divider cannot be 1 #111

Closed lachesis closed 8 months ago

lachesis commented 9 months ago

https://github.com/ridiculousfish/libdivide/blob/ee2a872b85648b16c0a65faa8cb646fa7e9e903a/libdivide.h#L742

But why? I guess there's no set of shifts and multiples that is equal to dividing by 1? And of course you cannot check if the input is 1 at runtime, as that adds a branch. But then... you're already branching to throw this error?

kimwalisch commented 8 months ago

Supporting 1 as a branchfree divider requires adding more instructions and will slow down the branchfree algorithm. For the branchfree algorithm speed is more important to me than supporting 1 as a divider. I also remember talking to another libdivide user a few years ago, who was also in favor of not supporting 1 as a branchfree divider in order to achieve maximum performance.

But 1 as a divider is supported by the default, non-branchfree, algorithm so I suggest you use this instead.