-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
127 lines (99 loc) · 2.84 KB
/
index.ts
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
import {fold} from "@softwareventures/array";
import {ieq as ueq, imul, ineq as uneq, inot as unot} from "i32";
export function u32(value: number): number {
return value >>> 0;
}
export {unot};
export function ucmp(value: number): number {
return (~value) >>> 0;
}
export function uneg(value: number): number {
return (-(value | 0)) >>> 0;
}
export function upow(a: number, b: number): number {
// Adapted from https://stackoverflow.com/a/101613/31662
let base = a >>> 0;
let exp = b >>> 0;
if (base === 1) {
return 1;
}
if (exp >= 32) {
return 0;
}
if (base === 2) {
return (1 << exp) >>> 0;
}
let accumulator = 1;
while (true) {
if (exp & 1) {
const next = umul(accumulator, base);
if ((accumulator > 0xffff || base > 0xffff) && udiv(next, accumulator) !== base) {
return 0;
}
accumulator = next;
}
exp = exp >>> 1;
if (exp === 0) {
return accumulator;
}
if (base > 0xffff) {
return 0;
}
base = umul(base, base);
}
}
export function umul(a: number, b: number): number {
return imul(a >>> 0, b >>> 0) >>> 0;
}
export function uproduct(...values: number[]): number {
return fold(values, umul, 1);
}
export function udiv(a: number, b: number): number {
return ((a >>> 0) / (b >>> 0)) >>> 0;
}
export function umod(a: number, b: number): number {
return ((a >>> 0) % (b >>> 0)) >>> 0;
}
export function uadd(a: number, b: number): number {
return ((a >>> 0) + (b >>> 0)) >>> 0;
}
export function usum(...values: number[]): number {
return fold(values, uadd, 0);
}
export function usub(a: number, b: number): number {
return ((a >>> 0) - (b >>> 0)) >>> 0;
}
export function ushl(a: number, b: number): number {
return (a << b) >>> 0;
}
export function ushr(a: number, b: number): number {
return a >>> b;
}
export function ult(a: number, b: number): number {
return (((a >>> 0) < (b >>> 0)) as any) >>> 0;
}
export function ulte(a: number, b: number): number {
return (((a >>> 0) <= (b >>> 0)) as any) >>> 0;
}
export function ugt(a: number, b: number): number {
return (((a >>> 0) > (b >>> 0)) as any) >>> 0;
}
export function ugte(a: number, b: number): number {
return (((a >>> 0) >= (b >>> 0)) as any) >>> 0;
}
export {ueq, uneq};
export function uband(a: number, b: number): number {
return (a & b) >>> 0;
}
export function uxor(a: number, b: number): number {
return (a ^ b) >>> 0;
}
export function ubor(a: number, b: number): number {
return (a | b) >>> 0;
}
export function uand(a: number, b: number): number {
return (((a >>> 0) && (b >>> 0)) as any) >>> 0;
}
export function uor(a: number, b: number): number {
return (((a >>> 0) || (b >>> 0)) as any) >>> 0;
}