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) \
81 (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
82
87#define GENMASK64(h, l) \
88 (((~0ULL) - (1ULL << (l)) + 1) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
89
91#define ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - (2 * !(cond))]) - 1)
92
93#if defined(__cplusplus)
94
95/* The built-in function used below for type checking in C is not
96 * supported by GNU C++.
97 */
98#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
99
100#else /* __cplusplus */
101
107#define IS_ARRAY(array) \
108 ZERO_OR_COMPILE_ERROR( \
109 !__builtin_types_compatible_p(__typeof__(array), \
110 __typeof__(&(array)[0])))
111
121#define ARRAY_SIZE(array) \
122 ((size_t) (IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0]))))
123
124#endif /* __cplusplus */
125
143#define FLEXIBLE_ARRAY_DECLARE(type, name) \
144 struct { \
145 struct { } __unused_##name; \
146 type name[]; \
147 }
148
163#define IS_ARRAY_ELEMENT(array, ptr) \
164 ((ptr) && POINTER_TO_UINT(array) <= POINTER_TO_UINT(ptr) && \
165 POINTER_TO_UINT(ptr) < POINTER_TO_UINT(&(array)[ARRAY_SIZE(array)]) && \
166 (POINTER_TO_UINT(ptr) - POINTER_TO_UINT(array)) % sizeof((array)[0]) == 0)
167
182#define ARRAY_INDEX(array, ptr) \
183 ({ \
184 __ASSERT_NO_MSG(IS_ARRAY_ELEMENT(array, ptr)); \
185 (__typeof__((array)[0]) *)(ptr) - (array); \
186 })
187
198#define PART_OF_ARRAY(array, ptr) \
199 ((ptr) && POINTER_TO_UINT(array) <= POINTER_TO_UINT(ptr) && \
200 POINTER_TO_UINT(ptr) < POINTER_TO_UINT(&(array)[ARRAY_SIZE(array)]))
201
219#define ARRAY_INDEX_FLOOR(array, ptr) \
220 ({ \
221 __ASSERT_NO_MSG(PART_OF_ARRAY(array, ptr)); \
222 (POINTER_TO_UINT(ptr) - POINTER_TO_UINT(array)) / sizeof((array)[0]); \
223 })
224
231#define ARRAY_FOR_EACH(array, idx) for (size_t idx = 0; (idx) < ARRAY_SIZE(array); ++(idx))
232
239#define ARRAY_FOR_EACH_PTR(array, ptr) \
240 for (__typeof__(*(array)) *ptr = (array); (size_t)((ptr) - (array)) < ARRAY_SIZE(array); \
241 ++(ptr))
242
250#define SAME_TYPE(a, b) __builtin_types_compatible_p(__typeof__(a), __typeof__(b))
251
255#ifndef __cplusplus
256#define CONTAINER_OF_VALIDATE(ptr, type, field) \
257 BUILD_ASSERT(SAME_TYPE(*(ptr), ((type *)0)->field) || \
258 SAME_TYPE(*(ptr), void), \
259 "pointer type mismatch in CONTAINER_OF");
260#else
261#define CONTAINER_OF_VALIDATE(ptr, type, field)
262#endif
263
285#define CONTAINER_OF(ptr, type, field) \
286 ({ \
287 CONTAINER_OF_VALIDATE(ptr, type, field) \
288 ((type *)(((char *)(ptr)) - offsetof(type, field))); \
289 })
290
299#define SIZEOF_FIELD(type, member) sizeof((((type *)0)->member))
300
312#define CONCAT(...) \
313 UTIL_CAT(_CONCAT_, NUM_VA_ARGS_LESS_1(__VA_ARGS__))(__VA_ARGS__)
314
318#define IS_ALIGNED(ptr, align) (((uintptr_t)(ptr)) % (align) == 0)
319
323#define ROUND_UP(x, align) \
324 ((((unsigned long)(x) + ((unsigned long)(align) - 1)) / \
325 (unsigned long)(align)) * (unsigned long)(align))
326
330#define ROUND_DOWN(x, align) \
331 (((unsigned long)(x) / (unsigned long)(align)) * (unsigned long)(align))
332
334#define WB_UP(x) ROUND_UP(x, sizeof(void *))
335
337#define WB_DN(x) ROUND_DOWN(x, sizeof(void *))
338
353#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
354
370#define DIV_ROUND_CLOSEST(n, d) \
371 (((((__typeof__(n))-1) < 0) && (((__typeof__(d))-1) < 0) && ((n) < 0) ^ ((d) < 0)) \
372 ? ((n) - ((d) / 2)) / (d) \
373 : ((n) + ((d) / 2)) / (d))
374
375#ifndef MAX
387#define MAX(a, b) (((a) > (b)) ? (a) : (b))
388#endif
389
390#ifndef MIN
402#define MIN(a, b) (((a) < (b)) ? (a) : (b))
403#endif
404
405#ifndef MAX_FROM_LIST
411#define Z_MAX_1(a) a
412
422#define Z_MAX_2(a, b) ((a) > (b) ? (a) : (b))
423
432#define Z_MAX_3(a, b, c) Z_MAX_2(a, Z_MAX_2(b, c))
433
443#define Z_MAX_4(a, b, c, d) Z_MAX_2(Z_MAX_2(a, b), Z_MAX_2(c, d))
444
449#define Z_MAX_5(a, b, c, d, e) Z_MAX_2(Z_MAX_4(a, b, c, d), e)
450
455#define Z_MAX_6(a, b, c, d, e, f) Z_MAX_2(Z_MAX_5(a, b, c, d, e), f)
456
461#define Z_MAX_7(a, b, c, d, e, f, g) Z_MAX_2(Z_MAX_6(a, b, c, d, e, f), g)
462
467#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)
468
473#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)
474
479#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)
480
503#define Z_GET_MAX_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, NAME, ...) NAME
504
521#define MAX_FROM_LIST(...) \
522 Z_GET_MAX_MACRO(__VA_ARGS__, Z_MAX_10, Z_MAX_9, Z_MAX_8, Z_MAX_7, Z_MAX_6, Z_MAX_5, \
523 Z_MAX_4, Z_MAX_3, Z_MAX_2, Z_MAX_1)(__VA_ARGS__)
524#endif
525
526#ifndef CLAMP
539#define CLAMP(val, low, high) (((val) <= (low)) ? (low) : MIN(val, high))
540#endif
541
554#define IN_RANGE(val, min, max) ((val) >= (min) && (val) <= (max))
555
570int bitmask_find_gap(uint32_t mask, size_t num_bits, size_t total_bits, bool first_match);
571
577static inline bool is_power_of_two(unsigned int x)
578{
579 return IS_POWER_OF_TWO(x);
580}
581
601static ALWAYS_INLINE bool is_null_no_warn(void *p)
602{
603 return p == NULL;
604}
605
614{
615 int64_t sign_ext;
616
617 if (shift == 0U) {
618 return value;
619 }
620
621 /* extract sign bit */
622 sign_ext = (value >> 63) & 1;
623
624 /* make all bits of sign_ext be the same as the value's sign bit */
625 sign_ext = -sign_ext;
626
627 /* shift value and fill opened bit positions with sign bit */
628 return (value >> shift) | (sign_ext << (64 - shift));
629}
630
640static inline void bytecpy(void *dst, const void *src, size_t size)
641{
642 size_t i;
643
644 for (i = 0; i < size; ++i) {
645 ((volatile uint8_t *)dst)[i] = ((volatile const uint8_t *)src)[i];
646 }
647}
648
659static inline void byteswp(void *a, void *b, size_t size)
660{
661 uint8_t t;
662 uint8_t *aa = (uint8_t *)a;
663 uint8_t *bb = (uint8_t *)b;
664
665 for (; size > 0; --size) {
666 t = *aa;
667 *aa++ = *bb;
668 *bb++ = t;
669 }
670}
671
680int char2hex(char c, uint8_t *x);
681
690int hex2char(uint8_t x, char *c);
691
702size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen);
703
714size_t hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen);
715
723static inline uint8_t bcd2bin(uint8_t bcd)
724{
725 return ((10 * (bcd >> 4)) + (bcd & 0x0F));
726}
727
735static inline uint8_t bin2bcd(uint8_t bin)
736{
737 return (((bin / 10) << 4) | (bin % 10));
738}
739
753uint8_t u8_to_dec(char *buf, uint8_t buflen, uint8_t value);
754
761static inline int32_t sign_extend(uint32_t value, uint8_t index)
762{
763 __ASSERT_NO_MSG(index <= 31);
764
765 uint8_t shift = 31 - index;
766
767 return (int32_t)(value << shift) >> shift;
768}
769
776static inline int64_t sign_extend_64(uint64_t value, uint8_t index)
777{
778 __ASSERT_NO_MSG(index <= 63);
779
780 uint8_t shift = 63 - index;
781
782 return (int64_t)(value << shift) >> shift;
783}
784
785#define __z_log2d(x) (32 - __builtin_clz(x) - 1)
786#define __z_log2q(x) (64 - __builtin_clzll(x) - 1)
787#define __z_log2(x) (sizeof(__typeof__(x)) > 4 ? __z_log2q(x) : __z_log2d(x))
788
799#define LOG2(x) ((x) < 1 ? -1 : __z_log2(x))
800
811#define LOG2CEIL(x) ((x) <= 1 ? 0 : __z_log2((x)-1) + 1)
812
825#define NHPOT(x) ((x) < 1 ? 1 : ((x) > (1ULL<<63) ? 0 : 1ULL << LOG2CEIL(x)))
826
839#define Z_DETECT_POINTER_OVERFLOW(addr, buflen) \
840 (((buflen) != 0) && \
841 ((UINTPTR_MAX - (uintptr_t)(addr)) <= ((uintptr_t)((buflen) - 1))))
842
851static inline void mem_xor_n(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, size_t len)
852{
853 while (len--) {
854 *dst++ = *src1++ ^ *src2++;
855 }
856}
857
865static inline void mem_xor_32(uint8_t dst[4], const uint8_t src1[4], const uint8_t src2[4])
866{
867 mem_xor_n(dst, src1, src2, 4U);
868}
869
877static inline void mem_xor_128(uint8_t dst[16], const uint8_t src1[16], const uint8_t src2[16])
878{
879 mem_xor_n(dst, src1, src2, 16);
880}
881
893static inline bool util_memeq(const void *m1, const void *m2, size_t n)
894{
895 return memcmp(m1, m2, n) == 0;
896}
897
911static inline bool util_eq(const void *m1, size_t len1, const void *m2, size_t len2)
912{
913 return len1 == len2 && (m1 == m2 || util_memeq(m1, m2, len1));
914}
915
922static inline size_t sys_count_bits(const void *value, size_t len)
923{
924 size_t cnt = 0U;
925 size_t i = 0U;
926
927#ifdef POPCOUNT
928 for (; i < len / sizeof(unsigned int); i++) {
929 unsigned int val;
930 (void)memcpy(&val, (const uint8_t *)value + i * sizeof(unsigned int),
931 sizeof(unsigned int));
932
933 cnt += POPCOUNT(val);
934 }
935 i *= sizeof(unsigned int); /* convert to a uint8_t index for the remainder (if any) */
936#endif
937
938 for (; i < len; i++) {
939 uint8_t value_u8 = ((const uint8_t *)value)[i];
940
941 /* Implements Brian Kernighan’s Algorithm to count bits */
942 while (value_u8) {
943 value_u8 &= (value_u8 - 1);
944 cnt++;
945 }
946 }
947
948 return cnt;
949}
950
951#ifdef __cplusplus
952}
953#endif
954
955/* This file must be included at the end of the !_ASMLANGUAGE guard.
956 * It depends on macros defined in this file above which cannot be forward declared.
957 */
959
960#endif /* !_ASMLANGUAGE */
961
963#ifdef _LINKER
964/* This is used in linker scripts so need to avoid type casting there */
965#define KB(x) ((x) << 10)
966#else
967#define KB(x) (((size_t)(x)) << 10)
968#endif
970#define MB(x) (KB(x) << 10)
972#define GB(x) (MB(x) << 10)
973
975#define KHZ(x) ((x) * 1000)
977#define MHZ(x) (KHZ(x) * 1000)
978
991#if defined(CONFIG_ARCH_POSIX)
992#define Z_SPIN_DELAY(t) k_busy_wait(t)
993#else
994#define Z_SPIN_DELAY(t)
995#endif
996
1012#define WAIT_FOR(expr, timeout, delay_stmt) \
1013 ({ \
1014 uint32_t _wf_cycle_count = k_us_to_cyc_ceil32(timeout); \
1015 uint32_t _wf_start = k_cycle_get_32(); \
1016 while (!(expr) && (_wf_cycle_count > (k_cycle_get_32() - _wf_start))) { \
1017 delay_stmt; \
1018 Z_SPIN_DELAY(10); \
1019 } \
1020 (expr); \
1021 })
1022
1026
1027#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:776
static int64_t arithmetic_shift_right(int64_t value, uint8_t shift)
Arithmetic shift right.
Definition util.h:613
static size_t sys_count_bits(const void *value, size_t len)
Returns the number of bits set in a value.
Definition util.h:922
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:640
#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:601
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:877
static uint8_t bin2bcd(uint8_t bin)
Convert a binary value to binary coded decimal (BCD 8421).
Definition util.h:735
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:865
static void byteswp(void *a, void *b, size_t size)
byte by byte swap.
Definition util.h:659
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:851
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:723
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:911
static bool util_memeq(const void *m1, const void *m2, size_t n)
Compare memory areas.
Definition util.h:893
static bool is_power_of_two(unsigned int x)
Is x a power of two?
Definition util.h:577
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:761
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.