Zephyr Project
A Scalable Open Source RTOS for IoT Embedded Devices
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 
17 #include <sys/util_macro.h>
18 
19 /* needs to be outside _ASMLANGUAGE so 'true' and 'false' can turn
20  * into '1' and '0' for asm or linker scripts
21  */
22 #include <stdbool.h>
23 
24 #ifndef _ASMLANGUAGE
25 
26 #include <zephyr/types.h>
27 #include <stddef.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
39 #define POINTER_TO_UINT(x) ((uintptr_t) (x))
40 
41 #define UINT_TO_POINTER(x) ((void *) (uintptr_t) (x))
42 
43 #define POINTER_TO_INT(x) ((intptr_t) (x))
44 
45 #define INT_TO_POINTER(x) ((void *) (intptr_t) (x))
46 
47 #if !(defined(__CHAR_BIT__) && defined(__SIZEOF_LONG__))
48 # error Missing required predefined macros for BITS_PER_LONG calculation
49 #endif
50 
52 #define BITS_PER_LONG (__CHAR_BIT__ * __SIZEOF_LONG__)
53 
58 #define GENMASK(h, l) \
59  (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
60 
62 #define ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - 2 * !(cond)]) - 1)
63 
64 #if defined(__cplusplus)
65 
66 /* The built-in function used below for type checking in C is not
67  * supported by GNU C++.
68  */
69 #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
70 
71 #else /* __cplusplus */
72 
78 #define IS_ARRAY(array) \
79  ZERO_OR_COMPILE_ERROR( \
80  !__builtin_types_compatible_p(__typeof__(array), \
81  __typeof__(&(array)[0])))
82 
92 #define ARRAY_SIZE(array) \
93  ((long) (IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0]))))
94 
95 #endif /* __cplusplus */
96 
107 #define PART_OF_ARRAY(array, ptr) \
108  ((ptr) && ((ptr) >= &array[0] && (ptr) < &array[ARRAY_SIZE(array)]))
109 
131 #define CONTAINER_OF(ptr, type, field) \
132  ((type *)(((char *)(ptr)) - offsetof(type, field)))
133 
138 #define ROUND_UP(x, align) \
139  (((unsigned long)(x) + ((unsigned long)(align) - 1)) & \
140  ~((unsigned long)(align) - 1))
141 
146 #define ROUND_DOWN(x, align) \
147  ((unsigned long)(x) & ~((unsigned long)(align) - 1))
148 
150 #define WB_UP(x) ROUND_UP(x, sizeof(void *))
151 
153 #define WB_DN(x) ROUND_DOWN(x, sizeof(void *))
154 
158 #define ceiling_fraction(numerator, divider) \
159  (((numerator) + ((divider) - 1)) / (divider))
160 
166 #ifndef MAX
167 /* Use Z_MAX for a GCC-only, single evaluation version */
168 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
169 #endif
170 
176 #ifndef MIN
177 /* Use Z_MIN for a GCC-only, single evaluation version */
178 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
179 #endif
180 
186 #ifndef CLAMP
187 /* Use Z_CLAMP for a GCC-only, single evaluation version */
188 #define CLAMP(val, low, high) (((val) <= (low)) ? (low) : MIN(val, high))
189 #endif
190 
196 static inline bool is_power_of_two(unsigned int x)
197 {
198  return (x != 0U) && ((x & (x - 1U)) == 0U);
199 }
200 
208 static inline int64_t arithmetic_shift_right(int64_t value, uint8_t shift)
209 {
210  int64_t sign_ext;
211 
212  if (shift == 0U) {
213  return value;
214  }
215 
216  /* extract sign bit */
217  sign_ext = (value >> 63) & 1;
218 
219  /* make all bits of sign_ext be the same as the value's sign bit */
220  sign_ext = -sign_ext;
221 
222  /* shift value and fill opened bit positions with sign bit */
223  return (value >> shift) | (sign_ext << (64 - shift));
224 }
225 
234 int char2hex(char c, uint8_t *x);
235 
244 int hex2char(uint8_t x, char *c);
245 
256 size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen);
257 
268 size_t hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen);
269 
283 uint8_t u8_to_dec(char *buf, uint8_t buflen, uint8_t value);
284 
285 #ifdef __cplusplus
286 }
287 #endif
288 
289 #endif /* !_ASMLANGUAGE */
290 
292 #ifdef _LINKER
293 /* This is used in linker scripts so need to avoid type casting there */
294 #define KB(x) ((x) << 10)
295 #else
296 #define KB(x) (((size_t)x) << 10)
297 #endif
298 
299 #define MB(x) (KB(x) << 10)
300 
301 #define GB(x) (MB(x) << 10)
302 
304 #define KHZ(x) ((x) * 1000)
305 
306 #define MHZ(x) (KHZ(x) * 1000)
307 
308 #ifndef BIT
309 #if defined(_ASMLANGUAGE)
310 #define BIT(n) (1 << (n))
311 #else
312 
316 #define BIT(n) (1UL << (n))
317 #endif
318 #endif
319 
321 #define BIT64(_n) (1ULL << (_n))
322 
333 #define WRITE_BIT(var, bit, set) \
334  ((var) = (set) ? ((var) | BIT(bit)) : ((var) & ~BIT(bit)))
335 
340 #define BIT_MASK(n) (BIT(n) - 1UL)
341 
346 #define BIT64_MASK(n) (BIT64(n) - 1ULL)
347 
352 #endif /* ZEPHYR_INCLUDE_SYS_UTIL_H_ */
util_macro.h
Macro utilities.
stdbool.h
uint8_t
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
types.h
bin2hex
size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen)
Convert a binary array into string representation.
hex
uint32_t hex
Definition: printk.c:77
arithmetic_shift_right
static int64_t arithmetic_shift_right(int64_t value, uint8_t shift)
Arithmetic shift right.
Definition: util.h:208
c
char c
Definition: printk.c:71
u8_to_dec
uint8_t u8_to_dec(char *buf, uint8_t buflen, uint8_t value)
Convert a uint8_t into a decimal string representation.
hex2char
int hex2char(uint8_t x, char *c)
Convert a single hexadecimal nibble into a character.
is_power_of_two
static bool is_power_of_two(unsigned int x)
Is x a power of two?
Definition: util.h:196
char2hex
int char2hex(char c, uint8_t *x)
Convert a single character into a hexadecimal nibble.
hex2bin
size_t hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen)
Convert a hexadecimal string into a binary array.
int64_t
__INT64_TYPE__ int64_t
Definition: stdint.h:45