I would like to perform the following operation as quickly as possible
x / LSB(x)
where x
is an integral value unknown at compile time and LSB(x) = x & -x
.
(Alternatively, the operation is equivalent to an even division by the highest power of 2 <= x.) I am looking for a reasonably portable solution (without compiler intrinsics/builtins like GCC's __builtin_clz
or alike).
My concern is that the following simple implementation
x / (x & -x)
would still result in an expensive division as compiler might fail to realize that the division is in fact equivalent to right-shift by the number of trailing zeroes in the divisor.
If my concerns are reasonable, what would be a more efficient way to implement it?
I would appreciate a solution that is easily extendible to integral types of sizes 32-bit, 64-bits, 128-bits, ...