Utilities
This page contains reference documentation for <sys/util.h>, which provides
miscellaneous utility functions and macros.
- group sys-util
- Since
- 2.4 
- Version
- 0.1.0 
 - Defines - 
POINTER_TO_UINT(x)
- Cast - x, a pointer, to an unsigned integer.
 - 
UINT_TO_POINTER(x)
- Cast - x, an unsigned integer, to a- void*.
 - 
POINTER_TO_INT(x)
- Cast - x, a pointer, to a signed integer.
 - 
INT_TO_POINTER(x)
- Cast - x, a signed integer, to a- void*.
 - 
BITS_PER_LONG
- Number of bits in a long int. 
 - 
BITS_PER_LONG_LONG
- Number of bits in a long long int. 
 - 
GENMASK(h, l)
- Create a contiguous bitmask starting at bit position - land ending at position- h.
 - 
GENMASK64(h, l)
- Create a contiguous 64-bit bitmask starting at bit position - land ending at position- h.
 - 
LSB_GET(value)
- Extract the Least Significant Bit from - value.
 - 
FIELD_GET(mask, value)
- Extract a bitfield element from - valuecorresponding to the field mask- mask.
 - 
FIELD_PREP(mask, value)
- Prepare a bitfield element using - valuewith- maskrepresenting its field position and width.- The result should be combined with other fields using a logical OR. 
 - 
ZERO_OR_COMPILE_ERROR(cond)
- 0 if - condis true-ish; causes a compile error otherwise.
 - 
IS_ARRAY(array)
- Zero if - arrayhas an array type, a compile error otherwise.- This macro is available only from C, not C++. 
 - 
ARRAY_SIZE(array)
- Number of elements in the given - array.- In C++, due to language limitations, this will accept as - arrayany type that implements- operator[]. The results may not be particularly meaningful in this case.- In C, passing a pointer as - arraycauses a compile error.
 - 
IS_ARRAY_ELEMENT(array, ptr)
- Whether - ptris an element of- array.- This macro can be seen as a slightly stricter version of PART_OF_ARRAY in that it also ensures that - ptris aligned to an array-element boundary of- array.- In C, passing a pointer as - arraycauses a compile error.- Parameters:
- array – the array in question 
- ptr – the pointer to check 
 
- Returns:
- 1 if - ptris part of- array, 0 otherwise
 
 - 
ARRAY_INDEX(array, ptr)
- Index of - ptrwithin- array.- With - CONFIG_ASSERT=y, this macro will trigger a runtime assertion when- ptrdoes not fall into the range of- arrayor when- ptris not aligned to an array-element boundary of- array.- In C, passing a pointer as - arraycauses a compile error.- Parameters:
- array – the array in question 
- ptr – pointer to an element of - array
 
- Returns:
- the array index of - ptrwithin- array, on success
 
 - 
PART_OF_ARRAY(array, ptr)
- Check if a pointer - ptrlies within- array.- In C but not C++, this causes a compile error if - arrayis not an array (e.g. if- ptrand- arrayare mixed up).- Parameters:
- array – an array 
- ptr – a pointer 
 
- Returns:
- 1 if - ptris part of- array, 0 otherwise
 
 - 
ARRAY_INDEX_FLOOR(array, ptr)
- Array-index of - ptrwithin- array, rounded down.- This macro behaves much like ARRAY_INDEX with the notable difference that it accepts any - ptrin the range of- arrayrather than exclusively a- ptraligned to an array-element boundary of- array.- With - CONFIG_ASSERT=y, this macro will trigger a runtime assertion when- ptrdoes not fall into the range of- array.- In C, passing a pointer as - arraycauses a compile error.- Parameters:
- array – the array in question 
- ptr – pointer to an element of - array
 
- Returns:
- the array index of - ptrwithin- array, on success
 
 - 
ARRAY_FOR_EACH(array, idx)
- Iterate over members of an array using an index variable. - Parameters:
- array – the array in question 
- idx – name of array index variable 
 
 
 - 
ARRAY_FOR_EACH_PTR(array, ptr)
- Iterate over members of an array using a pointer. - Parameters:
- array – the array in question 
- ptr – pointer to an element of - array
 
 
 - 
SAME_TYPE(a, b)
- Validate if two entities have a compatible type. - Parameters:
- a – the first entity to be compared 
- b – the second entity to be compared 
 
