RFR: 8240615: is_power_of_2() has Undefined Behaviour and is inconsistent
Andrew Haley
aph at redhat.com
Wed Mar 11 14:22:38 UTC 2020
On 3/11/20 1:53 PM, Stuart Monteith wrote:
> Look good, tests ok on aarch64. However, the case for "0" isn't passed
> in the test, or handled by the function. Is "count_leading_zeros"
> sufficiently well defined that we can expect it to produce "64" or "32"
> in this case, or should there be an assertion in exact_log2* to trip
> when 0 is passed?
Hmm. Am I missing something?
There is, surely, everything you need to reject 0 here:
bool is_power_of_2(T x) {
return (x > T(0)) && ((x & (x - 1)) == T(0));
}
inline int exact_log2(intptr_t x) {
assert(is_power_of_2((uintptr_t)x), "x must be a power of 2: " INTPTR_FORMAT, x);
--
Andrew Haley (he/him)
Java Platform Lead Engineer
Red Hat UK Ltd. <https://www.redhat.com>
https://keybase.io/andrewhaley
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671
More information about the hotspot-runtime-dev
mailing list