rstdlib

Generic Base Layer for writing C Programs
git clone anongit@rnpnr.xyz:rstdlib.git
Log | Files | Refs | Feed | README | LICENSE

Commit: b91d0d8b2e95d6784ae31f9fdafffe486e5bb1b4
Parent: 0ada27c25a107564321cdb43e884b9a9e9cb1484
Author: Randy Palamar
Date:   Sun, 22 Mar 2026 10:26:57 -0600

core: add round_up/down_power_two and str8_from_c_str

Diffstat:
Mbuild.sh | 2+-
Mrstd_core.h | 49+++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/build.sh b/build.sh @@ -6,4 +6,4 @@ cat \ rstd_intrinsics.h \ rstd_core.h \ rstd_platform.h \ ->> rstdlib.h +>| rstdlib.h diff --git a/rstd_core.h b/rstd_core.h @@ -67,6 +67,17 @@ ctz_u32(u32 a) } function force_inline u64 +clz_u64(u64 a) +{ + u64 result = 64, index; + if (a) { + _BitScanReverse64(&index, a); + result = index; + } + return result; +} + +function force_inline u64 ctz_u64(u64 a) { u64 result = 64, index; @@ -96,6 +107,14 @@ ctz_u32(u32 a) } function force_inline u64 +clz_u64(u64 a) +{ + u64 result = 64; + if (a) result = (u64)__builtin_clzll(a); + return result; +} + +function force_inline u64 ctz_u64(u64 a) { u64 result = 64; @@ -129,4 +148,34 @@ round_up_to(s64 value, s64 multiple) return result; } +// NOTE: from Hacker's Delight +function force_inline u64 +round_down_power_of_two(u64 a) +{ + u64 result = 0x8000000000000000ULL >> clz_u64(a); + return result; +} + +function force_inline u64 +round_up_power_of_two(u64 a) +{ + u64 result = 0x8000000000000000ULL >> (clz_u64(a - 1) - 1); + return result; +} + + +////////////////////////// +// NOTE: String Functions + +function str8 +str8_from_c_str(char *s) +{ + str8 result = {.data = (u8 *)s}; + if (s) { + while (*s) s++; + result.length = (u8 *)s - result.data; + } + return result; +} + #endif /* RSTD_CORE_H */