Zephyr API Documentation 4.2.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))
375
376#define _minmax_unique(op, a, b, ua, ub) ({ \
377 __typeof__(a) ua = (a); \
378 __typeof__(b) ub = (b); \
379 op(ua, ub); \
380 })
381
382#define _minmax_cnt(op, a, b, cnt) \
383 _minmax_unique(op, a, b, UTIL_CAT(_value_a_, cnt), UTIL_CAT(_value_b_, cnt))
384
385#define _minmax3_unique(op, a, b, c, ua, ub, uc) ({ \
386 __typeof__(a) ua = (a); \
387 __typeof__(b) ub = (b); \
388 __typeof__(c) uc = (c); \
389 op(ua, op(ub, uc)); \
390 })
391
392#define _minmax3_cnt(op, a, b, c, cnt) \
393 _minmax3_unique(op, a, b, c, \
394 UTIL_CAT(_value_a_, cnt), \
395 UTIL_CAT(_value_b_, cnt), \
396 UTIL_CAT(_value_c_, cnt))
400
401#ifndef MAX
413#define MAX(a, b) Z_INTERNAL_MAX(a, b)
414#endif
415
416#ifndef __cplusplus
426#define max(a, b) _minmax_cnt(Z_INTERNAL_MAX, a, b, __COUNTER__)
427#endif
428
434#define max3(a, b, c) _minmax3_cnt(Z_INTERNAL_MAX, a, b, c, __COUNTER__)
435
436#ifndef MIN
448#define MIN(a, b) Z_INTERNAL_MIN(a, b)
449#endif
450
451#ifndef __cplusplus
457#define min(a, b) _minmax_cnt(Z_INTERNAL_MIN, a, b, __COUNTER__)
458#endif
459
465#define min3(a, b, c) _minmax3_cnt(Z_INTERNAL_MIN, a, b, c, __COUNTER__)
466
467
468#ifndef MAX_FROM_LIST
474#define Z_MAX_1(a) a
475
485#define Z_MAX_2(a, b) ((a) > (b) ? (a) : (b))
486
495#define Z_MAX_3(a, b, c) Z_MAX_2(a, Z_MAX_2(b, c))
496
506#define Z_MAX_4(a, b, c, d) Z_MAX_2(Z_MAX_2(a, b), Z_MAX_2(c, d))
507
512#define Z_MAX_5(a, b, c, d, e) Z_MAX_2(Z_MAX_4(a, b, c, d), e)
513
518#define Z_MAX_6(a, b, c, d, e, f) Z_MAX_2(Z_MAX_5(a, b, c, d, e), f)
519
524#define Z_MAX_7(a, b, c, d, e, f, g) Z_MAX_2(Z_MAX_6(a, b, c, d, e, f), g)
525
530#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)
531
536#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)
537
542#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)
543
566#define Z_GET_MAX_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, NAME, ...) NAME
567
584#define MAX_FROM_LIST(...) \
585 Z_GET_MAX_MACRO(__VA_ARGS__, Z_MAX_10, Z_MAX_9, Z_MAX_8, Z_MAX_7, Z_MAX_6, Z_MAX_5, \
586 Z_MAX_4, Z_MAX_3, Z_MAX_2, Z_MAX_1)(__VA_ARGS__)
587#endif
588
589#ifndef CLAMP
602#define CLAMP(val, low, high) (((val) <= (low)) ? (low) : Z_INTERNAL_MIN(val, high))
603#endif
604
605#ifndef __cplusplus
611#define clamp(val, low, high) ({ \
612 /* random suffix to avoid naming conflict */ \
613 __typeof__(val) _value_val_ = (val); \
614 __typeof__(low) _value_low_ = (low); \
615 __typeof__(high) _value_high_ = (high); \
616 (_value_val_ < _value_low_) ? _value_low_ : \
617 (_value_val_ > _value_high_) ? _value_high_ : \
618 _value_val_; \
619 })
620#endif
621
634#define IN_RANGE(val, min, max) ((val) >= (min) && (val) <= (max))
635
650int bitmask_find_gap(uint32_t mask, size_t num_bits, size_t total_bits, bool first_match);
651
657static inline bool is_power_of_two(unsigned int x)
658{
659 return IS_POWER_OF_TWO(x);
660}
661
681static ALWAYS_INLINE bool is_null_no_warn(void *p)
682{
683 return p == NULL;
684}
685
694{
695 int64_t sign_ext;
696
697 if (shift == 0U) {
698 return value;
699 }
700
701 /* extract sign bit */
702 sign_ext = (value >> 63) & 1;
703
704 /* make all bits of sign_ext be the same as the value's sign bit */
705 sign_ext = -sign_ext;
706
707 /* shift value and fill opened bit positions with sign bit */
708 return (value >> shift) | (sign_ext << (64 - shift));
709}
710
720static inline void bytecpy(void *dst, const void *src, size_t size)
721{
722 size_t i;
723
724 for (i = 0; i < size; ++i) {
725 ((volatile uint8_t *)dst)[i] = ((volatile const uint8_t *)src)[i];
726 }
727}
728
739static inline void byteswp(void *a, void *b, size_t size)
740{
741 uint8_t t;
742 uint8_t *aa = (uint8_t *)a;
743 uint8_t *bb = (uint8_t *)b;
744
745 for (; size > 0; --size) {
746 t = *aa;
747 *aa++ = *bb;
748 *bb++ = t;
749 }
750}
751
760int char2hex(char c, uint8_t *x);
761
770int hex2char(uint8_t x, char *c);
771
782size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen);
783
794size_t hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen);
795
803static inline uint8_t bcd2bin(uint8_t bcd)
804{
805 return ((10 * (bcd >> 4)) + (bcd & 0x0F));
806}
807
815static inline uint8_t bin2bcd(uint8_t bin)
816{
817 return (((bin / 10) << 4) | (bin % 10));
818}
819
833uint8_t u8_to_dec(char *buf, uint8_t buflen, uint8_t value);
834
841static inline int32_t sign_extend(uint32_t value, uint8_t index)
842{
843 __ASSERT_NO_MSG(index <= 31);
844
845 uint8_t shift = 31 - index;
846
847 return (int32_t)(value << shift) >> shift;
848}
849
856static inline int64_t sign_extend_64(uint64_t value, uint8_t index)
857{
858 __ASSERT_NO_MSG(index <= 63);
859
860 uint8_t shift = 63 - index;
861
862 return (int64_t)(value << shift) >> shift;
863}
864
865#define __z_log2d(x) (32 - __builtin_clz(x) - 1)
866#define __z_log2q(x) (64 - __builtin_clzll(x) - 1)
867#define __z_log2(x) (sizeof(__typeof__(x)) > 4 ? __z_log2q(x) : __z_log2d(x))
868
879#define LOG2(x) ((x) < 1 ? -1 : __z_log2(x))
880
891#define LOG2CEIL(x) ((x) <= 1 ? 0 : __z_log2((x) - 1) + 1)
892
905#define NHPOT(x) ((x) < 1 ? 1 : ((x) > (1ULL << 63) ? 0 : 1ULL << LOG2CEIL(x)))
906
919#define Z_DETECT_POINTER_OVERFLOW(addr, buflen) \
920 (((buflen) != 0) && ((UINTPTR_MAX - (uintptr_t)(addr)) <= ((uintptr_t)((buflen) - 1))))
921
930static inline void mem_xor_n(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, size_t len)
931{
932 while (len--) {
933 *dst++ = *src1++ ^ *src2++;
934 }
935}
936
944static inline void mem_xor_32(uint8_t dst[4], const uint8_t src1[4], const uint8_t src2[4])
945{
946 mem_xor_n(dst, src1, src2, 4U);
947}
948
956static inline void mem_xor_128(uint8_t dst[16], const uint8_t src1[16], const uint8_t src2[16])
957{
958 mem_xor_n(dst, src1, src2, 16);
959}
960
972static inline bool util_memeq(const void *m1, const void *m2, size_t n)
973{
974 return memcmp(m1, m2, n) == 0;
975}
976
990static inline bool util_eq(const void *m1, size_t len1, const void *m2, size_t len2)
991{
992 return len1 == len2 && (m1 == m2 || util_memeq(m1, m2, len1));
993}
994
1001static inline size_t sys_count_bits(const void *value, size_t len)
1002{
1003 size_t cnt = 0U;
1004 size_t i = 0U;
1005
1006#ifdef POPCOUNT
1007 for (; i < len / sizeof(unsigned int); i++) {
1008 unsigned int val;
1009 (void)memcpy(&val, (const uint8_t *)value + i * sizeof(unsigned int),
1010 sizeof(unsigned int));
1011
1012 cnt += POPCOUNT(val);
1013 }
1014 i *= sizeof(unsigned int); /* convert to a uint8_t index for the remainder (if any) */
1015#endif
1016
1017 for (; i < len; i++) {
1018 uint8_t value_u8 = ((const uint8_t *)value)[i];
1019
1020 /* Implements Brian Kernighan’s Algorithm to count bits */
1021 while (value_u8) {
1022 value_u8 &= (value_u8 - 1);
1023 cnt++;
1024 }
1025 }
1026
1027 return cnt;
1028}
1029
1030#ifdef __cplusplus
1031}
1032#endif
1033
1034/* This file must be included at the end of the !_ASMLANGUAGE guard.
1035 * It depends on macros defined in this file above which cannot be forward declared.
1036 */
1037#include <zephyr/sys/time_units.h>
1038
1039#endif /* !_ASMLANGUAGE */
1040
1042#ifdef _LINKER
1043/* This is used in linker scripts so need to avoid type casting there */
1044#define KB(x) ((x) << 10)
1045#else
1046#define KB(x) (((size_t)(x)) << 10)
1047#endif
1049#define MB(x) (KB(x) << 10)
1051#define GB(x) (MB(x) << 10)
1052
1054#define KHZ(x) ((x) * 1000)
1056#define MHZ(x) (KHZ(x) * 1000)
1057
1070#if defined(CONFIG_ARCH_POSIX)
1071#define Z_SPIN_DELAY(t) k_busy_wait(t)
1072#else
1073#define Z_SPIN_DELAY(t)
1074#endif
1075
1091#define WAIT_FOR(expr, timeout, delay_stmt) \
1092 ({ \
1093 uint32_t _wf_cycle_count = k_us_to_cyc_ceil32(timeout); \
1094 uint32_t _wf_start = k_cycle_get_32(); \
1095 while (!(expr) && (_wf_cycle_count > (k_cycle_get_32() - _wf_start))) { \
1096 delay_stmt; \
1097 Z_SPIN_DELAY(10); \
1098 } \
1099 (expr); \
1100 })
1101
1105
1106#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:856
static int64_t arithmetic_shift_right(int64_t value, uint8_t shift)
Arithmetic shift right.
Definition util.h:693
static size_t sys_count_bits(const void *value, size_t len)
Returns the number of bits set in a value.
Definition util.h:1001
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:720
#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:681
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:956
static uint8_t bin2bcd(uint8_t bin)
Convert a binary value to binary coded decimal (BCD 8421).
Definition util.h:815
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:944
static void byteswp(void *a, void *b, size_t size)
byte by byte swap.
Definition util.h:739
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:930
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:803
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:990
static bool util_memeq(const void *m1, const void *m2, size_t n)
Compare memory areas.
Definition util.h:972
static bool is_power_of_two(unsigned int x)
Is x a power of two?
Definition util.h:657
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:841
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:160
__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.