Compare commits

...

4 Commits

Author SHA1 Message Date
Eric-Paul Ickhorn 6150e5dda6 documentation(readme): Extended README.md to include more information. 2024-05-23 01:23:49 +02:00
Eric-Paul Ickhorn d54bf3fe28 documentation(round): Fixed a minor typing mistake. 2024-05-23 01:20:07 +02:00
Eric-Paul Ickhorn 4244cf107b documentation(round): Added copyright-header and comments for the rounding-functions. 2024-05-23 01:18:53 +02:00
Eric-Paul Ickhorn f77dd2b23a feature(round): Added rounding library.
The first features are:
1) Floor'ing and Ceil'ing to the next value of a specific stepping
2) Floor'ing and Ceil'ing to the next power of 2.
2024-05-23 01:12:36 +02:00
2 changed files with 83 additions and 1 deletions

View File

@ -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.

74
inc-c/ufn/ufn_round.h Normal file
View File

@ -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