ic-macros  0.1.5
util.h File Reference
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <tgmath.h>
#include "ic_macros/assert_constant.h"
#include "ic_macros/assert_type.h"
#include "ic_macros/pragma_message.h"
#include "zephyr/build_assert.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define IC_NUM_BITS(t)   (sizeof(t) * 8)
 Number of bits that make up a type. More...
 
#define IC_POINTER_TO_UINT(x)   ((uintptr_t) (x))
 Cast x, a pointer, to an unsigned integer. More...
 
#define IC_UINT_TO_POINTER(x)   ((void *) (uintptr_t) (x))
 Cast x, an unsigned integer, to a void*. More...
 
#define IC_POINTER_TO_INT(x)   ((intptr_t) (x))
 Cast x, a pointer, to a signed integer. More...
 
#define IC_INT_TO_POINTER(x)   ((void *) (intptr_t) (x))
 Cast x, a signed integer, to a void*. More...
 
#define IC_GENMASK32(h, l)   (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (32UL - 1 - (h))))
 Create a contiguous bitmask starting at bit position l and ending at position h. More...
 
#define IC_GENMASK64(h, l)   (((~0ULL) - (1ULL << (l)) + 1) & (~0ULL >> (64ULL - 1 - (h))))
 Create a contiguous 64-bit bitmask starting at bit position l and ending at position h. More...
 
#define IC_G_LSB_GET(value)   ((value) & -(value))
 Only use in global scope; extract the Least Significant Bit from value. More...
 
#define IC_LSB_GET(value)   _IC_MAKE_SINGLE_EVAL_1(value)
 Extract the Least Significant Bit from value. More...
 
#define IC_G_FIELD_GET(mask, value)   (((value) & (mask)) / IC_LSB_GET(mask))
 Only use in global scope; extract a bitfield element from value corresponding to the field mask mask. More...
 
#define IC_FIELD_GET(mask, value)   _IC_MAKE_SINGLE_EVAL_2(IC_G_FIELD_GET, mask, value)
 Extract a bitfield element from value corresponding to the field mask mask. More...
 
#define IC_G_FIELD_PREP(mask, value)   (((value) * IC_LSB_GET(mask)) & (mask))
 Only use in global scope; prepare a bitfield element using value with mask representing its field position and width. The result should be combined with other fields using a logical OR. More...
 
#define IC_FIELD_PREP(mask, value)   _IC_MAKE_SINGLE_EVAL_2(IC_G_FIELD_PREP, mask, value)
 Prepare a bitfield element using value with mask representing its field position and width. The result should be combined with other fields using a logical OR. More...
 
#define IC_ZERO_OR_COMPILE_ERROR(cond)   ((int) sizeof(char[1 - 2 * !(cond)]) - 1)
 0 if cond is true-ish; causes a compile error otherwise. More...
 
#define IC_ARRAY_LENGTH(array)    (unsigned int) (_IC_IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0])))
 Number of elements in the given array; the "length". More...
 
#define IC_ARRAY_SIZE(array)   (size_t)(_IC_IS_ARRAY(array) + sizeof(array))
 Size of the given array in 8-bit bytes. More...
 
#define IC_IS_ARRAY_ELEMENT(array, ptr)
 Whether ptr is an element of array. More...
 
#define IC_ARRAY_INDEX(array, ptr)    IC_IS_ARRAY_ELEMENT(array, ptr) ? (__typeof__((array)[0]) *) (ptr) - (array) : -1
 Index of ptr within array. More...
 
#define IC_PART_OF_ARRAY(array, ptr)
 Check if a pointer ptr lies within array. More...
 
#define IC_ARRAY_INDEX_FLOOR(array, ptr)
 Array-index of ptr within array, rounded down. More...
 
#define IC_SAME_TYPE(a, b)   __builtin_types_compatible_p(__typeof__(a), __typeof__(b))
 Validate if two entities have a compatible type. More...
 
#define IC_CONTAINER_OF(ptr, type, field)
 Get a pointer to a structure containing the element. More...
 
#define IC_G_IROUND_UP(x, align)
 Only use in global scope; value of integer x rounded up to the next multiple of align. More...
 
#define IC_IROUND_UP(x, align)   _IC_MAKE_SINGLE_EVAL_2(IC_G_IROUND_UP, x, align)
 Value of integer x rounded up to the next multiple of align. More...
 
#define IC_G_IROUND_DOWN(x, align)    (((unsigned long) (x) / (unsigned long) (align)) * (unsigned long) (align));
 Only use in global scope; value of integer x rounded down to the next multiple of align. More...
 
#define IC_IROUND_DOWN(x, align)   _IC_MAKE_SINGLE_EVAL_2(IC_IROUND_DOWN, x, align)
 Value of integer x rounded down to the next multiple of align. More...
 
#define IC_WB_UP(x)   IC_IROUND_UP(x, sizeof(void *))
 Value of x rounded up to the next word boundary. More...
 
#define IC_WB_DN(x)   IC_IROUND_DOWN(x, sizeof(void *))
 Value of x rounded down to the previous word boundary. More...
 
#define IC_G_CEIL(x)   ceil(x)
 Only use in global scope; compile-time floating point ceiling of f32 (float), f64 (double), & f128 (long double). More...
 