- Returns:
- 1 if the two elements are compatible, 0 if they are not 
 
 - 
CONTAINER_OF_VALIDATE(ptr, type, field)
- Validate CONTAINER_OF parameters, only applies to C mode. 
 - 
CONTAINER_OF(ptr, type, field)
- Get a pointer to a structure containing the element. - Example: Above,- struct foo { int bar; }; struct foo my_foo; int *ptr = &my_foo.bar; struct foo *container = CONTAINER_OF(ptr, struct foo, bar);- containerpoints at- my_foo.- Parameters:
- ptr – pointer to a structure element 
- type – name of the type that - ptris an element of
- field – the name of the field within the struct - ptrpoints to
 
- Returns:
- a pointer to the structure that contains - ptr
 
 - 
SIZEOF_FIELD(type, member)
- Report the size of a struct field in bytes. - Parameters:
- type – The structure containing the field of interest. 
- member – The field to return the size of. 
 
- Returns:
- The field size. 
 
 - 
CONCAT(...)
- Concatenate input arguments. - Concatenate provided tokens into a combined token during the preprocessor pass. This can be used to, for ex., build an identifier out of multiple parts, where one of those parts may be, for ex, a number, another macro, or a macro argument. - Parameters:
- ... – Tokens to concatencate 
 
- Returns:
- Concatenated token. 
 
 - 
IS_ALIGNED(ptr, align)
- Check if - ptris aligned to- alignalignment.
 - 
ROUND_UP(x, align)
- Value of - xrounded up to the next multiple of- align.
 - 
ROUND_DOWN(x, align)
- Value of - xrounded down to the previous multiple of- align.
 - 
WB_UP(x)
- Value of - xrounded up to the next word boundary.
 - 
WB_DN(x)
- Value of - xrounded down to the previous word boundary.
 - 
DIV_ROUND_UP(n, d)
- Divide and round up. - Example: - DIV_ROUND_UP(1, 2); // 1 DIV_ROUND_UP(3, 2); // 2 - Parameters:
- n – Numerator. 
- d – Denominator. 
 
- Returns:
- The result of - n/- d, rounded up.
 
 - 
DIV_ROUND_CLOSEST(n, d)
- Divide and round to the nearest integer. - Example: - DIV_ROUND_CLOSEST(5, 2); // 3 DIV_ROUND_CLOSEST(5, -2); // -3 DIV_ROUND_CLOSEST(5, 3); // 2 - Parameters:
- n – Numerator. 
- d – Denominator. 
 
- Returns:
- The result of - n/- d, rounded to the nearest integer.
 
 - 
ceiling_fraction(numerator, divider)
- Ceiling function applied to - numerator/- divideras a fraction.- Deprecated:
- Use DIV_ROUND_UP() instead. 
 
 - 
MAX(a, b)
- Obtain the maximum of two values. - Note - Arguments are evaluated twice. Use Z_MAX for a GCC-only, single evaluation version - Parameters:
- a – First value. 
- b – Second value. 
 
- Returns:
- Maximum value of - aand- b.
 
 - 
MIN(a, b)
- Obtain the minimum of two values. - Note - Arguments are evaluated twice. Use Z_MIN for a GCC-only, single evaluation version - Parameters:
- a – First value. 
- b – Second value. 
 
- Returns:
- Minimum value of - aand- b.
 
 - 
CLAMP(val, low, high)
- Clamp a value to a given range. - Note - Arguments are evaluated multiple times. Use Z_CLAMP for a GCC-only, single evaluation version. - Parameters:
- val – Value to be clamped. 
- low – Lowest allowed value (inclusive). 
- high – Highest allowed value (inclusive). 
 
- Returns:
- Clamped value. 
 
 - 
IN_RANGE(val, min, max)
- Checks if a value is within range. - Note - valis evaluated twice.- Parameters:
- val – Value to be checked. 
- min – Lower bound (inclusive). 
- max – Upper bound (inclusive). 
 
- Return values:
- true – If value is within range 
- false – If the value is not within range 
 
 
 - 
LOG2(x)
- Compute log2(x) - Note - This macro expands its argument multiple times (to permit use in constant expressions), which must not have side effects. - Parameters:
- x – An unsigned integral value to compute logarithm of (positive only) 
 
