Zephyr API Documentation 3.7.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
util.h File Reference

Misc utilities. More...

#include <zephyr/sys/util_macro.h>
#include <zephyr/toolchain.h>
#include <stdbool.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/types.h>
#include <stddef.h>
#include <stdint.h>
#include <zephyr/sys/time_units.h>

Go to the source code of this file.

Macros

#define NUM_BITS(t)
 Number of bits that make up a type.
 
#define POINTER_TO_UINT(x)
 Cast x, a pointer, to an unsigned integer.
 
#define UINT_TO_POINTER(x)
 Cast x, an unsigned integer, to a void*.
 
#define POINTER_TO_INT(x)
 Cast x, a pointer, to a signed integer.
 
#define INT_TO_POINTER(x)
 Cast x, a signed integer, to a void*.
 
#define BITS_PER_BYTE   (__CHAR_BIT__)
 Number of bits in a byte.
 
#define BITS_PER_NIBBLE   (__CHAR_BIT__ / 2)
 Number of bits in a nibble.
 
#define NIBBLES_PER_BYTE   (BITS_PER_BYTE / BITS_PER_NIBBLE)
 Number of nibbles in a byte.
 
#define BITS_PER_LONG   (__CHAR_BIT__ * __SIZEOF_LONG__)
 Number of bits in a long int.
 
#define BITS_PER_LONG_LONG   (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
 Number of bits in a long long int.
 
#define GENMASK(h, l)
 Create a contiguous bitmask starting at bit position l and ending at position h.
 
#define GENMASK64(h, l)
 Create a contiguous 64-bit bitmask starting at bit position l and ending at position h.
 
#define ZERO_OR_COMPILE_ERROR(cond)
 0 if cond is true-ish; causes a compile error otherwise.
 
#define IS_ARRAY(array)
 Zero if array has an array type, a compile error otherwise.
 
#define ARRAY_SIZE(array)
 Number of elements in the given array.
 
#define FLEXIBLE_ARRAY_DECLARE(type, name)
 Declare a flexible array member.
 
#define IS_ARRAY_ELEMENT(array, ptr)
 Whether ptr is an element of array.
 
#define ARRAY_INDEX(array, ptr)
 Index of ptr within array.
 
#define PART_OF_ARRAY(array, ptr)
 Check if a pointer ptr lies within array.
 
#define ARRAY_INDEX_FLOOR(array, ptr)
 Array-index of ptr within array, rounded down.
 
#define ARRAY_FOR_EACH(array, idx)
 Iterate over members of an array using an index variable.
 
#define ARRAY_FOR_EACH_PTR(array, ptr)
 Iterate over members of an array using a pointer.
 
#define SAME_TYPE(a, b)
 Validate if two entities have a compatible type.
 
#define CONTAINER_OF_VALIDATE(ptr, type, field)
 Validate CONTAINER_OF parameters, only applies to C mode.
 
#define CONTAINER_OF(ptr, type, field)
 Get a pointer to a structure containing the element.
 
#define SIZEOF_FIELD(type, member)
 Report the size of a struct field in bytes.
 
#define CONCAT(...)
 Concatenate input arguments.
 
#define IS_ALIGNED(ptr, align)
 Check if ptr is aligned to align alignment.
 
#define ROUND_UP(x, align)
 Value of x rounded up to the next multiple of align.
 
#define ROUND_DOWN(x, align)
 Value of x rounded down to the previous multiple of align.
 
#define WB_UP(x)
 Value of x rounded up to the next word boundary.
 
#define WB_DN(x)
 Value of x rounded down to the previous word boundary.
 
#define DIV_ROUND_UP(n, d)
 Divide and round up.
 
#define DIV_ROUND_CLOSEST(n, d)
 Divide and round to the nearest integer.
 
#define MAX(a, b)
 Obtain the maximum of two values.
 
#define MIN(a, b)
 Obtain the minimum of two values.
 
#define CLAMP(val, low, high)
 Clamp a value to a given range.
 
#define IN_RANGE(val, min, max)
 Checks if a value is within range.
 
#define LOG2(x)
 Compute log2(x)
 
#define LOG2CEIL(x)
 Compute ceil(log2(x))
 
#define NHPOT(x)
 Compute next highest power of two.
 
#define KB(x)
 Number of bytes in x kibibytes.
 
#define MB(x)
 Number of bytes in x mebibytes.
 
#define GB(x)
 Number of bytes in x gibibytes.
 
#define KHZ(x)
 Number of Hz in x kHz.
 
#define MHZ(x)
 Number of Hz in x MHz.
 
#define WAIT_FOR(expr, timeout, delay_stmt)
 Wait for an expression to return true with a timeout.
 

Functions

static bool is_power_of_two (unsigned int x)
 Is x a power of two?
 
static ALWAYS_INLINE bool is_null_no_warn (void *p)
 Is p equal to NULL?
 
static int64_t arithmetic_shift_right (int64_t value, uint8_t shift)
 Arithmetic shift right.
 
static void bytecpy (void *dst, const void *src, size_t size)
 byte by byte memcpy.
 
static void byteswp (void *a, void *b, size_t size)
 byte by byte swap.
 
int char2hex (char c, uint8_t *x)
 Convert a single character into a hexadecimal nibble.
 
int hex2char (uint8_t x, char *c)
 Convert a single hexadecimal nibble into a character.
 
size_t bin2hex (const uint8_t *buf, size_t buflen, char *hex, size_t hexlen)
 Convert a binary array into string representation.
 
size_t hex2bin (const char *hex, size_t hexlen, uint8_t *buf, size_t buflen)
 Convert a hexadecimal string into a binary array.
 
static uint8_t bcd2bin (uint8_t bcd)
 Convert a binary coded decimal (BCD 8421) value to binary.
 
static uint8_t bin2bcd (uint8_t bin)
 Convert a binary value to binary coded decimal (BCD 8421).
 
uint8_t u8_to_dec (char *buf, uint8_t buflen, uint8_t value)
 Convert a uint8_t into a decimal string representation.
 
static 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.
 
static int64_t sign_extend_64 (uint64_t value, uint8_t index)
 Sign extend a 64 bit value using the index bit as sign bit.
 
char * utf8_trunc (char *utf8_str)
 Properly truncate a NULL-terminated UTF-8 string.
 
char * utf8_lcpy (char *dst, const char *src, size_t n)
 Copies a UTF-8 encoded string from src to dst.
 
static void mem_xor_n (uint8_t *dst, const uint8_t *src1, const uint8_t *src2, size_t len)
 XOR n bytes.
 
static void mem_xor_32 (uint8_t dst[4], const uint8_t src1[4], const uint8_t src2[4])
 XOR 32 bits.
 
static void mem_xor_128 (uint8_t dst[16], const uint8_t src1[16], const uint8_t src2[16])
 XOR 128 bits.
 

Detailed Description

Misc utilities.

Misc utilities usable by the kernel and application code.

Macro Definition Documentation

◆ NUM_BITS

#define NUM_BITS ( t)
Value:
(sizeof(t) * 8)

Number of bits that make up a type.