55 lines
1.2 KiB
C
55 lines
1.2 KiB
C
|
#include <voxula/internals/utility/math.h>
|
||
|
|
||
|
uint64_t vx_min_u64(uint64_t first, uint64_t second)
|
||
|
{
|
||
|
if(first < second)
|
||
|
{
|
||
|
return first;
|
||
|
}
|
||
|
return second;
|
||
|
}
|
||
|
|
||
|
uint64_t vx_max_u64(uint64_t first, uint64_t second)
|
||
|
{
|
||
|
if(first > second)
|
||
|
{
|
||
|
return first;
|
||
|
}
|
||
|
return second;
|
||
|
}
|
||
|
|
||
|
uint64_t vx_ceil_to(uint64_t base, uint64_t anchor)
|
||
|
{
|
||
|
return base - (base % anchor) + anchor;
|
||
|
}
|
||
|
|
||
|
uint64_t vx_ceil_pow2(uint64_t base)
|
||
|
{
|
||
|
// This function relies on the fact that a
|
||
|
// single set bit is always a power of two.
|
||
|
//
|
||
|
// If the single set bit is the only one that
|
||
|
// is in a number, the number itself is returned.
|
||
|
//
|
||
|
// If there are other bits, the next highest
|
||
|
// power of two is created by returning the
|
||
|
// highest set bit after it was moved one to
|
||
|
// the left.
|
||
|
|
||
|
// Get the most significant bit that is set.
|
||
|
uint64_t highest_set_bit = (uint64_t) 1 << 63;
|
||
|
while(highest_set_bit > 0)
|
||
|
{
|
||
|
if(base & highest_set_bit)
|
||
|
{
|
||
|
break;
|
||
|
}
|
||
|
highest_set_bit >>= 1;
|
||
|
}
|
||
|
if(base == highest_set_bit)
|
||
|
{
|
||
|
return base;
|
||
|
}
|
||
|
return vx_max_u64(highest_set_bit << 1, 1);
|
||
|
}
|