2024-05-22 23:18:53 +00:00
|
|
|
// ufn_round.h: More feature-rich rounding functions.
|
|
|
|
// Under MIT-License. Copyright by Eric-Paul Ickhorn (epickh).
|
2024-05-22 23:12:36 +00:00
|
|
|
|
|
|
|
#ifndef UFN_ROUND_H
|
|
|
|
#define UFN_ROUND_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2024-05-22 23:18:53 +00:00
|
|
|
/// @brief Rounds a value down to the last multiple of a stepping.
|
|
|
|
/// @param value Value to round down (floor).
|
|
|
|
/// @param stepping Stepping to round the value to.
|
|
|
|
/// @return Rounded value
|
2024-05-22 23:12:36 +00:00
|
|
|
int64_t ufn_floor_i64(int64_t value, int64_t stepping);
|
2024-05-22 23:18:53 +00:00
|
|
|
|
|
|
|
/// @brief Rounds a value up to the next multiple of a stepping.
|
|
|
|
/// @param value Value to round up (ceil).
|
|
|
|
/// @param stepping Stepping to round the value to.
|
|
|
|
/// @return Rounded value
|
2024-05-22 23:12:36 +00:00
|
|
|
int64_t ufn_ceil_i64(int64_t value, int64_t stepping);
|
|
|
|
|
2024-05-22 23:18:53 +00:00
|
|
|
/// @brief Rounds down (Floors) a value to the last power of 2.
|
|
|
|
/// @param value Value to find the nearest lower power of 2 of.
|
|
|
|
/// @return Power of 2 which is nearest lower one to 'value'.
|
2024-05-22 23:12:36 +00:00
|
|
|
uint64_t ufn_floor_pow2(uint64_t value);
|
2024-05-22 23:18:53 +00:00
|
|
|
|
|
|
|
/// @brief Rounds up (Ceils) a value to the next power of 2.
|
|
|
|
/// @note Internally, this uses the 'ufn_floor_pow2'-function but multiplies
|
|
|
|
/// its result by two to get the next higher power.
|
|
|
|
/// @param value Value to round up to the next power of 2.
|
2024-05-22 23:20:07 +00:00
|
|
|
/// @return Power of 2 which is the nearest higher one from 'value'.
|
2024-05-22 23:12:36 +00:00
|
|
|
uint64_t ufn_ceil_pow2(uint64_t value);
|
|
|
|
|
|
|
|
#ifdef UFN_IMPLEMENTATION
|
|
|
|
|
|
|
|
int64_t ufn_floor_i64(int64_t value, int64_t stepping)
|
|
|
|
{
|
|
|
|
return value - (value % stepping);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t ufn_ceil_i64(int64_t value, int64_t stepping)
|
|
|
|
{
|
|
|
|
return ufn_floor_i64(value, stepping) + stepping;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t ufn_floor_pow2(uint64_t value)
|
|
|
|
{
|
|
|
|
// Find highest-priority bit which is 1
|
|
|
|
|
|
|
|
int8_t bit_index = 63;
|
|
|
|
while(bit_index >= 0)
|
|
|
|
{
|
|
|
|
if(value & (1 << bit_index))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
--bit_index;
|
|
|
|
}
|
|
|
|
return 1 << bit_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t ufn_ceil_pow2(uint64_t value)
|
|
|
|
{
|
|
|
|
uint64_t floored = ufn_floor_pow2(value);
|
|
|
|
if(value == floored)
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return floored * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // UFN_IMPLEMENTATION
|
|
|
|
|
|
|
|
#endif // UFN_ROUND_H
|
|
|
|
|