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