#define IC_CEIL(x)
 Compile-time floating point ceiling of f32 (float), f64 (double), & f128 (long double). More...
 
#define IC_G_FLOOR(x)   floor(x)
 Only use in global scope; compile-time floating point floor of f32 (float), f64 (double), & f128 (long double). More...
 
#define IC_FLOOR(x)
 Compile-time floating point floor of f32 (float), f64 (double), & f128 (long double). More...
 
#define IC_G_ROUND(x)   (__typeof__(x)) (int64_t) ((x) + ((x) >= 0 ? 0.5 : -0.5))
 Only use in global scope; round of f32 (float), f64 (double), & f128 (long double). More...
 
#define IC_ROUND(x)
 Round of f32 (float), f64 (double), & f128 (long double). More...
 
#define IC_G_DIV_ROUND_UP(n, d)   (((n) + (d) -1) / (d))
 Only use in global scope; divide and round up. More...
 
#define IC_DIV_ROUND_UP(n, d)   _IC_MAKE_SINGLE_EVAL_2(IC_G_DIV_ROUND_UP, n, d)
 Divide and round up. More...
 
#define IC_G_DIV_ROUND_CLOSEST(n, d)    ((((n) < 0) ^ ((d) < 0)) ? ((n) - ((d) / 2)) / (d) : ((n) + ((d) / 2)) / (d))
 Only use in global scope; divide and round to the nearest integer. More...
 
#define IC_DIV_ROUND_CLOSEST(n, d)   _IC_MAKE_SINGLE_EVAL_2(IC_G_DIV_ROUND_CLOSEST, n, d)
 Divide and round to the nearest integer. More...
 
#define IC_G_MAX(a, b)   (((a) > (b)) ? (a) : (b))
 Only use in global scope; obtain the maximum of two values. More...
 
#define IC_MAX(a, b)   _IC_MAKE_SINGLE_EVAL_2(IC_G_MAX, a, b)
 Obtain the maximum of two values. More...
 
#define IC_G_MIN(a, b)   (((a) < (b)) ? (a) : (b))
 Only use in global scope; obtain the minimum of two values. More...
 
#define IC_MIN(a, b)   _IC_MAKE_SINGLE_EVAL_2(IC_G_MIN, a, b)
 Obtain the minimum of two values. More...
 
#define IC_G_CLAMP(val, low, high)   (((val) <= (low)) ? (low) : IC_MIN(val, high))
 Only use in global scope; clamp a value to a given range. More...
 
#define IC_CLAMP(val, low, high)   _IC_MAKE_SINGLE_EVAL_3(IC_G_CLAMP, val, low, high)
 Clamp a value to a given range. More...
 
#define IC_G_IN_RANGE(val, min, max)    ((__typeof__(val)) -1) > 0 ? ((val) <= (max)) : ((val) >= (min) && (val) <= (max))
 Only use in global scope; checks if a value is within range. More...
 
#define IC_IN_RANGE(val, min, max)   _IC_MAKE_SINGLE_EVAL_3(IC_G_IN_RANGE, val, min, max)
 Checks if a value is within range. More...
 
#define IC_G_ILOG2(x)    (sizeof(__typeof__(x)) > 4 ? (64 - __builtin_clzll(x) - 1) : (32 - __builtin_clz(x) - 1))
 Only use in global scope; compute integer value of log2(x) More...
 
#define IC_ILOG2(x)
 Compute integer value log2(x) More...
 
#define IC_G_LOG2CEIL(x)   (x) < 1 ? 0 : IC_G_ILOG2((x) -1) + 1
 Only use in global scope; compute ceil(log2(x)) More...
 
#define IC_LOG2CEIL(x)   _IC_MAKE_SINGLE_EVAL_1(IC_G_LOG2CEIL, x)
 Compute ceil(log2(x)) More...
 
#define IC_G_NHPOT(x)   ((x) < 1 ? 1 : ((x) > (1ULL << 63) ? 0 : 1ULL << IC_G_LOG2CEIL(x)))
 Only use in global scope; compute next highest power of two. More...
 
#define IC_NHPOT(x)   _IC_MAKE_SINGLE_EVAL_1(IC_G_NHPOT, x)
 Compute next highest power of two. More...
 
#define IC_G_POW2_CEIL(x)   ((x) <= 2UL ? (x) : (1UL << (8 * sizeof(long) - __builtin_clzl((x) -1))))
 Only use in global scope; calculate power of two ceiling for some nonzero value. More...
 
#define IC_POW2_CEIL(x)   _IC_MAKE_SINGLE_EVAL_1(IC_G_POW2_CEIL, x)
 Calculate power of two ceiling for some nonzero value. More...
 
#define IC_G_IS_POW2(x)   (((x) != 0) && (((x) & ((x) -1)) == 0))
 Only use in global scope; check whether or not a value is a power of 2. More...
 
#define IC_IS_POW2(x)   _IC_MAKE_SINGLE_EVAL_1(IC_G_IS_POW2)
 Check whether or not a value is a power of 2. More...
 
#define IC_BIT(n)   (1UL << (n))
 Unsigned integer with bit position n set. More...
 
#define IC_BIT64(n)   (1ULL << (n))
 64-bit unsigned integer with bit position n set. More...
 
