cbase 1.50.0
C/C++ Static Template
Loading...
Searching...
No Matches
Memory Alignment

Fast, bitwise macros for aligning pointers and sizes to powers of 2. More...

Macros

#define ALIGN_UP_POW2(x, p)
 Aligns a value UP to the nearest multiple of p (must be power of 2).
#define ALIGN_DOWN_POW2(x, p)
 Aligns a value DOWN to the nearest multiple of p (must be power of 2).
#define IS_POW2_OR_ZERO(x)
 Checks if a given integer is a power of 2 or zero.
#define IS_POW2(x)
 Checks if a given integer is a strictly positive power of 2.
#define SHIFT8(T, x, s)
 Shifts a byte value to a specific bit position.
#define PACK_U32_LE(a, b, c, d)
 Packs 4 bytes into a 32-bit unsigned integer (little-endian order).

Detailed Description

Fast, bitwise macros for aligning pointers and sizes to powers of 2.

Macro Definition Documentation

◆ ALIGN_DOWN_POW2

#define ALIGN_DOWN_POW2 ( x,
p )
Value:
((x) & ~((p) - 1))

Aligns a value DOWN to the nearest multiple of p (must be power of 2).

Example:

usize size = ALIGN_DOWN_POW2(13, 8); // 8
usize size = ALIGN_DOWN_POW2(16, 8); // 16 (already aligned)
#define ALIGN_DOWN_POW2(x, p)
Aligns a value DOWN to the nearest multiple of p (must be power of 2).
size_t usize
Definition base_types.h:68
Parameters
xThe value to align.
pThe alignment boundary (must be a power of 2).
Returns
The largest multiple of p that is <= x.

Definition at line 483 of file base_macros.h.

◆ ALIGN_UP_POW2

#define ALIGN_UP_POW2 ( x,
p )
Value:
(((x) + (p) - 1) & ~((p) - 1))

Aligns a value UP to the nearest multiple of p (must be power of 2).

Example:

usize size = ALIGN_UP_POW2(13, 8); // 16
usize size = ALIGN_UP_POW2(16, 8); // 16 (already aligned)
usize size = ALIGN_UP_POW2(1, 64); // 64
#define ALIGN_UP_POW2(x, p)
Aligns a value UP to the nearest multiple of p (must be power of 2).
Parameters
xThe value to align.
pThe alignment boundary (must be a power of 2).
Returns
The smallest multiple of p that is >= x.

Definition at line 468 of file base_macros.h.

◆ IS_POW2

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

Checks if a given integer is a strictly positive power of 2.

Unlike IS_POW2_OR_ZERO(), returns 0 for zero. Use this when validating alignment values since an alignment of 0 is never valid.

Example:

IS_POW2(0) // 0 (false — zero is not a valid alignment)
IS_POW2(1) // 1 (true)
IS_POW2(16) // 1 (true)
IS_POW2(15) // 0 (false)
#define IS_POW2(x)
Checks if a given integer is a strictly positive power of 2.
Parameters
xThe value to check.
Returns
1 if x > 0 and is a power of 2, 0 otherwise.

Definition at line 518 of file base_macros.h.

◆ IS_POW2_OR_ZERO

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

Checks if a given integer is a power of 2 or zero.

Example:

IS_POW2_OR_ZERO(0) // 1 (true)
IS_POW2_OR_ZERO(1) // 1 (true)
IS_POW2_OR_ZERO(16) // 1 (true)
IS_POW2_OR_ZERO(15) // 0 (false)
#define IS_POW2_OR_ZERO(x)
Checks if a given integer is a power of 2 or zero.
Parameters
xThe value to check.
Returns
1 if it is a power of 2 or zero, 0 otherwise.

Definition at line 499 of file base_macros.h.

◆ PACK_U32_LE

#define PACK_U32_LE ( a,
b,
c,
d )
Value:
(SHIFT8(U32, a, 0) | SHIFT8(U32, b, 8) | SHIFT8(U32, c, 16) | SHIFT8(U32, d, 24))
#define SHIFT8(T, x, s)
Shifts a byte value to a specific bit position.

Packs 4 bytes into a 32-bit unsigned integer (little-endian order).

Parameters
aLeast significant byte.
bSecond byte.
cThird byte.
dMost significant byte.
Returns
A packed U32 value.

Definition at line 536 of file base_macros.h.

◆ SHIFT8

#define SHIFT8 ( T,
x,
s )
Value:
((T)(x) << (s))

Shifts a byte value to a specific bit position.

Parameters
TTarget type (e.g., U32).
xValue to shift.
sBit position (0, 8, 16, 24).

Definition at line 526 of file base_macros.h.