-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathadd.ts
267 lines (226 loc) · 6.79 KB
/
add.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
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
import { ReverseString8Segments } from "./binary";
import { Ensure } from "./ensure";
import { Add } from "./hotscript-fork/numbers/impl/addition";
import { WasmValue } from "./wasm";
import type { Satisfies } from './utils'
type Counter = 1[];
type DigitToCounter<T extends string> =
T extends '0'
? []
: [1]
type Calculation = {
digit: string,
carry: Counter,
}
// TODO(perf) look into using a regular string here instead of DigitToCounter
/** this creates a Calculation for the addition */
type Calculate<T extends Counter> =
T['length'] extends 0 ? { digit: '0', carry: [] } :
T['length'] extends 1 ? { digit: '1', carry: [] } :
T['length'] extends 2 ? { digit: '0', carry: [1] } :
T['length'] extends 3 ? { digit: '1', carry: [1] } :
never
// QUESTION: is this faster?
// vvvvvvvvvvvvvvvvvvvvvvvvv
// T extends [1, 1, ...infer Remainder]
// ? {
// digit: `${Remainder['length']}`
// carry: [1]
// }
// : {
// digit: `${T['length']}`
// carry: []
// }
/** This function's purpose in life is to avoid needing to calculate C twice */
type AppendCalculationFixedReversed<
A_Remaining extends string,
B_Remaining extends string,
C extends Calculation
> = `${
C['digit']
}${
StringAddFixedReversed<
A_Remaining,
B_Remaining,
C['carry']
>
}`
export type StringAddFixedReversed<
A extends string,
B extends string,
carry extends Counter,
> =
A extends `${infer A_Digit}${infer A_Remaining}`
? // A has a digit
B extends `${infer B_Digit}${infer B_Remaining}`
? // A and B both have a digit
AppendCalculationFixedReversed<
A_Remaining,
B_Remaining,
Calculate<[
...DigitToCounter<A_Digit>,
...DigitToCounter<B_Digit>,
...carry,
]>
>
: // A has a digit, but B does not
''
: // out of digits. fuck the carry.
''
export type AddBinaryFixed<
A extends string,
B extends string,
> = Satisfies<string,
// we reverse the strings so we can add them from right to left
// there's no simply way in TypeScript to pick a character off the end of a string
// this is a huge performance bottleneck (in terms of recursions)
ReverseString8Segments<
StringAddFixedReversed<
ReverseString8Segments<A>,
ReverseString8Segments<B>,
[]
>
>
>
/** This function's purpose in life is to avoid needing to calculate C twice */
type AppendCalculationArbitraryReversed<
A_Remaining extends string,
B_Remaining extends string,
C extends Calculation
> = `${
C['digit']
}${
StringAddArbitraryReversed<
A_Remaining,
B_Remaining,
C['carry']
>
}`
export type StringAddArbitraryReversed<
A extends string,
B extends string,
carry extends Counter,
> =
A extends `${infer A_Digit}${infer A_Remaining}`
? // A has a digit
B extends `${infer B_Digit}${infer B_Remaining}`
? // A and B both have a digit
AppendCalculationArbitraryReversed<
A_Remaining,
B_Remaining,
Calculate<[
...DigitToCounter<A_Digit>,
...DigitToCounter<B_Digit>,
...carry,
]>
>
: // A has a digit, but B does not
AppendCalculationArbitraryReversed<
A_Remaining,
'', // since we know B is out of digits, we can just pass an empty string
Calculate<[
...DigitToCounter<A_Digit>,
...carry
]>
>
: // A does not have a digit
B extends `${infer B_Digit}${infer B_Remaining}`
? // A does not have a digit, but B does
AppendCalculationArbitraryReversed<
'', // since we know A is out of digits, we can just pass an empty string
B_Remaining,
Calculate<[
...DigitToCounter<B_Digit>,
...carry
]>
>
: // A and B do not have digits left. just need to resolve the carry
carry extends []
? // there was no a carry, base case of recursion
''
: // there was a carry, base case of recursion
'1'
export type I32AddBinary<
a extends WasmValue,
b extends WasmValue
> = Satisfies<WasmValue,
AddBinaryFixed<a, b>
>
export type I64AddBinary<
a extends WasmValue,
b extends WasmValue
> = Satisfies<WasmValue,
AddBinaryFixed<a, b>
>
export type I32AddDecimal<
a extends number,
b extends number,
> = Satisfies<number,
Add<a, b>
>
/*
The following is a disappointing failure.
The goal is to do addition from left to right (i.e. MSB to LSB).
To accomplish this, you consider a moving window where you can look at the next digit of A and B _as well as_ the next digit of each _after that_.
The problem is, you need an effectively infinite lookup to make sure you don't have to carry.
Consider the following:
'011',
'001'
for the first window we see `01` in A and `00` in B. This is a simple addition, no carry.
EXCEPT IT ISN"T because in the next frame you have a carry but you just can't see it yet.
That means you effectively have to implement an infinite forward lookup. I might. I fucking just might. But in the end it'll probably be easier to just.... I can't believe I'm even saying this... to just store everything in reverse order all the time.
*/
// type AdditionMatrixLookup = {
// // AABB
// // 0101
// '0000': '0', // one 1
// '0001': '0', // one 1
// '0010': '1', // one 1
// '0100': '0', // one 1
// '1000': '1', // one 1
// '0011': '1', // two 1s
// '0110': '1', // two 1s
// '1100': '1', // two 1s
// '1001': '1', // two 1s
// '0101': '1', // two 1s
// '1010': '0', // two 1s
// '1110': '0', // three 1s
// '1101': '0', // three 1s
// '1011': '0', // three 1s
// '0111': '0', // three 1s
// '1111': '1', // four 1s
// }
// type AdditionLookup = {
// // AB
// '00': '0',
// '10': '1',
// '01': '1',
// '11': '0',
// }
// export type AddBinaryDoNotLookDownVersion<
// A extends string,
// B extends string,
// Acc extends string = '',
// > =
// A extends `${infer A0 extends string}${infer A1 extends string}${infer ARest}`
// ? B extends `${infer B0 extends string}${infer B1 extends string}${infer BRest}`
// ? `${A0}${A1}${B0}${B1}` extends infer Lookup extends keyof AdditionMatrixLookup
// ? AddBinaryDoNotLookDownVersion<
// `${A1}${ARest}`,
// `${B1}${BRest}`,
// `${Acc}${AdditionMatrixLookup[Lookup]}`
// >
// : never // a not-binary number was passed in
// : never // unequal lengths were passed in
// : // we've reached the last digits
// `${Acc}${
// `${A}${B}` extends infer Lookup extends keyof AdditionLookup
// ? AdditionLookup[Lookup]
// : never // a not-binary number was passed in
// }`
// type t = AddBinaryDoNotLookDownVersion<
// '011',
// '001'
// // 0x
// // ^- here's where it fucks up because it can't see the carry coming from far ahead
// > // =>