no-OS
Loading...
Searching...
No Matches
no_os_util.h
Go to the documentation of this file.
1/***************************************************************************/
33#ifndef _NO_OS_UTIL_H_
34#define _NO_OS_UTIL_H_
35
36#include <stdint.h>
37#include <stdlib.h>
38#include <stdbool.h>
39
40#if _WIN32
41#define __no_os_weak__(x)
42#else
43#define __no_os_weak__(x) __attribute__(x)
44#endif
45
46#define NO_OS_BIT(x) (1 << (x))
47
48#define NO_OS_BIT_ULL(x) ((uint64_t) 1 << (x))
49
50#define NO_OS_ARRAY_SIZE(x) \
51 (sizeof(x) / sizeof((x)[0]))
52
53#define NO_OS_DIV_ROUND_UP(x,y) \
54 (((x) + (y) - 1) / (y))
55#define NO_OS_DIV_ROUND_CLOSEST(x, y) \
56 (((x) + (y) / 2) / (y))
57#define NO_OS_DIV_ROUND_CLOSEST_ULL(x, y) \
58 NO_OS_DIV_ROUND_CLOSEST(x, y)
59
60#define no_os_min(x, y) \
61 (((x) < (y)) ? (x) : (y))
62#define no_os_min_t(type, x, y) \
63 (type)no_os_min((type)(x), (type)(y))
64
65#define no_os_max(x, y) \
66 (((x) > (y)) ? (x) : (y))
67#define no_os_max_t(type, x, y) \
68 (type)no_os_max((type)(x), (type)(y))
69
70#define no_os_clamp(val, min_val, max_val) \
71 (no_os_max(no_os_min((val), (max_val)), (min_val)))
72#define no_os_clamp_t(type, val, min_val, max_val) \
73 (type)no_os_clamp((type)(val), (type)(min_val), (type)(max_val))
74
75#define no_os_swap(x, y) \
76 {typeof(x) _tmp_ = (x); (x) = (y); (y) = _tmp_;}
77
78#define no_os_round_up(x,y) \
79 (((x)+(y)-1)/(y))
80
81#define NO_OS_BITS_PER_LONG 32
82
83#define NO_OS_GENMASK(h, l) ({ \
84 uint32_t t = (uint32_t)(~0UL); \
85 t = t << (NO_OS_BITS_PER_LONG - (h - l + 1)); \
86 t = t >> (NO_OS_BITS_PER_LONG - (h + 1)); \
87 t; \
88})
89#define NO_OS_GENMASK_ULL(h, l) ({ \
90 unsigned long long t = (unsigned long long)(~0ULL); \
91 t = t << (64 - (h - l + 1)); \
92 t = t >> (64 - (h + 1)); \
93 t; \
94})
95
96#define no_os_bswap_constant_32(x) \
97 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
98 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
99
100#define no_os_bswap_constant_16(x) ((((x) & (uint16_t)0xff00) >> 8) | \
101 (((x) & (uint16_t)0x00ff) << 8))
102
103#define no_os_bit_swap_constant_8(x) \
104 ((((x) & 0x80) >> 7) | \
105 (((x) & 0x40) >> 5) | \
106 (((x) & 0x20) >> 3) | \
107 (((x) & 0x10) >> 1) | \
108 (((x) & 0x08) << 1) | \
109 (((x) & 0x04) << 3) | \
110 (((x) & 0x02) << 5) | \
111 (((x) & 0x01) << 7))
112
113#define NO_OS_U16_MAX ((uint16_t)~0U)
114#define NO_OS_S16_MAX ((int16_t)(NO_OS_U16_MAX>>1))
115
116#define NO_OS_DIV_U64(x, y) (x / y)
117
118#define NO_OS_UNUSED_PARAM(x) ((void)x)
119
120#define no_os_shift_right(x, s) ((x) < 0 ? -(-(x) >> (s)) : (x) >> (s))
121
122#define no_os_align(x, align) (((x) + ((typeof(x))(align) - 1)) & ~((typeof(x))(align) - 1))
123
124#define no_os_bcd2bin(x) (((x) & 0x0f) + ((x) >> 4) * 10)
125#define no_os_bin2bcd(x) ((((x) / 10) << 4) + (x) % 10)
126
127#define NO_OS_CONTAINER_OF(ptr, type, name) ((type *)((char *)(ptr) - offsetof(type, name)))
128
129/* Check if bit set */
130inline int no_os_test_bit(int pos, const volatile void * addr)
131{
132 return (((const int *)addr)[pos / 32] >> pos) & 1UL;
133}
134
135/* Find first set bit in word. */
136uint32_t no_os_find_first_set_bit(uint32_t word);
137uint64_t no_os_find_first_set_bit_u64(uint64_t word);
138/* Find last set bit in word. */
139uint32_t no_os_find_last_set_bit(uint32_t word);
140/* Locate the closest element in an array. */
141uint32_t no_os_find_closest(int32_t val,
142 const int32_t *array,
143 uint32_t size);
144/* Shift the value and apply the specified mask. */
145uint32_t no_os_field_prep(uint32_t mask, uint32_t val);
146uint64_t no_os_field_prep_u64(uint64_t mask, uint64_t val);
147/* Get a field specified by a mask from a word. */
148uint32_t no_os_field_get(uint32_t mask, uint32_t word);
149/* Produce the maximum value representable by a field */
150uint32_t no_os_field_max(uint32_t mask);
151uint64_t no_os_field_max_u64(uint64_t mask);
152
153/* Log base 2 of the given number. */
154int32_t no_os_log_base_2(uint32_t x);
155/* Find greatest common divisor of the given two numbers. */
157 uint32_t b);
159 uint64_t b);
160/* Find lowest common multiple of the given two numbers. */
161uint32_t no_os_lowest_common_multiple(uint32_t a, uint32_t b);
162/* Calculate best rational approximation for a given fraction. */
163void no_os_rational_best_approximation(uint32_t given_numerator,
164 uint32_t given_denominator,
165 uint32_t max_numerator,
166 uint32_t max_denominator,
167 uint32_t *best_numerator,
168 uint32_t *best_denominator);
169void no_os_rational_best_approximation_u64(uint64_t given_numerator,
170 uint64_t given_denominator,
171 uint64_t max_numerator,
172 uint64_t max_denominator,
173 uint64_t *best_numerator,
174 uint64_t *best_denominator);
175/* Calculate the number of set bits (8-bit size). */
176unsigned int no_os_hweight8(uint8_t word);
177/* Calculate the number of set bits (16-bit size). */
178unsigned int no_os_hweight16(uint16_t word);
179/* Calculate the number of set bits (32-bit size). */
180unsigned int no_os_hweight32(uint32_t word);
181/* Calculate the quotient and the remainder of an integer division. */
182uint64_t no_os_do_div(uint64_t* n,
183 uint64_t base);
184/* Unsigned 64bit divide with 64bit divisor and remainder */
185uint64_t no_os_div64_u64_rem(uint64_t dividend, uint64_t divisor,
186 uint64_t *remainder);
187/* Unsigned 64bit divide with 32bit divisor with remainder */
188uint64_t no_os_div_u64_rem(uint64_t dividend, uint32_t divisor,
189 uint32_t *remainder);
190int64_t no_os_div_s64_rem(int64_t dividend, int32_t divisor,
191 int32_t *remainder);
192/* Unsigned 64bit divide with 32bit divisor */
193uint64_t no_os_div_u64(uint64_t dividend, uint32_t divisor);
194int64_t no_os_div_s64(int64_t dividend, int32_t divisor);
195/* Converts from string to int32_t */
196int32_t no_os_str_to_int32(const char *str);
197/* Converts from string to uint32_t */
198uint32_t no_os_str_to_uint32(const char *str);
199
200void no_os_put_unaligned_be16(uint16_t val, uint8_t *buf);
201uint16_t no_os_get_unaligned_be16(uint8_t *buf);
202void no_os_put_unaligned_le16(uint16_t val, uint8_t *buf);
203uint16_t no_os_get_unaligned_le16(uint8_t *buf);
204void no_os_put_unaligned_be24(uint32_t val, uint8_t *buf);
205uint32_t no_os_get_unaligned_be24(uint8_t *buf);
206void no_os_put_unaligned_le24(uint32_t val, uint8_t *buf);
207uint32_t no_os_get_unaligned_le24(uint8_t *buf);
208void no_os_put_unaligned_be32(uint32_t val, uint8_t *buf);
209uint32_t no_os_get_unaligned_be32(uint8_t *buf);
210void no_os_put_unaligned_le32(uint32_t val, uint8_t *buf);
211uint32_t no_os_get_unaligned_le32(uint8_t *buf);
212void no_os_put_unaligned_be64(uint64_t val, uint8_t *buf);
213uint64_t no_os_get_unaligned_be64(uint8_t *buf);
214void no_os_put_unaligned_le64(uint64_t val, uint8_t *buf);
215uint64_t no_os_get_unaligned_le64(uint8_t *buf);
216
217int16_t no_os_sign_extend16(uint16_t value, int index);
218int32_t no_os_sign_extend32(uint32_t value, int index);
219uint64_t no_os_mul_u32_u32(uint32_t a, uint32_t b);
220uint64_t no_os_mul_u64_u32_shr(uint64_t a, uint32_t mul, unsigned int shift);
221uint64_t no_os_mul_u64_u32_div(uint64_t a, uint32_t mul, uint32_t divisor);
222
224void no_os_memswap64(void *buf, uint32_t bytes, uint32_t step);
225
226#endif // _NO_OS_UTIL_H_
int64_t no_os_div_s64_rem(int64_t dividend, int32_t divisor, int32_t *remainder)
unsigned int no_os_hweight32(uint32_t word)
int32_t no_os_sign_extend32(uint32_t value, int index)
uint64_t no_os_div64_u64_rem(uint64_t dividend, uint64_t divisor, uint64_t *remainder)
void no_os_memswap64(void *buf, uint32_t bytes, uint32_t step)
uint32_t no_os_find_first_set_bit(uint32_t word)
uint64_t no_os_find_first_set_bit_u64(uint64_t word)
uint32_t no_os_field_max(uint32_t mask)
void no_os_put_unaligned_le16(uint16_t val, uint8_t *buf)
void no_os_put_unaligned_be32(uint32_t val, uint8_t *buf)
uint64_t no_os_field_prep_u64(uint64_t mask, uint64_t val)
int16_t no_os_sign_extend16(uint16_t value, int index)
uint32_t no_os_find_closest(int32_t val, const int32_t *array, uint32_t size)
uint64_t no_os_field_max_u64(uint64_t mask)
uint64_t no_os_mul_u64_u32_shr(uint64_t a, uint32_t mul, unsigned int shift)
uint64_t no_os_do_div(uint64_t *n, uint64_t base)
void no_os_put_unaligned_be64(uint64_t val, uint8_t *buf)
uint64_t no_os_div_u64(uint64_t dividend, uint32_t divisor)
uint32_t no_os_get_unaligned_be32(uint8_t *buf)
uint16_t no_os_get_unaligned_be16(uint8_t *buf)
bool no_os_is_big_endian(void)
uint64_t no_os_greatest_common_divisor_u64(uint64_t a, uint64_t b)
uint32_t no_os_get_unaligned_le32(uint8_t *buf)
uint64_t no_os_get_unaligned_be64(uint8_t *buf)
uint32_t no_os_get_unaligned_le24(uint8_t *buf)
uint32_t no_os_str_to_uint32(const char *str)
uint32_t no_os_field_prep(uint32_t mask, uint32_t val)
void no_os_put_unaligned_le24(uint32_t val, uint8_t *buf)
uint32_t no_os_lowest_common_multiple(uint32_t a, uint32_t b)
unsigned int no_os_hweight8(uint8_t word)
int64_t no_os_div_s64(int64_t dividend, int32_t divisor)
uint32_t no_os_field_get(uint32_t mask, uint32_t word)
uint32_t no_os_greatest_common_divisor(uint32_t a, uint32_t b)
void no_os_put_unaligned_be16(uint16_t val, uint8_t *buf)
void no_os_put_unaligned_le64(uint64_t val, uint8_t *buf)
uint64_t no_os_get_unaligned_le64(uint8_t *buf)
unsigned int no_os_hweight16(uint16_t word)
int32_t no_os_str_to_int32(const char *str)
void no_os_put_unaligned_le32(uint32_t val, uint8_t *buf)
uint16_t no_os_get_unaligned_le16(uint8_t *buf)
void no_os_put_unaligned_be24(uint32_t val, uint8_t *buf)
uint32_t no_os_find_last_set_bit(uint32_t word)
int32_t no_os_log_base_2(uint32_t x)
uint64_t no_os_mul_u32_u32(uint32_t a, uint32_t b)
uint64_t no_os_mul_u64_u32_div(uint64_t a, uint32_t mul, uint32_t divisor)
uint64_t no_os_div_u64_rem(uint64_t dividend, uint32_t divisor, uint32_t *remainder)
void no_os_rational_best_approximation_u64(uint64_t given_numerator, uint64_t given_denominator, uint64_t max_numerator, uint64_t max_denominator, uint64_t *best_numerator, uint64_t *best_denominator)
uint32_t no_os_get_unaligned_be24(uint8_t *buf)
int no_os_test_bit(int pos, const volatile void *addr)
Definition no_os_util.h:130
void no_os_rational_best_approximation(uint32_t given_numerator, uint32_t given_denominator, uint32_t max_numerator, uint32_t max_denominator, uint32_t *best_numerator, uint32_t *best_denominator)