@the-w1zard/singularity 1.0.11 → 1.0.13

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.
@@ -0,0 +1,120 @@
1
+ let f64, pidToSlot, levelCounter, vm;
2
+ const programs = [];
3
+ export class CellVM {
4
+ f64;
5
+ pidToSlot;
6
+ #stack = new Float64Array(256);
7
+ constructor(f64, pidToSlot) {
8
+ this.f64 = f64;
9
+ this.pidToSlot = pidToSlot;
10
+ }
11
+ run(p) {
12
+ let sp = 0;
13
+ const { code, consts } = p;
14
+ for (let ip = 0; ip < code.length; ip += 2) {
15
+ const op = code[ip], arg = code[ip + 1];
16
+ switch (op) {
17
+ case 0 /* VM_OP.PUSH_CONST */:
18
+ this.#stack[sp++] = consts[arg];
19
+ break;
20
+ case 1 /* VM_OP.PUSH_CELL */: {
21
+ const slot = Atomics.load(this.pidToSlot, arg);
22
+ this.#stack[sp++] = slot < 0 ? NaN : this.f64[slot];
23
+ break;
24
+ }
25
+ case 2 /* VM_OP.ADD */:
26
+ this.#stack[sp - 2] += this.#stack[--sp];
27
+ break;
28
+ case 3 /* VM_OP.SUB */:
29
+ this.#stack[sp - 2] -= this.#stack[--sp];
30
+ break;
31
+ case 4 /* VM_OP.MUL */:
32
+ this.#stack[sp - 2] *= this.#stack[--sp];
33
+ break;
34
+ case 5 /* VM_OP.DIV */:
35
+ this.#stack[sp - 2] /= this.#stack[--sp];
36
+ break;
37
+ case 7 /* VM_OP.RET */: return this.#stack[0];
38
+ case 6 /* VM_OP.SUM_RANGE */: {
39
+ let s = 0;
40
+ for (const pid of p.ranges[arg]) {
41
+ const sl = Atomics.load(this.pidToSlot, pid);
42
+ if (sl >= 0)
43
+ s += this.f64[sl];
44
+ }
45
+ this.#stack[sp++] = s;
46
+ break;
47
+ }
48
+ case 8 /* VM_OP.AVG_RANGE */: {
49
+ let s = 0, n = 0;
50
+ for (const pid of p.ranges[arg]) {
51
+ const sl = Atomics.load(this.pidToSlot, pid);
52
+ if (sl < 0)
53
+ continue;
54
+ s += this.f64[sl];
55
+ n++;
56
+ }
57
+ this.#stack[sp++] = n ? s / n : 0;
58
+ break;
59
+ }
60
+ case 9 /* VM_OP.MIN_RANGE */: {
61
+ let m = Infinity;
62
+ for (const pid of p.ranges[arg]) {
63
+ const sl = Atomics.load(this.pidToSlot, pid);
64
+ if (sl >= 0)
65
+ m = Math.min(m, this.f64[sl]);
66
+ }
67
+ this.#stack[sp++] = Number.isFinite(m) ? m : 0;
68
+ break;
69
+ }
70
+ case 10 /* VM_OP.MAX_RANGE */: {
71
+ let m = -Infinity;
72
+ for (const pid of p.ranges[arg]) {
73
+ const sl = Atomics.load(this.pidToSlot, pid);
74
+ if (sl >= 0)
75
+ m = Math.max(m, this.f64[sl]);
76
+ }
77
+ this.#stack[sp++] = Number.isFinite(m) ? m : 0;
78
+ break;
79
+ }
80
+ case 11 /* VM_OP.COUNT_RANGE */: {
81
+ let n = 0;
82
+ for (const pid of p.ranges[arg])
83
+ if (Atomics.load(this.pidToSlot, pid) >= 0)
84
+ n++;
85
+ this.#stack[sp++] = n;
86
+ break;
87
+ }
88
+ }
89
+ }
90
+ return this.#stack[0];
91
+ }
92
+ }
93
+ self.onmessage = (e) => {
94
+ const d = e.data;
95
+ if (d.type === "init") {
96
+ f64 = new Float64Array(d.f64);
97
+ pidToSlot = new Int32Array(d.pidToSlot);
98
+ levelCounter = new Int32Array(d.levelCounter);
99
+ vm = new CellVM(f64, pidToSlot);
100
+ return;
101
+ }
102
+ if (d.type === "register") {
103
+ programs[d.pid] = d.program;
104
+ return;
105
+ }
106
+ if (d.type === "computeLevel") {
107
+ const pids = d.pids;
108
+ for (let i = 0; i < pids.length; i++) {
109
+ const pid = pids[i];
110
+ const program = programs[pid];
111
+ if (!program)
112
+ continue;
113
+ const slot = Atomics.load(pidToSlot, pid);
114
+ if (slot >= 0)
115
+ f64[slot] = vm.run(program);
116
+ }
117
+ Atomics.add(levelCounter, 0, -pids.length);
118
+ Atomics.notify(levelCounter, 0);
119
+ }
120
+ };