1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
// bits.h --- bit/memory manipulation utilities -*- C -*-
//
// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
//
// 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 <string.h> // 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
|