@amritk/nish 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +553 -0
- package/bin/launcher.js +186 -0
- package/bin/nish +23 -0
- package/bin/packaging.js +220 -0
- package/docs/AI.md +1076 -0
- package/docs/INSTALL.md +468 -0
- package/llms.txt +49 -0
- package/package.json +87 -0
- package/runtime/nish.d.ts +290 -0
- package/runtime/nish.h +340 -0
- package/runtime/nish.mjs +143 -0
- package/runtime/runtime.c +1184 -0
- package/runtime/runtime_os.c +351 -0
- package/runtime/runtime_parallel.c +156 -0
- package/runtime/runtime_wasm.c +99 -0
- package/runtime/shim.mjs +672 -0
- package/scripts/bootstrap.sh +357 -0
- package/scripts/build.sh +279 -0
- package/scripts/changelog-gen.mjs +528 -0
- package/scripts/changelog-section.sh +28 -0
- package/scripts/ci-profile.mjs +187 -0
- package/scripts/codes-registry.js +73 -0
- package/scripts/gen-diagnostic-codes.mjs +199 -0
- package/scripts/gen-pow5-tables.py +45 -0
- package/scripts/nish-compiler.sh +17 -0
- package/scripts/platform-package.mjs +91 -0
- package/scripts/postinstall.mjs +133 -0
- package/scripts/size-report.sh +72 -0
- package/scripts/smoke.sh +94 -0
- package/scripts/verify-binaries.sh +213 -0
- package/std/README.md +185 -0
- package/std/json.ts +402 -0
- package/std/pair.ts +28 -0
- package/std/testing.ts +347 -0
- package/std/text.ts +193 -0
|
@@ -0,0 +1,1184 @@
|
|
|
1
|
+
/* Nish runtime: arena + strings + cold paths. Layouts are ABI (runtime.ts, nish.h).
|
|
2
|
+
*
|
|
3
|
+
* The half of the runtime every program touches whatever it does, which is why
|
|
4
|
+
* it is also the half with the tighter size budget: the arena, strings, number
|
|
5
|
+
* formatting, the array cold paths, `process.argv`, `Math.random`, and the two
|
|
6
|
+
* panics. Files, directories, subprocesses, the environment and the clock are
|
|
7
|
+
* in runtime_os.c — every one of those wraps a system call, so that surface
|
|
8
|
+
* grows as the language reaches further into the operating system, and a
|
|
9
|
+
* program that reaches nowhere should not pay for it or be measured with it.
|
|
10
|
+
* runtime_os.c's header comment has the reasoning; tests/run.js gates the two
|
|
11
|
+
* `.text*` budgets separately and docs/wp7-runtime.md records both.
|
|
12
|
+
*
|
|
13
|
+
* Nothing here calls into runtime_os.c, which is why an old link line that
|
|
14
|
+
* names runtime.c alone still builds a program that uses none of that surface.
|
|
15
|
+
* The other direction does happen: `nish_readdir` allocates through
|
|
16
|
+
* `nish_alloc_struct` and `nish_str_new`, so those calls no longer inline into
|
|
17
|
+
* it without LTO. Measured in docs/wp7-runtime.md; the speed and size profiles
|
|
18
|
+
* both use `-flto`, and the cold paths that lost the inlining are cold.
|
|
19
|
+
*/
|
|
20
|
+
#define _POSIX_C_SOURCE 200809L
|
|
21
|
+
#include <inttypes.h>
|
|
22
|
+
#include <math.h>
|
|
23
|
+
#include <stddef.h>
|
|
24
|
+
#include <stdint.h>
|
|
25
|
+
#include <stdio.h>
|
|
26
|
+
#include <stdlib.h>
|
|
27
|
+
#include <string.h>
|
|
28
|
+
#include <time.h>
|
|
29
|
+
#include <unistd.h>
|
|
30
|
+
|
|
31
|
+
#ifdef __wasi__
|
|
32
|
+
/* No pids on WASI: clock nanoseconds salt the RNG seed instead. */
|
|
33
|
+
static int nish_wasi_salt(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return ts.tv_nsec; }
|
|
34
|
+
#define getpid nish_wasi_salt
|
|
35
|
+
/* wasi-libc's `_start` calls `__main_argc_argv` (clang's name for a C `main`); the IR entry is
|
|
36
|
+
* `main`. Weak, so a reactor build without an entry links. */
|
|
37
|
+
__attribute__((weak)) int nish_c_main(int, char **) __asm__("main");
|
|
38
|
+
int __main_argc_argv(int argc, char **argv) { return nish_c_main(argc, argv); }
|
|
39
|
+
#endif
|
|
40
|
+
|
|
41
|
+
#define NISH_COLD __attribute__((noreturn, cold, noinline))
|
|
42
|
+
|
|
43
|
+
/* ---- Thread-local state (WP20 T0)
|
|
44
|
+
The arena and the RNG seed are the only mutable process-wide objects here,
|
|
45
|
+
and both are a data race the moment a second thread runs compiled code. With
|
|
46
|
+
`-DNISH_THREADS` each thread gets its own, which is what `nish --threads`
|
|
47
|
+
compiles against: the flag makes the compiler emit `@nish_arena` as a
|
|
48
|
+
`thread_local(initialexec)` global, and a build that disagreed with this
|
|
49
|
+
file would not link at all, because ELF refuses a non-TLS reference to a TLS
|
|
50
|
+
definition. Off by default so the ordinary build pays nothing.
|
|
51
|
+
|
|
52
|
+
The same definition is in runtime/nish.h (for a host that includes it) and
|
|
53
|
+
runtime/runtime_wasm.c; they are one contract and move together. */
|
|
54
|
+
#ifdef NISH_THREADS
|
|
55
|
+
#define NISH_TLS _Thread_local
|
|
56
|
+
#else
|
|
57
|
+
#define NISH_TLS
|
|
58
|
+
#endif
|
|
59
|
+
|
|
60
|
+
/* ---- Arena: %struct.nish_arena = type { i8*, i64, i64, i8* }
|
|
61
|
+
|
|
62
|
+
The widths are fixed rather than `size_t` for the reason runtime_wasm.c
|
|
63
|
+
gives for its own copy: every compiled function inlines the bump allocator
|
|
64
|
+
and reads these fields directly, so the IR's `i64` is what `off` and `cap`
|
|
65
|
+
have to be on every target, not just the 64-bit ones. With `size_t` they
|
|
66
|
+
were four bytes each under wasm32 and the inlined fast path bumped an
|
|
67
|
+
offset that was really `cap`, then compared it against memory past the end
|
|
68
|
+
of the global — a wild pointer on the wasi profile for any program that
|
|
69
|
+
allocates from compiled code. The asserts below are the contract, checked
|
|
70
|
+
wherever this file is compiled. */
|
|
71
|
+
typedef struct nish_chunk { struct nish_chunk *next; uint64_t cap; } nish_chunk;
|
|
72
|
+
struct nish_arena { char *buf; uint64_t off; uint64_t cap; nish_chunk *chunks; };
|
|
73
|
+
/* One per thread under -DNISH_THREADS, one per process otherwise; the layout is
|
|
74
|
+
the same either way, which is why nothing else in this file has to know. */
|
|
75
|
+
NISH_TLS struct nish_arena nish_arena;
|
|
76
|
+
|
|
77
|
+
_Static_assert(sizeof(struct nish_arena) == 32, "arena layout is ABI: runtime.ts, nish.h");
|
|
78
|
+
_Static_assert(offsetof(struct nish_arena, off) == 8, "the inlined allocator bumps field 1");
|
|
79
|
+
_Static_assert(offsetof(struct nish_arena, cap) == 16, "the inlined allocator reads field 2");
|
|
80
|
+
_Static_assert(offsetof(struct nish_arena, chunks) == 24, "arena layout is ABI");
|
|
81
|
+
|
|
82
|
+
static NISH_COLD void nish_die(const char *msg) {
|
|
83
|
+
(void)!write(2, msg, strlen(msg));
|
|
84
|
+
_exit(1);
|
|
85
|
+
}
|
|
86
|
+
#define nish_oom() nish_die("nish: out of memory\n")
|
|
87
|
+
|
|
88
|
+
/* Slow path (size 8-byte rounded): push a chunk (>= 64 KB), bump from it. */
|
|
89
|
+
void *nish_arena_grow(uint64_t size) {
|
|
90
|
+
uint64_t cap = size > 65536 ? size : 65536;
|
|
91
|
+
/* A 64-bit request cannot always be asked for: wasm32's `size_t` is 32 bits,
|
|
92
|
+
and malloc would silently take the low half of a chunk nobody can address. */
|
|
93
|
+
if (cap > (uint64_t)(SIZE_MAX - sizeof(nish_chunk))) nish_oom();
|
|
94
|
+
nish_chunk *c = malloc(sizeof *c + (size_t)cap);
|
|
95
|
+
if (!c) nish_oom();
|
|
96
|
+
c->next = nish_arena.chunks;
|
|
97
|
+
c->cap = cap;
|
|
98
|
+
nish_arena.chunks = c;
|
|
99
|
+
nish_arena.buf = (char *)(c + 1);
|
|
100
|
+
nish_arena.off = size;
|
|
101
|
+
nish_arena.cap = cap;
|
|
102
|
+
return nish_arena.buf;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/* Bump allocation, 8-byte rounded/aligned, uninitialised; the compiler inlines it. */
|
|
106
|
+
void *nish_alloc_struct(uint64_t size) {
|
|
107
|
+
size = (size + 7) & ~(uint64_t)7;
|
|
108
|
+
if (nish_arena.off + size <= nish_arena.cap) {
|
|
109
|
+
void *p = nish_arena.buf + nish_arena.off;
|
|
110
|
+
nish_arena.off += size;
|
|
111
|
+
return p;
|
|
112
|
+
}
|
|
113
|
+
return nish_arena_grow(size);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
static void nish_free_until(nish_chunk *c, const nish_chunk *end) {
|
|
117
|
+
while (c != end) { nish_chunk *n = c->next; free(c); c = n; }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/* O(1) recycle: keep the newest chunk, free the rest. */
|
|
121
|
+
void nish_reset_arena(void) {
|
|
122
|
+
nish_chunk *keep = nish_arena.chunks;
|
|
123
|
+
if (!keep) return;
|
|
124
|
+
nish_free_until(keep->next, 0);
|
|
125
|
+
keep->next = 0;
|
|
126
|
+
nish_arena.off = 0;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
void nish_free_arena(void) {
|
|
130
|
+
nish_free_until(nish_arena.chunks, 0);
|
|
131
|
+
memset(&nish_arena, 0, sizeof nish_arena);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/* ---- Scopes: mark = buf + off (0 while empty); release rewinds, freeing newer chunks. */
|
|
135
|
+
uint64_t nish_arena_mark(void) {
|
|
136
|
+
return nish_arena.buf ? (uintptr_t)(nish_arena.buf + nish_arena.off) : 0;
|
|
137
|
+
}
|
|
138
|
+
uint64_t nish_arena_used(void) { return nish_arena.off; }
|
|
139
|
+
|
|
140
|
+
void nish_arena_release(uint64_t mark) {
|
|
141
|
+
nish_chunk *c = nish_arena.chunks;
|
|
142
|
+
if (!mark) { nish_reset_arena(); return; }
|
|
143
|
+
for (; c; c = c->next) {
|
|
144
|
+
uintptr_t base = (uintptr_t)(c + 1);
|
|
145
|
+
if (mark >= base && mark <= base + c->cap) break;
|
|
146
|
+
}
|
|
147
|
+
if (!c) return;
|
|
148
|
+
nish_free_until(nish_arena.chunks, c);
|
|
149
|
+
nish_arena = (struct nish_arena){ (char *)(c + 1), mark - (uintptr_t)(c + 1), c->cap, c };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/* Release back to `mark` while keeping the newest allocation; returns where it
|
|
153
|
+
ended up. `p` must be the last block the arena handed out, which is why only
|
|
154
|
+
a *string* may be kept: it is one flat block with no interior pointers, so
|
|
155
|
+
moving its bytes moves the whole value. Two outcomes, both reclaiming only
|
|
156
|
+
memory bumped after `mark` and before `p`:
|
|
157
|
+
- the mark's chunk has room below it: `p` moves down to `mark` and every
|
|
158
|
+
newer chunk is freed;
|
|
159
|
+
- it does not (the mark sat at the end of a full chunk): `p` stays where
|
|
160
|
+
it is and the chunks strictly between the two are freed.
|
|
161
|
+
The guards refuse instead, which only means less is reclaimed: `p` outside
|
|
162
|
+
the current chunk (a literal, or a value older than the mark), a mark in no
|
|
163
|
+
live chunk (a stale one, or 0 for an arena that was empty), or a mark newer
|
|
164
|
+
than `p`. */
|
|
165
|
+
void *nish_arena_keep(uint64_t mark, void *p) {
|
|
166
|
+
uintptr_t base = (uintptr_t)nish_arena.buf, q = (uintptr_t)p, top = base + nish_arena.off;
|
|
167
|
+
if (q < base || q >= top || mark == q) return p;
|
|
168
|
+
size_t size = top - q;
|
|
169
|
+
nish_chunk *pc = nish_arena.chunks, *c = pc; /* pc holds `p`: it is the current chunk */
|
|
170
|
+
for (; c; c = c->next) {
|
|
171
|
+
uintptr_t cb = (uintptr_t)(c + 1);
|
|
172
|
+
if (mark >= cb && mark <= cb + c->cap) break;
|
|
173
|
+
}
|
|
174
|
+
if (!c || (c == pc && mark > q)) return p;
|
|
175
|
+
/* mark <= q inside one chunk implies mark + size <= q + size <= its end, so
|
|
176
|
+
the second arm is only ever reached with `c` older than `pc`. */
|
|
177
|
+
if (mark + size <= (uintptr_t)(c + 1) + c->cap) {
|
|
178
|
+
memmove((void *)mark, p, size);
|
|
179
|
+
nish_free_until(pc, c);
|
|
180
|
+
nish_arena = (struct nish_arena){ (char *)(c + 1), mark + size - (uintptr_t)(c + 1), c->cap, c };
|
|
181
|
+
return (void *)mark;
|
|
182
|
+
}
|
|
183
|
+
nish_free_until(pc->next, c);
|
|
184
|
+
pc->next = c;
|
|
185
|
+
return p;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/* ---- Strings: { i64 len, bytes[len], '\0' }, immutable; 8 = the len header */
|
|
189
|
+
typedef struct { uint64_t len; char data[]; } nish_str;
|
|
190
|
+
|
|
191
|
+
nish_str *nish_str_new(const char *bytes, uint64_t len) {
|
|
192
|
+
nish_str *s = nish_alloc_struct(8 + len + 1);
|
|
193
|
+
s->len = len;
|
|
194
|
+
if (len) memcpy(s->data, bytes, len);
|
|
195
|
+
s->data[len] = 0;
|
|
196
|
+
return s;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
nish_str *nish_str_concat(const nish_str *a, const nish_str *b) {
|
|
200
|
+
nish_str *s = nish_alloc_struct(8 + a->len + b->len + 1);
|
|
201
|
+
s->len = a->len + b->len;
|
|
202
|
+
memcpy(s->data, a->data, a->len);
|
|
203
|
+
memcpy(s->data + a->len, b->data, b->len);
|
|
204
|
+
s->data[s->len] = 0;
|
|
205
|
+
return s;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
_Bool nish_str_eq(const nish_str *a, const nish_str *b) {
|
|
209
|
+
return a == b || (a->len == b->len && memcmp(a->data, b->data, a->len) == 0);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
uint64_t nish_str_len(const nish_str *s) { return s->len; }
|
|
213
|
+
|
|
214
|
+
/* `s.startsWith(sub)` is at 0, `s.endsWith(sub)` at len - sub->len, which is
|
|
215
|
+
negative when `sub` is the longer string. */
|
|
216
|
+
_Bool nish_str_at(const nish_str *s, int64_t at, const nish_str *sub) {
|
|
217
|
+
return at >= 0 && (uint64_t)at + sub->len <= s->len && !memcmp(s->data + at, sub->data, sub->len);
|
|
218
|
+
}
|
|
219
|
+
/* `s.indexOf(sub)`: the first byte offset where `sub` occurs, or -1. An empty
|
|
220
|
+
needle answers 0 and a needle longer than the haystack -1, both of which are
|
|
221
|
+
what JavaScript says and what the inline loop this replaces did.
|
|
222
|
+
|
|
223
|
+
This was lowered inline, one `nish_str_at` per offset, to keep `runtime.c`
|
|
224
|
+
small. That cost a byte-at-a-time scan — 53.7 ms over 52 MB of haystack —
|
|
225
|
+
and WP15 section 7's rule is that the budget yields to a measured win, so the
|
|
226
|
+
search moved here: `memchr` finds a candidate first byte and `memcmp`
|
|
227
|
+
confirms it, both the libc's vectorised routines. 2.9 ms, or 3.1 ms when the
|
|
228
|
+
needle starts with a byte the haystack is full of.
|
|
229
|
+
|
|
230
|
+
`memmem` is deliberately not used, and costs little: 2.5 ms on the same two
|
|
231
|
+
measurements. It is a GNU extension glibc hides behind `_GNU_SOURCE`, and
|
|
232
|
+
defining that here makes `<string.h>` pull in `<strings.h>` — which any `-I`
|
|
233
|
+
directory holding a file of that name then shadows. This project generates
|
|
234
|
+
exactly such a header from `examples/strings.ts`, and the interop tests
|
|
235
|
+
caught runtime.c picking it up and inheriting `nish.h` through it. A C host
|
|
236
|
+
would hit the same. A fifth of the time is not worth making the runtime
|
|
237
|
+
sensitive to its includer's `-I` paths. */
|
|
238
|
+
int64_t nish_str_index_of(const nish_str *s, const nish_str *sub) {
|
|
239
|
+
if (sub->len == 0) return 0;
|
|
240
|
+
if (sub->len > s->len) return -1;
|
|
241
|
+
const char *p = s->data, *end = s->data + s->len - sub->len + 1;
|
|
242
|
+
while (p < end) {
|
|
243
|
+
const char *c = memchr(p, sub->data[0], (size_t)(end - p));
|
|
244
|
+
if (!c) return -1;
|
|
245
|
+
if (memcmp(c + 1, sub->data + 1, sub->len - 1) == 0) return (int64_t)(c - s->data);
|
|
246
|
+
p = c + 1;
|
|
247
|
+
}
|
|
248
|
+
return -1;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/* `console.log` / `console.error` / `write` / `writeError` and the message of
|
|
252
|
+
`panic`: fd 1 or 2, with or without the trailing newline. */
|
|
253
|
+
void nish_write(const nish_str *s, int32_t fd, _Bool newline) {
|
|
254
|
+
(void)!write(fd, s->data, s->len);
|
|
255
|
+
if (newline) (void)!write(fd, "\n", 1);
|
|
256
|
+
}
|
|
257
|
+
/* console.log(s) */
|
|
258
|
+
void nish_print(const nish_str *s) { nish_write(s, 1, 1); }
|
|
259
|
+
|
|
260
|
+
/* Decimal digits of `u`, with a '-' in front when `neg`. 20 digits is the
|
|
261
|
+
widest a uint64_t can be, plus the sign. */
|
|
262
|
+
static nish_str *str_from_digits(uint64_t u, int neg) {
|
|
263
|
+
char tmp[21], *p = tmp + 21;
|
|
264
|
+
do { *--p = '0' + u % 10; u /= 10; } while (u);
|
|
265
|
+
if (neg) *--p = '-';
|
|
266
|
+
return nish_str_new(p, tmp + 21 - p);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
nish_str *nish_str_from_i64(int64_t v) {
|
|
270
|
+
return str_from_digits(v < 0 ? -(uint64_t)v : (uint64_t)v, v < 0);
|
|
271
|
+
}
|
|
272
|
+
nish_str *nish_str_from_i32(int32_t v) { return nish_str_from_i64(v); }
|
|
273
|
+
/* The one unsigned formatter (WP15): u8/u16/u32 are zero-extended by the
|
|
274
|
+
caller, so 0xFFFFFFFF prints as 4294967295 rather than -1. */
|
|
275
|
+
nish_str *nish_str_from_u64(uint64_t v) { return str_from_digits(v, 0); }
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
/* ---- Shortest round-trip digits (Ryu)
|
|
279
|
+
|
|
280
|
+
`String(x)` in JavaScript prints the *fewest* digits that read back as the
|
|
281
|
+
same double, and the language promises that spelling. Reaching it by asking
|
|
282
|
+
snprintf for k digits and strtod whether they round-trip -- which is what
|
|
283
|
+
this used to do -- is both slow (up to seventeen format-and-parse round
|
|
284
|
+
trips, 2.6 us per number) and, less obviously, *wrong*: printf can only hand
|
|
285
|
+
back the correctly-rounded k-digit string, and for some values the shortest
|
|
286
|
+
string that round-trips at that length is a different one. The old search
|
|
287
|
+
printed all seventeen digits of 7.120236347223045e-307 for that reason.
|
|
288
|
+
|
|
289
|
+
Ryu (Ulf Adams, PLDI 2018) computes the digits directly. The two tables are
|
|
290
|
+
125-bit fixed-point approximations of 5^-i and 5^i, generated with exact
|
|
291
|
+
integer arithmetic rather than transcribed. Read the paper before touching
|
|
292
|
+
the algorithm: the multipliers in `nish_pow5bits`, `nish_log10pow2` and
|
|
293
|
+
`nish_log10pow5` are exact over the ranges a double's exponent can reach
|
|
294
|
+
and nowhere else. */
|
|
295
|
+
static const uint64_t NISH_POW5_INV_SPLIT[292][2] = {
|
|
296
|
+
{ 1u, 2305843009213693952u },
|
|
297
|
+
{ 11068046444225730970u, 1844674407370955161u },
|
|
298
|
+
{ 5165088340638674453u, 1475739525896764129u },
|
|
299
|
+
{ 7821419487252849886u, 1180591620717411303u },
|
|
300
|
+
{ 8824922364862649494u, 1888946593147858085u },
|
|
301
|
+
{ 7059937891890119595u, 1511157274518286468u },
|
|
302
|
+
{ 13026647942995916322u, 1208925819614629174u },
|
|
303
|
+
{ 9774590264567735146u, 1934281311383406679u },
|
|
304
|
+
{ 11509021026396098440u, 1547425049106725343u },
|
|
305
|
+
{ 16585914450600699399u, 1237940039285380274u },
|
|
306
|
+
{ 15469416676735388068u, 1980704062856608439u },
|
|
307
|
+
{ 16064882156130220778u, 1584563250285286751u },
|
|
308
|
+
{ 9162556910162266299u, 1267650600228229401u },
|
|
309
|
+
{ 7281393426775805432u, 2028240960365167042u },
|
|
310
|
+
{ 16893161185646375315u, 1622592768292133633u },
|
|
311
|
+
{ 2446482504291369283u, 1298074214633706907u },
|
|
312
|
+
{ 7603720821608101175u, 2076918743413931051u },
|
|
313
|
+
{ 2393627842544570617u, 1661534994731144841u },
|
|
314
|
+
{ 16672297533003297786u, 1329227995784915872u },
|
|
315
|
+
{ 11918280793837635165u, 2126764793255865396u },
|
|
316
|
+
{ 5845275820328197809u, 1701411834604692317u },
|
|
317
|
+
{ 15744267100488289217u, 1361129467683753853u },
|
|
318
|
+
{ 3054734472329800808u, 2177807148294006166u },
|
|
319
|
+
{ 17201182836831481939u, 1742245718635204932u },
|
|
320
|
+
{ 6382248639981364905u, 1393796574908163946u },
|
|
321
|
+
{ 2832900194486363201u, 2230074519853062314u },
|
|
322
|
+
{ 5955668970331000884u, 1784059615882449851u },
|
|
323
|
+
{ 1075186361522890384u, 1427247692705959881u },
|
|
324
|
+
{ 12788344622662355584u, 2283596308329535809u },
|
|
325
|
+
{ 13920024512871794791u, 1826877046663628647u },
|
|
326
|
+
{ 3757321980813615186u, 1461501637330902918u },
|
|
327
|
+
{ 10384555214134712795u, 1169201309864722334u },
|
|
328
|
+
{ 5547241898389809503u, 1870722095783555735u },
|
|
329
|
+
{ 4437793518711847602u, 1496577676626844588u },
|
|
330
|
+
{ 10928932444453298728u, 1197262141301475670u },
|
|
331
|
+
{ 17486291911125277965u, 1915619426082361072u },
|
|
332
|
+
{ 6610335899416401726u, 1532495540865888858u },
|
|
333
|
+
{ 12666966349016942027u, 1225996432692711086u },
|
|
334
|
+
{ 12888448528943286597u, 1961594292308337738u },
|
|
335
|
+
{ 17689456452638449924u, 1569275433846670190u },
|
|
336
|
+
{ 14151565162110759939u, 1255420347077336152u },
|
|
337
|
+
{ 7885109000409574610u, 2008672555323737844u },
|
|
338
|
+
{ 9997436015069570011u, 1606938044258990275u },
|
|
339
|
+
{ 7997948812055656009u, 1285550435407192220u },
|
|
340
|
+
{ 12796718099289049614u, 2056880696651507552u },
|
|
341
|
+
{ 2858676849947419045u, 1645504557321206042u },
|
|
342
|
+
{ 13354987924183666206u, 1316403645856964833u },
|
|
343
|
+
{ 17678631863951955605u, 2106245833371143733u },
|
|
344
|
+
{ 3074859046935833515u, 1684996666696914987u },
|
|
345
|
+
{ 13527933681774397782u, 1347997333357531989u },
|
|
346
|
+
{ 10576647446613305481u, 2156795733372051183u },
|
|
347
|
+
{ 15840015586774465031u, 1725436586697640946u },
|
|
348
|
+
{ 8982663654677661702u, 1380349269358112757u },
|
|
349
|
+
{ 18061610662226169046u, 2208558830972980411u },
|
|
350
|
+
{ 10759939715039024913u, 1766847064778384329u },
|
|
351
|
+
{ 12297300586773130254u, 1413477651822707463u },
|
|
352
|
+
{ 15986332124095098083u, 2261564242916331941u },
|
|
353
|
+
{ 9099716884534168143u, 1809251394333065553u },
|
|
354
|
+
{ 14658471137111155161u, 1447401115466452442u },
|
|
355
|
+
{ 4348079280205103483u, 1157920892373161954u },
|
|
356
|
+
{ 14335624477811986218u, 1852673427797059126u },
|
|
357
|
+
{ 7779150767507678651u, 1482138742237647301u },
|
|
358
|
+
{ 2533971799264232598u, 1185710993790117841u },
|
|
359
|
+
{ 15122401323048503126u, 1897137590064188545u },
|
|
360
|
+
{ 12097921058438802501u, 1517710072051350836u },
|
|
361
|
+
{ 5988988032009131678u, 1214168057641080669u },
|
|
362
|
+
{ 16961078480698431330u, 1942668892225729070u },
|
|
363
|
+
{ 13568862784558745064u, 1554135113780583256u },
|
|
364
|
+
{ 7165741412905085728u, 1243308091024466605u },
|
|
365
|
+
{ 11465186260648137165u, 1989292945639146568u },
|
|
366
|
+
{ 16550846638002330379u, 1591434356511317254u },
|
|
367
|
+
{ 16930026125143774626u, 1273147485209053803u },
|
|
368
|
+
{ 4951948911778577463u, 2037035976334486086u },
|
|
369
|
+
{ 272210314680951647u, 1629628781067588869u },
|
|
370
|
+
{ 3907117066486671641u, 1303703024854071095u },
|
|
371
|
+
{ 6251387306378674625u, 2085924839766513752u },
|
|
372
|
+
{ 16069156289328670670u, 1668739871813211001u },
|
|
373
|
+
{ 9165976216721026213u, 1334991897450568801u },
|
|
374
|
+
{ 7286864317269821294u, 2135987035920910082u },
|
|
375
|
+
{ 16897537898041588005u, 1708789628736728065u },
|
|
376
|
+
{ 13518030318433270404u, 1367031702989382452u },
|
|
377
|
+
{ 6871453250525591353u, 2187250724783011924u },
|
|
378
|
+
{ 9186511415162383406u, 1749800579826409539u },
|
|
379
|
+
{ 11038557946871817048u, 1399840463861127631u },
|
|
380
|
+
{ 10282995085511086630u, 2239744742177804210u },
|
|
381
|
+
{ 8226396068408869304u, 1791795793742243368u },
|
|
382
|
+
{ 13959814484210916090u, 1433436634993794694u },
|
|
383
|
+
{ 11267656730511734774u, 2293498615990071511u },
|
|
384
|
+
{ 5324776569667477496u, 1834798892792057209u },
|
|
385
|
+
{ 7949170070475892320u, 1467839114233645767u },
|
|
386
|
+
{ 17427382500606444826u, 1174271291386916613u },
|
|
387
|
+
{ 5747719112518849781u, 1878834066219066582u },
|
|
388
|
+
{ 15666221734240810795u, 1503067252975253265u },
|
|
389
|
+
{ 12532977387392648636u, 1202453802380202612u },
|
|
390
|
+
{ 5295368560860596524u, 1923926083808324180u },
|
|
391
|
+
{ 4236294848688477220u, 1539140867046659344u },
|
|
392
|
+
{ 7078384693692692099u, 1231312693637327475u },
|
|
393
|
+
{ 11325415509908307358u, 1970100309819723960u },
|
|
394
|
+
{ 9060332407926645887u, 1576080247855779168u },
|
|
395
|
+
{ 14626963555825137356u, 1260864198284623334u },
|
|
396
|
+
{ 12335095245094488799u, 2017382717255397335u },
|
|
397
|
+
{ 9868076196075591040u, 1613906173804317868u },
|
|
398
|
+
{ 15273158586344293478u, 1291124939043454294u },
|
|
399
|
+
{ 13369007293925138595u, 2065799902469526871u },
|
|
400
|
+
{ 7005857020398200553u, 1652639921975621497u },
|
|
401
|
+
{ 16672732060544291412u, 1322111937580497197u },
|
|
402
|
+
{ 11918976037903224966u, 2115379100128795516u },
|
|
403
|
+
{ 5845832015580669650u, 1692303280103036413u },
|
|
404
|
+
{ 12055363241948356366u, 1353842624082429130u },
|
|
405
|
+
{ 841837113407818570u, 2166148198531886609u },
|
|
406
|
+
{ 4362818505468165179u, 1732918558825509287u },
|
|
407
|
+
{ 14558301248600263113u, 1386334847060407429u },
|
|
408
|
+
{ 12225235553534690011u, 2218135755296651887u },
|
|
409
|
+
{ 2401490813343931363u, 1774508604237321510u },
|
|
410
|
+
{ 1921192650675145090u, 1419606883389857208u },
|
|
411
|
+
{ 17831303500047873437u, 2271371013423771532u },
|
|
412
|
+
{ 6886345170554478103u, 1817096810739017226u },
|
|
413
|
+
{ 1819727321701672159u, 1453677448591213781u },
|
|
414
|
+
{ 16213177116328979020u, 1162941958872971024u },
|
|
415
|
+
{ 14873036941900635463u, 1860707134196753639u },
|
|
416
|
+
{ 15587778368262418694u, 1488565707357402911u },
|
|
417
|
+
{ 8780873879868024632u, 1190852565885922329u },
|
|
418
|
+
{ 2981351763563108441u, 1905364105417475727u },
|
|
419
|
+
{ 13453127855076217722u, 1524291284333980581u },
|
|
420
|
+
{ 7073153469319063855u, 1219433027467184465u },
|
|
421
|
+
{ 11317045550910502167u, 1951092843947495144u },
|
|
422
|
+
{ 12742985255470312057u, 1560874275157996115u },
|
|
423
|
+
{ 10194388204376249646u, 1248699420126396892u },
|
|
424
|
+
{ 1553625868034358140u, 1997919072202235028u },
|
|
425
|
+
{ 8621598323911307159u, 1598335257761788022u },
|
|
426
|
+
{ 17965325103354776697u, 1278668206209430417u },
|
|
427
|
+
{ 13987124906400001422u, 2045869129935088668u },
|
|
428
|
+
{ 121653480894270168u, 1636695303948070935u },
|
|
429
|
+
{ 97322784715416134u, 1309356243158456748u },
|
|
430
|
+
{ 14913111714512307107u, 2094969989053530796u },
|
|
431
|
+
{ 8241140556867935363u, 1675975991242824637u },
|
|
432
|
+
{ 17660958889720079260u, 1340780792994259709u },
|
|
433
|
+
{ 17189487779326395846u, 2145249268790815535u },
|
|
434
|
+
{ 13751590223461116677u, 1716199415032652428u },
|
|
435
|
+
{ 18379969808252713988u, 1372959532026121942u },
|
|
436
|
+
{ 14650556434236701088u, 2196735251241795108u },
|
|
437
|
+
{ 652398703163629901u, 1757388200993436087u },
|
|
438
|
+
{ 11589965406756634890u, 1405910560794748869u },
|
|
439
|
+
{ 7475898206584884855u, 2249456897271598191u },
|
|
440
|
+
{ 2291369750525997561u, 1799565517817278553u },
|
|
441
|
+
{ 9211793429904618695u, 1439652414253822842u },
|
|
442
|
+
{ 18428218302589300235u, 2303443862806116547u },
|
|
443
|
+
{ 7363877012587619542u, 1842755090244893238u },
|
|
444
|
+
{ 13269799239553916280u, 1474204072195914590u },
|
|
445
|
+
{ 10615839391643133024u, 1179363257756731672u },
|
|
446
|
+
{ 2227947767661371545u, 1886981212410770676u },
|
|
447
|
+
{ 16539753473096738529u, 1509584969928616540u },
|
|
448
|
+
{ 13231802778477390823u, 1207667975942893232u },
|
|
449
|
+
{ 6413489186596184024u, 1932268761508629172u },
|
|
450
|
+
{ 16198837793502678189u, 1545815009206903337u },
|
|
451
|
+
{ 5580372605318321905u, 1236652007365522670u },
|
|
452
|
+
{ 8928596168509315048u, 1978643211784836272u },
|
|
453
|
+
{ 18210923379033183008u, 1582914569427869017u },
|
|
454
|
+
{ 7190041073742725760u, 1266331655542295214u },
|
|
455
|
+
{ 436019273762630246u, 2026130648867672343u },
|
|
456
|
+
{ 7727513048493924843u, 1620904519094137874u },
|
|
457
|
+
{ 9871359253537050198u, 1296723615275310299u },
|
|
458
|
+
{ 4726128361433549347u, 2074757784440496479u },
|
|
459
|
+
{ 7470251503888749801u, 1659806227552397183u },
|
|
460
|
+
{ 13354898832594820487u, 1327844982041917746u },
|
|
461
|
+
{ 13989140502667892133u, 2124551971267068394u },
|
|
462
|
+
{ 14880661216876224029u, 1699641577013654715u },
|
|
463
|
+
{ 11904528973500979224u, 1359713261610923772u },
|
|
464
|
+
{ 4289851098633925465u, 2175541218577478036u },
|
|
465
|
+
{ 18189276137874781665u, 1740432974861982428u },
|
|
466
|
+
{ 3483374466074094362u, 1392346379889585943u },
|
|
467
|
+
{ 1884050330976640656u, 2227754207823337509u },
|
|
468
|
+
{ 5196589079523222848u, 1782203366258670007u },
|
|
469
|
+
{ 15225317707844309248u, 1425762693006936005u },
|
|
470
|
+
{ 5913764258841343181u, 2281220308811097609u },
|
|
471
|
+
{ 8420360221814984868u, 1824976247048878087u },
|
|
472
|
+
{ 17804334621677718864u, 1459980997639102469u },
|
|
473
|
+
{ 17932816512084085415u, 1167984798111281975u },
|
|
474
|
+
{ 10245762345624985047u, 1868775676978051161u },
|
|
475
|
+
{ 4507261061758077715u, 1495020541582440929u },
|
|
476
|
+
{ 7295157664148372495u, 1196016433265952743u },
|
|
477
|
+
{ 7982903447895485668u, 1913626293225524389u },
|
|
478
|
+
{ 10075671573058298858u, 1530901034580419511u },
|
|
479
|
+
{ 4371188443704728763u, 1224720827664335609u },
|
|
480
|
+
{ 14372599139411386667u, 1959553324262936974u },
|
|
481
|
+
{ 15187428126271019657u, 1567642659410349579u },
|
|
482
|
+
{ 15839291315758726049u, 1254114127528279663u },
|
|
483
|
+
{ 3206773216762499739u, 2006582604045247462u },
|
|
484
|
+
{ 13633465017635730761u, 1605266083236197969u },
|
|
485
|
+
{ 14596120828850494932u, 1284212866588958375u },
|
|
486
|
+
{ 4907049252451240275u, 2054740586542333401u },
|
|
487
|
+
{ 236290587219081897u, 1643792469233866721u },
|
|
488
|
+
{ 14946427728742906810u, 1315033975387093376u },
|
|
489
|
+
{ 16535586736504830250u, 2104054360619349402u },
|
|
490
|
+
{ 5849771759720043554u, 1683243488495479522u },
|
|
491
|
+
{ 15747863852001765813u, 1346594790796383617u },
|
|
492
|
+
{ 10439186904235184007u, 2154551665274213788u },
|
|
493
|
+
{ 15730047152871967852u, 1723641332219371030u },
|
|
494
|
+
{ 12584037722297574282u, 1378913065775496824u },
|
|
495
|
+
{ 9066413911450387881u, 2206260905240794919u },
|
|
496
|
+
{ 10942479943902220628u, 1765008724192635935u },
|
|
497
|
+
{ 8753983955121776503u, 1412006979354108748u },
|
|
498
|
+
{ 10317025513452932081u, 2259211166966573997u },
|
|
499
|
+
{ 874922781278525018u, 1807368933573259198u },
|
|
500
|
+
{ 8078635854506640661u, 1445895146858607358u },
|
|
501
|
+
{ 13841606313089133175u, 1156716117486885886u },
|
|
502
|
+
{ 14767872471458792434u, 1850745787979017418u },
|
|
503
|
+
{ 746251532941302978u, 1480596630383213935u },
|
|
504
|
+
{ 597001226353042382u, 1184477304306571148u },
|
|
505
|
+
{ 15712597221132509104u, 1895163686890513836u },
|
|
506
|
+
{ 8880728962164096960u, 1516130949512411069u },
|
|
507
|
+
{ 10793931984473187891u, 1212904759609928855u },
|
|
508
|
+
{ 17270291175157100626u, 1940647615375886168u },
|
|
509
|
+
{ 2748186495899949531u, 1552518092300708935u },
|
|
510
|
+
{ 2198549196719959625u, 1242014473840567148u },
|
|
511
|
+
{ 18275073973719576693u, 1987223158144907436u },
|
|
512
|
+
{ 10930710364233751031u, 1589778526515925949u },
|
|
513
|
+
{ 12433917106128911148u, 1271822821212740759u },
|
|
514
|
+
{ 8826220925580526867u, 2034916513940385215u },
|
|
515
|
+
{ 7060976740464421494u, 1627933211152308172u },
|
|
516
|
+
{ 16716827836597268165u, 1302346568921846537u },
|
|
517
|
+
{ 11989529279587987770u, 2083754510274954460u },
|
|
518
|
+
{ 9591623423670390216u, 1667003608219963568u },
|
|
519
|
+
{ 15051996368420132820u, 1333602886575970854u },
|
|
520
|
+
{ 13015147745246481542u, 2133764618521553367u },
|
|
521
|
+
{ 3033420566713364587u, 1707011694817242694u },
|
|
522
|
+
{ 6116085268112601993u, 1365609355853794155u },
|
|
523
|
+
{ 9785736428980163188u, 2184974969366070648u },
|
|
524
|
+
{ 15207286772667951197u, 1747979975492856518u },
|
|
525
|
+
{ 1097782973908629988u, 1398383980394285215u },
|
|
526
|
+
{ 1756452758253807981u, 2237414368630856344u },
|
|
527
|
+
{ 5094511021344956708u, 1789931494904685075u },
|
|
528
|
+
{ 4075608817075965366u, 1431945195923748060u },
|
|
529
|
+
{ 6520974107321544586u, 2291112313477996896u },
|
|
530
|
+
{ 1527430471115325346u, 1832889850782397517u },
|
|
531
|
+
{ 12289990821117991246u, 1466311880625918013u },
|
|
532
|
+
{ 17210690286378213644u, 1173049504500734410u },
|
|
533
|
+
{ 9090360384495590213u, 1876879207201175057u },
|
|
534
|
+
{ 18340334751822203140u, 1501503365760940045u },
|
|
535
|
+
{ 14672267801457762512u, 1201202692608752036u },
|
|
536
|
+
{ 16096930852848599373u, 1921924308174003258u },
|
|
537
|
+
{ 1809498238053148529u, 1537539446539202607u },
|
|
538
|
+
{ 12515645034668249793u, 1230031557231362085u },
|
|
539
|
+
{ 1578287981759648052u, 1968050491570179337u },
|
|
540
|
+
{ 12330676829633449412u, 1574440393256143469u },
|
|
541
|
+
{ 13553890278448669853u, 1259552314604914775u },
|
|
542
|
+
{ 3239480371808320148u, 2015283703367863641u },
|
|
543
|
+
{ 17348979556414297411u, 1612226962694290912u },
|
|
544
|
+
{ 6500486015647617283u, 1289781570155432730u },
|
|
545
|
+
{ 10400777625036187652u, 2063650512248692368u },
|
|
546
|
+
{ 15699319729512770768u, 1650920409798953894u },
|
|
547
|
+
{ 16248804598352126938u, 1320736327839163115u },
|
|
548
|
+
{ 7551343283653851484u, 2113178124542660985u },
|
|
549
|
+
{ 6041074626923081187u, 1690542499634128788u },
|
|
550
|
+
{ 12211557331022285596u, 1352433999707303030u },
|
|
551
|
+
{ 1091747655926105338u, 2163894399531684849u },
|
|
552
|
+
{ 4562746939482794594u, 1731115519625347879u },
|
|
553
|
+
{ 7339546366328145998u, 1384892415700278303u },
|
|
554
|
+
{ 8053925371383123274u, 2215827865120445285u },
|
|
555
|
+
{ 6443140297106498619u, 1772662292096356228u },
|
|
556
|
+
{ 12533209867169019542u, 1418129833677084982u },
|
|
557
|
+
{ 5295740528502789974u, 2269007733883335972u },
|
|
558
|
+
{ 15304638867027962949u, 1815206187106668777u },
|
|
559
|
+
{ 4865013464138549713u, 1452164949685335022u },
|
|
560
|
+
{ 14960057215536570740u, 1161731959748268017u },
|
|
561
|
+
{ 9178696285890871890u, 1858771135597228828u },
|
|
562
|
+
{ 14721654658196518159u, 1487016908477783062u },
|
|
563
|
+
{ 4398626097073393881u, 1189613526782226450u },
|
|
564
|
+
{ 7037801755317430209u, 1903381642851562320u },
|
|
565
|
+
{ 5630241404253944167u, 1522705314281249856u },
|
|
566
|
+
{ 814844308661245011u, 1218164251424999885u },
|
|
567
|
+
{ 1303750893857992017u, 1949062802279999816u },
|
|
568
|
+
{ 15800395974054034906u, 1559250241823999852u },
|
|
569
|
+
{ 5261619149759407279u, 1247400193459199882u },
|
|
570
|
+
{ 12107939454356961969u, 1995840309534719811u },
|
|
571
|
+
{ 5997002748743659252u, 1596672247627775849u },
|
|
572
|
+
{ 8486951013736837725u, 1277337798102220679u },
|
|
573
|
+
{ 2511075177753209390u, 2043740476963553087u },
|
|
574
|
+
{ 13076906586428298482u, 1634992381570842469u },
|
|
575
|
+
{ 14150874083884549109u, 1307993905256673975u },
|
|
576
|
+
{ 4194654460505726958u, 2092790248410678361u },
|
|
577
|
+
{ 18113118827372222859u, 1674232198728542688u },
|
|
578
|
+
{ 3422448617672047318u, 1339385758982834151u },
|
|
579
|
+
{ 16543964232501006678u, 2143017214372534641u },
|
|
580
|
+
{ 9545822571258895019u, 1714413771498027713u },
|
|
581
|
+
{ 15015355686490936662u, 1371531017198422170u },
|
|
582
|
+
{ 5577825024675947042u, 2194449627517475473u },
|
|
583
|
+
{ 11840957649224578280u, 1755559702013980378u },
|
|
584
|
+
{ 16851463748863483271u, 1404447761611184302u },
|
|
585
|
+
{ 12204946739213931940u, 2247116418577894884u },
|
|
586
|
+
{ 13453306206113055875u, 1797693134862315907u },
|
|
587
|
+
{ 3383947335406624054u, 1438154507889852726u },
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
static const uint64_t NISH_POW5_SPLIT[326][2] = {
|
|
591
|
+
{ 0u, 1152921504606846976u },
|
|
592
|
+
{ 0u, 1441151880758558720u },
|
|
593
|
+
{ 0u, 1801439850948198400u },
|
|
594
|
+
{ 0u, 2251799813685248000u },
|
|
595
|
+
{ 0u, 1407374883553280000u },
|
|
596
|
+
{ 0u, 1759218604441600000u },
|
|
597
|
+
{ 0u, 2199023255552000000u },
|
|
598
|
+
{ 0u, 1374389534720000000u },
|
|
599
|
+
{ 0u, 1717986918400000000u },
|
|
600
|
+
{ 0u, 2147483648000000000u },
|
|
601
|
+
{ 0u, 1342177280000000000u },
|
|
602
|
+
{ 0u, 1677721600000000000u },
|
|
603
|
+
{ 0u, 2097152000000000000u },
|
|
604
|
+
{ 0u, 1310720000000000000u },
|
|
605
|
+
{ 0u, 1638400000000000000u },
|
|
606
|
+
{ 0u, 2048000000000000000u },
|
|
607
|
+
{ 0u, 1280000000000000000u },
|
|
608
|
+
{ 0u, 1600000000000000000u },
|
|
609
|
+
{ 0u, 2000000000000000000u },
|
|
610
|
+
{ 0u, 1250000000000000000u },
|
|
611
|
+
{ 0u, 1562500000000000000u },
|
|
612
|
+
{ 0u, 1953125000000000000u },
|
|
613
|
+
{ 0u, 1220703125000000000u },
|
|
614
|
+
{ 0u, 1525878906250000000u },
|
|
615
|
+
{ 0u, 1907348632812500000u },
|
|
616
|
+
{ 0u, 1192092895507812500u },
|
|
617
|
+
{ 0u, 1490116119384765625u },
|
|
618
|
+
{ 4611686018427387904u, 1862645149230957031u },
|
|
619
|
+
{ 9799832789158199296u, 1164153218269348144u },
|
|
620
|
+
{ 12249790986447749120u, 1455191522836685180u },
|
|
621
|
+
{ 15312238733059686400u, 1818989403545856475u },
|
|
622
|
+
{ 14528612397897220096u, 2273736754432320594u },
|
|
623
|
+
{ 13692068767113150464u, 1421085471520200371u },
|
|
624
|
+
{ 12503399940464050176u, 1776356839400250464u },
|
|
625
|
+
{ 15629249925580062720u, 2220446049250313080u },
|
|
626
|
+
{ 9768281203487539200u, 1387778780781445675u },
|
|
627
|
+
{ 7598665485932036096u, 1734723475976807094u },
|
|
628
|
+
{ 274959820560269312u, 2168404344971008868u },
|
|
629
|
+
{ 9395221924704944128u, 1355252715606880542u },
|
|
630
|
+
{ 2520655369026404352u, 1694065894508600678u },
|
|
631
|
+
{ 12374191248137781248u, 2117582368135750847u },
|
|
632
|
+
{ 14651398557727195136u, 1323488980084844279u },
|
|
633
|
+
{ 13702562178731606016u, 1654361225106055349u },
|
|
634
|
+
{ 3293144668132343808u, 2067951531382569187u },
|
|
635
|
+
{ 18199116482078572544u, 1292469707114105741u },
|
|
636
|
+
{ 8913837547316051968u, 1615587133892632177u },
|
|
637
|
+
{ 15753982952572452864u, 2019483917365790221u },
|
|
638
|
+
{ 12152082354571476992u, 1262177448353618888u },
|
|
639
|
+
{ 15190102943214346240u, 1577721810442023610u },
|
|
640
|
+
{ 9764256642163156992u, 1972152263052529513u },
|
|
641
|
+
{ 17631875447420442880u, 1232595164407830945u },
|
|
642
|
+
{ 8204786253993389888u, 1540743955509788682u },
|
|
643
|
+
{ 1032610780636961552u, 1925929944387235853u },
|
|
644
|
+
{ 2951224747111794922u, 1203706215242022408u },
|
|
645
|
+
{ 3689030933889743652u, 1504632769052528010u },
|
|
646
|
+
{ 13834660704216955373u, 1880790961315660012u },
|
|
647
|
+
{ 17870034976990372916u, 1175494350822287507u },
|
|
648
|
+
{ 17725857702810578241u, 1469367938527859384u },
|
|
649
|
+
{ 3710578054803671186u, 1836709923159824231u },
|
|
650
|
+
{ 26536550077201078u, 2295887403949780289u },
|
|
651
|
+
{ 11545800389866720434u, 1434929627468612680u },
|
|
652
|
+
{ 14432250487333400542u, 1793662034335765850u },
|
|
653
|
+
{ 8816941072311974870u, 2242077542919707313u },
|
|
654
|
+
{ 17039803216263454053u, 1401298464324817070u },
|
|
655
|
+
{ 12076381983474541759u, 1751623080406021338u },
|
|
656
|
+
{ 5872105442488401391u, 2189528850507526673u },
|
|
657
|
+
{ 15199280947623720629u, 1368455531567204170u },
|
|
658
|
+
{ 9775729147674874978u, 1710569414459005213u },
|
|
659
|
+
{ 16831347453020981627u, 2138211768073756516u },
|
|
660
|
+
{ 1296220121283337709u, 1336382355046097823u },
|
|
661
|
+
{ 15455333206886335848u, 1670477943807622278u },
|
|
662
|
+
{ 10095794471753144002u, 2088097429759527848u },
|
|
663
|
+
{ 6309871544845715001u, 1305060893599704905u },
|
|
664
|
+
{ 12499025449484531656u, 1631326116999631131u },
|
|
665
|
+
{ 11012095793428276666u, 2039157646249538914u },
|
|
666
|
+
{ 11494245889320060820u, 1274473528905961821u },
|
|
667
|
+
{ 532749306367912313u, 1593091911132452277u },
|
|
668
|
+
{ 5277622651387278295u, 1991364888915565346u },
|
|
669
|
+
{ 7910200175544436838u, 1244603055572228341u },
|
|
670
|
+
{ 14499436237857933952u, 1555753819465285426u },
|
|
671
|
+
{ 8900923260467641632u, 1944692274331606783u },
|
|
672
|
+
{ 12480606065433357876u, 1215432671457254239u },
|
|
673
|
+
{ 10989071563364309441u, 1519290839321567799u },
|
|
674
|
+
{ 9124653435777998898u, 1899113549151959749u },
|
|
675
|
+
{ 8008751406574943263u, 1186945968219974843u },
|
|
676
|
+
{ 5399253239791291175u, 1483682460274968554u },
|
|
677
|
+
{ 15972438586593889776u, 1854603075343710692u },
|
|
678
|
+
{ 759402079766405302u, 1159126922089819183u },
|
|
679
|
+
{ 14784310654990170340u, 1448908652612273978u },
|
|
680
|
+
{ 9257016281882937117u, 1811135815765342473u },
|
|
681
|
+
{ 16182956370781059300u, 2263919769706678091u },
|
|
682
|
+
{ 7808504722524468110u, 1414949856066673807u },
|
|
683
|
+
{ 5148944884728197234u, 1768687320083342259u },
|
|
684
|
+
{ 1824495087482858639u, 2210859150104177824u },
|
|
685
|
+
{ 1140309429676786649u, 1381786968815111140u },
|
|
686
|
+
{ 1425386787095983311u, 1727233711018888925u },
|
|
687
|
+
{ 6393419502297367043u, 2159042138773611156u },
|
|
688
|
+
{ 13219259225790630210u, 1349401336733506972u },
|
|
689
|
+
{ 16524074032238287762u, 1686751670916883715u },
|
|
690
|
+
{ 16043406521870471799u, 2108439588646104644u },
|
|
691
|
+
{ 803757039314269066u, 1317774742903815403u },
|
|
692
|
+
{ 14839754354425000045u, 1647218428629769253u },
|
|
693
|
+
{ 4714634887749086344u, 2059023035787211567u },
|
|
694
|
+
{ 9864175832484260821u, 1286889397367007229u },
|
|
695
|
+
{ 16941905809032713930u, 1608611746708759036u },
|
|
696
|
+
{ 2730638187581340797u, 2010764683385948796u },
|
|
697
|
+
{ 10930020904093113806u, 1256727927116217997u },
|
|
698
|
+
{ 18274212148543780162u, 1570909908895272496u },
|
|
699
|
+
{ 4396021111970173586u, 1963637386119090621u },
|
|
700
|
+
{ 5053356204195052443u, 1227273366324431638u },
|
|
701
|
+
{ 15540067292098591362u, 1534091707905539547u },
|
|
702
|
+
{ 14813398096695851299u, 1917614634881924434u },
|
|
703
|
+
{ 13870059828862294966u, 1198509146801202771u },
|
|
704
|
+
{ 12725888767650480803u, 1498136433501503464u },
|
|
705
|
+
{ 15907360959563101004u, 1872670541876879330u },
|
|
706
|
+
{ 14553786618154326031u, 1170419088673049581u },
|
|
707
|
+
{ 4357175217410743827u, 1463023860841311977u },
|
|
708
|
+
{ 10058155040190817688u, 1828779826051639971u },
|
|
709
|
+
{ 7961007781811134206u, 2285974782564549964u },
|
|
710
|
+
{ 14199001900486734687u, 1428734239102843727u },
|
|
711
|
+
{ 13137066357181030455u, 1785917798878554659u },
|
|
712
|
+
{ 11809646928048900164u, 2232397248598193324u },
|
|
713
|
+
{ 16604401366885338411u, 1395248280373870827u },
|
|
714
|
+
{ 16143815690179285109u, 1744060350467338534u },
|
|
715
|
+
{ 10956397575869330579u, 2180075438084173168u },
|
|
716
|
+
{ 6847748484918331612u, 1362547148802608230u },
|
|
717
|
+
{ 17783057643002690323u, 1703183936003260287u },
|
|
718
|
+
{ 17617136035325974999u, 2128979920004075359u },
|
|
719
|
+
{ 17928239049719816230u, 1330612450002547099u },
|
|
720
|
+
{ 17798612793722382384u, 1663265562503183874u },
|
|
721
|
+
{ 13024893955298202172u, 2079081953128979843u },
|
|
722
|
+
{ 5834715712847682405u, 1299426220705612402u },
|
|
723
|
+
{ 16516766677914378815u, 1624282775882015502u },
|
|
724
|
+
{ 11422586310538197711u, 2030353469852519378u },
|
|
725
|
+
{ 11750802462513761473u, 1268970918657824611u },
|
|
726
|
+
{ 10076817059714813937u, 1586213648322280764u },
|
|
727
|
+
{ 12596021324643517422u, 1982767060402850955u },
|
|
728
|
+
{ 5566670318688504437u, 1239229412751781847u },
|
|
729
|
+
{ 2346651879933242642u, 1549036765939727309u },
|
|
730
|
+
{ 7545000868343941206u, 1936295957424659136u },
|
|
731
|
+
{ 4715625542714963254u, 1210184973390411960u },
|
|
732
|
+
{ 5894531928393704067u, 1512731216738014950u },
|
|
733
|
+
{ 16591536947346905892u, 1890914020922518687u },
|
|
734
|
+
{ 17287239619732898039u, 1181821263076574179u },
|
|
735
|
+
{ 16997363506238734644u, 1477276578845717724u },
|
|
736
|
+
{ 2799960309088866689u, 1846595723557147156u },
|
|
737
|
+
{ 10973347230035317489u, 1154122327223216972u },
|
|
738
|
+
{ 13716684037544146861u, 1442652909029021215u },
|
|
739
|
+
{ 12534169028502795672u, 1803316136286276519u },
|
|
740
|
+
{ 11056025267201106687u, 2254145170357845649u },
|
|
741
|
+
{ 18439230838069161439u, 1408840731473653530u },
|
|
742
|
+
{ 13825666510731675991u, 1761050914342066913u },
|
|
743
|
+
{ 3447025083132431277u, 2201313642927583642u },
|
|
744
|
+
{ 6766076695385157452u, 1375821026829739776u },
|
|
745
|
+
{ 8457595869231446815u, 1719776283537174720u },
|
|
746
|
+
{ 10571994836539308519u, 2149720354421468400u },
|
|
747
|
+
{ 6607496772837067824u, 1343575221513417750u },
|
|
748
|
+
{ 17482743002901110588u, 1679469026891772187u },
|
|
749
|
+
{ 17241742735199000331u, 2099336283614715234u },
|
|
750
|
+
{ 15387775227926763111u, 1312085177259197021u },
|
|
751
|
+
{ 5399660979626290177u, 1640106471573996277u },
|
|
752
|
+
{ 11361262242960250625u, 2050133089467495346u },
|
|
753
|
+
{ 11712474920277544544u, 1281333180917184591u },
|
|
754
|
+
{ 10028907631919542777u, 1601666476146480739u },
|
|
755
|
+
{ 7924448521472040567u, 2002083095183100924u },
|
|
756
|
+
{ 14176152362774801162u, 1251301934489438077u },
|
|
757
|
+
{ 3885132398186337741u, 1564127418111797597u },
|
|
758
|
+
{ 9468101516160310080u, 1955159272639746996u },
|
|
759
|
+
{ 15140935484454969608u, 1221974545399841872u },
|
|
760
|
+
{ 479425281859160394u, 1527468181749802341u },
|
|
761
|
+
{ 5210967620751338397u, 1909335227187252926u },
|
|
762
|
+
{ 17091912818251750210u, 1193334516992033078u },
|
|
763
|
+
{ 12141518985959911954u, 1491668146240041348u },
|
|
764
|
+
{ 15176898732449889943u, 1864585182800051685u },
|
|
765
|
+
{ 11791404716994875166u, 1165365739250032303u },
|
|
766
|
+
{ 10127569877816206054u, 1456707174062540379u },
|
|
767
|
+
{ 8047776328842869663u, 1820883967578175474u },
|
|
768
|
+
{ 836348374198811271u, 2276104959472719343u },
|
|
769
|
+
{ 7440246761515338900u, 1422565599670449589u },
|
|
770
|
+
{ 13911994470321561530u, 1778206999588061986u },
|
|
771
|
+
{ 8166621051047176104u, 2222758749485077483u },
|
|
772
|
+
{ 2798295147690791113u, 1389224218428173427u },
|
|
773
|
+
{ 17332926989895652603u, 1736530273035216783u },
|
|
774
|
+
{ 17054472718942177850u, 2170662841294020979u },
|
|
775
|
+
{ 8353202440125167204u, 1356664275808763112u },
|
|
776
|
+
{ 10441503050156459005u, 1695830344760953890u },
|
|
777
|
+
{ 3828506775840797949u, 2119787930951192363u },
|
|
778
|
+
{ 86973725686804766u, 1324867456844495227u },
|
|
779
|
+
{ 13943775212390669669u, 1656084321055619033u },
|
|
780
|
+
{ 3594660960206173375u, 2070105401319523792u },
|
|
781
|
+
{ 2246663100128858359u, 1293815875824702370u },
|
|
782
|
+
{ 12031700912015848757u, 1617269844780877962u },
|
|
783
|
+
{ 5816254103165035138u, 2021587305976097453u },
|
|
784
|
+
{ 5941001823691840913u, 1263492066235060908u },
|
|
785
|
+
{ 7426252279614801142u, 1579365082793826135u },
|
|
786
|
+
{ 4671129331091113523u, 1974206353492282669u },
|
|
787
|
+
{ 5225298841145639904u, 1233878970932676668u },
|
|
788
|
+
{ 6531623551432049880u, 1542348713665845835u },
|
|
789
|
+
{ 3552843420862674446u, 1927935892082307294u },
|
|
790
|
+
{ 16055585193321335241u, 1204959932551442058u },
|
|
791
|
+
{ 10846109454796893243u, 1506199915689302573u },
|
|
792
|
+
{ 18169322836923504458u, 1882749894611628216u },
|
|
793
|
+
{ 11355826773077190286u, 1176718684132267635u },
|
|
794
|
+
{ 9583097447919099954u, 1470898355165334544u },
|
|
795
|
+
{ 11978871809898874942u, 1838622943956668180u },
|
|
796
|
+
{ 14973589762373593678u, 2298278679945835225u },
|
|
797
|
+
{ 2440964573842414192u, 1436424174966147016u },
|
|
798
|
+
{ 3051205717303017741u, 1795530218707683770u },
|
|
799
|
+
{ 13037379183483547984u, 2244412773384604712u },
|
|
800
|
+
{ 8148361989677217490u, 1402757983365377945u },
|
|
801
|
+
{ 14797138505523909766u, 1753447479206722431u },
|
|
802
|
+
{ 13884737113477499304u, 2191809349008403039u },
|
|
803
|
+
{ 15595489723564518921u, 1369880843130251899u },
|
|
804
|
+
{ 14882676136028260747u, 1712351053912814874u },
|
|
805
|
+
{ 9379973133180550126u, 2140438817391018593u },
|
|
806
|
+
{ 17391698254306313589u, 1337774260869386620u },
|
|
807
|
+
{ 3292878744173340370u, 1672217826086733276u },
|
|
808
|
+
{ 4116098430216675462u, 2090272282608416595u },
|
|
809
|
+
{ 266718509671728212u, 1306420176630260372u },
|
|
810
|
+
{ 333398137089660265u, 1633025220787825465u },
|
|
811
|
+
{ 5028433689789463235u, 2041281525984781831u },
|
|
812
|
+
{ 10060300083759496378u, 1275800953740488644u },
|
|
813
|
+
{ 12575375104699370472u, 1594751192175610805u },
|
|
814
|
+
{ 1884160825592049379u, 1993438990219513507u },
|
|
815
|
+
{ 17318501580490888525u, 1245899368887195941u },
|
|
816
|
+
{ 7813068920331446945u, 1557374211108994927u },
|
|
817
|
+
{ 5154650131986920777u, 1946717763886243659u },
|
|
818
|
+
{ 915813323278131534u, 1216698602428902287u },
|
|
819
|
+
{ 14979824709379828129u, 1520873253036127858u },
|
|
820
|
+
{ 9501408849870009354u, 1901091566295159823u },
|
|
821
|
+
{ 12855909558809837702u, 1188182228934474889u },
|
|
822
|
+
{ 2234828893230133415u, 1485227786168093612u },
|
|
823
|
+
{ 2793536116537666769u, 1856534732710117015u },
|
|
824
|
+
{ 8663489100477123587u, 1160334207943823134u },
|
|
825
|
+
{ 1605989338741628675u, 1450417759929778918u },
|
|
826
|
+
{ 11230858710281811652u, 1813022199912223647u },
|
|
827
|
+
{ 9426887369424876662u, 2266277749890279559u },
|
|
828
|
+
{ 12809333633531629769u, 1416423593681424724u },
|
|
829
|
+
{ 16011667041914537212u, 1770529492101780905u },
|
|
830
|
+
{ 6179525747111007803u, 2213161865127226132u },
|
|
831
|
+
{ 13085575628799155685u, 1383226165704516332u },
|
|
832
|
+
{ 16356969535998944606u, 1729032707130645415u },
|
|
833
|
+
{ 15834525901571292854u, 2161290883913306769u },
|
|
834
|
+
{ 2979049660840976177u, 1350806802445816731u },
|
|
835
|
+
{ 17558870131333383934u, 1688508503057270913u },
|
|
836
|
+
{ 8113529608884566205u, 2110635628821588642u },
|
|
837
|
+
{ 9682642023980241782u, 1319147268013492901u },
|
|
838
|
+
{ 16714988548402690132u, 1648934085016866126u },
|
|
839
|
+
{ 11670363648648586857u, 2061167606271082658u },
|
|
840
|
+
{ 11905663298832754689u, 1288229753919426661u },
|
|
841
|
+
{ 1047021068258779650u, 1610287192399283327u },
|
|
842
|
+
{ 15143834390605638274u, 2012858990499104158u },
|
|
843
|
+
{ 4853210475701136017u, 1258036869061940099u },
|
|
844
|
+
{ 1454827076199032118u, 1572546086327425124u },
|
|
845
|
+
{ 1818533845248790147u, 1965682607909281405u },
|
|
846
|
+
{ 3442426662494187794u, 1228551629943300878u },
|
|
847
|
+
{ 13526405364972510550u, 1535689537429126097u },
|
|
848
|
+
{ 3072948650933474476u, 1919611921786407622u },
|
|
849
|
+
{ 15755650962115585259u, 1199757451116504763u },
|
|
850
|
+
{ 15082877684217093670u, 1499696813895630954u },
|
|
851
|
+
{ 9630225068416591280u, 1874621017369538693u },
|
|
852
|
+
{ 8324733676974063502u, 1171638135855961683u },
|
|
853
|
+
{ 5794231077790191473u, 1464547669819952104u },
|
|
854
|
+
{ 7242788847237739342u, 1830684587274940130u },
|
|
855
|
+
{ 18276858095901949986u, 2288355734093675162u },
|
|
856
|
+
{ 16034722328366106645u, 1430222333808546976u },
|
|
857
|
+
{ 1596658836748081690u, 1787777917260683721u },
|
|
858
|
+
{ 6607509564362490017u, 2234722396575854651u },
|
|
859
|
+
{ 1823850468512862308u, 1396701497859909157u },
|
|
860
|
+
{ 6891499104068465790u, 1745876872324886446u },
|
|
861
|
+
{ 17837745916940358045u, 2182346090406108057u },
|
|
862
|
+
{ 4231062170446641922u, 1363966306503817536u },
|
|
863
|
+
{ 5288827713058302403u, 1704957883129771920u },
|
|
864
|
+
{ 6611034641322878003u, 2131197353912214900u },
|
|
865
|
+
{ 13355268687681574560u, 1331998346195134312u },
|
|
866
|
+
{ 16694085859601968200u, 1664997932743917890u },
|
|
867
|
+
{ 11644235287647684442u, 2081247415929897363u },
|
|
868
|
+
{ 4971804045566108824u, 1300779634956185852u },
|
|
869
|
+
{ 6214755056957636030u, 1625974543695232315u },
|
|
870
|
+
{ 3156757802769657134u, 2032468179619040394u },
|
|
871
|
+
{ 6584659645158423613u, 1270292612261900246u },
|
|
872
|
+
{ 17454196593302805324u, 1587865765327375307u },
|
|
873
|
+
{ 17206059723201118751u, 1984832206659219134u },
|
|
874
|
+
{ 6142101308573311315u, 1240520129162011959u },
|
|
875
|
+
{ 3065940617289251240u, 1550650161452514949u },
|
|
876
|
+
{ 8444111790038951954u, 1938312701815643686u },
|
|
877
|
+
{ 665883850346957067u, 1211445438634777304u },
|
|
878
|
+
{ 832354812933696334u, 1514306798293471630u },
|
|
879
|
+
{ 10263815553021896226u, 1892883497866839537u },
|
|
880
|
+
{ 17944099766707154901u, 1183052186166774710u },
|
|
881
|
+
{ 13206752671529167818u, 1478815232708468388u },
|
|
882
|
+
{ 16508440839411459773u, 1848519040885585485u },
|
|
883
|
+
{ 12623618533845856310u, 1155324400553490928u },
|
|
884
|
+
{ 15779523167307320387u, 1444155500691863660u },
|
|
885
|
+
{ 1277659885424598868u, 1805194375864829576u },
|
|
886
|
+
{ 1597074856780748586u, 2256492969831036970u },
|
|
887
|
+
{ 5609857803915355770u, 1410308106144398106u },
|
|
888
|
+
{ 16235694291748970521u, 1762885132680497632u },
|
|
889
|
+
{ 1847873790976661535u, 2203606415850622041u },
|
|
890
|
+
{ 12684136165428883219u, 1377254009906638775u },
|
|
891
|
+
{ 11243484188358716120u, 1721567512383298469u },
|
|
892
|
+
{ 219297180166231438u, 2151959390479123087u },
|
|
893
|
+
{ 7054589765244976505u, 1344974619049451929u },
|
|
894
|
+
{ 13429923224983608535u, 1681218273811814911u },
|
|
895
|
+
{ 12175718012802122765u, 2101522842264768639u },
|
|
896
|
+
{ 14527352785642408584u, 1313451776415480399u },
|
|
897
|
+
{ 13547504963625622826u, 1641814720519350499u },
|
|
898
|
+
{ 12322695186104640628u, 2052268400649188124u },
|
|
899
|
+
{ 16925056528170176201u, 1282667750405742577u },
|
|
900
|
+
{ 7321262604930556539u, 1603334688007178222u },
|
|
901
|
+
{ 18374950293017971482u, 2004168360008972777u },
|
|
902
|
+
{ 4566814905495150320u, 1252605225005607986u },
|
|
903
|
+
{ 14931890668723713708u, 1565756531257009982u },
|
|
904
|
+
{ 9441491299049866327u, 1957195664071262478u },
|
|
905
|
+
{ 1289246043478778550u, 1223247290044539049u },
|
|
906
|
+
{ 6223243572775861092u, 1529059112555673811u },
|
|
907
|
+
{ 3167368447542438461u, 1911323890694592264u },
|
|
908
|
+
{ 1979605279714024038u, 1194577431684120165u },
|
|
909
|
+
{ 7086192618069917952u, 1493221789605150206u },
|
|
910
|
+
{ 18081112809442173248u, 1866527237006437757u },
|
|
911
|
+
{ 13606538515115052232u, 1166579523129023598u },
|
|
912
|
+
{ 7784801107039039482u, 1458224403911279498u },
|
|
913
|
+
{ 507629346944023544u, 1822780504889099373u },
|
|
914
|
+
{ 5246222702107417334u, 2278475631111374216u },
|
|
915
|
+
{ 3278889188817135834u, 1424047269444608885u },
|
|
916
|
+
{ 8710297504448807696u, 1780059086805761106u },
|
|
917
|
+
};
|
|
918
|
+
#define NISH_POW5_BITS 125
|
|
919
|
+
|
|
920
|
+
static uint32_t nish_pow5bits(int32_t e) { return (uint32_t)(((((uint32_t)e) * 1217359) >> 19) + 1); }
|
|
921
|
+
static uint32_t nish_log10pow2(int32_t e) { return (((uint32_t)e) * 78913) >> 18; }
|
|
922
|
+
static uint32_t nish_log10pow5(int32_t e) { return (((uint32_t)e) * 732923) >> 20; }
|
|
923
|
+
|
|
924
|
+
/* How many times 5 divides `v`; the trailing-zero tests below are its only users. */
|
|
925
|
+
static uint32_t nish_pow5factor(uint64_t v) {
|
|
926
|
+
uint32_t n = 0;
|
|
927
|
+
for (;;) {
|
|
928
|
+
uint64_t q = v / 5;
|
|
929
|
+
if (v - q * 5 != 0) break;
|
|
930
|
+
v = q; n++;
|
|
931
|
+
}
|
|
932
|
+
return n;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
/* (m * mul) >> j, with `mul` a 128-bit constant split into two 64-bit halves. */
|
|
936
|
+
static uint64_t nish_mulshift(uint64_t m, const uint64_t *mul, int32_t j) {
|
|
937
|
+
__uint128_t b0 = (__uint128_t)m * mul[0];
|
|
938
|
+
__uint128_t b2 = (__uint128_t)m * mul[1];
|
|
939
|
+
return (uint64_t)(((b0 >> 64) + b2) >> (j - 64));
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
/* The shortest digits of a finite, positive `v` into `digits` (no NUL), with
|
|
943
|
+
`*exp10` set so the value is `0.<digits> * 10^*exp10` -- the form the
|
|
944
|
+
ECMAScript layout below wants. Returns the digit count, 1 to 17. */
|
|
945
|
+
static int nish_shortest_digits(double v, char *digits, int *exp10) {
|
|
946
|
+
uint64_t bits;
|
|
947
|
+
memcpy(&bits, &v, 8);
|
|
948
|
+
const uint64_t mantissa = bits & ((1ull << 52) - 1);
|
|
949
|
+
const uint32_t exponent = (uint32_t)((bits >> 52) & 0x7ff);
|
|
950
|
+
|
|
951
|
+
/* The halfway points either side of `v` are what decide the shortest digits,
|
|
952
|
+
so everything is scaled by 4 to keep them integral. */
|
|
953
|
+
int32_t e2;
|
|
954
|
+
uint64_t m2;
|
|
955
|
+
if (exponent == 0) {
|
|
956
|
+
e2 = 1 - 1023 - 52 - 2;
|
|
957
|
+
m2 = mantissa;
|
|
958
|
+
} else {
|
|
959
|
+
e2 = (int32_t)exponent - 1023 - 52 - 2;
|
|
960
|
+
m2 = (1ull << 52) | mantissa;
|
|
961
|
+
}
|
|
962
|
+
const int acceptBounds = (m2 & 1) == 0;
|
|
963
|
+
const uint64_t mv = 4 * m2;
|
|
964
|
+
const uint32_t mmShift = (mantissa != 0 || exponent <= 1);
|
|
965
|
+
|
|
966
|
+
uint64_t vr, vp, vm;
|
|
967
|
+
int32_t e10;
|
|
968
|
+
int vmIsTrailingZeros = 0, vrIsTrailingZeros = 0;
|
|
969
|
+
if (e2 >= 0) {
|
|
970
|
+
const uint32_t q = nish_log10pow2(e2) - (e2 > 3);
|
|
971
|
+
const int32_t k = NISH_POW5_BITS + (int32_t)nish_pow5bits((int32_t)q) - 1;
|
|
972
|
+
const int32_t j = -e2 + (int32_t)q + k;
|
|
973
|
+
e10 = (int32_t)q;
|
|
974
|
+
vr = nish_mulshift(mv, NISH_POW5_INV_SPLIT[q], j);
|
|
975
|
+
vp = nish_mulshift(mv + 2, NISH_POW5_INV_SPLIT[q], j);
|
|
976
|
+
vm = nish_mulshift(mv - 1 - mmShift, NISH_POW5_INV_SPLIT[q], j);
|
|
977
|
+
if (q <= 21) {
|
|
978
|
+
if (mv % 5 == 0) {
|
|
979
|
+
vrIsTrailingZeros = nish_pow5factor(mv) >= q;
|
|
980
|
+
} else if (acceptBounds) {
|
|
981
|
+
vmIsTrailingZeros = nish_pow5factor(mv - 1 - mmShift) >= q;
|
|
982
|
+
} else {
|
|
983
|
+
vp -= nish_pow5factor(mv + 2) >= q;
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
} else {
|
|
987
|
+
const uint32_t q = nish_log10pow5(-e2) - (-e2 > 1);
|
|
988
|
+
const int32_t i = -e2 - (int32_t)q;
|
|
989
|
+
const int32_t k = (int32_t)nish_pow5bits(i) - NISH_POW5_BITS;
|
|
990
|
+
const int32_t j = (int32_t)q - k;
|
|
991
|
+
e10 = (int32_t)q + e2;
|
|
992
|
+
vr = nish_mulshift(mv, NISH_POW5_SPLIT[i], j);
|
|
993
|
+
vp = nish_mulshift(mv + 2, NISH_POW5_SPLIT[i], j);
|
|
994
|
+
vm = nish_mulshift(mv - 1 - mmShift, NISH_POW5_SPLIT[i], j);
|
|
995
|
+
if (q <= 1) {
|
|
996
|
+
vrIsTrailingZeros = 1;
|
|
997
|
+
if (acceptBounds) vmIsTrailingZeros = (mmShift == 1);
|
|
998
|
+
else --vp;
|
|
999
|
+
} else if (q < 63) {
|
|
1000
|
+
vrIsTrailingZeros = (mv & ((1ull << q) - 1)) == 0;
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
int32_t removed = 0;
|
|
1005
|
+
uint8_t lastDigit = 0;
|
|
1006
|
+
uint64_t output;
|
|
1007
|
+
if (vmIsTrailingZeros || vrIsTrailingZeros) {
|
|
1008
|
+
/* The rare path (~0.7%): a boundary is exact, so trailing zeros decide it. */
|
|
1009
|
+
for (;;) {
|
|
1010
|
+
const uint64_t vpDiv10 = vp / 10, vmDiv10 = vm / 10;
|
|
1011
|
+
if (vpDiv10 <= vmDiv10) break;
|
|
1012
|
+
const uint64_t vrDiv10 = vr / 10;
|
|
1013
|
+
vmIsTrailingZeros &= (uint32_t)(vm - 10 * vmDiv10) == 0;
|
|
1014
|
+
vrIsTrailingZeros &= lastDigit == 0;
|
|
1015
|
+
lastDigit = (uint8_t)(vr - 10 * vrDiv10);
|
|
1016
|
+
vr = vrDiv10; vp = vpDiv10; vm = vmDiv10;
|
|
1017
|
+
++removed;
|
|
1018
|
+
}
|
|
1019
|
+
if (vmIsTrailingZeros) {
|
|
1020
|
+
for (;;) {
|
|
1021
|
+
const uint64_t vmDiv10 = vm / 10;
|
|
1022
|
+
if ((uint32_t)(vm - 10 * vmDiv10) != 0) break;
|
|
1023
|
+
const uint64_t vpDiv10 = vp / 10, vrDiv10 = vr / 10;
|
|
1024
|
+
vrIsTrailingZeros &= lastDigit == 0;
|
|
1025
|
+
lastDigit = (uint8_t)(vr - 10 * vrDiv10);
|
|
1026
|
+
vr = vrDiv10; vp = vpDiv10; vm = vmDiv10;
|
|
1027
|
+
++removed;
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
/* Round half to even, which is the tie rule ECMAScript states for `s`. */
|
|
1031
|
+
if (vrIsTrailingZeros && lastDigit == 5 && vr % 2 == 0) lastDigit = 4;
|
|
1032
|
+
output = vr + ((vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || lastDigit >= 5);
|
|
1033
|
+
} else {
|
|
1034
|
+
/* The common path (~99.3%): two digits at a time first, then one. */
|
|
1035
|
+
int roundUp = 0;
|
|
1036
|
+
const uint64_t vpDiv100 = vp / 100, vmDiv100 = vm / 100;
|
|
1037
|
+
if (vpDiv100 > vmDiv100) {
|
|
1038
|
+
const uint64_t vrDiv100 = vr / 100;
|
|
1039
|
+
roundUp = (uint32_t)(vr - 100 * vrDiv100) >= 50;
|
|
1040
|
+
vr = vrDiv100; vp = vpDiv100; vm = vmDiv100;
|
|
1041
|
+
removed += 2;
|
|
1042
|
+
}
|
|
1043
|
+
for (;;) {
|
|
1044
|
+
const uint64_t vpDiv10 = vp / 10, vmDiv10 = vm / 10;
|
|
1045
|
+
if (vpDiv10 <= vmDiv10) break;
|
|
1046
|
+
const uint64_t vrDiv10 = vr / 10;
|
|
1047
|
+
roundUp = (uint32_t)(vr - 10 * vrDiv10) >= 5;
|
|
1048
|
+
vr = vrDiv10; vp = vpDiv10; vm = vmDiv10;
|
|
1049
|
+
++removed;
|
|
1050
|
+
}
|
|
1051
|
+
output = vr + (vr == vm || roundUp);
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
char tmp[20];
|
|
1055
|
+
int k = 0;
|
|
1056
|
+
do { tmp[k++] = (char)('0' + (int)(output % 10)); output /= 10; } while (output);
|
|
1057
|
+
for (int i = 0; i < k; i++) digits[i] = tmp[k - 1 - i];
|
|
1058
|
+
*exp10 = e10 + removed + k;
|
|
1059
|
+
return k;
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
/* JS Number#toString: shortest round-trip digits, e-form outside (-6, 21] */
|
|
1063
|
+
nish_str *nish_str_from_f64(double v) {
|
|
1064
|
+
char digits[24], out[32], *o = out;
|
|
1065
|
+
int k, n, i, e = 0;
|
|
1066
|
+
if (v != v) { memcpy(o, "NaN", 3); o += 3; }
|
|
1067
|
+
else if (v == 0) *o++ = '0'; /* -0 prints as 0 */
|
|
1068
|
+
else {
|
|
1069
|
+
if (v < 0) { *o++ = '-'; v = -v; }
|
|
1070
|
+
if (v == INFINITY) { memcpy(o, "Infinity", 8); o += 8; }
|
|
1071
|
+
else {
|
|
1072
|
+
k = nish_shortest_digits(v, digits, &n); /* value = 0.digits * 10^n */
|
|
1073
|
+
if (n > 21 || n <= -6) { e = n - 1; n = 1; }
|
|
1074
|
+
else if (n <= 0) { *o++ = '0'; *o++ = '.'; for (; n < 0; n++) *o++ = '0'; n = k; }
|
|
1075
|
+
for (i = 0; i < k || i < n; i++) { if (i == n) *o++ = '.'; *o++ = i < k ? digits[i] : '0'; }
|
|
1076
|
+
if (e) o += snprintf(o, 8, "e%+d", e);
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
return nish_str_new(out, o - out);
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
/* ---- Math.random: xorshift64*, seeded lazily from time and pid.
|
|
1083
|
+
Thread-local under -DNISH_THREADS (WP20 T0 §3.2): a shared seed word is a
|
|
1084
|
+
race, and a per-thread one also makes each worker's stream its own rather
|
|
1085
|
+
than an interleaving of everyone's. Two threads that start in the same
|
|
1086
|
+
second still salt from the same time and pid, so a program that needs
|
|
1087
|
+
distinct streams per thread seeds them itself. */
|
|
1088
|
+
static NISH_TLS uint64_t nish_rng;
|
|
1089
|
+
|
|
1090
|
+
double nish_random(void) {
|
|
1091
|
+
uint64_t x = nish_rng ? nish_rng : ((uint64_t)time(0) << 32) ^ getpid() ^ 0x9E3779B97F4A7C15ull;
|
|
1092
|
+
x ^= x >> 12; x ^= x << 25; x ^= x >> 27;
|
|
1093
|
+
nish_rng = x;
|
|
1094
|
+
return (double)((x * 0x2545F4914F6CDD1Dull) >> 11) * (1.0 / 9007199254740992.0);
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
/* ---- Arrays: %struct.nish_array = type { i64, i64, i8* }, cold paths */
|
|
1098
|
+
typedef struct nish_array { uint64_t len; uint64_t cap; char *data; } nish_array;
|
|
1099
|
+
|
|
1100
|
+
/* Host entry (WP8): `len` uninitialised elements, len == cap */
|
|
1101
|
+
nish_array *nish_alloc_array(uint64_t elem_size, uint64_t len) {
|
|
1102
|
+
nish_array *a = nish_alloc_struct(sizeof *a);
|
|
1103
|
+
*a = (nish_array){ len, len, nish_alloc_struct(len * elem_size) };
|
|
1104
|
+
return a;
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
/* process.argv: malloc, not the arena, so Arena.reset() cannot free it. Built once by @main.
|
|
1108
|
+
Deliberately *not* NISH_TLS (WP20 T0 §3.2): `nish_argv_init` writes it once
|
|
1109
|
+
before the program's `main` body runs and nothing writes it afterwards, so
|
|
1110
|
+
every thread may read the one copy. That holds only while no thread is
|
|
1111
|
+
spawned before initialisation, which the entry wrapper guarantees by calling
|
|
1112
|
+
this first. */
|
|
1113
|
+
nish_array *nish_argv;
|
|
1114
|
+
|
|
1115
|
+
void nish_argv_init(int32_t argc, char **argv) {
|
|
1116
|
+
nish_array *a = malloc(sizeof *a + argc * sizeof(nish_str *));
|
|
1117
|
+
nish_str **d = (nish_str **)(a + 1);
|
|
1118
|
+
if (!a) nish_oom();
|
|
1119
|
+
*a = (nish_array){ argc, argc, (char *)d };
|
|
1120
|
+
nish_argv = a;
|
|
1121
|
+
while (argc-- > 0) {
|
|
1122
|
+
size_t len = strlen(*argv);
|
|
1123
|
+
nish_str *s = malloc(8 + len + 1);
|
|
1124
|
+
if (!s) nish_oom();
|
|
1125
|
+
s->len = len;
|
|
1126
|
+
strcpy(s->data, *argv++);
|
|
1127
|
+
*d++ = s;
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
/* ---- String to number: mode 0 parseFloat, 1 Number, 2 parseInt (contract: nish.h). ASCII
|
|
1132
|
+
* whitespace only; strtod parses once the inf/nan spellings JS rejects are ruled out, and its `0x`
|
|
1133
|
+
* hex stays accepted (documented). */
|
|
1134
|
+
#define NISH_SPACES " \t\n\v\f\r"
|
|
1135
|
+
|
|
1136
|
+
double nish_parse_number(const nish_str *s, int32_t mode) {
|
|
1137
|
+
const char *p, *q, *stop;
|
|
1138
|
+
char *end;
|
|
1139
|
+
double v;
|
|
1140
|
+
if (mode == 2) return strtoll(s->data, 0, 10);
|
|
1141
|
+
p = s->data + strspn(s->data, NISH_SPACES);
|
|
1142
|
+
q = p + (*p == '+' || *p == '-');
|
|
1143
|
+
stop = s->data + s->len;
|
|
1144
|
+
if (!((*q >= '0' && *q <= '9') || *q == '.' || strncmp(q, "Infinity", 8) == 0)) return mode && p == stop ? 0 : NAN;
|
|
1145
|
+
v = strtod(p, &end);
|
|
1146
|
+
if (end == p) return NAN; /* "." alone */
|
|
1147
|
+
return mode && end + strspn(end, NISH_SPACES) != stop ? NAN : v;
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
/* push() when len == cap: double the capacity (4 from empty). `elem_size` is
|
|
1151
|
+
* `sizeof` one element, which for a record element type (WP15 section 2a) is
|
|
1152
|
+
* the whole struct, so this relocates the elements themselves rather than a
|
|
1153
|
+
* block of pointers to them. Compiled code may therefore hold no pointer into
|
|
1154
|
+
* `data` across a push, and the checker refuses the programs that would. */
|
|
1155
|
+
void nish_array_grow(nish_array *a, uint64_t elem_size) {
|
|
1156
|
+
uint64_t cap = a->cap ? a->cap * 2 : 4;
|
|
1157
|
+
char *data = nish_alloc_struct(cap * elem_size);
|
|
1158
|
+
if (a->len) memcpy(data, a->data, a->len * elem_size);
|
|
1159
|
+
a->data = data;
|
|
1160
|
+
a->cap = cap;
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
void nish_panic_index(uint64_t idx, uint64_t len) {
|
|
1164
|
+
dprintf(2, "index out of range: %" PRIu64 " >= %" PRIu64 "\n", idx, len);
|
|
1165
|
+
_exit(1);
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
/* `s.slice(start, end)` (WP15 section 4) with a range the string does not
|
|
1169
|
+
contain. The two ends are printed as the half-open interval that was asked
|
|
1170
|
+
for, because the failure is as often a reversed pair as an end past the
|
|
1171
|
+
string, and `nish_panic_index`'s "i >= len" says nothing useful about the
|
|
1172
|
+
first of those. They are signed here although the check compares them
|
|
1173
|
+
unsigned: that comparison is a trick for folding `>= 0` into one `icmp`, and
|
|
1174
|
+
a reader who wrote `s.slice(i - 1)` wants to be told `-1` rather than
|
|
1175
|
+
18446744073709551615. The length cannot be negative either way. */
|
|
1176
|
+
void nish_panic_slice(int64_t start, int64_t end, int64_t len) {
|
|
1177
|
+
dprintf(2, "slice out of range: [%" PRId64 ", %" PRId64 ") of length %" PRId64 "\n", start, end, len);
|
|
1178
|
+
_exit(1);
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
/* ---- Checked division (Rust semantics): the failed-check path */
|
|
1182
|
+
void nish_panic_div(_Bool by_zero) {
|
|
1183
|
+
nish_die(by_zero ? "attempt to divide by zero\n" : "attempt to divide with overflow\n");
|
|
1184
|
+
}
|