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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
// Copyright 2023, 2024 PARK Youngho.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
// This file may not be copied, modified, or distributed
// except according to those terms.
// #![warn(missing_docs)]
// #![warn(rustdoc::missing_doc_code_examples)]
// #![allow(missing_docs)]
// #![allow(rustdoc::missing_doc_code_examples)]
use std::mem::{ size_of, size_of_val };
use crate::number::{ SmallUInt, ShortUnion, IntUnion, LongUnion, LongerUnion, SizeUnion };
macro_rules! SmallUInt_methods_for_uint_impl
{
(u128) => {
impl SmallUInt for u128
{
const BITS: u32 = u128::BITS;
const MIN: Self = u128::MIN;
const MAX: Self = u128::MAX;
const ONE: Self = 1_u128;
SmallUInt_methods_for_uint_impl_!(u128);
}
};
(u64) => {
impl SmallUInt for u64
{
const BITS: u32 = u64::BITS;
const MIN: Self = u64::MIN;
const MAX: Self = u64::MAX;
const ONE: Self = 1_u64;
SmallUInt_methods_for_uint_impl_!(u64);
}
};
(u32) => {
impl SmallUInt for u32
{
const BITS: u32 = u32::BITS;
const MIN: Self = u32::MIN;
const MAX: Self = u32::MAX;
const ONE: Self = 1_u32;
SmallUInt_methods_for_uint_impl_!(u32);
}
};
(u16) => {
impl SmallUInt for u16
{
const BITS: u32 = u16::BITS;
const MIN: Self = u16::MIN;
const MAX: Self = u16::MAX;
const ONE: Self = 1_u16;
SmallUInt_methods_for_uint_impl_!(u16);
}
};
(u8) => {
impl SmallUInt for u8
{
const BITS: u32 = u8::BITS;
const MIN: Self = u8::MIN;
const MAX: Self = u8::MAX;
const ONE: Self = 1_u8;
SmallUInt_methods_for_uint_impl_!(u8);
}
};
(usize) => {
impl SmallUInt for usize
{
const BITS: u32 = usize::BITS;
const MIN: Self = usize::MIN;
const MAX: Self = usize::MAX;
const ONE: Self = 1_usize;
SmallUInt_methods_for_uint_impl_!(usize);
}
};
}
macro_rules! SmallUInt_methods_for_uint_impl_ {
($f:ty) => {
// impl SmallUInt for $f
// {
/// Calculates `self` + `rhs` + `carry` and returns a tuple
/// containing the sum and the output carry.
/// [Read more](trait@SmallUInt#tymethod.carrying_add) in detail.
fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool)
{
let (r1, c1) = self.overflowing_add(rhs);
let (r2, c2) = r1.overflowing_add(carry as Self);
(r2, c1 || c2)
}
/// Computes `self` + `rhs`, wrapping around at the boundary of
/// the type.
/// [Read more](trait@SmallUInt#tymethod.wrapping_add) in detail.
#[inline] fn wrapping_add(self, rhs: Self) -> Self { self.wrapping_add(rhs) }
/// Calculates `self` + `rhs` and returns a tuple of the addition
/// along with a boolean indicating whether an arithmetic overflow
/// would occur.
/// [Read more](trait@SmallUInt#tymethod.overflowing_add) in detail.
#[inline] fn overflowing_add(self, rhs: Self) -> (Self, bool) { self.overflowing_add(rhs) }
/// Computes `self` + `rhs` and returns None if overflow occurred.
/// [Read more](trait@SmallUInt#tymethod.checked_add) in detail.
#[inline] fn checked_add(self, rhs: Self) -> Option<Self> { self.checked_add(rhs) }
/// Computes `self` + `rhs`, assuming overflow cannot occur.
/// [Read more](trait@SmallUInt#tymethod.checked_add) in detail.
#[inline] fn unchecked_add(self, rhs: Self) -> Self { self.checked_add(rhs).unwrap() }
/// Computes `self` + `rhs`, saturating at the numeric bounds
/// instead of overflowing.
/// [Read more](trait@SmallUInt#tymethod.saturating_add) in detail.
#[inline] fn saturating_add(self, rhs: Self) -> Self { self.saturating_add(rhs) }
/// Computes `self` + `rhs`, wrapping at the numeric bounds instead
/// of overflowing in release mode,
/// but panics when overflow occurs in debug mode.
/// [Read more](trait@SmallUInt#tymethod.saturating_add) in detail.
#[inline] fn safe_add(self, rhs: Self) -> Self
{
#[cfg(debug_assertions)] return self + rhs;
#[cfg(not(debug_assertions))] return self.wrapping_add(rhs);
}
/// Computes `self` + `rhs`, wrapping at `modulus`
/// instead of overflowing.
/// [Read more](trait@SmallUInt#tymethod.modular_add) in detail.
fn modular_add(self, rhs: Self, modulus: Self) -> Self
{
let mlhs = self.wrapping_rem(modulus);
let mrhs = rhs.wrapping_rem(modulus);
let diff = modulus.wrapping_sub(mrhs);
if self >= diff
{ mlhs.wrapping_sub(diff) }
else
{ mlhs.wrapping_add(mrhs) }
}
/// Calculates `self` - `rhs` - `borrow`,
/// wrapping around at the boundary of the type.
/// [Read more](trait@SmallUInt#tymethod.borrowing_sub) in detail.
fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool)
{
let (r1, b1) = self.overflowing_sub(rhs);
let (r2, b2) = r1.overflowing_sub(borrow as Self);
(r2, b1 || b2)
}
/// Computes `self` - `rhs`, wrapping around at the boundary of
/// the type. [Read more](trait@SmallUInt#tymethod.wrapping_sub)
/// in detail.
#[inline] fn wrapping_sub(self, rhs: Self) -> Self { self.wrapping_sub(rhs) }
/// Calculates `self` - `rhs` and returns a tuple of the subtraction
/// along with a boolean indicating whether an arithmetic overflow
/// would occur.
/// [Read more](trait@SmallUInt#tymethod.overflowing_sub) in detail.
#[inline] fn overflowing_sub(self, rhs: Self) -> (Self, bool) { self.overflowing_sub(rhs) }
/// Computes `self` - `rhs`, returning None if overflow occurred.
/// [Read more](trait@SmallUInt#tymethod.checked_sub) in detail.
#[inline] fn checked_sub(self, rhs: Self) -> Option<Self> { self.checked_sub(rhs) }
/// Computes `self` - `rhs`, assuming overflow cannot occur.
/// [Read more](trait@SmallUInt#tymethod.unchecked_sub) in detail.
#[inline] fn unchecked_sub(self, rhs: Self) -> Self { self.checked_sub(rhs).unwrap() }
/// Computes `self` - `rhs`, saturating at the numeric bounds
/// instead of overflowing.
/// [Read more](trait@SmallUInt#tymethod.saturating_sub) in detail.
#[inline] fn saturating_sub(self, rhs: Self) -> Self { self.saturating_sub(rhs) }
/// Computes `self` - `rhs`, wrapping at the numeric bounds instead
/// of underflowing in release mode,
/// but panics when underflow occurs in debug mode.
#[inline] fn safe_sub(self, rhs: Self) -> Self
{
#[cfg(debug_assertions)] return self - rhs;
#[cfg(not(debug_assertions))] return self.wrapping_sub(rhs);
}
/// Computes the absolute difference between self and other.
/// [Read more](trait@SmallUInt#tymethod.abs_diff) in detail.
#[inline] fn abs_diff(self, other: Self) -> Self { self.abs_diff(other) }
/// Computes `self` - `rhs`, wrapping at `modulus`
/// instead of overflowing.
/// [Read more](trait@SmallUInt#tymethod.modular_sub) in detail.
fn modular_sub(self, rhs: Self, modulus: Self) -> Self
{
let mlhs = self.wrapping_rem(modulus);
let mrhs = rhs.wrapping_rem(modulus);
if mlhs >= mrhs
{
mlhs.wrapping_sub(mrhs)
}
else
{
let diff = modulus.wrapping_sub(mrhs);
mlhs.wrapping_add(diff)
}
}
/// Calculates the “full multiplication” `self` * `rhs` + `carry` without
/// the possibility to overflow.
/// [Read more](trait@SmallUInt#tymethod.carrying_mul) in detail.
#[inline] fn carrying_mul(self, rhs: Self, carry: Self) -> (Self, Self)
{
// _carrying_mul(self, rhs, carry)
let overflow;
let (mut low, mut high) = SmallUInt::widening_mul(self, rhs);
(low, overflow) = low.overflowing_add(carry);
if overflow
{ high = high.wrapping_add(1); }
(low, high)
}
// // fn carrying_mul_for_internal_use(self, rhs: Self, carry: Self) -> (Self, Self);
// /// It is for internal use. You are recommended to use
// /// [carrying_mul()](trait@SmallUInt#tymethod.carrying_mul) instead.
// fn _carrying_mul(self, rhs: Self, carry: Self) -> (Self, Self)
// {
// let overflow;
// let (mut low, mut high) = self._widening_mul(rhs);
// (low, overflow) = low.overflowing_add(carry);
// if overflow
// { high = high.wrapping_add(1); }
// (low, high)
// }
/// Calculates the complete product `self` * `rhs` without the
/// possibility to overflow.
/// [Read more](trait@SmallUInt#tymethod.widening_mul) in detail.
#[inline] fn widening_mul(self, rhs: Self) -> (Self, Self)
{
// _widening_mul(self, rhs)
let zero = SmallUInt::zero();
if rhs.is_zero() || self.is_zero()
{ return (zero, zero); }
let mut low = zero;
let mut high = zero;
let mut overflow: bool;
let mut bit_check = Self::one() << (Self::size_in_bits() - 1 - rhs.leading_zeros());
let adder = self;
while bit_check != 0
{
high <<= 1;
if low.is_msb_set()
{ high.set_lsb(); }
low <<= 1;
if bit_check & rhs != 0
{
(low, overflow) = low.overflowing_add(adder);
if overflow
{ high = high.wrapping_add(Self::one()); }
}
bit_check >>= 1;
}
(low, high)
}
// // fn carrying_mul_for_internal_use(self, rhs: Self, carry: Self) -> (Self, Self);
// /// It is for internal use. You are recommended to use
// /// [carrying_mul()](trait@SmallUInt#tymethod.widening_mul) instead.
// fn _widening_mul(self, rhs: Self) -> (Self, Self)
// {
// if (rhs == 0) || (self == 0)
// { return (0, 0); }
// let mut low: Self = 0;
// let mut high: Self = 0;
// let mut overflow: bool;
// let mut bit_check: Self = 1 << (Self::size_in_bits() - 1 - rhs.leading_zeros() as usize);
// let adder = self;
// while bit_check != 0
// {
// high <<= 1;
// if low.is_msb_set()
// { high.set_lsb(); }
// low <<= 1;
// if bit_check & rhs != 0
// {
// (low, overflow) = low.overflowing_add(adder);
// if overflow
// { high = high.wrapping_add(1); }
// }
// bit_check >>= 1;
// }
// (low, high)
// }
/// Computes `self` * `rhs`, wrapping around at the boundary of
/// the type. [Read more](trait@SmallUInt#tymethod.wrapping_mul)
/// in detail.
#[inline] fn wrapping_mul(self, rhs: Self) -> Self { self.wrapping_mul(rhs) }
/// Calculates the multiplication of `self` and `rhs` and returns
/// a tuple of the multiplication along with a boolean indicating
/// whether an arithmetic overflow would occur.
/// [Read more](trait@SmallUInt#tymethod.overflowing_mul) in detail.
#[inline] fn overflowing_mul(self, rhs: Self) -> (Self, bool) { self.overflowing_mul(rhs) }
/// Computes `self` * `rhs`, returning None if overflow occurred.
/// [Read more](trait@SmallUInt#tymethod.checked_mul) in detail.
#[inline] fn checked_mul(self, rhs: Self) -> Option<Self> { self.checked_mul(rhs) }
/// Computes `self` * `rhs`, assuming overflow cannot occur.
/// [Read more](trait@SmallUInt#tymethod.unchecked_mul) in detail.
#[inline] fn unchecked_mul(self, rhs: Self) -> Self { self.checked_mul(rhs).unwrap() }
/// Computes `self` * `rhs`, saturating at the numeric bounds
/// instead of overflowing.
/// [Read more](trait@SmallUInt#tymethod.saturating_mul) in detail.
#[inline] fn saturating_mul(self, rhs: Self) -> Self { self.saturating_mul(rhs) }
/// Computes `self` * `rhs`, wrapping at the numeric bounds instead
/// of overflowing in release mode,
/// but panics when overflowing occurs in debug mode.
#[inline] fn safe_mul(self, rhs: Self) -> Self
{
#[cfg(debug_assertions)] return self * rhs;
#[cfg(not(debug_assertions))] return self.wrapping_mul(rhs);
}
/// Computes `self` * `rhs`, wrapping at `modulus`
/// instead of overflowing.
/// [Read more](trait@SmallUInt#tymethod.modular_mul) in detail.
fn modular_mul(self, rhs: Self, modulus: Self) -> Self
{
let mut mlhs = self.wrapping_rem(modulus);
let mut mrhs = rhs.wrapping_rem(modulus);
let mut res = Self::zero();
while mrhs > 0
{
if mrhs.is_odd()
{ res = res.modular_add(mlhs, modulus); }
mlhs = mlhs.modular_add(mlhs, modulus);
mrhs >>= 1;
}
res
}
/// Computes `self` / `rhs`. Wrapped division on unsigned types is
/// just normal division.
/// [Read more](trait@SmallUInt#tymethod.wrapping_div) in detail.
#[inline] fn wrapping_div(self, rhs: Self) -> Self { self.wrapping_div(rhs) }
/// Calculates the divisor when `self` is divided by `rhs` and
/// returns a tuple of the divisor along with a boolean indicating
/// whether an arithmetic overflow would occur.
/// [Read more](trait@SmallUInt#tymethod.overflowing_div) in detail.
#[inline] fn overflowing_div(self, rhs: Self) -> (Self, bool) { self.overflowing_div(rhs) }
/// Computes `self` / `rhs`, returning `None` if `rhs` == 0.
/// [Read more](trait@SmallUInt#tymethod.checked_div) in detail.
#[inline] fn checked_div(self, rhs: Self) -> Option<Self> { self.checked_div(rhs) }
/// Computes `self` / `rhs`, assuming `rhs` cannot be `0`.
/// [Read more](trait@SmallUInt#tymethod.checked_div) in detail.
#[inline] fn unchecked_div(self, rhs: Self) -> Self { self.checked_div(rhs).unwrap() }
/// Computes `self` / `rhs`, saturating at the numeric bounds
/// instead of overflowing.
/// [Read more](trait@SmallUInt#tymethod.saturating_div) in detail.
#[inline] fn saturating_div(self, rhs: Self) -> Self { self.saturating_div(rhs) }
/// Computes `self` / `rhs`.
#[inline] fn safe_div(self, rhs: Self) -> Self
{
#[cfg(debug_assertions)] return self / rhs;
#[cfg(not(debug_assertions))] return self.wrapping_div(rhs);
}
/// Computes `self` % `rhs`. Wrapped remainder calculation on unsigned
/// types is just the regular remainder calculation.
/// [Read more](trait@SmallUInt#tymethod.wrapping_rem) in detail.
#[inline] fn wrapping_rem(self, rhs: Self) -> Self { self.wrapping_rem(rhs) }
/// Calculates the remainder when `self` is divided by `rhs`, and returns
/// a tuple of the remainder after dividing along with a boolean
/// indicating whether an arithmetic overflow would occur.
/// [Read more](trait@SmallUInt#tymethod.overflowing_rem) in detail.
#[inline] fn overflowing_rem(self, rhs: Self) -> (Self, bool) { self.overflowing_rem(rhs) }
/// Computes `self` % `rhs`, returning None if `rhs` == 0.
/// [Read more](trait@SmallUInt#tymethod.checked_rem) in detail.
#[inline] fn checked_rem(self, rhs: Self) -> Option<Self> { self.checked_rem(rhs) }
/// Computes `self` % `rhs`, assuming `rhs` cannot be `0`.
/// [Read more](trait@SmallUInt#tymethod.checked_rem) in detail.
#[inline] fn unchecked_rem(self, rhs: Self) -> Self { self.checked_rem(rhs).unwrap() }
/// Computes `self` % `rhs`.
#[inline] fn safe_rem(self, rhs: Self) -> Self
{
#[cfg(debug_assertions)] return self % rhs;
#[cfg(not(debug_assertions))] return self.wrapping_rem(rhs);
}
/// Computes `-self`, wrapping around at the boundary of the type.
/// [Read more](trait@SmallUInt#tymethod.wrapping_neg) in detail.
#[inline] fn wrapping_neg(self) -> Self { self.wrapping_neg() }
/// Negates `self` in an overflowing fashion.
/// [Read more](trait@SmallUInt#tymethod.overflowing_neg) in detail.
#[inline] fn overflowing_neg(self) -> (Self, bool) { self.overflowing_neg() }
/// Computes `self.pow(exp)`, wrapping around at the boundary of
/// the type. [Read more](trait@SmallUInt#tymethod.wrapping_pow)
/// in detail.
#[inline] fn wrapping_pow(self, exp: u32) -> Self { self.wrapping_pow(exp) }
/// Raises `self` to the power of `exp`, using exponentiation by
/// squaring. [Read more](trait@SmallUInt#tymethod.overflowing_pow)
/// in detail.
#[inline] fn overflowing_pow(self, exp: u32) -> (Self, bool) { self.overflowing_pow(exp) }
/// Computes `self.pow(exp)`, returning None if overflow occurred.
/// [Read more](trait@SmallUInt#tymethod.checked_pow) in detail.
#[inline] fn checked_pow(self, exp: u32) -> Option<Self> { self.checked_pow(exp) }
/// Computes `self.pow(exp)`, unless overflow does not occcurred.
/// Otherwise, it will panic.
/// [Read more](trait@SmallUInt#tymethod.unchecked_pow) in detail.
#[inline] fn unchecked_pow(self, exp: u32) -> Self { self.checked_pow(exp).unwrap() }
/// Computes `self`.pow(exp), saturating at the numeric bounds
/// instead of overflowing.
/// [Read more](trait@SmallUInt#tymethod.saturating_pow) in detail.
#[inline] fn saturating_pow(self, exp: u32) -> Self { self.saturating_pow(exp) }
/// Raises `self` to the power of `exp`, using exponentiation
/// by squaring. [Read more](trait@SmallUInt#tymethod.pow)
/// in detail.
#[inline] fn pow(self, exp: u32) -> Self { self.pow(exp) }
/// Computes `self.pow(exp)`, saturating at `modulus`
/// instead of overflowing.
/// [Read more](trait@SmallUInt#tymethod.modular_pow) in detail.
fn modular_pow(self, exp: Self, modulus: Self) -> Self
{
let mut mlhs = self.wrapping_rem(modulus);
let mut res = Self::one();
let mut mexp = exp;
while mexp > 0
{
if mexp.is_odd()
{ res = res.modular_mul(mlhs, modulus); }
mlhs = mlhs.modular_mul(mlhs, modulus);
mexp >>= 1;
}
res
}
/// Returns the logarithm of the number with respect to an
/// arbitrary base. [Read more](trait@SmallUInt#tymethod.ilog)
/// in detail.
#[inline] fn ilog(self, base: Self) -> u32 { self.ilog(base) }
/// Returns the base 10 logarithm of the number.
/// [Read more](trait@SmallUInt#tymethod.ilog10) in detail.
#[inline] fn ilog10(self) -> u32 { self.ilog10() }
/// Returns the base 2 logarithm of the number, rounded down.
/// [Read more](trait@SmallUInt#tymethod.ilog2) in detail.
#[inline] fn ilog2(self) -> u32 { self.ilog2() }
/// Returns the square root of the number.
/// [Read more](trait@SmallUInt#tymethod.isqrt) in detail.
#[inline] fn isqrt(self) -> Self
{
// _isqrt(self)
let mut adder;
let mut highest = (Self::size_in_bits() - self.leading_zeros()) >> 1;
let mut high;
let mut low;
let mut mid;
let mut res = Self::zero();
let mut sum;
let maximum = highest - 1;
loop
{
high = highest;
low = 0;
if high == 0
{
return res;
}
else // if high > 0
{
loop
{
mid = (high + low) >> 1;
adder = Self::generate_check_bits_(mid);
sum = res + adder;
let (sq, b_overflow) = sum.overflowing_mul(sum);
if !b_overflow && (sq < self)
{
if mid == maximum
{
res = sum;
break;
}
else if mid == low
{
res = sum;
if mid == 0
{ highest = 0; }
break;
}
low = mid;
}
else if b_overflow || (sq > self)
{
if mid == low
{
highest = mid;
break;
}
high = mid;
}
else // if sq == self
{
return sum;
}
}
}
}
}
// /// Returns the square root of the number.
// /// [Read more](trait@SmallUInt#tymethod.isqrt) in detail.
// fn _isqrt(self) -> Self
// {
// let mut adder;
// let mut highest = (Self::size_in_bits() - self.leading_zeros() as usize) >> 1;
// let mut high;
// let mut low;
// let mut mid;
// let mut res = Self::zero();
// let mut sum;
// let maximum = highest - 1;
// loop
// {
// high = highest;
// low = 0;
// if high == 0
// {
// return res;
// }
// else // if high > 0
// {
// loop
// {
// mid = (high + low) >> 1;
// adder = Self::generate_check_bits_(mid);
// sum = res + adder;
// let (sq, b_overflow) = sum.overflowing_mul(sum);
// if !b_overflow && (sq < self)
// {
// if mid == maximum
// {
// res = sum;
// break;
// }
// else if mid == low
// {
// res = sum;
// if mid == 0
// { highest = 0; }
// break;
// }
// low = mid;
// }
// else if b_overflow || (sq > self)
// {
// if mid == low
// {
// highest = mid;
// break;
// }
// high = mid;
// }
// else // if sq == self
// {
// return sum;
// }
// }
// }
// }
// }
/// Returns the `exp`-th root of the number.
/// [Read more](trait@SmallUInt#tymethod.iroot) in detail.
fn iroot(self, exp: Self) -> Self
{
let mut adder;
let mut highest = (Self::size_in_bits() - self.leading_zeros()) / (exp as u32);
let mut high;
let mut low;
let mut mid;
let mut res = Self::zero();
let mut sum;
let maximum = highest - 1;
loop
{
high = highest;
low = 0;
if high == 0
{
return res;
}
else // if high > 0
{
loop
{
mid = (high + low) >> 1;
adder = Self::generate_check_bits_(mid);
sum = res + adder;
let (sq, b_overflow) = sum.overflowing_pow(exp as u32);
if !b_overflow && (sq < self)
{
if mid == maximum
{
res = sum;
break;
}
else if mid == low
{
res = sum;
if mid == 0
{ highest = 0; }
break;
}
low = mid;
}
else if b_overflow || (sq > self)
{
if mid == low
{
highest = mid;
break;
}
high = mid;
}
else // if sq == self
{
return sum;
}
}
}
}
}
// fn filter_out_composite_number(&self) -> bool;
/// Filter out composite numbers.
/// If self is filtered out if it
///
/// # Output
/// `true` if `self` is is a composite number.
/// Otherwise, it returns `false`.
fn filter_out_composite_number(&self) -> bool
{
use crate::number::A_LIST;
if self.is_zero_or_one() || self.is_even()
{ return true; }
let len = A_LIST.len();
for i in 1..len
{
if self.into_u128().wrapping_rem(A_LIST[i].into_u128()).is_zero()
{ return true; }
}
false
}
/// Tests a `SmallUInt`-type object to find whether or not `self`
/// is a prime number.
/// [Read more](trait@SmallUInt#tymethod.test_miller_rabin)
/// in detail.
fn test_miller_rabin(self, a: Self) -> bool
{
let self_minus_one = self.wrapping_sub(Self::one());
let mut d = self_minus_one;
let mut s = 0_u64;
while d.is_even()
{
d >>= 1;
s += 1;
}
let mut x = a.modular_pow(d, self);
if x == self_minus_one || x.is_one()
{ return true; }
for _ in 0..s-1
{
x = x.modular_pow(Self::u8_as_smalluint(2), self);
if x == self_minus_one
{ return true; }
}
false
}
/// Tests a `SmallUInt`-type object to find whether or not it is a
/// primne number.
/// [Read more](trait@SmallUInt#tymethod.is_prime_using_miller_rabin)
/// in detail.
/// [Read more](https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test?utm_source=chatgpt.com#Deterministic_variants)
/// to learn deterministic decision with Millar-Rabin algorithm.
fn is_prime_using_miller_rabin(self, repetition: usize) -> bool
{
use crate::number::A_LIST;
if self.is_zero_or_one() || self.is_even()
{ return true; }
let a_list;
// if self.into_u128() <= u8::MAX as u128 // > u8::MAX, repetition is meaningless.
// { a_list = vec![2_u16]; } else
if self.into_u128() <= 2047_u128 // > u8::MAX, repetition is meaningless.
{ a_list = vec![2_u16]; }
// else if self.into_u128() <= u16::MAX as u128
// { a_list = vec![2_u16, 3]; }
else if self.into_u128() <= 137_3653_u128 // > u16::MAX
{ a_list = vec![2_u16, 3]; }
else if self.into_u128() <= 908_0191_u128 // < u32::MAX
{ a_list = vec![31_u16, 73]; }
// else if self.into_u128() <= u16::MAX as u128
// { a_list = vec![2_u16, 7, 61]; }
else if self.into_u128() <= 2532_6001_u128 // > u32::MAX == 47_5912_3141
{ a_list = vec![2_u16, 7, 61]; }
// else if self.into_u128() <= 1_1220_0466_9633_u128 // < u64::MAX
// { a_list = vec![2_u16, 13, 23, 1662803]; }
else if self.into_u128() <= 2_1523_0289_8747_u128 // < u64::MAX
{ a_list = vec![2_u16, 3, 5, 7, 11]; }
else if self.into_u128() <= 3_4747_4966_0383_u128 // < u64::MAX
{ a_list = vec![2_u16, 3, 5, 7, 11, 13]; }
else if self.into_u128() <= 341_5500_7172_8321_u128 // < u64::MAX
{ a_list = vec![2_u16, 3, 5, 7, 11, 13, 17]; }
else if self.into_u128() <= 382_5123_0565_4641_3051_u128 // < u64::MAX
{ a_list = vec![2_u16, 3, 5, 7, 11, 13, 17, 19, 23]; }
// else if self.into_u128() <= u64::MAX as u128
// { a_list = vec![2_u16, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]; }
else
{
if self.into_u128() <= 3186_6585_7834_0311_5116_7461_u128 // < u128::MAX
{ a_list = vec![2_u16, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]; }
else if self.into_u128() <= 3_3170_4406_4679_8873_8596_1981_u128 // < u128::MAX
{ a_list = vec![2_u16, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]; }
else // if self <= u128::MAX as Self
{
if self.filter_out_composite_number()
{ return false; }
a_list = A_LIST.to_vec();
}
}
let len = a_list.len();
let common = if len < repetition { len } else { repetition };
let mut i = 0;
while i < common
{
if !self.test_miller_rabin(a_list[i] as Self)
{ return false; }
i += 1;
}
let mut a = a_list[len-1] as u32 + 2;
for _ in i..repetition
{
if !self.test_miller_rabin(a as Self)
{ return false; }
a += 2;
}
true
}
/// Tests a `SmallUInt`-type object to find whether or not it is a
/// primne number. If the `self` is less than or equal to than
/// 3_3170_4406_4679_8873_8596_1981_u128, it is deterministic.
/// Otherwise, it is probabilistic.
/// [Read more](trait@SmallUInt#tymethod.is_prime_using_miller_rabin)
/// in detail.
#[inline]
fn is_prime(self) -> bool
{
self.is_prime_using_miller_rabin(256)
}
/// Calculates the greatest common divisor of `self` and `other`,
/// and returns the result.
/// [Read more](trait@SmallUInt#tymethod.gcd_assign)
/// in detail.
fn gcd(&self, other: Self) -> Self
{
if self.is_zero() || other.is_zero()
{ panic!(); }
let mut x = *self;
let mut y = other;
let mut t: Self;
while !y.is_zero()
{
t = y;
y = x.wrapping_rem(t);
x = t;
}
x
}
/// Calculates the greatest common divisor of `self` and `other`,
/// and assigns the result back to `self`.
/// [Read more](trait@SmallUInt#tymethod.gcd_assign)
/// in detail.
#[inline] fn gcd_assign(&mut self, other: Self)
{
*self = self.gcd(other);
}
// fn extended_gcd(&self, other: &Self) -> (Self, Self, Self);
/// Calculates the greatest common divisor of `self` and `other`,
/// [Read more](trait@SmallUInt#tymethod.extended_gcd)
/// in detail.
fn extended_gcd(&self, other: Self) -> (Self, Self, Self)
{
if self.is_zero() || other.is_zero()
{ panic!(); }
let mut a = *self;
let mut b = other;
let mut x0 = Self::one();
let mut y0 = Self::zero();
let mut x1 = Self::zero();
let mut y1 = Self::one();
let mut t: Self;
let mut q: Self;
while !b.is_zero()
{
q = a.wrapping_div(b);
t = x1;
x1 = x0.wrapping_sub(q.wrapping_mul(x1));
x0 = t;
t = y1;
y1 = y0.wrapping_sub(q.wrapping_mul(y1));
y0 = t;
t = b;
b = a.wrapping_rem(t);
a = t;
}
(a, x0, y0)
}
/// Calculates the least common multiple of `self` and `other`,
/// and returns the result.
/// [Read more](trait@SmallUInt#tymethod.lcm)
/// in detail.
fn lcm(&self, other: Self) -> Self
{
if self.is_zero() || other.is_zero()
{ panic!(); }
self.wrapping_div(self.gcd(other)).wrapping_mul(other)
}
/// Calculates the greatest common divisor of `self` and `other`,
/// and assigns the result back to `self`.
/// [Read more](trait@SmallUInt#tymethod.lcm_assign)
/// in detail.
fn lcm_assign(&mut self, other: Self)
{
*self = self.lcm(other);
}
/// Reverses the order of bits in the integer.
/// [Read more](trait@SmallUInt#tymethod.reverse_bits) in detail.
#[inline] fn reverse_bits(self) -> Self { self.reverse_bits() }
// #[inline] fn reverse_bits_assign(&mut self) { *self = self.reverse_bits(); }
/// Shifts the bits to the left by a specified amount, `n`,
/// wrapping the truncated bits to the end of the resulting integer.
/// [Read more](trait@SmallUInt#tymethod.rotate_left) in detail.
#[inline] fn rotate_left(self, n: u32) -> Self { self.rotate_left(n) }
/// Shifts the bits to the right by a specified amount, `n`,
/// wrapping the truncated bits to the end of the resulting integer.
/// [Read more](trait@SmallUInt#tymethod.rotate_right) in detail.
#[inline] fn rotate_right(self, n: u32) -> Self { self.rotate_right(n) }
/// Returns the number of ones in the binary representation of
/// `self`. [Read more](trait@SmallUInt#tymethod.count_ones)
/// in detail.
#[inline] fn count_ones(self) -> u32 { self.count_ones() }
/// Returns the number of zeros in the binary representation of
/// `self`. [Read more](trait@SmallUInt#tymethod.count_zeros)
/// in detail.
#[inline] fn count_zeros(self) -> u32 { self.count_zeros() }
/// Returns the number of leading ones
/// in the binary representation of `self`.
/// [Read more](trait@SmallUInt#tymethod.leading_ones) in detail.
#[inline] fn leading_ones(self) -> u32 { self.leading_ones() }
/// Returns the number of leading zeros
/// in the binary representation of `self`.
/// [Read more](trait@SmallUInt#tymethod.leading_zeros) in detail.
#[inline] fn leading_zeros(self) -> u32 { self.leading_zeros() }
/// Returns the number of trailing ones
/// in the binary representation of `self`.
/// [Read more](trait@SmallUInt#tymethod.trailing_ones) in detail.
#[inline] fn trailing_ones(self) -> u32 { self.trailing_ones() }
/// Returns the number of trailing zeros
/// in the binary representation of `self`.
/// [Read more](trait@SmallUInt#tymethod.trailing_zeros) in detail.
#[inline] fn trailing_zeros(self) -> u32 { self.trailing_zeros() }
/// Converts an integer from big endian to the target’s endianness.
/// [Read more](trait@SmallUInt#tymethod.from_be) in detail.
#[inline] fn from_be(x: Self) -> Self { Self::from_be(x) }
/// Converts an integer from little endian to the target’s
/// endianness. [Read more](trait@SmallUInt#tymethod.from_le)
/// in detail.
#[inline] fn from_le(x: Self) -> Self { Self::from_le(x) }
/// Converts `self` to big endian from the target’s endianness.
/// [Read more](trait@SmallUInt#tymethod.to_be) in detail.
#[inline] fn to_be(self) -> Self { self.to_be() }
/// Converts self to little endian from the target’s endianness.
/// [Read more](trait@SmallUInt#tymethod.to_le) in detail.
#[inline] fn to_le(self) -> Self { self.to_le() }
/// Reverses the byte order of the integer.
/// [Read more](trait@SmallUInt#tymethod.swap_bytes) in detail.
#[inline] fn swap_bytes(self) -> Self { self.swap_bytes() }
/// Returns `true` if and only if `self` == `2^k` for some `k`.
/// [Read more](trait@SmallUInt#tymethod.is_power_of_two) in detail.
#[inline] fn is_power_of_two(self) -> bool { self.is_power_of_two() }
/// Returns the smallest power of two greater than or equal to
/// `self`. [Read more](trait@SmallUInt#tymethod.next_power_of_two)
/// in detail.
#[inline] fn next_power_of_two(self) -> Self { self.next_power_of_two() }
/// Converts `self` into `f64` and return it.
/// [Read more](trait@SmallUInt#tymethod.into_f64) in detail.
#[inline] fn into_f64(self) -> f64 { self as f64 }
/// Converts `self` into `f32` and return it.
/// [Read more](trait@SmallUInt#tymethod.into_f32) in detail.
#[inline] fn into_f32(self) -> f32 { self as f32 }
/// Converts `self` into `u128` and return it.
/// [Read more](trait@SmallUInt#tymethod.into_u128) in detail.
#[inline] fn into_u128(self) -> u128 { self as u128 }
/// Converts `self` into `u64` and return it.
/// [Read more](trait@SmallUInt#tymethod.into_u64) in detail.
#[inline] fn into_u64(self) -> u64 { self as u64 }
/// Converts `self` into `u32` and return it.
/// [Read more](trait@SmallUInt#tymethod.into_u32) in detail.
#[inline] fn into_u32(self) -> u32 { self as u32 }
/// Converts `self` into `u16` and return it.
/// [Read more](trait@SmallUInt#tymethod.into_u16) in detail.
#[inline] fn into_u16(self) -> u16 { self as u16 }
/// Converts `self` into `u8` and return it.
/// [Read more](trait@SmallUInt#tymethod.into_u8) in detail.
#[inline] fn into_u8(self) -> u8 { self as u8 }
/// Converts `self` into `usize` and return it.
/// [Read more](trait@SmallUInt#tymethod.into_usize) in detail.
#[inline] fn into_usize(self) -> usize { self as usize }
/// Converts `self` into `bool` and return it.
/// [Read more](trait@SmallUInt#tymethod.into_bool) in detail.
#[inline] fn into_bool(self) -> bool { self != 0 }
/// Converts `self` into `ShortUnion` and return it.
/// [Read more](trait@SmallUInt#tymethod.into_shortunion)
/// in detail.
#[inline] fn into_shortunion(self) -> ShortUnion { ShortUnion::new_with(self.into_u16() ) }
/// Converts `self` into `IntUnion` and return it.
/// [Read more](trait@SmallUInt#tymethod.into_intunion) in detail.
#[inline] fn into_intunion(self) -> IntUnion { IntUnion::new_with(self.into_u32() ) }
/// Converts `self` into `LongUnion` and return it.
/// [Read more](trait@SmallUInt#tymethod.into_longunion) in detail.
#[inline] fn into_longunion(self) -> LongUnion { LongUnion::new_with(self.into_u64() ) }
/// Converts `self` into `LongerUnion` and return it.
/// [Read more](trait@SmallUInt#tymethod.into_longerunion)
/// in detail.
#[inline] fn into_longerunion(self) -> LongerUnion { LongerUnion::new_with(self.into_u128() ) }
/// Converts `self` into `SizeUnion` and return it.
/// [Read more](trait@SmallUInt#tymethod.into_sizeunion) in detail.
#[inline] fn into_sizeunion(self) -> SizeUnion { SizeUnion::new_with(self.into_usize() ) }
/// Returns `zero` which is of `Self`-type.
/// [Read more](trait@SmallUInt#tymethod.zero) in detail.
#[inline] fn zero() -> Self { 0 }
/// Returns `one` which is of `Self`-type.
/// [Read more](trait@SmallUInt#tymethod.one) in detail.
#[inline] fn one() -> Self { 1 }
/// Returns the maximum value of `Self`-type.
/// [Read more](trait@SmallUInt#tymethod.max) in detail.
#[inline] fn max() -> Self { Self::MAX }
/// Returns the maximum value of `Self`-type.
/// [Read more](trait@SmallUInt#tymethod.min) in detail.
#[inline] fn min() -> Self { Self::MIN }
/// Converts `u128`-typed number `n` into `Self`-type.
/// [Read more](trait@SmallUInt#tymethod.u128_as_smalluint)
/// in detail.
#[inline] fn u128_as_smalluint(n: u128) -> Self { n as Self }
/// Converts `u64`-typed number `n` into `Self`-type.
/// [Read more](trait@SmallUInt#tymethod.u64_as_smalluint)
/// in detail.
#[inline] fn u64_as_smalluint(n: u64) -> Self { n as Self }
/// Converts `u32`-typed number `n` into `Self`-type.
/// [Read more](trait@SmallUInt#tymethod.u32_as_smalluint)
/// in detail.
#[inline] fn u32_as_smalluint(n: u32) -> Self { n as Self }
/// Converts `u16`-typed number `n` into `Self`-type.
/// [Read more](trait@SmallUInt#tymethod.u16_as_smalluint)
/// in detail.
#[inline] fn u16_as_smalluint(n: u16) -> Self { n as Self }
/// Converts `u8`-typed number `n` into `Self`-type.
/// [Read more](trait@SmallUInt#tymethod.u8_as_smalluint)
/// in detail.
#[inline] fn u8_as_smalluint(n: u8) -> Self { n as Self }
/// Converts `usize`-typed number `n` into `Self`-type.
/// [Read more](trait@SmallUInt#tymethod.usize_as_smalluint)
/// in detail.
#[inline] fn usize_as_smalluint(n: usize) -> Self { n as Self }
/// Converts `bool`-typed number `n` into `Self`-type.
/// [Read more](trait@SmallUInt#tymethod.bool_as_smalluint)
/// in detail.
#[inline] fn bool_as_smalluint(n: bool) -> Self { n as Self }
/// Converts `T`-typed number `n` into `Self`-type.
/// [Read more](trait@SmallUInt#tymethod.num) in detail.
fn num<T: SmallUInt>(n: T) -> Self
{
match size_of::<T>()
{
1 => { return Self::u8_as_smalluint(n.into_u8()); },
2 => { return Self::u16_as_smalluint(n.into_u16()); },
4 => { return Self::u32_as_smalluint(n.into_u32()); },
8 => { return Self::u64_as_smalluint(n.into_u64()); },
_ => { return Self::u128_as_smalluint(n.into_u128()); },
}
}
/// Checks whether `SmallUInt` to be zero, and returns true
/// if it is zero, and returns false if it is not zero.
/// [Read more](trait@SmallUInt#tymethod.is_zero) in detail.
#[inline] fn is_zero(self) -> bool { self == 0 }
/// Checks whether `SmallUInt` to be one, and returns true
/// if it is one, and returns false if it is not one.
/// [Read more](trait@SmallUInt#tymethod.is_one) in detail.
#[inline] fn is_one(self) -> bool { self == 1 }
/// Checks whether or not `self` is either zero or one,
/// and returns true if it is either zero or one.
/// Otherwise, it returns false.
/// [Read more](trait@SmallUInt#tymethod.is_zero_or_one) in detail.
#[inline] fn is_zero_or_one(self) -> bool { self < 2 }
/// Sets the MSB (Most Significant Bit) of `SmallUInt`-type
/// number with `1`.
/// [Read more](trait@SmallUInt#tymethod.set_msb) in detail.
#[inline] fn set_msb(&mut self) { *self |= !(Self::MAX >> 1); }
/// Sets the MSB (Most Significant Bit) of `SmallUInt`-type
/// number with `0`.
/// [Read more](trait@SmallUInt#tymethod.set_msb) in detail.
#[inline] fn reset_msb(&mut self) { *self &= (Self::MAX >> 1); }
/// Sets the LSB (Least Significant Bit) of `SmallUInt`-type
/// number with `1`.
/// [Read more](trait@SmallUInt#tymethod.set_lsb) in detail.
#[inline] fn set_lsb(&mut self) { *self |= 1; }
/// Sets the LSB (Least Significant Bit) of `SmallUInt`-type
/// number with `0`.
/// [Read more](trait@SmallUInt#tymethod.set_lsb) in detail.
#[inline] fn reset_lsb(&mut self) { *self &= !1; }
/// Constucts a new `SmallUInt` which has the value zero and sets
/// only the bit specified by the argument bit_pos to be 1.
/// [Read more](trait@SmallUInt#tymethod.generate_check_bits)
/// in detail.
#[inline] fn generate_check_bits(bit_pos: u32) -> Option<Self> { if bit_pos < Self::size_in_bits() { Some(Self::generate_check_bits_(bit_pos)) } else { None } }
/// Constucts a new `SmallUInt` which has the value zero and sets
/// only the bit specified by the argument bit_pos to be 1.
/// [Read more](trait@SmallUInt#tymethod.generate_check_bits_)
/// in detail.
#[inline] fn generate_check_bits_(bit_pos: u32) -> Self { Self::one() << bit_pos }
/// Checks whether or not `Self` is an odd number.
/// [Read more](trait@SmallUInt#tymethod.is_odd) in detail.
#[inline] fn is_odd(self) -> bool { (self & 1) != 0 }
/// Checks whether or not `Self` is an even number.
/// [Read more](trait@SmallUInt#tymethod.is_even) in detail.
#[inline] fn is_even(self) -> bool { !self.is_odd() }
/// Checks whether or not the MSB (Most Segnificant Bit) of
/// `self` is set to be one.
/// [Read more](trait@SmallUInt#tymethod.is_msb_set) in detail.
#[inline] fn is_msb_set(self) -> bool { (self & !(Self::MAX >> 1)) != 0 }
/// Checks whether or not the bit of `self` specified by `bit_pos`
/// is set one.
/// [Read more](trait@SmallUInt#tymethod.is_bit_set) in detail.
#[inline] fn is_bit_set(self, bit_pos: u32) -> Option<bool> { if bit_pos < Self::size_in_bits() { Some(self & Self::generate_check_bits_(bit_pos) != 0) } else { None } }
/// Checks whether or not the bit of `self` specified by `bit_pos`
/// is set one.
/// [Read more](trait@SmallUInt#tymethod.is_bit_set_) in detail.
#[inline] fn is_bit_set_(self, bit_pos: u32) -> bool { self & Self::generate_check_bits_(bit_pos) != 0 }
/// Sets `Self`-type number to be maximum value in which all bits
/// are set to be `1`.
/// [Read more](trait@SmallUInt#tymethod.set_max) in detail.
#[inline] fn set_max(&mut self) { *self = Self::MAX }
/// Checks whether or not `Self`-type number to be maximum value.
/// [Read more](trait@SmallUInt#tymethod.is_max)
#[inline] fn is_max(self) -> bool { self == Self::MAX }
/// Sets `Self`-type number to be `size_in_bits`-bit long maximum
/// value in which all bits of `size_in_bits`-bit long lower part
/// are set to be `1`.
/// [Read more](trait@SmallUInt#tymethod.set_submax) in detail.
fn set_submax(&mut self, size_in_bits: u32)
{
if size_in_bits >= self.length_in_bits()
{ self.set_max(); }
else if size_in_bits == 0
{ self.set_zero(); }
else
{
self.set_max();
*self = *self >> (Self::size_in_bits() - size_in_bits);
}
}
/// Sets `Self`-type number to be half long maximum value
/// in which all bits of half-lower part are set to be `1`.
/// [Read more](trait@SmallUInt#tymethod.set_halfmax) in detail.
#[inline] fn set_halfmax(&mut self) { self.set_submax(self.length_in_bits() >> 1); }
/// Sets `self` to be one.
/// [Read more](trait@SmallUInt#tymethod.set_one) in detail.
#[inline] fn set_one(&mut self) { *self = Self::one(); }
/// Sets `self` to be zero.
/// [Read more](trait@SmallUInt#tymethod.set_zero) in detail.
#[inline] fn set_zero(&mut self) { *self = Self::zero(); }
/// Returns the size of `Self` in bytes
/// [Read more](trait@SmallUInt#tymethod.size_in_bytes) in detail.
#[inline] fn size_in_bytes() -> u32 { Self::BITS / 8 }
/// Returns the size of `Self` in bits
/// [Read more](trait@SmallUInt#tymethod.size_in_bits) in detail.
#[inline] fn size_in_bits() -> u32 { Self::BITS }
/// Returns the size of `self` in bytes
/// [Read more](trait@SmallUInt#tymethod.length_in_bytes) in detail.
#[inline] fn length_in_bytes(self) -> u32 { size_of_val(&self) as u32 }
/// Returns the size of `self` in bits
/// [Read more](trait@SmallUInt#tymethod.length_in_bits) in detail.
#[inline] fn length_in_bits(self) -> u32 { size_of_val(&self) as u32 * 8 }
// }
}
}
SmallUInt_methods_for_uint_impl! { u8 }
SmallUInt_methods_for_uint_impl! { u16 }
SmallUInt_methods_for_uint_impl! { u32 }
SmallUInt_methods_for_uint_impl! { u64 }
SmallUInt_methods_for_uint_impl! { u128 }
SmallUInt_methods_for_uint_impl! { usize }