#define IC_G_WRITE_BIT(var, bit, set)   (set) ? ((var) | IC_BIT(bit)) : ((var) & ~IC_BIT(bit))
 Only use in global scope; set or clear a bit depending on a boolean value. More...
 
#define IC_WRITE_BIT(var, bit, set)   _IC_MAKE_SINGLE_EVAL_3(IC_G_WRITE_BIT, var, bit, set)
 Set or clear a bit depending on a boolean value. More...
 
#define IC_BIT_MASK(n)   (IC_BIT(n) - 1UL)
 Bit mask with bits 0 through n-1 (inclusive) set, or 0 if n is 0. More...
 
#define IC_BIT64_MASK(n)   (IC_BIT64(n) - 1ULL)
 64-bit bit mask with bits 0 through n-1 (inclusive) set, or 0 if n is 0. More...
 
#define IC_G_IS_SHIFTED_BIT_MASK(m, s)   (!(((m) >> (s)) & (((m) >> (s)) + 1U)))
 Only use in global scope; check if bits are set continuously from the specified bit. More...
 
#define IC_IS_SHIFTED_BIT_MASK(m, s)   _IC_MAKE_SINGLE_EVAL_2(IC_G_IS_SHIFTED_BIT_MASK, m, s)
 Check if bits are set continuously from the specified bit. More...
 
#define IC_G_IS_BIT_MASK(m)   IC_G_IS_SHIFTED_BIT_MASK(m, 0)
 Only use in global scope; check if bits are set continuously from the LSB. More...
 
#define IC_IS_BIT_MASK(m)   IC_IS_SHIFTED_BIT_MASK(m, 0)
 Check if bits are set continuously from the LSB. More...
 
#define IC_G_BSWAP24(x)    (((x) >> 16) & 0x0000FF) | (((x) >> 00) & 0x00FF00) | ((((x) & 0x0000FF) << 16))
 Only use in global scope; 24-bit byte swap. More...
 
#define IC_BSWAP24(x)   _IC_MAKE_SINGLE_EVAL_1(IC_G_BSWAP24, x)
 24-bit byte swap. More...
 

Macro Definition Documentation

◆ IC_ARRAY_INDEX

#define IC_ARRAY_INDEX (   array,
  ptr 
)     IC_IS_ARRAY_ELEMENT(array, ptr) ? (__typeof__((array)[0]) *) (ptr) - (array) : -1

Index of ptr within array.

This macro will return -1 when ptr does not fall into the range of array or when ptr is not aligned to an array-element boundary of array.

In C, passing a pointer as array causes a compile error.

Parameters
arraythe array in question
ptrpointer to an element of array
Returns
-1 on error, or the array index of ptr within array, on success

Definition at line 178 of file util.h.

◆ IC_ARRAY_INDEX_FLOOR

#define IC_ARRAY_INDEX_FLOOR (   array,
  ptr 
)
Value:
IC_PART_OF_ARRAY(array, ptr) \
? (IC_POINTER_TO_UINT(ptr) - IC_POINTER_TO_UINT(array)) / sizeof((array)[0]) : -1
#define IC_PART_OF_ARRAY(array, ptr)
Check if a pointer ptr lies within array.
Definition: util.h:191
#define IC_POINTER_TO_UINT(x)
Cast x, a pointer, to an unsigned integer.
Definition: util.h:55

Array-index of ptr within array, rounded down.

This macro behaves much like IC_ARRAY_INDEX with the notable difference that it accepts any ptr in the range of array rather than exclusively a ptr aligned to an array-element boundary of array.

This macro will return -1 when ptr does not fall into the range of array.

In C, passing a pointer as array causes a compile error.

Parameters
arraythe array in question
ptrpointer to an element of array
Returns
-1 on error, or the array index of ptr within array, on success

Definition at line 212 of file util.h.

◆ IC_ARRAY_LENGTH

#define IC_ARRAY_LENGTH (   array)     (unsigned int) (_IC_IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0])))

Number of elements in the given array; the "length".

Passing a pointer as array causes a compile error.

Parameters
arrayA C array (that has not decayed to a pointer)
Returns
unsigned int - number of elements in the array

Definition at line 130 of file util.h.

◆ IC_ARRAY_SIZE

#define IC_ARRAY_SIZE (   array)    (size_t)(_IC_IS_ARRAY(array) + sizeof(array))

Size of the given array in 8-bit bytes.

Passing a pointer as array causes a compile error.

Parameters
arrayA C array (that has not decayed to a pointer)
Returns
size_t - size of the array in 8-bit bytes

Definition at line 141 of file util.h.

◆ IC_BIT

#define IC_BIT (   n)    (1UL << (n))

Unsigned integer with bit position n set.

Definition at line 664 of file util.h.

◆ IC_BIT64

#define IC_BIT64 (   n)    (1ULL << (n))

64-bit unsigned integer with bit position n set.

Definition at line 667 of file util.h.

◆ IC_BIT64_MASK

#define IC_BIT64_MASK (   n)    (IC_BIT64(n) - 1ULL)

64-bit bit mask with bits 0 through n-1 (inclusive) set, or 0 if n is 0.

Definition at line 701 of file util.h.

