-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathmain.c
401 lines (339 loc) · 13.3 KB
/
main.c
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#define INTERACTIVE 1
#undef NDEBUG
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#ifdef _EZ80
# include <debug.h>
# include <ti/screen.h>
# include <ti/getcsc.h>
# if INTERACTIVE
# define x_printf printf
# else
# define x_printf(...) dbg_printf(__VA_ARGS__)
# endif
#else
# ifndef __cplusplus
typedef int32_t int24_t;
typedef uint32_t uint24_t;
# else
template <unsigned bits, typename U>
struct IntN final
{
private:
static constexpr int extraBits = sizeof(U) * 8 - bits;
static_assert(extraBits >= 0, "underlying type is not big enough");
public:
constexpr IntN(U value) : value(value * (1ULL << extraBits)) {}
constexpr operator U() const { return value >> extraBits; }
private:
U value;
};
using uint24_t = IntN<24, uint_fast32_t>;
using int24_t = IntN<24, int_fast32_t>;
# endif
static int24_t __builtin_bitreverse24(int24_t x)
{
return __builtin_bitreverse32(x) >> 8;
}
#endif
#if INTERACTIVE || !defined(_EZ80)
# define x_printf printf
#else
# define x_printf(...) dbg_printf(__VA_ARGS__)
#endif
static void separateOutput()
{
#if INTERACTIVE && defined(_EZ80)
os_ClrHomeFull();
#else
x_printf("\n--------------------------\n");
#endif
}
static void waitForKey()
{
#if INTERACTIVE && defined(_EZ80)
while (!os_GetCSC())
;
#endif
}
static void finishOutput()
{
waitForKey();
separateOutput();
}
#define DEFINE_UNOP_PREFIX_FUNC(type, name, op) \
static type name##_(type x) \
{ \
return (type)(op(x)); \
}
#define DEFINE_BINOP_FUNC_FUNC(type, name, func, post) \
static type name##_(type x, type y) \
{ \
return (type)(func(x, y)post); \
}
#define DEFINE_BINOP_INFIX_FUNC(type, name, op) \
static type name##_(type x, type y) \
{ \
return (type)(x op y); \
}
#define DEFINE_UNOP_PREFIX_FUNC_B(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC(u##int8_t, b##name, op)
#define DEFINE_UNOP_PREFIX_FUNC_S(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC(u##int16_t, s##name, op)
#define DEFINE_UNOP_PREFIX_FUNC_I(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC(u##int24_t, i##name, op)
#define DEFINE_UNOP_PREFIX_FUNC_L(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC(u##int32_t, l##name, op)
#define DEFINE_UNOP_PREFIX_FUNC_LL(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC(u##int64_t, ll##name, op)
#define DEFINE_BINOP_FUNC_FUNC_B(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC(u##int8_t, b##name, b##func, post)
#define DEFINE_BINOP_FUNC_FUNC_S(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC(u##int16_t, s##name, s##func, post)
#define DEFINE_BINOP_FUNC_FUNC_I(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC(u##int24_t, i##name, func, post)
#define DEFINE_BINOP_FUNC_FUNC_L(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC(u##int32_t, l##name, l##func, post)
#define DEFINE_BINOP_FUNC_FUNC_LL(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC(u##int64_t, ll##name, ll##func, post)
#define DEFINE_BINOP_INFIX_FUNC_B(u, name, op) \
DEFINE_BINOP_INFIX_FUNC(u##int8_t, b##name, op)
#define DEFINE_BINOP_INFIX_FUNC_S(u, name, op) \
DEFINE_BINOP_INFIX_FUNC(u##int16_t, s##name, op)
#define DEFINE_BINOP_INFIX_FUNC_I(u, name, op) \
DEFINE_BINOP_INFIX_FUNC(u##int24_t, i##name, op)
#define DEFINE_BINOP_INFIX_FUNC_L(u, name, op) \
DEFINE_BINOP_INFIX_FUNC(u##int32_t, l##name, op)
#define DEFINE_BINOP_INFIX_FUNC_LL(u, name, op) \
DEFINE_BINOP_INFIX_FUNC(u##int64_t, ll##name, op)
#define DEFINE_UNOP_PREFIX_FUNC_B_TO_S(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC_B(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC_S(u, name, op)
#define DEFINE_UNOP_PREFIX_FUNC_B_TO_I(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC_B_TO_S(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC_I(u, name, op)
#define DEFINE_UNOP_PREFIX_FUNC_B_TO_L(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC_B_TO_I(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC_L(u, name, op)
#define DEFINE_UNOP_PREFIX_FUNC_B_TO_LL(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC_B_TO_L(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC_LL(u, name, op)
#define DEFINE_BINOP_INFIX_FUNC_B_TO_S(u, name, op) \
DEFINE_BINOP_INFIX_FUNC_B(u, name, op) \
DEFINE_BINOP_INFIX_FUNC_S(u, name, op)
#define DEFINE_BINOP_INFIX_FUNC_B_TO_I(u, name, op) \
DEFINE_BINOP_INFIX_FUNC_B_TO_S(u, name, op) \
DEFINE_BINOP_INFIX_FUNC_I(u, name, op)
#define DEFINE_BINOP_INFIX_FUNC_B_TO_L(u, name, op) \
DEFINE_BINOP_INFIX_FUNC_B_TO_I(u, name, op) \
DEFINE_BINOP_INFIX_FUNC_L(u, name, op)
#define DEFINE_BINOP_INFIX_FUNC_B_TO_LL(u, name, op) \
DEFINE_BINOP_INFIX_FUNC_B_TO_L(u, name, op) \
DEFINE_BINOP_INFIX_FUNC_LL(u, name, op)
#define DEFINE_BINOP_FUNC_FUNC_I_TO_L(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC_I(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC_L(u, name, func, post)
#define DEFINE_BINOP_FUNC_FUNC_I_TO_LL(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC_I_TO_L(u, name, func, post) \
DEFINE_BINOP_FUNC_FUNC_LL(u, name, func, post)
#define DEFINE_UNOP_TYPE(u) \
typedef struct u##UnOp_ \
{ \
const char *name; \
u##int8_t (*b)(u##int8_t); \
u##int16_t (*s)(u##int16_t); \
u##int24_t (*i)(u##int24_t); \
u##int32_t (*l)(u##int32_t); \
u##int64_t (*ll)(u##int64_t); \
} u##UnOp;
#define DEFINE_BINOP_TYPE(u) \
typedef struct u##BinOp_ \
{ \
const char *name; \
u##int8_t (*b)(u##int8_t, u##int8_t); \
u##int16_t (*s)(u##int16_t, u##int16_t); \
u##int24_t (*i)(u##int24_t, u##int24_t); \
u##int32_t (*l)(u##int32_t, u##int32_t); \
u##int64_t (*ll)(u##int64_t, u##int64_t); \
} u##BinOp;
DEFINE_UNOP_TYPE()
DEFINE_UNOP_TYPE(u)
DEFINE_BINOP_TYPE()
DEFINE_BINOP_TYPE(u)
#define DEFINE_UNOP_STRUCT_B(u, name) \
static const u##UnOp unop_##name = {#name, b##name##_};
#define DEFINE_UNOP_STRUCT_B_TO_S(u, name) \
static const u##UnOp unop_##name = {#name, b##name##_, s##name##_};
#define DEFINE_UNOP_STRUCT_B_TO_I(u, name) \
static const u##UnOp unop_##name = {#name, b##name##_, s##name##_, i##name##_};
#define DEFINE_UNOP_STRUCT_B_TO_L(u, name) \
static const u##UnOp unop_##name = {#name, b##name##_, s##name##_, i##name##_, l##name##_};
#define DEFINE_UNOP_STRUCT_B_TO_LL(u, name) \
static const u##UnOp unop_##name = {#name, b##name##_, s##name##_, i##name##_, l##name##_, ll##name##_};
#define DEFINE_UNOP_STRUCT_S_L_LL(u, name) \
static const u##UnOp unop_##name = {#name, 0, s##name##_, 0, l##name##_, ll##name##_};
#define DEFINE_BINOP_STRUCT_B(u, name) \
static const u##BinOp binop_##name = {#name, b##name##_};
#define DEFINE_BINOP_STRUCT_B_TO_S(u, name) \
static const u##BinOp binop_##name = {#name, b##name##_, s##name##_};
#define DEFINE_BINOP_STRUCT_B_TO_I(u, name) \
static const u##BinOp binop_##name = {#name, b##name##_, s##name##_, i##name##_};
#define DEFINE_BINOP_STRUCT_B_TO_L(u, name) \
static const u##BinOp binop_##name = {#name, b##name##_, s##name##_, i##name##_, l##name##_};
#define DEFINE_BINOP_STRUCT_B_TO_LL(u, name) \
static const u##BinOp binop_##name = {#name, b##name##_, s##name##_, i##name##_, l##name##_, ll##name##_};
#define DEFINE_BINOP_STRUCT_I_TO_L(u, name) \
static const u##BinOp binop_##name = {#name, NULL, NULL, i##name##_, l##name##_};
#define DEFINE_BINOP_STRUCT_I_TO_LL(u, name) \
static const u##BinOp binop_##name = {#name, NULL, NULL, i##name##_, l##name##_, ll##name##_};
#define DEFINE_UNOP_PREFIX_B(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC_B(u, name, op) \
DEFINE_UNOP_STRUCT_B(u, name)
#define DEFINE_UNOP_PREFIX_B_TO_S(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC_B_TO_S(u, name, op) \
DEFINE_UNOP_STRUCT_B_TO_S(u, name)
#define DEFINE_UNOP_PREFIX_B_TO_I(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC_B_TO_I(u, name, op) \
DEFINE_UNOP_STRUCT_B_TO_I(u, name)
#define DEFINE_UNOP_PREFIX_B_TO_L(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC_B_TO_L(u, name, op) \
DEFINE_UNOP_STRUCT_B_TO_L(u, name)
#define DEFINE_UNOP_PREFIX_B_TO_LL(u, name, op) \
DEFINE_UNOP_PREFIX_FUNC_B_TO_LL(u, name, op) \
DEFINE_UNOP_STRUCT_B_TO_LL(u, name)
#define DEFINE_BINOP_INFIX_B(u, name, op) \
DEFINE_BINOP_INFIX_FUNC_B(u, name, op) \
DEFINE_BINOP_STRUCT_B(u, name)
#define DEFINE_BINOP_INFIX_B_TO_S(u, name, op) \
DEFINE_BINOP_INFIX_FUNC_B_TO_S(u, name, op) \
DEFINE_BINOP_STRUCT_B_TO_S(u, name)
#define DEFINE_BINOP_INFIX_B_TO_I(u, name, op) \
DEFINE_BINOP_INFIX_FUNC_B_TO_I(u, name, op) \
DEFINE_BINOP_STRUCT_B_TO_I(u, name)
#define DEFINE_BINOP_INFIX_B_TO_L(u, name, op) \
DEFINE_BINOP_INFIX_FUNC_B_TO_L(u, name, op) \
DEFINE_BINOP_STRUCT_B_TO_L(u, name)
#define DEFINE_BINOP_INFIX_B_TO_LL(u, name, op) \
DEFINE_BINOP_INFIX_FUNC_B_TO_LL(u, name, op) \
DEFINE_BINOP_STRUCT_B_TO_LL(u, name)
static void testOp(bool isBinOp, const BinOp *op, int64_t x, int64_t y)
{
unsigned lhsLength = 8;
unsigned nameLength = strlen(op->name);
unsigned prefixLength = lhsLength - nameLength;
x_printf("%*s=%016llX\n", lhsLength, "x", (long long)x);
if (!isBinOp)
{
x_printf("\n");
}
else
{
x_printf("%*s=%016llX\n", lhsLength, "y", (long long)y);
}
#define TEST_OP(prefix, bits) \
x_printf("\n"); \
if (op->prefix) \
{ \
unsigned digits = (bits + 3) / 4; \
unsigned long long result = (op->prefix)(x, y) & ((1ULL << (bits - 1) << 1) - 1); \
x_printf("%*s%s=%*s%0*llX", prefixLength, #prefix, op->name, 16 - digits, "", digits, result); \
}
TEST_OP(b, 8)
TEST_OP(s, 16)
TEST_OP(i, 24)
TEST_OP(l, 32)
TEST_OP(ll, 64)
finishOutput();
}
static void testUnOp(const UnOp *op, int64_t x)
{
testOp(false, (const BinOp*)op, x, 0);
}
static void testBinOp(const BinOp *op, int64_t x, int64_t y)
{
testOp(true, op, x, y);
}
DEFINE_UNOP_PREFIX_B_TO_LL( , not, ~)
DEFINE_UNOP_PREFIX_B_TO_LL( , neg, -)
DEFINE_UNOP_PREFIX_FUNC_B_TO_I( , abs, abs)
DEFINE_UNOP_PREFIX_FUNC_L( , abs, labs)
DEFINE_UNOP_PREFIX_FUNC_LL( , abs, llabs)
DEFINE_UNOP_STRUCT_B_TO_LL( , abs)
DEFINE_UNOP_PREFIX_FUNC_B( , bitrev, __builtin_bitreverse8)
DEFINE_UNOP_PREFIX_FUNC_S( , bitrev, __builtin_bitreverse16)
DEFINE_UNOP_PREFIX_FUNC_I( , bitrev, __builtin_bitreverse24)
DEFINE_UNOP_PREFIX_FUNC_L( , bitrev, __builtin_bitreverse32)
DEFINE_UNOP_PREFIX_FUNC_LL( , bitrev, __builtin_bitreverse64)
DEFINE_UNOP_STRUCT_B_TO_LL( , bitrev)
// Needs to be unsigned to avoid extra bits from sign extension
DEFINE_UNOP_PREFIX_FUNC_S(u, bswap, __builtin_bswap16)
DEFINE_UNOP_PREFIX_FUNC_L(u, bswap, __builtin_bswap32)
DEFINE_UNOP_PREFIX_FUNC_LL(u, bswap, __builtin_bswap64)
DEFINE_UNOP_STRUCT_S_L_LL(u, bswap)
// Needs to be unsigned to avoid extra bits from sign extension
DEFINE_UNOP_PREFIX_FUNC_B_TO_I(u, popcnt, __builtin_popcount)
DEFINE_UNOP_PREFIX_FUNC_L(u, popcnt, __builtin_popcountl)
DEFINE_UNOP_PREFIX_FUNC_LL(u, popcnt, __builtin_popcountll)
DEFINE_UNOP_STRUCT_B_TO_LL(u, popcnt)
DEFINE_BINOP_INFIX_B_TO_LL( , and, &)
DEFINE_BINOP_INFIX_B_TO_LL( , or, |)
DEFINE_BINOP_INFIX_B_TO_LL( , xor, ^)
DEFINE_BINOP_INFIX_B_TO_LL( , add, +)
DEFINE_BINOP_INFIX_B_TO_LL( , sub, -)
DEFINE_BINOP_INFIX_B_TO_LL( , shl, <<)
DEFINE_BINOP_INFIX_B_TO_LL(u, shru, >>)
DEFINE_BINOP_INFIX_B_TO_LL( , shrs, >>)
DEFINE_BINOP_INFIX_B_TO_LL(u, mulu, *)
DEFINE_BINOP_INFIX_B_TO_LL(u, divu, /)
DEFINE_BINOP_INFIX_B_TO_LL(u, remu, %)
DEFINE_BINOP_INFIX_B_TO_LL( , divs, /)
DEFINE_BINOP_INFIX_B_TO_LL( , rems, %)
DEFINE_BINOP_FUNC_FUNC_I_TO_LL( , div_q, div, .quot)
DEFINE_BINOP_STRUCT_I_TO_LL( , div_q)
DEFINE_BINOP_FUNC_FUNC_I_TO_LL( , div_r, div, .rem)
DEFINE_BINOP_STRUCT_I_TO_LL( , div_r)
static const UnOp *unops[] = {
&unop_not,
&unop_neg,
&unop_abs,
&unop_bitrev,
(const UnOp *)&unop_bswap,
(const UnOp *)&unop_popcnt,
};
static const BinOp *binops[] = {
&binop_and,
&binop_or,
&binop_xor,
&binop_add,
&binop_sub,
&binop_shl,
(const BinOp *)&binop_shru,
&binop_shrs,
(const BinOp *)&binop_mulu,
(const BinOp *)&binop_divu,
(const BinOp *)&binop_remu,
&binop_divs,
&binop_rems,
&binop_div_q,
&binop_div_r,
};
int main(int argc, char *argv[])
{
int64_t x = argc > 1 ? atoll(argv[1]) : (int64_t)0xDFA5FBC197EDB389LL;
int64_t y = argc > 2 ? atoll(argv[2]) : (int64_t)0x08010A030C050E07LL;
separateOutput();
for (size_t i = 0; i < sizeof(unops) / sizeof(*unops); i++)
{
testUnOp(unops[i], x);
}
for (size_t i = 0; i < sizeof(binops) / sizeof(*binops); i++)
{
testBinOp(binops[i], x, y);
}
return 0;
}