-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
153 lines (121 loc) · 3.79 KB
/
Copy pathindex.ts
File metadata and controls
153 lines (121 loc) · 3.79 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
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
import {fold} from "@softwareventures/array";
/** Coerces the specified value to a signed 32-bit integer. */
export function i32(value: number): number {
return value | 0;
}
export function inot(value: number): number {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (!(value | 0) as any) | 0;
}
export function icmp(value: number): number {
return ~value;
}
export function ineg(value: number): number {
return -(value | 0) | 0;
}
export function ipow(a: number, b: number): number {
// Adapted from https://stackoverflow.com/a/101613/31662
let base = a | 0;
let exp = b | 0;
if (exp < 0) {
return 0;
}
if (base === 1) {
return 1;
}
if (exp >= 31) {
return 0;
}
if (base === 2) {
return 1 << exp;
}
let accumulator = 1;
// eslint-disable-next-line no-constant-condition
while (true) {
if (exp & 1) {
const next = imul(accumulator, base);
if ((accumulator > 0xb504 || base > 0xb504) && idiv(next, accumulator) !== base) {
return 0;
}
accumulator = next;
}
exp = exp >>> 1;
if (exp === 0) {
return accumulator;
}
if (base > 0xb504) {
return 0;
}
base = imul(base, base);
}
}
export import imul = require("imul");
export function iproduct(...values: number[]): number {
return fold(values, imul, 1);
}
export function idiv(a: number, b: number): number {
return ((a | 0) / (b | 0)) | 0;
}
export function imod(a: number, b: number): number {
return (a | 0) % (b | 0) | 0;
}
export function iadd(a: number, b: number): number {
return ((a | 0) + (b | 0)) | 0;
}
export function isum(...values: number[]): number {
return fold(values, iadd, 0);
}
export function isub(a: number, b: number): number {
return ((a | 0) - (b | 0)) | 0;
}
export function ishl(a: number, b: number): number {
return a << b;
}
export function ishr(a: number, b: number): number {
return a >> b;
}
export function ilt(a: number, b: number): number {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (((a | 0) < (b | 0)) as any) | 0;
}
export function ilte(a: number, b: number): number {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (((a | 0) <= (b | 0)) as any) | 0;
}
export function igt(a: number, b: number): number {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (((a | 0) > (b | 0)) as any) | 0;
}
export function igte(a: number, b: number): number {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (((a | 0) >= (b | 0)) as any) | 0;
}
export function ieq(a: number, b: number): number {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (((a | 0) === (b | 0)) as any) | 0;
}
export function ineq(a: number, b: number): number {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (((a | 0) !== (b | 0)) as any) | 0;
}
export function iband(a: number, b: number): number {
return a & b;
}
export function ixor(a: number, b: number): number {
return a ^ b;
}
export function ibor(a: number, b: number): number {
return a | b;
}
export function iand(a: number, b: number): number {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return ((a | 0 && b | 0) as any) | 0;
}
export function ior(a: number, b: number): number {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return ((a | 0 || b | 0) as any) | 0;
}
export function iclamp(n: number, min: number, max: number): number {
return i32(Math.max(Math.min(i32(n), i32(max)), i32(min)));
}