◆ IC_BIT_MASK

#define IC_BIT_MASK (   n)    (IC_BIT(n) - 1UL)

Bit mask with bits 0 through n-1 (inclusive) set, or 0 if n is 0.

Definition at line 695 of file util.h.

◆ IC_BSWAP24

#define IC_BSWAP24 (   x)    _IC_MAKE_SINGLE_EVAL_1(IC_G_BSWAP24, x)

24-bit byte swap.

Parameters
x24-bit integer to byte swap.

Definition at line 750 of file util.h.

◆ IC_CEIL

#define IC_CEIL (   x)
Value:
({ \
IC_BUILD_ASSERT( \
IC_SAME_TYPE(x, float) || IC_SAME_TYPE(x, double) || IC_SAME_TYPE(x, long double), \
"Argument must be a floating point number!" \
); \
/* use <tgmath.h> */ \
IC_REQUIRE_CONSTANT(ceil(x)); \
})
#define IC_SAME_TYPE(a, b)
Validate if two entities have a compatible type.
Definition: util.h:223

Compile-time floating point ceiling of f32 (float), f64 (double), & f128 (long double).

Useful for casting to an integer: #define ROUNDED_UP IC_CEIL(MY_FLOATING_POINT_NUMBER)

Note
Generates compile error if x is not a compile-time constant; use ceil(x) from <tgmath.h>.
Parameters
xfloating point number to ceiling.
Returns
the output of ceil(x); same type as the argument

Definition at line 325 of file util.h.

◆ IC_CLAMP

#define IC_CLAMP (   val,
  low,
  high 
)    _IC_MAKE_SINGLE_EVAL_3(IC_G_CLAMP, val, low, high)

Clamp a value to a given range.

Parameters
valValue to be clamped.
lowLowest allowed value (inclusive).
highHighest allowed value (inclusive).
Returns
Clamped value.
Note
Expressions are only evaluated once.

Definition at line 536 of file util.h.

◆ IC_CONTAINER_OF

#define IC_CONTAINER_OF (   ptr,
  type,
  field 
)
Value:
({ \
_IC_CONTAINER_OF_VALIDATE(ptr, type, field) \
((type *) (((char *) (ptr)) - offsetof(type, field))); \
})

Get a pointer to a structure containing the element.

Example:

struct foo { int bar; };

struct foo my_foo; int * ptr = &my_foo.bar;

struct foo * container = IC_CONTAINER_OF(ptr, struct foo, bar);

Above, container points at my_foo.

Parameters
ptrpointer to a structure element
typename of the type that ptr is an element of
fieldthe name of the field within the struct ptr points to
Returns
a pointer to the structure that contains ptr

Definition at line 259 of file util.h.

◆ IC_DIV_ROUND_CLOSEST

#define IC_DIV_ROUND_CLOSEST (   n,
 
)    _IC_MAKE_SINGLE_EVAL_2(IC_G_DIV_ROUND_CLOSEST, n, d)

Divide and round to the nearest integer.

Example:

IC_DIV_ROUND_CLOSEST(5, -2); // -3
#define IC_DIV_ROUND_CLOSEST(n, d)
Divide and round to the nearest integer.
Definition: util.h:467
Parameters
nNumerator.
dDenominator.
Returns
The result of n / d, rounded to the nearest integer.
Note
Expressions are only evaluated once.

Definition at line 467 of file util.h.

◆ IC_DIV_ROUND_UP

#define IC_DIV_ROUND_UP (   n,
 
)    _IC_MAKE_SINGLE_EVAL_2(IC_G_DIV_ROUND_UP, n, d)

Divide and round up.

Example:

IC_DIV_ROUND_UP(1, 2); // 1
IC_DIV_ROUND_UP(3, 2); // 2
#define IC_DIV_ROUND_UP(n, d)
Divide and round up.
Definition: util.h:430
Parameters
nNumerator.
dDenominator.
Returns
The result of n / d, rounded up.
Note
Expressions are only evaluated once.

Definition at line 430 of file util.h.

◆ IC_FIELD_GET

#define IC_FIELD_GET (   mask,
  value 
)    _IC_MAKE_SINGLE_EVAL_2(IC_G_FIELD_GET, mask, value)

Extract a bitfield element from value corresponding to the field mask mask.

Definition at line 92 of file util.h.

◆ IC_FIELD_PREP

#define IC_FIELD_PREP (   mask,
  value 
)    _IC_MAKE_SINGLE_EVAL_2(IC_G_FIELD_PREP, mask, value)

Prepare a bitfield element using value with mask representing its field position and width. The result should be combined with other fields using a logical OR.

Definition at line 106 of file util.h.

◆ IC_FLOOR

#define IC_FLOOR (   x)
Value:
({ \
IC_BUILD_ASSERT( \
IC_SAME_TYPE(x, float) || IC_SAME_TYPE(x, double) || IC_SAME_TYPE(x, long double), \
"Argument must be a floating point number!" \
); \
/* use <tgmath.h> */ \
IC_REQUIRE_CONSTANT(floor(x)); \
})

Compile-time floating point floor of f32 (float), f64 (double), & f128 (long double).

Useful for casting to an integer: #define ROUNDED_DOWN IC_FLOOR(MY_FLOATING_POINT_NUMBER)

