Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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
13
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/sys/__assert.h>
28#include <zephyr/types.h>
29#include <stddef.h>
30#include <stdint.h>
31#include <string.h>
32
34#define NUM_BITS(t) (sizeof(t) * BITS_PER_BYTE)
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
47
49#define POINTER_TO_UINT(x) ((uintptr_t)(x))
51#define UINT_TO_POINTER(x) ((void *)(uintptr_t)(x))
53#define POINTER_TO_INT(x) ((intptr_t)(x))
55#define INT_TO_POINTER(x) ((void *)(intptr_t)(x))
56
57#if !(defined(__CHAR_BIT__) && defined(__SIZEOF_LONG__) && defined(__SIZEOF_LONG_LONG__))
58#error Missing required predefined macros for BITS_PER_LONG calculation
59#endif
60
62#define BITS_PER_BYTE (__CHAR_BIT__)
63
65#define BITS_PER_NIBBLE (__CHAR_BIT__ / 2)
66
68#define NIBBLES_PER_BYTE (BITS_PER_BYTE / BITS_PER_NIBBLE)
69
71#define BITS_PER_LONG (__CHAR_BIT__ * __SIZEOF_LONG__)
72
74#define BITS_PER_LONG_LONG (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
75
80#define GENMASK(h, l) (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
81
86#define GENMASK64(h, l) (((~0ULL) - (1ULL << (l)) + 1) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
87
89#define ZERO_OR_COMPILE_ERROR(cond) ((int)sizeof(char[1 - (2 * !(cond))]) - 1)
90
91#if defined(__cplusplus)
92
93/* The built-in function used below for type checking in C is not
94 * supported by GNU C++.
95 */
96#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
97
98#else /* __cplusplus */
99
105#define IS_ARRAY(array) \
106 ZERO_OR_COMPILE_ERROR( \
107 !__builtin_types_compatible_p(__typeof__(array), __typeof__(&(array)[0])))
108
118#define ARRAY_SIZE(array) ((size_t)(IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0]))))
119
120#endif /* __cplusplus */
121
139#define FLEXIBLE_ARRAY_DECLARE(type, name) \
140 struct { \
141 struct { \
142 } __unused_##name; \
143 type name[]; \
144 }
145
160#define IS_ARRAY_ELEMENT(array, ptr) \
161 ((ptr) && POINTER_TO_UINT(array) <= POINTER_TO_UINT(ptr) && \
162 POINTER_TO_UINT(ptr) < POINTER_TO_UINT(&(array)[ARRAY_SIZE(array)]) && \
163 (POINTER_TO_UINT(ptr) - POINTER_TO_UINT(array)) % sizeof((array)[0]) == 0)
164
179#define ARRAY_INDEX(array, ptr) \
180 ({ \
181 __ASSERT_NO_MSG(IS_ARRAY_ELEMENT(array, ptr)); \
182 (__typeof__((array)[0]) *)(ptr) - (array); \
183 })
184
195#define PART_OF_ARRAY(array, ptr) \
196 ((ptr) && POINTER_TO_UINT(array) <= POINTER_TO_UINT(ptr) && \
197 POINTER_TO_UINT(ptr) < POINTER_TO_UINT(&(array)[ARRAY_SIZE(array)]))
198
216#define ARRAY_INDEX_FLOOR(array, ptr) \
217 ({ \
218 __ASSERT_NO_MSG(PART_OF_ARRAY(array, ptr)); \
219 (POINTER_TO_UINT(ptr) - POINTER_TO_UINT(array)) / sizeof((array)[0]); \
220 })
221
228#define ARRAY_FOR_EACH(array, idx) for (size_t idx = 0; (idx) < ARRAY_SIZE(array); ++(idx))
229
236#define ARRAY_FOR_EACH_PTR(array, ptr) \
237 for (__typeof__(*(array)) *ptr = (array); (size_t)((ptr) - (array)) < ARRAY_SIZE(array); \
238 ++(ptr))
239
247#define SAME_TYPE(a, b) __builtin_types_compatible_p(__typeof__(a), __typeof__(b))
248
252#ifndef __cplusplus
253#define CONTAINER_OF_VALIDATE(ptr, type, field) \
254 BUILD_ASSERT(SAME_TYPE(*(ptr), ((type *)0)->field) || SAME_TYPE(*(ptr), void), \
255 "pointer type mismatch in CONTAINER_OF");
256#else
257#define CONTAINER_OF_VALIDATE(ptr, type, field)
258#endif
259
281#define CONTAINER_OF(ptr, type, field) \
282 ({ \
283 CONTAINER_OF_VALIDATE(ptr, type, field) \
284 ((type *)(((char *)(ptr)) - offsetof(type, field))); \
285 })
286
295#define SIZEOF_FIELD(type, member) sizeof((((type *)0)->member))
296
308#define CONCAT(...) UTIL_CAT(_CONCAT_, NUM_VA_ARGS_LESS_1(__VA_ARGS__))(__VA_ARGS__)
309
313#define IS_ALIGNED(ptr, align) (((uintptr_t)(ptr)) % (align) == 0)
314
318#define ROUND_UP(x, align) \
319 ((((unsigned long)(x) + ((unsigned long)(align) - 1)) / (unsigned long)(align)) * \
320 (unsigned long)(align))
321
325#define ROUND_DOWN(x, align) \
326 (((unsigned long)(x) / (unsigned long)(align)) * (unsigned long)(align))
327
329#define WB_UP(x) ROUND_UP(x, sizeof(void *))
330
332#define WB_DN(x) ROUND_DOWN(x, sizeof(void *))
333
348#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
349
365#define DIV_ROUND_CLOSEST(n, d) \
366 (((((__typeof__(n))-1) < 0) && (((__typeof__(d))-1) < 0) && ((n) < 0) ^ ((d) < 0)) \
367 ? ((n) - ((d) / 2)) / (d) \
368 : ((n) + ((d) / 2)) / (d))
369
373#define Z_INTERNAL_MAX(a, b) (((a) > (b)) ? (a) : (b))
374#define Z_INTERNAL_MIN(a, b) (((a) < (b)) ? (a) : (b))
378
379#ifndef MAX
391#define MAX(a, b) Z_INTERNAL_MAX(a, b)
392#endif
393
394#ifndef MIN
406#define MIN(a, b) Z_INTERNAL_MIN(a, b)
407#endif
408
409#ifndef MAX_FROM_LIST
415#define Z_MAX_1(a) a
416
426#define Z_MAX_2(a, b) ((a) > (b) ? (a) : (b))
427
436#define Z_MAX_3(a, b, c) Z_MAX_2(a, Z_MAX_2(b, c))
437
447#define Z_MAX_4(a, b, c, d) Z_MAX_2(Z_MAX_2(a, b), Z_MAX_2(c, d))
448
453#define Z_MAX_5(a, b, c, d, e) Z_MAX_2(Z_MAX_4(a, b, c, d), e)
454
459#define Z_MAX_6(a, b, c, d, e, f) Z_MAX_2(Z_MAX_5(a, b, c, d, e), f)
460
465#define Z_MAX_7(a, b, c, d, e, f, g) Z_MAX_2(Z_MAX_6(a, b, c, d, e, f), g)
466
471#define Z_MAX_8(a, b, c, d, e, f, g, h) Z_MAX_2(Z_MAX_7(a, b, c, d, e, f, g), h)
472
477#define Z_MAX_9(a, b, c, d, e, f, g, h, i) Z_MAX_2(Z_MAX_8(a, b, c, d, e, f, g, h), i)
478
483#define Z_MAX_10(a, b, c, d, e, f, g, h, i, j) Z_MAX_2(Z_MAX_9(a, b, c, d, e, f, g, h, i), j)
484
507#define Z_GET_MAX_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, NAME, ...) NAME
508
525#define MAX_FROM_LIST(...) \
526 Z_GET_MAX_MACRO(__VA_ARGS__, Z_MAX_10, Z_MAX_9, Z_MAX_8, Z_MAX_7, Z_MAX_6, Z_MAX_5, \
527 Z_MAX_4, Z_MAX_3, Z_MAX_2, Z_MAX_1)(__VA_ARGS__)
528#endif
529
530#ifndef CLAMP
543#define CLAMP(val, low, high) (((val) <= (low)) ? (low) : Z_INTERNAL_MIN(val, high))
544#endif
545
558#define IN_RANGE(val, min, max) ((val) >= (min) && (val) <= (max))
559
574int bitmask_find_gap(uint32_t mask, size_t num_bits, size_t total_bits, bool first_match);
575
581static inline bool is_power_of_two(unsigned int x)
582{
583 return IS_POWER_OF_TWO(x);
584}
585
605static ALWAYS_INLINE bool is_null_no_warn(void *p)
606{
607 return p == NULL;
608}
609
618{
619 int64_t sign_ext;
620
621 if (shift == 0U) {
622 return value;
623 }
624
625 /* extract sign bit */
626 sign_ext = (value >> 63) & 1;
627
628 /* make all bits of sign_ext be the same as the value's sign bit */
629 sign_ext = -sign_ext;
630
631 /* shift value and fill opened bit positions with sign bit */
632 return (value >> shift) | (sign_ext << (64 - shift));
633}
634
644static inline void bytecpy(void *dst, const void *src, size_t size)
645{
646 size_t i;
647
648 for (i = 0; i < size; ++i) {
649 ((volatile uint8_t *)dst)[i] = ((volatile const uint8_t *)src)[i];
650 }
651}
652
663static inline void byteswp(void *a, void *b, size_t size)
664{
665 uint8_t t;
666 uint8_t *aa = (uint8_t *)a;
667 uint8_t *bb = (uint8_t *)b;
668
669 for (; size > 0; --size) {
670 t = *aa;
671 *aa++ = *bb;
672 *bb++ = t;
673 }
674}
675
684int char2hex(char c, uint8_t *x);
685
694int hex2char(uint8_t x, char *c);
695
706size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen);
707
718size_t hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen);
719
727static inline uint8_t bcd2bin(uint8_t bcd)
728{
729 return ((10 * (bcd >> 4)) + (bcd & 0x0F));
730}
731
739static inline uint8_t bin2bcd(uint8_t bin)
740{
741 return (((bin / 10) << 4) | (bin % 10));
742}
743
757uint8_t u8_to_dec(char *buf, uint8_t buflen, uint8_t value);
758
765static inline int32_t sign_extend(uint32_t value, uint8_t index)
766{
767 __ASSERT_NO_MSG(index <= 31);
768
769 uint8_t shift = 31 - index;
770
771 return (int32_t)(value << shift) >> shift;
772}
773
780static inline int64_t sign_extend_64(uint64_t value, uint8_t index)
781{
782 __ASSERT_NO_MSG(index <= 63);
783
784 uint8_t shift = 63 - index;
785
786 return (int64_t)(value << shift) >> shift;
787}
788
789#define __z_log2d(x) (32 - __builtin_clz(x) - 1)
790#define __z_log2q(x) (64 - __builtin_clzll(x) - 1)
791#define __z_log2(x) (sizeof(__typeof__(x)) > 4 ? __z_log2q(x) : __z_log2d(x))
792
803#define LOG2(x) ((x) < 1 ? -1 : __z_log2(x))
804
815#define LOG2CEIL(x) ((x) <= 1 ? 0 : __z_log2((x) - 1) + 1)
816
829#define NHPOT(x) ((x) < 1 ? 1 : ((x) > (1ULL << 63) ? 0 : 1ULL << LOG2CEIL(x)))
830
843#define Z_DETECT_POINTER_OVERFLOW(addr, buflen) \
844 (((buflen) != 0) && ((UINTPTR_MAX - (uintptr_t)(addr)) <= ((uintptr_t)((buflen) - 1))))
845
854static inline void mem_xor_n(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, size_t len)
855{
856 while (len--) {
857 *dst++ = *src1++ ^ *src2++;
858 }
859}
860
868static inline void mem_xor_32(uint8_t dst[4], const uint8_t src1[4], const uint8_t src2[4])
869{
870 mem_xor_n(dst, src1, src2, 4U);
871}
872
880static inline void mem_xor_128(uint8_t dst[16], const uint8_t src1[16], const uint8_t src2[16])
881{
882 mem_xor_n(dst, src1, src2, 16);
883}
884
896static inline bool util_memeq(const void *m1, const void *m2, size_t n)
897{
898 return memcmp(m1, m2, n) == 0;
899}
900
914static inline bool util_eq(const void *m1, size_t len1, const void *m2, size_t len2)
915{
916 return len1 == len2 && (m1 == m2 || util_memeq(m1, m2, len1));
917}
918
925static inline size_t sys_count_bits(const void *value, size_t len)
926{
927 size_t cnt = 0U;
928 size_t i = 0U;
929
930#ifdef POPCOUNT
931 for (; i < len / sizeof(unsigned int); i++) {
932 unsigned int val;
933 (void)memcpy(&val, (const uint8_t *)value + i * sizeof(unsigned int),
934 sizeof(unsigned int));
935
936 cnt += POPCOUNT(val);
937 }
938 i *= sizeof(unsigned int); /* convert to a uint8_t index for the remainder (if any) */
939#endif
940
941 for (; i < len; i++) {
942 uint8_t value_u8 = ((const uint8_t *)value)[i];
943
944 /* Implements Brian Kernighan’s Algorithm to count bits */
945 while (value_u8) {
946 value_u8 &= (value_u8 - 1);
947 cnt++;
948 }
949 }
950
951 return cnt;
952}
953
963#define SYS_SIGN(x) (((x) > 0) - ((x) < 0))
964
975#define sys_gcd(a, b) ((((__typeof__(a))-1) < 0) ? sys_gcd_s(a, b) : sys_gcd_u(a, b))
976
980static ALWAYS_INLINE uint32_t sys_gcd_u(uint32_t a, uint32_t b)
981{
982 uint32_t c;
983
984 if (a == 0) {
985 return b;
986 }
987
988 if (b == 0) {
989 return a;
990 }
991
992 c = a % b;
993 while (c != 0) {
994 a = b;
995 b = c;
996 c = a % b;
997 }
998
999 return b;
1000}
1001
1002static ALWAYS_INLINE uint32_t sys_gcd_s(int32_t a, int32_t b)
1003{
1004 return sys_gcd_u(a < 0 ? -(uint32_t)a : (uint32_t)a, b < 0 ? -(uint32_t)b : (uint32_t)b);
1005}
1009
1019#define sys_lcm(a, b) ((((__typeof__(a))-1) < 0) ? sys_lcm_s(a, b) : sys_lcm_u(a, b))
1020
1024static ALWAYS_INLINE uint64_t sys_lcm_u(uint32_t a, uint32_t b)
1025{
1026 if (a == 0 || b == 0) {
1027 return 0;
1028 }
1029
1030 return (uint64_t)(a / sys_gcd_u(a, b)) * (uint64_t)b;
1031}
1032
1033static ALWAYS_INLINE uint64_t sys_lcm_s(int32_t a, int32_t b)
1034{
1035 return sys_lcm_u(a < 0 ? -(uint32_t)a : (uint32_t)a, b < 0 ? -(uint32_t)b : (uint32_t)b);
1036}
1040
1041#ifdef __cplusplus
1042}
1043#endif
1044
1045/* This file must be included at the end of the !_ASMLANGUAGE guard.
1046 * It depends on macros defined in this file above which cannot be forward declared.
1047 */
1048#include <zephyr/sys/time_units.h>
1049
1050#endif /* !_ASMLANGUAGE */
1051
1053#ifdef _LINKER
1054/* This is used in linker scripts so need to avoid type casting there */
1055#define KB(x) ((x) << 10)
1056#else
1057#define KB(x) (((size_t)(x)) << 10)
1058#endif
1060#define MB(x) (KB(x) << 10)
1062#define GB(x) (MB(x) << 10)
1063
1065#define KHZ(x) ((x) * 1000)
1067#define MHZ(x) (KHZ(x) * 1000)
1068
1081#if defined(CONFIG_ARCH_POSIX)
1082#define Z_SPIN_DELAY(t) k_busy_wait(t)
1083#else
1084#define Z_SPIN_DELAY(t)
1085#endif
1086
1102#define WAIT_FOR(expr, timeout, delay_stmt) \
1103 ({ \
1104 uint32_t _wf_cycle_count = k_us_to_cyc_ceil32(timeout); \
1105 uint32_t _wf_start = k_cycle_get_32(); \
1106 bool _wf_ret; \
1107 while (!(_wf_ret = (expr)) && \
1108 (_wf_cycle_count > (k_cycle_get_32() - _wf_start))) { \
1109 delay_stmt; \
1110 Z_SPIN_DELAY(10); \
1111 } \
1112 (_wf_ret); \
1113 })
1114
1122#ifdef CONFIG_ZTEST
1123#define ZTESTABLE_STATIC
1124#else
1125#define ZTESTABLE_STATIC static
1126#endif
1127
1131
1132#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
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:780
static int64_t arithmetic_shift_right(int64_t value, uint8_t shift)
Arithmetic shift right.
Definition util.h:617
static size_t sys_count_bits(const void *value, size_t len)
Returns the number of bits set in a value.
Definition util.h:925
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:644
#define IS_POWER_OF_TWO(x)
Check if a x is a power of two.
Definition util_macro.h:83
static ALWAYS_INLINE bool is_null_no_warn(void *p)
Is p equal to NULL?
Definition util.h:605
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:880
static uint8_t bin2bcd(uint8_t bin)
Convert a binary value to binary coded decimal (BCD 8421).
Definition util.h:739
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:868
static void byteswp(void *a, void *b, size_t size)
byte by byte swap.
Definition util.h:663
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:854
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:727
int char2hex(char c, uint8_t *x)
Convert a single character into a hexadecimal nibble.
int bitmask_find_gap(uint32_t mask, size_t num_bits, size_t total_bits, bool first_match)
Find number of contiguous bits which are not set in the bit mask (32 bits).
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:914
static bool util_memeq(const void *m1, const void *m2, size_t n)
Compare memory areas.
Definition util.h:896
static bool is_power_of_two(unsigned int x)
Is x a power of two?
Definition util.h:581
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:765
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
#define ALWAYS_INLINE
Definition common.h:161
__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)
void * memcpy(void *ZRESTRICT d, const void *ZRESTRICT s, size_t n)
Macros to abstract toolchain specific capabilities.
Macro utilities.