-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpower_two_functions.h
executable file
·28 lines (23 loc) · 1 KB
/
power_two_functions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// The core library - copyright GarageGames. The core library is released under the MIT Open Source software license. See /license.txt in this distribution for specific license terms.
/// Determines if number is a power of two.
inline bool is_power_of_2(const uint32 number)
{
return (number & (number - 1)) == 0;
}
/// Determines the binary logarithm of the input value rounded down to the nearest power of 2.
inline uint32 get_binary_log(uint32 value)
{
float32 floatValue = float32(value);
return (*((uint32 *) &floatValue) >> 23) - 127;
}
/// Determines the binary logarithm of the next greater power of two of the input number.
inline uint32 get_next_binary_log(uint32 number)
{
return get_binary_log(number) + (is_power_of_2(number) ? 0 : 1);
}
/// Determines the next greater power of two from the value. If the value is a power of two, it is returned.
inline uint32 get_next_power_of_2(uint32 value)
{
return is_power_of_2(value) ? value : (1 << (get_binary_log(value) + 1));
}
#define bit(x) (1 << (x))