RFR: 8257815: Replace global log2 functions with efficient implementations

Kim Barrett kim.barrett at oracle.com
Tue Dec 8 13:46:24 UTC 2020


> On Dec 8, 2020, at 7:49 AM, Claes Redestad <claes.redestad at oracle.com> wrote:
> 
> Hi Kim,
> 
> On 2020-12-08 02:43, Kim Barrett wrote:
> 
> I think you misread slightly: the behavior of the pre-existing code *is*
> to similarly treat signed values as unsigned wrt checking power_of_2:
> 
> 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);
>  ...
> 
> inline int exact_log2_long(jlong x) {
>  assert(is_power_of_2((julong)x), "x must be a power of 2: " JLONG_FORMAT, x);
> 
> exact_log2i does an equivalent check. So unless I'm misreading the
> context I'm _preserving_ this behavior.

You are right, I missed those pesky “u”s in those pesky casts.  Yuck!

> I agree we could opt for a stricter precondition in the new method
> (exact_ilog2?), while retrofitting exact_log/exact_log2_long to be
> backwards compatible w.r.t. accepting signed values that turn into
> 0x8000... when cast to unsigned. I think we should then follow-up
> and remove exact_log2/-_long.
> 
> WDYT?

That seems like a good plan to me.  Similarly for ilog2.

I think with that approach you don’t need the _allow_zero form either.
It’s just a precondition of ilog2 that the argument is > 0.

Also, exact_ilog2 should use count_trailing_zeros.



More information about the hotspot-dev mailing list