14#ifndef ZEPHYR_INCLUDE_SYS_UTIL_H_
15#define ZEPHYR_INCLUDE_SYS_UTIL_H_
36#define NUM_BITS(t) (sizeof(t) * BITS_PER_BYTE)
51#define POINTER_TO_UINT(x) ((uintptr_t) (x))
53#define UINT_TO_POINTER(x) ((void *) (uintptr_t) (x))
55#define POINTER_TO_INT(x) ((intptr_t) (x))
57#define INT_TO_POINTER(x) ((void *) (intptr_t) (x))
59#if !(defined(__CHAR_BIT__) && defined(__SIZEOF_LONG__) && defined(__SIZEOF_LONG_LONG__))
60# error Missing required predefined macros for BITS_PER_LONG calculation
64#define BITS_PER_BYTE (__CHAR_BIT__)
67#define BITS_PER_NIBBLE (__CHAR_BIT__ / 2)
70#define NIBBLES_PER_BYTE (BITS_PER_BYTE / BITS_PER_NIBBLE)
73#define BITS_PER_LONG (__CHAR_BIT__ * __SIZEOF_LONG__)
76#define BITS_PER_LONG_LONG (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
82#define GENMASK(h, l) \
83 (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
89#define GENMASK64(h, l) \
90 (((~0ULL) - (1ULL << (l)) + 1) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
93#define ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - (2 * !(cond))]) - 1)
95#if defined(__cplusplus)
100#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
109#define IS_ARRAY(array) \
110 ZERO_OR_COMPILE_ERROR( \
111 !__builtin_types_compatible_p(__typeof__(array), \
112 __typeof__(&(array)[0])))
123#define ARRAY_SIZE(array) \
124 ((size_t) (IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0]))))
145#define FLEXIBLE_ARRAY_DECLARE(type, name) \
147 struct { } __unused_##name; \
165#define IS_ARRAY_ELEMENT(array, ptr) \
166 ((ptr) && POINTER_TO_UINT(array) <= POINTER_TO_UINT(ptr) && \
167 POINTER_TO_UINT(ptr) < POINTER_TO_UINT(&(array)[ARRAY_SIZE(array)]) && \
168 (POINTER_TO_UINT(ptr) - POINTER_TO_UINT(array)) % sizeof((array)[0]) == 0)
184#define ARRAY_INDEX(array, ptr) \
186 __ASSERT_NO_MSG(IS_ARRAY_ELEMENT(array, ptr)); \
187 (__typeof__((array)[0]) *)(ptr) - (array); \
200#define PART_OF_ARRAY(array, ptr) \
201 ((ptr) && POINTER_TO_UINT(array) <= POINTER_TO_UINT(ptr) && \
202 POINTER_TO_UINT(ptr) < POINTER_TO_UINT(&(array)[ARRAY_SIZE(array)]))
221#define ARRAY_INDEX_FLOOR(array, ptr) \
223 __ASSERT_NO_MSG(PART_OF_ARRAY(array, ptr)); \
224 (POINTER_TO_UINT(ptr) - POINTER_TO_UINT(array)) / sizeof((array)[0]); \
233#define ARRAY_FOR_EACH(array, idx) for (size_t idx = 0; (idx) < ARRAY_SIZE(array); ++(idx))
241#define ARRAY_FOR_EACH_PTR(array, ptr) \
242 for (__typeof__(*(array)) *ptr = (array); (size_t)((ptr) - (array)) < ARRAY_SIZE(array); \
252#define SAME_TYPE(a, b) __builtin_types_compatible_p(__typeof__(a), __typeof__(b))
258#define CONTAINER_OF_VALIDATE(ptr, type, field) \
259 BUILD_ASSERT(SAME_TYPE(*(ptr), ((type *)0)->field) || \
260 SAME_TYPE(*(ptr), void), \
261 "pointer type mismatch in CONTAINER_OF");
263#define CONTAINER_OF_VALIDATE(ptr, type, field)
287#define CONTAINER_OF(ptr, type, field) \
289 CONTAINER_OF_VALIDATE(ptr, type, field) \
290 ((type *)(((char *)(ptr)) - offsetof(type, field))); \
301#define SIZEOF_FIELD(type, member) sizeof((((type *)0)->member))
315 UTIL_CAT(_CONCAT_, NUM_VA_ARGS_LESS_1(__VA_ARGS__))(__VA_ARGS__)
320#define IS_ALIGNED(ptr, align) (((uintptr_t)(ptr)) % (align) == 0)
325#define ROUND_UP(x, align) \
326 ((((unsigned long)(x) + ((unsigned long)(align) - 1)) / \
327 (unsigned long)(align)) * (unsigned long)(align))
332#define ROUND_DOWN(x, align) \
333 (((unsigned long)(x) / (unsigned long)(align)) * (unsigned long)(align))
336#define WB_UP(x) ROUND_UP(x, sizeof(void *))
339#define WB_DN(x) ROUND_DOWN(x, sizeof(void *))
355#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
372#define DIV_ROUND_CLOSEST(n, d) \
373 (((((__typeof__(n))-1) < 0) && (((__typeof__(d))-1) < 0) && ((n) < 0) ^ ((d) < 0)) \
374 ? ((n) - ((d) / 2)) / (d) \
375 : ((n) + ((d) / 2)) / (d))
389#define MAX(a, b) (((a) > (b)) ? (a) : (b))
404#define MIN(a, b) (((a) < (b)) ? (a) : (b))
420#define CLAMP(val, low, high) (((val) <= (low)) ? (low) : MIN(val, high))
435#define IN_RANGE(val, min, max) ((val) >= (min) && (val) <= (max))
487 sign_ext = (value >> 63) & 1;
490 sign_ext = -sign_ext;
493 return (value >> shift) | (sign_ext << (64 - shift));
505static inline void bytecpy(
void *dst,
const void *src,
size_t size)
509 for (i = 0; i < size; ++i) {
510 ((
volatile uint8_t *)dst)[i] = ((
volatile const uint8_t *)src)[i];
524static inline void byteswp(
void *a,
void *b,
size_t size)
530 for (; size > 0; --size) {
590 return ((10 * (bcd >> 4)) + (bcd & 0x0F));
602 return (((bin / 10) << 4) | (bin % 10));
628 __ASSERT_NO_MSG(index <= 31);
632 return (
int32_t)(value << shift) >> shift;
643 __ASSERT_NO_MSG(index <= 63);
647 return (
int64_t)(value << shift) >> shift;
707#define __z_log2d(x) (32 - __builtin_clz(x) - 1)
708#define __z_log2q(x) (64 - __builtin_clzll(x) - 1)
709#define __z_log2(x) (sizeof(__typeof__(x)) > 4 ? __z_log2q(x) : __z_log2d(x))
721#define LOG2(x) ((x) < 1 ? -1 : __z_log2(x))
733#define LOG2CEIL(x) ((x) <= 1 ? 0 : __z_log2((x)-1) + 1)
747#define NHPOT(x) ((x) < 1 ? 1 : ((x) > (1ULL<<63) ? 0 : 1ULL << LOG2CEIL(x)))
761#define Z_DETECT_POINTER_OVERFLOW(addr, buflen) \
762 (((buflen) != 0) && \
763 ((UINTPTR_MAX - (uintptr_t)(addr)) <= ((uintptr_t)((buflen) - 1))))
776 *dst++ = *src1++ ^ *src2++;
815static inline bool util_memeq(
const void *m1,
const void *m2,
size_t n)
817 return memcmp(m1, m2, n) == 0;
833static inline bool util_eq(
const void *m1,
size_t len1,
const void *m2,
size_t len2)
835 return len1 == len2 && (m1 == m2 ||
util_memeq(m1, m2, len1));
852#define KB(x) ((x) << 10)
854#define KB(x) (((size_t)(x)) << 10)
857#define MB(x) (KB(x) << 10)
859#define GB(x) (MB(x) << 10)
862#define KHZ(x) ((x) * 1000)
864#define MHZ(x) (KHZ(x) * 1000)
878#if defined(CONFIG_ARCH_POSIX)
879#define Z_SPIN_DELAY(t) k_busy_wait(t)
881#define Z_SPIN_DELAY(t)
899#define WAIT_FOR(expr, timeout, delay_stmt) \
901 uint32_t _wf_cycle_count = k_us_to_cyc_ceil32(timeout); \
902 uint32_t _wf_start = k_cycle_get_32(); \
903 while (!(expr) && (_wf_cycle_count > (k_cycle_get_32() - _wf_start))) { \
irp nz macro MOVR cc s mov cc s endm endr irp aw macro LDR aa s
Definition asm-macro-32-bit-gnu.h:17
irp nz macro MOVR cc s mov cc s endm endr irp aa
Definition asm-macro-32-bit-gnu.h:16
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.
Definition util.h:641
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:478
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:505
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 ALWAYS_INLINE bool is_null_no_warn(void *p)
Is p equal to NULL?
Definition util.h:466
static void mem_xor_128(uint8_t dst[16], const uint8_t src1[16], const uint8_t src2[16])
XOR 128 bits.
Definition util.h:799
static uint8_t bin2bcd(uint8_t bin)
Convert a binary value to binary coded decimal (BCD 8421).
Definition util.h:600
static void mem_xor_32(uint8_t dst[4], const uint8_t src1[4], const uint8_t src2[4])
XOR 32 bits.
Definition util.h:787
static void byteswp(void *a, void *b, size_t size)
byte by byte swap.
Definition util.h:524
static void mem_xor_n(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, size_t len)
XOR n bytes.
Definition util.h:773
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:588
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 util_eq(const void *m1, size_t len1, const void *m2, size_t len2)
Compare memory areas and their length.
Definition util.h:833
static bool util_memeq(const void *m1, const void *m2, size_t n)
Compare memory areas.
Definition util.h:815
static bool is_power_of_two(unsigned int x)
Is x a power of two?
Definition util.h:442
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.
Definition util.h:626
ssize_t utf8_count_chars(const char *s)
Counts the characters in a UTF-8 encoded string s.
size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen)
Convert a binary array into string representation.
#define NULL
Definition iar_missing_defs.h:20
__SIZE_TYPE__ ssize_t
Definition types.h:28
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__INT64_TYPE__ int64_t
Definition stdint.h:75
int memcmp(const void *m1, const void *m2, size_t n)