-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathruntime_core.c
More file actions
535 lines (464 loc) · 15.7 KB
/
runtime_core.c
File metadata and controls
535 lines (464 loc) · 15.7 KB
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
* Copyright 2026 International Digital Economy Academy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef __cplusplus
extern "C" {
#endif
#include "moonbit.h"
#ifdef _MSC_VER
#define _Noreturn __declspec(noreturn)
#endif
#if defined(__clang__)
#pragma clang diagnostic ignored "-Wshift-op-parentheses"
#pragma clang diagnostic ignored "-Wtautological-compare"
#endif
MOONBIT_EXPORT _Noreturn void moonbit_panic(void);
MOONBIT_EXPORT void *moonbit_malloc_array(enum moonbit_block_kind kind,
int elem_size_shift, int32_t len);
MOONBIT_EXPORT int moonbit_val_array_equal(const void *lhs, const void *rhs);
MOONBIT_EXPORT moonbit_string_t moonbit_add_string(moonbit_string_t s1,
moonbit_string_t s2);
MOONBIT_EXPORT void moonbit_unsafe_bytes_blit(moonbit_bytes_t dst,
int32_t dst_start,
moonbit_bytes_t src,
int32_t src_offset, int32_t len);
MOONBIT_EXPORT moonbit_string_t moonbit_unsafe_bytes_sub_string(
moonbit_bytes_t bytes, int32_t start, int32_t len);
MOONBIT_EXPORT void moonbit_println(moonbit_string_t str);
MOONBIT_EXPORT moonbit_bytes_t *moonbit_get_cli_args(void);
MOONBIT_EXPORT void moonbit_runtime_init(int argc, char **argv);
MOONBIT_EXPORT void moonbit_drop_object(void *);
#define Moonbit_make_regular_object_header(ptr_field_offset, ptr_field_count, \
tag) \
(((uint32_t)moonbit_BLOCK_KIND_REGULAR << 30) | \
(((uint32_t)(ptr_field_offset) & (((uint32_t)1 << 11) - 1)) << 19) | \
(((uint32_t)(ptr_field_count) & (((uint32_t)1 << 11) - 1)) << 8) | \
((tag) & 0xFF))
// header manipulation macros
#define Moonbit_object_ptr_field_offset(obj) \
((Moonbit_object_header(obj)->meta >> 19) & (((uint32_t)1 << 11) - 1))
#define Moonbit_object_ptr_field_count(obj) \
((Moonbit_object_header(obj)->meta >> 8) & (((uint32_t)1 << 11) - 1))
#if !defined(_WIN64) && !defined(_WIN32)
void *malloc(size_t size);
void free(void *ptr);
#define libc_malloc malloc
#define libc_free free
#endif
// several important runtime functions are inlined
static void *moonbit_malloc_inlined(size_t size) {
struct moonbit_object *ptr = (struct moonbit_object *)libc_malloc(
sizeof(struct moonbit_object) + size);
ptr->rc = 1;
return ptr + 1;
}
#define moonbit_malloc(obj) moonbit_malloc_inlined(obj)
#define moonbit_free(obj) libc_free(Moonbit_object_header(obj))
static void moonbit_incref_inlined(void *ptr) {
struct moonbit_object *header = Moonbit_object_header(ptr);
int32_t const count = header->rc;
if (count > 0) {
header->rc = count + 1;
}
}
#define moonbit_incref moonbit_incref_inlined
static void moonbit_decref_inlined(void *ptr) {
struct moonbit_object *header = Moonbit_object_header(ptr);
int32_t const count = header->rc;
if (count > 1) {
header->rc = count - 1;
} else if (count == 1) {
moonbit_drop_object(ptr);
}
}
#define moonbit_decref moonbit_decref_inlined
#define moonbit_unsafe_make_string moonbit_make_string
// detect whether compiler builtins exist for advanced bitwise operations
#ifdef __has_builtin
#if __has_builtin(__builtin_clz)
#define HAS_BUILTIN_CLZ
#endif
#if __has_builtin(__builtin_ctz)
#define HAS_BUILTIN_CTZ
#endif
#if __has_builtin(__builtin_popcount)
#define HAS_BUILTIN_POPCNT
#endif
#if __has_builtin(__builtin_sqrt)
#define HAS_BUILTIN_SQRT
#endif
#if __has_builtin(__builtin_sqrtf)
#define HAS_BUILTIN_SQRTF
#endif
#if __has_builtin(__builtin_fabs)
#define HAS_BUILTIN_FABS
#endif
#if __has_builtin(__builtin_fabsf)
#define HAS_BUILTIN_FABSF
#endif
#endif
// if there is no builtin operators, use software implementation
#ifdef HAS_BUILTIN_CLZ
static inline int32_t moonbit_clz32(int32_t x) {
return x == 0 ? 32 : __builtin_clz(x);
}
static inline int32_t moonbit_clz64(int64_t x) {
return x == 0 ? 64 : __builtin_clzll(x);
}
#undef HAS_BUILTIN_CLZ
#else
// table for [clz] value of 4bit integer.
static const uint8_t moonbit_clz4[] = {4, 3, 2, 2, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0};
int32_t moonbit_clz32(uint32_t x) {
/* The ideas is to:
1. narrow down the 4bit block where the most signficant "1" bit lies,
using binary search
2. find the number of leading zeros in that 4bit block via table lookup
Different time/space tradeoff can be made here by enlarging the table
and do less binary search.
One benefit of the 4bit lookup table is that it can fit into a single cache
line.
*/
int32_t result = 0;
if (x > 0xffff) {
x >>= 16;
} else {
result += 16;
}
if (x > 0xff) {
x >>= 8;
} else {
result += 8;
}
if (x > 0xf) {
x >>= 4;
} else {
result += 4;
}
return result + moonbit_clz4[x];
}
int32_t moonbit_clz64(uint64_t x) {
int32_t result = 0;
if (x > 0xffffffff) {
x >>= 32;
} else {
result += 32;
}
return result + moonbit_clz32((uint32_t)x);
}
#endif
#ifdef HAS_BUILTIN_CTZ
static inline int32_t moonbit_ctz32(int32_t x) {
return x == 0 ? 32 : __builtin_ctz(x);
}
static inline int32_t moonbit_ctz64(int64_t x) {
return x == 0 ? 64 : __builtin_ctzll(x);
}
#undef HAS_BUILTIN_CTZ
#else
int32_t moonbit_ctz32(int32_t x) {
/* The algorithm comes from:
Leiserson, Charles E. et al. “Using de Bruijn Sequences to Index a 1 in a
Computer Word.” (1998).
The ideas is:
1. leave only the least significant "1" bit in the input,
set all other bits to "0". This is achieved via [x & -x]
2. now we have [x * n == n << ctz(x)], if [n] is a de bruijn sequence
(every 5bit pattern occurn exactly once when you cycle through the bit
string), we can find [ctz(x)] from the most significant 5 bits of [x * n]
*/
static const uint32_t de_bruijn_32 = 0x077CB531;
static const uint8_t index32[] = {0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20,
15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19,
16, 7, 26, 12, 18, 6, 11, 5, 10, 9};
return (x == 0) * 32 + index32[(de_bruijn_32 * (x & -x)) >> 27];
}
int32_t moonbit_ctz64(int64_t x) {
static const uint64_t de_bruijn_64 = 0x0218A392CD3D5DBF;
static const uint8_t index64[] = {
0, 1, 2, 7, 3, 13, 8, 19, 4, 25, 14, 28, 9, 34, 20, 40,
5, 17, 26, 38, 15, 46, 29, 48, 10, 31, 35, 54, 21, 50, 41, 57,
63, 6, 12, 18, 24, 27, 33, 39, 16, 37, 45, 47, 30, 53, 49, 56,
62, 11, 23, 32, 36, 44, 52, 55, 61, 22, 43, 51, 60, 42, 59, 58};
return (x == 0) * 64 + index64[(de_bruijn_64 * (x & -x)) >> 58];
}
#endif
#ifdef HAS_BUILTIN_POPCNT
#define moonbit_popcnt32 __builtin_popcount
#define moonbit_popcnt64 __builtin_popcountll
#undef HAS_BUILTIN_POPCNT
#else
int32_t moonbit_popcnt32(uint32_t x) {
/* The classic SIMD Within A Register algorithm.
ref: [https://nimrod.blog/posts/algorithms-behind-popcount/]
*/
x = x - ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
x = (x + (x >> 4)) & 0x0F0F0F0F;
return (x * 0x01010101) >> 24;
}
int32_t moonbit_popcnt64(uint64_t x) {
x = x - ((x >> 1) & 0x5555555555555555);
x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333);
x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0F;
return (x * 0x0101010101010101) >> 56;
}
#endif
/* The following sqrt implementation comes from
[musl](https://git.musl-libc.org/cgit/musl),
with some helpers inlined to make it zero dependency.
*/
#ifdef MOONBIT_NATIVE_NO_SYS_HEADER
const uint16_t __rsqrt_tab[128] = {
0xb451, 0xb2f0, 0xb196, 0xb044, 0xaef9, 0xadb6, 0xac79, 0xab43, 0xaa14,
0xa8eb, 0xa7c8, 0xa6aa, 0xa592, 0xa480, 0xa373, 0xa26b, 0xa168, 0xa06a,
0x9f70, 0x9e7b, 0x9d8a, 0x9c9d, 0x9bb5, 0x9ad1, 0x99f0, 0x9913, 0x983a,
0x9765, 0x9693, 0x95c4, 0x94f8, 0x9430, 0x936b, 0x92a9, 0x91ea, 0x912e,
0x9075, 0x8fbe, 0x8f0a, 0x8e59, 0x8daa, 0x8cfe, 0x8c54, 0x8bac, 0x8b07,
0x8a64, 0x89c4, 0x8925, 0x8889, 0x87ee, 0x8756, 0x86c0, 0x862b, 0x8599,
0x8508, 0x8479, 0x83ec, 0x8361, 0x82d8, 0x8250, 0x81c9, 0x8145, 0x80c2,
0x8040, 0xff02, 0xfd0e, 0xfb25, 0xf947, 0xf773, 0xf5aa, 0xf3ea, 0xf234,
0xf087, 0xeee3, 0xed47, 0xebb3, 0xea27, 0xe8a3, 0xe727, 0xe5b2, 0xe443,
0xe2dc, 0xe17a, 0xe020, 0xdecb, 0xdd7d, 0xdc34, 0xdaf1, 0xd9b3, 0xd87b,
0xd748, 0xd61a, 0xd4f1, 0xd3cd, 0xd2ad, 0xd192, 0xd07b, 0xcf69, 0xce5b,
0xcd51, 0xcc4a, 0xcb48, 0xca4a, 0xc94f, 0xc858, 0xc764, 0xc674, 0xc587,
0xc49d, 0xc3b7, 0xc2d4, 0xc1f4, 0xc116, 0xc03c, 0xbf65, 0xbe90, 0xbdbe,
0xbcef, 0xbc23, 0xbb59, 0xba91, 0xb9cc, 0xb90a, 0xb84a, 0xb78c, 0xb6d0,
0xb617, 0xb560,
};
/* returns a*b*2^-32 - e, with error 0 <= e < 1. */
static inline uint32_t mul32(uint32_t a, uint32_t b) {
return (uint64_t)a * b >> 32;
}
#endif
#ifdef MOONBIT_NATIVE_NO_SYS_HEADER
float sqrtf(float x) {
uint32_t ix, m, m1, m0, even, ey;
ix = *(uint32_t *)&x;
if (ix - 0x00800000 >= 0x7f800000 - 0x00800000) {
/* x < 0x1p-126 or inf or nan. */
if (ix * 2 == 0)
return x;
if (ix == 0x7f800000)
return x;
if (ix > 0x7f800000)
return (x - x) / (x - x);
/* x is subnormal, normalize it. */
x *= 0x1p23f;
ix = *(uint32_t *)&x;
ix -= 23 << 23;
}
/* x = 4^e m; with int e and m in [1, 4). */
even = ix & 0x00800000;
m1 = (ix << 8) | 0x80000000;
m0 = (ix << 7) & 0x7fffffff;
m = even ? m0 : m1;
/* 2^e is the exponent part of the return value. */
ey = ix >> 1;
ey += 0x3f800000 >> 1;
ey &= 0x7f800000;
/* compute r ~ 1/sqrt(m), s ~ sqrt(m) with 2 goldschmidt iterations. */
static const uint32_t three = 0xc0000000;
uint32_t r, s, d, u, i;
i = (ix >> 17) % 128;
r = (uint32_t)__rsqrt_tab[i] << 16;
/* |r*sqrt(m) - 1| < 0x1p-8 */
s = mul32(m, r);
/* |s/sqrt(m) - 1| < 0x1p-8 */
d = mul32(s, r);
u = three - d;
r = mul32(r, u) << 1;
/* |r*sqrt(m) - 1| < 0x1.7bp-16 */
s = mul32(s, u) << 1;
/* |s/sqrt(m) - 1| < 0x1.7bp-16 */
d = mul32(s, r);
u = three - d;
s = mul32(s, u);
/* -0x1.03p-28 < s/sqrt(m) - 1 < 0x1.fp-31 */
s = (s - 1) >> 6;
/* s < sqrt(m) < s + 0x1.08p-23 */
/* compute nearest rounded result. */
uint32_t d0, d1, d2;
float y, t;
d0 = (m << 16) - s * s;
d1 = s - d0;
d2 = d1 + s + 1;
s += d1 >> 31;
s &= 0x007fffff;
s |= ey;
y = *(float *)&s;
/* handle rounding and inexact exception. */
uint32_t tiny = d2 == 0 ? 0 : 0x01000000;
tiny |= (d1 ^ d2) & 0x80000000;
t = *(float *)&tiny;
y = y + t;
return y;
}
#endif
#ifdef MOONBIT_NATIVE_NO_SYS_HEADER
/* returns a*b*2^-64 - e, with error 0 <= e < 3. */
static inline uint64_t mul64(uint64_t a, uint64_t b) {
uint64_t ahi = a >> 32;
uint64_t alo = a & 0xffffffff;
uint64_t bhi = b >> 32;
uint64_t blo = b & 0xffffffff;
return ahi * bhi + (ahi * blo >> 32) + (alo * bhi >> 32);
}
double sqrt(double x) {
uint64_t ix, top, m;
/* special case handling. */
ix = *(uint64_t *)&x;
top = ix >> 52;
if (top - 0x001 >= 0x7ff - 0x001) {
/* x < 0x1p-1022 or inf or nan. */
if (ix * 2 == 0)
return x;
if (ix == 0x7ff0000000000000)
return x;
if (ix > 0x7ff0000000000000)
return (x - x) / (x - x);
/* x is subnormal, normalize it. */
x *= 0x1p52;
ix = *(uint64_t *)&x;
top = ix >> 52;
top -= 52;
}
/* argument reduction:
x = 4^e m; with integer e, and m in [1, 4)
m: fixed point representation [2.62]
2^e is the exponent part of the result. */
int even = top & 1;
m = (ix << 11) | 0x8000000000000000;
if (even)
m >>= 1;
top = (top + 0x3ff) >> 1;
/* approximate r ~ 1/sqrt(m) and s ~ sqrt(m) when m in [1,4)
initial estimate:
7bit table lookup (1bit exponent and 6bit significand).
iterative approximation:
using 2 goldschmidt iterations with 32bit int arithmetics
and a final iteration with 64bit int arithmetics.
details:
the relative error (e = r0 sqrt(m)-1) of a linear estimate
(r0 = a m + b) is |e| < 0.085955 ~ 0x1.6p-4 at best,
a table lookup is faster and needs one less iteration
6 bit lookup table (128b) gives |e| < 0x1.f9p-8
7 bit lookup table (256b) gives |e| < 0x1.fdp-9
for single and double prec 6bit is enough but for quad
prec 7bit is needed (or modified iterations). to avoid
one more iteration >=13bit table would be needed (16k).
a newton-raphson iteration for r is
w = r*r
u = 3 - m*w
r = r*u/2
can use a goldschmidt iteration for s at the end or
s = m*r
first goldschmidt iteration is
s = m*r
u = 3 - s*r
r = r*u/2
s = s*u/2
next goldschmidt iteration is
u = 3 - s*r
r = r*u/2
s = s*u/2
and at the end r is not computed only s.
they use the same amount of operations and converge at the
same quadratic rate, i.e. if
r1 sqrt(m) - 1 = e, then
r2 sqrt(m) - 1 = -3/2 e^2 - 1/2 e^3
the advantage of goldschmidt is that the mul for s and r
are independent (computed in parallel), however it is not
"self synchronizing": it only uses the input m in the
first iteration so rounding errors accumulate. at the end
or when switching to larger precision arithmetics rounding
errors dominate so the first iteration should be used.
the fixed point representations are
m: 2.30 r: 0.32, s: 2.30, d: 2.30, u: 2.30, three: 2.30
and after switching to 64 bit
m: 2.62 r: 0.64, s: 2.62, d: 2.62, u: 2.62, three: 2.62 */
static const uint64_t three = 0xc0000000;
uint64_t r, s, d, u, i;
i = (ix >> 46) % 128;
r = (uint32_t)__rsqrt_tab[i] << 16;
/* |r sqrt(m) - 1| < 0x1.fdp-9 */
s = mul32(m >> 32, r);
/* |s/sqrt(m) - 1| < 0x1.fdp-9 */
d = mul32(s, r);
u = three - d;
r = mul32(r, u) << 1;
/* |r sqrt(m) - 1| < 0x1.7bp-16 */
s = mul32(s, u) << 1;
/* |s/sqrt(m) - 1| < 0x1.7bp-16 */
d = mul32(s, r);
u = three - d;
r = mul32(r, u) << 1;
/* |r sqrt(m) - 1| < 0x1.3704p-29 (measured worst-case) */
r = r << 32;
s = mul64(m, r);
d = mul64(s, r);
u = (three << 32) - d;
s = mul64(s, u); /* repr: 3.61 */
/* -0x1p-57 < s - sqrt(m) < 0x1.8001p-61 */
s = (s - 2) >> 9; /* repr: 12.52 */
/* -0x1.09p-52 < s - sqrt(m) < -0x1.fffcp-63 */
/* s < sqrt(m) < s + 0x1.09p-52,
compute nearest rounded result:
the nearest result to 52 bits is either s or s+0x1p-52,
we can decide by comparing (2^52 s + 0.5)^2 to 2^104 m. */
uint64_t d0, d1, d2;
double y, t;
d0 = (m << 42) - s * s;
d1 = s - d0;
d2 = d1 + s + 1;
s += d1 >> 63;
s &= 0x000fffffffffffff;
s |= top << 52;
y = *(double *)&s;
return y;
}
#endif
#ifdef MOONBIT_NATIVE_NO_SYS_HEADER
double fabs(double x) {
union {
double f;
uint64_t i;
} u = {x};
u.i &= 0x7fffffffffffffffULL;
return u.f;
}
#endif
#ifdef MOONBIT_NATIVE_NO_SYS_HEADER
float fabsf(float x) {
union {
float f;
uint32_t i;
} u = {x};
u.i &= 0x7fffffff;
return u.f;
}
#endif
#ifdef _MSC_VER
/* MSVC treats syntactic division by zero as fatal error,
even for float point numbers,
so we have to use a constant variable to work around this */
static const int MOONBIT_ZERO = 0;
#else
#define MOONBIT_ZERO 0
#endif
#ifdef __cplusplus
}
#endif