Closed otherjason closed 8 years ago
This should calculate 1 / 1, which should be 1. However, this program prints:
Unfortunately this is by design, we even have assertions in the code that check this case:
static inline struct libdivide_u32_t libdivide_internal_u32_gen(uint32_t d, int branchfree) {
// 1 is not supported with branchfree algorithm
LIBDIVIDE_ASSERT(!branchfree || d != 1);
static inline struct libdivide_u64_t libdivide_internal_u64_gen(uint64_t d, int branchfree) {
// 1 is not supported with branchfree algorithm
LIBDIVIDE_ASSERT(!branchfree || d != 1);
The branchfree algorithm works for all integers except for 1 (and 0 obviously). We could probably make it work for 1 as well by adding a bunch of nasty operations but this would most likely slow down the algorithm so much that it would be totally useless.
The branchfree algorithm is very new, so it has not yet been documented yet on http://libdivide.com/documentation.html. To me the branchfree algorithm is still very usefull as is, I am using it in my primecount project and it gives me a nice speed up compared to the default branchfull algorithm.
I see; that makes sense. I'm surprised that the assertions didn't fire, but it's not a huge deal for me. In my use case, I would never be dividing by 1 anyway.
The assertion did not fire because by default assertions are disabled in libdivde (for performance reasons).
Self-contained example:
This should calculate
1 / 1
, which should be1
. However, this program prints:This was compiled on Ubuntu 16.04 with:
I see the same behavior with
-O0
or-O3
.