14#ifndef ZEPHYR_INCLUDE_SYS_UTIL_H_
15#define ZEPHYR_INCLUDE_SYS_UTIL_H_
39#define POINTER_TO_UINT(x) ((uintptr_t) (x))
41#define UINT_TO_POINTER(x) ((void *) (uintptr_t) (x))
43#define POINTER_TO_INT(x) ((intptr_t) (x))
45#define INT_TO_POINTER(x) ((void *) (intptr_t) (x))
47#if !(defined(__CHAR_BIT__) && defined(__SIZEOF_LONG__))
48# error Missing required predefined macros for BITS_PER_LONG calculation
52#define BITS_PER_LONG (__CHAR_BIT__ * __SIZEOF_LONG__)
58#define GENMASK(h, l) \
59 (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
62#define LSB_GET(value) ((value) & -(value))
68#define FIELD_GET(mask, value) (((value) & (mask)) / LSB_GET(mask))
75#define FIELD_PREP(mask, value) (((value) * LSB_GET(mask)) & (mask))
78#define ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - 2 * !(cond)]) - 1)
80#if defined(__cplusplus)
85#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
94#define IS_ARRAY(array) \
95 ZERO_OR_COMPILE_ERROR( \
96 !__builtin_types_compatible_p(__typeof__(array), \
97 __typeof__(&(array)[0])))
108#define ARRAY_SIZE(array) \
109 ((size_t) (IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0]))))
127#define IS_ARRAY_ELEMENT(array, ptr) \
128 ((ptr) && POINTER_TO_UINT(array) <= POINTER_TO_UINT(ptr) && \
129 POINTER_TO_UINT(ptr) < POINTER_TO_UINT(&(array)[ARRAY_SIZE(array)]) && \
130 (POINTER_TO_UINT(ptr) - POINTER_TO_UINT(array)) % sizeof((array)[0]) == 0)
146#define ARRAY_INDEX(array, ptr) \
148 __ASSERT_NO_MSG(IS_ARRAY_ELEMENT(array, ptr)); \
149 (__typeof__((array)[0]) *)(ptr) - (array); \
162#define PART_OF_ARRAY(array, ptr) \
163 ((ptr) && POINTER_TO_UINT(array) <= POINTER_TO_UINT(ptr) && \
164 POINTER_TO_UINT(ptr) < POINTER_TO_UINT(&(array)[ARRAY_SIZE(array)]))
183#define ARRAY_INDEX_FLOOR(array, ptr) \
185 __ASSERT_NO_MSG(PART_OF_ARRAY(array, ptr)); \
186 (POINTER_TO_UINT(ptr) - POINTER_TO_UINT(array)) / sizeof((array)[0]); \
210#define CONTAINER_OF(ptr, type, field) \
211 ((type *)(((char *)(ptr)) - offsetof(type, field)))
217#define ROUND_UP(x, align) \
218 (((unsigned long)(x) + ((unsigned long)(align) - 1)) & \
219 ~((unsigned long)(align) - 1))
225#define ROUND_DOWN(x, align) \
226 ((unsigned long)(x) & ~((unsigned long)(align) - 1))
229#define WB_UP(x) ROUND_UP(x, sizeof(void *))
232#define WB_DN(x) ROUND_DOWN(x, sizeof(void *))
237#define ceiling_fraction(numerator, divider) \
238 (((numerator) + ((divider) - 1)) / (divider))
252#define MAX(a, b) (((a) > (b)) ? (a) : (b))
267#define MIN(a, b) (((a) < (b)) ? (a) : (b))
283#define CLAMP(val, low, high) (((val) <= (low)) ? (low) : MIN(val, high))
298#define IN_RANGE(val, min, max) ((val) >= (min) && (val) <= (max))
307 return (x != 0U) && ((x & (x - 1U)) == 0U);
326 sign_ext = (value >> 63) & 1;
329 sign_ext = -sign_ext;
332 return (value >> shift) | (sign_ext << (64 - shift));
344static inline void bytecpy(
void *dst,
const void *src,
size_t size)
348 for (i = 0; i < size; ++i) {
349 ((
volatile uint8_t *)dst)[i] = ((
volatile const uint8_t *)src)[i];
363static inline void byteswp(
void *a,
void *b,
size_t size)
369 for (; size > 0; --size) {
429 return ((10 * (bcd >> 4)) + (bcd & 0x0F));
441 return (((bin / 10) << 4) | (bin % 10));
510#define KB(x) ((x) << 10)
512#define KB(x) (((size_t)x) << 10)
515#define MB(x) (KB(x) << 10)
517#define GB(x) (MB(x) << 10)
520#define KHZ(x) ((x) * 1000)
522#define MHZ(x) (KHZ(x) * 1000)
539#define WAIT_FOR(expr, timeout, delay_stmt) \
541 uint32_t cycle_count = k_us_to_cyc_ceil32(timeout); \
542 uint32_t start = k_cycle_get_32(); \
543 while (!(expr) && (cycle_count > (k_cycle_get_32() - 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:317
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:344
char * utf8_lcpy(char *dst, const char *src, size_t n)
Copies a UTF-8 encoded string from src to dst.
static uint8_t bin2bcd(uint8_t bin)
Convert a binary value to binary coded decimal (BCD 8421).
Definition: util.h:439
static void byteswp(void *a, void *b, size_t size)
byte by byte swap.
Definition: util.h:363
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:427
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:305
size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen)
Convert a binary array into string representation.
struct k_thread t
Definition: kobject.c:1327
uint32_t hex
Definition: printk.c:109
char c
Definition: printk.c:103
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__INT64_TYPE__ int64_t
Definition: stdint.h:75