Note
Generates compile error if x is not a compile-time constant; use floor(x) from <tgmath.h>.
Parameters
xfloating point number to floor.
Returns
the output of floor(x); same type as the argument

Definition at line 356 of file util.h.

◆ IC_G_BSWAP24

#define IC_G_BSWAP24 (   x)     (((x) >> 16) & 0x0000FF) | (((x) >> 00) & 0x00FF00) | ((((x) & 0x0000FF) << 16))

Only use in global scope; 24-bit byte swap.

Parameters
x24-bit integer to byte swap.

Definition at line 742 of file util.h.

◆ IC_G_CEIL

#define IC_G_CEIL (   x)    ceil(x)

Only use in global scope; compile-time floating point ceiling of f32 (float), f64 (double), & f128 (long double).

Useful for casting to an integer: #define ROUNDED_UP IC_CEIL(MY_FLOATING_POINT_NUMBER)

Parameters
xfloating point number to ceiling.
Returns
the output of ceil(x); same type as the argument

Definition at line 313 of file util.h.

◆ IC_G_CLAMP

#define IC_G_CLAMP (   val,
  low,
  high 
)    (((val) <= (low)) ? (low) : IC_MIN(val, high))

Only use in global scope; clamp a value to a given range.

Parameters
valValue to be clamped.
lowLowest allowed value (inclusive).
highHighest allowed value (inclusive).
Returns
Clamped value.

Definition at line 523 of file util.h.

◆ IC_G_DIV_ROUND_CLOSEST

#define IC_G_DIV_ROUND_CLOSEST (   n,
 
)     ((((n) < 0) ^ ((d) < 0)) ? ((n) - ((d) / 2)) / (d) : ((n) + ((d) / 2)) / (d))

Only use in global scope; divide and round to the nearest integer.

Example:

IC_DIV_ROUND_CLOSEST(5, -2); // -3
Parameters
nNumerator.
dDenominator.
Returns
The result of n / d, rounded to the nearest integer.

Definition at line 447 of file util.h.

◆ IC_G_DIV_ROUND_UP

#define IC_G_DIV_ROUND_UP (   n,
 
)    (((n) + (d) -1) / (d))

Only use in global scope; divide and round up.

Example:

IC_DIV_ROUND_UP(1, 2); // 1
IC_DIV_ROUND_UP(3, 2); // 2
Parameters
nNumerator.
dDenominator.
Returns
The result of n / d, rounded up.

Definition at line 412 of file util.h.

◆ IC_G_FIELD_GET

#define IC_G_FIELD_GET (   mask,
  value 
)    (((value) & (mask)) / IC_LSB_GET(mask))

Only use in global scope; extract a bitfield element from value corresponding to the field mask mask.

Definition at line 86 of file util.h.

◆ IC_G_FIELD_PREP

#define IC_G_FIELD_PREP (   mask,
  value 
)    (((value) * IC_LSB_GET(mask)) & (mask))

Only use in global scope; prepare a bitfield element using value with mask representing its field position and width. The result should be combined with other fields using a logical OR.

Definition at line 99 of file util.h.

◆ IC_G_FLOOR

#define IC_G_FLOOR (   x)    floor(x)

Only use in global scope; compile-time floating point floor of f32 (float), f64 (double), & f128 (long double).

Useful for casting to an integer: #define ROUNDED_DOWN IC_FLOOR(MY_FLOATING_POINT_NUMBER)

Note
Generates compile error if x is not a compile-time constant; use floor(x) from <tgmath.h>.
Parameters
xfloating point number to floor.
Returns
the output of floor(x); same type as the argument

Definition at line 345 of file util.h.

◆ IC_G_ILOG2

#define IC_G_ILOG2 (   x)     (sizeof(__typeof__(x)) > 4 ? (64 - __builtin_clzll(x) - 1) : (32 - __builtin_clz(x) - 1))

Only use in global scope; compute integer value of log2(x)

Parameters
xAn unsigned integral value to compute logarithm of (positive only)
Returns
log2(x)

Definition at line 572 of file util.h.

◆ IC_G_IN_RANGE

#define IC_G_IN_RANGE (   val,
  min,
  max 
)     ((__typeof__(val)) -1) > 0 ? ((val) <= (max)) : ((val) >= (min) && (val) <= (max))

Only use in global scope; checks if a value is within range.

Parameters
valValue to be checked.
minLower bound (inclusive).
maxUpper bound (inclusive).
Return values
trueIf value is within range
falseIf the value is not within range

Definition at line 548 of file util.h.

◆ IC_G_IROUND_DOWN

#define IC_G_IROUND_DOWN (   x,
  align 
)     (((unsigned long) (x) / (unsigned long) (align)) * (unsigned long) (align));

Only use in global scope; value of integer x rounded down to the next multiple of align.

Parameters
xinteger to round
Returns
the rounded integer

Definition at line 288 of file util.h.

◆ IC_G_IROUND_UP

#define IC_G_IROUND_UP (   x,
  align 
)
Value:
((((unsigned long) (x) + ((unsigned long) (align) -1)) / (unsigned long) (align)) \
* (unsigned long) (align))