- Returns:
- log2(x) when 1 <= x <= max(x), -1 when x < 1 
 
 - 
LOG2CEIL(x)
- Compute ceil(log2(x)) - Note - This macro expands its argument multiple times (to permit use in constant expressions), which must not have side effects. - Parameters:
- x – An unsigned integral value 
 
- Returns:
- ceil(log2(x)) when 1 <= x <= max(type(x)), 0 when x < 1 
 
 - 
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:
- x – An unsigned integral value 
 
- Returns:
- 2^ceil(log2(x)) or 0 if 2^ceil(log2(x)) would saturate 64-bits 
 
 - 
KB(x)
- Number of bytes in - xkibibytes.
 - 
MB(x)
- Number of bytes in - xmebibytes.
 - 
GB(x)
- Number of bytes in - xgibibytes.
 - 
KHZ(x)
- Number of Hz in - xkHz.
 - 
MHZ(x)
- Number of Hz in - xMHz.
 - 
WAIT_FOR(expr, timeout, delay_stmt)
- Wait for an expression to return true with a timeout. - Spin on an expression with a timeout and optional delay between iterations - Commonly needed when waiting on hardware to complete an asynchronous request to read/write/initialize/reset, but useful for any expression. - Parameters:
- expr – Truth expression upon which to poll, e.g.: XYZREG & XYZREG_EN 
- timeout – Timeout to wait for in microseconds, e.g.: 1000 (1ms) 
- delay_stmt – Delay statement to perform each poll iteration e.g.: NULL, k_yield(), k_msleep(1) or k_busy_wait(1) 
 
- Return values:
- expr – As a boolean return, if false then it has timed out. 
 
 - 
BIT(n)
- Unsigned integer with bit position - nset (signed in assembly language).
 - 
BIT64(_n)
- 64-bit unsigned integer with bit position - _nset.
 - 
WRITE_BIT(var, bit, set)
- Set or clear a bit depending on a boolean value. - The argument - varis a variable whose value is written to as a side effect.- Parameters:
- var – Variable to be altered 
- bit – Bit number 
- set – if 0, clears - bitin- var; any other value sets- bit
 
 
 - 
BIT_MASK(n)
- Bit mask with bits 0 through - n-1(inclusive) set, or 0 if- nis 0.
 - 
BIT64_MASK(n)
- 64-bit bit mask with bits 0 through - n-1(inclusive) set, or 0 if- nis 0.
 - 
IS_POWER_OF_TWO(x)
- Check if a - xis a power of two.
 - 
IS_SHIFTED_BIT_MASK(m, s)
- Check if bits are set continuously from the specified bit. - The macro is not dependent on the bit-width. - Parameters:
- m – Check whether the bits are set continuously or not. 
- s – Specify the lowest bit for that is continuously set bits. 
 
 
 - 
IS_BIT_MASK(m)
- Check if bits are set continuously from the LSB. - Parameters:
- m – Check whether the bits are set continuously from LSB. 
 
 
 - 
IS_ENABLED(config_macro)
- Check for macro definition in compiler-visible expressions. - This trick was pioneered in Linux as the config_enabled() macro. It has the effect of taking a macro value that may be defined to “1” or may not be defined at all and turning it into a literal expression that can be handled by the C compiler instead of just the preprocessor. It is often used with a - CONFIG_FOOmacro which may be defined to 1 via Kconfig, or left undefined.- That is, it works similarly to - #if defined(CONFIG_FOO)except that its expansion is a C expression. Thus, much- #ifdefusage can be replaced with equivalents like:This is cleaner since the compiler can generate errors and warnings for- if (IS_ENABLED(CONFIG_FOO)) { do_something_with_foo }- do_something_with_fooeven when- CONFIG_FOOis undefined.- Note: Use of IS_ENABLED in a - #ifstatement is discouraged as it doesn’t provide any benefit vs plain- #if defined()- Parameters:
- config_macro – Macro to check 
 
- Returns:
- 1 if - config_macrois defined to 1, 0 otherwise (including if- config_macrois not defined)
 
 - 
