On Monday, 25 April 2016 at 19:37:45 UTC, Andrei Alexandrescu
wrote:
On 04/25/2016 03:21 PM, Solomon E wrote:
I'm more concerned with getting the application
logic right when I program than optimizing for speed or bytes,
until
something is noticeably pausing or taking extra megabytes.
This is a luxury not available to library writers.
I agree.
bool isPow2D(T)(T x)
{
return (x > 0) && !(x & (x - 1));
}
This is worse than what we have now, which is easy to show is
correct.
Andrei
I wasn't recommending isPow2D for the library, just for the
luxury use case of writing cautiously to show the logic is
correct.
It's easy to show that the current code for
experimental.common.isPowerOf2 (the same logic as isPow2B) does
wrong things when a signed type is forced into it.
I was hoping to suggest that if isPow2F isn't wrong, it might be
used, because it has been proved and checked to be correct in
this thread. However, I don't know if it actually is faster, and
it would be difficult to test the speed up, if any, since the
function variants are all so small, and isPow2F compiles to
different instructions when used with different types. So it
might not be worthwhile, unless there's an advantage of
correctness or type-safety, which I'm not experienced enough in D
to judge.
(The library's descriptions of std.math.nextPow2 are vastly
oversimplified compared to what it does as shown in the examples,
so attempting to use that for recognizing simple powers of 2
would be a complexity mismatch.)