Only use in global scope; value of integer x rounded up to the next multiple of align.

Parameters
xinteger to round
Returns
the rounded integer

Definition at line 271 of file util.h.

◆ IC_G_IS_BIT_MASK

#define IC_G_IS_BIT_MASK (   m)    IC_G_IS_SHIFTED_BIT_MASK(m, 0)

Only use in global scope; check if bits are set continuously from the LSB.

Parameters
mCheck whether the bits are set continuously from LSB.

Definition at line 728 of file util.h.

◆ IC_G_IS_POW2

#define IC_G_IS_POW2 (   x)    (((x) != 0) && (((x) & ((x) -1)) == 0))

Only use in global scope; check whether or not a value is a power of 2.

Parameters
xThe value to check
Returns
true if x is a power of 2, false otherwise

Definition at line 653 of file util.h.

◆ IC_G_IS_SHIFTED_BIT_MASK

#define IC_G_IS_SHIFTED_BIT_MASK (   m,
 
)    (!(((m) >> (s)) & (((m) >> (s)) + 1U)))

Only use in global scope; check if bits are set continuously from the specified bit.

Note
The macro is not dependent on the bit-width.
Parameters
mCheck whether the bits are set continuously or not.
sSpecify the lowest bit for that is continuously set bits.

Definition at line 711 of file util.h.

◆ IC_G_LOG2CEIL

#define IC_G_LOG2CEIL (   x)    (x) < 1 ? 0 : IC_G_ILOG2((x) -1) + 1

Only use in global scope; compute ceil(log2(x))

Parameters
xAn unsigned integral value
Returns
ceil(log2(x)) when 1 <= x <= max(type(x)), 0 when x < 1

Definition at line 595 of file util.h.

◆ IC_G_LSB_GET

#define IC_G_LSB_GET (   value)    ((value) & -(value))

Only use in global scope; extract the Least Significant Bit from value.

Definition at line 77 of file util.h.

◆ IC_G_MAX

#define IC_G_MAX (   a,
 
)    (((a) > (b)) ? (a) : (b))

Only use in global scope; obtain the maximum of two values.

Parameters
aFirst value.
bSecond value.
Returns
Maximum value of a and b.

Definition at line 477 of file util.h.

◆ IC_G_MIN

#define IC_G_MIN (   a,
 
)    (((a) < (b)) ? (a) : (b))

Only use in global scope; obtain the minimum of two values.

Parameters
aFirst value.
bSecond value.
Returns
Minimum value of a and b.

Definition at line 500 of file util.h.

◆ IC_G_NHPOT

#define IC_G_NHPOT (   x)    ((x) < 1 ? 1 : ((x) > (1ULL << 63) ? 0 : 1ULL << IC_G_LOG2CEIL(x)))

Only use in global scope; compute next highest power of two.

Equivalent to 2^ceil(log2(x))

Parameters
xAn unsigned integral value
Returns
2^ceil(log2(x)) or 0 if 2^ceil(log2(x)) would saturate 64-bits

Definition at line 615 of file util.h.

◆ IC_G_POW2_CEIL

#define IC_G_POW2_CEIL (   x)    ((x) <= 2UL ? (x) : (1UL << (8 * sizeof(long) - __builtin_clzl((x) -1))))

Only use in global scope; calculate power of two ceiling for some nonzero value.

Parameters
xNonzero unsigned long value
Returns
X rounded up to the next power of two

Definition at line 637 of file util.h.

◆ IC_G_ROUND

#define IC_G_ROUND (   x)    (__typeof__(x)) (int64_t) ((x) + ((x) >= 0 ? 0.5 : -0.5))

Only use in global scope; round of f32 (float), f64 (double), & f128 (long double).

Useful for casting to an integer: uint32_t const ROUNDED = (uint32_t) IC_G_ROUND(MY_FLOATING_POINT_NUMBER)

Note
Tie-breaks .5 (1/2) AWAY FROM ZERO for positive and negative numbers, e.g. 0.5 -> 1 and -0.5 -> -1.
Parameters
xfloating point number to round.
Returns
The rounded number

Definition at line 375 of file util.h.

◆ IC_G_WRITE_BIT

#define IC_G_WRITE_BIT (   var,
  bit,
  set 
)    (set) ? ((var) | IC_BIT(bit)) : ((var) & ~IC_BIT(bit))

Only use in global scope; set or clear a bit depending on a boolean value.

Parameters
varVariable to be altered
bitBit number
setif 0, clears bit in var; any other value sets bit
Returns
The value of var with the specified bit set or cleared

Definition at line 678 of file util.h.

◆ IC_GENMASK32

#define IC_GENMASK32 (   h,
 
)    (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (32UL - 1 - (h))))

Create a contiguous bitmask starting at bit position l and ending at position h.

Definition at line 67 of file util.h.

◆ IC_GENMASK64

#define IC_GENMASK64 (   h,
 
)    (((~0ULL) - (1ULL << (l)) + 1) & (~0ULL >> (64ULL - 1 - (h))))

Create a contiguous 64-bit bitmask starting at bit position l and ending at position h.

Definition at line 73 of file util.h.

◆ IC_ILOG2