COND_CODE_1(_flag, _if_1_code, _else_code)
- Insert code depending on whether - _flagexpands to 1 or not.- This relies on similar tricks as IS_ENABLED(), but as the result of - _flagexpansion, results in either- _if_1_codeor- _else_codeis expanded.- To prevent the preprocessor from treating commas as argument separators, the - _if_1_codeand- _else_codeexpressions must be inside brackets/parentheses:- (). These are stripped away during macro expansion.- Example: If- COND_CODE_1(CONFIG_FLAG, (uint32_t x;), (there_is_no_flag();)) - CONFIG_FLAGis defined to 1, this expands to:It expands to- uint32_t x; - there_is_no_flag();otherwise.- This could be used as an alternative to: However, the advantage of COND_CODE_1() is that code is resolved in place where it is used, while the- #if defined(CONFIG_FLAG) && (CONFIG_FLAG == 1) #define MAYBE_DECLARE(x) uint32_t x #else #define MAYBE_DECLARE(x) there_is_no_flag() #endif MAYBE_DECLARE(x); - #ifmethod defines- MAYBE_DECLAREon two lines and requires it to be invoked again on a separate line. This makes COND_CODE_1() more concise and also sometimes more useful when used within another macro’s expansion.- Note - _flagcan be the result of preprocessor expansion, e.g. an expression involving- NUM_VA_ARGS_LESS_1(...). However,- _if_1_codeis only expanded if- _flagexpands to the integer literal 1. Integer expressions that evaluate to 1, e.g. after doing some arithmetic, will not work.- Parameters:
- _flag – evaluated flag 
- _if_1_code – result if - _flagexpands to 1; must be in parentheses
- _else_code – result otherwise; must be in parentheses 
 
 
 - 
COND_CODE_0(_flag, _if_0_code, _else_code)
- Like COND_CODE_1() except tests if - _flagis 0.- This is like COND_CODE_1(), except that it tests whether - _flagexpands to the integer literal 0. It expands to- _if_0_codeif so, and- _else_codeotherwise; both of these must be enclosed in parentheses.- See also - Parameters:
- _flag – evaluated flag 
- _if_0_code – result if - _flagexpands to 0; must be in parentheses
- _else_code – result otherwise; must be in parentheses 
 
 
 - 
IF_ENABLED(_flag, _code)
- Insert code if - _flagis defined and equals 1.- Like COND_CODE_1(), this expands to - _codeif- _flagis defined to 1; it expands to nothing otherwise.- Example: If- IF_ENABLED(CONFIG_FLAG, (uint32_t foo;)) - CONFIG_FLAGis defined to 1, this expands to:and to nothing otherwise.- uint32_t foo; - It can be considered as a more compact alternative to: - #if defined(CONFIG_FLAG) && (CONFIG_FLAG == 1) uint32_t foo; #endif - Parameters:
- _flag – evaluated flag 
- _code – result if - _flagexpands to 1; must be in parentheses
 
 
 - 
IF_DISABLED(_flag, _code)
- Insert code if - _flagis not defined as 1.- This expands to nothing if - _flagis defined and equal to 1; it expands to- _codeotherwise.- Example: If- IF_DISABLED(CONFIG_FLAG, (uint32_t foo;)) - CONFIG_FLAGisn’t defined or different than 1, this expands to:and to nothing otherwise.- uint32_t foo; - IF_DISABLED does the opposite of IF_ENABLED. - Parameters:
- _flag – evaluated flag 
- _code – result if - _flagdoes not expand to 1; must be in parentheses
 
 
 - 
IS_EMPTY(...)
- Check if a macro has a replacement expression. - If - ais a macro defined to a nonempty value, this will return true, otherwise it will return false. It only works with defined macros, so an additional- #ifdeftest may be needed in some cases.- This macro may be used with COND_CODE_1() and COND_CODE_0() while processing - __VA_ARGS__to avoid processing empty arguments.- Example: In above examples, the invocations of IS_EMPTY(…) return- #define EMPTY #define NON_EMPTY 1 #undef UNDEFINED IS_EMPTY(EMPTY) IS_EMPTY(NON_EMPTY) IS_EMPTY(UNDEFINED) #if defined(EMPTY) && IS_EMPTY(EMPTY) == true some_conditional_code #endif - true,- false, and- true;- some_conditional_codeis included.- Parameters:
- ... – macro to check for emptiness (may be - __VA_ARGS__)
 
 
 - 
