cbase 1.50.0
C/C++ Static Template
Loading...
Searching...
No Matches
Utility Math & Helpers

Quality-of-life macros for strings, pointers, and math. More...

Macros

#define STRINGIFY_DETAIL(x)
 Internal implementation of STRINGIFY. Do not use directly.
#define STRINGIFY(x)
 Safely converts a macro argument into a string literal.
#define GLUE_DETAIL(x, y)
 Internal implementation of GLUE. Do not use directly.
#define GLUE(x, y)
 Safely concatenates two tokens into a single token.
#define INT_FROM_PTR(ptr)
 Safely casts a pointer to a uintptr_t integer.
#define PTR_FROM_INT(type, val)
 Safely casts a pointer-sized integer back to a typed pointer.
#define ARRAY_SIZE(arr)
 Calculates the number of elements in a statically allocated array.
#define MIN(a, b)
 Returns the minimum of two values (Standard C fallback).
#define MAX(a, b)
 Returns the maximum of two values (Standard C fallback).
#define CLAMP(val, min, max)
 Clamps a value between a minimum and a maximum (Standard C fallback).
#define SWAP(type, a, b)
 Generic variable swap using a temporary.
#define Boolify(x)
 Converts any scalar value to a strict 1 or 0 boolean.
#define AsciiID4(a, b, c, d)
 Packs four ASCII characters into a single 32-bit FourCC integer.
#define ExpandAsciiID(x)
 Expands a FourCC u32 for use in a printf "%.*s" format specifier.

Detailed Description

Quality-of-life macros for strings, pointers, and math.

Macro Definition Documentation

◆ ARRAY_SIZE

#define ARRAY_SIZE ( arr)
Value:
(sizeof(arr) / sizeof((arr)[0]))

Calculates the number of elements in a statically allocated array.

Example:

int primes[] = {2, 3, 5, 7, 11};
for (usize i = 0; i < ARRAY_SIZE(primes); i++) {
printf("%d\n", primes[i]);
}
size_t usize
Definition base_types.h:68
#define ARRAY_SIZE(arr)
Calculates the number of elements in a statically allocated array.
Warning
Do not pass a decayed pointer. Only works on actual stack arrays.
Parameters
arrThe static array.
Returns
The element count as a size_t.

Definition at line 318 of file base_macros.h.

◆ AsciiID4

#define AsciiID4 ( a,
b,
c,
d )
Value:
(((u32)(u8)(d) << 24) | ((u32)(u8)(c) << 16) | ((u32)(u8)(b) << 8) | (u32)(u8)(a))
uint8_t u8
Definition base_types.h:46
uint32_t u32
Definition base_types.h:48

Packs four ASCII characters into a single 32-bit FourCC integer.

FourCC codes are commonly used for file format identification and chunk tags in binary formats (RIFF, IFF, etc.).

Example:

u32 riff_id = AsciiID4('R', 'I', 'F', 'F');
u32 wave_id = AsciiID4('W', 'A', 'V', 'E');
if (header.id == riff_id) { ... }
#define AsciiID4(a, b, c, d)
Packs four ASCII characters into a single 32-bit FourCC integer.
Parameters
a1st character (least significant byte).
b2nd character.
c3rd character.
d4th character (most significant byte).
Returns
A u32 with the four bytes packed in little-endian order.

Definition at line 431 of file base_macros.h.

◆ Boolify

#define Boolify ( x)
Value:
((x) != 0)

Converts any scalar value to a strict 1 or 0 boolean.

Useful when you need a guaranteed 0/1 integer from an expression that might return any non-zero value (e.g., a pointer, a flags field).

Example:

int flags = 0b1010;
b32 has_flag = Boolify(flags & 0b0010); // 1, not 2
#define Boolify(x)
Converts any scalar value to a strict 1 or 0 boolean.
int32_t b32
Definition base_types.h:111
Parameters
xThe value to evaluate.
Returns
1 if x is non-zero, 0 otherwise.

Definition at line 409 of file base_macros.h.

◆ CLAMP

#define CLAMP ( val,
min,
max )
Value:
(((val) < (min)) ? (min) : ((val) > (max)) ? (max) : (val))

Clamps a value between a minimum and a maximum (Standard C fallback).

Definition at line 367 of file base_macros.h.

◆ ExpandAsciiID

#define ExpandAsciiID ( x)
Value:
(int)(sizeof(x)), (char *)(&(x))

