-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbinary.ts
458 lines (387 loc) · 10.8 KB
/
binary.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import { AddBinaryFixed, StringAddFixedReversed } from "./add";
import { BitwiseNotBinary } from "./bitwise";
import type { DivTSBigInt, DivTSNumbers, Mod, ModBigInt } from "./hotscript-fork/numbers/impl/division";
import type { Length } from "./hotscript-fork/strings/impl/length";
import type { Add, AddBigInt } from './hotscript-fork/numbers/impl/addition';
import type { Convert } from './conversion';
import { WasmValue } from "./wasm";
import type { Satisfies } from './utils'
type PowersOfTwo = [
/* 2**0 */ 1,
/* 2**1 */ 2,
/* 2**2 */ 4,
/* 2**3 */ 8,
/* 2**4 */ 16,
/* 2**5 */ 32,
/* 2**6 */ 64,
/* 2**7 */ 128,
/* 2**8 */ 256,
/* 2**9 */ 512,
/* 2**10 */ 1024,
/* 2**11 */ 2048,
/* 2**12 */ 4096,
/* 2**13 */ 8192,
/* 2**14 */ 16384,
/* 2**15 */ 32768,
/* 2**16 */ 65536,
/* 2**17 */ 131072,
/* 2**18 */ 262144,
/* 2**19 */ 524288,
/* 2**20 */ 1048576,
/* 2**21 */ 2097152,
/* 2**22 */ 4194304,
/* 2**23 */ 8388608,
/* 2**24 */ 16777216,
/* 2**25 */ 33554432,
/* 2**26 */ 67108864,
/* 2**27 */ 134217728,
/* 2**28 */ 268435456,
/* 2**29 */ 536870912,
/* 2**30 */ 1073741824,
/* 2**31 */ 2147483648,
];
type PowersOfTwoBigInt = [
/* 2**0 */ 1n,
/* 2**1 */ 2n,
/* 2**2 */ 4n,
/* 2**3 */ 8n,
/* 2**4 */ 16n,
/* 2**5 */ 32n,
/* 2**6 */ 64n,
/* 2**7 */ 128n,
/* 2**8 */ 256n,
/* 2**9 */ 512n,
/* 2**10 */ 1024n,
/* 2**11 */ 2048n,
/* 2**12 */ 4096n,
/* 2**13 */ 8192n,
/* 2**14 */ 16384n,
/* 2**15 */ 32768n,
/* 2**16 */ 65536n,
/* 2**17 */ 131072n,
/* 2**18 */ 262144n,
/* 2**19 */ 524288n,
/* 2**20 */ 1048576n,
/* 2**21 */ 2097152n,
/* 2**22 */ 4194304n,
/* 2**23 */ 8388608n,
/* 2**24 */ 16777216n,
/* 2**25 */ 33554432n,
/* 2**26 */ 67108864n,
/* 2**27 */ 134217728n,
/* 2**28 */ 268435456n,
/* 2**29 */ 536870912n,
/* 2**30 */ 1073741824n,
/* 2**31 */ 2147483648n,
/* 2**32 */ 4294967296n,
/* 2**33 */ 8589934592n,
/* 2**34 */ 17179869184n,
/* 2**35 */ 34359738368n,
/* 2**36 */ 68719476736n,
/* 2**37 */ 137438953472n,
/* 2**38 */ 274877906944n,
/* 2**39 */ 549755813888n,
/* 2**40 */ 1099511627776n,
/* 2**41 */ 2199023255552n,
/* 2**42 */ 4398046511104n,
/* 2**43 */ 8796093022208n,
/* 2**44 */ 17592186044416n,
/* 2**45 */ 35184372088832n,
/* 2**46 */ 70368744177664n,
/* 2**47 */ 140737488355328n,
/* 2**48 */ 281474976710656n,
/* 2**49 */ 562949953421312n,
/* 2**50 */ 1125899906842624n,
/* 2**51 */ 2251799813685248n,
/* 2**52 */ 4503599627370496n,
/* 2**53 */ 9007199254740992n,
/* 2**54 */ 18014398509481984n,
/* 2**55 */ 36028797018963968n,
/* 2**56 */ 72057594037927936n,
/* 2**57 */ 144115188075855872n,
/* 2**58 */ 288230376151711744n,
/* 2**59 */ 576460752303423488n,
/* 2**60 */ 1152921504606846976n,
/* 2**61 */ 2305843009213693952n,
/* 2**62 */ 4611686018427387904n,
/* 2**63 */ 9223372036854775808n,
/* 2**64 */ 18446744073709551616n,
];
type ProcessTSNumber<
T extends number,
_Acc extends string = ''
> =
T extends 0
? _Acc
: Mod<T, 2> extends infer remainder
? remainder extends 0
? ProcessTSNumber<DivTSNumbers<T, 2>, `0${_Acc}`>
: ProcessTSNumber<DivTSNumbers<T, 2>, `1${_Acc}`>
:never;
type ProcessTSBigInt<
T extends bigint,
_Acc extends string = ''
> =
T extends 0n
? _Acc
: ModBigInt<T, 2n> extends infer remainder
? remainder extends 0n
? ProcessTSBigInt<DivTSBigInt<T, 2n>, `0${_Acc}`>
: ProcessTSBigInt<DivTSBigInt<T, 2n>, `1${_Acc}`>
:never;
export namespace Pad {
export type StartWith8Zeros<
input extends string
> = `00000000${input}`
export type StartWith16Zeros<
input extends string
> = `0000000000000000${input}`
export type StartWith24Zeros<
input extends string
> = `000000000000000000000000${input}`
export type StartWith32Zeros<
input extends string
> = `00000000000000000000000000000000${input}`
export type StartWith40Zeros<
input extends string
> = `0000000000000000000000000000000000000000${input}`
export type StartWith48Zeros<
input extends string
> = `000000000000000000000000000000000000000000000000${input}`
export type StartWith56Zeros<
input extends string
> = `00000000000000000000000000000000000000000000000000000000${input}`
/** @deprecated avoid using this and try to use one of the dedicated ones if you know how many 0s you want to add */
export type StartWithZeros<
input extends string,
finalLength extends number,
> = Satisfies<string,
Length<input> extends finalLength
? input
: StartWithZeros<`0${input}`, finalLength>
>
export type StartWith8Ones<
input extends string
> = `11111111${input}`
export type StartWith16Ones<
input extends string
> = `1111111111111111${input}`
export type StartWith24Ones<
input extends string
> = `111111111111111111111111${input}`
export type StartWith32Ones<
input extends string
> = `11111111111111111111111111111111${input}`
export type StartWith40Ones<
input extends string
> = `1111111111111111111111111111111111111111${input}`
export type StartWith48Ones<
input extends string
> = `111111111111111111111111111111111111111111111111${input}`
export type StartWith56Ones<
input extends string
> = `11111111111111111111111111111111111111111111111111111111${input}`
}
export type TsBigIntIsNegative<
T extends bigint
> =
`${T}` extends `-${string}`
? true
: false
export type TsNumberIsNegative<
T extends number
> =
`${T}` extends `-${string}`
? true
: false
export type To32Binary<
T extends number,
> = Satisfies<string,
TsNumberIsNegative<T> extends true
? // we need do the Two's Complement fliperouney thing
TwosComplementFlip<Pad.StartWithZeros<ProcessTSNumber<T>, 32>>
: // full 32 bit
Pad.StartWithZeros<ProcessTSNumber<T>, 32>
>
export type To64Binary<
T extends bigint,
> = Satisfies<string,
TsBigIntIsNegative<T> extends true
? // we need do the Two's Complement fliperouney thing
TwosComplementFlip<Pad.StartWithZeros<ProcessTSBigInt<T>, 64>>
: // full 64 bit
Pad.StartWithZeros<ProcessTSBigInt<T>, 64>
>
/**
* @interesting this can do a 32 bit string in four gulps (rather than 32)
*
*/
export type ReverseString8Segments<T extends string> =
// let's go 8 at a time (rather than 1)
T extends `${infer h1}${infer h2}${infer h3}${infer h4}${infer h5}${infer h6}${infer h7}${infer h8}${infer Tail}`
? // put them back together (but backwards)
`${ReverseString8Segments<Tail>}${h8}${h7}${h6}${h5}${h4}${h3}${h2}${h1}`
: // we have less than 8 left
T extends `${infer h0}${infer t0}`
? // it wasn't some multiple of 8 so to finish up let's go one at a time
`${ReverseString8Segments<t0>}${h0}`
: // we're done
''
/** You better be absolutely sure there's no other way to chunk up the string. */
export type ReverseStringTheWorstWayPossible<
T extends string
> =
T extends `${infer L}${infer R}`
? `${ReverseStringTheWorstWayPossible<R>}${L}`
: ''
// QUESTION: I have lookup tables for 0-255 (from both binary and decimal).
// Would it be better or faster to just check that object real quick first and return that value if it exists?
export type ToDecimalUnsigned<T extends string> =
_ToDecimalUnsigned<ReverseString8Segments<T>>;
type _ToDecimalUnsigned<
Binary extends string,
_PowerOfTwo extends number = 0,
_NextPowerOfTwo extends number = Add<_PowerOfTwo, 1>
> =
Binary extends `${infer Head}${infer Tail}`
? Head extends '0'
? _ToDecimalUnsigned<
Tail,
_NextPowerOfTwo
>
: Head extends '1'
? Add<
_ToDecimalUnsigned<
Tail,
_NextPowerOfTwo
>,
PowersOfTwo[_PowerOfTwo]
>
: never
: 0
type TSNumberToTSBigint<
T extends number
> = Satisfies<bigint,
`${T}` extends `${infer X extends bigint}` ? X : never
>
export type ToDecimalUnsignedBigInt<
T extends string
> = Satisfies<bigint,
_ToDecimalUnsignedBigInt<ReverseString8Segments<T>>
>;
type _ToDecimalUnsignedBigInt<
Binary extends string,
_PowerOfTwo extends bigint = 0n,
_NextPowerOfTwo extends bigint = AddBigInt<_PowerOfTwo, 1n>
> =
Binary extends `${infer Head}${infer Tail}`
? Head extends '0'
? _ToDecimalUnsignedBigInt<
Tail,
_NextPowerOfTwo
>
: Head extends '1'
? AddBigInt<
_ToDecimalUnsignedBigInt<
Tail,
_NextPowerOfTwo
>,
PowersOfTwoBigInt[Convert.TSBigInt.ToTSNumber<_PowerOfTwo>]
>
: never
: 0n
/** '0' or '1' */
export type Bit = string;
export type IsNegativeBinary<
binary extends string
> = Satisfies<boolean,
binary extends `1${string}`
? true
: false
>
export type SignBit<
binary extends string
> = Satisfies<Bit,
binary extends `${infer Head extends Bit}${string}`
? Head
: never
>
// type x = ToDecimalUnsigned<"00000000001100010111100011000110">
// ^?
export type TwosComplementFlip<
T extends string
> = ReverseString8Segments<
StringAddFixedReversed<
ReverseString8Segments<
BitwiseNotBinary<T>
>,
"1000000000000000000000000000000000000000000000000000000000000000", // pre-reversed 1
[]
>
>
// this takes a positive TsNumber nad makes it negative
export type WithNegativeSign<
T extends number
> = `-${T}` extends `${infer U extends number}` ? U : never
// this takes a positive TsNumber nad makes it negative
export type WithNegativeSignBigInt<
T extends bigint
> = `-${T}` extends `${infer U extends bigint}` ? U : never
export type ToDecimalSigned<
binary extends string
> = Satisfies<number,
binary extends `0${string}`
? // positive number
ToDecimalUnsigned<binary>
: // negative number
WithNegativeSign<
ToDecimalUnsigned<
TwosComplementFlip<
binary
>
>
>
>
export type ToDecimalSignedBigInt<
binary extends string
> = Satisfies<bigint,
binary extends `0${string}`
? // positive number
ToDecimalUnsignedBigInt<binary>
: // negative number
WithNegativeSignBigInt<
ToDecimalUnsignedBigInt<
TwosComplementFlip<
binary
>
>
>
>
type CountLeadingZeros<
a extends WasmValue,
count extends 1[] = []
> = Satisfies<number,
a extends `0${infer tail extends string}`
? CountLeadingZeros<
tail,
[...count, 1]
>
: count['length']
>
export type I32ClzBinary<
a extends WasmValue
> = Satisfies<WasmValue,
Convert.TSNumber.ToWasmValue<
CountLeadingZeros<a>,
'i32'
>
>
export type I64ClzBinary64<
a extends WasmValue
> = Satisfies<WasmValue,
Convert.TSBigInt.ToWasmValue<
Convert.TSNumber.ToTSBigInt<
CountLeadingZeros<a>
>
>
>