提交 3d9a7d91 authored 作者: LaughingMan's avatar LaughingMan

Replace the loops in MathUtils.nextPowerOf2 by bit math

This is an O(1) implementation compared to the previous O(log n).
In my tests it was faster for all values >4 (int) / >8 (long).
上级 5d5c337d
......@@ -213,33 +213,46 @@ public class MathUtils {
}
/**
* Get the value that is equal or higher than this value, and that is a
* Get the value that is equal to or higher than this value, and that is a
* power of two.
*
* @param x the original value
* @return the next power of two value
*/
public static int nextPowerOf2(int x) {
long i = 1;
while (i < x && i < (Integer.MAX_VALUE / 2)) {
i += i;
if (x == 0) {
return 1;
}
return (int) i;
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x++;
return x;
}
/**
* Get the value that is equal or higher than this value, and that is a
* Get the value that is equal to or higher than this value, and that is a
* power of two.
*
* @param x the original value
* @return the next power of two value
*/
public static long nextPowerOf2(long x) {
long i = 1;
while (i < x && i < (Long.MAX_VALUE / 2)) {
i += i;
if (x == 0) {
return 1;
}
return i;
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
x++;
return x;
}
/**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论