Derive-C
Loading...
Searching...
No Matches
math.h File Reference
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <derive-c/core/attributes.h>
#include <derive-c/core/panic.h>

Go to the source code of this file.

Macros

#define DC_MATH_IS_POWER_OF_2(x)
#define DC_MATH_MSB_INDEX(x)

Functions

static DC_INLINE DC_CONST size_t dc_math_next_power_of_2 (size_t x)
static size_t DC_INLINE DC_CONST dc_math_modulus_power_of_2_capacity (size_t index, size_t capacity)
static bool DC_INLINE DC_CONST dc_math_is_aligned_pow2_exp (const void *ptr, unsigned exp)

Macro Definition Documentation

◆ DC_MATH_IS_POWER_OF_2

#define DC_MATH_IS_POWER_OF_2 ( x)
Value:
((x) != 0 && ((x) & ((x) - 1)) == 0)

Definition at line 12 of file math.h.

◆ DC_MATH_MSB_INDEX

#define DC_MATH_MSB_INDEX ( x)
Value:
((x) == 0 ? 0 \
: _Generic((x), \
uint8_t: (7u - __builtin_clz((uint32_t)((x)) << 24)), \
uint16_t: (15u - __builtin_clz((uint32_t)((x)) << 16)), \
uint32_t: (31u - __builtin_clz((uint32_t)(x))), \
uint64_t: (63u - __builtin_clzll((uint64_t)(x)))))

Definition at line 38 of file math.h.

38 #define DC_MATH_MSB_INDEX(x) \
39 ((x) == 0 ? 0 \
40 : _Generic((x), \
41 uint8_t: (7u - __builtin_clz((uint32_t)((x)) << 24)), \
42 uint16_t: (15u - __builtin_clz((uint32_t)((x)) << 16)), \
43 uint32_t: (31u - __builtin_clz((uint32_t)(x))), \
44 uint64_t: (63u - __builtin_clzll((uint64_t)(x)))))

Function Documentation

◆ dc_math_is_aligned_pow2_exp()

bool DC_INLINE DC_CONST dc_math_is_aligned_pow2_exp ( const void * ptr,
unsigned exp )
static

Definition at line 69 of file math.h.

69 {
70 uintptr_t const mask = (1U << exp) - 1;
71 return (((uintptr_t)ptr) & mask) == 0;
72}

◆ dc_math_modulus_power_of_2_capacity()

size_t DC_INLINE DC_CONST dc_math_modulus_power_of_2_capacity ( size_t index,
size_t capacity )
static

Definition at line 63 of file math.h.

64 {
66 return index & (capacity - 1);
67}
static size_t capacity()
Definition template.h:66
#define DC_MATH_IS_POWER_OF_2(x)
Definition math.h:12
#define DC_ASSUME(expr,...)
Definition panic.h:56

◆ dc_math_next_power_of_2()

DC_INLINE DC_CONST size_t dc_math_next_power_of_2 ( size_t x)
static

Definition at line 48 of file math.h.

48 {
49 if (x == 0)
50 return 1;
51 x--;
52 x |= x >> 1;
53 x |= x >> 2;
54 x |= x >> 4;
55 x |= x >> 8;
56 x |= x >> 16;
57#if SIZE_MAX > 0xFFFFFFFF
58 x |= x >> 32; // For 64-bit platforms
59#endif
60 return x + 1;
61}