Compare commits
4 Commits
a5e78dca9a
...
6150e5dda6
Author | SHA1 | Date |
---|---|---|
Eric-Paul Ickhorn | 6150e5dda6 | |
Eric-Paul Ickhorn | d54bf3fe28 | |
Eric-Paul Ickhorn | 4244cf107b | |
Eric-Paul Ickhorn | f77dd2b23a |
10
README.md
10
README.md
|
@ -1,3 +1,11 @@
|
|||
# libUFN
|
||||
|
||||
LibUFN is a library of micro-functionalities (µFN).
|
||||
LibUFN is a library of micro-functionalities (The name stands for µFN).
|
||||
It currently consists of 2 independent single-header libraries
|
||||
|
||||
## Functionality
|
||||
|
||||
There are only 2 libraries currently:
|
||||
1) ufn_arena: An arena allocator for optimization of string processing.
|
||||
2) ufn_round: Functions for rounding to the next multiple of some number
|
||||
or to the next power of 2. Currently only floor()'s and ceil()'s to those.
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
// ufn_round.h: More feature-rich rounding functions.
|
||||
// Under MIT-License. Copyright by Eric-Paul Ickhorn (epickh).
|
||||
|
||||
#ifndef UFN_ROUND_H
|
||||
#define UFN_ROUND_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/// @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
|
||||
int64_t ufn_floor_i64(int64_t value, int64_t stepping);
|
||||
|
||||
/// @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
|
||||
int64_t ufn_ceil_i64(int64_t value, int64_t stepping);
|
||||
|
||||
/// @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'.
|
||||
uint64_t ufn_floor_pow2(uint64_t value);
|
||||
|
||||
/// @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.
|
||||
/// @return Power of 2 which is the nearest higher one from 'value'.
|
||||
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
|
||||
|
Loading…
Reference in New Issue