IS_EQ(a, b)
- Like - a == b, but does evaluation and short-circuiting at C preprocessor time.- This however only works for integer literal from 0 to 4095. 
 - 
LIST_DROP_EMPTY(...)
- Remove empty arguments from list. - During macro expansion, - __VA_ARGS__and other preprocessor generated lists may contain empty elements, e.g.:Using EMPTY to show each empty element, LIST contains:- #define LIST ,a,b,,d, When processing such lists, e.g. using FOR_EACH(), all empty elements will be processed, and may require filtering out. To make that process easier, it is enough to invoke LIST_DROP_EMPTY which will remove all empty elements.- EMPTY, a, b, EMPTY, d - Example: expands to:- LIST_DROP_EMPTY(LIST) - a, b, d - Parameters:
- ... – list to be processed 
 
 
 - 
EMPTY
- Macro with an empty expansion. - This trivial definition is provided for readability when a macro should expand to an empty result, which e.g. is sometimes needed to silence checkpatch. - Example: The above would cause checkpatch to complain, but:- #define LIST_ITEM(n) , item##n would not.- #define LIST_ITEM(n) EMPTY, item##n 
 - 
IDENTITY(V)
- Macro that expands to its argument. - This is useful in macros like - FOR_EACH()when there is no transformation required on the list elements.- Parameters:
- V – any value 
 
 
 - 
GET_ARG_N(N, ...)
- Get nth argument from argument list. - Parameters:
- N – Argument index to fetch. Counter from 1. 
- ... – Variable list of arguments from which one argument is returned. 
 
- Returns:
- Nth argument. 
 
 - 
GET_ARGS_LESS_N(N, ...)
- Strips n first arguments from the argument list. - Parameters:
- N – Number of arguments to discard. 
- ... – Variable list of arguments. 
 
- Returns:
- argument list without N first arguments. 
 
 - 
UTIL_OR(a, b)
- Like - a || b, but does evaluation and short-circuiting at C preprocessor time.- This is not the same as the binary - ||operator; in particular,- ashould expand to an integer literal 0 or 1. However,- bcan be any value.- This can be useful when - bis an expression that would cause a build error when- ais 1.
 - 
UTIL_AND(a, b)
- Like - a && b, but does evaluation and short-circuiting at C preprocessor time.- This is not the same as the binary - &&, however; in particular,- ashould expand to an integer literal 0 or 1. However,- bcan be any value.- This can be useful when - bis an expression that would cause a build error when- ais 0.
 - 
UTIL_INC(x)
- UTIL_INC(x) for an integer literal x from 0 to 4095 expands to an integer literal whose value is x+1. - See also 
 - 
UTIL_DEC(x)
- UTIL_DEC(x) for an integer literal x from 0 to 4095 expands to an integer literal whose value is x-1. - See also 
 - 
UTIL_X2(y)
- UTIL_X2(y) for an integer literal y from 0 to 4095 expands to an integer literal whose value is 2y. 
 - 
LISTIFY(LEN, F, sep, ...)
- Generates a sequence of code with configurable separator. - Example: The above two lines expand to:- #define FOO(i, _) MY_PWM ## i { LISTIFY(PWM_COUNT, FOO, (,)) }- { MY_PWM0 , MY_PWM1 } - Note - Calling LISTIFY with undefined arguments has undefined behavior. - Parameters:
- LEN – The length of the sequence. Must be an integer literal less than 4095. 
- F – A macro function that accepts at least two arguments: - F(i, ...).- Fis called repeatedly in the expansion. Its first argument- iis the index in the sequence, and the variable list of arguments passed to LISTIFY are passed through to- F.
- sep – Separator (e.g. comma or semicolon). Must be in parentheses; this is required to enable providing a comma as separator. 
 
 
 - 
FOR_EACH(F, sep, ...)
- Call a macro - Fon each provided argument with a given separator between each call.- Example: This expands to:- #define F(x) int a##x FOR_EACH(F, (;), 4, 5, 6); - int a4; int a5; int a6; - Parameters:
- F – Macro to invoke 
- sep – Separator (e.g. comma or semicolon). Must be in parentheses; this is required to enable providing a comma as separator. 
- ... – Variable argument list. The macro - Fis invoked as- F(element)for each element in the list.
 
 
 - 
