forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnum.fe
More file actions
1906 lines (1789 loc) · 84 KB
/
Copy pathnum.fe
File metadata and controls
1906 lines (1789 loc) · 84 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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use super::ops::*
use super::default::Default
use super::marker::Copy
use super::option::Option::{self, *}
use super::panic
// Generic bitcast intrinsic - reinterprets bits between any two primitive integer types
extern {
const fn __bitcast<From, To>(_: From) -> To
}
// Generic checked arithmetic intrinsics for primitive integer types.
extern {
const fn __checked_add<T: IntWord>(_: T, _: T) -> T
const fn __checked_sub<T: IntWord>(_: T, _: T) -> T
const fn __checked_mul<T: IntWord>(_: T, _: T) -> T
const fn __checked_div<T: IntWord>(_: T, _: T) -> T
const fn __checked_rem<T: IntWord>(_: T, _: T) -> T
const fn __checked_pow<T: IntWord>(_: T, _: T) -> T
const fn __checked_neg<T: IntWord>(_: T) -> T
}
// Generic saturating arithmetic intrinsics for primitive integer types.
extern {
const fn __saturating_add<T: IntWord>(_: T, _: T) -> T
const fn __saturating_sub<T: IntWord>(_: T, _: T) -> T
const fn __saturating_mul<T: IntWord>(_: T, _: T) -> T
}
// Extern intrinsics for u8
extern {
const fn __add_u8(_: u8, _: u8) -> u8
const fn __sub_u8(_: u8, _: u8) -> u8
const fn __mul_u8(_: u8, _: u8) -> u8
const fn __div_u8(_: u8, _: u8) -> u8
const fn __rem_u8(_: u8, _: u8) -> u8
const fn __pow_u8(_: u8, _: u8) -> u8
const fn __shl_u8(_: u8, _: u8) -> u8
const fn __shr_u8(_: u8, _: u8) -> u8
const fn __bitand_u8(_: u8, _: u8) -> u8
const fn __bitor_u8(_: u8, _: u8) -> u8
const fn __bitxor_u8(_: u8, _: u8) -> u8
const fn __eq_u8(_: u8, _: u8) -> bool
const fn __ne_u8(_: u8, _: u8) -> bool
const fn __lt_u8(_: u8, _: u8) -> bool
const fn __le_u8(_: u8, _: u8) -> bool
const fn __gt_u8(_: u8, _: u8) -> bool
const fn __ge_u8(_: u8, _: u8) -> bool
const fn __bitnot_u8(_: u8) -> u8
}
// Extern intrinsics for i8
extern {
const fn __add_i8(_: i8, _: i8) -> i8
const fn __sub_i8(_: i8, _: i8) -> i8
const fn __mul_i8(_: i8, _: i8) -> i8
const fn __div_i8(_: i8, _: i8) -> i8
const fn __rem_i8(_: i8, _: i8) -> i8
const fn __pow_i8(_: i8, _: i8) -> i8
const fn __shl_i8(_: i8, _: i8) -> i8
const fn __shr_i8(_: i8, _: i8) -> i8
const fn __bitand_i8(_: i8, _: i8) -> i8
const fn __bitor_i8(_: i8, _: i8) -> i8
const fn __bitxor_i8(_: i8, _: i8) -> i8
const fn __eq_i8(_: i8, _: i8) -> bool
const fn __ne_i8(_: i8, _: i8) -> bool
const fn __lt_i8(_: i8, _: i8) -> bool
const fn __le_i8(_: i8, _: i8) -> bool
const fn __gt_i8(_: i8, _: i8) -> bool
const fn __ge_i8(_: i8, _: i8) -> bool
const fn __neg_i8(_: i8) -> i8
const fn __bitnot_i8(_: i8) -> i8
}
// Extern intrinsics for i16
extern {
const fn __add_i16(_: i16, _: i16) -> i16
const fn __sub_i16(_: i16, _: i16) -> i16
const fn __mul_i16(_: i16, _: i16) -> i16
const fn __div_i16(_: i16, _: i16) -> i16
const fn __rem_i16(_: i16, _: i16) -> i16
const fn __pow_i16(_: i16, _: i16) -> i16
const fn __shl_i16(_: i16, _: i16) -> i16
const fn __shr_i16(_: i16, _: i16) -> i16
const fn __bitand_i16(_: i16, _: i16) -> i16
const fn __bitor_i16(_: i16, _: i16) -> i16
const fn __bitxor_i16(_: i16, _: i16) -> i16
const fn __eq_i16(_: i16, _: i16) -> bool
const fn __ne_i16(_: i16, _: i16) -> bool
const fn __lt_i16(_: i16, _: i16) -> bool
const fn __le_i16(_: i16, _: i16) -> bool
const fn __gt_i16(_: i16, _: i16) -> bool
const fn __ge_i16(_: i16, _: i16) -> bool
const fn __neg_i16(_: i16) -> i16
const fn __bitnot_i16(_: i16) -> i16
}
// Extern intrinsics for u16
extern {
const fn __add_u16(_: u16, _: u16) -> u16
const fn __sub_u16(_: u16, _: u16) -> u16
const fn __mul_u16(_: u16, _: u16) -> u16
const fn __div_u16(_: u16, _: u16) -> u16
const fn __rem_u16(_: u16, _: u16) -> u16
const fn __pow_u16(_: u16, _: u16) -> u16
const fn __shl_u16(_: u16, _: u16) -> u16
const fn __shr_u16(_: u16, _: u16) -> u16
const fn __bitand_u16(_: u16, _: u16) -> u16
const fn __bitor_u16(_: u16, _: u16) -> u16
const fn __bitxor_u16(_: u16, _: u16) -> u16
const fn __eq_u16(_: u16, _: u16) -> bool
const fn __ne_u16(_: u16, _: u16) -> bool
const fn __lt_u16(_: u16, _: u16) -> bool
const fn __le_u16(_: u16, _: u16) -> bool
const fn __gt_u16(_: u16, _: u16) -> bool
const fn __ge_u16(_: u16, _: u16) -> bool
const fn __bitnot_u16(_: u16) -> u16
}
// Extern intrinsics for i32
extern {
const fn __add_i32(_: i32, _: i32) -> i32
const fn __sub_i32(_: i32, _: i32) -> i32
const fn __mul_i32(_: i32, _: i32) -> i32
const fn __div_i32(_: i32, _: i32) -> i32
const fn __rem_i32(_: i32, _: i32) -> i32
const fn __pow_i32(_: i32, _: i32) -> i32
const fn __shl_i32(_: i32, _: i32) -> i32
const fn __shr_i32(_: i32, _: i32) -> i32
const fn __bitand_i32(_: i32, _: i32) -> i32
const fn __bitor_i32(_: i32, _: i32) -> i32
const fn __bitxor_i32(_: i32, _: i32) -> i32
const fn __eq_i32(_: i32, _: i32) -> bool
const fn __ne_i32(_: i32, _: i32) -> bool
const fn __lt_i32(_: i32, _: i32) -> bool
const fn __le_i32(_: i32, _: i32) -> bool
const fn __gt_i32(_: i32, _: i32) -> bool
const fn __ge_i32(_: i32, _: i32) -> bool
const fn __neg_i32(_: i32) -> i32
const fn __bitnot_i32(_: i32) -> i32
}
// Extern intrinsics for u32
extern {
const fn __add_u32(_: u32, _: u32) -> u32
const fn __sub_u32(_: u32, _: u32) -> u32
const fn __mul_u32(_: u32, _: u32) -> u32
const fn __div_u32(_: u32, _: u32) -> u32
const fn __rem_u32(_: u32, _: u32) -> u32
const fn __pow_u32(_: u32, _: u32) -> u32
const fn __shl_u32(_: u32, _: u32) -> u32
const fn __shr_u32(_: u32, _: u32) -> u32
const fn __bitand_u32(_: u32, _: u32) -> u32
const fn __bitor_u32(_: u32, _: u32) -> u32
const fn __bitxor_u32(_: u32, _: u32) -> u32
const fn __eq_u32(_: u32, _: u32) -> bool
const fn __ne_u32(_: u32, _: u32) -> bool
const fn __lt_u32(_: u32, _: u32) -> bool
const fn __le_u32(_: u32, _: u32) -> bool
const fn __gt_u32(_: u32, _: u32) -> bool
const fn __ge_u32(_: u32, _: u32) -> bool
const fn __bitnot_u32(_: u32) -> u32
}
// Extern intrinsics for i64
extern {
const fn __add_i64(_: i64, _: i64) -> i64
const fn __sub_i64(_: i64, _: i64) -> i64
const fn __mul_i64(_: i64, _: i64) -> i64
const fn __div_i64(_: i64, _: i64) -> i64
const fn __rem_i64(_: i64, _: i64) -> i64
const fn __pow_i64(_: i64, _: i64) -> i64
const fn __shl_i64(_: i64, _: i64) -> i64
const fn __shr_i64(_: i64, _: i64) -> i64
const fn __bitand_i64(_: i64, _: i64) -> i64
const fn __bitor_i64(_: i64, _: i64) -> i64
const fn __bitxor_i64(_: i64, _: i64) -> i64
const fn __eq_i64(_: i64, _: i64) -> bool
const fn __ne_i64(_: i64, _: i64) -> bool
const fn __lt_i64(_: i64, _: i64) -> bool
const fn __le_i64(_: i64, _: i64) -> bool
const fn __gt_i64(_: i64, _: i64) -> bool
const fn __ge_i64(_: i64, _: i64) -> bool
const fn __neg_i64(_: i64) -> i64
const fn __bitnot_i64(_: i64) -> i64
}
// Extern intrinsics for i128
extern {
const fn __add_i128(_: i128, _: i128) -> i128
const fn __sub_i128(_: i128, _: i128) -> i128
const fn __mul_i128(_: i128, _: i128) -> i128
const fn __div_i128(_: i128, _: i128) -> i128
const fn __rem_i128(_: i128, _: i128) -> i128
const fn __pow_i128(_: i128, _: i128) -> i128
const fn __shl_i128(_: i128, _: i128) -> i128
const fn __shr_i128(_: i128, _: i128) -> i128
const fn __bitand_i128(_: i128, _: i128) -> i128
const fn __bitor_i128(_: i128, _: i128) -> i128
const fn __bitxor_i128(_: i128, _: i128) -> i128
const fn __eq_i128(_: i128, _: i128) -> bool
const fn __ne_i128(_: i128, _: i128) -> bool
const fn __lt_i128(_: i128, _: i128) -> bool
const fn __le_i128(_: i128, _: i128) -> bool
const fn __gt_i128(_: i128, _: i128) -> bool
const fn __ge_i128(_: i128, _: i128) -> bool
const fn __neg_i128(_: i128) -> i128
const fn __bitnot_i128(_: i128) -> i128
}
// Extern intrinsics for u64
extern {
const fn __add_u64(_: u64, _: u64) -> u64
const fn __sub_u64(_: u64, _: u64) -> u64
const fn __mul_u64(_: u64, _: u64) -> u64
const fn __div_u64(_: u64, _: u64) -> u64
const fn __rem_u64(_: u64, _: u64) -> u64
const fn __pow_u64(_: u64, _: u64) -> u64
const fn __shl_u64(_: u64, _: u64) -> u64
const fn __shr_u64(_: u64, _: u64) -> u64
const fn __bitand_u64(_: u64, _: u64) -> u64
const fn __bitor_u64(_: u64, _: u64) -> u64
const fn __bitxor_u64(_: u64, _: u64) -> u64
const fn __eq_u64(_: u64, _: u64) -> bool
const fn __ne_u64(_: u64, _: u64) -> bool
const fn __lt_u64(_: u64, _: u64) -> bool
const fn __le_u64(_: u64, _: u64) -> bool
const fn __gt_u64(_: u64, _: u64) -> bool
const fn __ge_u64(_: u64, _: u64) -> bool
const fn __bitnot_u64(_: u64) -> u64
}
// Extern intrinsics for u128
extern {
const fn __add_u128(_: u128, _: u128) -> u128
const fn __sub_u128(_: u128, _: u128) -> u128
const fn __mul_u128(_: u128, _: u128) -> u128
const fn __div_u128(_: u128, _: u128) -> u128
const fn __rem_u128(_: u128, _: u128) -> u128
const fn __pow_u128(_: u128, _: u128) -> u128
const fn __shl_u128(_: u128, _: u128) -> u128
const fn __shr_u128(_: u128, _: u128) -> u128
const fn __bitand_u128(_: u128, _: u128) -> u128
const fn __bitor_u128(_: u128, _: u128) -> u128
const fn __bitxor_u128(_: u128, _: u128) -> u128
const fn __eq_u128(_: u128, _: u128) -> bool
const fn __ne_u128(_: u128, _: u128) -> bool
const fn __lt_u128(_: u128, _: u128) -> bool
const fn __le_u128(_: u128, _: u128) -> bool
const fn __gt_u128(_: u128, _: u128) -> bool
const fn __ge_u128(_: u128, _: u128) -> bool
const fn __bitnot_u128(_: u128) -> u128
}
// Extern intrinsics for usize
extern {
const fn __add_usize(_: usize, _: usize) -> usize
const fn __sub_usize(_: usize, _: usize) -> usize
const fn __mul_usize(_: usize, _: usize) -> usize
const fn __div_usize(_: usize, _: usize) -> usize
const fn __rem_usize(_: usize, _: usize) -> usize
const fn __pow_usize(_: usize, _: usize) -> usize
const fn __shl_usize(_: usize, _: usize) -> usize
const fn __shr_usize(_: usize, _: usize) -> usize
const fn __bitand_usize(_: usize, _: usize) -> usize
const fn __bitor_usize(_: usize, _: usize) -> usize
const fn __bitxor_usize(_: usize, _: usize) -> usize
const fn __eq_usize(_: usize, _: usize) -> bool
const fn __ne_usize(_: usize, _: usize) -> bool
const fn __lt_usize(_: usize, _: usize) -> bool
const fn __le_usize(_: usize, _: usize) -> bool
const fn __gt_usize(_: usize, _: usize) -> bool
const fn __ge_usize(_: usize, _: usize) -> bool
const fn __bitnot_usize(_: usize) -> usize
}
// Extern intrinsics for i256
extern {
const fn __add_i256(_: i256, _: i256) -> i256
const fn __sub_i256(_: i256, _: i256) -> i256
const fn __mul_i256(_: i256, _: i256) -> i256
const fn __div_i256(_: i256, _: i256) -> i256
const fn __rem_i256(_: i256, _: i256) -> i256
const fn __pow_i256(_: i256, _: i256) -> i256
const fn __shl_i256(_: i256, _: i256) -> i256
const fn __shr_i256(_: i256, _: i256) -> i256
const fn __bitand_i256(_: i256, _: i256) -> i256
const fn __bitor_i256(_: i256, _: i256) -> i256
const fn __bitxor_i256(_: i256, _: i256) -> i256
const fn __eq_i256(_: i256, _: i256) -> bool
const fn __ne_i256(_: i256, _: i256) -> bool
const fn __lt_i256(_: i256, _: i256) -> bool
const fn __le_i256(_: i256, _: i256) -> bool
const fn __gt_i256(_: i256, _: i256) -> bool
const fn __ge_i256(_: i256, _: i256) -> bool
const fn __neg_i256(_: i256) -> i256
const fn __bitnot_i256(_: i256) -> i256
}
// Extern intrinsics for u256
extern {
const fn __add_u256(_: u256, _: u256) -> u256
const fn __sub_u256(_: u256, _: u256) -> u256
const fn __mul_u256(_: u256, _: u256) -> u256
const fn __div_u256(_: u256, _: u256) -> u256
const fn __rem_u256(_: u256, _: u256) -> u256
const fn __pow_u256(_: u256, _: u256) -> u256
const fn __shl_u256(_: u256, _: u256) -> u256
const fn __shr_u256(_: u256, _: u256) -> u256
const fn __bitand_u256(_: u256, _: u256) -> u256
const fn __bitor_u256(_: u256, _: u256) -> u256
const fn __bitxor_u256(_: u256, _: u256) -> u256
const fn __eq_u256(_: u256, _: u256) -> bool
const fn __ne_u256(_: u256, _: u256) -> bool
const fn __lt_u256(_: u256, _: u256) -> bool
const fn __le_u256(_: u256, _: u256) -> bool
const fn __gt_u256(_: u256, _: u256) -> bool
const fn __ge_u256(_: u256, _: u256) -> bool
const fn __bitnot_u256(_: u256) -> u256
}
// Extern intrinsics for isize
extern {
const fn __add_isize(_: isize, _: isize) -> isize
const fn __sub_isize(_: isize, _: isize) -> isize
const fn __mul_isize(_: isize, _: isize) -> isize
const fn __div_isize(_: isize, _: isize) -> isize
const fn __rem_isize(_: isize, _: isize) -> isize
const fn __pow_isize(_: isize, _: isize) -> isize
const fn __shl_isize(_: isize, _: isize) -> isize
const fn __shr_isize(_: isize, _: isize) -> isize
const fn __bitand_isize(_: isize, _: isize) -> isize
const fn __bitor_isize(_: isize, _: isize) -> isize
const fn __bitxor_isize(_: isize, _: isize) -> isize
const fn __eq_isize(_: isize, _: isize) -> bool
const fn __ne_isize(_: isize, _: isize) -> bool
const fn __lt_isize(_: isize, _: isize) -> bool
const fn __le_isize(_: isize, _: isize) -> bool
const fn __gt_isize(_: isize, _: isize) -> bool
const fn __ge_isize(_: isize, _: isize) -> bool
const fn __neg_isize(_: isize) -> isize
const fn __bitnot_isize(_: isize) -> isize
}
// Extern intrinsics for bool
extern {
const fn __not_bool(_: bool) -> bool
const fn __bitand_bool(_: bool, _: bool) -> bool
const fn __bitor_bool(_: bool, _: bool) -> bool
const fn __bitxor_bool(_: bool, _: bool) -> bool
const fn __eq_bool(_: bool, _: bool) -> bool
const fn __ne_bool(_: bool, _: bool) -> bool
}
// Masks used to truncate a 256-bit word down to the requested primitive width.
const U8_MASK: u256 = 0xff
const U16_MASK: u256 = 0xffff
const U32_MASK: u256 = 0xffffffff
const U64_MASK: u256 = 0xffffffffffffffff
const U128_MASK: u256 = 0xffffffffffffffffffffffffffffffff
const U256_MASK: u256 = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
// Sign bits used when performing two's-complement sign extension per width.
const I8_SIGN_BIT: u256 = 0x80
const I16_SIGN_BIT: u256 = 0x8000
const I32_SIGN_BIT: u256 = 0x80000000
const I64_SIGN_BIT: u256 = 0x8000000000000000
const I128_SIGN_BIT: u256 = 0x80000000000000000000000000000000
const I256_SIGN_BIT: u256 = 0x8000000000000000000000000000000000000000000000000000000000000000
pub trait IntDowncast<T> {
const fn downcast(self) -> Option<T>
const fn downcast_truncate(self) -> T
const fn downcast_saturate(self) -> T
const fn downcast_unchecked(self) -> T
}
// Inclusive lower and upper bounds of a numeric type, exposed as `const fn`
// so they resolve uniformly on concrete types (`u32::max()`, `i32::min()`).
pub trait Bounded {
const fn min() -> Self
const fn max() -> Self
}
impl Bounded for u8 {
const fn min() -> u8 { 0 }
const fn max() -> u8 { 255 }
}
impl Bounded for u16 {
const fn min() -> u16 { 0 }
const fn max() -> u16 { 65535 }
}
impl Bounded for u32 {
const fn min() -> u32 { 0 }
const fn max() -> u32 { 4294967295 }
}
impl Bounded for u64 {
const fn min() -> u64 { 0 }
const fn max() -> u64 { 18446744073709551615 }
}
impl Bounded for u128 {
const fn min() -> u128 { 0 }
const fn max() -> u128 { 340282366920938463463374607431768211455 }
}
impl Bounded for u256 {
const fn min() -> u256 { 0 }
const fn max() -> u256 { 115792089237316195423570985008687907853269984665640564039457584007913129639935 }
}
impl Bounded for usize {
const fn min() -> usize { 0 }
const fn max() -> usize { 115792089237316195423570985008687907853269984665640564039457584007913129639935 }
}
impl Bounded for i8 {
const fn min() -> i8 { -128 }
const fn max() -> i8 { 127 }
}
impl Bounded for i16 {
const fn min() -> i16 { -32768 }
const fn max() -> i16 { 32767 }
}
impl Bounded for i32 {
const fn min() -> i32 { -2147483648 }
const fn max() -> i32 { 2147483647 }
}
impl Bounded for i64 {
const fn min() -> i64 { -9223372036854775808 }
const fn max() -> i64 { 9223372036854775807 }
}
impl Bounded for i128 {
const fn min() -> i128 { -170141183460469231731687303715884105728 }
const fn max() -> i128 { 170141183460469231731687303715884105727 }
}
impl Bounded for i256 {
const fn min() -> i256 { -57896044618658097711785492504343953926634992332820282019728792003956564819968 }
const fn max() -> i256 { 57896044618658097711785492504343953926634992332820282019728792003956564819967 }
}
impl Bounded for isize {
const fn min() -> isize { -57896044618658097711785492504343953926634992332820282019728792003956564819968 }
const fn max() -> isize { 57896044618658097711785492504343953926634992332820282019728792003956564819967 }
}
// Magnitude of a signed integer, returned in the unsigned type of the same
// width. Unlike `-self`, this handles the `T::MIN` case without overflow.
pub trait UnsignedAbs {
type Output
const fn unsigned_abs(self) -> Self::Output
}
impl UnsignedAbs for i8 {
type Output = u8
const fn unsigned_abs(self) -> u8 {
let bits: u8 = self.downcast_unchecked()
if self < 0 { (0 as u8).wrapping_sub(bits) } else { bits }
}
}
impl UnsignedAbs for i16 {
type Output = u16
const fn unsigned_abs(self) -> u16 {
let bits: u16 = self.downcast_unchecked()
if self < 0 { (0 as u16).wrapping_sub(bits) } else { bits }
}
}
impl UnsignedAbs for i32 {
type Output = u32
const fn unsigned_abs(self) -> u32 {
let bits: u32 = self.downcast_unchecked()
if self < 0 { (0 as u32).wrapping_sub(bits) } else { bits }
}
}
impl UnsignedAbs for i64 {
type Output = u64
const fn unsigned_abs(self) -> u64 {
let bits: u64 = self.downcast_unchecked()
if self < 0 { (0 as u64).wrapping_sub(bits) } else { bits }
}
}
impl UnsignedAbs for i128 {
type Output = u128
const fn unsigned_abs(self) -> u128 {
let bits: u128 = self.downcast_unchecked()
if self < 0 { (0 as u128).wrapping_sub(bits) } else { bits }
}
}
impl UnsignedAbs for i256 {
type Output = u256
const fn unsigned_abs(self) -> u256 {
let bits: u256 = self.downcast_unchecked()
if self < 0 { (0 as u256).wrapping_sub(bits) } else { bits }
}
}
impl UnsignedAbs for isize {
type Output = usize
const fn unsigned_abs(self) -> usize {
let bits: usize = self.downcast_unchecked()
if self < 0 { (0 as usize).wrapping_sub(bits) } else { bits }
}
}
// Absolute value of a signed integer, in the same signed type. Bundles the
// usual checked / wrapping / saturating / overflowing variants alongside the
// default panicking `abs()`, matching how `IntDowncast` bundles its variants.
//
// `T::MIN` is the asymmetric edge: its magnitude doesn't fit in the signed
// type, so each variant takes a different stance on what to do:
// abs -> panic (uses Neg, which is `__checked_neg`)
// checked_abs -> None
// wrapping_abs -> T::MIN (the bit pattern of |T::MIN| in T is T::MIN)
// saturating_abs -> T::MAX
// overflowing_abs -> (T::MIN, true)
//
// For `T::MIN`-free magnitude in an unsigned type, use `UnsignedAbs`.
pub trait Abs {
const fn abs(self) -> Self
const fn checked_abs(self) -> Option<Self>
const fn wrapping_abs(self) -> Self
const fn saturating_abs(self) -> Self
const fn overflowing_abs(self) -> (Self, bool)
}
impl Abs for i8 {
const fn abs(self) -> i8 {
if self < 0 { -self } else { self }
}
const fn checked_abs(self) -> Option<i8> {
if self == i8::min() { Option::None } else if self < 0 { Option::Some(-self) } else { Option::Some(self) }
}
const fn wrapping_abs(self) -> i8 {
if self < 0 { (0 as i8).wrapping_sub(self) } else { self }
}
const fn saturating_abs(self) -> i8 {
if self == i8::min() { i8::max() } else if self < 0 { -self } else { self }
}
const fn overflowing_abs(self) -> (i8, bool) {
if self == i8::min() { (i8::min(), true) } else if self < 0 { (-self, false) } else { (self, false) }
}
}
impl Abs for i16 {
const fn abs(self) -> i16 {
if self < 0 { -self } else { self }
}
const fn checked_abs(self) -> Option<i16> {
if self == i16::min() { Option::None } else if self < 0 { Option::Some(-self) } else { Option::Some(self) }
}
const fn wrapping_abs(self) -> i16 {
if self < 0 { (0 as i16).wrapping_sub(self) } else { self }
}
const fn saturating_abs(self) -> i16 {
if self == i16::min() { i16::max() } else if self < 0 { -self } else { self }
}
const fn overflowing_abs(self) -> (i16, bool) {
if self == i16::min() { (i16::min(), true) } else if self < 0 { (-self, false) } else { (self, false) }
}
}
impl Abs for i32 {
const fn abs(self) -> i32 {
if self < 0 { -self } else { self }
}
const fn checked_abs(self) -> Option<i32> {
if self == i32::min() { Option::None } else if self < 0 { Option::Some(-self) } else { Option::Some(self) }
}
const fn wrapping_abs(self) -> i32 {
if self < 0 { (0 as i32).wrapping_sub(self) } else { self }
}
const fn saturating_abs(self) -> i32 {
if self == i32::min() { i32::max() } else if self < 0 { -self } else { self }
}
const fn overflowing_abs(self) -> (i32, bool) {
if self == i32::min() { (i32::min(), true) } else if self < 0 { (-self, false) } else { (self, false) }
}
}
impl Abs for i64 {
const fn abs(self) -> i64 {
if self < 0 { -self } else { self }
}
const fn checked_abs(self) -> Option<i64> {
if self == i64::min() { Option::None } else if self < 0 { Option::Some(-self) } else { Option::Some(self) }
}
const fn wrapping_abs(self) -> i64 {
if self < 0 { (0 as i64).wrapping_sub(self) } else { self }
}
const fn saturating_abs(self) -> i64 {
if self == i64::min() { i64::max() } else if self < 0 { -self } else { self }
}
const fn overflowing_abs(self) -> (i64, bool) {
if self == i64::min() { (i64::min(), true) } else if self < 0 { (-self, false) } else { (self, false) }
}
}
impl Abs for i128 {
const fn abs(self) -> i128 {
if self < 0 { -self } else { self }
}
const fn checked_abs(self) -> Option<i128> {
if self == i128::min() { Option::None } else if self < 0 { Option::Some(-self) } else { Option::Some(self) }
}
const fn wrapping_abs(self) -> i128 {
if self < 0 { (0 as i128).wrapping_sub(self) } else { self }
}
const fn saturating_abs(self) -> i128 {
if self == i128::min() { i128::max() } else if self < 0 { -self } else { self }
}
const fn overflowing_abs(self) -> (i128, bool) {
if self == i128::min() { (i128::min(), true) } else if self < 0 { (-self, false) } else { (self, false) }
}
}
impl Abs for i256 {
const fn abs(self) -> i256 {
if self < 0 { -self } else { self }
}
const fn checked_abs(self) -> Option<i256> {
if self == i256::min() { Option::None } else if self < 0 { Option::Some(-self) } else { Option::Some(self) }
}
const fn wrapping_abs(self) -> i256 {
if self < 0 { (0 as i256).wrapping_sub(self) } else { self }
}
const fn saturating_abs(self) -> i256 {
if self == i256::min() { i256::max() } else if self < 0 { -self } else { self }
}
const fn overflowing_abs(self) -> (i256, bool) {
if self == i256::min() { (i256::min(), true) } else if self < 0 { (-self, false) } else { (self, false) }
}
}
impl Abs for isize {
const fn abs(self) -> isize {
if self < 0 { -self } else { self }
}
const fn checked_abs(self) -> Option<isize> {
if self == isize::min() { Option::None } else if self < 0 { Option::Some(-self) } else { Option::Some(self) }
}
const fn wrapping_abs(self) -> isize {
if self < 0 { (0 as isize).wrapping_sub(self) } else { self }
}
const fn saturating_abs(self) -> isize {
if self == isize::min() { isize::max() } else if self < 0 { -self } else { self }
}
const fn overflowing_abs(self) -> (isize, bool) {
if self == isize::min() { (isize::min(), true) } else if self < 0 { (-self, false) } else { (self, false) }
}
}
pub trait IntWord: Copy {
const MASK: u256
const SIGN_BIT: u256
const IS_SIGNED: bool
const fn to_word(self) -> u256
const fn from_word(_: u256) -> Self
}
pub trait WordValue {
fn to_word(self) -> u256
}
impl<T> WordValue for T
where T: IntWord
{
fn to_word(self) -> u256 { IntWord::to_word(self) }
}
impl<const LEN: usize> WordValue for String<LEN> {
fn to_word(self) -> u256 {
let word: u256 = __bitcast(self)
let width_bits: u256 = (LEN * 8) as u256
let mask: u256 = (1 << width_bits) - 1
word & mask
}
}
impl IntWord for u8 {
const MASK: u256 = U8_MASK
const SIGN_BIT: u256 = 0
const IS_SIGNED: bool = false
const fn to_word(self) -> u256 { self as u256 }
const fn from_word(_ word: u256) -> Self { __bitcast(word) }
}
impl IntWord for u16 {
const MASK: u256 = U16_MASK
const SIGN_BIT: u256 = 0
const IS_SIGNED: bool = false
const fn to_word(self) -> u256 { self as u256 }
const fn from_word(_ word: u256) -> Self { __bitcast(word) }
}
impl IntWord for u32 {
const MASK: u256 = U32_MASK
const SIGN_BIT: u256 = 0
const IS_SIGNED: bool = false
const fn to_word(self) -> u256 { self as u256 }
const fn from_word(_ word: u256) -> Self { __bitcast(word) }
}
impl IntWord for u64 {
const MASK: u256 = U64_MASK
const SIGN_BIT: u256 = 0
const IS_SIGNED: bool = false
const fn to_word(self) -> u256 { self as u256 }
const fn from_word(_ word: u256) -> Self { __bitcast(word) }
}
impl IntWord for u128 {
const MASK: u256 = U128_MASK
const SIGN_BIT: u256 = 0
const IS_SIGNED: bool = false
const fn to_word(self) -> u256 { self as u256 }
const fn from_word(_ word: u256) -> Self { __bitcast(word) }
}
impl IntWord for u256 {
const MASK: u256 = U256_MASK
const SIGN_BIT: u256 = 0
const IS_SIGNED: bool = false
const fn to_word(self) -> u256 { self }
const fn from_word(_ word: u256) -> Self { word }
}
impl IntWord for usize {
const MASK: u256 = U256_MASK
const SIGN_BIT: u256 = 0
const IS_SIGNED: bool = false
const fn to_word(self) -> u256 { self as u256 }
const fn from_word(_ word: u256) -> Self { __bitcast(word) }
}
impl IntWord for i8 {
const MASK: u256 = U8_MASK
const SIGN_BIT: u256 = I8_SIGN_BIT
const IS_SIGNED: bool = true
const fn to_word(self) -> u256 { __bitcast(self as i256) }
const fn from_word(_ word: u256) -> Self { __bitcast(word) }
}
impl IntWord for i16 {
const MASK: u256 = U16_MASK
const SIGN_BIT: u256 = I16_SIGN_BIT
const IS_SIGNED: bool = true
const fn to_word(self) -> u256 { __bitcast(self as i256) }
const fn from_word(_ word: u256) -> Self { __bitcast(word) }
}
impl IntWord for i32 {
const MASK: u256 = U32_MASK
const SIGN_BIT: u256 = I32_SIGN_BIT
const IS_SIGNED: bool = true
const fn to_word(self) -> u256 { __bitcast(self as i256) }
const fn from_word(_ word: u256) -> Self { __bitcast(word) }
}
impl IntWord for i64 {
const MASK: u256 = U64_MASK
const SIGN_BIT: u256 = I64_SIGN_BIT
const IS_SIGNED: bool = true
const fn to_word(self) -> u256 { __bitcast(self as i256) }
const fn from_word(_ word: u256) -> Self { __bitcast(word) }
}
impl IntWord for i128 {
const MASK: u256 = U128_MASK
const SIGN_BIT: u256 = I128_SIGN_BIT
const IS_SIGNED: bool = true
const fn to_word(self) -> u256 { __bitcast(self as i256) }
const fn from_word(_ word: u256) -> Self { __bitcast(word) }
}
impl IntWord for i256 {
const MASK: u256 = U256_MASK
const SIGN_BIT: u256 = I256_SIGN_BIT
const IS_SIGNED: bool = true
const fn to_word(self) -> u256 { __bitcast(self) }
const fn from_word(_ word: u256) -> Self { __bitcast(word) }
}
impl IntWord for isize {
const MASK: u256 = U256_MASK
const SIGN_BIT: u256 = I256_SIGN_BIT
const IS_SIGNED: bool = true
const fn to_word(self) -> u256 { __bitcast(self as i256) }
const fn from_word(_ word: u256) -> Self { __bitcast(word) }
}
impl<S: IntWord, T: IntWord> IntDowncast<T> for S {
const fn downcast(self) -> Option<T> {
let word = IntWord::to_word(self)
if T::IS_SIGNED {
let t_min_word = sign_extend(word: T::SIGN_BIT, mask: T::MASK, sign_bit: T::SIGN_BIT)
let t_max_word = T::SIGN_BIT - 1
let t_min: i256 = __bitcast(t_min_word)
let t_max: i256 = __bitcast(t_max_word)
if !S::IS_SIGNED && (word & I256_SIGN_BIT != 0) {
Option::None
} else {
let x: i256 = __bitcast(word)
if x < t_min {
Option::None
} else if x > t_max {
Option::None
} else {
Option::Some(T::from_word(word))
}
}
} else {
if S::IS_SIGNED && (word & I256_SIGN_BIT != 0) {
Option::None
} else if word > T::MASK {
Option::None
} else {
Option::Some(T::from_word(word))
}
}
}
const fn downcast_truncate(self) -> T {
let word = IntWord::to_word(self)
if T::IS_SIGNED {
T::from_word(sign_extend(word: word, mask: T::MASK, sign_bit: T::SIGN_BIT))
} else {
T::from_word(word & T::MASK)
}
}
const fn downcast_saturate(self) -> T {
let word = IntWord::to_word(self)
if !T::IS_SIGNED {
let mut unsigned_word = word
if S::IS_SIGNED && (word & I256_SIGN_BIT != 0) {
unsigned_word = 0
}
if unsigned_word > T::MASK {
T::from_word(T::MASK)
} else {
T::from_word(unsigned_word)
}
} else {
let t_min_word = sign_extend(word: T::SIGN_BIT, mask: T::MASK, sign_bit: T::SIGN_BIT)
let t_max_word = T::SIGN_BIT - 1
let t_min: i256 = __bitcast(t_min_word)
let t_max: i256 = __bitcast(t_max_word)
if S::IS_SIGNED {
let x: i256 = __bitcast(word)
if x < t_min {
T::from_word(t_min_word)
} else if x > t_max {
T::from_word(t_max_word)
} else {
T::from_word(word)
}
} else {
if word & I256_SIGN_BIT != 0 {
T::from_word(t_max_word)
} else {
let x: i256 = __bitcast(word)
if x > t_max {
T::from_word(t_max_word)
} else {
T::from_word(word)
}
}
}
}
}
const fn downcast_unchecked(self) -> T {
let word = IntWord::to_word(self)
T::from_word(word)
}
}
/// Performs two's-complement sign extension within the provided mask/sign bit.
pub const fn sign_extend(word: u256, _ mask: u256, _ sign_bit: u256) -> u256 {
let value = word & mask
if value & sign_bit != 0 {
value | (U256_MASK ^ mask)
} else {
value
}
}
/// Returns floor(sqrt(x)), i.e. the largest `z` such that `z * z <= x`.
#[arithmetic(unchecked)]
pub fn isqrt(_ x: u256) -> u256 {
if x == 0 {
return 0
}
// Seed with `1 << (log2(x) / 2)`, a power of two within a factor of two
// of the true root, so a fixed number of Newton iterations suffices.
let mut y = x
let mut z: u256 = 1
if y >= 1 << 128 {
y >>= 128
z <<= 64
}
if y >= 1 << 64 {
y >>= 64
z <<= 32
}
if y >= 1 << 32 {
y >>= 32
z <<= 16
}
if y >= 1 << 16 {
y >>= 16
z <<= 8
}
if y >= 1 << 8 {
y >>= 8
z <<= 4
}
if y >= 1 << 4 {
y >>= 4
z <<= 2
}
if y >= 1 << 2 {
z <<= 1
}
// Newton's method doubles the number of correct bits per step, so seven
// iterations refine the seed past the 128 bits a root can occupy.
z = (z + x / z) >> 1
z = (z + x / z) >> 1
z = (z + x / z) >> 1
z = (z + x / z) >> 1
z = (z + x / z) >> 1
z = (z + x / z) >> 1
z = (z + x / z) >> 1
// Newton converges from above and can settle on `floor + 1`; subtract
// one exactly when `z` overshoots (`z > x / z`) to round down to the floor.
let q = x / z
z - ((z > q) as u256)
}
const fn pow_checked<T>(_ base: own T, _ exp: own T) -> T
where T: IntWord, T: Mul<Output = T>
{
__checked_pow(base, exp)
}
// u8 impls
impl BitNot for u8 { const fn bit_not(own self) -> u8 { __bitnot_u8(self) } }
impl Not for u8 { const fn not(own self) -> u8 { __bitnot_u8(self) } }
impl Add for u8 {
const fn add(own self, _ other: own u8) -> u8 {
__checked_add(self, other)
}
}
impl Sub for u8 {
const fn sub(own self, _ other: own u8) -> u8 {
__checked_sub(self, other)
}
}
impl Mul for u8 {
const fn mul(own self, _ other: own u8) -> u8 {
__checked_mul(self, other)
}
}