Zephyr API Documentation  3.6.0
A Scalable Open Source RTOS
3.6.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
util.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2014, Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
14#ifndef ZEPHYR_INCLUDE_SYS_UTIL_H_
15#define ZEPHYR_INCLUDE_SYS_UTIL_H_
16
18#include <zephyr/toolchain.h>
19
20/* needs to be outside _ASMLANGUAGE so 'true' and 'false' can turn
21 * into '1' and '0' for asm or linker scripts
22 */
23#include <stdbool.h>
24
25#ifndef _ASMLANGUAGE
26
27#include <zephyr/types.h>
28#include <stddef.h>
29#include <stdint.h>
30
32#define NUM_BITS(t) (sizeof(t) * 8)
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
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))
52
53#if !(defined(__CHAR_BIT__) && defined(__SIZEOF_LONG__) && defined(__SIZEOF_LONG_LONG__))
54# error Missing required predefined macros for BITS_PER_LONG calculation
55#endif
56
58#define BITS_PER_LONG (__CHAR_BIT__ * __SIZEOF_LONG__)
59
61#define BITS_PER_LONG_LONG (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
62
67#define GENMASK(h, l) \
68 (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
69
74#define GENMASK64(h, l) \
75 (((~0ULL) - (1ULL << (l)) + 1) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
76
78#define LSB_GET(value) ((value) & -(value))
79
84#define FIELD_GET(mask, value) (((value) & (mask)) / LSB_GET(mask))
85
91#define FIELD_PREP(mask, value) (((value) * LSB_GET(mask)) & (mask))
92
94#define ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - 2 * !(cond)]) - 1)
95
96#if defined(__cplusplus)
97
98/* The built-in function used below for type checking in C is not
99 * supported by GNU C++.
100 */
101#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
102
103#else /* __cplusplus */
104
110#define IS_ARRAY(array) \
111 ZERO_OR_COMPILE_ERROR( \
112 !__builtin_types_compatible_p(__typeof__(array), \
113 __typeof__(&(array)[0])))
114
124#define ARRAY_SIZE(array) \
125 ((size_t) (IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0]))))
126
127#endif /* __cplusplus */
128
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)
147
162#define ARRAY_INDEX(array, ptr) \
163 ({ \
164 __ASSERT_NO_MSG(IS_ARRAY_ELEMENT(array, ptr)); \
165 (__typeof__((array)[0]) *)(ptr) - (array); \
166 })
167
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)]))
181
199#define ARRAY_INDEX_FLOOR(array, ptr) \
200 ({ \
201 __ASSERT_NO_MSG(PART_OF_ARRAY(array, ptr)); \
202 (POINTER_TO_UINT(ptr) - POINTER_TO_UINT(array)) / sizeof((array)[0]); \
203 })
204
211#define ARRAY_FOR_EACH(array, idx) for (size_t idx = 0; (idx) < ARRAY_SIZE(array); ++(idx))
212
219#define ARRAY_FOR_EACH_PTR(array, ptr) \
220 for (__typeof__(*(array)) *ptr = (array); (size_t)((ptr) - (array)) < ARRAY_SIZE(array); \
221 ++(ptr))
222
230#define SAME_TYPE(a, b) __builtin_types_compatible_p(__typeof__(a), __typeof__(b))
231
235#ifndef __cplusplus
236#define CONTAINER_OF_VALIDATE(ptr, type, field) \
237 BUILD_ASSERT(SAME_TYPE(*(ptr), ((type *)0)->field) || \
238 SAME_TYPE(*(ptr), void), \
239 "pointer type mismatch in CONTAINER_OF");
240#else
241#define CONTAINER_OF_VALIDATE(ptr, type, field)
242#endif
243
265#define CONTAINER_OF(ptr, type, field) \
266 ({ \
267 CONTAINER_OF_VALIDATE(ptr, type, field) \
268 ((type *)(((char *)(ptr)) - offsetof(type, field))); \
269 })
270
282#define CONCAT(...) \
283 UTIL_CAT(_CONCAT_, NUM_VA_ARGS_LESS_1(__VA_ARGS__))(__VA_ARGS__)
284
288#define ROUND_UP(x, align) \
289 ((((unsigned long)(x) + ((unsigned long)(align) - 1)) / \
290 (unsigned long)(align)) * (unsigned long)(align))
291
295#define ROUND_DOWN(x, align) \
296 (((unsigned long)(x) / (unsigned long)(align)) * (unsigned long)(align))
297
299#define WB_UP(x) ROUND_UP(x, sizeof(void *))
300
302#define WB_DN(x) ROUND_DOWN(x, sizeof(void *))
303
318#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
319
335#define DIV_ROUND_CLOSEST(n, d) \
336 ((((n) < 0) ^ ((d) < 0)) ? ((n) - ((d) / 2)) / (d) : \
337 ((n) + ((d) / 2)) / (d))
338
343#define ceiling_fraction(numerator, divider) __DEPRECATED_MACRO \
344 DIV_ROUND_UP(numerator, divider)
345
346#ifndef MAX
358#define MAX(a, b) (((a) > (b)) ? (a) : (b))
359#endif
360
361#ifndef MIN
373#define MIN(a, b) (((a) < (b)) ? (a) : (b))
374#endif
375
376#ifndef CLAMP
389#define CLAMP(val, low, high) (((val) <= (low)) ? (low) : MIN(val, high))
390#endif
391
404#define IN_RANGE(val, min, max) ((val) >= (min) && (val) <= (max))
405
411static inline bool is_power_of_two(unsigned int x)
412{
413 return IS_POWER_OF_TWO(x);
414}
415
424{
425 int64_t sign_ext;
426
427 if (shift == 0U) {
428 return value;
429 }
430
431 /* extract sign bit */
432 sign_ext = (value >> 63) & 1;
433
434 /* make all bits of sign_ext be the same as the value's sign bit */
435 sign_ext = -sign_ext;
436
437 /* shift value and fill opened bit positions with sign bit */
438 return (value >> shift) | (sign_ext << (64 - shift));
439}
440
450static inline void bytecpy(void *dst, const void *src, size_t size)
451{
452 size_t i;
453
454 for (i = 0; i < size; ++i) {
455 ((volatile uint8_t *)dst)[i] = ((volatile const uint8_t *)src)[i];
456 }
457}
458
469static inline void byteswp(void *a, void *b, size_t size)
470{
471 uint8_t t;
472 uint8_t *aa = (uint8_t *)a;
473 uint8_t *bb = (uint8_t *)b;
474
475 for (; size > 0; --size) {
476 t = *aa;
477 *aa++ = *bb;
478 *bb++ = t;
479 }
480}
481
490int char2hex(char c, uint8_t *x);
491
500int hex2char(uint8_t x, char *c);
501
512size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen);
513
524size_t hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen);
525
533static inline uint8_t bcd2bin(uint8_t bcd)
534{
535 return ((10 * (bcd >> 4)) + (bcd & 0x0F));
536}
537
545static inline uint8_t bin2bcd(uint8_t bin)
546{
547 return (((bin / 10) << 4) | (bin % 10));
548}
549
563uint8_t u8_to_dec(char *buf, uint8_t buflen, uint8_t value);
564
589char *utf8_trunc(char *utf8_str);
590
605char *utf8_lcpy(char *dst, const char *src, size_t n);
606
607#define __z_log2d(x) (32 - __builtin_clz(x) - 1)
608#define __z_log2q(x) (64 - __builtin_clzll(x) - 1)
609#define __z_log2(x) (sizeof(__typeof__(x)) > 4 ? __z_log2q(x) : __z_log2d(x))
610
621#define LOG2(x) ((x) < 1 ? -1 : __z_log2(x))
622
633#define LOG2CEIL(x) ((x) < 1 ? 0 : __z_log2((x)-1) + 1)
634
647#define NHPOT(x) ((x) < 1 ? 1 : ((x) > (1ULL<<63) ? 0 : 1ULL << LOG2CEIL(x)))
648
661#define Z_DETECT_POINTER_OVERFLOW(addr, buflen) \
662 (((buflen) != 0) && \
663 ((UINTPTR_MAX - (uintptr_t)(addr)) <= ((uintptr_t)((buflen) - 1))))
664
673static inline void mem_xor_n(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, size_t len)
674{
675 while (len--) {
676 *dst++ = *src1++ ^ *src2++;
677 }
678}
679
687static inline void mem_xor_32(uint8_t dst[4], const uint8_t src1[4], const uint8_t src2[4])
688{
689 mem_xor_n(dst, src1, src2, 4U);
690}
691
699static inline void mem_xor_128(uint8_t dst[16], const uint8_t src1[16], const uint8_t src2[16])
700{
701 mem_xor_n(dst, src1, src2, 16);
702}
703
704#ifdef __cplusplus
705}
706#endif
707
708/* This file must be included at the end of the !_ASMLANGUAGE guard.
709 * It depends on macros defined in this file above which cannot be forward declared.
710 */
712
713#endif /* !_ASMLANGUAGE */
714
716#ifdef _LINKER
717/* This is used in linker scripts so need to avoid type casting there */
718#define KB(x) ((x) << 10)
719#else
720#define KB(x) (((size_t)x) << 10)
721#endif
723#define MB(x) (KB(x) << 10)
725#define GB(x) (MB(x) << 10)
726
728#define KHZ(x) ((x) * 1000)
730#define MHZ(x) (KHZ(x) * 1000)
731
744#if defined(CONFIG_ARCH_POSIX)
745#define Z_SPIN_DELAY(t) k_busy_wait(t)
746#else
747#define Z_SPIN_DELAY(t)
748#endif
749
765#define WAIT_FOR(expr, timeout, delay_stmt) \
766 ({ \
767 uint32_t _wf_cycle_count = k_us_to_cyc_ceil32(timeout); \
768 uint32_t _wf_start = k_cycle_get_32(); \
769 while (!(expr) && (_wf_cycle_count > (k_cycle_get_32() - _wf_start))) { \
770 delay_stmt; \
771 Z_SPIN_DELAY(10); \
772 } \
773 (expr); \
774 })
775
780#endif /* ZEPHYR_INCLUDE_SYS_UTIL_H_ */
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:423
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:450
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 void mem_xor_128(uint8_t dst[16], const uint8_t src1[16], const uint8_t src2[16])
XOR 128 bits.
Definition: util.h:699
static uint8_t bin2bcd(uint8_t bin)
Convert a binary value to binary coded decimal (BCD 8421).
Definition: util.h:545
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:687
static void byteswp(void *a, void *b, size_t size)
byte by byte swap.
Definition: util.h:469
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:673
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:533
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:411
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
Macros to abstract toolchain specific capabilities.
Macro utilities.