FOR_EACH_NONEMPTY_TERM(F, term, ...)
- Like FOR_EACH(), but with a terminator instead of a separator, and drops empty elements from the argument list. - The - separgument to- FOR_EACH(F, (sep), a, b)is a separator which is placed between calls to- F, like this:By contrast, the- FOR_EACH(F, (sep), a, b) // F(a) sep F(b) // ^^^ no sep here!- termargument to- FOR_EACH_NONEMPTY_TERM(F, (term),a, b)is added after each time- Fappears in the expansion:Further, any empty elements are dropped:- FOR_EACH_NONEMPTY_TERM(F, (term), a, b) // F(a) term F(b) term // ^^^^This is more convenient in some cases, because FOR_EACH_NONEMPTY_TERM() expands to nothing when given an empty argument list, and it’s often cumbersome to write a macro- FOR_EACH_NONEMPTY_TERM(F, (term), a, EMPTY, b) // F(a) term F(b) term - Fthat does the right thing even when given an empty argument.- One example is when - __VA_ARGS__may or may not be empty, and the results are embedded in a larger initializer:This is more convenient than:- #define SQUARE(x) ((x)*(x)) int my_array[] = { FOR_EACH_NONEMPTY_TERM(SQUARE, (,), FOO(...)) FOR_EACH_NONEMPTY_TERM(SQUARE, (,), BAR(...)) FOR_EACH_NONEMPTY_TERM(SQUARE, (,), BAZ(...)) };- figuring out whether the - FOO,- BAR, and- BAZexpansions are empty and adding a comma manually (or not) between FOR_EACH() calls
- rewriting SQUARE so it reacts appropriately when “x” is empty (which would be necessary if e.g. - FOOexpands to nothing)
 - Parameters:
- F – Macro to invoke on each nonempty element of the variable arguments 
- term – Terminator (e.g. comma or semicolon) placed after each invocation of F. Must be in parentheses; this is required to enable providing a comma as separator. 
- ... – Variable argument list. The macro - Fis invoked as- F(element)for each nonempty element in the list.
 
 
 - 
FOR_EACH_IDX(F, sep, ...)
- Call macro - Fon each provided argument, with the argument’s index as an additional parameter.- This is like FOR_EACH(), except - Fshould be a macro which takes two arguments:- F(index, variable_arg).- Example: This expands to:- #define F(idx, x) int a##idx = x FOR_EACH_IDX(F, (;), 4, 5, 6); - int a0 = 4; int a1 = 5; int a2 = 6; - Parameters:
- F – Macro to invoke 
- sep – Separator (e.g. comma or semicolon). Must be in parentheses; this is required to enable providing a comma as separator. 
- ... – Variable argument list. The macro - Fis invoked as- F(index, element)for each element in the list.
 
 
 - 
FOR_EACH_FIXED_ARG(F, sep, fixed_arg, ...)
- Call macro - Fon each provided argument, with an additional fixed argument as a parameter.- This is like FOR_EACH(), except - Fshould be a macro which takes two arguments:- F(variable_arg, fixed_arg).- Example: This expands to:- static void func(int val, void *dev); FOR_EACH_FIXED_ARG(func, (;), dev, 4, 5, 6); - func(4, dev); func(5, dev); func(6, dev); - Parameters:
- F – Macro to invoke 
- sep – Separator (e.g. comma or semicolon). Must be in parentheses; this is required to enable providing a comma as separator. 
- fixed_arg – Fixed argument passed to - Fas the second macro parameter.
- ... – Variable argument list. The macro - Fis invoked as- F(element, fixed_arg)for each element in the list.
 
 
 - 
FOR_EACH_IDX_FIXED_ARG(F, sep, fixed_arg, ...)
- Calls macro - Ffor each variable argument with an index and fixed argument.- This is like the combination of FOR_EACH_IDX() with FOR_EACH_FIXED_ARG(). - Example: This expands to:- #define F(idx, x, fixed_arg) int fixed_arg##idx = x FOR_EACH_IDX_FIXED_ARG(F, (;), a, 4, 5, 6); - int a0 = 4; int a1 = 5; int a2 = 6; - Parameters:
- F – Macro to invoke 
- sep – Separator (e.g. comma or semicolon). Must be in parentheses; This is required to enable providing a comma as separator. 
- fixed_arg – Fixed argument passed to - Fas the third macro parameter.
- ... – Variable list of arguments. The macro - Fis invoked as- F(index, element, fixed_arg)for each element in the list.
 
 
 - 
