@@ -46,16 +46,16 @@ extern "C" {
46
46
*/
47
47
48
48
/** @brief Cast @p x, a pointer, to an unsigned integer. */
49
- #define POINTER_TO_UINT (x ) ((uintptr_t) (x))
49
+ #define POINTER_TO_UINT (x ) ((uintptr_t)(x))
50
50
/** @brief Cast @p x, an unsigned integer, to a <tt>void*</tt>. */
51
- #define UINT_TO_POINTER (x ) ((void *) (uintptr_t) (x))
51
+ #define UINT_TO_POINTER (x ) ((void *)(uintptr_t)(x))
52
52
/** @brief Cast @p x, a pointer, to a signed integer. */
53
- #define POINTER_TO_INT (x ) ((intptr_t) (x))
53
+ #define POINTER_TO_INT (x ) ((intptr_t)(x))
54
54
/** @brief Cast @p x, a signed integer, to a <tt>void*</tt>. */
55
- #define INT_TO_POINTER (x ) ((void *) (intptr_t) (x))
55
+ #define INT_TO_POINTER (x ) ((void *)(intptr_t)(x))
56
56
57
57
#if !(defined(__CHAR_BIT__ ) && defined(__SIZEOF_LONG__ ) && defined(__SIZEOF_LONG_LONG__ ))
58
- # error Missing required predefined macros for BITS_PER_LONG calculation
58
+ #error Missing required predefined macros for BITS_PER_LONG calculation
59
59
#endif
60
60
61
61
/** Number of bits in a byte. */
@@ -68,27 +68,25 @@ extern "C" {
68
68
#define NIBBLES_PER_BYTE (BITS_PER_BYTE / BITS_PER_NIBBLE)
69
69
70
70
/** Number of bits in a long int. */
71
- #define BITS_PER_LONG (__CHAR_BIT__ * __SIZEOF_LONG__)
71
+ #define BITS_PER_LONG (__CHAR_BIT__ * __SIZEOF_LONG__)
72
72
73
73
/** Number of bits in a long long int. */
74
- #define BITS_PER_LONG_LONG (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
74
+ #define BITS_PER_LONG_LONG (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
75
75
76
76
/**
77
77
* @brief Create a contiguous bitmask starting at bit position @p l
78
78
* and ending at position @p h.
79
79
*/
80
- #define GENMASK (h , l ) \
81
- (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
80
+ #define GENMASK (h , l ) (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
82
81
83
82
/**
84
83
* @brief Create a contiguous 64-bit bitmask starting at bit position @p l
85
84
* and ending at position @p h.
86
85
*/
87
- #define GENMASK64 (h , l ) \
88
- (((~0ULL) - (1ULL << (l)) + 1) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
86
+ #define GENMASK64 (h , l ) (((~0ULL) - (1ULL << (l)) + 1) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
89
87
90
88
/** @brief 0 if @p cond is true-ish; causes a compile error otherwise. */
91
- #define ZERO_OR_COMPILE_ERROR (cond ) ((int) sizeof(char[1 - 2 * !(cond)]) - 1)
89
+ #define ZERO_OR_COMPILE_ERROR (cond ) ((int)sizeof(char[1 - 2 * !(cond)]) - 1)
92
90
93
91
#if defined(__cplusplus )
94
92
@@ -104,10 +102,9 @@ extern "C" {
104
102
*
105
103
* This macro is available only from C, not C++.
106
104
*/
107
- #define IS_ARRAY (array ) \
108
- ZERO_OR_COMPILE_ERROR( \
109
- !__builtin_types_compatible_p(__typeof__(array), \
110
- __typeof__(&(array)[0])))
105
+ #define IS_ARRAY (array ) \
106
+ ZERO_OR_COMPILE_ERROR( \
107
+ !__builtin_types_compatible_p(__typeof__(array), __typeof__(&(array)[0])))
111
108
112
109
/**
113
110
* @brief Number of elements in the given @p array
@@ -118,8 +115,7 @@ extern "C" {
118
115
*
119
116
* In C, passing a pointer as @p array causes a compile error.
120
117
*/
121
- #define ARRAY_SIZE (array ) \
122
- ((size_t) (IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0]))))
118
+ #define ARRAY_SIZE (array ) ((size_t)(IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0]))))
123
119
124
120
#endif /* __cplusplus */
125
121
@@ -140,10 +136,11 @@ extern "C" {
140
136
* It is specially useful for cases where flexible arrays are
141
137
* used in unions or are not the last element in the struct.
142
138
*/
143
- #define FLEXIBLE_ARRAY_DECLARE (type , name ) \
144
- struct { \
145
- struct { } __unused_##name; \
146
- type name[]; \
139
+ #define FLEXIBLE_ARRAY_DECLARE (type , name ) \
140
+ struct { \
141
+ struct { \
142
+ } __unused_##name; \
143
+ type name[]; \
147
144
}
148
145
149
146
/**
@@ -161,7 +158,7 @@ extern "C" {
161
158
* @return 1 if @p ptr is part of @p array, 0 otherwise
162
159
*/
163
160
#define IS_ARRAY_ELEMENT (array , ptr ) \
164
- ((ptr) && POINTER_TO_UINT(array) <= POINTER_TO_UINT(ptr) && \
161
+ ((ptr) && POINTER_TO_UINT(array) <= POINTER_TO_UINT(ptr) && \
165
162
POINTER_TO_UINT(ptr) < POINTER_TO_UINT(&(array)[ARRAY_SIZE(array)]) && \
166
163
(POINTER_TO_UINT(ptr) - POINTER_TO_UINT(array)) % sizeof((array)[0]) == 0)
167
164
@@ -253,9 +250,8 @@ extern "C" {
253
250
* @brief Validate CONTAINER_OF parameters, only applies to C mode.
254
251
*/
255
252
#ifndef __cplusplus
256
- #define CONTAINER_OF_VALIDATE (ptr , type , field ) \
257
- BUILD_ASSERT(SAME_TYPE(*(ptr), ((type *)0)->field) || \
258
- SAME_TYPE(*(ptr), void), \
253
+ #define CONTAINER_OF_VALIDATE (ptr , type , field ) \
254
+ BUILD_ASSERT(SAME_TYPE(*(ptr), ((type *)0)->field) || SAME_TYPE(*(ptr), void), \
259
255
"pointer type mismatch in CONTAINER_OF");
260
256
#else
261
257
#define CONTAINER_OF_VALIDATE (ptr , type , field )
@@ -282,10 +278,10 @@ extern "C" {
282
278
* @param field the name of the field within the struct @p ptr points to
283
279
* @return a pointer to the structure that contains @p ptr
284
280
*/
285
- #define CONTAINER_OF (ptr , type , field ) \
286
- ({ \
287
- CONTAINER_OF_VALIDATE(ptr, type, field) \
288
- ((type *)(((char *)(ptr)) - offsetof(type, field))); \
281
+ #define CONTAINER_OF (ptr , type , field ) \
282
+ ({ \
283
+ CONTAINER_OF_VALIDATE(ptr, type, field) \
284
+ ((type *)(((char *)(ptr)) - offsetof(type, field))); \
289
285
})
290
286
291
287
/**
@@ -309,8 +305,7 @@ extern "C" {
309
305
*
310
306
* @return Concatenated token.
311
307
*/
312
- #define CONCAT (...) \
313
- UTIL_CAT(_CONCAT_, NUM_VA_ARGS_LESS_1(__VA_ARGS__))(__VA_ARGS__)
308
+ #define CONCAT (...) UTIL_CAT(_CONCAT_, NUM_VA_ARGS_LESS_1(__VA_ARGS__))(__VA_ARGS__)
314
309
315
310
/**
316
311
* @brief Check if @p ptr is aligned to @p align alignment
@@ -320,14 +315,14 @@ extern "C" {
320
315
/**
321
316
* @brief Value of @p x rounded up to the next multiple of @p align.
322
317
*/
323
- #define ROUND_UP (x , align ) \
324
- ((((unsigned long)(x) + ((unsigned long)(align) - 1)) / \
325
- (unsigned long)(align)) * (unsigned long)(align))
318
+ #define ROUND_UP (x , align ) \
319
+ ((((unsigned long)(x) + ((unsigned long)(align) - 1)) / (unsigned long)(align)) * \
320
+ (unsigned long)(align))
326
321
327
322
/**
328
323
* @brief Value of @p x rounded down to the previous multiple of @p align.
329
324
*/
330
- #define ROUND_DOWN (x , align ) \
325
+ #define ROUND_DOWN (x , align ) \
331
326
(((unsigned long)(x) / (unsigned long)(align)) * (unsigned long)(align))
332
327
333
328
/** @brief Value of @p x rounded up to the next word boundary. */
@@ -690,7 +685,7 @@ char *utf8_lcpy(char *dst, const char *src, size_t n);
690
685
691
686
#define __z_log2d (x ) (32 - __builtin_clz(x) - 1)
692
687
#define __z_log2q (x ) (64 - __builtin_clzll(x) - 1)
693
- #define __z_log2 (x ) (sizeof(__typeof__(x)) > 4 ? __z_log2q(x) : __z_log2d(x))
688
+ #define __z_log2 (x ) (sizeof(__typeof__(x)) > 4 ? __z_log2q(x) : __z_log2d(x))
694
689
695
690
/**
696
691
* @brief Compute log2(x)
@@ -714,7 +709,7 @@ char *utf8_lcpy(char *dst, const char *src, size_t n);
714
709
*
715
710
* @return ceil(log2(x)) when 1 <= x <= max(type(x)), 0 when x < 1
716
711
*/
717
- #define LOG2CEIL (x ) ((x) <= 1 ? 0 : __z_log2((x)- 1) + 1)
712
+ #define LOG2CEIL (x ) ((x) <= 1 ? 0 : __z_log2((x) - 1) + 1)
718
713
719
714
/**
720
715
* @brief Compute next highest power of two
@@ -728,7 +723,7 @@ char *utf8_lcpy(char *dst, const char *src, size_t n);
728
723
*
729
724
* @return 2^ceil(log2(x)) or 0 if 2^ceil(log2(x)) would saturate 64-bits
730
725
*/
731
- #define NHPOT (x ) ((x) < 1 ? 1 : ((x) > (1ULL<< 63) ? 0 : 1ULL << LOG2CEIL(x)))
726
+ #define NHPOT (x ) ((x) < 1 ? 1 : ((x) > (1ULL << 63) ? 0 : 1ULL << LOG2CEIL(x)))
732
727
733
728
/**
734
729
* @brief Determine if a buffer exceeds highest address
@@ -742,9 +737,8 @@ char *utf8_lcpy(char *dst, const char *src, size_t n);
742
737
*
743
738
* @return true if pointer overflow detected, false otherwise
744
739
*/
745
- #define Z_DETECT_POINTER_OVERFLOW (addr , buflen ) \
746
- (((buflen) != 0) && \
747
- ((UINTPTR_MAX - (uintptr_t)(addr)) <= ((uintptr_t)((buflen) - 1))))
740
+ #define Z_DETECT_POINTER_OVERFLOW (addr , buflen ) \
741
+ (((buflen) != 0) && ((UINTPTR_MAX - (uintptr_t)(addr)) <= ((uintptr_t)((buflen) - 1))))
748
742
749
743
/**
750
744
* @brief XOR n bytes
@@ -819,6 +813,39 @@ static inline bool util_eq(const void *m1, size_t len1, const void *m2, size_t l
819
813
return len1 == len2 && (m1 == m2 || util_memeq (m1 , m2 , len1 ));
820
814
}
821
815
816
+ /**
817
+ * @brief Returns the number of bits set in a value
818
+ *
819
+ * @param value The value to count number of bits set of
820
+ * @param len The number of octets in @p value
821
+ */
822
+ static inline size_t zephyr_count_bits (const void * value , size_t len )
823
+ {
824
+ size_t cnt = 0U ;
825
+ size_t i = 0U ;
826
+
827
+ #ifdef POPCOUNT
828
+ for (; i < len / sizeof (unsigned int ); i ++ ) {
829
+ unsigned int val = ((const unsigned int * )value )[i ];
830
+
831
+ cnt += POPCOUNT (val );
832
+ }
833
+ i *= sizeof (unsigned int ); /* convert to a uint8_t index for the remainder (if any) */
834
+ #endif
835
+
836
+ for (; i < len ; i ++ ) {
837
+ uint8_t value_u8 = ((const uint8_t * )value )[i ];
838
+
839
+ /* Implements Brian Kernighan’s Algorithm to count bits */
840
+ while (value_u8 ) {
841
+ value_u8 &= (value_u8 - 1 );
842
+ cnt ++ ;
843
+ }
844
+ }
845
+
846
+ return cnt ;
847
+ }
848
+
822
849
#ifdef __cplusplus
823
850
}
824
851
#endif
0 commit comments