Zephyr Project API  3.4.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
30#define NUM_BITS(t) (sizeof(t) * 8)
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
43#define POINTER_TO_UINT(x) ((uintptr_t) (x))
45#define UINT_TO_POINTER(x) ((void *) (uintptr_t) (x))
47#define POINTER_TO_INT(x) ((intptr_t) (x))
49#define INT_TO_POINTER(x) ((void *) (intptr_t) (x))
50
51#if !(defined(__CHAR_BIT__) && defined(__SIZEOF_LONG__) && defined(__SIZEOF_LONG_LONG__))
52# error Missing required predefined macros for BITS_PER_LONG calculation
53#endif
54
56#define BITS_PER_LONG (__CHAR_BIT__ * __SIZEOF_LONG__)
57
59#define BITS_PER_LONG_LONG (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
60
65#define GENMASK(h, l) \
66 (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
67
72#define GENMASK64(h, l) \
73 (((~0ULL) - (1ULL << (l)) + 1) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
74
76#define LSB_GET(value) ((value) & -(value))
77
82#define FIELD_GET(mask, value) (((value) & (mask)) / LSB_GET(mask))
83
89#define FIELD_PREP(mask, value) (((value) * LSB_GET(mask)) & (mask))
90
92#define ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - 2 * !(cond)]) - 1)
93
94#if defined(__cplusplus)
95
96/* The built-in function used below for type checking in C is not
97 * supported by GNU C++.
98 */
99#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
100
101#else /* __cplusplus */
102
108#define IS_ARRAY(array) \
109 ZERO_OR_COMPILE_ERROR( \
110 !__builtin_types_compatible_p(__typeof__(array), \
111 __typeof__(&(array)[0])))
112
122#define ARRAY_SIZE(array) \
123 ((size_t) (IS_ARRAY(array) + (sizeof(array) / sizeof((array)[0]))))
124
125#endif /* __cplusplus */
126
141#define IS_ARRAY_ELEMENT(array, ptr) \
142 ((ptr) && POINTER_TO_UINT(array) <= POINTER_TO_UINT(ptr) && \
143 POINTER_TO_UINT(ptr) < POINTER_TO_UINT(&(array)[ARRAY_SIZE(array)]) && \
144 (POINTER_TO_UINT(ptr) - POINTER_TO_UINT(array)) % sizeof((array)[0]) == 0)
145
160#define ARRAY_INDEX(array, ptr) \
161 ({ \
162 __ASSERT_NO_MSG(IS_ARRAY_ELEMENT(array, ptr)); \
163 (__typeof__((array)[0]) *)(ptr) - (array); \
164 })
165
176#define PART_OF_ARRAY(array, ptr) \
177 ((ptr) && POINTER_TO_UINT(array) <= POINTER_TO_UINT(ptr) && \
178 POINTER_TO_UINT(ptr) < POINTER_TO_UINT(&(array)[ARRAY_SIZE(array)]))
179
197#define ARRAY_INDEX_FLOOR(array, ptr) \
198 ({ \
199 __ASSERT_NO_MSG(PART_OF_ARRAY(array, ptr)); \
200 (POINTER_TO_UINT(ptr) - POINTER_TO_UINT(array)) / sizeof((array)[0]); \
201 })
202
224#define CONTAINER_OF(ptr, type, field) \
225 ((type *)(((char *)(ptr)) - offsetof(type, field)))
226
231#define ROUND_UP(x, align) \
232 (((unsigned long)(x) + ((unsigned long)(align) - 1)) & \
233 ~((unsigned long)(align) - 1))
234
239#define ROUND_DOWN(x, align) \
240 ((unsigned long)(x) & ~((unsigned long)(align) - 1))
241
243#define WB_UP(x) ROUND_UP(x, sizeof(void *))
244
246#define WB_DN(x) ROUND_DOWN(x, sizeof(void *))
247
262#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
263
268#define ceiling_fraction(numerator, divider) __DEPRECATED_MACRO \
269 DIV_ROUND_UP(numerator, divider)
270
271#ifndef MAX
283#define MAX(a, b) (((a) > (b)) ? (a) : (b))
284#endif
285
286#ifndef MIN
298#define MIN(a, b) (((a) < (b)) ? (a) : (b))
299#endif
300
301#ifndef CLAMP
314#define CLAMP(val, low, high) (((val) <= (low)) ? (low) : MIN(val, high))
315#endif
316
329#define IN_RANGE(val, min, max) ((val) >= (min) && (val) <= (max))
330
336static inline bool is_power_of_two(unsigned int x)
337{
338 return IS_POWER_OF_TWO(x);
339}
340
349{
350 int64_t sign_ext;
351
352 if (shift == 0U) {
353 return value;
354 }
355
356 /* extract sign bit */
357 sign_ext = (value >> 63) & 1;
358
359 /* make all bits of sign_ext be the same as the value's sign bit */
360 sign_ext = -sign_ext;
361
362 /* shift value and fill opened bit positions with sign bit */
363 return (value >> shift) | (sign_ext << (64 - shift));
364}
365
375static inline void bytecpy(void *dst, const void *src, size_t size)
376{
377 size_t i;
378
379 for (i = 0; i < size; ++i) {
380 ((volatile uint8_t *)dst)[i] = ((volatile const uint8_t *)src)[i];
381 }
382}
383
394static inline void byteswp(void *a, void *b, size_t size)
395{
396 uint8_t t;
397 uint8_t *aa = (uint8_t *)a;
398 uint8_t *bb = (uint8_t *)b;
399
400 for (; size > 0; --size) {
401 t = *aa;
402 *aa++ = *bb;
403 *bb++ = t;
404 }
405}
406
415int char2hex(char c, uint8_t *x);
416
425int hex2char(uint8_t x, char *c);
426
437size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen);
438
449size_t hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen);
450
458static inline uint8_t bcd2bin(uint8_t bcd)
459{
460 return ((10 * (bcd >> 4)) + (bcd & 0x0F));
461}
462
470static inline uint8_t bin2bcd(uint8_t bin)
471{
472 return (((bin / 10) << 4) | (bin % 10));
473}
474
488uint8_t u8_to_dec(char *buf, uint8_t buflen, uint8_t value);
489
514char *utf8_trunc(char *utf8_str);
515
530char *utf8_lcpy(char *dst, const char *src, size_t n);
531
532#define __z_log2d(x) (32 - __builtin_clz(x) - 1)
533#define __z_log2q(x) (64 - __builtin_clzll(x) - 1)
534#define __z_log2(x) (sizeof(__typeof__(x)) > 4 ? __z_log2q(x) : __z_log2d(x))
535
546#define LOG2(x) ((x) < 1 ? -1 : __z_log2(x))
547
558#define LOG2CEIL(x) ((x) < 1 ? 0 : __z_log2((x)-1) + 1)
559
572#define NHPOT(x) ((x) < 1 ? 1 : ((x) > (1ULL<<63) ? 0 : 1ULL << LOG2CEIL(x)))
573
574#ifdef __cplusplus
575}
576#endif
577
578/* This file must be included at the end of the !_ASMLANGUAGE guard.
579 * It depends on macros defined in this file above which cannot be forward declared.
580 */
582
583#endif /* !_ASMLANGUAGE */
584
586#ifdef _LINKER
587/* This is used in linker scripts so need to avoid type casting there */
588#define KB(x) ((x) << 10)
589#else
590#define KB(x) (((size_t)x) << 10)
591#endif
593#define MB(x) (KB(x) << 10)
595#define GB(x) (MB(x) << 10)
596
598#define KHZ(x) ((x) * 1000)
600#define MHZ(x) (KHZ(x) * 1000)
601
614#if defined(CONFIG_ARCH_POSIX)
615#define Z_SPIN_DELAY(t) k_busy_wait(t)
616#else
617#define Z_SPIN_DELAY(t)
618#endif
619
635#define WAIT_FOR(expr, timeout, delay_stmt) \
636 ({ \
637 uint32_t cycle_count = k_us_to_cyc_ceil32(timeout); \
638 uint32_t start = k_cycle_get_32(); \
639 while (!(expr) && (cycle_count > (k_cycle_get_32() - start))) { \
640 delay_stmt; \
641 Z_SPIN_DELAY(10); \
642 } \
643 (expr); \
644 })
645
650#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:348
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:375
char * utf8_lcpy(char *dst, const char *src, size_t n)
Copies a UTF-8 encoded string from src to dst.
#define IS_POWER_OF_TWO(x)
Check if a x is a power of two.
Definition: util_macro.h:77
static uint8_t bin2bcd(uint8_t bin)
Convert a binary value to binary coded decimal (BCD 8421).
Definition: util.h:470
static void byteswp(void *a, void *b, size_t size)
byte by byte swap.
Definition: util.h:394
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:458
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:336
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.