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>
#include <derive-c/core/compiler.h>
#include <derive-c/core/namespace.h>
#include <bit>
#include <type_traits>

Go to the source code of this file.

Namespaces

namespace  dc
namespace  dc::math

Macros

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

Functions

template<typename T>
constexpr unsigned dc::math::msb_index (T x) noexcept
static DC_INLINE DC_CONST size_t dc_math_next_power_of_2 (size_t x)
static DC_PUBLIC size_t DC_INLINE DC_CONST dc_math_modulus_power_of_2_capacity (size_t index, size_t capacity)
static DC_PUBLIC bool DC_INLINE DC_CONST dc_math_is_aligned_pow2 (const void *ptr, unsigned alignment)

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 14 of file math.h.

◆ DC_MATH_MSB_INDEX

#define DC_MATH_MSB_INDEX ( x)
Value:
constexpr unsigned msb_index(T x) noexcept
Definition math.h:30

Definition at line 43 of file math.h.

Function Documentation

◆ dc_math_is_aligned_pow2()

DC_PUBLIC bool DC_INLINE DC_CONST dc_math_is_aligned_pow2 ( const void * ptr,
unsigned alignment )
static

Definition at line 67 of file math.h.

68 {
70 return ((uintptr_t)ptr & (alignment - 1)) == 0;
71}
#define DC_MATH_IS_POWER_OF_2(x)
Definition math.h:14
#define DC_ASSUME(expr,...)
Definition panic.h:57

◆ dc_math_modulus_power_of_2_capacity()

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

Definition at line 61 of file math.h.

62 {
64 return index & (capacity - 1);
65}

◆ 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 46 of file math.h.

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