-
Notifications
You must be signed in to change notification settings - Fork 14
/
nolibc.h
447 lines (374 loc) · 8.07 KB
/
nolibc.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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#ifndef NOLIBC_H
#define NOLIBC_H
#include <limits.h>
#include <stdint.h>
#include <stddef.h>
#include <float.h>
#if __riscv_xlen == 32
typedef uint32_t ux;
typedef float fx;
#define IF64(...)
#else
typedef uint64_t ux;
typedef double fx;
#define IF64(...) __VA_ARGS__
#endif
static void print_flush(void);
#ifdef CUSTOM_HOST
#define IFHOSTED(...)
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
/* customize me */
static void
memwrite(void const *ptr, size_t len) { }
// static size_t /* only needed for vector-utf/bench.c */
// memread(void *ptr, size_t len) { }
static void
exit(int x) { __asm volatile("unimp\n"); }
int main(void);
void _start(void) {
int x = main();
print_flush();
exit(x);
}
#elif __STDC_HOSTED__
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define IFHOSTED(...) __VA_ARGS__
int nolibc_main(void);
static void
memwrite(void const *ptr, size_t len)
{
fwrite(ptr, 1, len, stdout);
}
static size_t
memread(void *ptr, size_t len)
{
return fread(ptr, 1, len, stdin);
}
#ifndef ENABLE_RDCYCLE_HACK
int main(void) {
int x = nolibc_main();
print_flush();
exit(x);
}
#else
#include <linux/perf_event.h>
#include <asm/unistd.h>
#include <unistd.h>
#include <sys/ioctl.h>
int main(void) {
struct perf_event_attr pe = { 0 };
pe.type = PERF_TYPE_HARDWARE;
pe.size = sizeof pe;
pe.config = PERF_COUNT_HW_CPU_CYCLES;
pe.disabled = 0;
pe.exclude_kernel = 1;
long fd = syscall(__NR_perf_event_open, &pe, 0, -1, -1, 0);
if (fd < 0) perror("perf_event_open"),exit(EXIT_FAILURE);
int x = nolibc_main();
print_flush();
exit(x);
}
#endif
#define main nolibc_main
#else
#define IFHOSTED(...)
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
static void
exit(int x)
{
__asm volatile(
"mv a0, %0\n"
"li a7, 93\n"
"ecall\n"
:
: "r"(x)
: "a0", "a7");
}
static void
memwrite(void const *ptr, size_t len)
{
__asm volatile(
"li a0, 1\n"
"mv a1, %0\n"
"mv a2, %1\n"
"li a7, 64\n"
"ecall\n"
:
: "r"(ptr), "r"(len)
: "a0", "a1", "a2", "a7"
);
}
static size_t
memread(void *ptr, size_t len)
{
size_t ret;
__asm volatile(
"li a0, 0\n"
"mv a1, %1\n"
"mv a2, %2\n"
"li a7, 63\n"
"ecall\n"
"mv %0, a0\n"
: "=r"(ret)
: "r"(ptr), "r"(len)
: "a0", "a1", "a2", "a7"
);
return ret;
}
int main(void);
void _start(void) {
int x = main();
print_flush();
exit(x);
}
#endif
/* utils */
static inline ux
rv_cycles(void)
{
ux cycle;
#ifdef READ_MCYCLE
__asm volatile ("csrr %0, mcycle" : "=r"(cycle));
#else
__asm volatile ("csrr %0, cycle" : "=r"(cycle));
#endif
return cycle;
}
static void
memswap(void *a, void *b, size_t size)
{
unsigned char *A = (unsigned char*)a, *B = (unsigned char*)b;
unsigned char *aEnd = A + size;
while (A < aEnd) {
unsigned char temp = *A;
*A++ = *B;
*B++ = temp;
}
}
#if !__STDC_HOSTED__
void *
memcpy(void *restrict dest, void const *restrict src, size_t n)
{
unsigned char *d = dest;
unsigned char const *s = src;
while (n--) *d++ = *s++;
return dest;
}
void *
memset(void *dest, int c, size_t n)
{
unsigned char *d = dest;
while (n--) *d++ = c;
return dest;
}
size_t
strlen(char const *str)
{
size_t len = 0;
while (*str++) ++len;
return len;
}
void
qsort(void *base, size_t len, size_t size, int (*cmp)(const void *, const void *))
{
if (len < 2) return;
unsigned char *b = base;
size_t pivot = len >> 1, l = 0, h = len - 1;
for (; 1; ++l, --h) {
while (cmp(b + l*size, b + pivot*size) < 0) ++l;
while (cmp(b + h*size, b + pivot*size) > 0) --h;
if (l >= h) break;
memswap( b + l*size, b + h*size, size);
pivot = pivot == l ? h : pivot == h ? l : pivot;
}
qsort(b, l, size, cmp);
qsort(b + h * size + size, len - h - 1, size, cmp);
}
#endif
static void
rv_fencei() { __asm volatile("fence.i"); }
static ux
usqrt(ux y)
{
ux L = 0, R = y + 1;
while (L != R - 1) {
ux M = (L + R) / 2;
if (M * M <= y) L = M;
else R = M;
}
return L;
}
static ux
uhash(ux x)
{
#if __riscv_xlen == 32
/* MurmurHash3 32-bit finalizer */
x ^= x >> 16;
x *= 0x85ebca6b;
x ^= x >> 13;
x *= 0xc2b2ae35;
x ^= x >> 16;
#else
/* splitmix64 finalizer */
x ^= x >> 30;
x *= 0xbf58476d1ce4e5b9U;
x ^= x >> 27;
x *= 0x94d049bb133111ebU;
x ^= x >> 31;
#endif
return x;
}
typedef struct { ux x, y, z; } URand;
#define ROTL(x,n) (((x) << (n)) | ((x) >> (8*sizeof(x) - (n))))
/* RomuDuoJr, see https://romu-random.org/ */
static inline ux
urand(URand *r)
{
ux xp = r->x, yp = r->y, zp = r->z;
#if __riscv_xlen == 32
r->x = 3323815723u * zp;
r->y = ROTL(yp - xp, 6);
r->z = ROTL(zp - yp, 22);
#else
r->x = 15241094284759029579u * zp;
r->y = ROTL(yp - xp, 12);
r->z = ROTL(zp - yp, 44);
#endif
return xp;
}
static inline float
urandf(URand *r) {
uint32_t x = urand(r);
return (x >> (32-24)) * (1.0f / (((uint32_t)1) << 24));
}
static void
memrand(URand *r, void *ptr, size_t n)
{
unsigned char *p = (unsigned char*)ptr;
#ifdef __GNUC__
typedef ux __attribute__((__may_alias__)) uxa;
for (; n && (uintptr_t)p % sizeof(uxa); --n) *p++ = urand(r);
uxa *px = (uxa*)p;
for (; n > sizeof(ux); n -= sizeof(ux)) *px++ = urand(r);
p = (unsigned char*)px;
#endif
while (n--) *p++ = urand(r);
}
/* string conversions */
#if __riscv_xlen == 32
#define UXTOA_MAX 10
#else
#define UXTOA_MAX 20
#endif
static size_t
uxtoa(char *str, ux val)
{
char buf[UXTOA_MAX], *end = buf + sizeof buf, *it = end;
do *--it = (val % 10) + '0'; while ((val /= 10));
val = end - it;
memcpy(str, it, val);
return val;
}
#define FTOA_MAX (20+9+2)
static inline size_t
ftoa(char *str, fx val, size_t prec)
{
static const fx pow10[] = {
1, 10, 100, 1000, 10000, 100000, 1000000,
10000000, 100000000, 1000000000
};
char buf[FTOA_MAX], *end = buf + sizeof buf, *it = end;
prec = prec > 9 ? 9 : prec;
if (val != val) return memcpy(buf, "NaN", 3), 3;
if (val < 0) val = -val, *--it = '-';
ux ival = val, frac = (val-ival) * pow10[prec];
do *--it = frac % 10 + '0'; while ((frac /= 10, --prec));
*--it = '.';
do *--it = ival % 10 + '0'; while ((ival /= 10));
prec = end - it;
memcpy(str, it, prec);
return prec;
}
/* print API */
static char printBuffer[1024];
static char *printIt = printBuffer;
static char *const printEnd = printBuffer + sizeof printBuffer;
#define print_lit(s) print_raw(s, (sizeof s) - 1)
static void
print_flush(void)
{
memwrite(printBuffer, printIt - printBuffer);
printIt = printBuffer;
}
static void
print_raw(const char *s, size_t len)
{
if ((uintptr_t)(printEnd - printIt) > len) {
memcpy(printIt, s, len);
printIt += len;
} else {
print_flush();
memwrite(s, len);
}
}
static void print_s(const char *s) { print_raw(s, strlen(s)); }
static void
print_u(ux val)
{
if (printEnd - printIt < UXTOA_MAX)
print_flush();
printIt += uxtoa(printIt, val);
}
static void
print_h(ux val, size_t n)
{
if ((n << 2) >= sizeof printBuffer)
return;
if ((size_t)(printEnd - printIt) < n)
print_flush();
while (n--) {
*printIt++ = "0123456789abcdef"[(val >> (n << 2)) & 0xf];
}
}
static void
print_b(ux val, size_t n)
{
if (n >= sizeof printBuffer)
return;
if ((size_t)(printEnd - printIt) < n)
print_flush();
while (n--) {
*printIt++ = ((val >> n) & 1) + '0';
}
}
static void
print_fn(size_t prec, fx val)
{
if (printEnd - printIt < FTOA_MAX)
print_flush();
printIt += ftoa(printIt, val, prec);
}
static void print_f(fx val) { print_fn(7, val); }
#define ARR_LEN(x) (sizeof x / sizeof *(x))
#define PRINT_AT_1(a,b,...) b
#define PRINT_SELECT(a,b,...) PRINT_AT_1(b,PRINT_FUNC,)
#define PRINT_LIT ,print_lit
#define PRINT_FUNC(f,...) print_##f(__VA_ARGS__)
#define PRINT_A(...) PRINT_SELECT(__VA_ARGS__,PRINT_LIT,)(__VA_ARGS__), (void)PRINT_B
#define PRINT_B(...) PRINT_SELECT(__VA_ARGS__,PRINT_LIT,)(__VA_ARGS__), (void)PRINT_A
#define print PRINT_A
static const char PRINT_A = 0, PRINT_B = 0;
#define PRINT_TEST_A(_) 1+PRINT_TEST_B
#define PRINT_TEST_B(_) 1+PRINT_TEST_A
#if PRINT_TEST_A(0)(0)(0) != 3 // if you get an error here, see below
// Your preprocessor implementation doesn't support sequence iteration,
// you can make the code compile by continuing the following pattern
# undef PRINT_B
# define PRINT_B(...) PRINT_NEXT(__VA_ARGS__) PRINT_B1
# define PRINT_B1(...) PRINT_NEXT(__VA_ARGS__) PRINT_B2
#endif
#endif