From e691d05ceb749651e3c698fe39321b86692336f1 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Fri, 6 Nov 2026 05:42:14 +0300 Subject: feat(bits): add bit/memory manipulation utilities --- include/bits.h | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 include/bits.h (limited to 'include/bits.h') diff --git a/include/bits.h b/include/bits.h new file mode 100644 index 0000000..3c4eeb5 --- /dev/null +++ b/include/bits.h @@ -0,0 +1,141 @@ +// bits.h --- bit/memory manipulation utilities -*- C -*- +// +// Copyright (c) 2026 Martin Michalec +// +// SPDX-License-Identifier: MIT +// +// This file is part of COSMOS Codebase. +// +// Commentary: +// +// Code: + +#ifndef INCLUDED_COSMOS_BITS_H +#define INCLUDED_COSMOS_BITS_H + +#include "types.h" +#include // MAYBE(cmmm): remove libc dep? (memcpy) + +#if defined (__BYTE_ORDER__) && defined (__ORDER_LITTLE_ENDIAN__) +// GCC and Clang built-ins +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +#define HOST_LITTLE_ENDIAN 1 +#else +#define HOST_LITTLE_ENDIAN 0 +#endif +#elif defined (__LITTLE_ENDIAN__) +#define HOST_LITTLE_ENDIAN 1 +#elif defined (__BIG_ENDIAN__) +#define HOST_LITTLE_ENDIAN 0 +#elif defined (_WIN32) || defined (_MSC_VER) +#define HOST_LITTLE_ENDIAN 1 +#else +#warn "Target endianness could not be detected at compile time." +#warn "Choosing little endian as default." +#define HOST_LITTLE_ENDIAN 1 +#endif + +#if defined (__GNUC__) || defined (__clang__) || defined (__TINYC__) +#define POPCOUNT(val) __builtin_popcount (val) +#define BSWAP16(val) __builtin_bswap16 (val) +#define BSWAP32(val) __builtin_bswap32 (val) +#define BSWAP64(val) __builtin_bswap64 (val) +#elif defined (_MSC_VER) +#define BSWAP16(val) _byteswap_uint16 (val) +#define BSWAP32(val) _byteswap_uint32 (val) +#define BSWAP64(val) _byteswap_uint64 (val) +#else +#define BSWAP16(val) \ + ((((val) & 0x00ff0000) >> 8) | \ + (((val) & 0x0000ff00) << 8)) +#define BSWAP32(val) \ + ((((val) & 0xff000000) >> 24) | \ + (((val) & 0x00ff0000) >> 8) | \ + (((val) & 0x0000ff00) << 8) | \ + (((val) & 0x000000ff) << 24)) +#define BSWAP64(val) \ + ((((val) & 0xff00000000000000) >> 56) | \ + (((val) & 0x00ff000000000000) >> 40) | \ + (((val) & 0x0000ff0000000000) >> 24) | \ + (((val) & 0x000000ff00000000) >> 8) | \ + (((val) & 0x00000000ff000000) << 8) | \ + (((val) & 0x0000000000ff0000) << 24) | \ + (((val) & 0x000000000000ff00) << 40) | \ + (((val) & 0x00000000000000ff) << 56)) +#endif + +#define IS_POWER_OF_2(num) (((num) & ((num) - 1)) == 0) + +#define ALIGN_UPTO( ptr, mask) ((typeof (ptr)) ((uptr) ptr + mask & ~mask)) +#define ALIGN_DOWNTO(ptr, mask) ((typeof (ptr)) ((uptr) ptr - mask & ~mask)) + +#if HOST_LITTLE_ENDIAN == 1 +#define HTON16(host) BSWAP16 (host) +#define HTON32(host) BSWAP32 (host) +#define HTON64(host) BSWAP64 (host) +#define NTOH16(net ) BSWAP16 (net ) +#define NTOH32(net ) BSWAP32 (net ) +#define NTOH64(net ) BSWAP64 (net ) +#else +#define HTON16(host) host +#define HTON32(host) host +#define HTON64(host) host +#define NTOH16(net ) net +#define NTOH32(net ) net +#define NTOH64(net ) net +#endif + +#define ALIGN(x, n) (((x) + ((1 << (n)) - 1)) & ~((1 << (n)) - 1)) + +static inline u8 load8 (const void *p) { u8 v; memcpy (&v, p, 1); return v; } +static inline u16 load16 (const void *p) { u16 v; memcpy (&v, p, 2); return v; } +static inline u32 load32 (const void *p) { u32 v; memcpy (&v, p, 4); return v; } +static inline u64 load64 (const void *p) { u64 v; memcpy (&v, p, 8); return v; } + +static inline void store8 (void *p, u8 v) { memcpy (p, &v, 1); } +static inline void store16 (void *p, u16 v) { memcpy (p, &v, 2); } +static inline void store32 (void *p, u32 v) { memcpy (p, &v, 4); } +static inline void store64 (void *p, u64 v) { memcpy (p, &v, 8); } + +static inline u8 ror8 (u8 v, int n) { return (v >> n) | (v << 8 - n); } +static inline u16 ror16 (u16 v, int n) { return (v >> n) | (v << 16 - n); } +static inline u32 ror32 (u32 v, int n) { return (v >> n) | (v << 32 - n); } +static inline u64 ror64 (u64 v, int n) { return (v >> n) | (v << 64 - n); } + +static inline u8 rol8 (u8 v, int n) { return (v << n) | (v >> 8 - n); } +static inline u16 rol16 (u16 v, int n) { return (v << n) | (v >> 16 - n); } +static inline u32 rol32 (u32 v, int n) { return (v << n) | (v >> 32 - n); } +static inline u64 rol64 (u64 v, int n) { return (v << n) | (v >> 64 - n); } + +static inline u64 +splitmix64 (u64 state) +{ + u64 z = state + 0x9e3779b97f4a7c15; + z = (z ^ (z >> 30))*0xbf58476d1ce4e5b9; + z = (z ^ (z >> 27))*0x94d049bb133111eb; + return z ^ (z >> 31); +} + +static inline u64 +bit_ceil_64 (u64 x) +{ + if (x <= 1) + return 1; + + // TODO(cmmm): use 1 << 64 - CLZ64 (x - 1) when available + + x += 1; + + x |= x >> 0x01; + x |= x >> 0x02; + x |= x >> 0x04; + x |= x >> 0x08; + x |= x >> 0x10; + x |= x >> 0x20; + + return x + 1; +} + +#endif // INCLUDED_COSMOS_BITS_H + +// bits.h ends here -- cgit v1.3.1