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
|
#ifndef APOS_UTILS_H
#define APOS_UTILS_H
#define ABS(a) (a < 0 ? -a : a)
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
#define MAX3(a, b, c) (MAX(a, b) >= MAX(b, c) ? MAX(a, b) : MAX(b, c))
#define MAX4(a, b, c, d) \
(MAX3(a, b, c) >= MAX3(b, c, d) ? MAX3(a, b, c) : MAX3(b, c, d))
/* etc... */
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
#define MIN3(a, b, c) (MIN(a, b) <= MIN(b, c) ? MIN(a, b) : MIN(b, c))
#define MIN4(a, b, c, d) \
(MIN3(a, b, c) <= MIN3(b, c, d) ? MIN3(a, b, c) : MIN3(b, c, d))
/* etc... */
#define GLUE2(x, y) x##y
#define GLUE(x, y) GLUE2(x, y)
#define QUOTE2(x) #x
#define QUOTE(x) QUOTE2(x)
#define UNUSED(x) ((void)(x))
#include <apos/builtin.h>
#if __has_builtin(__builtin_offsetof)
#define offsetof(type, member) __builtin_offsetof(type, member)
#else
#define offsetof(type, member) ((uintptr_t) & ((type *)0)->member)
#endif
#if __has_builtin(__builtin_expect)
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#else
#define likely(x) (x)
#define unlikely(x) (x)
#endif
#define container_of(ptr, type, member) \
((type *)((char *)(ptr)-offsetof(type, member)))
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define ALIGNED(x, a) ((x) % a == 0)
#define ptradd(x, y) (((vm_t)(x)) + ((vm_t)(y)))
#include <apos/types.h>
#define align_up(x, y) \
_Generic((x), int8_t \
: align_up_int8_t, int16_t \
: align_up_int16_t, int32_t \
: align_up_int32_t, int64_t \
: align_up_int64_t, \
\
uint8_t \
: align_up_uint8_t, uint16_t \
: align_up_uint16_t, uint32_t \
: align_up_uint32_t, uint64_t \
: align_up_uint64_t)((x), (y))
#define DEFINE_ALIGN_UP(type) \
static inline type align_up_##type(type val, type a) \
{ \
if (!a) \
return val; \
\
type rem = val % a; \
\
if (rem == 0) \
return val; \
\
return val + a - rem; \
}
DEFINE_ALIGN_UP(int8_t);
DEFINE_ALIGN_UP(int16_t);
DEFINE_ALIGN_UP(int32_t);
DEFINE_ALIGN_UP(int64_t);
DEFINE_ALIGN_UP(uint8_t);
DEFINE_ALIGN_UP(uint16_t);
DEFINE_ALIGN_UP(uint32_t);
DEFINE_ALIGN_UP(uint64_t);
#define align_down(x, y) \
_Generic((x), int8_t \
: align_down_int8_t, int16_t \
: align_down_int16_t, int32_t \
: align_down_int32_t, int64_t \
: align_down_int64_t, \
\
uint8_t \
: align_down_uint8_t, uint16_t \
: align_down_uint16_t, uint32_t \
: align_down_uint32_t, uint64_t \
: align_down_uint64_t)((x), (y))
#define DEFINE_ALIGN_DOWN(type) \
static inline type align_down_##type(type val, type a) \
{ \
if (!a) \
return val; \
\
return val - (val % a); \
}
DEFINE_ALIGN_DOWN(int8_t);
DEFINE_ALIGN_DOWN(int16_t);
DEFINE_ALIGN_DOWN(int32_t);
DEFINE_ALIGN_DOWN(int64_t);
DEFINE_ALIGN_DOWN(uint8_t);
DEFINE_ALIGN_DOWN(uint16_t);
DEFINE_ALIGN_DOWN(uint32_t);
DEFINE_ALIGN_DOWN(uint64_t);
#define is_aligned(x, y) \
_Generic((x), int8_t \
: is_aligned_int8_t, int16_t \
: is_aligned_int16_t, int32_t \
: is_aligned_int32_t, int64_t \
: is_aligned_int64_t, \
\
uint8_t \
: is_aligned_uint8_t, uint16_t \
: is_aligned_uint16_t, uint32_t \
: is_aligned_uint32_t, uint64_t \
: is_aligned_uint64_t)((x), (y))
#define DEFINE_ALIGNED(type) \
static inline bool is_aligned_##type(type val, type a) \
{ \
if (!a) \
return true; \
\
return val % a == 0; \
}
DEFINE_ALIGNED(int8_t);
DEFINE_ALIGNED(int16_t);
DEFINE_ALIGNED(int32_t);
DEFINE_ALIGNED(int64_t);
DEFINE_ALIGNED(uint8_t);
DEFINE_ALIGNED(uint16_t);
DEFINE_ALIGNED(uint32_t);
DEFINE_ALIGNED(uint64_t);
static inline int asciinum(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
else if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
else if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
else
return 0;
}
static inline uintmax_t convnum(const char *c, size_t len, size_t base)
{
size_t multiplier = 1;
size_t sum = 0;
for (size_t i = 0; i < len; ++i) {
sum += asciinum(c[len - 1 - i]) * multiplier;
multiplier *= base;
}
return sum;
}
#endif /* APOS_UTILS_H */
|