REVERSE_ARGS(...)
- Reverse arguments order. - Parameters:
- ... – Variable argument list. 
 
 
 - 
NUM_VA_ARGS_LESS_1(...)
- Number of arguments in the variable arguments list minus one. - Note - Supports up to 64 arguments. - Parameters:
- ... – List of arguments 
 
- Returns:
- Number of variadic arguments in the argument list, minus one 
 
 - 
NUM_VA_ARGS(...)
- Number of arguments in the variable arguments list. - Note - Supports up to 63 arguments. - Parameters:
- ... – List of arguments 
 
- Returns:
- Number of variadic arguments in the argument list 
 
 - 
MACRO_MAP_CAT(...)
- Mapping macro that pastes results together. - This is similar to FOR_EACH() in that it invokes a macro repeatedly on each element of - __VA_ARGS__. However, unlike FOR_EACH(), MACRO_MAP_CAT() pastes the results together into a single token.- For example, with this macro FOO: - #define FOO(x) item_##x##_ - MACRO_MAP_CAT(FOO, a, b, c),expands to the token:- item_a_item_b_item_c_ - Parameters:
- ... – Macro to expand on each argument, followed by its arguments. (The macro should take exactly one argument.) 
 
- Returns:
- The results of expanding the macro on each argument, all pasted together 
 
 - 
MACRO_MAP_CAT_N(N, ...)
- Mapping macro that pastes a fixed number of results together. - Similar to MACRO_MAP_CAT(), but expects a fixed number of arguments. If more arguments are given than are expected, the rest are ignored. - Parameters:
- N – Number of arguments to map 
- ... – Macro to expand on each argument, followed by its arguments. (The macro should take exactly one argument.) 
 
- Returns:
- The results of expanding the macro on each argument, all pasted together 
 
 - Functions - 
static inline bool is_power_of_two(unsigned int x)
- Is - xa power of two?- Parameters:
- x – value to check 
 
- Returns:
- true if - xis a power of two, false otherwise
 
 - 
ALWAYS_INLINE static bool is_null_no_warn(void *p)
- Is - pequal to- NULL?- Some macros may need to check their arguments against NULL to support multiple use-cases, but NULL checks can generate warnings if such a macro is used in contexts where that particular argument can never be NULL. - The warnings can be triggered if: a) all macros are expanded (e.g. when using CONFIG_COMPILER_SAVE_TEMPS=y) or b) tracking of macro expansions are turned off (-ftrack-macro-expansion=0) - The warnings can be circumvented by using this inline function for doing the NULL check within the macro. The compiler is still able to optimize the NULL check out at a later stage. - Parameters:
- p – Pointer to check 
 
- Returns:
- true if - pis equal to- NULL, false otherwise
 
 - 
static inline int64_t arithmetic_shift_right(int64_t value, uint8_t shift)
- Arithmetic shift right. - Parameters:
- value – value to shift 
- shift – number of bits to shift 
 
- Returns:
- valueshifted right by- shift; opened bit positions are filled with the sign bit
 
 - 
static inline void bytecpy(void *dst, const void *src, size_t size)
- byte by byte memcpy. - Copy - sizebytes of- srcinto- dest. This is guaranteed to be done byte by byte.- Parameters:
- dst – Pointer to the destination memory. 
- src – Pointer to the source of the data. 
- size – The number of bytes to copy. 
 
 
 - 
static inline void byteswp(void *a, void *b, size_t size)
- byte by byte swap. - Swap size bytes between memory regions a and b. This is guaranteed to be done byte by byte. - Parameters:
- a – Pointer to the first memory region. 
- b – Pointer to the second memory region. 
- size – The number of bytes to swap. 
 
 
 - 
int char2hex(char c, uint8_t *x)
- Convert a single character into a hexadecimal nibble. - Parameters:
- c – The character to convert 
- x – The address of storage for the converted number. 
 
- Returns:
- Zero on success or (negative) error code otherwise. 
 
 - 
int hex2char(uint8_t x, char *c)
- Convert a single hexadecimal nibble into a character. - Parameters:
- c – The number to convert 
- x – The address of storage for the converted character. 
 
