Bug in method

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Bug in method

Vitaliy Biryukov
Hi, igniters!

I noticed that the method "IgniteUtils.ceilPow2" can overflow for values
greater than 2^30 and return Integer.MIN_VALUE for them.

Maybe this check was skipped  for method speed. In this case, we need to
add information about this into javaDoc.

Current implementation:

public static int ceilPow2(int v) {
    return Integer.highestOneBit(v - 1) << 1;
}


It can be fixed like this:
bit impl:

int i = v - 1;

return Integer.highestOneBit(i) << 1 - (i >>> 30 ^ v >> 31);

more readable impl:

if (v == Integer.MIN_VALUE)
    return 0;

int pow30 = 1 << 30;

return v >= pow30 ? pow30 : Integer.highestOneBit(v - 1) << 1;


What do you think? Sould I create  ticket with PR?

--
Best Regards,
Vitaliy Biryukov
Reply | Threaded
Open this post in threaded view
|

Re: Bug in method

Anton Vinogradov
Vitaliy,

Let's file an issue and solve it :)
Don't forget to add tests.

On Thu, Jul 27, 2017 at 5:24 PM, Vitaliy Biryukov <
[hidden email]> wrote:

> Hi, igniters!
>
> I noticed that the method "IgniteUtils.ceilPow2" can overflow for values
> greater than 2^30 and return Integer.MIN_VALUE for them.
>
> Maybe this check was skipped  for method speed. In this case, we need to
> add information about this into javaDoc.
>
> Current implementation:
>
> public static int ceilPow2(int v) {
>     return Integer.highestOneBit(v - 1) << 1;
> }
>
>
> It can be fixed like this:
> bit impl:
>
> int i = v - 1;
>
> return Integer.highestOneBit(i) << 1 - (i >>> 30 ^ v >> 31);
>
> more readable impl:
>
> if (v == Integer.MIN_VALUE)
>     return 0;
>
> int pow30 = 1 << 30;
>
> return v >= pow30 ? pow30 : Integer.highestOneBit(v - 1) << 1;
>
>
> What do you think? Sould I create  ticket with PR?
>
> --
> Best Regards,
> Vitaliy Biryukov
>