1macro_rules! int_impl {
2 (
3 Self = $SelfT:ty,
4 ActualT = $ActualT:ident,
5 UnsignedT = $UnsignedT:ty,
6
7 BITS = $BITS:literal,
12 BITS_MINUS_ONE = $BITS_MINUS_ONE:literal,
13 Min = $Min:literal,
14 Max = $Max:literal,
15 rot = $rot:literal,
16 rot_op = $rot_op:literal,
17 rot_result = $rot_result:literal,
18 swap_op = $swap_op:literal,
19 swapped = $swapped:literal,
20 reversed = $reversed:literal,
21 le_bytes = $le_bytes:literal,
22 be_bytes = $be_bytes:literal,
23 to_xe_bytes_doc = $to_xe_bytes_doc:expr,
24 from_xe_bytes_doc = $from_xe_bytes_doc:expr,
25 bound_condition = $bound_condition:literal,
26 ) => {
27 #[doc = concat!("(−2<sup>", $BITS_MINUS_ONE, "</sup>", $bound_condition, ").")]
29 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN, ", stringify!($Min), ");")]
34 #[stable(feature = "assoc_int_consts", since = "1.43.0")]
36 pub const MIN: Self = !Self::MAX;
37
38 #[doc = concat!("(2<sup>", $BITS_MINUS_ONE, "</sup> − 1", $bound_condition, ").")]
40 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX, ", stringify!($Max), ");")]
45 #[stable(feature = "assoc_int_consts", since = "1.43.0")]
47 pub const MAX: Self = (<$UnsignedT>::MAX >> 1) as Self;
48
49 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::BITS, ", stringify!($BITS), ");")]
55 #[stable(feature = "int_bits_const", since = "1.53.0")]
57 pub const BITS: u32 = <$UnsignedT>::BITS;
58
59 #[doc = concat!("let n = 0b111_1001", stringify!($SelfT), ";")]
74 #[unstable(feature = "get_set_bit", issue = "147702")]
84 #[rustc_const_unstable(feature = "get_set_bit", issue = "147702")]
85 #[rustc_inherit_overflow_checks]
86 #[inline]
87 #[must_use = "this returns the result of the operation, \
88 without modifying the original"]
89 #[track_caller]
90 pub const fn bit(self, index: u32) -> bool {
91 self.cast_unsigned().bit(index)
92 }
93
94 #[doc = concat!("let mut n = 0b101_0101", stringify!($SelfT), ";")]
109 #[unstable(feature = "get_set_bit", issue = "147702")]
121 #[rustc_const_unstable(feature = "get_set_bit", issue = "147702")]
122 #[rustc_inherit_overflow_checks]
123 #[inline]
124 #[must_use = "this returns the result of the operation, \
125 without modifying the original"]
126 #[track_caller]
127 pub const fn set_bit(self, index: u32, value: bool) -> Self {
128 self.cast_unsigned().set_bit(index, value).cast_signed()
129 }
130
131 #[doc = concat!("let n = 0b100_0000", stringify!($SelfT), ";")]
137 #[stable(feature = "rust1", since = "1.0.0")]
142 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
143 #[doc(alias = "popcount")]
144 #[doc(alias = "popcnt")]
145 #[must_use = "this returns the result of the operation, \
146 without modifying the original"]
147 #[inline(always)]
148 #[ferrocene::prevalidated]
149 pub const fn count_ones(self) -> u32 { (self as $UnsignedT).count_ones() }
150
151 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.count_zeros(), 1);")]
157 #[stable(feature = "rust1", since = "1.0.0")]
159 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
160 #[must_use = "this returns the result of the operation, \
161 without modifying the original"]
162 #[inline(always)]
163 #[ferrocene::prevalidated]
164 pub const fn count_zeros(self) -> u32 {
165 (!self).count_ones()
166 }
167
168 #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
177 #[doc = concat!("[`ilog2`]: ", stringify!($SelfT), "::ilog2")]
181 #[stable(feature = "rust1", since = "1.0.0")]
182 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
183 #[must_use = "this returns the result of the operation, \
184 without modifying the original"]
185 #[inline(always)]
186 #[ferrocene::prevalidated]
187 pub const fn leading_zeros(self) -> u32 {
188 (self as $UnsignedT).leading_zeros()
189 }
190
191 #[doc = concat!("let n = -4", stringify!($SelfT), ";")]
197 #[stable(feature = "rust1", since = "1.0.0")]
201 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
202 #[must_use = "this returns the result of the operation, \
203 without modifying the original"]
204 #[inline(always)]
205 pub const fn trailing_zeros(self) -> u32 {
206 (self as $UnsignedT).trailing_zeros()
207 }
208
209 #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
215 #[doc = concat!("assert_eq!(n.leading_ones(), ", stringify!($BITS), ");")]
217 #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
219 #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
220 #[must_use = "this returns the result of the operation, \
221 without modifying the original"]
222 #[inline(always)]
223 pub const fn leading_ones(self) -> u32 {
224 (self as $UnsignedT).leading_ones()
225 }
226
227 #[doc = concat!("let n = 3", stringify!($SelfT), ";")]
233 #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
237 #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
238 #[must_use = "this returns the result of the operation, \
239 without modifying the original"]
240 #[inline(always)]
241 pub const fn trailing_ones(self) -> u32 {
242 (self as $UnsignedT).trailing_ones()
243 }
244
245 #[doc = concat!("let n: ", stringify!($SelfT), " = 0b_01100100;")]
252 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".isolate_highest_one(), 0);")]
255 #[stable(feature = "isolate_most_least_significant_one", since = "1.97.0")]
257 #[rustc_const_stable(feature = "isolate_most_least_significant_one", since = "1.97.0")]
258 #[must_use = "this returns the result of the operation, \
259 without modifying the original"]
260 #[inline(always)]
261 pub const fn isolate_highest_one(self) -> Self {
262 self & (((1 as $SelfT) << (<$SelfT>::BITS - 1)).wrapping_shr(self.leading_zeros()))
263 }
264
265 #[doc = concat!("let n: ", stringify!($SelfT), " = 0b_01100100;")]
272 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".isolate_lowest_one(), 0);")]
275 #[stable(feature = "isolate_most_least_significant_one", since = "1.97.0")]
277 #[rustc_const_stable(feature = "isolate_most_least_significant_one", since = "1.97.0")]
278 #[must_use = "this returns the result of the operation, \
279 without modifying the original"]
280 #[inline(always)]
281 pub const fn isolate_lowest_one(self) -> Self {
282 self & self.wrapping_neg()
283 }
284
285 #[doc = concat!("assert_eq!(0b0_", stringify!($SelfT), ".highest_one(), None);")]
295 #[doc = concat!("assert_eq!(0b1_", stringify!($SelfT), ".highest_one(), Some(0));")]
296 #[doc = concat!("assert_eq!(0b1_0000_", stringify!($SelfT), ".highest_one(), Some(4));")]
297 #[doc = concat!("assert_eq!(0b1_1111_", stringify!($SelfT), ".highest_one(), Some(4));")]
298 #[stable(feature = "int_lowest_highest_one", since = "1.97.0")]
300 #[rustc_const_stable(feature = "int_lowest_highest_one", since = "1.97.0")]
301 #[must_use = "this returns the result of the operation, \
302 without modifying the original"]
303 #[inline(always)]
304 pub const fn highest_one(self) -> Option<u32> {
305 (self as $UnsignedT).highest_one()
306 }
307
308 #[doc = concat!("assert_eq!(0b0_", stringify!($SelfT), ".lowest_one(), None);")]
315 #[doc = concat!("assert_eq!(0b1_", stringify!($SelfT), ".lowest_one(), Some(0));")]
316 #[doc = concat!("assert_eq!(0b1_0000_", stringify!($SelfT), ".lowest_one(), Some(4));")]
317 #[doc = concat!("assert_eq!(0b1_1111_", stringify!($SelfT), ".lowest_one(), Some(0));")]
318 #[stable(feature = "int_lowest_highest_one", since = "1.97.0")]
320 #[rustc_const_stable(feature = "int_lowest_highest_one", since = "1.97.0")]
321 #[must_use = "this returns the result of the operation, \
322 without modifying the original"]
323 #[inline(always)]
324 pub const fn lowest_one(self) -> Option<u32> {
325 (self as $UnsignedT).lowest_one()
326 }
327
328 #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
337 #[doc = concat!("assert_eq!(n.cast_unsigned(), ", stringify!($UnsignedT), "::MAX);")]
339 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
341 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
342 #[must_use = "this returns the result of the operation, \
343 without modifying the original"]
344 #[inline(always)]
345 #[ferrocene::prevalidated]
346 pub const fn cast_unsigned(self) -> $UnsignedT {
347 self as $UnsignedT
348 }
349
350 #[doc = concat!("let n = ", stringify!($SelfT), "::MIN;")]
364 #[doc = concat!("assert_eq!(n.saturating_cast_unsigned(), 0", stringify!($UnsignedT), ");")]
366 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".saturating_cast_unsigned(), 64", stringify!($UnsignedT), ");")]
367 #[rustc_const_unstable(feature = "integer_cast_extras", issue = "154650")]
369 #[unstable(feature = "integer_cast_extras", issue = "154650")]
370 #[must_use = "this returns the result of the operation, \
371 without modifying the original"]
372 #[inline(always)]
373 pub const fn saturating_cast_unsigned(self) -> $UnsignedT {
374 if self >= 0 {
375 self.cast_unsigned()
376 } else {
377 0
378 }
379 }
380
381 #[doc = concat!("let n = ", stringify!($SelfT), "::MIN;")]
394 #[doc = concat!("assert_eq!(n.checked_cast_unsigned(), None);")]
396 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".checked_cast_unsigned(), Some(64", stringify!($UnsignedT), "));")]
397 #[rustc_const_unstable(feature = "integer_cast_extras", issue = "154650")]
399 #[unstable(feature = "integer_cast_extras", issue = "154650")]
400 #[must_use = "this returns the result of the operation, \
401 without modifying the original"]
402 #[inline(always)]
403 pub const fn checked_cast_unsigned(self) -> Option<$UnsignedT> {
404 if self >= 0 {
405 Some(self.cast_unsigned())
406 } else {
407 None
408 }
409 }
410
411 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_cast_unsigned();")]
424 #[rustc_const_unstable(feature = "integer_cast_extras", issue = "154650")]
426 #[unstable(feature = "integer_cast_extras", issue = "154650")]
427 #[must_use = "this returns the result of the operation, \
428 without modifying the original"]
429 #[inline]
430 #[track_caller]
431 pub const fn strict_cast_unsigned(self) -> $UnsignedT {
432 match self.checked_cast_unsigned() {
433 Some(n) => n,
434 None => imp::overflow_panic::cast_integer(),
435 }
436 }
437
438 #[doc = concat!("let n = ", $rot_op, stringify!($SelfT), ";")]
451 #[doc = concat!("let m = ", $rot_result, ";")]
452 #[doc = concat!("assert_eq!(n.rotate_left(", $rot, "), m);")]
454 #[doc = concat!("assert_eq!(n.rotate_left(1024), n);")]
455 #[stable(feature = "rust1", since = "1.0.0")]
457 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
458 #[must_use = "this returns the result of the operation, \
459 without modifying the original"]
460 #[inline(always)]
461 #[ferrocene::prevalidated]
462 pub const fn rotate_left(self, n: u32) -> Self {
463 (self as $UnsignedT).rotate_left(n) as Self
464 }
465
466 #[doc = concat!("let n = ", $rot_result, stringify!($SelfT), ";")]
480 #[doc = concat!("let m = ", $rot_op, ";")]
481 #[doc = concat!("assert_eq!(n.rotate_right(", $rot, "), m);")]
483 #[doc = concat!("assert_eq!(n.rotate_right(1024), n);")]
484 #[stable(feature = "rust1", since = "1.0.0")]
486 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
487 #[must_use = "this returns the result of the operation, \
488 without modifying the original"]
489 #[inline(always)]
490 pub const fn rotate_right(self, n: u32) -> Self {
491 (self as $UnsignedT).rotate_right(n) as Self
492 }
493
494 #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
500 #[doc = concat!("assert_eq!(m, ", $swapped, ");")]
504 #[stable(feature = "rust1", since = "1.0.0")]
506 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
507 #[must_use = "this returns the result of the operation, \
508 without modifying the original"]
509 #[inline(always)]
510 #[ferrocene::prevalidated]
511 pub const fn swap_bytes(self) -> Self {
512 (self as $UnsignedT).swap_bytes() as Self
513 }
514
515 #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
522 #[doc = concat!("assert_eq!(m, ", $reversed, ");")]
525 #[doc = concat!("assert_eq!(0, 0", stringify!($SelfT), ".reverse_bits());")]
526 #[stable(feature = "reverse_bits", since = "1.37.0")]
528 #[rustc_const_stable(feature = "reverse_bits", since = "1.37.0")]
529 #[must_use = "this returns the result of the operation, \
530 without modifying the original"]
531 #[inline(always)]
532 pub const fn reverse_bits(self) -> Self {
533 (self as $UnsignedT).reverse_bits() as Self
534 }
535
536 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
546 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_be(n), n)")]
549 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_be(n), n.swap_bytes())")]
551 #[stable(feature = "rust1", since = "1.0.0")]
554 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
555 #[must_use]
556 #[inline]
557 pub const fn from_be(x: Self) -> Self {
558 cfg_select! {
559 target_endian = "big" => x,
560 _ => x.swap_bytes(),
561 }
562 }
563
564 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
574 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_le(n), n)")]
577 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_le(n), n.swap_bytes())")]
579 #[stable(feature = "rust1", since = "1.0.0")]
582 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
583 #[must_use]
584 #[inline]
585 #[ferrocene::prevalidated]
586 pub const fn from_le(x: Self) -> Self {
587 cfg_select! {
588 target_endian = "little" => x,
589 _ => x.swap_bytes(),
590 }
591 }
592
593 #[doc = concat!("`", stringify!($SelfT), "`.")]
600 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
607 #[stable(feature = "rust1", since = "1.0.0")]
615 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
616 #[must_use = "this returns the result of the operation, \
617 without modifying the original"]
618 #[inline]
619 pub const fn to_be(self) -> Self { cfg_select! {
621 target_endian = "big" => self,
622 _ => self.swap_bytes(),
623 }
624 }
625
626 #[doc = concat!("`", stringify!($SelfT), "`.")]
633 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
640 #[stable(feature = "rust1", since = "1.0.0")]
648 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
649 #[must_use = "this returns the result of the operation, \
650 without modifying the original"]
651 #[inline]
652 #[ferrocene::prevalidated]
653 pub const fn to_le(self) -> Self {
654 cfg_select! {
655 target_endian = "little" => self,
656 _ => self.swap_bytes(),
657 }
658 }
659
660 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(1), Some(", stringify!($SelfT), "::MAX - 1));")]
667 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);")]
668 #[stable(feature = "rust1", since = "1.0.0")]
670 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
671 #[must_use = "this returns the result of the operation, \
672 without modifying the original"]
673 #[inline]
674 #[ferrocene::prevalidated]
675 pub const fn checked_add(self, rhs: Self) -> Option<Self> {
676 let (a, b) = self.overflowing_add(rhs);
677 if intrinsics::unlikely(b) { None } else { Some(a) }
678 }
679
680 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).strict_add(1), ", stringify!($SelfT), "::MAX - 1);")]
693 #[doc = concat!("let _ = (", stringify!($SelfT), "::MAX - 2).strict_add(3);")]
699 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
701 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
702 #[must_use = "this returns the result of the operation, \
703 without modifying the original"]
704 #[inline]
705 #[track_caller]
706 pub const fn strict_add(self, rhs: Self) -> Self {
707 let (a, b) = self.overflowing_add(rhs);
708 if b { imp::overflow_panic::add() } else { a }
709 }
710
711 #[doc = concat!("[`checked_add`]: ", stringify!($SelfT), "::checked_add")]
727 #[doc = concat!("[`wrapping_add`]: ", stringify!($SelfT), "::wrapping_add")]
728 #[stable(feature = "unchecked_math", since = "1.79.0")]
729 #[rustc_const_stable(feature = "unchecked_math", since = "1.79.0")]
730 #[must_use = "this returns the result of the operation, \
731 without modifying the original"]
732 #[inline(always)]
733 #[track_caller]
734 pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
735 assert_unsafe_precondition!(
736 check_language_ub,
737 concat!(stringify!($SelfT), "::unchecked_add cannot overflow"),
738 (
739 lhs: $SelfT = self,
740 rhs: $SelfT = rhs,
741 ) => !lhs.overflowing_add(rhs).1,
742 );
743
744 unsafe {
746 intrinsics::unchecked_add(self, rhs)
747 }
748 }
749
750 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_add_unsigned(2), Some(3));")]
757 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add_unsigned(3), None);")]
758 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
760 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
761 #[must_use = "this returns the result of the operation, \
762 without modifying the original"]
763 #[inline]
764 #[ferrocene::prevalidated]
765 pub const fn checked_add_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
766 let (a, b) = self.overflowing_add_unsigned(rhs);
767 if intrinsics::unlikely(b) { None } else { Some(a) }
768 }
769
770 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".strict_add_unsigned(2), 3);")]
783 #[doc = concat!("let _ = (", stringify!($SelfT), "::MAX - 2).strict_add_unsigned(3);")]
789 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
791 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
792 #[must_use = "this returns the result of the operation, \
793 without modifying the original"]
794 #[inline]
795 #[track_caller]
796 pub const fn strict_add_unsigned(self, rhs: $UnsignedT) -> Self {
797 let (a, b) = self.overflowing_add_unsigned(rhs);
798 if b { imp::overflow_panic::add() } else { a }
799 }
800
801 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub(1), Some(", stringify!($SelfT), "::MIN + 1));")]
808 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub(3), None);")]
809 #[stable(feature = "rust1", since = "1.0.0")]
811 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
812 #[must_use = "this returns the result of the operation, \
813 without modifying the original"]
814 #[inline]
815 #[ferrocene::prevalidated]
816 pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
817 let (a, b) = self.overflowing_sub(rhs);
818 if intrinsics::unlikely(b) { None } else { Some(a) }
819 }
820
821 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).strict_sub(1), ", stringify!($SelfT), "::MIN + 1);")]
834 #[doc = concat!("let _ = (", stringify!($SelfT), "::MIN + 2).strict_sub(3);")]
840 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
842 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
843 #[must_use = "this returns the result of the operation, \
844 without modifying the original"]
845 #[inline]
846 #[track_caller]
847 pub const fn strict_sub(self, rhs: Self) -> Self {
848 let (a, b) = self.overflowing_sub(rhs);
849 if b { imp::overflow_panic::sub() } else { a }
850 }
851
852 #[doc = concat!("[`checked_sub`]: ", stringify!($SelfT), "::checked_sub")]
868 #[doc = concat!("[`wrapping_sub`]: ", stringify!($SelfT), "::wrapping_sub")]
869 #[stable(feature = "unchecked_math", since = "1.79.0")]
870 #[rustc_const_stable(feature = "unchecked_math", since = "1.79.0")]
871 #[must_use = "this returns the result of the operation, \
872 without modifying the original"]
873 #[inline(always)]
874 #[track_caller]
875 pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
876 assert_unsafe_precondition!(
877 check_language_ub,
878 concat!(stringify!($SelfT), "::unchecked_sub cannot overflow"),
879 (
880 lhs: $SelfT = self,
881 rhs: $SelfT = rhs,
882 ) => !lhs.overflowing_sub(rhs).1,
883 );
884
885 unsafe {
887 intrinsics::unchecked_sub(self, rhs)
888 }
889 }
890
891 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_sub_unsigned(2), Some(-1));")]
898 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub_unsigned(3), None);")]
899 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
901 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
902 #[must_use = "this returns the result of the operation, \
903 without modifying the original"]
904 #[inline]
905 #[ferrocene::prevalidated]
906 pub const fn checked_sub_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
907 let (a, b) = self.overflowing_sub_unsigned(rhs);
908 if intrinsics::unlikely(b) { None } else { Some(a) }
909 }
910
911 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".strict_sub_unsigned(2), -1);")]
924 #[doc = concat!("let _ = (", stringify!($SelfT), "::MIN + 2).strict_sub_unsigned(3);")]
930 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
932 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
933 #[must_use = "this returns the result of the operation, \
934 without modifying the original"]
935 #[inline]
936 #[track_caller]
937 pub const fn strict_sub_unsigned(self, rhs: $UnsignedT) -> Self {
938 let (a, b) = self.overflowing_sub_unsigned(rhs);
939 if b { imp::overflow_panic::sub() } else { a }
940 }
941
942 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(1), Some(", stringify!($SelfT), "::MAX));")]
949 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);")]
950 #[stable(feature = "rust1", since = "1.0.0")]
952 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
953 #[must_use = "this returns the result of the operation, \
954 without modifying the original"]
955 #[inline]
956 #[ferrocene::prevalidated]
957 pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
958 let (a, b) = self.overflowing_mul(rhs);
959 if intrinsics::unlikely(b) { None } else { Some(a) }
960 }
961
962 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.strict_mul(1), ", stringify!($SelfT), "::MAX);")]
975 #[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_mul(2);")]
981 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
983 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
984 #[must_use = "this returns the result of the operation, \
985 without modifying the original"]
986 #[inline]
987 #[track_caller]
988 pub const fn strict_mul(self, rhs: Self) -> Self {
989 let (a, b) = self.overflowing_mul(rhs);
990 if b { imp::overflow_panic::mul() } else { a }
991 }
992
993 #[doc = concat!("[`checked_mul`]: ", stringify!($SelfT), "::checked_mul")]
1009 #[doc = concat!("[`wrapping_mul`]: ", stringify!($SelfT), "::wrapping_mul")]
1010 #[stable(feature = "unchecked_math", since = "1.79.0")]
1011 #[rustc_const_stable(feature = "unchecked_math", since = "1.79.0")]
1012 #[must_use = "this returns the result of the operation, \
1013 without modifying the original"]
1014 #[inline(always)]
1015 #[track_caller]
1016 pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
1017 assert_unsafe_precondition!(
1018 check_language_ub,
1019 concat!(stringify!($SelfT), "::unchecked_mul cannot overflow"),
1020 (
1021 lhs: $SelfT = self,
1022 rhs: $SelfT = rhs,
1023 ) => !lhs.overflowing_mul(rhs).1,
1024 );
1025
1026 unsafe {
1028 intrinsics::unchecked_mul(self, rhs)
1029 }
1030 }
1031
1032 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div(-1), Some(", stringify!($Max), "));")]
1039 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div(-1), None);")]
1040 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div(0), None);")]
1041 #[stable(feature = "rust1", since = "1.0.0")]
1043 #[rustc_const_stable(feature = "const_checked_int_div", since = "1.52.0")]
1044 #[must_use = "this returns the result of the operation, \
1045 without modifying the original"]
1046 #[inline]
1047 pub const fn checked_div(self, rhs: Self) -> Option<Self> {
1048 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
1049 None
1050 } else {
1051 Some(unsafe { intrinsics::unchecked_div(self, rhs) })
1053 }
1054 }
1055
1056 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).strict_div(-1), ", stringify!($Max), ");")]
1078 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_div(-1);")]
1084 #[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div(0);")]
1090 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1092 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1093 #[must_use = "this returns the result of the operation, \
1094 without modifying the original"]
1095 #[inline]
1096 #[track_caller]
1097 pub const fn strict_div(self, rhs: Self) -> Self {
1098 self / rhs
1100 }
1101
1102 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div_euclid(-1), Some(", stringify!($Max), "));")]
1109 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div_euclid(-1), None);")]
1110 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div_euclid(0), None);")]
1111 #[stable(feature = "euclidean_division", since = "1.38.0")]
1113 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1114 #[must_use = "this returns the result of the operation, \
1115 without modifying the original"]
1116 #[inline]
1117 pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
1118 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
1120 None
1121 } else {
1122 Some(self.div_euclid(rhs))
1123 }
1124 }
1125
1126 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).strict_div_euclid(-1), ", stringify!($Max), ");")]
1148 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_div_euclid(-1);")]
1154 #[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div_euclid(0);")]
1160 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1162 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1163 #[must_use = "this returns the result of the operation, \
1164 without modifying the original"]
1165 #[inline]
1166 #[track_caller]
1167 pub const fn strict_div_euclid(self, rhs: Self) -> Self {
1168 self.div_euclid(rhs)
1170 }
1171
1172 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div_exact(-1), Some(", stringify!($Max), "));")]
1181 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_div_exact(2), None);")]
1182 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div_exact(-1), None);")]
1183 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div_exact(0), None);")]
1184 #[unstable(
1186 feature = "exact_div",
1187 issue = "139911",
1188 )]
1189 #[must_use = "this returns the result of the operation, \
1190 without modifying the original"]
1191 #[inline]
1192 pub const fn checked_div_exact(self, rhs: Self) -> Option<Self> {
1193 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
1194 None
1195 } else {
1196 unsafe {
1198 if intrinsics::unlikely(intrinsics::unchecked_rem(self, rhs) != 0) {
1199 None
1200 } else {
1201 Some(intrinsics::exact_div(self, rhs))
1202 }
1203 }
1204 }
1205 }
1206
1207 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".div_exact(2), Some(32));")]
1223 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".div_exact(32), Some(2));")]
1224 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).div_exact(-1), Some(", stringify!($Max), "));")]
1225 #[doc = concat!("assert_eq!(65", stringify!($SelfT), ".div_exact(2), None);")]
1226 #[doc = concat!("let _ = 64", stringify!($SelfT),".div_exact(0);")]
1230 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.div_exact(-1);")]
1234 #[unstable(
1236 feature = "exact_div",
1237 issue = "139911",
1238 )]
1239 #[must_use = "this returns the result of the operation, \
1240 without modifying the original"]
1241 #[inline]
1242 #[rustc_inherit_overflow_checks]
1243 pub const fn div_exact(self, rhs: Self) -> Option<Self> {
1244 if self % rhs != 0 {
1245 None
1246 } else {
1247 Some(self / rhs)
1248 }
1249 }
1250
1251 #[doc = concat!("`self == ", stringify!($SelfT), "::MIN && rhs == -1`,")]
1257 #[unstable(
1259 feature = "exact_div",
1260 issue = "139911",
1261 )]
1262 #[must_use = "this returns the result of the operation, \
1263 without modifying the original"]
1264 #[inline]
1265 pub const unsafe fn unchecked_div_exact(self, rhs: Self) -> Self {
1266 assert_unsafe_precondition!(
1267 check_language_ub,
1268 concat!(stringify!($SelfT), "::unchecked_div_exact cannot overflow, divide by zero, or leave a remainder"),
1269 (
1270 lhs: $SelfT = self,
1271 rhs: $SelfT = rhs,
1272 ) => rhs != 0 && lhs % rhs == 0 && (lhs != <$SelfT>::MIN || rhs != -1),
1273 );
1274 unsafe { intrinsics::exact_div(self, rhs) }
1276 }
1277
1278 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(2), Some(1));")]
1285 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(0), None);")]
1286 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem(-1), None);")]
1287 #[stable(feature = "wrapping", since = "1.7.0")]
1289 #[rustc_const_stable(feature = "const_checked_int_div", since = "1.52.0")]
1290 #[must_use = "this returns the result of the operation, \
1291 without modifying the original"]
1292 #[inline]
1293 pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
1294 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
1295 None
1296 } else {
1297 Some(unsafe { intrinsics::unchecked_rem(self, rhs) })
1299 }
1300 }
1301
1302 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_rem(2), 1);")]
1320 #[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem(0);")]
1326 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_rem(-1);")]
1332 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1334 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1335 #[must_use = "this returns the result of the operation, \
1336 without modifying the original"]
1337 #[inline]
1338 #[track_caller]
1339 pub const fn strict_rem(self, rhs: Self) -> Self {
1340 let (a, b) = self.overflowing_rem(rhs);
1341 if b { imp::overflow_panic::rem() } else { a }
1342 }
1343
1344 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(2), Some(1));")]
1351 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(0), None);")]
1352 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem_euclid(-1), None);")]
1353 #[stable(feature = "euclidean_division", since = "1.38.0")]
1355 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1356 #[must_use = "this returns the result of the operation, \
1357 without modifying the original"]
1358 #[inline]
1359 pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
1360 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
1362 None
1363 } else {
1364 Some(self.rem_euclid(rhs))
1365 }
1366 }
1367
1368 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_rem_euclid(2), 1);")]
1386 #[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem_euclid(0);")]
1392 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_rem_euclid(-1);")]
1398 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1400 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1401 #[must_use = "this returns the result of the operation, \
1402 without modifying the original"]
1403 #[inline]
1404 #[track_caller]
1405 pub const fn strict_rem_euclid(self, rhs: Self) -> Self {
1406 let (a, b) = self.overflowing_rem_euclid(rhs);
1407 if b { imp::overflow_panic::rem() } else { a }
1408 }
1409
1410 #[doc = concat!(" unsafe { 1_", stringify!($SelfT), ".unchecked_disjoint_bitor(-2) },")]
1424 #[unstable(feature = "disjoint_bitor", issue = "135758")]
1435 #[rustc_const_unstable(feature = "disjoint_bitor", issue = "135758")]
1436 #[must_use = "this returns the result of the operation, \
1437 without modifying the original"]
1438 #[inline]
1439 pub const unsafe fn unchecked_disjoint_bitor(self, rhs: Self) -> Self {
1440 assert_unsafe_precondition!(
1441 check_language_ub,
1442 concat!(stringify!($SelfT), "::unchecked_disjoint_bitor cannot bitor overlapping ones"),
1443 (
1444 lhs: $SelfT = self,
1445 rhs: $SelfT = rhs,
1446 ) => (lhs & rhs) == 0,
1447 );
1448
1449 unsafe { intrinsics::disjoint_bitor(self, rhs) }
1451 }
1452
1453 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_neg(), Some(-5));")]
1459 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_neg(), None);")]
1460 #[stable(feature = "wrapping", since = "1.7.0")]
1462 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1463 #[must_use = "this returns the result of the operation, \
1464 without modifying the original"]
1465 #[inline]
1466 pub const fn checked_neg(self) -> Option<Self> {
1467 let (a, b) = self.overflowing_neg();
1468 if intrinsics::unlikely(b) { None } else { Some(a) }
1469 }
1470
1471 #[doc = concat!("`self == ", stringify!($SelfT), "::MIN`,")]
1477 #[doc = concat!("[`checked_neg`]: ", stringify!($SelfT), "::checked_neg")]
1480 #[stable(feature = "unchecked_neg", since = "1.93.0")]
1481 #[rustc_const_stable(feature = "unchecked_neg", since = "1.93.0")]
1482 #[must_use = "this returns the result of the operation, \
1483 without modifying the original"]
1484 #[inline(always)]
1485 #[track_caller]
1486 #[ferrocene::prevalidated]
1487 pub const unsafe fn unchecked_neg(self) -> Self {
1488 assert_unsafe_precondition!(
1489 check_language_ub,
1490 concat!(stringify!($SelfT), "::unchecked_neg cannot overflow"),
1491 (
1492 lhs: $SelfT = self,
1493 ) => !lhs.overflowing_neg().1,
1494 );
1495
1496 unsafe {
1498 intrinsics::unchecked_sub(0, self)
1499 }
1500 }
1501
1502 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_neg(), -5);")]
1514 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_neg();")]
1520 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1522 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1523 #[must_use = "this returns the result of the operation, \
1524 without modifying the original"]
1525 #[inline]
1526 #[track_caller]
1527 pub const fn strict_neg(self) -> Self {
1528 let (a, b) = self.overflowing_neg();
1529 if b { imp::overflow_panic::neg() } else { a }
1530 }
1531
1532 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(4), Some(0x10));")]
1539 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(129), None);")]
1540 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shl(", stringify!($BITS_MINUS_ONE), "), Some(0));")]
1541 #[stable(feature = "wrapping", since = "1.7.0")]
1543 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1544 #[must_use = "this returns the result of the operation, \
1545 without modifying the original"]
1546 #[inline]
1547 pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
1548 if rhs < Self::BITS {
1550 Some(unsafe { self.unchecked_shl(rhs) })
1552 } else {
1553 None
1554 }
1555 }
1556
1557 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".strict_shl(4), 0x10);")]
1570 #[doc = concat!("let _ = 0x1", stringify!($SelfT), ".strict_shl(129);")]
1576 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1578 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1579 #[must_use = "this returns the result of the operation, \
1580 without modifying the original"]
1581 #[inline]
1582 #[track_caller]
1583 pub const fn strict_shl(self, rhs: u32) -> Self {
1584 let (a, b) = self.overflowing_shl(rhs);
1585 if b { imp::overflow_panic::shl() } else { a }
1586 }
1587
1588 #[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
1598 #[stable(feature = "unchecked_shifts", since = "1.93.0")]
1599 #[rustc_const_stable(feature = "unchecked_shifts", since = "1.93.0")]
1600 #[must_use = "this returns the result of the operation, \
1601 without modifying the original"]
1602 #[inline(always)]
1603 #[track_caller]
1604 #[ferrocene::prevalidated]
1605 pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
1606 assert_unsafe_precondition!(
1607 check_language_ub,
1608 concat!(stringify!($SelfT), "::unchecked_shl cannot overflow"),
1609 (
1610 rhs: u32 = rhs,
1611 ) => rhs < <$ActualT>::BITS,
1612 );
1613
1614 unsafe {
1616 intrinsics::unchecked_shl(self, rhs)
1617 }
1618 }
1619
1620 #[doc = concat!("assert_eq!(0x1_", stringify!($SelfT), ".unbounded_shl(4), 0x10);")]
1629 #[doc = concat!("assert_eq!(0x1_", stringify!($SelfT), ".unbounded_shl(129), 0);")]
1630 #[doc = concat!("assert_eq!(0b101_", stringify!($SelfT), ".unbounded_shl(0), 0b101);")]
1631 #[doc = concat!("assert_eq!(0b101_", stringify!($SelfT), ".unbounded_shl(1), 0b1010);")]
1632 #[doc = concat!("assert_eq!(0b101_", stringify!($SelfT), ".unbounded_shl(2), 0b10100);")]
1633 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shl(", stringify!($BITS), "), 0);")]
1634 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shl(1).unbounded_shl(", stringify!($BITS_MINUS_ONE), "), 0);")]
1635 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shl(", stringify!($BITS), "), 0);")]
1636 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shl(1).unbounded_shl(", stringify!($BITS_MINUS_ONE), "), 0);")]
1637 #[stable(feature = "unbounded_shifts", since = "1.87.0")]
1639 #[rustc_const_stable(feature = "unbounded_shifts", since = "1.87.0")]
1640 #[must_use = "this returns the result of the operation, \
1641 without modifying the original"]
1642 #[inline]
1643 pub const fn unbounded_shl(self, rhs: u32) -> $SelfT{
1644 if rhs < Self::BITS {
1645 unsafe { self.unchecked_shl(rhs) }
1648 } else {
1649 0
1650 }
1651 }
1652
1653 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1658 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".shl_exact(4), Some(0x10));")]
1666 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".shl_exact(", stringify!($SelfT), "::BITS - 2), Some(1 << ", stringify!($SelfT), "::BITS - 2));")]
1667 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".shl_exact(", stringify!($SelfT), "::BITS - 1), None);")]
1668 #[doc = concat!("assert_eq!((-0x2", stringify!($SelfT), ").shl_exact(", stringify!($SelfT), "::BITS - 2), Some(-0x2 << ", stringify!($SelfT), "::BITS - 2));")]
1669 #[doc = concat!("assert_eq!((-0x2", stringify!($SelfT), ").shl_exact(", stringify!($SelfT), "::BITS - 1), None);")]
1670 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1672 #[must_use = "this returns the result of the operation, \
1673 without modifying the original"]
1674 #[inline]
1675 pub const fn shl_exact(self, rhs: u32) -> Option<$SelfT> {
1676 if rhs < self.leading_zeros() || rhs < self.leading_ones() {
1677 Some(unsafe { self.unchecked_shl(rhs) })
1679 } else {
1680 None
1681 }
1682 }
1683
1684 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1687 #[doc = concat!("[`", stringify!($SelfT), "::shl_exact`]")]
1693 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1695 #[must_use = "this returns the result of the operation, \
1696 without modifying the original"]
1697 #[inline]
1698 pub const unsafe fn unchecked_shl_exact(self, rhs: u32) -> $SelfT {
1699 assert_unsafe_precondition!(
1700 check_library_ub,
1701 concat!(stringify!($SelfT), "::unchecked_shl_exact cannot shift out bits that would change the value of the first bit"),
1702 (
1703 zeros: u32 = self.leading_zeros(),
1704 ones: u32 = self.leading_ones(),
1705 rhs: u32 = rhs,
1706 ) => rhs < zeros || rhs < ones,
1707 );
1708
1709 unsafe { self.unchecked_shl(rhs) }
1711 }
1712
1713 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(4), Some(0x1));")]
1720 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(128), None);")]
1721 #[stable(feature = "wrapping", since = "1.7.0")]
1723 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1724 #[must_use = "this returns the result of the operation, \
1725 without modifying the original"]
1726 #[inline]
1727 pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
1728 if rhs < Self::BITS {
1730 Some(unsafe { self.unchecked_shr(rhs) })
1732 } else {
1733 None
1734 }
1735 }
1736
1737 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".strict_shr(4), 0x1);")]
1750 #[doc = concat!("let _ = 0x10", stringify!($SelfT), ".strict_shr(128);")]
1756 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1758 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1759 #[must_use = "this returns the result of the operation, \
1760 without modifying the original"]
1761 #[inline]
1762 #[track_caller]
1763 pub const fn strict_shr(self, rhs: u32) -> Self {
1764 let (a, b) = self.overflowing_shr(rhs);
1765 if b { imp::overflow_panic::shr() } else { a }
1766 }
1767
1768 #[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
1778 #[stable(feature = "unchecked_shifts", since = "1.93.0")]
1779 #[rustc_const_stable(feature = "unchecked_shifts", since = "1.93.0")]
1780 #[must_use = "this returns the result of the operation, \
1781 without modifying the original"]
1782 #[inline(always)]
1783 #[track_caller]
1784 #[ferrocene::prevalidated]
1785 pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
1786 assert_unsafe_precondition!(
1787 check_language_ub,
1788 concat!(stringify!($SelfT), "::unchecked_shr cannot overflow"),
1789 (
1790 rhs: u32 = rhs,
1791 ) => rhs < <$ActualT>::BITS,
1792 );
1793
1794 unsafe {
1796 intrinsics::unchecked_shr(self, rhs)
1797 }
1798 }
1799
1800 #[doc = concat!("assert_eq!(0x10_", stringify!($SelfT), ".unbounded_shr(4), 0x1);")]
1810 #[doc = concat!("assert_eq!(0x10_", stringify!($SelfT), ".unbounded_shr(129), 0);")]
1811 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.unbounded_shr(129), -1);")]
1812 #[doc = concat!("assert_eq!(0b1010_", stringify!($SelfT), ".unbounded_shr(0), 0b1010);")]
1813 #[doc = concat!("assert_eq!(0b1010_", stringify!($SelfT), ".unbounded_shr(1), 0b101);")]
1814 #[doc = concat!("assert_eq!(0b1010_", stringify!($SelfT), ".unbounded_shr(2), 0b10);")]
1815 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shr(", stringify!($BITS), "), 0);")]
1816 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shr(1).unbounded_shr(", stringify!($BITS_MINUS_ONE), "), 0);")]
1817 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shr(", stringify!($BITS), "), -1);")]
1818 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shr(1).unbounded_shr(", stringify!($BITS_MINUS_ONE), "), -1);")]
1819 #[ferrocene::prevalidated]
1821 #[stable(feature = "unbounded_shifts", since = "1.87.0")]
1822 #[rustc_const_stable(feature = "unbounded_shifts", since = "1.87.0")]
1823 #[must_use = "this returns the result of the operation, \
1824 without modifying the original"]
1825 #[inline]
1826 pub const fn unbounded_shr(self, rhs: u32) -> $SelfT{
1827 if rhs < Self::BITS {
1828 unsafe { self.unchecked_shr(rhs) }
1831 } else {
1832 unsafe { self.unchecked_shr(Self::BITS - 1) }
1837 }
1838 }
1839
1840 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1844 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".shr_exact(4), Some(0x1));")]
1852 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".shr_exact(5), None);")]
1853 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1855 #[must_use = "this returns the result of the operation, \
1856 without modifying the original"]
1857 #[inline]
1858 pub const fn shr_exact(self, rhs: u32) -> Option<$SelfT> {
1859 if rhs <= self.trailing_zeros() && rhs < <$SelfT>::BITS {
1860 Some(unsafe { self.unchecked_shr(rhs) })
1862 } else {
1863 None
1864 }
1865 }
1866
1867 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1870 #[doc = concat!(stringify!($SelfT), "::BITS`")]
1875 #[doc = concat!("[`", stringify!($SelfT), "::shr_exact`]")]
1877 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1879 #[must_use = "this returns the result of the operation, \
1880 without modifying the original"]
1881 #[inline]
1882 pub const unsafe fn unchecked_shr_exact(self, rhs: u32) -> $SelfT {
1883 assert_unsafe_precondition!(
1884 check_library_ub,
1885 concat!(stringify!($SelfT), "::unchecked_shr_exact cannot shift out non-zero bits"),
1886 (
1887 zeros: u32 = self.trailing_zeros(),
1888 bits: u32 = <$SelfT>::BITS,
1889 rhs: u32 = rhs,
1890 ) => rhs <= zeros && rhs < bits,
1891 );
1892
1893 unsafe { self.unchecked_shr(rhs) }
1895 }
1896
1897 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_abs(), Some(5));")]
1904 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_abs(), None);")]
1905 #[stable(feature = "no_panic_abs", since = "1.13.0")]
1907 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1908 #[must_use = "this returns the result of the operation, \
1909 without modifying the original"]
1910 #[inline]
1911 pub const fn checked_abs(self) -> Option<Self> {
1912 if self.is_negative() {
1913 self.checked_neg()
1914 } else {
1915 Some(self)
1916 }
1917 }
1918
1919 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").strict_abs(), 5);")]
1932 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_abs();")]
1938 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1940 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1941 #[must_use = "this returns the result of the operation, \
1942 without modifying the original"]
1943 #[inline]
1944 #[track_caller]
1945 pub const fn strict_abs(self) -> Self {
1946 if self.is_negative() {
1947 self.strict_neg()
1948 } else {
1949 self
1950 }
1951 }
1952
1953 #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".checked_pow(2), Some(64));")]
1960 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".checked_pow(0), Some(1));")]
1961 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);")]
1962 #[stable(feature = "no_panic_pow", since = "1.34.0")]
1965 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1966 #[must_use = "this returns the result of the operation, \
1967 without modifying the original"]
1968 #[inline]
1969 pub const fn checked_pow(self, mut exp: u32) -> Option<Self> {
1970 let mut base = self;
1971 let mut acc: Self = 1;
1972
1973 if intrinsics::is_val_statically_known(base) && base.unsigned_abs().is_power_of_two() {
1974 let k = base.unsigned_abs().ilog2();
1975 let shift = try_opt!(k.checked_mul(exp));
1976 return if base < 0 && (exp % 2) == 1 {
1977 (-1 as Self).shl_exact(shift)
1978 } else {
1979 (1 as Self).shl_exact(shift)
1980 }
1981 }
1982
1983 if exp == 0 {
1984 return Some(1);
1985 }
1986
1987 if intrinsics::is_val_statically_known(exp) {
1988 while exp > 1 {
1989 if (exp & 1) == 1 {
1990 acc = try_opt!(acc.checked_mul(base));
1991 }
1992 exp /= 2;
1993 base = try_opt!(base.checked_mul(base));
1994 }
1995
1996 return acc.checked_mul(base);
2001 }
2002
2003 loop {
2004 if (exp & 1) == 1 {
2005 acc = try_opt!(acc.checked_mul(base));
2006 if exp == 1 {
2008 return Some(acc);
2009 }
2010 }
2011 exp /= 2;
2012 base = try_opt!(base.checked_mul(base));
2013 }
2014 }
2015
2016 #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".strict_pow(2), 64);")]
2029 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".strict_pow(0), 1);")]
2030 #[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_pow(2);")]
2036 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
2038 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
2039 #[must_use = "this returns the result of the operation, \
2040 without modifying the original"]
2041 #[inline]
2042 #[track_caller]
2043 pub const fn strict_pow(self, exp: u32) -> Self {
2044 match self.checked_pow(exp) {
2045 Some(x) => x,
2046 None => imp::overflow_panic::pow(),
2047 }
2048 }
2049
2050 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_isqrt(), Some(3));")]
2062 #[stable(feature = "isqrt", since = "1.84.0")]
2064 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
2065 #[must_use = "this returns the result of the operation, \
2066 without modifying the original"]
2067 #[inline]
2068 pub const fn checked_isqrt(self) -> Option<Self> {
2069 if self < 0 {
2070 None
2071 } else {
2072 let result = self.cast_unsigned().isqrt().cast_signed();
2075
2076 unsafe {
2088 const MAX_RESULT: $SelfT = <$SelfT>::MAX.cast_unsigned().isqrt().cast_signed();
2089 crate::hint::assert_unchecked(result <= MAX_RESULT);
2090 }
2091 Some(result)
2092 }
2093 }
2094
2095 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_add(1), 101);")]
2102 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add(100), ", stringify!($SelfT), "::MAX);")]
2103 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_add(-1), ", stringify!($SelfT), "::MIN);")]
2104 #[stable(feature = "rust1", since = "1.0.0")]
2107 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2108 #[must_use = "this returns the result of the operation, \
2109 without modifying the original"]
2110 #[inline(always)]
2111 #[ferrocene::prevalidated]
2112 pub const fn saturating_add(self, rhs: Self) -> Self {
2113 intrinsics::saturating_add(self, rhs)
2114 }
2115
2116 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".saturating_add_unsigned(2), 3);")]
2123 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add_unsigned(100), ", stringify!($SelfT), "::MAX);")]
2124 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2126 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2127 #[must_use = "this returns the result of the operation, \
2128 without modifying the original"]
2129 #[inline]
2130 pub const fn saturating_add_unsigned(self, rhs: $UnsignedT) -> Self {
2131 match self.checked_add_unsigned(rhs) {
2134 Some(x) => x,
2135 None => Self::MAX,
2136 }
2137 }
2138
2139 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub(127), -27);")]
2146 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub(100), ", stringify!($SelfT), "::MIN);")]
2147 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_sub(-1), ", stringify!($SelfT), "::MAX);")]
2148 #[stable(feature = "rust1", since = "1.0.0")]
2150 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2151 #[must_use = "this returns the result of the operation, \
2152 without modifying the original"]
2153 #[inline(always)]
2154 #[ferrocene::prevalidated]
2155 pub const fn saturating_sub(self, rhs: Self) -> Self {
2156 intrinsics::saturating_sub(self, rhs)
2157 }
2158
2159 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub_unsigned(127), -27);")]
2166 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub_unsigned(100), ", stringify!($SelfT), "::MIN);")]
2167 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2169 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2170 #[must_use = "this returns the result of the operation, \
2171 without modifying the original"]
2172 #[inline]
2173 pub const fn saturating_sub_unsigned(self, rhs: $UnsignedT) -> Self {
2174 match self.checked_sub_unsigned(rhs) {
2177 Some(x) => x,
2178 None => Self::MIN,
2179 }
2180 }
2181
2182 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_neg(), -100);")]
2189 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_neg(), 100);")]
2190 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_neg(), ", stringify!($SelfT), "::MAX);")]
2191 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_neg(), ", stringify!($SelfT), "::MIN + 1);")]
2192 #[stable(feature = "saturating_neg", since = "1.45.0")]
2195 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2196 #[must_use = "this returns the result of the operation, \
2197 without modifying the original"]
2198 #[inline(always)]
2199 #[ferrocene::prevalidated]
2200 pub const fn saturating_neg(self) -> Self {
2201 intrinsics::saturating_sub(0, self)
2202 }
2203
2204 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_abs(), 100);")]
2211 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_abs(), 100);")]
2212 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_abs(), ", stringify!($SelfT), "::MAX);")]
2213 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).saturating_abs(), ", stringify!($SelfT), "::MAX);")]
2214 #[stable(feature = "saturating_neg", since = "1.45.0")]
2217 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2218 #[must_use = "this returns the result of the operation, \
2219 without modifying the original"]
2220 #[inline]
2221 pub const fn saturating_abs(self) -> Self {
2222 if self.is_negative() {
2223 self.saturating_neg()
2224 } else {
2225 self
2226 }
2227 }
2228
2229 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".saturating_mul(12), 120);")]
2236 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_mul(10), ", stringify!($SelfT), "::MAX);")]
2237 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_mul(10), ", stringify!($SelfT), "::MIN);")]
2238 #[stable(feature = "wrapping", since = "1.7.0")]
2240 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2241 #[must_use = "this returns the result of the operation, \
2242 without modifying the original"]
2243 #[inline]
2244 #[ferrocene::prevalidated]
2245 pub const fn saturating_mul(self, rhs: Self) -> Self {
2246 match self.checked_mul(rhs) {
2247 Some(x) => x,
2248 None => if (self < 0) == (rhs < 0) {
2249 Self::MAX
2250 } else {
2251 Self::MIN
2252 }
2253 }
2254 }
2255
2256 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
2267 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_div(-1), ", stringify!($SelfT), "::MIN + 1);")]
2268 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_div(-1), ", stringify!($SelfT), "::MAX);")]
2269 #[ferrocene::prevalidated]
2272 #[stable(feature = "saturating_div", since = "1.58.0")]
2273 #[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
2274 #[must_use = "this returns the result of the operation, \
2275 without modifying the original"]
2276 #[inline]
2277 #[track_caller]
2278 pub const fn saturating_div(self, rhs: Self) -> Self {
2279 match self.overflowing_div(rhs) {
2280 (result, false) => result,
2281 (_result, true) => Self::MAX, }
2283 }
2284
2285 #[doc = concat!("assert_eq!((-4", stringify!($SelfT), ").saturating_pow(3), -64);")]
2292 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".saturating_pow(0), 1);")]
2293 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(2), ", stringify!($SelfT), "::MAX);")]
2294 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(3), ", stringify!($SelfT), "::MIN);")]
2295 #[stable(feature = "no_panic_pow", since = "1.34.0")]
2297 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2298 #[must_use = "this returns the result of the operation, \
2299 without modifying the original"]
2300 #[inline]
2301 pub const fn saturating_pow(self, exp: u32) -> Self {
2302 match self.checked_pow(exp) {
2303 Some(x) => x,
2304 None if self < 0 && exp % 2 == 1 => Self::MIN,
2305 None => Self::MAX,
2306 }
2307 }
2308
2309 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add(27), 127);")]
2316 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add(2), ", stringify!($SelfT), "::MIN + 1);")]
2317 #[stable(feature = "rust1", since = "1.0.0")]
2319 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2320 #[must_use = "this returns the result of the operation, \
2321 without modifying the original"]
2322 #[inline(always)]
2323 #[ferrocene::prevalidated]
2324 pub const fn wrapping_add(self, rhs: Self) -> Self {
2325 intrinsics::wrapping_add(self, rhs)
2326 }
2327
2328 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add_unsigned(27), 127);")]
2335 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add_unsigned(2), ", stringify!($SelfT), "::MIN + 1);")]
2336 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2338 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2339 #[must_use = "this returns the result of the operation, \
2340 without modifying the original"]
2341 #[inline(always)]
2342 pub const fn wrapping_add_unsigned(self, rhs: $UnsignedT) -> Self {
2343 self.wrapping_add(rhs as Self)
2344 }
2345
2346 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub(127), -127);")]
2353 #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub(", stringify!($SelfT), "::MAX), ", stringify!($SelfT), "::MAX);")]
2354 #[stable(feature = "rust1", since = "1.0.0")]
2356 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2357 #[must_use = "this returns the result of the operation, \
2358 without modifying the original"]
2359 #[inline(always)]
2360 #[ferrocene::prevalidated]
2361 pub const fn wrapping_sub(self, rhs: Self) -> Self {
2362 intrinsics::wrapping_sub(self, rhs)
2363 }
2364
2365 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub_unsigned(127), -127);")]
2372 #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub_unsigned(", stringify!($UnsignedT), "::MAX), -1);")]
2373 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2375 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2376 #[must_use = "this returns the result of the operation, \
2377 without modifying the original"]
2378 #[inline(always)]
2379 pub const fn wrapping_sub_unsigned(self, rhs: $UnsignedT) -> Self {
2380 self.wrapping_sub(rhs as Self)
2381 }
2382
2383 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".wrapping_mul(12), 120);")]
2390 #[stable(feature = "rust1", since = "1.0.0")]
2393 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2394 #[must_use = "this returns the result of the operation, \
2395 without modifying the original"]
2396 #[inline(always)]
2397 #[ferrocene::prevalidated]
2398 pub const fn wrapping_mul(self, rhs: Self) -> Self {
2399 intrinsics::wrapping_mul(self, rhs)
2400 }
2401
2402 #[ferrocene::prevalidated]
2417 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div(10), 10);")]
2418 #[stable(feature = "num_wrapping", since = "1.2.0")]
2421 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
2422 #[must_use = "this returns the result of the operation, \
2423 without modifying the original"]
2424 #[inline]
2425 #[track_caller]
2426 pub const fn wrapping_div(self, rhs: Self) -> Self {
2427 self.overflowing_div(rhs).0
2428 }
2429
2430 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div_euclid(10), 10);")]
2445 #[stable(feature = "euclidean_division", since = "1.38.0")]
2448 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2449 #[must_use = "this returns the result of the operation, \
2450 without modifying the original"]
2451 #[inline]
2452 #[track_caller]
2453 pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
2454 self.overflowing_div_euclid(rhs).0
2455 }
2456
2457 #[ferrocene::prevalidated]
2472 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem(10), 0);")]
2473 #[stable(feature = "num_wrapping", since = "1.2.0")]
2476 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
2477 #[must_use = "this returns the result of the operation, \
2478 without modifying the original"]
2479 #[inline]
2480 #[track_caller]
2481 pub const fn wrapping_rem(self, rhs: Self) -> Self {
2482 self.overflowing_rem(rhs).0
2483 }
2484
2485 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);")]
2499 #[stable(feature = "euclidean_division", since = "1.38.0")]
2502 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2503 #[must_use = "this returns the result of the operation, \
2504 without modifying the original"]
2505 #[inline]
2506 #[track_caller]
2507 pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
2508 self.overflowing_rem_euclid(rhs).0
2509 }
2510
2511 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_neg(), -100);")]
2522 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_neg(), 100);")]
2523 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_neg(), ", stringify!($SelfT), "::MIN);")]
2524 #[stable(feature = "num_wrapping", since = "1.2.0")]
2526 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2527 #[must_use = "this returns the result of the operation, \
2528 without modifying the original"]
2529 #[inline(always)]
2530 #[ferrocene::prevalidated]
2531 pub const fn wrapping_neg(self) -> Self {
2532 (0 as $SelfT).wrapping_sub(self)
2533 }
2534
2535 #[doc = concat!("assert_eq!((-1_", stringify!($SelfT), ").wrapping_shl(7), -128);")]
2554 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shl(", stringify!($BITS), "), 42);")]
2555 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shl(1).wrapping_shl(", stringify!($BITS_MINUS_ONE), "), 0);")]
2556 #[doc = concat!("assert_eq!((-1_", stringify!($SelfT), ").wrapping_shl(128), -1);")]
2557 #[doc = concat!("assert_eq!(5_", stringify!($SelfT), ".wrapping_shl(1025), 10);")]
2558 #[stable(feature = "num_wrapping", since = "1.2.0")]
2560 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2561 #[must_use = "this returns the result of the operation, \
2562 without modifying the original"]
2563 #[inline(always)]
2564 #[ferrocene::prevalidated]
2565 pub const fn wrapping_shl(self, rhs: u32) -> Self {
2566 unsafe {
2569 self.unchecked_shl(rhs & (Self::BITS - 1))
2570 }
2571 }
2572
2573 #[doc = concat!("assert_eq!((-128_", stringify!($SelfT), ").wrapping_shr(7), -1);")]
2592 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shr(", stringify!($BITS), "), 42);")]
2593 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shr(1).wrapping_shr(", stringify!($BITS_MINUS_ONE), "), 0);")]
2594 #[doc = concat!("assert_eq!(10_", stringify!($SelfT), ".wrapping_shr(1025), 5);")]
2596 #[stable(feature = "num_wrapping", since = "1.2.0")]
2598 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2599 #[must_use = "this returns the result of the operation, \
2600 without modifying the original"]
2601 #[inline(always)]
2602 #[ferrocene::prevalidated]
2603 pub const fn wrapping_shr(self, rhs: u32) -> Self {
2604 unsafe {
2607 self.unchecked_shr(rhs & (Self::BITS - 1))
2608 }
2609 }
2610
2611 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_abs(), 100);")]
2622 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_abs(), 100);")]
2623 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_abs(), ", stringify!($SelfT), "::MIN);")]
2624 #[stable(feature = "no_panic_abs", since = "1.13.0")]
2627 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2628 #[must_use = "this returns the result of the operation, \
2629 without modifying the original"]
2630 #[inline]
2631 #[ferrocene::prevalidated]
2632 pub const fn wrapping_abs(self) -> Self {
2633 if self.is_negative() {
2634 self.wrapping_neg()
2635 } else {
2636 self
2637 }
2638 }
2639
2640 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".unsigned_abs(), 100", stringify!($UnsignedT), ");")]
2648 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").unsigned_abs(), 100", stringify!($UnsignedT), ");")]
2649 #[stable(feature = "unsigned_abs", since = "1.51.0")]
2652 #[rustc_const_stable(feature = "unsigned_abs", since = "1.51.0")]
2653 #[must_use = "this returns the result of the operation, \
2654 without modifying the original"]
2655 #[inline]
2656 #[ferrocene::prevalidated]
2657 pub const fn unsigned_abs(self) -> $UnsignedT {
2658 self.wrapping_abs() as $UnsignedT
2659 }
2660
2661 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".wrapping_pow(4), 81);")]
2668 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".wrapping_pow(0), 1);")]
2671 #[stable(feature = "no_panic_pow", since = "1.34.0")]
2673 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2674 #[must_use = "this returns the result of the operation, \
2675 without modifying the original"]
2676 #[inline]
2677 pub const fn wrapping_pow(self, exp: u32) -> Self {
2678 let (a, _) = self.overflowing_pow(exp);
2679 a
2680 }
2681
2682 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_add(2), (7, false));")]
2693 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.overflowing_add(1), (", stringify!($SelfT), "::MIN, true));")]
2694 #[stable(feature = "wrapping", since = "1.7.0")]
2696 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2697 #[must_use = "this returns the result of the operation, \
2698 without modifying the original"]
2699 #[inline(always)]
2700 #[ferrocene::prevalidated]
2701 pub const fn overflowing_add(self, rhs: Self) -> (Self, bool) {
2702 let (a, b) = intrinsics::add_with_overflow(self as $ActualT, rhs as $ActualT);
2703 (a as Self, b)
2704 }
2705
2706 #[doc = concat!("[`", stringify!($UnsignedT), "::carrying_add`]")]
2718 #[doc = concat!("// 10 MAX (a = 10 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")]
2736 #[doc = concat!("// + -5 9 (b = -5 × 2^", stringify!($BITS), " + 9)")]
2737 #[doc = concat!("// 6 8 (sum = 6 × 2^", stringify!($BITS), " + 8)")]
2739 #[doc = concat!("let (a1, a0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (10, ", stringify!($UnsignedT), "::MAX);")]
2741 #[doc = concat!("let (b1, b0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (-5, 9);")]
2742 #[doc = concat!("// ", stringify!($UnsignedT), "::carrying_add for the less significant words")]
2745 #[doc = concat!("// ", stringify!($SelfT), "::carrying_add for the most significant word")]
2749 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2755 #[must_use = "this returns the result of the operation, \
2756 without modifying the original"]
2757 #[inline]
2758 pub const fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool) {
2759 let (a, b) = self.overflowing_add(rhs);
2762 let (c, d) = a.overflowing_add(carry as $SelfT);
2763 (c, b != d)
2764 }
2765
2766 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_add_unsigned(2), (3, false));")]
2776 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_add_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MAX, false));")]
2777 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).overflowing_add_unsigned(3), (", stringify!($SelfT), "::MIN, true));")]
2778 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2780 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2781 #[must_use = "this returns the result of the operation, \
2782 without modifying the original"]
2783 #[inline]
2784 #[ferrocene::prevalidated]
2785 pub const fn overflowing_add_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
2786 let rhs = rhs as Self;
2787 let (res, overflowed) = self.overflowing_add(rhs);
2788 (res, overflowed ^ (rhs < 0))
2789 }
2790
2791 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_sub(2), (3, false));")]
2801 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_sub(1), (", stringify!($SelfT), "::MAX, true));")]
2802 #[stable(feature = "wrapping", since = "1.7.0")]
2804 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2805 #[must_use = "this returns the result of the operation, \
2806 without modifying the original"]
2807 #[inline(always)]
2808 #[ferrocene::prevalidated]
2809 pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
2810 let (a, b) = intrinsics::sub_with_overflow(self as $ActualT, rhs as $ActualT);
2811 (a as Self, b)
2812 }
2813
2814 #[doc = concat!("[`", stringify!($UnsignedT), "::borrowing_sub`]")]
2827 #[doc = concat!("// 6 8 (a = 6 × 2^", stringify!($BITS), " + 8)")]
2845 #[doc = concat!("// - -5 9 (b = -5 × 2^", stringify!($BITS), " + 9)")]
2846 #[doc = concat!("// 10 MAX (diff = 10 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")]
2848 #[doc = concat!("let (a1, a0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (6, 8);")]
2850 #[doc = concat!("let (b1, b0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (-5, 9);")]
2851 #[doc = concat!("// ", stringify!($UnsignedT), "::borrowing_sub for the less significant words")]
2854 #[doc = concat!("// ", stringify!($SelfT), "::borrowing_sub for the most significant word")]
2858 #[doc = concat!("assert_eq!((diff1, diff0), (10, ", stringify!($UnsignedT), "::MAX));")]
2862 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2864 #[must_use = "this returns the result of the operation, \
2865 without modifying the original"]
2866 #[inline]
2867 pub const fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
2868 let (a, b) = self.overflowing_sub(rhs);
2871 let (c, d) = a.overflowing_sub(borrow as $SelfT);
2872 (c, b != d)
2873 }
2874
2875 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_sub_unsigned(2), (-1, false));")]
2885 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX).overflowing_sub_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MIN, false));")]
2886 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).overflowing_sub_unsigned(3), (", stringify!($SelfT), "::MAX, true));")]
2887 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2889 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2890 #[must_use = "this returns the result of the operation, \
2891 without modifying the original"]
2892 #[inline]
2893 #[ferrocene::prevalidated]
2894 pub const fn overflowing_sub_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
2895 let rhs = rhs as Self;
2896 let (res, overflowed) = self.overflowing_sub(rhs);
2897 (res, overflowed ^ (rhs < 0))
2898 }
2899
2900 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_mul(2), (10, false));")]
2909 #[stable(feature = "wrapping", since = "1.7.0")]
2912 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2913 #[must_use = "this returns the result of the operation, \
2914 without modifying the original"]
2915 #[inline(always)]
2916 #[ferrocene::prevalidated]
2917 pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
2918 let (a, b) = intrinsics::mul_with_overflow(self as $ActualT, rhs as $ActualT);
2919 (a as Self, b)
2920 }
2921
2922 #[doc = concat!("assert_eq!(",
2943 stringify!($SelfT), "::MAX.carrying_mul(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
2944 "(", stringify!($SelfT), "::MAX.unsigned_abs() + 1, ", stringify!($SelfT), "::MAX / 2));"
2945 )]
2946 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2948 #[rustc_const_unstable(feature = "signed_bigint_helpers", issue = "151989")]
2949 #[must_use = "this returns the result of the operation, \
2950 without modifying the original"]
2951 #[inline]
2952 pub const fn carrying_mul(self, rhs: Self, carry: Self) -> ($UnsignedT, Self) {
2953 Self::carrying_mul_add(self, rhs, carry, 0)
2954 }
2955
2956 #[doc = concat!("assert_eq!(",
2979 stringify!($SelfT), "::MAX.carrying_mul_add(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
2980 "(", stringify!($UnsignedT), "::MAX, ", stringify!($SelfT), "::MAX / 2));"
2981 )]
2982 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2984 #[rustc_const_unstable(feature = "signed_bigint_helpers", issue = "151989")]
2985 #[must_use = "this returns the result of the operation, \
2986 without modifying the original"]
2987 #[inline]
2988 pub const fn carrying_mul_add(self, rhs: Self, carry: Self, add: Self) -> ($UnsignedT, Self) {
2989 intrinsics::carrying_mul_add(self, rhs, carry, add)
2990 }
2991
2992 #[ferrocene::prevalidated]
3005 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div(2), (2, false));")]
3006 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div(-1), (", stringify!($SelfT), "::MIN, true));")]
3007 #[inline]
3009 #[stable(feature = "wrapping", since = "1.7.0")]
3010 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
3011 #[must_use = "this returns the result of the operation, \
3012 without modifying the original"]
3013 #[track_caller]
3014 pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
3015 if intrinsics::unlikely((self == Self::MIN) & (rhs == -1)) {
3017 (self, true)
3018 } else {
3019 (self / rhs, false)
3020 }
3021 }
3022
3023 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div_euclid(2), (2, false));")]
3036 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringify!($SelfT), "::MIN, true));")]
3037 #[inline]
3039 #[stable(feature = "euclidean_division", since = "1.38.0")]
3040 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3041 #[must_use = "this returns the result of the operation, \
3042 without modifying the original"]
3043 #[track_caller]
3044 pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
3045 if intrinsics::unlikely((self == Self::MIN) & (rhs == -1)) {
3047 (self, true)
3048 } else {
3049 (self.div_euclid(rhs), false)
3050 }
3051 }
3052
3053 #[ferrocene::prevalidated]
3066 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem(2), (1, false));")]
3067 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem(-1), (0, true));")]
3068 #[inline]
3070 #[stable(feature = "wrapping", since = "1.7.0")]
3071 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
3072 #[must_use = "this returns the result of the operation, \
3073 without modifying the original"]
3074 #[track_caller]
3075 pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
3076 if intrinsics::unlikely(rhs == -1) {
3077 (0, self == Self::MIN)
3078 } else {
3079 (self % rhs, false)
3080 }
3081 }
3082
3083 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem_euclid(2), (1, false));")]
3096 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));")]
3097 #[stable(feature = "euclidean_division", since = "1.38.0")]
3099 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3100 #[must_use = "this returns the result of the operation, \
3101 without modifying the original"]
3102 #[inline]
3103 #[track_caller]
3104 pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
3105 if intrinsics::unlikely(rhs == -1) {
3106 (0, self == Self::MIN)
3107 } else {
3108 (self.rem_euclid(rhs), false)
3109 }
3110 }
3111
3112 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2, false));")]
3122 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($SelfT), "::MIN, true));")]
3123 #[ferrocene::prevalidated]
3125 #[inline]
3126 #[stable(feature = "wrapping", since = "1.7.0")]
3127 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3128 #[must_use = "this returns the result of the operation, \
3129 without modifying the original"]
3130 pub const fn overflowing_neg(self) -> (Self, bool) {
3131 if intrinsics::unlikely(self == Self::MIN) {
3132 (Self::MIN, true)
3133 } else {
3134 (-self, false)
3135 }
3136 }
3137
3138 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT),".overflowing_shl(4), (0x10, false));")]
3148 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shl(", stringify!($BITS_MINUS_ONE), "), (0, false));")]
3150 #[stable(feature = "wrapping", since = "1.7.0")]
3152 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3153 #[must_use = "this returns the result of the operation, \
3154 without modifying the original"]
3155 #[inline]
3156 pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
3157 (self.wrapping_shl(rhs), rhs >= Self::BITS)
3158 }
3159
3160 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shr(4), (0x1, false));")]
3170 #[stable(feature = "wrapping", since = "1.7.0")]
3173 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3174 #[must_use = "this returns the result of the operation, \
3175 without modifying the original"]
3176 #[inline]
3177 pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
3178 (self.wrapping_shr(rhs), rhs >= Self::BITS)
3179 }
3180
3181 #[doc = concat!("(e.g., [`", stringify!($SelfT), "::MIN`] for values of type [`", stringify!($SelfT), "`]),")]
3186 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".overflowing_abs(), (10, false));")]
3193 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").overflowing_abs(), (10, false));")]
3194 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_abs(), (", stringify!($SelfT), "::MIN, true));")]
3195 #[stable(feature = "no_panic_abs", since = "1.13.0")]
3197 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3198 #[must_use = "this returns the result of the operation, \
3199 without modifying the original"]
3200 #[inline]
3201 pub const fn overflowing_abs(self) -> (Self, bool) {
3202 (self.wrapping_abs(), self == Self::MIN)
3203 }
3204
3205 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".overflowing_pow(4), (81, false));")]
3214 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".overflowing_pow(0), (1, false));")]
3215 #[stable(feature = "no_panic_pow", since = "1.34.0")]
3218 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
3219 #[must_use = "this returns the result of the operation, \
3220 without modifying the original"]
3221 #[inline]
3222 pub const fn overflowing_pow(self, mut exp: u32) -> (Self, bool) {
3223 let mut base = self;
3224 let mut acc: Self = 1;
3225 let mut overflow = false;
3226 let mut tmp_overflow;
3227
3228 if intrinsics::is_val_statically_known(base) && base.unsigned_abs().is_power_of_two() {
3229 let k = base.unsigned_abs().ilog2();
3230 let Some(shift) = k.checked_mul(exp) else {
3231 return (0, true)
3232 };
3233 let base: Self = if base < 0 && (exp % 2) != 0 { -1 } else { 1 };
3234 return (base.unbounded_shl(shift), base.shl_exact(shift).is_none());
3235 }
3236
3237 if exp == 0 {
3238 return (1, false);
3239 }
3240
3241 if intrinsics::is_val_statically_known(exp) {
3242 while exp > 1 {
3243 if (exp & 1) == 1 {
3244 (acc, tmp_overflow) = acc.overflowing_mul(base);
3245 overflow |= tmp_overflow;
3246 }
3247 exp /= 2;
3248 (base, tmp_overflow) = base.overflowing_mul(base);
3249 overflow |= tmp_overflow;
3250 }
3251
3252 (acc, tmp_overflow) = acc.overflowing_mul(base);
3257 overflow |= tmp_overflow;
3258 return (acc, overflow);
3259 }
3260
3261 loop {
3262 if (exp & 1) == 1 {
3263 (acc, tmp_overflow) = acc.overflowing_mul(base);
3264 overflow |= tmp_overflow;
3265 if exp == 1 {
3267 return (acc, overflow);
3268 }
3269 }
3270 exp /= 2;
3271 (base, tmp_overflow) = base.overflowing_mul(base);
3272 overflow |= tmp_overflow;
3273 }
3274 }
3275
3276 #[doc = concat!("let x: ", stringify!($SelfT), " = 2; // or any other integer type")]
3282 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".pow(0), 1);")]
3285 #[stable(feature = "rust1", since = "1.0.0")]
3287 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
3288 #[must_use = "this returns the result of the operation, \
3289 without modifying the original"]
3290 #[inline]
3291 #[rustc_inherit_overflow_checks]
3292 pub const fn pow(self, exp: u32) -> Self {
3293 if intrinsics::overflow_checks() {
3294 self.strict_pow(exp)
3295 } else {
3296 self.wrapping_pow(exp)
3297 }
3298 }
3299
3300 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".isqrt(), 3);")]
3314 #[stable(feature = "isqrt", since = "1.84.0")]
3316 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
3317 #[must_use = "this returns the result of the operation, \
3318 without modifying the original"]
3319 #[inline]
3320 #[track_caller]
3321 pub const fn isqrt(self) -> Self {
3322 match self.checked_isqrt() {
3323 Some(sqrt) => sqrt,
3324 None => imp::int_sqrt::panic_for_negative_argument(),
3325 }
3326 }
3327
3328 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
3349 #[stable(feature = "euclidean_division", since = "1.38.0")]
3357 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3358 #[must_use = "this returns the result of the operation, \
3359 without modifying the original"]
3360 #[inline]
3361 #[track_caller]
3362 pub const fn div_euclid(self, rhs: Self) -> Self {
3363 let q = self / rhs;
3364 if self % rhs < 0 {
3365 return if rhs > 0 { q - 1 } else { q + 1 }
3366 }
3367 q
3368 }
3369
3370 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
3386 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.rem_euclid(-1);")]
3397 #[doc(alias = "modulo", alias = "mod")]
3399 #[stable(feature = "euclidean_division", since = "1.38.0")]
3400 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3401 #[must_use = "this returns the result of the operation, \
3402 without modifying the original"]
3403 #[inline]
3404 #[track_caller]
3405 pub const fn rem_euclid(self, rhs: Self) -> Self {
3406 let r = self % rhs;
3407 if r < 0 {
3408 r.wrapping_add(rhs.wrapping_abs())
3417 } else {
3418 r
3419 }
3420 }
3421
3422 #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3434 #[unstable(feature = "int_roundings", issue = "88581")]
3442 #[must_use = "this returns the result of the operation, \
3443 without modifying the original"]
3444 #[inline]
3445 #[track_caller]
3446 pub const fn div_floor(self, rhs: Self) -> Self {
3447 let d = self / rhs;
3448 let r = self % rhs;
3449
3450 let correction = (self ^ rhs) >> (Self::BITS - 1);
3457 if r != 0 {
3458 d + correction
3459 } else {
3460 d
3461 }
3462 }
3463
3464 #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3476 #[unstable(feature = "int_roundings", issue = "88581")]
3484 #[must_use = "this returns the result of the operation, \
3485 without modifying the original"]
3486 #[inline]
3487 #[track_caller]
3488 pub const fn div_ceil(self, rhs: Self) -> Self {
3489 let d = self / rhs;
3490 let r = self % rhs;
3491
3492 let correction = 1 + ((self ^ rhs) >> (Self::BITS - 1));
3495 if r != 0 {
3496 d + correction
3497 } else {
3498 d
3499 }
3500 }
3501
3502 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(8), 16);")]
3521 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(8), 24);")]
3522 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
3523 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
3524 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
3525 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
3526 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(-8), -16);")]
3527 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(-8), -24);")]
3528 #[unstable(feature = "int_roundings", issue = "88581")]
3530 #[must_use = "this returns the result of the operation, \
3531 without modifying the original"]
3532 #[inline]
3533 #[rustc_inherit_overflow_checks]
3534 pub const fn next_multiple_of(self, rhs: Self) -> Self {
3535 if rhs == -1 {
3537 return self;
3538 }
3539
3540 let r = self % rhs;
3541 let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
3542 r + rhs
3543 } else {
3544 r
3545 };
3546
3547 if m == 0 {
3548 self
3549 } else {
3550 self + (rhs - m)
3551 }
3552 }
3553
3554 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(16));")]
3565 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(24));")]
3566 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
3567 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
3568 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
3569 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
3570 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-16));")]
3571 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-24));")]
3572 #[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".checked_next_multiple_of(0), None);")]
3573 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_next_multiple_of(2), None);")]
3574 #[unstable(feature = "int_roundings", issue = "88581")]
3576 #[must_use = "this returns the result of the operation, \
3577 without modifying the original"]
3578 #[inline]
3579 pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
3580 if rhs == -1 {
3582 return Some(self);
3583 }
3584
3585 let r = try_opt!(self.checked_rem(rhs));
3586 let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
3587 r + rhs
3589 } else {
3590 r
3591 };
3592
3593 if m == 0 {
3594 Some(self)
3595 } else {
3596 self.checked_add(rhs - m)
3598 }
3599 }
3600
3601 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".ilog(5), 1);")]
3617 #[stable(feature = "int_log", since = "1.67.0")]
3619 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3620 #[must_use = "this returns the result of the operation, \
3621 without modifying the original"]
3622 #[inline]
3623 #[track_caller]
3624 #[ferrocene::prevalidated]
3625 pub const fn ilog(self, base: Self) -> u32 {
3626 assert!(base >= 2, "base of integer logarithm must be at least 2");
3627 if let Some(log) = self.checked_ilog(base) {
3628 log
3629 } else {
3630 imp::int_log10::panic_for_nonpositive_argument()
3631 }
3632 }
3633
3634 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".ilog2(), 1);")]
3644 #[stable(feature = "int_log", since = "1.67.0")]
3646 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3647 #[must_use = "this returns the result of the operation, \
3648 without modifying the original"]
3649 #[inline]
3650 #[track_caller]
3651 #[ferrocene::prevalidated]
3652 pub const fn ilog2(self) -> u32 {
3653 if let Some(log) = self.checked_ilog2() {
3654 log
3655 } else {
3656 imp::int_log10::panic_for_nonpositive_argument()
3657 }
3658 }
3659
3660 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".ilog10(), 1);")]
3670 #[stable(feature = "int_log", since = "1.67.0")]
3672 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3673 #[must_use = "this returns the result of the operation, \
3674 without modifying the original"]
3675 #[inline]
3676 #[track_caller]
3677 pub const fn ilog10(self) -> u32 {
3678 if let Some(log) = self.checked_ilog10() {
3679 log
3680 } else {
3681 imp::int_log10::panic_for_nonpositive_argument()
3682 }
3683 }
3684
3685 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_ilog(5), Some(1));")]
3698 #[stable(feature = "int_log", since = "1.67.0")]
3700 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3701 #[must_use = "this returns the result of the operation, \
3702 without modifying the original"]
3703 #[inline]
3704 #[ferrocene::prevalidated]
3705 pub const fn checked_ilog(self, base: Self) -> Option<u32> {
3706 if self <= 0 || base <= 1 {
3707 None
3708 } else {
3709 (self as $UnsignedT).checked_ilog(base as $UnsignedT)
3712 }
3713 }
3714
3715 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_ilog2(), Some(1));")]
3726 #[stable(feature = "int_log", since = "1.67.0")]
3728 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3729 #[must_use = "this returns the result of the operation, \
3730 without modifying the original"]
3731 #[inline]
3732 #[ferrocene::prevalidated]
3733 pub const fn checked_ilog2(self) -> Option<u32> {
3734 if self <= 0 {
3735 None
3736 } else {
3737 let log = (Self::BITS - 1) - unsafe { intrinsics::ctlz_nonzero(self) as u32 };
3739 Some(log)
3740 }
3741 }
3742
3743 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_ilog10(), Some(1));")]
3751 #[stable(feature = "int_log", since = "1.67.0")]
3753 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3754 #[must_use = "this returns the result of the operation, \
3755 without modifying the original"]
3756 #[inline]
3757 pub const fn checked_ilog10(self) -> Option<u32> {
3758 imp::int_log10::$ActualT(self as $ActualT)
3759 }
3760
3761 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
3767 #[doc = concat!("`", stringify!($SelfT), "`,")]
3769 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
3773 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".abs(), 10);")]
3780 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").abs(), 10);")]
3781 #[stable(feature = "rust1", since = "1.0.0")]
3783 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3784 #[must_use = "this returns the result of the operation, \
3785 without modifying the original"]
3786 #[inline]
3787 #[rustc_inherit_overflow_checks]
3788 #[ferrocene::prevalidated]
3789 pub const fn abs(self) -> Self {
3790 if self.is_negative() {
3794 -self
3795 } else {
3796 self
3797 }
3798 }
3799
3800 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(80), 20", stringify!($UnsignedT), ");")]
3809 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(110), 10", stringify!($UnsignedT), ");")]
3810 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(80), 180", stringify!($UnsignedT), ");")]
3811 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(-120), 20", stringify!($UnsignedT), ");")]
3812 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.abs_diff(", stringify!($SelfT), "::MAX), ", stringify!($UnsignedT), "::MAX);")]
3813 #[stable(feature = "int_abs_diff", since = "1.60.0")]
3815 #[rustc_const_stable(feature = "int_abs_diff", since = "1.60.0")]
3816 #[must_use = "this returns the result of the operation, \
3817 without modifying the original"]
3818 #[inline]
3819 #[ferrocene::prevalidated]
3820 pub const fn abs_diff(self, other: Self) -> $UnsignedT {
3821 if self < other {
3822 (other as $UnsignedT).wrapping_sub(self as $UnsignedT)
3836 } else {
3837 (self as $UnsignedT).wrapping_sub(other as $UnsignedT)
3838 }
3839 }
3840
3841 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".signum(), 1);")]
3851 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".signum(), 0);")]
3852 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").signum(), -1);")]
3853 #[stable(feature = "rust1", since = "1.0.0")]
3855 #[rustc_const_stable(feature = "const_int_sign", since = "1.47.0")]
3856 #[must_use = "this returns the result of the operation, \
3857 without modifying the original"]
3858 #[inline(always)]
3859 pub const fn signum(self) -> Self {
3860 crate::intrinsics::three_way_compare(self, 0) as Self
3866 }
3867
3868 #[doc = concat!("assert!(10", stringify!($SelfT), ".is_positive());")]
3875 #[doc = concat!("assert!(!(-10", stringify!($SelfT), ").is_positive());")]
3876 #[must_use]
3878 #[stable(feature = "rust1", since = "1.0.0")]
3879 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3880 #[inline(always)]
3881 pub const fn is_positive(self) -> bool { self > 0 }
3882
3883 #[doc = concat!("assert!((-10", stringify!($SelfT), ").is_negative());")]
3890 #[doc = concat!("assert!(!10", stringify!($SelfT), ".is_negative());")]
3891 #[must_use]
3893 #[stable(feature = "rust1", since = "1.0.0")]
3894 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3895 #[inline(always)]
3896 #[ferrocene::prevalidated]
3897 pub const fn is_negative(self) -> bool { self < 0 }
3898
3899 #[doc = $to_xe_bytes_doc]
3903 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();")]
3908 #[doc = concat!("assert_eq!(bytes, ", $be_bytes, ");")]
3909 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3911 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3912 #[must_use = "this returns the result of the operation, \
3913 without modifying the original"]
3914 #[inline]
3915 pub const fn to_be_bytes(self) -> [u8; size_of::<Self>()] {
3916 self.to_be().to_ne_bytes()
3917 }
3918
3919 #[doc = $to_xe_bytes_doc]
3923 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();")]
3928 #[doc = concat!("assert_eq!(bytes, ", $le_bytes, ");")]
3929 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3931 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3932 #[must_use = "this returns the result of the operation, \
3933 without modifying the original"]
3934 #[inline]
3935 #[ferrocene::prevalidated]
3936 pub const fn to_le_bytes(self) -> [u8; size_of::<Self>()] {
3937 self.to_le().to_ne_bytes()
3938 }
3939
3940 #[doc = $to_xe_bytes_doc]
3948 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_ne_bytes();")]
3956 #[doc = concat!(" ", $be_bytes)]
3960 #[doc = concat!(" ", $le_bytes)]
3962 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3966 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3967 #[allow(unnecessary_transmutes)]
3968 #[must_use = "this returns the result of the operation, \
3971 without modifying the original"]
3972 #[inline]
3973 #[ferrocene::prevalidated]
3974 pub const fn to_ne_bytes(self) -> [u8; size_of::<Self>()] {
3975 unsafe { mem::transmute(self) }
3978 }
3979
3980 #[doc = $from_xe_bytes_doc]
3984 #[doc = concat!("let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, ");")]
3989 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3990 #[doc = concat!("fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3996 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3997 #[doc = concat!(" ", stringify!($SelfT), "::from_be_bytes(int_bytes.try_into().unwrap())")]
3999 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
4002 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
4003 #[must_use]
4004 #[inline]
4005 pub const fn from_be_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
4006 Self::from_be(Self::from_ne_bytes(bytes))
4007 }
4008
4009 #[doc = $from_xe_bytes_doc]
4013 #[doc = concat!("let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, ");")]
4018 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
4019 #[doc = concat!("fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
4025 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
4026 #[doc = concat!(" ", stringify!($SelfT), "::from_le_bytes(int_bytes.try_into().unwrap())")]
4028 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
4031 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
4032 #[must_use]
4033 #[inline]
4034 #[ferrocene::prevalidated]
4035 pub const fn from_le_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
4036 Self::from_le(Self::from_ne_bytes(bytes))
4037 }
4038
4039 #[doc = $from_xe_bytes_doc]
4050 #[doc = concat!("let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"big\") {")]
4055 #[doc = concat!(" ", $be_bytes)]
4056 #[doc = concat!(" ", $le_bytes)]
4058 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
4060 #[doc = concat!("fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
4066 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
4067 #[doc = concat!(" ", stringify!($SelfT), "::from_ne_bytes(int_bytes.try_into().unwrap())")]
4069 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
4072 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
4073 #[allow(unnecessary_transmutes)]
4074 #[must_use]
4075 #[inline]
4078 #[ferrocene::prevalidated]
4079 pub const fn from_ne_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
4080 unsafe { mem::transmute(bytes) }
4082 }
4083
4084 #[doc = concat!("[`", stringify!($SelfT), "::MIN", "`] instead.")]
4086 #[stable(feature = "rust1", since = "1.0.0")]
4089 #[inline(always)]
4090 #[rustc_promotable]
4091 #[rustc_const_stable(feature = "const_min_value", since = "1.32.0")]
4092 #[deprecated(since = "1.99.0", note = "replaced by the `MIN` associated constant on this type")]
4093 #[rustc_diagnostic_item = concat!(stringify!($SelfT), "_legacy_fn_min_value")]
4094 pub const fn min_value() -> Self {
4095 Self::MIN
4096 }
4097
4098 #[doc = concat!("[`", stringify!($SelfT), "::MAX", "`] instead.")]
4100 #[stable(feature = "rust1", since = "1.0.0")]
4103 #[inline(always)]
4104 #[rustc_promotable]
4105 #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
4106 #[deprecated(since = "1.99.0", note = "replaced by the `MAX` associated constant on this type")]
4107 #[rustc_diagnostic_item = concat!(stringify!($SelfT), "_legacy_fn_max_value")]
4108 pub const fn max_value() -> Self {
4109 Self::MAX
4110 }
4111
4112 #[doc = concat!("assert_eq!(120", stringify!($SelfT), ".clamp_magnitude(100), 100);")]
4124 #[doc = concat!("assert_eq!((-120", stringify!($SelfT), ").clamp_magnitude(100), -100);")]
4125 #[doc = concat!("assert_eq!(80", stringify!($SelfT), ".clamp_magnitude(100), 80);")]
4126 #[doc = concat!("assert_eq!((-80", stringify!($SelfT), ").clamp_magnitude(100), -80);")]
4127 #[inline]
4129 #[must_use = "method returns a new number and does not mutate the original value"]
4130 #[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
4131 #[unstable(feature = "clamp_magnitude", issue = "148519")]
4132 pub const fn clamp_magnitude(self, limit: $UnsignedT) -> Self {
4133 if let Ok(limit) = core::convert::TryInto::<$SelfT>::try_into(limit) {
4134 self.clamp(-limit, limit)
4135 } else {
4136 self
4137 }
4138 }
4139
4140 #[doc = concat!("assert_eq!(120i8, 120", stringify!($SelfT), ".truncate());")]
4148 #[doc = concat!("assert_eq!(-120i8, (-120", stringify!($SelfT), ").truncate());")]
4149 #[must_use = "this returns the truncated value and does not modify the original"]
4152 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
4153 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
4154 #[inline]
4155 pub const fn truncate<Target>(self) -> Target
4156 where Self: [const] traits::TruncateTarget<Target>
4157 {
4158 traits::TruncateTarget::internal_truncate(self)
4159 }
4160
4161 #[doc = concat!("assert_eq!(120i8, 120", stringify!($SelfT), ".saturating_truncate());")]
4169 #[doc = concat!("assert_eq!(-120i8, (-120", stringify!($SelfT), ").saturating_truncate());")]
4170 #[must_use = "this returns the truncated value and does not modify the original"]
4174 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
4175 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
4176 #[inline]
4177 pub const fn saturating_truncate<Target>(self) -> Target
4178 where Self: [const] traits::TruncateTarget<Target>
4179 {
4180 traits::TruncateTarget::internal_saturating_truncate(self)
4181 }
4182
4183 #[doc = concat!("assert_eq!(Some(120i8), 120", stringify!($SelfT), ".checked_truncate());")]
4191 #[doc = concat!("assert_eq!(Some(-120i8), (-120", stringify!($SelfT), ").checked_truncate());")]
4192 #[must_use = "this returns the truncated value and does not modify the original"]
4196 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
4197 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
4198 #[inline]
4199 pub const fn checked_truncate<Target>(self) -> Option<Target>
4200 where Self: [const] traits::TruncateTarget<Target>
4201 {
4202 traits::TruncateTarget::internal_checked_truncate(self)
4203 }
4204
4205 #[doc = concat!("assert_eq!(120i128, 120i8.widen());")]
4212 #[doc = concat!("assert_eq!(-120i128, (-120i8).widen());")]
4213 #[must_use = "this returns the widened value and does not modify the original"]
4215 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
4216 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
4217 #[inline]
4218 pub const fn widen<Target>(self) -> Target
4219 where Self: [const] traits::WidenTarget<Target>
4220 {
4221 traits::WidenTarget::internal_widen(self)
4222 }
4223
4224 #[doc = concat!("assert_eq!(i8::MAX, ", stringify!($SelfT), "::MAX.saturating_cast());")]
4232 #[doc = concat!("assert_eq!(i8::MIN, ", stringify!($SelfT), "::MIN.saturating_cast());")]
4233 #[doc = concat!("assert_eq!(42u8, 42", stringify!($SelfT), ".saturating_cast());")]
4234 #[doc = concat!("assert_eq!(0u8, (-42", stringify!($SelfT), ").saturating_cast());")]
4235 #[must_use = "this returns the cast result and does not modify the original"]
4237 #[unstable(feature = "integer_casts", issue = "157388")]
4238 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4239 #[inline(always)]
4240 pub const fn saturating_cast<T: [const] BoundedCastFromInt<Self>>(self) -> T {
4241 T::saturating_cast_from(self)
4242 }
4243
4244 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX as i8, ", stringify!($SelfT), "::MAX.wrapping_cast());")]
4252 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN as i8, ", stringify!($SelfT), "::MIN.wrapping_cast());")]
4253 #[doc = concat!("assert_eq!(42u8, 42", stringify!($SelfT), ".wrapping_cast());")]
4254 #[doc = concat!("assert_eq!(u8::MAX - 41, (-42", stringify!($SelfT), ").wrapping_cast());")]
4255 #[must_use = "this returns the cast result and does not modify the original"]
4257 #[unstable(feature = "integer_casts", issue = "157388")]
4258 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4259 #[inline(always)]
4260 pub const fn wrapping_cast<T: [const] BoundedCastFromInt<Self>>(self) -> T {
4261 T::wrapping_cast_from(self)
4262 }
4263
4264 #[doc = concat!("assert_eq!(Some(42u8), 42", stringify!($SelfT), ".checked_cast());")]
4272 #[doc = concat!("assert_eq!((-42", stringify!($SelfT), ").checked_cast::<u8>(), None);")]
4273 #[must_use = "this returns the cast result and does not modify the original"]
4275 #[unstable(feature = "integer_casts", issue = "157388")]
4276 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4277 #[inline(always)]
4278 pub const fn checked_cast<T: [const] CheckedCastFromInt<Self>>(self) -> Option<T> {
4279 T::checked_cast_from(self)
4280 }
4281
4282 #[doc = concat!("assert_eq!(42u8, 42", stringify!($SelfT), ".strict_cast());")]
4294 #[doc = concat!("let _ = (-42", stringify!($SelfT), ").strict_cast::<u8>();")]
4301 #[must_use = "this returns the cast result and does not modify the original"]
4303 #[unstable(feature = "integer_casts", issue = "157388")]
4304 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4305 #[inline(always)]
4306 #[track_caller]
4307 pub const fn strict_cast<T: [const] CheckedCastFromInt<Self>>(self) -> T {
4308 T::strict_cast_from(self)
4309 }
4310
4311 #[must_use = "this returns the cast result and does not modify the original"]
4319 #[unstable(feature = "integer_casts", issue = "157388")]
4320 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4321 #[inline(always)]
4322 pub const unsafe fn unchecked_cast<T: [const] CheckedCastFromInt<Self>>(self) -> T {
4323 assert_unsafe_precondition!(
4324 check_language_ub,
4325 concat!(stringify!($SelfT), "::unchecked_cast must fit in the target type"),
4326 (
4327 in_bounds: bool = {
4329 let cast_val = self.checked_cast::<T>();
4330 let ret = cast_val.is_some();
4331 core::mem::forget(cast_val); ret
4333 },
4334 ) => in_bounds,
4335 );
4336
4337 unsafe { T::unchecked_cast_from(self) }
4339 }
4340 }
4341}