Expands a FourCC u32 for use in a printf "%.*s" format specifier.

Example:

u32 id = AsciiID4('R', 'I', 'F', 'F');
printf("chunk id: %.*s\n", ExpandAsciiID(id)); // prints: chunk id: RIFF
#define ExpandAsciiID(x)
Expands a FourCC u32 for use in a printf "%.*s" format specifier.
Parameters
xThe FourCC integer to expand.

Definition at line 445 of file base_macros.h.

◆ GLUE

#define GLUE ( x,
y )
Value:
#define GLUE_DETAIL(x, y)
Internal implementation of GLUE. Do not use directly.

Safely concatenates two tokens into a single token.

Double-expansion ensures macros in x or y are expanded first.

Example:

#define PREFIX my_
void GLUE(PREFIX, function)(void); // expands to: void my_function(void);
#define function
File-scoped function (invisible to the linker).
#define GLUE(x, y)
Safely concatenates two tokens into a single token.
Parameters
xThe first token.
yThe second token.

Definition at line 271 of file base_macros.h.

◆ GLUE_DETAIL

#define GLUE_DETAIL ( x,
y )
Value:
x##y

Internal implementation of GLUE. Do not use directly.

Definition at line 256 of file base_macros.h.

◆ INT_FROM_PTR

#define INT_FROM_PTR ( ptr)
Value:
((uintptr_t)(ptr))

Safely casts a pointer to a uintptr_t integer.

Example:

void *ptr = some_allocation();
uintptr_t addr = INT_FROM_PTR(ptr);
printf("allocated at 0x%llx\n", (unsigned long long)addr);
#define INT_FROM_PTR(ptr)
Safely casts a pointer to a uintptr_t integer.
Parameters
ptrThe pointer to cast.
Returns
An unsigned integer representing the memory address.

Definition at line 286 of file base_macros.h.

◆ MAX

#define MAX ( a,
b )
Value:
(((a) > (b)) ? (a) : (b))

Returns the maximum of two values (Standard C fallback).

Definition at line 365 of file base_macros.h.

◆ MIN

#define MIN ( a,
b )
Value:
(((a) < (b)) ? (a) : (b))

Returns the minimum of two values (Standard C fallback).

Definition at line 363 of file base_macros.h.

◆ PTR_FROM_INT

#define PTR_FROM_INT ( type,
val )
Value:
((type *)(uintptr_t)(val))

Safely casts a pointer-sized integer back to a typed pointer.

Example:

uintptr_t addr = INT_FROM_PTR(my_ptr);
MyStruct *p = PTR_FROM_INT(MyStruct, addr);
#define PTR_FROM_INT(type, val)
Safely casts a pointer-sized integer back to a typed pointer.
Parameters
typeThe type of pointer to return.
valThe integer memory address.
Returns
A casted pointer of the given type.

Definition at line 301 of file base_macros.h.

◆ STRINGIFY

#define STRINGIFY ( x)
Value:
#define STRINGIFY_DETAIL(x)
Internal implementation of STRINGIFY. Do not use directly.

Safely converts a macro argument into a string literal.

Unlike the # operator used directly, this double-expansion ensures that macro arguments are expanded before stringification.

Example:

#define VERSION 42
const char *s = STRINGIFY(VERSION); // "42", not "VERSION"
#define STRINGIFY(x)
Safely converts a macro argument into a string literal.
Parameters
xThe token to stringify (macros are expanded first).
Returns
A string literal of the evaluated token.

Definition at line 253 of file base_macros.h.

◆ STRINGIFY_DETAIL

#define STRINGIFY_DETAIL ( x)
Value:
#x

Internal implementation of STRINGIFY. Do not use directly.

Definition at line 237 of file base_macros.h.

◆ SWAP

#define SWAP ( type,
a,
b )
Value:
do { \
type _tmp = (a); \
(a) = (b); \
(b) = _tmp; \
} while (0)

Generic variable swap using a temporary.

Example:

int a = 10, b = 20;
SWAP(int, a, b);
// a == 20, b == 10
float *p = arr_a, *q = arr_b;
SWAP(float *, p, q);
#define SWAP(type, a, b)
Generic variable swap using a temporary.
Parameters
typeThe data type of the variables being swapped.
aThe first variable.
bThe second variable.

Definition at line 387 of file base_macros.h.