- Returns:
- Zero on success or (negative) error code otherwise. 
 
 - 
size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen)
- Convert a binary array into string representation. - Parameters:
- buf – The binary array to convert 
- buflen – The length of the binary array to convert 
- hex – Address of where to store the string representation. 
- hexlen – Size of the storage area for string representation. 
 
- Returns:
- The length of the converted string, or 0 if an error occurred. 
 
 - 
size_t hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen)
- Convert a hexadecimal string into a binary array. - Parameters:
- hex – The hexadecimal string to convert 
- hexlen – The length of the hexadecimal string to convert. 
- buf – Address of where to store the binary data 
- buflen – Size of the storage area for binary data 
 
- Returns:
- The length of the binary array, or 0 if an error occurred. 
 
 - 
static inline uint8_t bcd2bin(uint8_t bcd)
- Convert a binary coded decimal (BCD 8421) value to binary. - Parameters:
- bcd – BCD 8421 value to convert. 
 
- Returns:
- Binary representation of input value. 
 
 - 
static inline uint8_t bin2bcd(uint8_t bin)
- Convert a binary value to binary coded decimal (BCD 8421). - Parameters:
- bin – Binary value to convert. 
 
- Returns:
- BCD 8421 representation of input value. 
 
 - 
uint8_t u8_to_dec(char *buf, uint8_t buflen, uint8_t value)
- Convert a uint8_t into a decimal string representation. - Convert a uint8_t value into its ASCII decimal string representation. The string is terminated if there is enough space in buf. - Parameters:
- buf – Address of where to store the string representation. 
- buflen – Size of the storage area for string representation. 
- value – The value to convert to decimal string 
 
- Returns:
- The length of the converted string (excluding terminator if any), or 0 if an error occurred. 
 
 - 
static inline int32_t sign_extend(uint32_t value, uint8_t index)
- Sign extend an 8, 16 or 32 bit value using the index bit as sign bit. - Parameters:
- value – The value to sign expand. 
- index – 0 based bit index to sign bit (0 to 31) 
 
 
 - 
static inline int64_t sign_extend_64(uint64_t value, uint8_t index)
- Sign extend a 64 bit value using the index bit as sign bit. - Parameters:
- value – The value to sign expand. 
- index – 0 based bit index to sign bit (0 to 63) 
 
 
 - 
char *utf8_trunc(char *utf8_str)
- Properly truncate a NULL-terminated UTF-8 string. - Take a NULL-terminated UTF-8 string and ensure that if the string has been truncated (by setting the NULL terminator) earlier by other means, that the string ends with a properly formatted UTF-8 character (1-4 bytes). - Parameters:
- utf8_str – NULL-terminated string 
 
- Returns:
- Pointer to the - utf8_str
 
 - 
char *utf8_lcpy(char *dst, const char *src, size_t n)
- Copies a UTF-8 encoded string from - srcto- dst.- The resulting - dstwill always be NULL terminated if- nis larger than 0, and the- dststring will always be properly UTF-8 truncated.- Parameters:
- dst – The destination of the UTF-8 string. 
- src – The source string 
- n – The size of the - dstbuffer. Maximum number of characters copied is- n- 1. If 0 nothing will be done, and the- dstwill not be NULL terminated.
 
- Returns:
- Pointer to the - dst
 
 - 
static inline void mem_xor_n(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, size_t len)
- XOR n bytes. - Parameters:
- dst – Destination of where to store result. Shall be - lenbytes.
- src1 – First source. Shall be - lenbytes.
- src2 – Second source. Shall be - lenbytes.
- len – Number of bytes to XOR. 
 
 
 - 
static inline void mem_xor_32(uint8_t dst[4], const uint8_t src1[4], const uint8_t src2[4])
- XOR 32 bits. - Parameters:
- dst – Destination of where to store result. Shall be 32 bits. 
- src1 – First source. Shall be 32 bits. 
- src2 – Second source. Shall be 32 bits. 
 
 
 - 
static inline void mem_xor_128(uint8_t dst[16], const uint8_t src1[16], const uint8_t src2[16])
- XOR 128 bits. - Parameters:
- dst – Destination of where to store result. Shall be 128 bits. 
- src1 – First source. Shall be 128 bits. 
- src2 – Second source. Shall be 128 bits.