Zephyr Project API  3.3.0
A Scalable Open Source RTOS
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
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
30extern "C" {
31#endif
32
39#define POINTER_TO_UINT(x) ((uintptr_t) (x))
41#define UINT_TO_POINTER(x) ((void *) (uintptr_t) (x))
43#define POINTER_TO_INT(x) ((intptr_t) (x))
45#define INT_TO_POINTER(x) ((void *) (intptr_t) (x))
46
47#if !(defined(__CHAR_BIT__) && defined(__SIZEOF_LONG__) && defined(__SIZEOF_LONG_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
55#define BITS_PER_LONG_LONG (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
56
61#define GENMASK(h, l) \
62 (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
63
68#define GENMASK64(h, l) \
69 (((~0ULL) - (1ULL << (l)) + 1) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
70
72#define LSB_GET(value) ((value) & -(value))
73
78#define FIELD_GET(mask, value) (((value) & (mask)) / LSB_GET(mask))
79
85#define FIELD_PREP(mask, value) (((value) * LSB_GET(mask)) & (mask))
86
88#define ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - 2 * !(cond)]) - 1)
89
90#if defined(__cplusplus)
91
92/* The built-in function used below for type checking in C is not
93 * supported by GNU C++.
94 */
95#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
96
97#else /* __cplusplus */
98
104#define IS_ARRAY(array) \
105 ZERO_OR_COMPILE_ERROR( \
106 !__builtin_types_compatible_p(__typeof__(array), \
107 __typeof__(&(array)[0])))
108
118#define ARRAY_SIZE(array) \
119 ((size_t) (IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0]))))
120
121#endif /* __cplusplus */
122
137#define IS_ARRAY_ELEMENT(array, ptr) \
138 ((ptr) && POINTER_TO_UINT(array) <= POINTER_TO_UINT(ptr) && \
139 POINTER_TO_UINT(ptr) < POINTER_TO_UINT(&(array)[ARRAY_SIZE(array)]) && \
140 (POINTER_TO_UINT(ptr) - POINTER_TO_UINT(array)) % sizeof((array)[0]) == 0)
141
156#define ARRAY_INDEX(array, ptr) \
157 ({ \
158 __ASSERT_NO_MSG(IS_ARRAY_ELEMENT(array, ptr)); \
159 (__typeof__((array)[0]) *)(ptr) - (array); \
160 })
161
172#define PART_OF_ARRAY(array, ptr) \
173 ((ptr) && POINTER_TO_UINT(array) <= POINTER_TO_UINT(ptr) && \
174 POINTER_TO_UINT(ptr) < POINTER_TO_UINT(&(array)[ARRAY_SIZE(array)]))
175
193#define ARRAY_INDEX_FLOOR(array, ptr) \
194 ({ \
195 __ASSERT_NO_MSG(PART_OF_ARRAY(array, ptr)); \
196 (POINTER_TO_UINT(ptr) - POINTER_TO_UINT(array)) / sizeof((array)[0]); \
197 })
198
220#define CONTAINER_OF(ptr, type, field) \
221 ((type *)(((char *)(ptr)) - offsetof(type, field)))
222
227#define ROUND_UP(x, align) \
228 (((unsigned long)(x) + ((unsigned long)(align) - 1)) & \
229 ~((unsigned long)(align) - 1))
230
235#define ROUND_DOWN(x, align) \
236 ((unsigned long)(x) & ~((unsigned long)(align) - 1))
237
239#define WB_UP(x) ROUND_UP(x, sizeof(void *))
240
242#define WB_DN(x) ROUND_DOWN(x, sizeof(void *))
243
247#define ceiling_fraction(numerator, divider) \
248 (((numerator) + ((divider) - 1)) / (divider))
249
250#ifndef MAX
262#define MAX(a, b) (((a) > (b)) ? (a) : (b))
263#endif
264
265#ifndef MIN
277#define MIN(a, b) (((a) < (b)) ? (a) : (b))
278#endif
279
280#ifndef CLAMP
293#define CLAMP(val, low, high) (((val) <= (low)) ? (low) : MIN(val, high))
294#endif
295
308#define IN_RANGE(val, min, max) ((val) >= (min) && (val) <= (max))
309
315static inline bool is_power_of_two(unsigned int x)
316{
317 return (x != 0U) && ((x & (x - 1U)) == 0U);
318}
319
328{
329 int64_t sign_ext;
330
331 if (shift == 0U) {
332 return value;
333 }
334
335 /* extract sign bit */
336 sign_ext = (value >> 63) & 1;
337
338 /* make all bits of sign_ext be the same as the value's sign bit */
339 sign_ext = -sign_ext;
340
341 /* shift value and fill opened bit positions with sign bit */
342 return (value >> shift) | (sign_ext << (64 - shift));
343}
344
354static inline void bytecpy(void *dst, const void *src, size_t size)
355{
356 size_t i;
357
358 for (i = 0; i < size; ++i) {
359 ((volatile uint8_t *)dst)[i] = ((volatile const uint8_t *)src)[i];
360 }
361}
362
373static inline void byteswp(void *a, void *b, size_t size)
374{
375 uint8_t t;
376 uint8_t *aa = (uint8_t *)a;
377 uint8_t *bb = (uint8_t *)b;
378
379 for (; size > 0; --size) {
380 t = *aa;
381 *aa++ = *bb;
382 *bb++ = t;
383 }
384}
385
394int char2hex(char c, uint8_t *x);
395
404int hex2char(uint8_t x, char *c);
405
416size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen);
417
428size_t hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen);
429
437static inline uint8_t bcd2bin(uint8_t bcd)
438{
439 return ((10 * (bcd >> 4)) + (bcd & 0x0F));
440}
441
449static inline uint8_t bin2bcd(uint8_t bin)
450{
451 return (((bin / 10) << 4) | (bin % 10));
452}
453
467uint8_t u8_to_dec(char *buf, uint8_t buflen, uint8_t value);
468
493char *utf8_trunc(char *utf8_str);
494
509char *utf8_lcpy(char *dst, const char *src, size_t n);
510
511#ifdef __cplusplus
512}
513#endif
514
515#endif /* !_ASMLANGUAGE */
516
518#ifdef _LINKER
519/* This is used in linker scripts so need to avoid type casting there */
520#define KB(x) ((x) << 10)
521#else
522#define KB(x) (((size_t)x) << 10)
523#endif
525#define MB(x) (KB(x) << 10)
527#define GB(x) (MB(x) << 10)
528
530#define KHZ(x) ((x) * 1000)
532#define MHZ(x) (KHZ(x) * 1000)
533
549#define WAIT_FOR(expr, timeout, delay_stmt) \
550 ({ \
551 uint32_t cycle_count = k_us_to_cyc_ceil32(timeout); \
552 uint32_t start = k_cycle_get_32(); \
553 while (!(expr) && (cycle_count > (k_cycle_get_32() - start))) { \
554 delay_stmt; \
555 } \
556 (expr); \
557 })
558
563#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
char * utf8_trunc(char *utf8_str)
Properly truncate a NULL-terminated UTF-8 string.
static int64_t arithmetic_shift_right(int64_t value, uint8_t shift)
Arithmetic shift right.
Definition: util.h:327
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:354
char * utf8_lcpy(char *dst, const char *src, size_t n)
Copies a UTF-8 encoded string from src to dst.
static uint8_t bin2bcd(uint8_t bin)
Convert a binary value to binary coded decimal (BCD 8421).
Definition: util.h:449
static void byteswp(void *a, void *b, size_t size)
byte by byte swap.
Definition: util.h:373
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:437
int char2hex(char c, uint8_t *x)
Convert a single character into a hexadecimal nibble.
uint8_t u8_to_dec(char *buf, uint8_t buflen, uint8_t value)
Convert a uint8_t into a decimal string representation.
static bool is_power_of_two(unsigned int x)
Is x a power of two?
Definition: util.h:315
size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen)
Convert a binary array into string representation.
struct k_thread t
Definition: kobject.c:1327
uint32_t hex
Definition: printk.c:118
char c
Definition: printk.c:112
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__INT64_TYPE__ int64_t
Definition: stdint.h:75
Macro utilities.