-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.h
218 lines (174 loc) · 5.32 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* Copyright (c) 2020-2021 Siddharth Chandrasekaran <sidcha.dev@gmail.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _UTILS_UTILS_H_
#define _UTILS_UTILS_H_
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#ifndef NULL
#define NULL ((void *)0)
#endif
#ifndef TRUE
#define TRUE (1)
#endif
#ifndef FALSE
#define FALSE (0)
#endif
#define BYTE_0(x) (uint8_t)(((x) >> 0) & 0xFF)
#define BYTE_1(x) (uint8_t)(((x) >> 8) & 0xFF)
#define BYTE_2(x) (uint8_t)(((x) >> 16) & 0xFF)
#define BYTE_3(x) (uint8_t)(((x) >> 24) & 0xFF)
#define BIT(n) (1ull << (n))
#define MASK(n) (BIT((n) + 1) - 1)
#define BIT_IS_SET(m, n) (bool)((m) & BIT(n))
#define BIT_SET(m, n) ((m) |= BIT(n))
#define BIT_CLEAR(m, n) ((m) &= ~BIT(n))
#define BIT_FLIP(m, n) ((m) ^= BIT(n))
#define BIT_TEST_SET(m, n) ({ BIT_IS_SET(m, n) ? false : (bool) BIT_SET(m, n) })
#define ARG_UNUSED(x) (void)(x)
#define STR(x) #x
#define XSTR(x) STR(x)
#define STRINGIFY(x) #x
#define ROUND_UP(x, y) ((x + y - 1) & ~ (y - 1))
#define MATH_MOD(a, b) (((a % b) + b) % b)
#define IS_POW2(n) ((n) & ((n) - 1))
#define ARRAY_SIZEOF(x) \
(sizeof(x) / sizeof(x[0]))
#define ARRAY_BASE(ptr, type, offset) \
((char *)(ptr)) - ((sizeof(type)) * offset)
#define OFFSET_OF(type, field) (size_t)(&((type *)(0))->field)
#define CONTAINER_OF(ptr, type, field) \
((type *)(((char *)(ptr)) - OFFSET_OF(type, field)))
#define MAX(a,b) ({ \
__typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; \
})
#define MIN(a,b) ({ \
__typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _b : _a; \
})
#define SWAP(a,b) { \
__typeof__ (a) _tmp; \
_tmp = a; \
b = a; \
a = _tmp; \
}
#define __READ_ONCE(x) (*(volatile typeof(x) *)&(x))
#define READ_ONCE(x) ({ \
typeof(x) *__xp = &(x); \
typeof(x) __x = __READ_ONCE(*__xp); \
__x; \
})
#define WRITE_ONCE(x, val) do { \
typeof(x) *__xp = &(x); \
*(volatile typeof(x) *)__xp = (val); \
} while (0)
#define ABS(x) ((x) >= 0 ? (x) : -(x))
/* config_enabled() from the kernel */
#define __IS_ENABLED1(x) __IS_ENABLED2(__XXXX ## x)
#define __XXXX1 __YYYY,
#define __IS_ENABLED2(y) __IS_ENABLED3(y 1, 0)
#define __IS_ENABLED3(_i, val, ...) val
#define IS_ENABLED(x) __IS_ENABLED1(x)
//#define IS_ENABLED(x) (pd->debugflags && x) // VK2TDS
/* gcc attribute shorthands */
#ifndef __fallthrough
#if __GNUC__ >= 7
#define __fallthrough __attribute__((fallthrough))
#else
#define __fallthrough
#endif
#endif
#ifndef __packed
#define __packed __attribute__((__packed__))
#endif
#undef __weak
#define __weak __attribute__((weak))
/**
* @brief Return random number between 0 and `limit` both inclusive.
*
* Note: the random number generator must be pre-seeded.
*/
int randint(int limit);
/**
* @brief Rounds up 32-bit v to nearest power of 2. If v is already a power
* of 2 it is returned unmodified.
*/
uint32_t round_up_pow2(uint32_t v);
/**
* @brief Retruns number of digits in a given number in its decimal form.
*/
int num_digits_in_number(int num);
/**
* @brief Dumps an array of bytes in HEX and ASCII formats for debugging. `head`
* is string that is printed before the actual bytes are dumped.
*
* Example:
* int len;
* uint8_t data[MAX_LEN];
* len = get_data_from_somewhere(data, MAX_LEN);
* hexdump(data, len, "Data From Somewhere");
*/
void hexdump(const void *data, size_t len, const char *fmt, ...);
/**
* @brief Get the time in micro seconds.
*/
int64_t usec_now();
/**
* @brief Get time elapsed in micro seconds since `last`. Used along with
* usec_now().
*/
int64_t usec_since(int64_t last);
/**
* @brief Get the time in milli seconds.
*/
int64_t millis_now();
/**
* @brief Get time elapsed in milli seconds since `last`. Used along with
* millis_now().
*/
int64_t millis_since(int64_t last);
/**
* @brief Print the stack trace
*/
void dump_trace(void);
static inline bool char_is_space(int c)
{
unsigned char d = c - 9;
return (0x80001FU >> (d & 31)) & (1U >> (d >> 5));
}
static inline bool char_is_digit(int c)
{
return (unsigned int)(('0' - 1 - c) & (c - ('9' + 1))) >> (sizeof(c) * 8 - 1);
}
static inline bool char_is_alpha(int c)
{
return (unsigned int)(('a' - 1 - (c | 32)) & ((c | 32) - ('z' + 1))) >> (sizeof(c) * 8 - 1);
}
inline uint8_t u8_bit_reverse(uint8_t b)
{
b = (((b & 0xaa) >> 1) | ((b & 0x55) << 1));
b = (((b & 0xcc) >> 2) | ((b & 0x33) << 2));
return ((b >> 4) | (b << 4));
}
inline uint16_t u16_bit_reverse(uint16_t x)
{
x = (((x & 0xaaaa) >> 1) | ((x & 0x5555) << 1));
x = (((x & 0xcccc) >> 2) | ((x & 0x3333) << 2));
x = (((x & 0xf0f0) >> 4) | ((x & 0x0f0f) << 4));
return((x >> 8) | (x << 8));
}
inline uint32_t u32_bit_reverse(uint32_t x)
{
x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
return ((x >> 16) | (x << 16));
}
#endif /* _UTILS_UTILS_H_ */