14#ifndef ZEPHYR_INCLUDE_SYS_UTIL_H_
15#define ZEPHYR_INCLUDE_SYS_UTIL_H_
32#define NUM_BITS(t) (sizeof(t) * 8)
45#define POINTER_TO_UINT(x) ((uintptr_t) (x))
47#define UINT_TO_POINTER(x) ((void *) (uintptr_t) (x))
49#define POINTER_TO_INT(x) ((intptr_t) (x))
51#define INT_TO_POINTER(x) ((void *) (intptr_t) (x))
53#if !(defined(__CHAR_BIT__) && defined(__SIZEOF_LONG__) && defined(__SIZEOF_LONG_LONG__))
54# error Missing required predefined macros for BITS_PER_LONG calculation
58#define BITS_PER_LONG (__CHAR_BIT__ * __SIZEOF_LONG__)
61#define BITS_PER_LONG_LONG (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
67#define GENMASK(h, l) \
68 (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
74#define GENMASK64(h, l) \
75 (((~0ULL) - (1ULL << (l)) + 1) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
78#define LSB_GET(value) ((value) & -(value))
84#define FIELD_GET(mask, value) (((value) & (mask)) / LSB_GET(mask))
91#define FIELD_PREP(mask, value) (((value) * LSB_GET(mask)) & (mask))
94#define ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - 2 * !(cond)]) - 1)
96#if defined(__cplusplus)
101#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
110#define IS_ARRAY(array) \
111 ZERO_OR_COMPILE_ERROR( \
112 !__builtin_types_compatible_p(__typeof__(array), \
113 __typeof__(&(array)[0])))
124#define ARRAY_SIZE(array) \
125 ((size_t) (IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0]))))
143#define IS_ARRAY_ELEMENT(array, ptr) \
144 ((ptr) && POINTER_TO_UINT(array) <= POINTER_TO_UINT(ptr) && \
145 POINTER_TO_UINT(ptr) < POINTER_TO_UINT(&(array)[ARRAY_SIZE(array)]) && \
146 (POINTER_TO_UINT(ptr) - POINTER_TO_UINT(array)) % sizeof((array)[0]) == 0)
162#define ARRAY_INDEX(array, ptr) \
164 __ASSERT_NO_MSG(IS_ARRAY_ELEMENT(array, ptr)); \
165 (__typeof__((array)[0]) *)(ptr) - (array); \
178#define PART_OF_ARRAY(array, ptr) \
179 ((ptr) && POINTER_TO_UINT(array) <= POINTER_TO_UINT(ptr) && \
180 POINTER_TO_UINT(ptr) < POINTER_TO_UINT(&(array)[ARRAY_SIZE(array)]))
199#define ARRAY_INDEX_FLOOR(array, ptr) \
201 __ASSERT_NO_MSG(PART_OF_ARRAY(array, ptr)); \
202 (POINTER_TO_UINT(ptr) - POINTER_TO_UINT(array)) / sizeof((array)[0]); \
212#define SAME_TYPE(a, b) __builtin_types_compatible_p(__typeof__(a), __typeof__(b))
218#define CONTAINER_OF_VALIDATE(ptr, type, field) \
219 BUILD_ASSERT(SAME_TYPE(*(ptr), ((type *)0)->field) || \
220 SAME_TYPE(*(ptr), void), \
221 "pointer type mismatch in CONTAINER_OF");
223#define CONTAINER_OF_VALIDATE(ptr, type, field)
247#define CONTAINER_OF(ptr, type, field) \
249 CONTAINER_OF_VALIDATE(ptr, type, field) \
250 ((type *)(((char *)(ptr)) - offsetof(type, field))); \
256#define ROUND_UP(x, align) \
257 ((((unsigned long)(x) + ((unsigned long)(align) - 1)) / \
258 (unsigned long)(align)) * (unsigned long)(align))
263#define ROUND_DOWN(x, align) \
264 (((unsigned long)(x) / (unsigned long)(align)) * (unsigned long)(align))
267#define WB_UP(x) ROUND_UP(x, sizeof(void *))
270#define WB_DN(x) ROUND_DOWN(x, sizeof(void *))
286#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
303#define DIV_ROUND_CLOSEST(n, d) \
304 ((((n) < 0) ^ ((d) < 0)) ? ((n) - ((d) / 2)) / (d) : \
305 ((n) + ((d) / 2)) / (d))
311#define ceiling_fraction(numerator, divider) __DEPRECATED_MACRO \
312 DIV_ROUND_UP(numerator, divider)
326#define MAX(a, b) (((a) > (b)) ? (a) : (b))
341#define MIN(a, b) (((a) < (b)) ? (a) : (b))
357#define CLAMP(val, low, high) (((val) <= (low)) ? (low) : MIN(val, high))
372#define IN_RANGE(val, min, max) ((val) >= (min) && (val) <= (max))
400 sign_ext = (value >> 63) & 1;
403 sign_ext = -sign_ext;
406 return (value >> shift) | (sign_ext << (64 - shift));
418static inline void bytecpy(
void *dst,
const void *src,
size_t size)
422 for (i = 0; i < size; ++i) {
423 ((
volatile uint8_t *)dst)[i] = ((
volatile const uint8_t *)src)[i];
437static inline void byteswp(
void *a,
void *b,
size_t size)
443 for (; size > 0; --size) {
503 return ((10 * (bcd >> 4)) + (bcd & 0x0F));
515 return (((bin / 10) << 4) | (bin % 10));
575#define __z_log2d(x) (32 - __builtin_clz(x) - 1)
576#define __z_log2q(x) (64 - __builtin_clzll(x) - 1)
577#define __z_log2(x) (sizeof(__typeof__(x)) > 4 ? __z_log2q(x) : __z_log2d(x))
589#define LOG2(x) ((x) < 1 ? -1 : __z_log2(x))
601#define LOG2CEIL(x) ((x) < 1 ? 0 : __z_log2((x)-1) + 1)
615#define NHPOT(x) ((x) < 1 ? 1 : ((x) > (1ULL<<63) ? 0 : 1ULL << LOG2CEIL(x)))
629#define Z_DETECT_POINTER_OVERFLOW(addr, buflen) \
630 (((buflen) != 0) && \
631 ((UINTPTR_MAX - (uintptr_t)(addr)) <= ((uintptr_t)((buflen) - 1))))
647#define KB(x) ((x) << 10)
649#define KB(x) (((size_t)x) << 10)
652#define MB(x) (KB(x) << 10)
654#define GB(x) (MB(x) << 10)
657#define KHZ(x) ((x) * 1000)
659#define MHZ(x) (KHZ(x) * 1000)
673#if defined(CONFIG_ARCH_POSIX)
674#define Z_SPIN_DELAY(t) k_busy_wait(t)
676#define Z_SPIN_DELAY(t)
694#define WAIT_FOR(expr, timeout, delay_stmt) \
696 uint32_t _wf_cycle_count = k_us_to_cyc_ceil32(timeout); \
697 uint32_t _wf_start = k_cycle_get_32(); \
698 while (!(expr) && (_wf_cycle_count > (k_cycle_get_32() - _wf_start))) { \
irp nz macro MOVR cc s mov cc s endm endr irp aa
Definition: asm-macro-32-bit-gnu.h:16
char * utf8_trunc(char *utf8_str)
Properly truncate a NULL-terminated UTF-8 string.
static int64_t arithmetic_shift_right(int64_t value, uint8_t shift)
Arithmetic shift right.
Definition: util.h:391
size_t hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen)
Convert a hexadecimal string into a binary array.
static void bytecpy(void *dst, const void *src, size_t size)
byte by byte memcpy.
Definition: util.h:418
char * utf8_lcpy(char *dst, const char *src, size_t n)
Copies a UTF-8 encoded string from src to dst.
#define IS_POWER_OF_TWO(x)
Check if a x is a power of two.
Definition: util_macro.h:77
static uint8_t bin2bcd(uint8_t bin)
Convert a binary value to binary coded decimal (BCD 8421).
Definition: util.h:513
static void byteswp(void *a, void *b, size_t size)
byte by byte swap.
Definition: util.h:437
int hex2char(uint8_t x, char *c)
Convert a single hexadecimal nibble into a character.
static uint8_t bcd2bin(uint8_t bcd)
Convert a binary coded decimal (BCD 8421) value to binary.
Definition: util.h:501
int char2hex(char c, uint8_t *x)
Convert a single character into a hexadecimal nibble.
uint8_t u8_to_dec(char *buf, uint8_t buflen, uint8_t value)
Convert a uint8_t into a decimal string representation.
static bool is_power_of_two(unsigned int x)
Is x a power of two?
Definition: util.h:379
size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen)
Convert a binary array into string representation.
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__INT64_TYPE__ int64_t
Definition: stdint.h:75