#define IC_ILOG2 (   x)
Value:
__builtin_constant_p(x) ? IC_G_ILOG2(x) : ({ \
__auto_type const _ic_z_ilog2_x_ = (IC_Z_REQUIRE_UINT(x)); \
_ic_z_ilog2_x_ < 1 ? -1 : IC_G_ILOG2(_ic_z_ilog2_x_); \
})
#define IC_Z_REQUIRE_UINT(expr)
Compile-time require expr is an unsigned integer.
Definition: assert_type.h:79
#define IC_G_ILOG2(x)
Only use in global scope; compute integer value of log2(x)
Definition: util.h:572

Compute integer value log2(x)

Parameters
xAn unsigned integral value to compute logarithm of (positive only)
Returns
log2(x) or -1 if x < 1

Definition at line 582 of file util.h.

◆ IC_IN_RANGE

#define IC_IN_RANGE (   val,
  min,
  max 
)    _IC_MAKE_SINGLE_EVAL_3(IC_G_IN_RANGE, val, min, max)

Checks if a value is within range.

Parameters
valValue to be checked.
minLower bound (inclusive).
maxUpper bound (inclusive).
Return values
trueIf value is within range
falseIf the value is not within range
Note
Expressions are only evaluated once.

Definition at line 563 of file util.h.

◆ IC_INT_TO_POINTER

#define IC_INT_TO_POINTER (   x)    ((void *) (intptr_t) (x))

Cast x, a signed integer, to a void*.

Definition at line 61 of file util.h.

◆ IC_IROUND_DOWN

#define IC_IROUND_DOWN (   x,
  align 
)    _IC_MAKE_SINGLE_EVAL_2(IC_IROUND_DOWN, x, align)

Value of integer x rounded down to the next multiple of align.

Parameters
xinteger to round
Returns
the rounded integer

Definition at line 296 of file util.h.

◆ IC_IROUND_UP

#define IC_IROUND_UP (   x,
  align 
)    _IC_MAKE_SINGLE_EVAL_2(IC_G_IROUND_UP, x, align)

Value of integer x rounded up to the next multiple of align.

Parameters
xinteger to round
Returns
the rounded integer

Definition at line 280 of file util.h.

◆ IC_IS_ARRAY_ELEMENT

#define IC_IS_ARRAY_ELEMENT (   array,
  ptr 
)
Value:
((ptr) && IC_POINTER_TO_UINT(array) <= IC_POINTER_TO_UINT(ptr) \
&& (IC_POINTER_TO_UINT(ptr) - IC_POINTER_TO_UINT(array)) % sizeof((array)[0]) == 0)
#define IC_ARRAY_LENGTH(array)
Number of elements in the given array; the "length".
Definition: util.h:130

Whether ptr is an element of array.

This macro can be seen as a slightly stricter version of PART_OF_ARRAY in that it also ensures that ptr is aligned to an array-element boundary of array.

In C, passing a pointer as array causes a compile error.

Parameters
arraythe array in question
ptrthe pointer to check
Returns
1 if ptr is part of array, 0 otherwise

Definition at line 159 of file util.h.

◆ IC_IS_BIT_MASK

#define IC_IS_BIT_MASK (   m)    IC_IS_SHIFTED_BIT_MASK(m, 0)

Check if bits are set continuously from the LSB.

Parameters
mCheck whether the bits are set continuously from LSB.

Definition at line 735 of file util.h.

◆ IC_IS_POW2

#define IC_IS_POW2 (   x)    _IC_MAKE_SINGLE_EVAL_1(IC_G_IS_POW2)

Check whether or not a value is a power of 2.

Parameters
xThe value to check
Returns
true if x is a power of 2, false otherwise

Definition at line 661 of file util.h.

◆ IC_IS_SHIFTED_BIT_MASK

#define IC_IS_SHIFTED_BIT_MASK (   m,
 
)    _IC_MAKE_SINGLE_EVAL_2(IC_G_IS_SHIFTED_BIT_MASK, m, s)

Check if bits are set continuously from the specified bit.

Note
The macro is not dependent on the bit-width.
Parameters
mCheck whether the bits are set continuously or not.
sSpecify the lowest bit for that is continuously set bits.

Definition at line 721 of file util.h.

◆ IC_LOG2CEIL

#define IC_LOG2CEIL (   x)    _IC_MAKE_SINGLE_EVAL_1(IC_G_LOG2CEIL, x)

Compute ceil(log2(x))

Parameters
xAn unsigned integral value
Returns
ceil(log2(x)) when 1 <= x <= max(type(x)), 0 when x < 1

Definition at line 604 of file util.h.

◆ IC_LSB_GET

#define IC_LSB_GET (   value)    _IC_MAKE_SINGLE_EVAL_1(value)

Extract the Least Significant Bit from value.

Definition at line 80 of file util.h.

◆ IC_MAX

#define IC_MAX (   a,
 
)    _IC_MAKE_SINGLE_EVAL_2(IC_G_MAX, a, b)

Obtain the maximum of two values.

Parameters
aFirst value.
bSecond value.
Returns
Maximum value of a and b.
Note
Expressions are only evaluated once.

Definition at line 489 of file util.h.

◆ IC_MIN

#define IC_MIN (   a,
 
)    _IC_MAKE_SINGLE_EVAL_2(IC_G_MIN, a, b)

