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 = 0b100_0000", stringify!($SelfT), ";")]
65 #[stable(feature = "rust1", since = "1.0.0")]
70 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
71 #[doc(alias = "popcount")]
72 #[doc(alias = "popcnt")]
73 #[must_use = "this returns the result of the operation, \
74 without modifying the original"]
75 #[inline(always)]
76 pub const fn count_ones(self) -> u32 { (self as $UnsignedT).count_ones() }
77
78 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.count_zeros(), 1);")]
84 #[stable(feature = "rust1", since = "1.0.0")]
86 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
87 #[must_use = "this returns the result of the operation, \
88 without modifying the original"]
89 #[inline(always)]
90 pub const fn count_zeros(self) -> u32 {
91 (!self).count_ones()
92 }
93
94 #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
103 #[doc = concat!("[`ilog2`]: ", stringify!($SelfT), "::ilog2")]
107 #[stable(feature = "rust1", since = "1.0.0")]
108 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
109 #[must_use = "this returns the result of the operation, \
110 without modifying the original"]
111 #[inline(always)]
112 pub const fn leading_zeros(self) -> u32 {
113 (self as $UnsignedT).leading_zeros()
114 }
115
116 #[doc = concat!("let n = -4", stringify!($SelfT), ";")]
122 #[stable(feature = "rust1", since = "1.0.0")]
126 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
127 #[must_use = "this returns the result of the operation, \
128 without modifying the original"]
129 #[inline(always)]
130 pub const fn trailing_zeros(self) -> u32 {
131 (self as $UnsignedT).trailing_zeros()
132 }
133
134 #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
140 #[doc = concat!("assert_eq!(n.leading_ones(), ", stringify!($BITS), ");")]
142 #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
144 #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
145 #[must_use = "this returns the result of the operation, \
146 without modifying the original"]
147 #[inline(always)]
148 pub const fn leading_ones(self) -> u32 {
149 (self as $UnsignedT).leading_ones()
150 }
151
152 #[doc = concat!("let n = 3", stringify!($SelfT), ";")]
158 #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
162 #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
163 #[must_use = "this returns the result of the operation, \
164 without modifying the original"]
165 #[inline(always)]
166 pub const fn trailing_ones(self) -> u32 {
167 (self as $UnsignedT).trailing_ones()
168 }
169
170 #[doc = concat!("let n: ", stringify!($SelfT), " = 0b_01100100;")]
177 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".isolate_highest_one(), 0);")]
180 #[stable(feature = "isolate_most_least_significant_one", since = "1.97.0")]
182 #[rustc_const_stable(feature = "isolate_most_least_significant_one", since = "1.97.0")]
183 #[must_use = "this returns the result of the operation, \
184 without modifying the original"]
185 #[inline(always)]
186 pub const fn isolate_highest_one(self) -> Self {
187 self & (((1 as $SelfT) << (<$SelfT>::BITS - 1)).wrapping_shr(self.leading_zeros()))
188 }
189
190 #[doc = concat!("let n: ", stringify!($SelfT), " = 0b_01100100;")]
197 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".isolate_lowest_one(), 0);")]
200 #[stable(feature = "isolate_most_least_significant_one", since = "1.97.0")]
202 #[rustc_const_stable(feature = "isolate_most_least_significant_one", since = "1.97.0")]
203 #[must_use = "this returns the result of the operation, \
204 without modifying the original"]
205 #[inline(always)]
206 pub const fn isolate_lowest_one(self) -> Self {
207 self & self.wrapping_neg()
208 }
209
210 #[doc = concat!("assert_eq!(0b0_", stringify!($SelfT), ".highest_one(), None);")]
220 #[doc = concat!("assert_eq!(0b1_", stringify!($SelfT), ".highest_one(), Some(0));")]
221 #[doc = concat!("assert_eq!(0b1_0000_", stringify!($SelfT), ".highest_one(), Some(4));")]
222 #[doc = concat!("assert_eq!(0b1_1111_", stringify!($SelfT), ".highest_one(), Some(4));")]
223 #[stable(feature = "int_lowest_highest_one", since = "1.97.0")]
225 #[rustc_const_stable(feature = "int_lowest_highest_one", since = "1.97.0")]
226 #[must_use = "this returns the result of the operation, \
227 without modifying the original"]
228 #[inline(always)]
229 pub const fn highest_one(self) -> Option<u32> {
230 (self as $UnsignedT).highest_one()
231 }
232
233 #[doc = concat!("assert_eq!(0b0_", stringify!($SelfT), ".lowest_one(), None);")]
240 #[doc = concat!("assert_eq!(0b1_", stringify!($SelfT), ".lowest_one(), Some(0));")]
241 #[doc = concat!("assert_eq!(0b1_0000_", stringify!($SelfT), ".lowest_one(), Some(4));")]
242 #[doc = concat!("assert_eq!(0b1_1111_", stringify!($SelfT), ".lowest_one(), Some(0));")]
243 #[stable(feature = "int_lowest_highest_one", since = "1.97.0")]
245 #[rustc_const_stable(feature = "int_lowest_highest_one", since = "1.97.0")]
246 #[must_use = "this returns the result of the operation, \
247 without modifying the original"]
248 #[inline(always)]
249 pub const fn lowest_one(self) -> Option<u32> {
250 (self as $UnsignedT).lowest_one()
251 }
252
253 #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
262 #[doc = concat!("assert_eq!(n.cast_unsigned(), ", stringify!($UnsignedT), "::MAX);")]
264 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
266 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
267 #[must_use = "this returns the result of the operation, \
268 without modifying the original"]
269 #[inline(always)]
270 pub const fn cast_unsigned(self) -> $UnsignedT {
271 self as $UnsignedT
272 }
273
274 #[doc = concat!("let n = ", stringify!($SelfT), "::MIN;")]
288 #[doc = concat!("assert_eq!(n.saturating_cast_unsigned(), 0", stringify!($UnsignedT), ");")]
290 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".saturating_cast_unsigned(), 64", stringify!($UnsignedT), ");")]
291 #[rustc_const_unstable(feature = "integer_cast_extras", issue = "154650")]
293 #[unstable(feature = "integer_cast_extras", issue = "154650")]
294 #[must_use = "this returns the result of the operation, \
295 without modifying the original"]
296 #[inline(always)]
297 pub const fn saturating_cast_unsigned(self) -> $UnsignedT {
298 if self >= 0 {
299 self.cast_unsigned()
300 } else {
301 0
302 }
303 }
304
305 #[doc = concat!("let n = ", stringify!($SelfT), "::MIN;")]
318 #[doc = concat!("assert_eq!(n.checked_cast_unsigned(), None);")]
320 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".checked_cast_unsigned(), Some(64", stringify!($UnsignedT), "));")]
321 #[rustc_const_unstable(feature = "integer_cast_extras", issue = "154650")]
323 #[unstable(feature = "integer_cast_extras", issue = "154650")]
324 #[must_use = "this returns the result of the operation, \
325 without modifying the original"]
326 #[inline(always)]
327 pub const fn checked_cast_unsigned(self) -> Option<$UnsignedT> {
328 if self >= 0 {
329 Some(self.cast_unsigned())
330 } else {
331 None
332 }
333 }
334
335 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_cast_unsigned();")]
348 #[rustc_const_unstable(feature = "integer_cast_extras", issue = "154650")]
350 #[unstable(feature = "integer_cast_extras", issue = "154650")]
351 #[must_use = "this returns the result of the operation, \
352 without modifying the original"]
353 #[inline]
354 #[track_caller]
355 pub const fn strict_cast_unsigned(self) -> $UnsignedT {
356 match self.checked_cast_unsigned() {
357 Some(n) => n,
358 None => imp::overflow_panic::cast_integer(),
359 }
360 }
361
362 #[doc = concat!("let n = ", $rot_op, stringify!($SelfT), ";")]
375 #[doc = concat!("let m = ", $rot_result, ";")]
376 #[doc = concat!("assert_eq!(n.rotate_left(", $rot, "), m);")]
378 #[doc = concat!("assert_eq!(n.rotate_left(1024), n);")]
379 #[stable(feature = "rust1", since = "1.0.0")]
381 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
382 #[must_use = "this returns the result of the operation, \
383 without modifying the original"]
384 #[inline(always)]
385 pub const fn rotate_left(self, n: u32) -> Self {
386 (self as $UnsignedT).rotate_left(n) as Self
387 }
388
389 #[doc = concat!("let n = ", $rot_result, stringify!($SelfT), ";")]
403 #[doc = concat!("let m = ", $rot_op, ";")]
404 #[doc = concat!("assert_eq!(n.rotate_right(", $rot, "), m);")]
406 #[doc = concat!("assert_eq!(n.rotate_right(1024), n);")]
407 #[stable(feature = "rust1", since = "1.0.0")]
409 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
410 #[must_use = "this returns the result of the operation, \
411 without modifying the original"]
412 #[inline(always)]
413 pub const fn rotate_right(self, n: u32) -> Self {
414 (self as $UnsignedT).rotate_right(n) as Self
415 }
416
417 #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
423 #[doc = concat!("assert_eq!(m, ", $swapped, ");")]
427 #[stable(feature = "rust1", since = "1.0.0")]
429 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
430 #[must_use = "this returns the result of the operation, \
431 without modifying the original"]
432 #[inline(always)]
433 pub const fn swap_bytes(self) -> Self {
434 (self as $UnsignedT).swap_bytes() as Self
435 }
436
437 #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
444 #[doc = concat!("assert_eq!(m, ", $reversed, ");")]
447 #[doc = concat!("assert_eq!(0, 0", stringify!($SelfT), ".reverse_bits());")]
448 #[stable(feature = "reverse_bits", since = "1.37.0")]
450 #[rustc_const_stable(feature = "reverse_bits", since = "1.37.0")]
451 #[must_use = "this returns the result of the operation, \
452 without modifying the original"]
453 #[inline(always)]
454 pub const fn reverse_bits(self) -> Self {
455 (self as $UnsignedT).reverse_bits() as Self
456 }
457
458 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
468 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_be(n), n)")]
471 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_be(n), n.swap_bytes())")]
473 #[stable(feature = "rust1", since = "1.0.0")]
476 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
477 #[must_use]
478 #[inline]
479 pub const fn from_be(x: Self) -> Self {
480 cfg_select! {
481 target_endian = "big" => x,
482 _ => x.swap_bytes(),
483 }
484 }
485
486 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
496 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_le(n), n)")]
499 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_le(n), n.swap_bytes())")]
501 #[stable(feature = "rust1", since = "1.0.0")]
504 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
505 #[must_use]
506 #[inline]
507 pub const fn from_le(x: Self) -> Self {
508 cfg_select! {
509 target_endian = "little" => x,
510 _ => x.swap_bytes(),
511 }
512 }
513
514 #[doc = concat!("`", stringify!($SelfT), "`.")]
521 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
528 #[stable(feature = "rust1", since = "1.0.0")]
536 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
537 #[must_use = "this returns the result of the operation, \
538 without modifying the original"]
539 #[inline]
540 pub const fn to_be(self) -> Self { cfg_select! {
542 target_endian = "big" => self,
543 _ => self.swap_bytes(),
544 }
545 }
546
547 #[doc = concat!("`", stringify!($SelfT), "`.")]
554 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
561 #[stable(feature = "rust1", since = "1.0.0")]
569 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
570 #[must_use = "this returns the result of the operation, \
571 without modifying the original"]
572 #[inline]
573 pub const fn to_le(self) -> Self {
574 cfg_select! {
575 target_endian = "little" => self,
576 _ => self.swap_bytes(),
577 }
578 }
579
580 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(1), Some(", stringify!($SelfT), "::MAX - 1));")]
587 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);")]
588 #[stable(feature = "rust1", since = "1.0.0")]
590 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
591 #[must_use = "this returns the result of the operation, \
592 without modifying the original"]
593 #[inline]
594 pub const fn checked_add(self, rhs: Self) -> Option<Self> {
595 let (a, b) = self.overflowing_add(rhs);
596 if intrinsics::unlikely(b) { None } else { Some(a) }
597 }
598
599 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).strict_add(1), ", stringify!($SelfT), "::MAX - 1);")]
612 #[doc = concat!("let _ = (", stringify!($SelfT), "::MAX - 2).strict_add(3);")]
618 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
620 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
621 #[must_use = "this returns the result of the operation, \
622 without modifying the original"]
623 #[inline]
624 #[track_caller]
625 pub const fn strict_add(self, rhs: Self) -> Self {
626 let (a, b) = self.overflowing_add(rhs);
627 if b { imp::overflow_panic::add() } else { a }
628 }
629
630 #[doc = concat!("`self + rhs > ", stringify!($SelfT), "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`,")]
643 #[doc = concat!("[`checked_add`]: ", stringify!($SelfT), "::checked_add")]
647 #[doc = concat!("[`wrapping_add`]: ", stringify!($SelfT), "::wrapping_add")]
648 #[stable(feature = "unchecked_math", since = "1.79.0")]
649 #[rustc_const_stable(feature = "unchecked_math", since = "1.79.0")]
650 #[must_use = "this returns the result of the operation, \
651 without modifying the original"]
652 #[inline(always)]
653 #[track_caller]
654 pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
655 assert_unsafe_precondition!(
656 check_language_ub,
657 concat!(stringify!($SelfT), "::unchecked_add cannot overflow"),
658 (
659 lhs: $SelfT = self,
660 rhs: $SelfT = rhs,
661 ) => !lhs.overflowing_add(rhs).1,
662 );
663
664 unsafe {
666 intrinsics::unchecked_add(self, rhs)
667 }
668 }
669
670 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_add_unsigned(2), Some(3));")]
677 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add_unsigned(3), None);")]
678 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
680 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
681 #[must_use = "this returns the result of the operation, \
682 without modifying the original"]
683 #[inline]
684 pub const fn checked_add_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
685 let (a, b) = self.overflowing_add_unsigned(rhs);
686 if intrinsics::unlikely(b) { None } else { Some(a) }
687 }
688
689 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".strict_add_unsigned(2), 3);")]
702 #[doc = concat!("let _ = (", stringify!($SelfT), "::MAX - 2).strict_add_unsigned(3);")]
708 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
710 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
711 #[must_use = "this returns the result of the operation, \
712 without modifying the original"]
713 #[inline]
714 #[track_caller]
715 pub const fn strict_add_unsigned(self, rhs: $UnsignedT) -> Self {
716 let (a, b) = self.overflowing_add_unsigned(rhs);
717 if b { imp::overflow_panic::add() } else { a }
718 }
719
720 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub(1), Some(", stringify!($SelfT), "::MIN + 1));")]
727 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub(3), None);")]
728 #[stable(feature = "rust1", since = "1.0.0")]
730 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
731 #[must_use = "this returns the result of the operation, \
732 without modifying the original"]
733 #[inline]
734 pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
735 let (a, b) = self.overflowing_sub(rhs);
736 if intrinsics::unlikely(b) { None } else { Some(a) }
737 }
738
739 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).strict_sub(1), ", stringify!($SelfT), "::MIN + 1);")]
752 #[doc = concat!("let _ = (", stringify!($SelfT), "::MIN + 2).strict_sub(3);")]
758 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
760 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
761 #[must_use = "this returns the result of the operation, \
762 without modifying the original"]
763 #[inline]
764 #[track_caller]
765 pub const fn strict_sub(self, rhs: Self) -> Self {
766 let (a, b) = self.overflowing_sub(rhs);
767 if b { imp::overflow_panic::sub() } else { a }
768 }
769
770 #[doc = concat!("`self - rhs > ", stringify!($SelfT), "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`,")]
783 #[doc = concat!("[`checked_sub`]: ", stringify!($SelfT), "::checked_sub")]
787 #[doc = concat!("[`wrapping_sub`]: ", stringify!($SelfT), "::wrapping_sub")]
788 #[stable(feature = "unchecked_math", since = "1.79.0")]
789 #[rustc_const_stable(feature = "unchecked_math", since = "1.79.0")]
790 #[must_use = "this returns the result of the operation, \
791 without modifying the original"]
792 #[inline(always)]
793 #[track_caller]
794 pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
795 assert_unsafe_precondition!(
796 check_language_ub,
797 concat!(stringify!($SelfT), "::unchecked_sub cannot overflow"),
798 (
799 lhs: $SelfT = self,
800 rhs: $SelfT = rhs,
801 ) => !lhs.overflowing_sub(rhs).1,
802 );
803
804 unsafe {
806 intrinsics::unchecked_sub(self, rhs)
807 }
808 }
809
810 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_sub_unsigned(2), Some(-1));")]
817 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub_unsigned(3), None);")]
818 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
820 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
821 #[must_use = "this returns the result of the operation, \
822 without modifying the original"]
823 #[inline]
824 pub const fn checked_sub_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
825 let (a, b) = self.overflowing_sub_unsigned(rhs);
826 if intrinsics::unlikely(b) { None } else { Some(a) }
827 }
828
829 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".strict_sub_unsigned(2), -1);")]
842 #[doc = concat!("let _ = (", stringify!($SelfT), "::MIN + 2).strict_sub_unsigned(3);")]
848 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
850 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
851 #[must_use = "this returns the result of the operation, \
852 without modifying the original"]
853 #[inline]
854 #[track_caller]
855 pub const fn strict_sub_unsigned(self, rhs: $UnsignedT) -> Self {
856 let (a, b) = self.overflowing_sub_unsigned(rhs);
857 if b { imp::overflow_panic::sub() } else { a }
858 }
859
860 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(1), Some(", stringify!($SelfT), "::MAX));")]
867 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);")]
868 #[stable(feature = "rust1", since = "1.0.0")]
870 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
871 #[must_use = "this returns the result of the operation, \
872 without modifying the original"]
873 #[inline]
874 pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
875 let (a, b) = self.overflowing_mul(rhs);
876 if intrinsics::unlikely(b) { None } else { Some(a) }
877 }
878
879 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.strict_mul(1), ", stringify!($SelfT), "::MAX);")]
892 #[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_mul(2);")]
898 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
900 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
901 #[must_use = "this returns the result of the operation, \
902 without modifying the original"]
903 #[inline]
904 #[track_caller]
905 pub const fn strict_mul(self, rhs: Self) -> Self {
906 let (a, b) = self.overflowing_mul(rhs);
907 if b { imp::overflow_panic::mul() } else { a }
908 }
909
910 #[doc = concat!("`self * rhs > ", stringify!($SelfT), "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`,")]
923 #[doc = concat!("[`checked_mul`]: ", stringify!($SelfT), "::checked_mul")]
927 #[doc = concat!("[`wrapping_mul`]: ", stringify!($SelfT), "::wrapping_mul")]
928 #[stable(feature = "unchecked_math", since = "1.79.0")]
929 #[rustc_const_stable(feature = "unchecked_math", since = "1.79.0")]
930 #[must_use = "this returns the result of the operation, \
931 without modifying the original"]
932 #[inline(always)]
933 #[track_caller]
934 pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
935 assert_unsafe_precondition!(
936 check_language_ub,
937 concat!(stringify!($SelfT), "::unchecked_mul cannot overflow"),
938 (
939 lhs: $SelfT = self,
940 rhs: $SelfT = rhs,
941 ) => !lhs.overflowing_mul(rhs).1,
942 );
943
944 unsafe {
946 intrinsics::unchecked_mul(self, rhs)
947 }
948 }
949
950 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div(-1), Some(", stringify!($Max), "));")]
957 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div(-1), None);")]
958 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div(0), None);")]
959 #[stable(feature = "rust1", since = "1.0.0")]
961 #[rustc_const_stable(feature = "const_checked_int_div", since = "1.52.0")]
962 #[must_use = "this returns the result of the operation, \
963 without modifying the original"]
964 #[inline]
965 pub const fn checked_div(self, rhs: Self) -> Option<Self> {
966 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
967 None
968 } else {
969 Some(unsafe { intrinsics::unchecked_div(self, rhs) })
971 }
972 }
973
974 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).strict_div(-1), ", stringify!($Max), ");")]
996 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_div(-1);")]
1002 #[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div(0);")]
1008 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1010 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1011 #[must_use = "this returns the result of the operation, \
1012 without modifying the original"]
1013 #[inline]
1014 #[track_caller]
1015 pub const fn strict_div(self, rhs: Self) -> Self {
1016 self / rhs
1018 }
1019
1020 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div_euclid(-1), Some(", stringify!($Max), "));")]
1027 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div_euclid(-1), None);")]
1028 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div_euclid(0), None);")]
1029 #[stable(feature = "euclidean_division", since = "1.38.0")]
1031 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1032 #[must_use = "this returns the result of the operation, \
1033 without modifying the original"]
1034 #[inline]
1035 pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
1036 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
1038 None
1039 } else {
1040 Some(self.div_euclid(rhs))
1041 }
1042 }
1043
1044 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).strict_div_euclid(-1), ", stringify!($Max), ");")]
1066 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_div_euclid(-1);")]
1072 #[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div_euclid(0);")]
1078 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1080 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1081 #[must_use = "this returns the result of the operation, \
1082 without modifying the original"]
1083 #[inline]
1084 #[track_caller]
1085 pub const fn strict_div_euclid(self, rhs: Self) -> Self {
1086 self.div_euclid(rhs)
1088 }
1089
1090 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div_exact(-1), Some(", stringify!($Max), "));")]
1099 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_div_exact(2), None);")]
1100 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div_exact(-1), None);")]
1101 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div_exact(0), None);")]
1102 #[unstable(
1104 feature = "exact_div",
1105 issue = "139911",
1106 )]
1107 #[must_use = "this returns the result of the operation, \
1108 without modifying the original"]
1109 #[inline]
1110 pub const fn checked_div_exact(self, rhs: Self) -> Option<Self> {
1111 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
1112 None
1113 } else {
1114 unsafe {
1116 if intrinsics::unlikely(intrinsics::unchecked_rem(self, rhs) != 0) {
1117 None
1118 } else {
1119 Some(intrinsics::exact_div(self, rhs))
1120 }
1121 }
1122 }
1123 }
1124
1125 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".div_exact(2), Some(32));")]
1141 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".div_exact(32), Some(2));")]
1142 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).div_exact(-1), Some(", stringify!($Max), "));")]
1143 #[doc = concat!("assert_eq!(65", stringify!($SelfT), ".div_exact(2), None);")]
1144 #[doc = concat!("let _ = 64", stringify!($SelfT),".div_exact(0);")]
1148 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.div_exact(-1);")]
1152 #[unstable(
1154 feature = "exact_div",
1155 issue = "139911",
1156 )]
1157 #[must_use = "this returns the result of the operation, \
1158 without modifying the original"]
1159 #[inline]
1160 #[rustc_inherit_overflow_checks]
1161 pub const fn div_exact(self, rhs: Self) -> Option<Self> {
1162 if self % rhs != 0 {
1163 None
1164 } else {
1165 Some(self / rhs)
1166 }
1167 }
1168
1169 #[doc = concat!("`self == ", stringify!($SelfT), "::MIN && rhs == -1`,")]
1175 #[unstable(
1177 feature = "exact_div",
1178 issue = "139911",
1179 )]
1180 #[must_use = "this returns the result of the operation, \
1181 without modifying the original"]
1182 #[inline]
1183 pub const unsafe fn unchecked_div_exact(self, rhs: Self) -> Self {
1184 assert_unsafe_precondition!(
1185 check_language_ub,
1186 concat!(stringify!($SelfT), "::unchecked_div_exact cannot overflow, divide by zero, or leave a remainder"),
1187 (
1188 lhs: $SelfT = self,
1189 rhs: $SelfT = rhs,
1190 ) => rhs != 0 && lhs % rhs == 0 && (lhs != <$SelfT>::MIN || rhs != -1),
1191 );
1192 unsafe { intrinsics::exact_div(self, rhs) }
1194 }
1195
1196 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(2), Some(1));")]
1203 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(0), None);")]
1204 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem(-1), None);")]
1205 #[stable(feature = "wrapping", since = "1.7.0")]
1207 #[rustc_const_stable(feature = "const_checked_int_div", since = "1.52.0")]
1208 #[must_use = "this returns the result of the operation, \
1209 without modifying the original"]
1210 #[inline]
1211 pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
1212 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
1213 None
1214 } else {
1215 Some(unsafe { intrinsics::unchecked_rem(self, rhs) })
1217 }
1218 }
1219
1220 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_rem(2), 1);")]
1238 #[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem(0);")]
1244 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_rem(-1);")]
1250 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1252 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1253 #[must_use = "this returns the result of the operation, \
1254 without modifying the original"]
1255 #[inline]
1256 #[track_caller]
1257 pub const fn strict_rem(self, rhs: Self) -> Self {
1258 let (a, b) = self.overflowing_rem(rhs);
1259 if b { imp::overflow_panic::rem() } else { a }
1260 }
1261
1262 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(2), Some(1));")]
1269 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(0), None);")]
1270 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem_euclid(-1), None);")]
1271 #[stable(feature = "euclidean_division", since = "1.38.0")]
1273 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1274 #[must_use = "this returns the result of the operation, \
1275 without modifying the original"]
1276 #[inline]
1277 pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
1278 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
1280 None
1281 } else {
1282 Some(self.rem_euclid(rhs))
1283 }
1284 }
1285
1286 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_rem_euclid(2), 1);")]
1304 #[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem_euclid(0);")]
1310 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_rem_euclid(-1);")]
1316 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1318 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1319 #[must_use = "this returns the result of the operation, \
1320 without modifying the original"]
1321 #[inline]
1322 #[track_caller]
1323 pub const fn strict_rem_euclid(self, rhs: Self) -> Self {
1324 let (a, b) = self.overflowing_rem_euclid(rhs);
1325 if b { imp::overflow_panic::rem() } else { a }
1326 }
1327
1328 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_neg(), Some(-5));")]
1334 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_neg(), None);")]
1335 #[stable(feature = "wrapping", since = "1.7.0")]
1337 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1338 #[must_use = "this returns the result of the operation, \
1339 without modifying the original"]
1340 #[inline]
1341 pub const fn checked_neg(self) -> Option<Self> {
1342 let (a, b) = self.overflowing_neg();
1343 if intrinsics::unlikely(b) { None } else { Some(a) }
1344 }
1345
1346 #[doc = concat!("`self == ", stringify!($SelfT), "::MIN`,")]
1352 #[doc = concat!("[`checked_neg`]: ", stringify!($SelfT), "::checked_neg")]
1355 #[stable(feature = "unchecked_neg", since = "1.93.0")]
1356 #[rustc_const_stable(feature = "unchecked_neg", since = "1.93.0")]
1357 #[must_use = "this returns the result of the operation, \
1358 without modifying the original"]
1359 #[inline(always)]
1360 #[track_caller]
1361 pub const unsafe fn unchecked_neg(self) -> Self {
1362 assert_unsafe_precondition!(
1363 check_language_ub,
1364 concat!(stringify!($SelfT), "::unchecked_neg cannot overflow"),
1365 (
1366 lhs: $SelfT = self,
1367 ) => !lhs.overflowing_neg().1,
1368 );
1369
1370 unsafe {
1372 intrinsics::unchecked_sub(0, self)
1373 }
1374 }
1375
1376 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_neg(), -5);")]
1388 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_neg();")]
1394 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1396 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1397 #[must_use = "this returns the result of the operation, \
1398 without modifying the original"]
1399 #[inline]
1400 #[track_caller]
1401 pub const fn strict_neg(self) -> Self {
1402 let (a, b) = self.overflowing_neg();
1403 if b { imp::overflow_panic::neg() } else { a }
1404 }
1405
1406 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(4), Some(0x10));")]
1413 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(129), None);")]
1414 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shl(", stringify!($BITS_MINUS_ONE), "), Some(0));")]
1415 #[stable(feature = "wrapping", since = "1.7.0")]
1417 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1418 #[must_use = "this returns the result of the operation, \
1419 without modifying the original"]
1420 #[inline]
1421 pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
1422 if rhs < Self::BITS {
1424 Some(unsafe { self.unchecked_shl(rhs) })
1426 } else {
1427 None
1428 }
1429 }
1430
1431 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".strict_shl(4), 0x10);")]
1444 #[doc = concat!("let _ = 0x1", stringify!($SelfT), ".strict_shl(129);")]
1450 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1452 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1453 #[must_use = "this returns the result of the operation, \
1454 without modifying the original"]
1455 #[inline]
1456 #[track_caller]
1457 pub const fn strict_shl(self, rhs: u32) -> Self {
1458 let (a, b) = self.overflowing_shl(rhs);
1459 if b { imp::overflow_panic::shl() } else { a }
1460 }
1461
1462 #[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
1472 #[stable(feature = "unchecked_shifts", since = "1.93.0")]
1473 #[rustc_const_stable(feature = "unchecked_shifts", since = "1.93.0")]
1474 #[must_use = "this returns the result of the operation, \
1475 without modifying the original"]
1476 #[inline(always)]
1477 #[track_caller]
1478 pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
1479 assert_unsafe_precondition!(
1480 check_language_ub,
1481 concat!(stringify!($SelfT), "::unchecked_shl cannot overflow"),
1482 (
1483 rhs: u32 = rhs,
1484 ) => rhs < <$ActualT>::BITS,
1485 );
1486
1487 unsafe {
1489 intrinsics::unchecked_shl(self, rhs)
1490 }
1491 }
1492
1493 #[doc = concat!("assert_eq!(0x1_", stringify!($SelfT), ".unbounded_shl(4), 0x10);")]
1502 #[doc = concat!("assert_eq!(0x1_", stringify!($SelfT), ".unbounded_shl(129), 0);")]
1503 #[doc = concat!("assert_eq!(0b101_", stringify!($SelfT), ".unbounded_shl(0), 0b101);")]
1504 #[doc = concat!("assert_eq!(0b101_", stringify!($SelfT), ".unbounded_shl(1), 0b1010);")]
1505 #[doc = concat!("assert_eq!(0b101_", stringify!($SelfT), ".unbounded_shl(2), 0b10100);")]
1506 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shl(", stringify!($BITS), "), 0);")]
1507 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shl(1).unbounded_shl(", stringify!($BITS_MINUS_ONE), "), 0);")]
1508 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shl(", stringify!($BITS), "), 0);")]
1509 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shl(1).unbounded_shl(", stringify!($BITS_MINUS_ONE), "), 0);")]
1510 #[stable(feature = "unbounded_shifts", since = "1.87.0")]
1512 #[rustc_const_stable(feature = "unbounded_shifts", since = "1.87.0")]
1513 #[must_use = "this returns the result of the operation, \
1514 without modifying the original"]
1515 #[inline]
1516 pub const fn unbounded_shl(self, rhs: u32) -> $SelfT{
1517 if rhs < Self::BITS {
1518 unsafe { self.unchecked_shl(rhs) }
1521 } else {
1522 0
1523 }
1524 }
1525
1526 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1531 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".shl_exact(4), Some(0x10));")]
1539 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".shl_exact(", stringify!($SelfT), "::BITS - 2), Some(1 << ", stringify!($SelfT), "::BITS - 2));")]
1540 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".shl_exact(", stringify!($SelfT), "::BITS - 1), None);")]
1541 #[doc = concat!("assert_eq!((-0x2", stringify!($SelfT), ").shl_exact(", stringify!($SelfT), "::BITS - 2), Some(-0x2 << ", stringify!($SelfT), "::BITS - 2));")]
1542 #[doc = concat!("assert_eq!((-0x2", stringify!($SelfT), ").shl_exact(", stringify!($SelfT), "::BITS - 1), None);")]
1543 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1545 #[must_use = "this returns the result of the operation, \
1546 without modifying the original"]
1547 #[inline]
1548 pub const fn shl_exact(self, rhs: u32) -> Option<$SelfT> {
1549 if rhs < self.leading_zeros() || rhs < self.leading_ones() {
1550 Some(unsafe { self.unchecked_shl(rhs) })
1552 } else {
1553 None
1554 }
1555 }
1556
1557 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1560 #[doc = concat!("[`", stringify!($SelfT), "::shl_exact`]")]
1566 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1568 #[must_use = "this returns the result of the operation, \
1569 without modifying the original"]
1570 #[inline]
1571 pub const unsafe fn unchecked_shl_exact(self, rhs: u32) -> $SelfT {
1572 assert_unsafe_precondition!(
1573 check_library_ub,
1574 concat!(stringify!($SelfT), "::unchecked_shl_exact cannot shift out bits that would change the value of the first bit"),
1575 (
1576 zeros: u32 = self.leading_zeros(),
1577 ones: u32 = self.leading_ones(),
1578 rhs: u32 = rhs,
1579 ) => rhs < zeros || rhs < ones,
1580 );
1581
1582 unsafe { self.unchecked_shl(rhs) }
1584 }
1585
1586 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(4), Some(0x1));")]
1593 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(128), None);")]
1594 #[stable(feature = "wrapping", since = "1.7.0")]
1596 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1597 #[must_use = "this returns the result of the operation, \
1598 without modifying the original"]
1599 #[inline]
1600 pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
1601 if rhs < Self::BITS {
1603 Some(unsafe { self.unchecked_shr(rhs) })
1605 } else {
1606 None
1607 }
1608 }
1609
1610 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".strict_shr(4), 0x1);")]
1623 #[doc = concat!("let _ = 0x10", stringify!($SelfT), ".strict_shr(128);")]
1629 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1631 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1632 #[must_use = "this returns the result of the operation, \
1633 without modifying the original"]
1634 #[inline]
1635 #[track_caller]
1636 pub const fn strict_shr(self, rhs: u32) -> Self {
1637 let (a, b) = self.overflowing_shr(rhs);
1638 if b { imp::overflow_panic::shr() } else { a }
1639 }
1640
1641 #[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
1651 #[stable(feature = "unchecked_shifts", since = "1.93.0")]
1652 #[rustc_const_stable(feature = "unchecked_shifts", since = "1.93.0")]
1653 #[must_use = "this returns the result of the operation, \
1654 without modifying the original"]
1655 #[inline(always)]
1656 #[track_caller]
1657 pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
1658 assert_unsafe_precondition!(
1659 check_language_ub,
1660 concat!(stringify!($SelfT), "::unchecked_shr cannot overflow"),
1661 (
1662 rhs: u32 = rhs,
1663 ) => rhs < <$ActualT>::BITS,
1664 );
1665
1666 unsafe {
1668 intrinsics::unchecked_shr(self, rhs)
1669 }
1670 }
1671
1672 #[doc = concat!("assert_eq!(0x10_", stringify!($SelfT), ".unbounded_shr(4), 0x1);")]
1682 #[doc = concat!("assert_eq!(0x10_", stringify!($SelfT), ".unbounded_shr(129), 0);")]
1683 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.unbounded_shr(129), -1);")]
1684 #[doc = concat!("assert_eq!(0b1010_", stringify!($SelfT), ".unbounded_shr(0), 0b1010);")]
1685 #[doc = concat!("assert_eq!(0b1010_", stringify!($SelfT), ".unbounded_shr(1), 0b101);")]
1686 #[doc = concat!("assert_eq!(0b1010_", stringify!($SelfT), ".unbounded_shr(2), 0b10);")]
1687 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shr(", stringify!($BITS), "), 0);")]
1688 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shr(1).unbounded_shr(", stringify!($BITS_MINUS_ONE), "), 0);")]
1689 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shr(", stringify!($BITS), "), -1);")]
1690 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shr(1).unbounded_shr(", stringify!($BITS_MINUS_ONE), "), -1);")]
1691 #[stable(feature = "unbounded_shifts", since = "1.87.0")]
1693 #[rustc_const_stable(feature = "unbounded_shifts", since = "1.87.0")]
1694 #[must_use = "this returns the result of the operation, \
1695 without modifying the original"]
1696 #[inline]
1697 pub const fn unbounded_shr(self, rhs: u32) -> $SelfT{
1698 if rhs < Self::BITS {
1699 unsafe { self.unchecked_shr(rhs) }
1702 } else {
1703 unsafe { self.unchecked_shr(Self::BITS - 1) }
1708 }
1709 }
1710
1711 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1715 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".shr_exact(4), Some(0x1));")]
1723 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".shr_exact(5), None);")]
1724 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1726 #[must_use = "this returns the result of the operation, \
1727 without modifying the original"]
1728 #[inline]
1729 pub const fn shr_exact(self, rhs: u32) -> Option<$SelfT> {
1730 if rhs <= self.trailing_zeros() && rhs < <$SelfT>::BITS {
1731 Some(unsafe { self.unchecked_shr(rhs) })
1733 } else {
1734 None
1735 }
1736 }
1737
1738 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1741 #[doc = concat!(stringify!($SelfT), "::BITS`")]
1746 #[doc = concat!("[`", stringify!($SelfT), "::shr_exact`]")]
1748 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1750 #[must_use = "this returns the result of the operation, \
1751 without modifying the original"]
1752 #[inline]
1753 pub const unsafe fn unchecked_shr_exact(self, rhs: u32) -> $SelfT {
1754 assert_unsafe_precondition!(
1755 check_library_ub,
1756 concat!(stringify!($SelfT), "::unchecked_shr_exact cannot shift out non-zero bits"),
1757 (
1758 zeros: u32 = self.trailing_zeros(),
1759 bits: u32 = <$SelfT>::BITS,
1760 rhs: u32 = rhs,
1761 ) => rhs <= zeros && rhs < bits,
1762 );
1763
1764 unsafe { self.unchecked_shr(rhs) }
1766 }
1767
1768 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_abs(), Some(5));")]
1775 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_abs(), None);")]
1776 #[stable(feature = "no_panic_abs", since = "1.13.0")]
1778 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1779 #[must_use = "this returns the result of the operation, \
1780 without modifying the original"]
1781 #[inline]
1782 pub const fn checked_abs(self) -> Option<Self> {
1783 if self.is_negative() {
1784 self.checked_neg()
1785 } else {
1786 Some(self)
1787 }
1788 }
1789
1790 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").strict_abs(), 5);")]
1803 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_abs();")]
1809 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1811 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1812 #[must_use = "this returns the result of the operation, \
1813 without modifying the original"]
1814 #[inline]
1815 #[track_caller]
1816 pub const fn strict_abs(self) -> Self {
1817 if self.is_negative() {
1818 self.strict_neg()
1819 } else {
1820 self
1821 }
1822 }
1823
1824 #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".checked_pow(2), Some(64));")]
1831 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".checked_pow(0), Some(1));")]
1832 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);")]
1833 #[stable(feature = "no_panic_pow", since = "1.34.0")]
1836 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1837 #[must_use = "this returns the result of the operation, \
1838 without modifying the original"]
1839 #[inline]
1840 pub const fn checked_pow(self, mut exp: u32) -> Option<Self> {
1841 let mut base = self;
1842 let mut acc: Self = 1;
1843
1844 if intrinsics::is_val_statically_known(base) && base.unsigned_abs().is_power_of_two() {
1845 let k = base.unsigned_abs().ilog2();
1846 let shift = try_opt!(k.checked_mul(exp));
1847 return if base < 0 && (exp % 2) == 1 {
1848 (-1 as Self).shl_exact(shift)
1849 } else {
1850 (1 as Self).shl_exact(shift)
1851 }
1852 }
1853
1854 if exp == 0 {
1855 return Some(1);
1856 }
1857
1858 if intrinsics::is_val_statically_known(exp) {
1859 while exp > 1 {
1860 if (exp & 1) == 1 {
1861 acc = try_opt!(acc.checked_mul(base));
1862 }
1863 exp /= 2;
1864 base = try_opt!(base.checked_mul(base));
1865 }
1866
1867 return acc.checked_mul(base);
1872 }
1873
1874 loop {
1875 if (exp & 1) == 1 {
1876 acc = try_opt!(acc.checked_mul(base));
1877 if exp == 1 {
1879 return Some(acc);
1880 }
1881 }
1882 exp /= 2;
1883 base = try_opt!(base.checked_mul(base));
1884 }
1885 }
1886
1887 #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".strict_pow(2), 64);")]
1900 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".strict_pow(0), 1);")]
1901 #[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_pow(2);")]
1907 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1909 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1910 #[must_use = "this returns the result of the operation, \
1911 without modifying the original"]
1912 #[inline]
1913 #[track_caller]
1914 pub const fn strict_pow(self, exp: u32) -> Self {
1915 match self.checked_pow(exp) {
1916 Some(x) => x,
1917 None => imp::overflow_panic::pow(),
1918 }
1919 }
1920
1921 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_isqrt(), Some(3));")]
1933 #[stable(feature = "isqrt", since = "1.84.0")]
1935 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
1936 #[must_use = "this returns the result of the operation, \
1937 without modifying the original"]
1938 #[inline]
1939 pub const fn checked_isqrt(self) -> Option<Self> {
1940 if self < 0 {
1941 None
1942 } else {
1943 let result = self.cast_unsigned().isqrt().cast_signed();
1946
1947 unsafe {
1959 const MAX_RESULT: $SelfT = <$SelfT>::MAX.cast_unsigned().isqrt().cast_signed();
1960 crate::hint::assert_unchecked(result <= MAX_RESULT);
1961 }
1962 Some(result)
1963 }
1964 }
1965
1966 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_add(1), 101);")]
1973 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add(100), ", stringify!($SelfT), "::MAX);")]
1974 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_add(-1), ", stringify!($SelfT), "::MIN);")]
1975 #[stable(feature = "rust1", since = "1.0.0")]
1978 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1979 #[must_use = "this returns the result of the operation, \
1980 without modifying the original"]
1981 #[inline(always)]
1982 pub const fn saturating_add(self, rhs: Self) -> Self {
1983 intrinsics::saturating_add(self, rhs)
1984 }
1985
1986 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".saturating_add_unsigned(2), 3);")]
1993 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add_unsigned(100), ", stringify!($SelfT), "::MAX);")]
1994 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1996 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1997 #[must_use = "this returns the result of the operation, \
1998 without modifying the original"]
1999 #[inline]
2000 pub const fn saturating_add_unsigned(self, rhs: $UnsignedT) -> Self {
2001 match self.checked_add_unsigned(rhs) {
2004 Some(x) => x,
2005 None => Self::MAX,
2006 }
2007 }
2008
2009 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub(127), -27);")]
2016 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub(100), ", stringify!($SelfT), "::MIN);")]
2017 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_sub(-1), ", stringify!($SelfT), "::MAX);")]
2018 #[stable(feature = "rust1", since = "1.0.0")]
2020 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2021 #[must_use = "this returns the result of the operation, \
2022 without modifying the original"]
2023 #[inline(always)]
2024 pub const fn saturating_sub(self, rhs: Self) -> Self {
2025 intrinsics::saturating_sub(self, rhs)
2026 }
2027
2028 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub_unsigned(127), -27);")]
2035 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub_unsigned(100), ", stringify!($SelfT), "::MIN);")]
2036 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2038 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2039 #[must_use = "this returns the result of the operation, \
2040 without modifying the original"]
2041 #[inline]
2042 pub const fn saturating_sub_unsigned(self, rhs: $UnsignedT) -> Self {
2043 match self.checked_sub_unsigned(rhs) {
2046 Some(x) => x,
2047 None => Self::MIN,
2048 }
2049 }
2050
2051 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_neg(), -100);")]
2058 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_neg(), 100);")]
2059 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_neg(), ", stringify!($SelfT), "::MAX);")]
2060 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_neg(), ", stringify!($SelfT), "::MIN + 1);")]
2061 #[stable(feature = "saturating_neg", since = "1.45.0")]
2064 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2065 #[must_use = "this returns the result of the operation, \
2066 without modifying the original"]
2067 #[inline(always)]
2068 pub const fn saturating_neg(self) -> Self {
2069 intrinsics::saturating_sub(0, self)
2070 }
2071
2072 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_abs(), 100);")]
2079 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_abs(), 100);")]
2080 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_abs(), ", stringify!($SelfT), "::MAX);")]
2081 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).saturating_abs(), ", stringify!($SelfT), "::MAX);")]
2082 #[stable(feature = "saturating_neg", since = "1.45.0")]
2085 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2086 #[must_use = "this returns the result of the operation, \
2087 without modifying the original"]
2088 #[inline]
2089 pub const fn saturating_abs(self) -> Self {
2090 if self.is_negative() {
2091 self.saturating_neg()
2092 } else {
2093 self
2094 }
2095 }
2096
2097 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".saturating_mul(12), 120);")]
2104 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_mul(10), ", stringify!($SelfT), "::MAX);")]
2105 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_mul(10), ", stringify!($SelfT), "::MIN);")]
2106 #[stable(feature = "wrapping", since = "1.7.0")]
2108 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2109 #[must_use = "this returns the result of the operation, \
2110 without modifying the original"]
2111 #[inline]
2112 pub const fn saturating_mul(self, rhs: Self) -> Self {
2113 match self.checked_mul(rhs) {
2114 Some(x) => x,
2115 None => if (self < 0) == (rhs < 0) {
2116 Self::MAX
2117 } else {
2118 Self::MIN
2119 }
2120 }
2121 }
2122
2123 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
2134 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_div(-1), ", stringify!($SelfT), "::MIN + 1);")]
2135 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_div(-1), ", stringify!($SelfT), "::MAX);")]
2136 #[stable(feature = "saturating_div", since = "1.58.0")]
2139 #[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
2140 #[must_use = "this returns the result of the operation, \
2141 without modifying the original"]
2142 #[inline]
2143 pub const fn saturating_div(self, rhs: Self) -> Self {
2144 match self.overflowing_div(rhs) {
2145 (result, false) => result,
2146 (_result, true) => Self::MAX, }
2148 }
2149
2150 #[doc = concat!("assert_eq!((-4", stringify!($SelfT), ").saturating_pow(3), -64);")]
2157 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".saturating_pow(0), 1);")]
2158 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(2), ", stringify!($SelfT), "::MAX);")]
2159 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(3), ", stringify!($SelfT), "::MIN);")]
2160 #[stable(feature = "no_panic_pow", since = "1.34.0")]
2162 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2163 #[must_use = "this returns the result of the operation, \
2164 without modifying the original"]
2165 #[inline]
2166 pub const fn saturating_pow(self, exp: u32) -> Self {
2167 match self.checked_pow(exp) {
2168 Some(x) => x,
2169 None if self < 0 && exp % 2 == 1 => Self::MIN,
2170 None => Self::MAX,
2171 }
2172 }
2173
2174 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add(27), 127);")]
2181 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add(2), ", stringify!($SelfT), "::MIN + 1);")]
2182 #[stable(feature = "rust1", since = "1.0.0")]
2184 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2185 #[must_use = "this returns the result of the operation, \
2186 without modifying the original"]
2187 #[inline(always)]
2188 pub const fn wrapping_add(self, rhs: Self) -> Self {
2189 intrinsics::wrapping_add(self, rhs)
2190 }
2191
2192 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add_unsigned(27), 127);")]
2199 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add_unsigned(2), ", stringify!($SelfT), "::MIN + 1);")]
2200 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2202 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2203 #[must_use = "this returns the result of the operation, \
2204 without modifying the original"]
2205 #[inline(always)]
2206 pub const fn wrapping_add_unsigned(self, rhs: $UnsignedT) -> Self {
2207 self.wrapping_add(rhs as Self)
2208 }
2209
2210 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub(127), -127);")]
2217 #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub(", stringify!($SelfT), "::MAX), ", stringify!($SelfT), "::MAX);")]
2218 #[stable(feature = "rust1", since = "1.0.0")]
2220 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2221 #[must_use = "this returns the result of the operation, \
2222 without modifying the original"]
2223 #[inline(always)]
2224 pub const fn wrapping_sub(self, rhs: Self) -> Self {
2225 intrinsics::wrapping_sub(self, rhs)
2226 }
2227
2228 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub_unsigned(127), -127);")]
2235 #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub_unsigned(", stringify!($UnsignedT), "::MAX), -1);")]
2236 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2238 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2239 #[must_use = "this returns the result of the operation, \
2240 without modifying the original"]
2241 #[inline(always)]
2242 pub const fn wrapping_sub_unsigned(self, rhs: $UnsignedT) -> Self {
2243 self.wrapping_sub(rhs as Self)
2244 }
2245
2246 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".wrapping_mul(12), 120);")]
2253 #[stable(feature = "rust1", since = "1.0.0")]
2256 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2257 #[must_use = "this returns the result of the operation, \
2258 without modifying the original"]
2259 #[inline(always)]
2260 pub const fn wrapping_mul(self, rhs: Self) -> Self {
2261 intrinsics::wrapping_mul(self, rhs)
2262 }
2263
2264 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div(10), 10);")]
2279 #[stable(feature = "num_wrapping", since = "1.2.0")]
2282 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
2283 #[must_use = "this returns the result of the operation, \
2284 without modifying the original"]
2285 #[inline]
2286 pub const fn wrapping_div(self, rhs: Self) -> Self {
2287 self.overflowing_div(rhs).0
2288 }
2289
2290 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div_euclid(10), 10);")]
2305 #[stable(feature = "euclidean_division", since = "1.38.0")]
2308 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2309 #[must_use = "this returns the result of the operation, \
2310 without modifying the original"]
2311 #[inline]
2312 pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
2313 self.overflowing_div_euclid(rhs).0
2314 }
2315
2316 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem(10), 0);")]
2331 #[stable(feature = "num_wrapping", since = "1.2.0")]
2334 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
2335 #[must_use = "this returns the result of the operation, \
2336 without modifying the original"]
2337 #[inline]
2338 pub const fn wrapping_rem(self, rhs: Self) -> Self {
2339 self.overflowing_rem(rhs).0
2340 }
2341
2342 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);")]
2356 #[stable(feature = "euclidean_division", since = "1.38.0")]
2359 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2360 #[must_use = "this returns the result of the operation, \
2361 without modifying the original"]
2362 #[inline]
2363 pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
2364 self.overflowing_rem_euclid(rhs).0
2365 }
2366
2367 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_neg(), -100);")]
2378 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_neg(), 100);")]
2379 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_neg(), ", stringify!($SelfT), "::MIN);")]
2380 #[stable(feature = "num_wrapping", since = "1.2.0")]
2382 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2383 #[must_use = "this returns the result of the operation, \
2384 without modifying the original"]
2385 #[inline(always)]
2386 pub const fn wrapping_neg(self) -> Self {
2387 (0 as $SelfT).wrapping_sub(self)
2388 }
2389
2390 #[doc = concat!("assert_eq!((-1_", stringify!($SelfT), ").wrapping_shl(7), -128);")]
2409 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shl(", stringify!($BITS), "), 42);")]
2410 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shl(1).wrapping_shl(", stringify!($BITS_MINUS_ONE), "), 0);")]
2411 #[doc = concat!("assert_eq!((-1_", stringify!($SelfT), ").wrapping_shl(128), -1);")]
2412 #[doc = concat!("assert_eq!(5_", stringify!($SelfT), ".wrapping_shl(1025), 10);")]
2413 #[stable(feature = "num_wrapping", since = "1.2.0")]
2415 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2416 #[must_use = "this returns the result of the operation, \
2417 without modifying the original"]
2418 #[inline(always)]
2419 pub const fn wrapping_shl(self, rhs: u32) -> Self {
2420 unsafe {
2423 self.unchecked_shl(rhs & (Self::BITS - 1))
2424 }
2425 }
2426
2427 #[doc = concat!("assert_eq!((-128_", stringify!($SelfT), ").wrapping_shr(7), -1);")]
2446 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shr(", stringify!($BITS), "), 42);")]
2447 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shr(1).wrapping_shr(", stringify!($BITS_MINUS_ONE), "), 0);")]
2448 #[doc = concat!("assert_eq!(10_", stringify!($SelfT), ".wrapping_shr(1025), 5);")]
2450 #[stable(feature = "num_wrapping", since = "1.2.0")]
2452 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2453 #[must_use = "this returns the result of the operation, \
2454 without modifying the original"]
2455 #[inline(always)]
2456 pub const fn wrapping_shr(self, rhs: u32) -> Self {
2457 unsafe {
2460 self.unchecked_shr(rhs & (Self::BITS - 1))
2461 }
2462 }
2463
2464 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_abs(), 100);")]
2475 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_abs(), 100);")]
2476 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_abs(), ", stringify!($SelfT), "::MIN);")]
2477 #[stable(feature = "no_panic_abs", since = "1.13.0")]
2480 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2481 #[must_use = "this returns the result of the operation, \
2482 without modifying the original"]
2483 #[allow(unused_attributes)]
2484 #[inline]
2485 pub const fn wrapping_abs(self) -> Self {
2486 if self.is_negative() {
2487 self.wrapping_neg()
2488 } else {
2489 self
2490 }
2491 }
2492
2493 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".unsigned_abs(), 100", stringify!($UnsignedT), ");")]
2501 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").unsigned_abs(), 100", stringify!($UnsignedT), ");")]
2502 #[stable(feature = "unsigned_abs", since = "1.51.0")]
2505 #[rustc_const_stable(feature = "unsigned_abs", since = "1.51.0")]
2506 #[must_use = "this returns the result of the operation, \
2507 without modifying the original"]
2508 #[inline]
2509 pub const fn unsigned_abs(self) -> $UnsignedT {
2510 self.wrapping_abs() as $UnsignedT
2511 }
2512
2513 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".wrapping_pow(4), 81);")]
2520 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".wrapping_pow(0), 1);")]
2523 #[stable(feature = "no_panic_pow", since = "1.34.0")]
2525 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2526 #[must_use = "this returns the result of the operation, \
2527 without modifying the original"]
2528 #[inline]
2529 pub const fn wrapping_pow(self, exp: u32) -> Self {
2530 let (a, _) = self.overflowing_pow(exp);
2531 a
2532 }
2533
2534 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_add(2), (7, false));")]
2545 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.overflowing_add(1), (", stringify!($SelfT), "::MIN, true));")]
2546 #[stable(feature = "wrapping", since = "1.7.0")]
2548 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2549 #[must_use = "this returns the result of the operation, \
2550 without modifying the original"]
2551 #[inline(always)]
2552 pub const fn overflowing_add(self, rhs: Self) -> (Self, bool) {
2553 let (a, b) = intrinsics::add_with_overflow(self as $ActualT, rhs as $ActualT);
2554 (a as Self, b)
2555 }
2556
2557 #[doc = concat!("[`", stringify!($UnsignedT), "::carrying_add`]")]
2569 #[doc = concat!("// 10 MAX (a = 10 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")]
2587 #[doc = concat!("// + -5 9 (b = -5 × 2^", stringify!($BITS), " + 9)")]
2588 #[doc = concat!("// 6 8 (sum = 6 × 2^", stringify!($BITS), " + 8)")]
2590 #[doc = concat!("let (a1, a0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (10, ", stringify!($UnsignedT), "::MAX);")]
2592 #[doc = concat!("let (b1, b0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (-5, 9);")]
2593 #[doc = concat!("// ", stringify!($UnsignedT), "::carrying_add for the less significant words")]
2596 #[doc = concat!("// ", stringify!($SelfT), "::carrying_add for the most significant word")]
2600 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2606 #[must_use = "this returns the result of the operation, \
2607 without modifying the original"]
2608 #[inline]
2609 pub const fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool) {
2610 let (a, b) = self.overflowing_add(rhs);
2613 let (c, d) = a.overflowing_add(carry as $SelfT);
2614 (c, b != d)
2615 }
2616
2617 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_add_unsigned(2), (3, false));")]
2627 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_add_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MAX, false));")]
2628 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).overflowing_add_unsigned(3), (", stringify!($SelfT), "::MIN, true));")]
2629 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2631 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2632 #[must_use = "this returns the result of the operation, \
2633 without modifying the original"]
2634 #[inline]
2635 pub const fn overflowing_add_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
2636 let rhs = rhs as Self;
2637 let (res, overflowed) = self.overflowing_add(rhs);
2638 (res, overflowed ^ (rhs < 0))
2639 }
2640
2641 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_sub(2), (3, false));")]
2651 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_sub(1), (", stringify!($SelfT), "::MAX, true));")]
2652 #[stable(feature = "wrapping", since = "1.7.0")]
2654 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2655 #[must_use = "this returns the result of the operation, \
2656 without modifying the original"]
2657 #[inline(always)]
2658 pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
2659 let (a, b) = intrinsics::sub_with_overflow(self as $ActualT, rhs as $ActualT);
2660 (a as Self, b)
2661 }
2662
2663 #[doc = concat!("[`", stringify!($UnsignedT), "::borrowing_sub`]")]
2676 #[doc = concat!("// 6 8 (a = 6 × 2^", stringify!($BITS), " + 8)")]
2694 #[doc = concat!("// - -5 9 (b = -5 × 2^", stringify!($BITS), " + 9)")]
2695 #[doc = concat!("// 10 MAX (diff = 10 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")]
2697 #[doc = concat!("let (a1, a0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (6, 8);")]
2699 #[doc = concat!("let (b1, b0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (-5, 9);")]
2700 #[doc = concat!("// ", stringify!($UnsignedT), "::borrowing_sub for the less significant words")]
2703 #[doc = concat!("// ", stringify!($SelfT), "::borrowing_sub for the most significant word")]
2707 #[doc = concat!("assert_eq!((diff1, diff0), (10, ", stringify!($UnsignedT), "::MAX));")]
2711 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2713 #[must_use = "this returns the result of the operation, \
2714 without modifying the original"]
2715 #[inline]
2716 pub const fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
2717 let (a, b) = self.overflowing_sub(rhs);
2720 let (c, d) = a.overflowing_sub(borrow as $SelfT);
2721 (c, b != d)
2722 }
2723
2724 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_sub_unsigned(2), (-1, false));")]
2734 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX).overflowing_sub_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MIN, false));")]
2735 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).overflowing_sub_unsigned(3), (", stringify!($SelfT), "::MAX, true));")]
2736 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2738 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2739 #[must_use = "this returns the result of the operation, \
2740 without modifying the original"]
2741 #[inline]
2742 pub const fn overflowing_sub_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
2743 let rhs = rhs as Self;
2744 let (res, overflowed) = self.overflowing_sub(rhs);
2745 (res, overflowed ^ (rhs < 0))
2746 }
2747
2748 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_mul(2), (10, false));")]
2757 #[stable(feature = "wrapping", since = "1.7.0")]
2760 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2761 #[must_use = "this returns the result of the operation, \
2762 without modifying the original"]
2763 #[inline(always)]
2764 pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
2765 let (a, b) = intrinsics::mul_with_overflow(self as $ActualT, rhs as $ActualT);
2766 (a as Self, b)
2767 }
2768
2769 #[doc = concat!("assert_eq!(",
2790 stringify!($SelfT), "::MAX.carrying_mul(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
2791 "(", stringify!($SelfT), "::MAX.unsigned_abs() + 1, ", stringify!($SelfT), "::MAX / 2));"
2792 )]
2793 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2795 #[rustc_const_unstable(feature = "signed_bigint_helpers", issue = "151989")]
2796 #[must_use = "this returns the result of the operation, \
2797 without modifying the original"]
2798 #[inline]
2799 pub const fn carrying_mul(self, rhs: Self, carry: Self) -> ($UnsignedT, Self) {
2800 Self::carrying_mul_add(self, rhs, carry, 0)
2801 }
2802
2803 #[doc = concat!("assert_eq!(",
2826 stringify!($SelfT), "::MAX.carrying_mul_add(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
2827 "(", stringify!($UnsignedT), "::MAX, ", stringify!($SelfT), "::MAX / 2));"
2828 )]
2829 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2831 #[rustc_const_unstable(feature = "signed_bigint_helpers", issue = "151989")]
2832 #[must_use = "this returns the result of the operation, \
2833 without modifying the original"]
2834 #[inline]
2835 pub const fn carrying_mul_add(self, rhs: Self, carry: Self, add: Self) -> ($UnsignedT, Self) {
2836 intrinsics::carrying_mul_add(self, rhs, carry, add)
2837 }
2838
2839 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div(2), (2, false));")]
2852 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div(-1), (", stringify!($SelfT), "::MIN, true));")]
2853 #[inline]
2855 #[stable(feature = "wrapping", since = "1.7.0")]
2856 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
2857 #[must_use = "this returns the result of the operation, \
2858 without modifying the original"]
2859 pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
2860 if intrinsics::unlikely((self == Self::MIN) & (rhs == -1)) {
2862 (self, true)
2863 } else {
2864 (self / rhs, false)
2865 }
2866 }
2867
2868 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div_euclid(2), (2, false));")]
2881 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringify!($SelfT), "::MIN, true));")]
2882 #[inline]
2884 #[stable(feature = "euclidean_division", since = "1.38.0")]
2885 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2886 #[must_use = "this returns the result of the operation, \
2887 without modifying the original"]
2888 pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
2889 if intrinsics::unlikely((self == Self::MIN) & (rhs == -1)) {
2891 (self, true)
2892 } else {
2893 (self.div_euclid(rhs), false)
2894 }
2895 }
2896
2897 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem(2), (1, false));")]
2910 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem(-1), (0, true));")]
2911 #[inline]
2913 #[stable(feature = "wrapping", since = "1.7.0")]
2914 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
2915 #[must_use = "this returns the result of the operation, \
2916 without modifying the original"]
2917 pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
2918 if intrinsics::unlikely(rhs == -1) {
2919 (0, self == Self::MIN)
2920 } else {
2921 (self % rhs, false)
2922 }
2923 }
2924
2925
2926 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem_euclid(2), (1, false));")]
2939 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));")]
2940 #[stable(feature = "euclidean_division", since = "1.38.0")]
2942 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2943 #[must_use = "this returns the result of the operation, \
2944 without modifying the original"]
2945 #[inline]
2946 #[track_caller]
2947 pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
2948 if intrinsics::unlikely(rhs == -1) {
2949 (0, self == Self::MIN)
2950 } else {
2951 (self.rem_euclid(rhs), false)
2952 }
2953 }
2954
2955
2956 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2, false));")]
2966 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($SelfT), "::MIN, true));")]
2967 #[inline]
2969 #[stable(feature = "wrapping", since = "1.7.0")]
2970 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2971 #[must_use = "this returns the result of the operation, \
2972 without modifying the original"]
2973 #[allow(unused_attributes)]
2974 pub const fn overflowing_neg(self) -> (Self, bool) {
2975 if intrinsics::unlikely(self == Self::MIN) {
2976 (Self::MIN, true)
2977 } else {
2978 (-self, false)
2979 }
2980 }
2981
2982 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT),".overflowing_shl(4), (0x10, false));")]
2992 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shl(", stringify!($BITS_MINUS_ONE), "), (0, false));")]
2994 #[stable(feature = "wrapping", since = "1.7.0")]
2996 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2997 #[must_use = "this returns the result of the operation, \
2998 without modifying the original"]
2999 #[inline]
3000 pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
3001 (self.wrapping_shl(rhs), rhs >= Self::BITS)
3002 }
3003
3004 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shr(4), (0x1, false));")]
3014 #[stable(feature = "wrapping", since = "1.7.0")]
3017 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3018 #[must_use = "this returns the result of the operation, \
3019 without modifying the original"]
3020 #[inline]
3021 pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
3022 (self.wrapping_shr(rhs), rhs >= Self::BITS)
3023 }
3024
3025 #[doc = concat!("(e.g., [`", stringify!($SelfT), "::MIN`] for values of type [`", stringify!($SelfT), "`]),")]
3030 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".overflowing_abs(), (10, false));")]
3037 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").overflowing_abs(), (10, false));")]
3038 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_abs(), (", stringify!($SelfT), "::MIN, true));")]
3039 #[stable(feature = "no_panic_abs", since = "1.13.0")]
3041 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3042 #[must_use = "this returns the result of the operation, \
3043 without modifying the original"]
3044 #[inline]
3045 pub const fn overflowing_abs(self) -> (Self, bool) {
3046 (self.wrapping_abs(), self == Self::MIN)
3047 }
3048
3049 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".overflowing_pow(4), (81, false));")]
3058 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".overflowing_pow(0), (1, false));")]
3059 #[stable(feature = "no_panic_pow", since = "1.34.0")]
3062 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
3063 #[must_use = "this returns the result of the operation, \
3064 without modifying the original"]
3065 #[inline]
3066 pub const fn overflowing_pow(self, mut exp: u32) -> (Self, bool) {
3067 let mut base = self;
3068 let mut acc: Self = 1;
3069 let mut overflow = false;
3070 let mut tmp_overflow;
3071
3072 if intrinsics::is_val_statically_known(base) && base.unsigned_abs().is_power_of_two() {
3073 let k = base.unsigned_abs().ilog2();
3074 let Some(shift) = k.checked_mul(exp) else {
3075 return (0, true)
3076 };
3077 let base: Self = if base < 0 && (exp % 2) != 0 { -1 } else { 1 };
3078 return (base.unbounded_shl(shift), base.shl_exact(shift).is_none());
3079 }
3080
3081 if exp == 0 {
3082 return (1, false);
3083 }
3084
3085 if intrinsics::is_val_statically_known(exp) {
3086 while exp > 1 {
3087 if (exp & 1) == 1 {
3088 (acc, tmp_overflow) = acc.overflowing_mul(base);
3089 overflow |= tmp_overflow;
3090 }
3091 exp /= 2;
3092 (base, tmp_overflow) = base.overflowing_mul(base);
3093 overflow |= tmp_overflow;
3094 }
3095
3096 (acc, tmp_overflow) = acc.overflowing_mul(base);
3101 overflow |= tmp_overflow;
3102 return (acc, overflow);
3103 }
3104
3105 loop {
3106 if (exp & 1) == 1 {
3107 (acc, tmp_overflow) = acc.overflowing_mul(base);
3108 overflow |= tmp_overflow;
3109 if exp == 1 {
3111 return (acc, overflow);
3112 }
3113 }
3114 exp /= 2;
3115 (base, tmp_overflow) = base.overflowing_mul(base);
3116 overflow |= tmp_overflow;
3117 }
3118 }
3119
3120 #[doc = concat!("let x: ", stringify!($SelfT), " = 2; // or any other integer type")]
3126 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".pow(0), 1);")]
3129 #[stable(feature = "rust1", since = "1.0.0")]
3131 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
3132 #[must_use = "this returns the result of the operation, \
3133 without modifying the original"]
3134 #[inline]
3135 #[rustc_inherit_overflow_checks]
3136 pub const fn pow(self, exp: u32) -> Self {
3137 if intrinsics::overflow_checks() {
3138 self.strict_pow(exp)
3139 } else {
3140 self.wrapping_pow(exp)
3141 }
3142 }
3143
3144 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".isqrt(), 3);")]
3158 #[stable(feature = "isqrt", since = "1.84.0")]
3160 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
3161 #[must_use = "this returns the result of the operation, \
3162 without modifying the original"]
3163 #[inline]
3164 #[track_caller]
3165 pub const fn isqrt(self) -> Self {
3166 match self.checked_isqrt() {
3167 Some(sqrt) => sqrt,
3168 None => imp::int_sqrt::panic_for_negative_argument(),
3169 }
3170 }
3171
3172 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
3193 #[stable(feature = "euclidean_division", since = "1.38.0")]
3201 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3202 #[must_use = "this returns the result of the operation, \
3203 without modifying the original"]
3204 #[inline]
3205 #[track_caller]
3206 pub const fn div_euclid(self, rhs: Self) -> Self {
3207 let q = self / rhs;
3208 if self % rhs < 0 {
3209 return if rhs > 0 { q - 1 } else { q + 1 }
3210 }
3211 q
3212 }
3213
3214
3215 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
3231 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.rem_euclid(-1);")]
3242 #[doc(alias = "modulo", alias = "mod")]
3244 #[stable(feature = "euclidean_division", since = "1.38.0")]
3245 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3246 #[must_use = "this returns the result of the operation, \
3247 without modifying the original"]
3248 #[inline]
3249 #[track_caller]
3250 pub const fn rem_euclid(self, rhs: Self) -> Self {
3251 let r = self % rhs;
3252 if r < 0 {
3253 r.wrapping_add(rhs.wrapping_abs())
3262 } else {
3263 r
3264 }
3265 }
3266
3267 #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3279 #[unstable(feature = "int_roundings", issue = "88581")]
3287 #[must_use = "this returns the result of the operation, \
3288 without modifying the original"]
3289 #[inline]
3290 #[track_caller]
3291 pub const fn div_floor(self, rhs: Self) -> Self {
3292 let d = self / rhs;
3293 let r = self % rhs;
3294
3295 let correction = (self ^ rhs) >> (Self::BITS - 1);
3302 if r != 0 {
3303 d + correction
3304 } else {
3305 d
3306 }
3307 }
3308
3309 #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3321 #[unstable(feature = "int_roundings", issue = "88581")]
3329 #[must_use = "this returns the result of the operation, \
3330 without modifying the original"]
3331 #[inline]
3332 #[track_caller]
3333 pub const fn div_ceil(self, rhs: Self) -> Self {
3334 let d = self / rhs;
3335 let r = self % rhs;
3336
3337 let correction = 1 + ((self ^ rhs) >> (Self::BITS - 1));
3340 if r != 0 {
3341 d + correction
3342 } else {
3343 d
3344 }
3345 }
3346
3347 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(8), 16);")]
3366 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(8), 24);")]
3367 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
3368 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
3369 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
3370 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
3371 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(-8), -16);")]
3372 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(-8), -24);")]
3373 #[unstable(feature = "int_roundings", issue = "88581")]
3375 #[must_use = "this returns the result of the operation, \
3376 without modifying the original"]
3377 #[inline]
3378 #[rustc_inherit_overflow_checks]
3379 pub const fn next_multiple_of(self, rhs: Self) -> Self {
3380 if rhs == -1 {
3382 return self;
3383 }
3384
3385 let r = self % rhs;
3386 let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
3387 r + rhs
3388 } else {
3389 r
3390 };
3391
3392 if m == 0 {
3393 self
3394 } else {
3395 self + (rhs - m)
3396 }
3397 }
3398
3399 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(16));")]
3410 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(24));")]
3411 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
3412 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
3413 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
3414 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
3415 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-16));")]
3416 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-24));")]
3417 #[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".checked_next_multiple_of(0), None);")]
3418 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_next_multiple_of(2), None);")]
3419 #[unstable(feature = "int_roundings", issue = "88581")]
3421 #[must_use = "this returns the result of the operation, \
3422 without modifying the original"]
3423 #[inline]
3424 pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
3425 if rhs == -1 {
3427 return Some(self);
3428 }
3429
3430 let r = try_opt!(self.checked_rem(rhs));
3431 let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
3432 r + rhs
3434 } else {
3435 r
3436 };
3437
3438 if m == 0 {
3439 Some(self)
3440 } else {
3441 self.checked_add(rhs - m)
3443 }
3444 }
3445
3446 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".ilog(5), 1);")]
3462 #[stable(feature = "int_log", since = "1.67.0")]
3464 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3465 #[must_use = "this returns the result of the operation, \
3466 without modifying the original"]
3467 #[inline]
3468 #[track_caller]
3469 pub const fn ilog(self, base: Self) -> u32 {
3470 assert!(base >= 2, "base of integer logarithm must be at least 2");
3471 if let Some(log) = self.checked_ilog(base) {
3472 log
3473 } else {
3474 imp::int_log10::panic_for_nonpositive_argument()
3475 }
3476 }
3477
3478 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".ilog2(), 1);")]
3488 #[stable(feature = "int_log", since = "1.67.0")]
3490 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3491 #[must_use = "this returns the result of the operation, \
3492 without modifying the original"]
3493 #[inline]
3494 #[track_caller]
3495 pub const fn ilog2(self) -> u32 {
3496 if let Some(log) = self.checked_ilog2() {
3497 log
3498 } else {
3499 imp::int_log10::panic_for_nonpositive_argument()
3500 }
3501 }
3502
3503 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".ilog10(), 1);")]
3513 #[stable(feature = "int_log", since = "1.67.0")]
3515 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3516 #[must_use = "this returns the result of the operation, \
3517 without modifying the original"]
3518 #[inline]
3519 #[track_caller]
3520 pub const fn ilog10(self) -> u32 {
3521 if let Some(log) = self.checked_ilog10() {
3522 log
3523 } else {
3524 imp::int_log10::panic_for_nonpositive_argument()
3525 }
3526 }
3527
3528 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_ilog(5), Some(1));")]
3541 #[stable(feature = "int_log", since = "1.67.0")]
3543 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3544 #[must_use = "this returns the result of the operation, \
3545 without modifying the original"]
3546 #[inline]
3547 pub const fn checked_ilog(self, base: Self) -> Option<u32> {
3548 if self <= 0 || base <= 1 {
3549 None
3550 } else {
3551 (self as $UnsignedT).checked_ilog(base as $UnsignedT)
3554 }
3555 }
3556
3557 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_ilog2(), Some(1));")]
3568 #[stable(feature = "int_log", since = "1.67.0")]
3570 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3571 #[must_use = "this returns the result of the operation, \
3572 without modifying the original"]
3573 #[inline]
3574 pub const fn checked_ilog2(self) -> Option<u32> {
3575 if self <= 0 {
3576 None
3577 } else {
3578 let log = (Self::BITS - 1) - unsafe { intrinsics::ctlz_nonzero(self) as u32 };
3580 Some(log)
3581 }
3582 }
3583
3584 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_ilog10(), Some(1));")]
3592 #[stable(feature = "int_log", since = "1.67.0")]
3594 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3595 #[must_use = "this returns the result of the operation, \
3596 without modifying the original"]
3597 #[inline]
3598 pub const fn checked_ilog10(self) -> Option<u32> {
3599 imp::int_log10::$ActualT(self as $ActualT)
3600 }
3601
3602 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
3608 #[doc = concat!("`", stringify!($SelfT), "`,")]
3610 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
3614 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".abs(), 10);")]
3621 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").abs(), 10);")]
3622 #[stable(feature = "rust1", since = "1.0.0")]
3624 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3625 #[allow(unused_attributes)]
3626 #[must_use = "this returns the result of the operation, \
3627 without modifying the original"]
3628 #[inline]
3629 #[rustc_inherit_overflow_checks]
3630 pub const fn abs(self) -> Self {
3631 if self.is_negative() {
3635 -self
3636 } else {
3637 self
3638 }
3639 }
3640
3641 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(80), 20", stringify!($UnsignedT), ");")]
3650 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(110), 10", stringify!($UnsignedT), ");")]
3651 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(80), 180", stringify!($UnsignedT), ");")]
3652 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(-120), 20", stringify!($UnsignedT), ");")]
3653 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.abs_diff(", stringify!($SelfT), "::MAX), ", stringify!($UnsignedT), "::MAX);")]
3654 #[stable(feature = "int_abs_diff", since = "1.60.0")]
3656 #[rustc_const_stable(feature = "int_abs_diff", since = "1.60.0")]
3657 #[must_use = "this returns the result of the operation, \
3658 without modifying the original"]
3659 #[inline]
3660 pub const fn abs_diff(self, other: Self) -> $UnsignedT {
3661 if self < other {
3662 (other as $UnsignedT).wrapping_sub(self as $UnsignedT)
3676 } else {
3677 (self as $UnsignedT).wrapping_sub(other as $UnsignedT)
3678 }
3679 }
3680
3681 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".signum(), 1);")]
3691 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".signum(), 0);")]
3692 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").signum(), -1);")]
3693 #[stable(feature = "rust1", since = "1.0.0")]
3695 #[rustc_const_stable(feature = "const_int_sign", since = "1.47.0")]
3696 #[must_use = "this returns the result of the operation, \
3697 without modifying the original"]
3698 #[inline(always)]
3699 pub const fn signum(self) -> Self {
3700 crate::intrinsics::three_way_compare(self, 0) as Self
3706 }
3707
3708 #[doc = concat!("assert!(10", stringify!($SelfT), ".is_positive());")]
3715 #[doc = concat!("assert!(!(-10", stringify!($SelfT), ").is_positive());")]
3716 #[must_use]
3718 #[stable(feature = "rust1", since = "1.0.0")]
3719 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3720 #[inline(always)]
3721 pub const fn is_positive(self) -> bool { self > 0 }
3722
3723 #[doc = concat!("assert!((-10", stringify!($SelfT), ").is_negative());")]
3730 #[doc = concat!("assert!(!10", stringify!($SelfT), ".is_negative());")]
3731 #[must_use]
3733 #[stable(feature = "rust1", since = "1.0.0")]
3734 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3735 #[inline(always)]
3736 pub const fn is_negative(self) -> bool { self < 0 }
3737
3738 #[doc = $to_xe_bytes_doc]
3742 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();")]
3747 #[doc = concat!("assert_eq!(bytes, ", $be_bytes, ");")]
3748 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3750 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3751 #[must_use = "this returns the result of the operation, \
3752 without modifying the original"]
3753 #[inline]
3754 pub const fn to_be_bytes(self) -> [u8; size_of::<Self>()] {
3755 self.to_be().to_ne_bytes()
3756 }
3757
3758 #[doc = $to_xe_bytes_doc]
3762 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();")]
3767 #[doc = concat!("assert_eq!(bytes, ", $le_bytes, ");")]
3768 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3770 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3771 #[must_use = "this returns the result of the operation, \
3772 without modifying the original"]
3773 #[inline]
3774 pub const fn to_le_bytes(self) -> [u8; size_of::<Self>()] {
3775 self.to_le().to_ne_bytes()
3776 }
3777
3778 #[doc = $to_xe_bytes_doc]
3786 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_ne_bytes();")]
3794 #[doc = concat!(" ", $be_bytes)]
3798 #[doc = concat!(" ", $le_bytes)]
3800 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3804 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3805 #[allow(unnecessary_transmutes)]
3806 #[must_use = "this returns the result of the operation, \
3809 without modifying the original"]
3810 #[inline]
3811 pub const fn to_ne_bytes(self) -> [u8; size_of::<Self>()] {
3812 unsafe { mem::transmute(self) }
3815 }
3816
3817 #[doc = $from_xe_bytes_doc]
3821 #[doc = concat!("let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, ");")]
3826 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3827 #[doc = concat!("fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3833 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3834 #[doc = concat!(" ", stringify!($SelfT), "::from_be_bytes(int_bytes.try_into().unwrap())")]
3836 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3839 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3840 #[must_use]
3841 #[inline]
3842 pub const fn from_be_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3843 Self::from_be(Self::from_ne_bytes(bytes))
3844 }
3845
3846 #[doc = $from_xe_bytes_doc]
3850 #[doc = concat!("let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, ");")]
3855 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3856 #[doc = concat!("fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3862 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3863 #[doc = concat!(" ", stringify!($SelfT), "::from_le_bytes(int_bytes.try_into().unwrap())")]
3865 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3868 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3869 #[must_use]
3870 #[inline]
3871 pub const fn from_le_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3872 Self::from_le(Self::from_ne_bytes(bytes))
3873 }
3874
3875 #[doc = $from_xe_bytes_doc]
3886 #[doc = concat!("let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"big\") {")]
3891 #[doc = concat!(" ", $be_bytes)]
3892 #[doc = concat!(" ", $le_bytes)]
3894 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3896 #[doc = concat!("fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3902 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3903 #[doc = concat!(" ", stringify!($SelfT), "::from_ne_bytes(int_bytes.try_into().unwrap())")]
3905 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3908 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3909 #[allow(unnecessary_transmutes)]
3910 #[must_use]
3911 #[inline]
3914 pub const fn from_ne_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3915 unsafe { mem::transmute(bytes) }
3917 }
3918
3919 #[doc = concat!("[`", stringify!($SelfT), "::MIN", "`] instead.")]
3921 #[stable(feature = "rust1", since = "1.0.0")]
3924 #[inline(always)]
3925 #[rustc_promotable]
3926 #[rustc_const_stable(feature = "const_min_value", since = "1.32.0")]
3927 #[deprecated(since = "TBD", note = "replaced by the `MIN` associated constant on this type")]
3928 #[rustc_diagnostic_item = concat!(stringify!($SelfT), "_legacy_fn_min_value")]
3929 pub const fn min_value() -> Self {
3930 Self::MIN
3931 }
3932
3933 #[doc = concat!("[`", stringify!($SelfT), "::MAX", "`] instead.")]
3935 #[stable(feature = "rust1", since = "1.0.0")]
3938 #[inline(always)]
3939 #[rustc_promotable]
3940 #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
3941 #[deprecated(since = "TBD", note = "replaced by the `MAX` associated constant on this type")]
3942 #[rustc_diagnostic_item = concat!(stringify!($SelfT), "_legacy_fn_max_value")]
3943 pub const fn max_value() -> Self {
3944 Self::MAX
3945 }
3946
3947 #[doc = concat!("assert_eq!(120", stringify!($SelfT), ".clamp_magnitude(100), 100);")]
3959 #[doc = concat!("assert_eq!(-120", stringify!($SelfT), ".clamp_magnitude(100), -100);")]
3960 #[doc = concat!("assert_eq!(80", stringify!($SelfT), ".clamp_magnitude(100), 80);")]
3961 #[doc = concat!("assert_eq!(-80", stringify!($SelfT), ".clamp_magnitude(100), -80);")]
3962 #[must_use = "this returns the clamped value and does not modify the original"]
3964 #[unstable(feature = "clamp_magnitude", issue = "148519")]
3965 #[inline]
3966 pub fn clamp_magnitude(self, limit: $UnsignedT) -> Self {
3967 if let Ok(limit) = core::convert::TryInto::<$SelfT>::try_into(limit) {
3968 self.clamp(-limit, limit)
3969 } else {
3970 self
3971 }
3972 }
3973
3974 #[doc = concat!("assert_eq!(120i8, 120", stringify!($SelfT), ".truncate());")]
3982 #[doc = concat!("assert_eq!(-120i8, (-120", stringify!($SelfT), ").truncate());")]
3983 #[must_use = "this returns the truncated value and does not modify the original"]
3986 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
3987 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
3988 #[inline]
3989 pub const fn truncate<Target>(self) -> Target
3990 where Self: [const] traits::TruncateTarget<Target>
3991 {
3992 traits::TruncateTarget::internal_truncate(self)
3993 }
3994
3995 #[doc = concat!("assert_eq!(120i8, 120", stringify!($SelfT), ".saturating_truncate());")]
4003 #[doc = concat!("assert_eq!(-120i8, (-120", stringify!($SelfT), ").saturating_truncate());")]
4004 #[must_use = "this returns the truncated value and does not modify the original"]
4008 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
4009 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
4010 #[inline]
4011 pub const fn saturating_truncate<Target>(self) -> Target
4012 where Self: [const] traits::TruncateTarget<Target>
4013 {
4014 traits::TruncateTarget::internal_saturating_truncate(self)
4015 }
4016
4017 #[doc = concat!("assert_eq!(Some(120i8), 120", stringify!($SelfT), ".checked_truncate());")]
4025 #[doc = concat!("assert_eq!(Some(-120i8), (-120", stringify!($SelfT), ").checked_truncate());")]
4026 #[must_use = "this returns the truncated value and does not modify the original"]
4030 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
4031 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
4032 #[inline]
4033 pub const fn checked_truncate<Target>(self) -> Option<Target>
4034 where Self: [const] traits::TruncateTarget<Target>
4035 {
4036 traits::TruncateTarget::internal_checked_truncate(self)
4037 }
4038
4039 #[doc = concat!("assert_eq!(120i128, 120i8.widen());")]
4046 #[doc = concat!("assert_eq!(-120i128, (-120i8).widen());")]
4047 #[must_use = "this returns the widened value and does not modify the original"]
4049 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
4050 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
4051 #[inline]
4052 pub const fn widen<Target>(self) -> Target
4053 where Self: [const] traits::WidenTarget<Target>
4054 {
4055 traits::WidenTarget::internal_widen(self)
4056 }
4057
4058
4059 #[doc = concat!("assert_eq!(i8::MAX, ", stringify!($SelfT), "::MAX.saturating_cast());")]
4067 #[doc = concat!("assert_eq!(i8::MIN, ", stringify!($SelfT), "::MIN.saturating_cast());")]
4068 #[doc = concat!("assert_eq!(42u8, 42", stringify!($SelfT), ".saturating_cast());")]
4069 #[doc = concat!("assert_eq!(0u8, (-42", stringify!($SelfT), ").saturating_cast());")]
4070 #[must_use = "this returns the cast result and does not modify the original"]
4072 #[unstable(feature = "integer_casts", issue = "157388")]
4073 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4074 #[inline(always)]
4075 pub const fn saturating_cast<T: [const] BoundedCastFromInt<Self>>(self) -> T {
4076 T::saturating_cast_from(self)
4077 }
4078
4079 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX as i8, ", stringify!($SelfT), "::MAX.wrapping_cast());")]
4087 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN as i8, ", stringify!($SelfT), "::MIN.wrapping_cast());")]
4088 #[doc = concat!("assert_eq!(42u8, 42", stringify!($SelfT), ".wrapping_cast());")]
4089 #[doc = concat!("assert_eq!(u8::MAX - 41, (-42", stringify!($SelfT), ").wrapping_cast());")]
4090 #[must_use = "this returns the cast result and does not modify the original"]
4092 #[unstable(feature = "integer_casts", issue = "157388")]
4093 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4094 #[inline(always)]
4095 pub const fn wrapping_cast<T: [const] BoundedCastFromInt<Self>>(self) -> T {
4096 T::wrapping_cast_from(self)
4097 }
4098
4099 #[doc = concat!("assert_eq!(Some(42u8), 42", stringify!($SelfT), ".checked_cast());")]
4107 #[doc = concat!("assert_eq!((-42", stringify!($SelfT), ").checked_cast::<u8>(), None);")]
4108 #[must_use = "this returns the cast result and does not modify the original"]
4110 #[unstable(feature = "integer_casts", issue = "157388")]
4111 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4112 #[inline(always)]
4113 pub const fn checked_cast<T: [const] CheckedCastFromInt<Self>>(self) -> Option<T> {
4114 T::checked_cast_from(self)
4115 }
4116
4117 #[doc = concat!("assert_eq!(42u8, 42", stringify!($SelfT), ".strict_cast());")]
4129 #[doc = concat!("let _ = (-42", stringify!($SelfT), ").strict_cast::<u8>();")]
4136 #[must_use = "this returns the cast result and does not modify the original"]
4138 #[unstable(feature = "integer_casts", issue = "157388")]
4139 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4140 #[inline(always)]
4141 #[track_caller]
4142 pub const fn strict_cast<T: [const] CheckedCastFromInt<Self>>(self) -> T {
4143 T::strict_cast_from(self)
4144 }
4145
4146 #[must_use = "this returns the cast result and does not modify the original"]
4154 #[unstable(feature = "integer_casts", issue = "157388")]
4155 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4156 #[inline(always)]
4157 pub const unsafe fn unchecked_cast<T: [const] CheckedCastFromInt<Self>>(self) -> T {
4158 assert_unsafe_precondition!(
4159 check_language_ub,
4160 concat!(stringify!($SelfT), "::unchecked_cast must fit in the target type"),
4161 (
4162 in_bounds: bool = {
4164 let cast_val = self.checked_cast::<T>();
4165 let ret = cast_val.is_some();
4166 core::mem::forget(cast_val); ret
4168 },
4169 ) => in_bounds,
4170 );
4171
4172 unsafe { T::unchecked_cast_from(self) }
4174 }
4175 }
4176}