8257815: Replace global log2 functions with efficient implementations

Reviewed-by: kbarrett, stefank
This commit is contained in:
Claes Redestad 2021-01-04 21:01:25 +00:00
parent f80c63b380
commit 9d160aa1b7
41 changed files with 183 additions and 209 deletions

View file

@ -1042,59 +1042,6 @@ inline T clamp(T value, T min, T max) {
return MIN2(MAX2(value, min), max);
}
// Returns largest i such that 2^i <= x.
// If x == 0, the function returns -1.
inline int log2_intptr(uintptr_t x) {
int i = -1;
uintptr_t p = 1;
while (p != 0 && p <= x) {
// p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x)
i++; p *= 2;
}
// p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1))
// If p = 0, overflow has occurred and i = 31 or i = 63 (depending on the machine word size).
return i;
}
//* largest i such that 2^i <= x
inline int log2_long(julong x) {
int i = -1;
julong p = 1;
while (p != 0 && p <= x) {
// p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x)
i++; p *= 2;
}
// p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1))
// (if p = 0 then overflow occurred and i = 63)
return i;
}
// If x < 0, the function returns 31 on a 32-bit machine and 63 on a 64-bit machine.
inline int log2_intptr(intptr_t x) {
return log2_intptr((uintptr_t)x);
}
inline int log2_int(int x) {
STATIC_ASSERT(sizeof(int) <= sizeof(uintptr_t));
return log2_intptr((uintptr_t)(unsigned int)x);
}
inline int log2_jint(jint x) {
STATIC_ASSERT(sizeof(jint) <= sizeof(uintptr_t));
return log2_intptr((uintptr_t)(juint)x);
}
inline int log2_uint(uint x) {
STATIC_ASSERT(sizeof(uint) <= sizeof(uintptr_t));
return log2_intptr((uintptr_t)x);
}
// A negative value of 'x' will return '63'
inline int log2_jlong(jlong x) {
STATIC_ASSERT(sizeof(jlong) <= sizeof(julong));
return log2_long((julong)x);
}
inline bool is_odd (intx x) { return x & 1; }
inline bool is_even(intx x) { return !is_odd(x); }