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