Derive-C
Loading...
Searching...
No Matches
helpers.h
Go to the documentation of this file.
1#pragma once
3#include <stdlib.h>
4
5#define NS_EXPANDED(pre, post) pre##_##post
6#define NS(pre, post) NS_EXPANDED(pre, post)
7
8#define PRIVATE(name) NS(__private, name)
9
10#if !defined NDEBUG
11 #define DEBUG_UNUSED(ident) ident __attribute__((unused))
12#else
13 #define DEBUG_UNUSED(ident)
14#endif
15
16#define UNUSED(ident) ident __attribute__((unused))
17
18#define LIKELY(x) __builtin_expect(!!(x), 1)
19
20#define EXPAND(...) __VA_ARGS__
21
22#ifdef __cplusplus
23struct gdb_marker {
24 char UNUSED(_dummy_cpp_object_size_compatibility);
25};
26#else
27typedef struct {
29#endif
30
31static inline size_t next_power_of_2(size_t x) {
32 if (x == 0)
33 return 1;
34 x--;
35 x |= x >> 1;
36 x |= x >> 2;
37 x |= x >> 4;
38 x |= x >> 8;
39 x |= x >> 16;
40#if SIZE_MAX > 0xFFFFFFFF
41 x |= x >> 32; // For 64-bit platforms
42#endif
43 return x + 1;
44}
45
46static inline bool is_power_of_2(size_t x) { return x != 0 && (x & (x - 1)) == 0; }
47
48static inline size_t modulus_power_of_2_capacity(size_t index, size_t capacity) {
50 // NOTE: If we know capacity is a power of 2, we can reduce the cost of 'index + 1 % capacity'
51 return index & (capacity - 1);
52}
53
54#define FORCE_INLINE inline __attribute__((always_inline))
static bool is_power_of_2(size_t x)
Definition helpers.h:46
#define UNUSED(ident)
Definition helpers.h:16
static size_t next_power_of_2(size_t x)
Definition helpers.h:31
static size_t modulus_power_of_2_capacity(size_t index, size_t capacity)
Definition helpers.h:48
#define DEBUG_ASSERT(expr)
Definition panic.h:34