Obtain the minimum of two values.

Parameters
aFirst value.
bSecond value.
Returns
Minimum value of a and b.
Note
Expressions are only evaluated once.

Definition at line 512 of file util.h.

◆ IC_NHPOT

#define IC_NHPOT (   x)    _IC_MAKE_SINGLE_EVAL_1(IC_G_NHPOT, x)

Compute next highest power of two.

Equivalent to 2^ceil(log2(x))

Note
This macro expands its argument multiple times (to permit use in constant expressions), which must not have side effects.
Parameters
xAn unsigned integral value
Returns
2^ceil(log2(x)) or 0 if 2^ceil(log2(x)) would saturate 64-bits

Definition at line 629 of file util.h.

◆ IC_NUM_BITS

#define IC_NUM_BITS (   t)    (sizeof(t) * 8)

Number of bits that make up a type.

Definition at line 52 of file util.h.

◆ IC_PART_OF_ARRAY

#define IC_PART_OF_ARRAY (   array,
  ptr 
)
Value:
((ptr) && IC_POINTER_TO_UINT(array) <= IC_POINTER_TO_UINT(ptr) \

Check if a pointer ptr lies within array.

In C but not C++, this causes a compile error if array is not an array (e.g. if ptr and array are mixed up).

Parameters
arrayan array
ptra pointer
Returns
1 if ptr is part of array, 0 otherwise

Definition at line 191 of file util.h.

◆ IC_POINTER_TO_INT

#define IC_POINTER_TO_INT (   x)    ((intptr_t) (x))

Cast x, a pointer, to a signed integer.

Definition at line 59 of file util.h.

◆ IC_POINTER_TO_UINT

#define IC_POINTER_TO_UINT (   x)    ((uintptr_t) (x))

Cast x, a pointer, to an unsigned integer.

Definition at line 55 of file util.h.

◆ IC_POW2_CEIL

#define IC_POW2_CEIL (   x)    _IC_MAKE_SINGLE_EVAL_1(IC_G_POW2_CEIL, x)

Calculate power of two ceiling for some nonzero value.

Parameters
xNonzero unsigned long value
Returns
X rounded up to the next power of two

Definition at line 645 of file util.h.

◆ IC_ROUND

#define IC_ROUND (   x)
Value:
({ \
IC_BUILD_ASSERT( \
IC_SAME_TYPE(x, float) || IC_SAME_TYPE(x, double) || IC_SAME_TYPE(x, long double), \
"Argument must be a floating point number!" \
); /* unclear that ARM's <tgmath.h> implements round() */ \
IC_BUILD_ASSERT((x) >= INT64_MIN || (x) <= INT64_MAX); \
IC_REQUIRE_CONSTANT(IC_G_ROUND(x)); \
})
#define IC_G_ROUND(x)
Only use in global scope; round of f32 (float), f64 (double), & f128 (long double).
Definition: util.h:375

Round of f32 (float), f64 (double), & f128 (long double).

Useful for casting to an integer: #define ROUNDED (uint32_t) IC_ROUND(MY_FLOATING_POINT_NUMBER)

Note
Generates compile error if x is not a compile-time constant; use round*(x) from <math.h> or <tgmath.h>
Tie-breaks .5 (1/2) AWAY FROM ZERO for positive and negative numbers, e.g. 0.5 -> 1 and -0.5 -> -1.
Parameters
xfloating point number to round.
Returns
The rounded number

Definition at line 388 of file util.h.

◆ IC_SAME_TYPE

#define IC_SAME_TYPE (   a,
 
)    __builtin_types_compatible_p(__typeof__(a), __typeof__(b))

Validate if two entities have a compatible type.

Parameters
athe first entity to be compared
bthe second entity to be compared
Returns
1 if the two elements are compatible, 0 if they are not

Definition at line 223 of file util.h.

◆ IC_UINT_TO_POINTER

#define IC_UINT_TO_POINTER (   x)    ((void *) (uintptr_t) (x))

Cast x, an unsigned integer, to a void*.

Definition at line 57 of file util.h.

◆ IC_WB_DN

#define IC_WB_DN (   x)    IC_IROUND_DOWN(x, sizeof(void *))

Value of x rounded down to the previous word boundary.

Definition at line 302 of file util.h.

◆ IC_WB_UP

#define IC_WB_UP (   x)    IC_IROUND_UP(x, sizeof(void *))

Value of x rounded up to the next word boundary.

Definition at line 299 of file util.h.

◆ IC_WRITE_BIT

#define IC_WRITE_BIT (   var,
  bit,
  set 
)    _IC_MAKE_SINGLE_EVAL_3(IC_G_WRITE_BIT, var, bit, set)

Set or clear a bit depending on a boolean value.

Parameters
varVariable to be altered
bitBit number
setif 0, clears bit in var; any other value sets bit
Returns
The value of var with the specified bit set or cleared

Definition at line 689 of file util.h.

◆ IC_ZERO_OR_COMPILE_ERROR

#define IC_ZERO_OR_COMPILE_ERROR (   cond)    ((int) sizeof(char[1 - 2 * !(cond)]) - 1)

0 if cond is true-ish; causes a compile error otherwise.

Definition at line 109 of file util.h.