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), ");")]
993 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_div(-1);")]
999 #[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div(0);")]
1005 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1007 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1008 #[must_use = "this returns the result of the operation, \
1009 without modifying the original"]
1010 #[inline]
1011 #[track_caller]
1012 pub const fn strict_div(self, rhs: Self) -> Self {
1013 let (a, b) = self.overflowing_div(rhs);
1014 if b { imp::overflow_panic::div() } else { a }
1015 }
1016
1017 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div_euclid(-1), Some(", stringify!($Max), "));")]
1024 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div_euclid(-1), None);")]
1025 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div_euclid(0), None);")]
1026 #[stable(feature = "euclidean_division", since = "1.38.0")]
1028 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1029 #[must_use = "this returns the result of the operation, \
1030 without modifying the original"]
1031 #[inline]
1032 pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
1033 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
1035 None
1036 } else {
1037 Some(self.div_euclid(rhs))
1038 }
1039 }
1040
1041 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).strict_div_euclid(-1), ", stringify!($Max), ");")]
1060 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_div_euclid(-1);")]
1066 #[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div_euclid(0);")]
1072 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1074 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1075 #[must_use = "this returns the result of the operation, \
1076 without modifying the original"]
1077 #[inline]
1078 #[track_caller]
1079 pub const fn strict_div_euclid(self, rhs: Self) -> Self {
1080 let (a, b) = self.overflowing_div_euclid(rhs);
1081 if b { imp::overflow_panic::div() } else { a }
1082 }
1083
1084 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div_exact(-1), Some(", stringify!($Max), "));")]
1093 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_div_exact(2), None);")]
1094 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div_exact(-1), None);")]
1095 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div_exact(0), None);")]
1096 #[unstable(
1098 feature = "exact_div",
1099 issue = "139911",
1100 )]
1101 #[must_use = "this returns the result of the operation, \
1102 without modifying the original"]
1103 #[inline]
1104 pub const fn checked_div_exact(self, rhs: Self) -> Option<Self> {
1105 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
1106 None
1107 } else {
1108 unsafe {
1110 if intrinsics::unlikely(intrinsics::unchecked_rem(self, rhs) != 0) {
1111 None
1112 } else {
1113 Some(intrinsics::exact_div(self, rhs))
1114 }
1115 }
1116 }
1117 }
1118
1119 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".div_exact(2), Some(32));")]
1135 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".div_exact(32), Some(2));")]
1136 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).div_exact(-1), Some(", stringify!($Max), "));")]
1137 #[doc = concat!("assert_eq!(65", stringify!($SelfT), ".div_exact(2), None);")]
1138 #[doc = concat!("let _ = 64", stringify!($SelfT),".div_exact(0);")]
1142 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.div_exact(-1);")]
1146 #[unstable(
1148 feature = "exact_div",
1149 issue = "139911",
1150 )]
1151 #[must_use = "this returns the result of the operation, \
1152 without modifying the original"]
1153 #[inline]
1154 #[rustc_inherit_overflow_checks]
1155 pub const fn div_exact(self, rhs: Self) -> Option<Self> {
1156 if self % rhs != 0 {
1157 None
1158 } else {
1159 Some(self / rhs)
1160 }
1161 }
1162
1163 #[doc = concat!("`self == ", stringify!($SelfT), "::MIN && rhs == -1`,")]
1169 #[unstable(
1171 feature = "exact_div",
1172 issue = "139911",
1173 )]
1174 #[must_use = "this returns the result of the operation, \
1175 without modifying the original"]
1176 #[inline]
1177 pub const unsafe fn unchecked_div_exact(self, rhs: Self) -> Self {
1178 assert_unsafe_precondition!(
1179 check_language_ub,
1180 concat!(stringify!($SelfT), "::unchecked_div_exact cannot overflow, divide by zero, or leave a remainder"),
1181 (
1182 lhs: $SelfT = self,
1183 rhs: $SelfT = rhs,
1184 ) => rhs > 0 && lhs % rhs == 0 && (lhs != <$SelfT>::MIN || rhs != -1),
1185 );
1186 unsafe { intrinsics::exact_div(self, rhs) }
1188 }
1189
1190 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(2), Some(1));")]
1197 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(0), None);")]
1198 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem(-1), None);")]
1199 #[stable(feature = "wrapping", since = "1.7.0")]
1201 #[rustc_const_stable(feature = "const_checked_int_div", since = "1.52.0")]
1202 #[must_use = "this returns the result of the operation, \
1203 without modifying the original"]
1204 #[inline]
1205 pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
1206 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
1207 None
1208 } else {
1209 Some(unsafe { intrinsics::unchecked_rem(self, rhs) })
1211 }
1212 }
1213
1214 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_rem(2), 1);")]
1232 #[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem(0);")]
1238 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_rem(-1);")]
1244 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1246 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1247 #[must_use = "this returns the result of the operation, \
1248 without modifying the original"]
1249 #[inline]
1250 #[track_caller]
1251 pub const fn strict_rem(self, rhs: Self) -> Self {
1252 let (a, b) = self.overflowing_rem(rhs);
1253 if b { imp::overflow_panic::rem() } else { a }
1254 }
1255
1256 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(2), Some(1));")]
1263 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(0), None);")]
1264 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem_euclid(-1), None);")]
1265 #[stable(feature = "euclidean_division", since = "1.38.0")]
1267 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1268 #[must_use = "this returns the result of the operation, \
1269 without modifying the original"]
1270 #[inline]
1271 pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
1272 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
1274 None
1275 } else {
1276 Some(self.rem_euclid(rhs))
1277 }
1278 }
1279
1280 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_rem_euclid(2), 1);")]
1298 #[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem_euclid(0);")]
1304 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_rem_euclid(-1);")]
1310 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1312 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1313 #[must_use = "this returns the result of the operation, \
1314 without modifying the original"]
1315 #[inline]
1316 #[track_caller]
1317 pub const fn strict_rem_euclid(self, rhs: Self) -> Self {
1318 let (a, b) = self.overflowing_rem_euclid(rhs);
1319 if b { imp::overflow_panic::rem() } else { a }
1320 }
1321
1322 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_neg(), Some(-5));")]
1328 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_neg(), None);")]
1329 #[stable(feature = "wrapping", since = "1.7.0")]
1331 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1332 #[must_use = "this returns the result of the operation, \
1333 without modifying the original"]
1334 #[inline]
1335 pub const fn checked_neg(self) -> Option<Self> {
1336 let (a, b) = self.overflowing_neg();
1337 if intrinsics::unlikely(b) { None } else { Some(a) }
1338 }
1339
1340 #[doc = concat!("`self == ", stringify!($SelfT), "::MIN`,")]
1346 #[doc = concat!("[`checked_neg`]: ", stringify!($SelfT), "::checked_neg")]
1349 #[stable(feature = "unchecked_neg", since = "1.93.0")]
1350 #[rustc_const_stable(feature = "unchecked_neg", since = "1.93.0")]
1351 #[must_use = "this returns the result of the operation, \
1352 without modifying the original"]
1353 #[inline(always)]
1354 #[track_caller]
1355 pub const unsafe fn unchecked_neg(self) -> Self {
1356 assert_unsafe_precondition!(
1357 check_language_ub,
1358 concat!(stringify!($SelfT), "::unchecked_neg cannot overflow"),
1359 (
1360 lhs: $SelfT = self,
1361 ) => !lhs.overflowing_neg().1,
1362 );
1363
1364 unsafe {
1366 intrinsics::unchecked_sub(0, self)
1367 }
1368 }
1369
1370 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_neg(), -5);")]
1382 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_neg();")]
1388 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1390 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1391 #[must_use = "this returns the result of the operation, \
1392 without modifying the original"]
1393 #[inline]
1394 #[track_caller]
1395 pub const fn strict_neg(self) -> Self {
1396 let (a, b) = self.overflowing_neg();
1397 if b { imp::overflow_panic::neg() } else { a }
1398 }
1399
1400 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(4), Some(0x10));")]
1407 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(129), None);")]
1408 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shl(", stringify!($BITS_MINUS_ONE), "), Some(0));")]
1409 #[stable(feature = "wrapping", since = "1.7.0")]
1411 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1412 #[must_use = "this returns the result of the operation, \
1413 without modifying the original"]
1414 #[inline]
1415 pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
1416 if rhs < Self::BITS {
1418 Some(unsafe { self.unchecked_shl(rhs) })
1420 } else {
1421 None
1422 }
1423 }
1424
1425 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".strict_shl(4), 0x10);")]
1438 #[doc = concat!("let _ = 0x1", stringify!($SelfT), ".strict_shl(129);")]
1444 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1446 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1447 #[must_use = "this returns the result of the operation, \
1448 without modifying the original"]
1449 #[inline]
1450 #[track_caller]
1451 pub const fn strict_shl(self, rhs: u32) -> Self {
1452 let (a, b) = self.overflowing_shl(rhs);
1453 if b { imp::overflow_panic::shl() } else { a }
1454 }
1455
1456 #[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
1466 #[stable(feature = "unchecked_shifts", since = "1.93.0")]
1467 #[rustc_const_stable(feature = "unchecked_shifts", since = "1.93.0")]
1468 #[must_use = "this returns the result of the operation, \
1469 without modifying the original"]
1470 #[inline(always)]
1471 #[track_caller]
1472 pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
1473 assert_unsafe_precondition!(
1474 check_language_ub,
1475 concat!(stringify!($SelfT), "::unchecked_shl cannot overflow"),
1476 (
1477 rhs: u32 = rhs,
1478 ) => rhs < <$ActualT>::BITS,
1479 );
1480
1481 unsafe {
1483 intrinsics::unchecked_shl(self, rhs)
1484 }
1485 }
1486
1487 #[doc = concat!("assert_eq!(0x1_", stringify!($SelfT), ".unbounded_shl(4), 0x10);")]
1496 #[doc = concat!("assert_eq!(0x1_", stringify!($SelfT), ".unbounded_shl(129), 0);")]
1497 #[doc = concat!("assert_eq!(0b101_", stringify!($SelfT), ".unbounded_shl(0), 0b101);")]
1498 #[doc = concat!("assert_eq!(0b101_", stringify!($SelfT), ".unbounded_shl(1), 0b1010);")]
1499 #[doc = concat!("assert_eq!(0b101_", stringify!($SelfT), ".unbounded_shl(2), 0b10100);")]
1500 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shl(", stringify!($BITS), "), 0);")]
1501 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shl(1).unbounded_shl(", stringify!($BITS_MINUS_ONE), "), 0);")]
1502 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shl(", stringify!($BITS), "), 0);")]
1503 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shl(1).unbounded_shl(", stringify!($BITS_MINUS_ONE), "), 0);")]
1504 #[stable(feature = "unbounded_shifts", since = "1.87.0")]
1506 #[rustc_const_stable(feature = "unbounded_shifts", since = "1.87.0")]
1507 #[must_use = "this returns the result of the operation, \
1508 without modifying the original"]
1509 #[inline]
1510 pub const fn unbounded_shl(self, rhs: u32) -> $SelfT{
1511 if rhs < Self::BITS {
1512 unsafe { self.unchecked_shl(rhs) }
1515 } else {
1516 0
1517 }
1518 }
1519
1520 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1525 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".shl_exact(4), Some(0x10));")]
1533 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".shl_exact(", stringify!($SelfT), "::BITS - 2), Some(1 << ", stringify!($SelfT), "::BITS - 2));")]
1534 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".shl_exact(", stringify!($SelfT), "::BITS - 1), None);")]
1535 #[doc = concat!("assert_eq!((-0x2", stringify!($SelfT), ").shl_exact(", stringify!($SelfT), "::BITS - 2), Some(-0x2 << ", stringify!($SelfT), "::BITS - 2));")]
1536 #[doc = concat!("assert_eq!((-0x2", stringify!($SelfT), ").shl_exact(", stringify!($SelfT), "::BITS - 1), None);")]
1537 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1539 #[must_use = "this returns the result of the operation, \
1540 without modifying the original"]
1541 #[inline]
1542 pub const fn shl_exact(self, rhs: u32) -> Option<$SelfT> {
1543 if rhs < self.leading_zeros() || rhs < self.leading_ones() {
1544 Some(unsafe { self.unchecked_shl(rhs) })
1546 } else {
1547 None
1548 }
1549 }
1550
1551 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1554 #[doc = concat!("[`", stringify!($SelfT), "::shl_exact`]")]
1560 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1562 #[must_use = "this returns the result of the operation, \
1563 without modifying the original"]
1564 #[inline]
1565 pub const unsafe fn unchecked_shl_exact(self, rhs: u32) -> $SelfT {
1566 assert_unsafe_precondition!(
1567 check_library_ub,
1568 concat!(stringify!($SelfT), "::unchecked_shl_exact cannot shift out bits that would change the value of the first bit"),
1569 (
1570 zeros: u32 = self.leading_zeros(),
1571 ones: u32 = self.leading_ones(),
1572 rhs: u32 = rhs,
1573 ) => rhs < zeros || rhs < ones,
1574 );
1575
1576 unsafe { self.unchecked_shl(rhs) }
1578 }
1579
1580 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(4), Some(0x1));")]
1587 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(128), None);")]
1588 #[stable(feature = "wrapping", since = "1.7.0")]
1590 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1591 #[must_use = "this returns the result of the operation, \
1592 without modifying the original"]
1593 #[inline]
1594 pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
1595 if rhs < Self::BITS {
1597 Some(unsafe { self.unchecked_shr(rhs) })
1599 } else {
1600 None
1601 }
1602 }
1603
1604 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".strict_shr(4), 0x1);")]
1617 #[doc = concat!("let _ = 0x10", stringify!($SelfT), ".strict_shr(128);")]
1623 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1625 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1626 #[must_use = "this returns the result of the operation, \
1627 without modifying the original"]
1628 #[inline]
1629 #[track_caller]
1630 pub const fn strict_shr(self, rhs: u32) -> Self {
1631 let (a, b) = self.overflowing_shr(rhs);
1632 if b { imp::overflow_panic::shr() } else { a }
1633 }
1634
1635 #[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
1645 #[stable(feature = "unchecked_shifts", since = "1.93.0")]
1646 #[rustc_const_stable(feature = "unchecked_shifts", since = "1.93.0")]
1647 #[must_use = "this returns the result of the operation, \
1648 without modifying the original"]
1649 #[inline(always)]
1650 #[track_caller]
1651 pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
1652 assert_unsafe_precondition!(
1653 check_language_ub,
1654 concat!(stringify!($SelfT), "::unchecked_shr cannot overflow"),
1655 (
1656 rhs: u32 = rhs,
1657 ) => rhs < <$ActualT>::BITS,
1658 );
1659
1660 unsafe {
1662 intrinsics::unchecked_shr(self, rhs)
1663 }
1664 }
1665
1666 #[doc = concat!("assert_eq!(0x10_", stringify!($SelfT), ".unbounded_shr(4), 0x1);")]
1676 #[doc = concat!("assert_eq!(0x10_", stringify!($SelfT), ".unbounded_shr(129), 0);")]
1677 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.unbounded_shr(129), -1);")]
1678 #[doc = concat!("assert_eq!(0b1010_", stringify!($SelfT), ".unbounded_shr(0), 0b1010);")]
1679 #[doc = concat!("assert_eq!(0b1010_", stringify!($SelfT), ".unbounded_shr(1), 0b101);")]
1680 #[doc = concat!("assert_eq!(0b1010_", stringify!($SelfT), ".unbounded_shr(2), 0b10);")]
1681 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shr(", stringify!($BITS), "), 0);")]
1682 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shr(1).unbounded_shr(", stringify!($BITS_MINUS_ONE), "), 0);")]
1683 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shr(", stringify!($BITS), "), -1);")]
1684 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shr(1).unbounded_shr(", stringify!($BITS_MINUS_ONE), "), -1);")]
1685 #[stable(feature = "unbounded_shifts", since = "1.87.0")]
1687 #[rustc_const_stable(feature = "unbounded_shifts", since = "1.87.0")]
1688 #[must_use = "this returns the result of the operation, \
1689 without modifying the original"]
1690 #[inline]
1691 pub const fn unbounded_shr(self, rhs: u32) -> $SelfT{
1692 if rhs < Self::BITS {
1693 unsafe { self.unchecked_shr(rhs) }
1696 } else {
1697 unsafe { self.unchecked_shr(Self::BITS - 1) }
1702 }
1703 }
1704
1705 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1709 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".shr_exact(4), Some(0x1));")]
1717 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".shr_exact(5), None);")]
1718 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1720 #[must_use = "this returns the result of the operation, \
1721 without modifying the original"]
1722 #[inline]
1723 pub const fn shr_exact(self, rhs: u32) -> Option<$SelfT> {
1724 if rhs <= self.trailing_zeros() && rhs < <$SelfT>::BITS {
1725 Some(unsafe { self.unchecked_shr(rhs) })
1727 } else {
1728 None
1729 }
1730 }
1731
1732 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1735 #[doc = concat!(stringify!($SelfT), "::BITS`")]
1740 #[doc = concat!("[`", stringify!($SelfT), "::shr_exact`]")]
1742 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1744 #[must_use = "this returns the result of the operation, \
1745 without modifying the original"]
1746 #[inline]
1747 pub const unsafe fn unchecked_shr_exact(self, rhs: u32) -> $SelfT {
1748 assert_unsafe_precondition!(
1749 check_library_ub,
1750 concat!(stringify!($SelfT), "::unchecked_shr_exact cannot shift out non-zero bits"),
1751 (
1752 zeros: u32 = self.trailing_zeros(),
1753 bits: u32 = <$SelfT>::BITS,
1754 rhs: u32 = rhs,
1755 ) => rhs <= zeros && rhs < bits,
1756 );
1757
1758 unsafe { self.unchecked_shr(rhs) }
1760 }
1761
1762 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_abs(), Some(5));")]
1769 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_abs(), None);")]
1770 #[stable(feature = "no_panic_abs", since = "1.13.0")]
1772 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1773 #[must_use = "this returns the result of the operation, \
1774 without modifying the original"]
1775 #[inline]
1776 pub const fn checked_abs(self) -> Option<Self> {
1777 if self.is_negative() {
1778 self.checked_neg()
1779 } else {
1780 Some(self)
1781 }
1782 }
1783
1784 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").strict_abs(), 5);")]
1797 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_abs();")]
1803 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1805 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1806 #[must_use = "this returns the result of the operation, \
1807 without modifying the original"]
1808 #[inline]
1809 #[track_caller]
1810 pub const fn strict_abs(self) -> Self {
1811 if self.is_negative() {
1812 self.strict_neg()
1813 } else {
1814 self
1815 }
1816 }
1817
1818 #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".checked_pow(2), Some(64));")]
1825 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".checked_pow(0), Some(1));")]
1826 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);")]
1827 #[stable(feature = "no_panic_pow", since = "1.34.0")]
1830 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1831 #[must_use = "this returns the result of the operation, \
1832 without modifying the original"]
1833 #[inline]
1834 pub const fn checked_pow(self, mut exp: u32) -> Option<Self> {
1835 let mut base = self;
1836 let mut acc: Self = 1;
1837
1838 if intrinsics::is_val_statically_known(base) && base.unsigned_abs().is_power_of_two() {
1839 let k = base.unsigned_abs().ilog2();
1840 let shift = try_opt!(k.checked_mul(exp));
1841 return if base < 0 && (exp % 2) == 1 {
1842 (-1 as Self).shl_exact(shift)
1843 } else {
1844 (1 as Self).shl_exact(shift)
1845 }
1846 }
1847
1848 if exp == 0 {
1849 return Some(1);
1850 }
1851
1852 if intrinsics::is_val_statically_known(exp) {
1853 while exp > 1 {
1854 if (exp & 1) == 1 {
1855 acc = try_opt!(acc.checked_mul(base));
1856 }
1857 exp /= 2;
1858 base = try_opt!(base.checked_mul(base));
1859 }
1860
1861 return acc.checked_mul(base);
1866 }
1867
1868 loop {
1869 if (exp & 1) == 1 {
1870 acc = try_opt!(acc.checked_mul(base));
1871 if exp == 1 {
1873 return Some(acc);
1874 }
1875 }
1876 exp /= 2;
1877 base = try_opt!(base.checked_mul(base));
1878 }
1879 }
1880
1881 #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".strict_pow(2), 64);")]
1894 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".strict_pow(0), 1);")]
1895 #[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_pow(2);")]
1901 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1903 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1904 #[must_use = "this returns the result of the operation, \
1905 without modifying the original"]
1906 #[inline]
1907 #[track_caller]
1908 pub const fn strict_pow(self, exp: u32) -> Self {
1909 match self.checked_pow(exp) {
1910 Some(x) => x,
1911 None => imp::overflow_panic::pow(),
1912 }
1913 }
1914
1915 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_isqrt(), Some(3));")]
1927 #[stable(feature = "isqrt", since = "1.84.0")]
1929 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
1930 #[must_use = "this returns the result of the operation, \
1931 without modifying the original"]
1932 #[inline]
1933 pub const fn checked_isqrt(self) -> Option<Self> {
1934 if self < 0 {
1935 None
1936 } else {
1937 let result = self.cast_unsigned().isqrt().cast_signed();
1940
1941 unsafe {
1953 const MAX_RESULT: $SelfT = <$SelfT>::MAX.cast_unsigned().isqrt().cast_signed();
1954 crate::hint::assert_unchecked(result <= MAX_RESULT);
1955 }
1956 Some(result)
1957 }
1958 }
1959
1960 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_add(1), 101);")]
1967 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add(100), ", stringify!($SelfT), "::MAX);")]
1968 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_add(-1), ", stringify!($SelfT), "::MIN);")]
1969 #[stable(feature = "rust1", since = "1.0.0")]
1972 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1973 #[must_use = "this returns the result of the operation, \
1974 without modifying the original"]
1975 #[inline(always)]
1976 pub const fn saturating_add(self, rhs: Self) -> Self {
1977 intrinsics::saturating_add(self, rhs)
1978 }
1979
1980 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".saturating_add_unsigned(2), 3);")]
1987 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add_unsigned(100), ", stringify!($SelfT), "::MAX);")]
1988 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1990 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1991 #[must_use = "this returns the result of the operation, \
1992 without modifying the original"]
1993 #[inline]
1994 pub const fn saturating_add_unsigned(self, rhs: $UnsignedT) -> Self {
1995 match self.checked_add_unsigned(rhs) {
1998 Some(x) => x,
1999 None => Self::MAX,
2000 }
2001 }
2002
2003 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub(127), -27);")]
2010 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub(100), ", stringify!($SelfT), "::MIN);")]
2011 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_sub(-1), ", stringify!($SelfT), "::MAX);")]
2012 #[stable(feature = "rust1", since = "1.0.0")]
2014 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2015 #[must_use = "this returns the result of the operation, \
2016 without modifying the original"]
2017 #[inline(always)]
2018 pub const fn saturating_sub(self, rhs: Self) -> Self {
2019 intrinsics::saturating_sub(self, rhs)
2020 }
2021
2022 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub_unsigned(127), -27);")]
2029 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub_unsigned(100), ", stringify!($SelfT), "::MIN);")]
2030 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2032 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2033 #[must_use = "this returns the result of the operation, \
2034 without modifying the original"]
2035 #[inline]
2036 pub const fn saturating_sub_unsigned(self, rhs: $UnsignedT) -> Self {
2037 match self.checked_sub_unsigned(rhs) {
2040 Some(x) => x,
2041 None => Self::MIN,
2042 }
2043 }
2044
2045 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_neg(), -100);")]
2052 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_neg(), 100);")]
2053 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_neg(), ", stringify!($SelfT), "::MAX);")]
2054 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_neg(), ", stringify!($SelfT), "::MIN + 1);")]
2055 #[stable(feature = "saturating_neg", since = "1.45.0")]
2058 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2059 #[must_use = "this returns the result of the operation, \
2060 without modifying the original"]
2061 #[inline(always)]
2062 pub const fn saturating_neg(self) -> Self {
2063 intrinsics::saturating_sub(0, self)
2064 }
2065
2066 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_abs(), 100);")]
2073 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_abs(), 100);")]
2074 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_abs(), ", stringify!($SelfT), "::MAX);")]
2075 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).saturating_abs(), ", stringify!($SelfT), "::MAX);")]
2076 #[stable(feature = "saturating_neg", since = "1.45.0")]
2079 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2080 #[must_use = "this returns the result of the operation, \
2081 without modifying the original"]
2082 #[inline]
2083 pub const fn saturating_abs(self) -> Self {
2084 if self.is_negative() {
2085 self.saturating_neg()
2086 } else {
2087 self
2088 }
2089 }
2090
2091 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".saturating_mul(12), 120);")]
2098 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_mul(10), ", stringify!($SelfT), "::MAX);")]
2099 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_mul(10), ", stringify!($SelfT), "::MIN);")]
2100 #[stable(feature = "wrapping", since = "1.7.0")]
2102 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2103 #[must_use = "this returns the result of the operation, \
2104 without modifying the original"]
2105 #[inline]
2106 pub const fn saturating_mul(self, rhs: Self) -> Self {
2107 match self.checked_mul(rhs) {
2108 Some(x) => x,
2109 None => if (self < 0) == (rhs < 0) {
2110 Self::MAX
2111 } else {
2112 Self::MIN
2113 }
2114 }
2115 }
2116
2117 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
2128 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_div(-1), ", stringify!($SelfT), "::MIN + 1);")]
2129 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_div(-1), ", stringify!($SelfT), "::MAX);")]
2130 #[stable(feature = "saturating_div", since = "1.58.0")]
2133 #[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
2134 #[must_use = "this returns the result of the operation, \
2135 without modifying the original"]
2136 #[inline]
2137 pub const fn saturating_div(self, rhs: Self) -> Self {
2138 match self.overflowing_div(rhs) {
2139 (result, false) => result,
2140 (_result, true) => Self::MAX, }
2142 }
2143
2144 #[doc = concat!("assert_eq!((-4", stringify!($SelfT), ").saturating_pow(3), -64);")]
2151 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".saturating_pow(0), 1);")]
2152 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(2), ", stringify!($SelfT), "::MAX);")]
2153 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(3), ", stringify!($SelfT), "::MIN);")]
2154 #[stable(feature = "no_panic_pow", since = "1.34.0")]
2156 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2157 #[must_use = "this returns the result of the operation, \
2158 without modifying the original"]
2159 #[inline]
2160 pub const fn saturating_pow(self, exp: u32) -> Self {
2161 match self.checked_pow(exp) {
2162 Some(x) => x,
2163 None if self < 0 && exp % 2 == 1 => Self::MIN,
2164 None => Self::MAX,
2165 }
2166 }
2167
2168 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add(27), 127);")]
2175 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add(2), ", stringify!($SelfT), "::MIN + 1);")]
2176 #[stable(feature = "rust1", since = "1.0.0")]
2178 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2179 #[must_use = "this returns the result of the operation, \
2180 without modifying the original"]
2181 #[inline(always)]
2182 pub const fn wrapping_add(self, rhs: Self) -> Self {
2183 intrinsics::wrapping_add(self, rhs)
2184 }
2185
2186 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add_unsigned(27), 127);")]
2193 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add_unsigned(2), ", stringify!($SelfT), "::MIN + 1);")]
2194 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2196 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2197 #[must_use = "this returns the result of the operation, \
2198 without modifying the original"]
2199 #[inline(always)]
2200 pub const fn wrapping_add_unsigned(self, rhs: $UnsignedT) -> Self {
2201 self.wrapping_add(rhs as Self)
2202 }
2203
2204 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub(127), -127);")]
2211 #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub(", stringify!($SelfT), "::MAX), ", stringify!($SelfT), "::MAX);")]
2212 #[stable(feature = "rust1", since = "1.0.0")]
2214 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2215 #[must_use = "this returns the result of the operation, \
2216 without modifying the original"]
2217 #[inline(always)]
2218 pub const fn wrapping_sub(self, rhs: Self) -> Self {
2219 intrinsics::wrapping_sub(self, rhs)
2220 }
2221
2222 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub_unsigned(127), -127);")]
2229 #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub_unsigned(", stringify!($UnsignedT), "::MAX), -1);")]
2230 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2232 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2233 #[must_use = "this returns the result of the operation, \
2234 without modifying the original"]
2235 #[inline(always)]
2236 pub const fn wrapping_sub_unsigned(self, rhs: $UnsignedT) -> Self {
2237 self.wrapping_sub(rhs as Self)
2238 }
2239
2240 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".wrapping_mul(12), 120);")]
2247 #[stable(feature = "rust1", since = "1.0.0")]
2250 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2251 #[must_use = "this returns the result of the operation, \
2252 without modifying the original"]
2253 #[inline(always)]
2254 pub const fn wrapping_mul(self, rhs: Self) -> Self {
2255 intrinsics::wrapping_mul(self, rhs)
2256 }
2257
2258 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div(10), 10);")]
2273 #[stable(feature = "num_wrapping", since = "1.2.0")]
2276 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
2277 #[must_use = "this returns the result of the operation, \
2278 without modifying the original"]
2279 #[inline]
2280 pub const fn wrapping_div(self, rhs: Self) -> Self {
2281 self.overflowing_div(rhs).0
2282 }
2283
2284 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div_euclid(10), 10);")]
2299 #[stable(feature = "euclidean_division", since = "1.38.0")]
2302 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2303 #[must_use = "this returns the result of the operation, \
2304 without modifying the original"]
2305 #[inline]
2306 pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
2307 self.overflowing_div_euclid(rhs).0
2308 }
2309
2310 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem(10), 0);")]
2325 #[stable(feature = "num_wrapping", since = "1.2.0")]
2328 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
2329 #[must_use = "this returns the result of the operation, \
2330 without modifying the original"]
2331 #[inline]
2332 pub const fn wrapping_rem(self, rhs: Self) -> Self {
2333 self.overflowing_rem(rhs).0
2334 }
2335
2336 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);")]
2350 #[stable(feature = "euclidean_division", since = "1.38.0")]
2353 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2354 #[must_use = "this returns the result of the operation, \
2355 without modifying the original"]
2356 #[inline]
2357 pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
2358 self.overflowing_rem_euclid(rhs).0
2359 }
2360
2361 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_neg(), -100);")]
2372 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_neg(), 100);")]
2373 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_neg(), ", stringify!($SelfT), "::MIN);")]
2374 #[stable(feature = "num_wrapping", since = "1.2.0")]
2376 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2377 #[must_use = "this returns the result of the operation, \
2378 without modifying the original"]
2379 #[inline(always)]
2380 pub const fn wrapping_neg(self) -> Self {
2381 (0 as $SelfT).wrapping_sub(self)
2382 }
2383
2384 #[doc = concat!("assert_eq!((-1_", stringify!($SelfT), ").wrapping_shl(7), -128);")]
2403 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shl(", stringify!($BITS), "), 42);")]
2404 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shl(1).wrapping_shl(", stringify!($BITS_MINUS_ONE), "), 0);")]
2405 #[doc = concat!("assert_eq!((-1_", stringify!($SelfT), ").wrapping_shl(128), -1);")]
2406 #[doc = concat!("assert_eq!(5_", stringify!($SelfT), ".wrapping_shl(1025), 10);")]
2407 #[stable(feature = "num_wrapping", since = "1.2.0")]
2409 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2410 #[must_use = "this returns the result of the operation, \
2411 without modifying the original"]
2412 #[inline(always)]
2413 pub const fn wrapping_shl(self, rhs: u32) -> Self {
2414 unsafe {
2417 self.unchecked_shl(rhs & (Self::BITS - 1))
2418 }
2419 }
2420
2421 #[doc = concat!("assert_eq!((-128_", stringify!($SelfT), ").wrapping_shr(7), -1);")]
2440 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shr(", stringify!($BITS), "), 42);")]
2441 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shr(1).wrapping_shr(", stringify!($BITS_MINUS_ONE), "), 0);")]
2442 #[doc = concat!("assert_eq!(10_", stringify!($SelfT), ".wrapping_shr(1025), 5);")]
2444 #[stable(feature = "num_wrapping", since = "1.2.0")]
2446 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2447 #[must_use = "this returns the result of the operation, \
2448 without modifying the original"]
2449 #[inline(always)]
2450 pub const fn wrapping_shr(self, rhs: u32) -> Self {
2451 unsafe {
2454 self.unchecked_shr(rhs & (Self::BITS - 1))
2455 }
2456 }
2457
2458 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_abs(), 100);")]
2469 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_abs(), 100);")]
2470 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_abs(), ", stringify!($SelfT), "::MIN);")]
2471 #[stable(feature = "no_panic_abs", since = "1.13.0")]
2474 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2475 #[must_use = "this returns the result of the operation, \
2476 without modifying the original"]
2477 #[allow(unused_attributes)]
2478 #[inline]
2479 pub const fn wrapping_abs(self) -> Self {
2480 if self.is_negative() {
2481 self.wrapping_neg()
2482 } else {
2483 self
2484 }
2485 }
2486
2487 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".unsigned_abs(), 100", stringify!($UnsignedT), ");")]
2495 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").unsigned_abs(), 100", stringify!($UnsignedT), ");")]
2496 #[stable(feature = "unsigned_abs", since = "1.51.0")]
2499 #[rustc_const_stable(feature = "unsigned_abs", since = "1.51.0")]
2500 #[must_use = "this returns the result of the operation, \
2501 without modifying the original"]
2502 #[inline]
2503 pub const fn unsigned_abs(self) -> $UnsignedT {
2504 self.wrapping_abs() as $UnsignedT
2505 }
2506
2507 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".wrapping_pow(4), 81);")]
2514 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".wrapping_pow(0), 1);")]
2517 #[stable(feature = "no_panic_pow", since = "1.34.0")]
2519 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2520 #[must_use = "this returns the result of the operation, \
2521 without modifying the original"]
2522 #[inline]
2523 pub const fn wrapping_pow(self, exp: u32) -> Self {
2524 let (a, _) = self.overflowing_pow(exp);
2525 a
2526 }
2527
2528 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_add(2), (7, false));")]
2539 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.overflowing_add(1), (", stringify!($SelfT), "::MIN, true));")]
2540 #[stable(feature = "wrapping", since = "1.7.0")]
2542 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2543 #[must_use = "this returns the result of the operation, \
2544 without modifying the original"]
2545 #[inline(always)]
2546 pub const fn overflowing_add(self, rhs: Self) -> (Self, bool) {
2547 let (a, b) = intrinsics::add_with_overflow(self as $ActualT, rhs as $ActualT);
2548 (a as Self, b)
2549 }
2550
2551 #[doc = concat!("[`", stringify!($UnsignedT), "::carrying_add`]")]
2563 #[doc = concat!("// 10 MAX (a = 10 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")]
2581 #[doc = concat!("// + -5 9 (b = -5 × 2^", stringify!($BITS), " + 9)")]
2582 #[doc = concat!("// 6 8 (sum = 6 × 2^", stringify!($BITS), " + 8)")]
2584 #[doc = concat!("let (a1, a0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (10, ", stringify!($UnsignedT), "::MAX);")]
2586 #[doc = concat!("let (b1, b0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (-5, 9);")]
2587 #[doc = concat!("// ", stringify!($UnsignedT), "::carrying_add for the less significant words")]
2590 #[doc = concat!("// ", stringify!($SelfT), "::carrying_add for the most significant word")]
2594 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2600 #[must_use = "this returns the result of the operation, \
2601 without modifying the original"]
2602 #[inline]
2603 pub const fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool) {
2604 let (a, b) = self.overflowing_add(rhs);
2607 let (c, d) = a.overflowing_add(carry as $SelfT);
2608 (c, b != d)
2609 }
2610
2611 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_add_unsigned(2), (3, false));")]
2621 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_add_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MAX, false));")]
2622 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).overflowing_add_unsigned(3), (", stringify!($SelfT), "::MIN, true));")]
2623 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2625 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2626 #[must_use = "this returns the result of the operation, \
2627 without modifying the original"]
2628 #[inline]
2629 pub const fn overflowing_add_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
2630 let rhs = rhs as Self;
2631 let (res, overflowed) = self.overflowing_add(rhs);
2632 (res, overflowed ^ (rhs < 0))
2633 }
2634
2635 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_sub(2), (3, false));")]
2645 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_sub(1), (", stringify!($SelfT), "::MAX, true));")]
2646 #[stable(feature = "wrapping", since = "1.7.0")]
2648 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2649 #[must_use = "this returns the result of the operation, \
2650 without modifying the original"]
2651 #[inline(always)]
2652 pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
2653 let (a, b) = intrinsics::sub_with_overflow(self as $ActualT, rhs as $ActualT);
2654 (a as Self, b)
2655 }
2656
2657 #[doc = concat!("[`", stringify!($UnsignedT), "::borrowing_sub`]")]
2670 #[doc = concat!("// 6 8 (a = 6 × 2^", stringify!($BITS), " + 8)")]
2688 #[doc = concat!("// - -5 9 (b = -5 × 2^", stringify!($BITS), " + 9)")]
2689 #[doc = concat!("// 10 MAX (diff = 10 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")]
2691 #[doc = concat!("let (a1, a0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (6, 8);")]
2693 #[doc = concat!("let (b1, b0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (-5, 9);")]
2694 #[doc = concat!("// ", stringify!($UnsignedT), "::borrowing_sub for the less significant words")]
2697 #[doc = concat!("// ", stringify!($SelfT), "::borrowing_sub for the most significant word")]
2701 #[doc = concat!("assert_eq!((diff1, diff0), (10, ", stringify!($UnsignedT), "::MAX));")]
2705 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2707 #[must_use = "this returns the result of the operation, \
2708 without modifying the original"]
2709 #[inline]
2710 pub const fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
2711 let (a, b) = self.overflowing_sub(rhs);
2714 let (c, d) = a.overflowing_sub(borrow as $SelfT);
2715 (c, b != d)
2716 }
2717
2718 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_sub_unsigned(2), (-1, false));")]
2728 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX).overflowing_sub_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MIN, false));")]
2729 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).overflowing_sub_unsigned(3), (", stringify!($SelfT), "::MAX, true));")]
2730 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2732 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2733 #[must_use = "this returns the result of the operation, \
2734 without modifying the original"]
2735 #[inline]
2736 pub const fn overflowing_sub_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
2737 let rhs = rhs as Self;
2738 let (res, overflowed) = self.overflowing_sub(rhs);
2739 (res, overflowed ^ (rhs < 0))
2740 }
2741
2742 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_mul(2), (10, false));")]
2751 #[stable(feature = "wrapping", since = "1.7.0")]
2754 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2755 #[must_use = "this returns the result of the operation, \
2756 without modifying the original"]
2757 #[inline(always)]
2758 pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
2759 let (a, b) = intrinsics::mul_with_overflow(self as $ActualT, rhs as $ActualT);
2760 (a as Self, b)
2761 }
2762
2763 #[doc = concat!("assert_eq!(",
2784 stringify!($SelfT), "::MAX.carrying_mul(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
2785 "(", stringify!($SelfT), "::MAX.unsigned_abs() + 1, ", stringify!($SelfT), "::MAX / 2));"
2786 )]
2787 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2789 #[rustc_const_unstable(feature = "signed_bigint_helpers", issue = "151989")]
2790 #[must_use = "this returns the result of the operation, \
2791 without modifying the original"]
2792 #[inline]
2793 pub const fn carrying_mul(self, rhs: Self, carry: Self) -> ($UnsignedT, Self) {
2794 Self::carrying_mul_add(self, rhs, carry, 0)
2795 }
2796
2797 #[doc = concat!("assert_eq!(",
2820 stringify!($SelfT), "::MAX.carrying_mul_add(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
2821 "(", stringify!($UnsignedT), "::MAX, ", stringify!($SelfT), "::MAX / 2));"
2822 )]
2823 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2825 #[rustc_const_unstable(feature = "signed_bigint_helpers", issue = "151989")]
2826 #[must_use = "this returns the result of the operation, \
2827 without modifying the original"]
2828 #[inline]
2829 pub const fn carrying_mul_add(self, rhs: Self, carry: Self, add: Self) -> ($UnsignedT, Self) {
2830 intrinsics::carrying_mul_add(self, rhs, carry, add)
2831 }
2832
2833 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div(2), (2, false));")]
2846 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div(-1), (", stringify!($SelfT), "::MIN, true));")]
2847 #[inline]
2849 #[stable(feature = "wrapping", since = "1.7.0")]
2850 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
2851 #[must_use = "this returns the result of the operation, \
2852 without modifying the original"]
2853 pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
2854 if intrinsics::unlikely((self == Self::MIN) & (rhs == -1)) {
2856 (self, true)
2857 } else {
2858 (self / rhs, false)
2859 }
2860 }
2861
2862 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div_euclid(2), (2, false));")]
2875 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringify!($SelfT), "::MIN, true));")]
2876 #[inline]
2878 #[stable(feature = "euclidean_division", since = "1.38.0")]
2879 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2880 #[must_use = "this returns the result of the operation, \
2881 without modifying the original"]
2882 pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
2883 if intrinsics::unlikely((self == Self::MIN) & (rhs == -1)) {
2885 (self, true)
2886 } else {
2887 (self.div_euclid(rhs), false)
2888 }
2889 }
2890
2891 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem(2), (1, false));")]
2904 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem(-1), (0, true));")]
2905 #[inline]
2907 #[stable(feature = "wrapping", since = "1.7.0")]
2908 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
2909 #[must_use = "this returns the result of the operation, \
2910 without modifying the original"]
2911 pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
2912 if intrinsics::unlikely(rhs == -1) {
2913 (0, self == Self::MIN)
2914 } else {
2915 (self % rhs, false)
2916 }
2917 }
2918
2919
2920 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem_euclid(2), (1, false));")]
2933 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));")]
2934 #[stable(feature = "euclidean_division", since = "1.38.0")]
2936 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2937 #[must_use = "this returns the result of the operation, \
2938 without modifying the original"]
2939 #[inline]
2940 #[track_caller]
2941 pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
2942 if intrinsics::unlikely(rhs == -1) {
2943 (0, self == Self::MIN)
2944 } else {
2945 (self.rem_euclid(rhs), false)
2946 }
2947 }
2948
2949
2950 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2, false));")]
2960 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($SelfT), "::MIN, true));")]
2961 #[inline]
2963 #[stable(feature = "wrapping", since = "1.7.0")]
2964 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2965 #[must_use = "this returns the result of the operation, \
2966 without modifying the original"]
2967 #[allow(unused_attributes)]
2968 pub const fn overflowing_neg(self) -> (Self, bool) {
2969 if intrinsics::unlikely(self == Self::MIN) {
2970 (Self::MIN, true)
2971 } else {
2972 (-self, false)
2973 }
2974 }
2975
2976 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT),".overflowing_shl(4), (0x10, false));")]
2986 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shl(", stringify!($BITS_MINUS_ONE), "), (0, false));")]
2988 #[stable(feature = "wrapping", since = "1.7.0")]
2990 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2991 #[must_use = "this returns the result of the operation, \
2992 without modifying the original"]
2993 #[inline]
2994 pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
2995 (self.wrapping_shl(rhs), rhs >= Self::BITS)
2996 }
2997
2998 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shr(4), (0x1, false));")]
3008 #[stable(feature = "wrapping", since = "1.7.0")]
3011 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3012 #[must_use = "this returns the result of the operation, \
3013 without modifying the original"]
3014 #[inline]
3015 pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
3016 (self.wrapping_shr(rhs), rhs >= Self::BITS)
3017 }
3018
3019 #[doc = concat!("(e.g., ", stringify!($SelfT), "::MIN for values of type ", stringify!($SelfT), "),")]
3024 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".overflowing_abs(), (10, false));")]
3031 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").overflowing_abs(), (10, false));")]
3032 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_abs(), (", stringify!($SelfT), "::MIN, true));")]
3033 #[stable(feature = "no_panic_abs", since = "1.13.0")]
3035 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3036 #[must_use = "this returns the result of the operation, \
3037 without modifying the original"]
3038 #[inline]
3039 pub const fn overflowing_abs(self) -> (Self, bool) {
3040 (self.wrapping_abs(), self == Self::MIN)
3041 }
3042
3043 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".overflowing_pow(4), (81, false));")]
3052 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".overflowing_pow(0), (1, false));")]
3053 #[stable(feature = "no_panic_pow", since = "1.34.0")]
3056 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
3057 #[must_use = "this returns the result of the operation, \
3058 without modifying the original"]
3059 #[inline]
3060 pub const fn overflowing_pow(self, mut exp: u32) -> (Self, bool) {
3061 let mut base = self;
3062 let mut acc: Self = 1;
3063 let mut overflow = false;
3064 let mut tmp_overflow;
3065
3066 if intrinsics::is_val_statically_known(base) && base.unsigned_abs().is_power_of_two() {
3067 let k = base.unsigned_abs().ilog2();
3068 let Some(shift) = k.checked_mul(exp) else {
3069 return (0, true)
3070 };
3071 let base: Self = if base < 0 && (exp % 2) != 0 { -1 } else { 1 };
3072 return (base.unbounded_shl(shift), base.shl_exact(shift).is_none());
3073 }
3074
3075 if exp == 0 {
3076 return (1, false);
3077 }
3078
3079 if intrinsics::is_val_statically_known(exp) {
3080 while exp > 1 {
3081 if (exp & 1) == 1 {
3082 (acc, tmp_overflow) = acc.overflowing_mul(base);
3083 overflow |= tmp_overflow;
3084 }
3085 exp /= 2;
3086 (base, tmp_overflow) = base.overflowing_mul(base);
3087 overflow |= tmp_overflow;
3088 }
3089
3090 (acc, tmp_overflow) = acc.overflowing_mul(base);
3095 overflow |= tmp_overflow;
3096 return (acc, overflow);
3097 }
3098
3099 loop {
3100 if (exp & 1) == 1 {
3101 (acc, tmp_overflow) = acc.overflowing_mul(base);
3102 overflow |= tmp_overflow;
3103 if exp == 1 {
3105 return (acc, overflow);
3106 }
3107 }
3108 exp /= 2;
3109 (base, tmp_overflow) = base.overflowing_mul(base);
3110 overflow |= tmp_overflow;
3111 }
3112 }
3113
3114 #[doc = concat!("let x: ", stringify!($SelfT), " = 2; // or any other integer type")]
3120 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".pow(0), 1);")]
3123 #[stable(feature = "rust1", since = "1.0.0")]
3125 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
3126 #[must_use = "this returns the result of the operation, \
3127 without modifying the original"]
3128 #[inline]
3129 #[rustc_inherit_overflow_checks]
3130 pub const fn pow(self, exp: u32) -> Self {
3131 if intrinsics::overflow_checks() {
3132 self.strict_pow(exp)
3133 } else {
3134 self.wrapping_pow(exp)
3135 }
3136 }
3137
3138 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".isqrt(), 3);")]
3152 #[stable(feature = "isqrt", since = "1.84.0")]
3154 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
3155 #[must_use = "this returns the result of the operation, \
3156 without modifying the original"]
3157 #[inline]
3158 #[track_caller]
3159 pub const fn isqrt(self) -> Self {
3160 match self.checked_isqrt() {
3161 Some(sqrt) => sqrt,
3162 None => imp::int_sqrt::panic_for_negative_argument(),
3163 }
3164 }
3165
3166 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
3187 #[stable(feature = "euclidean_division", since = "1.38.0")]
3195 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3196 #[must_use = "this returns the result of the operation, \
3197 without modifying the original"]
3198 #[inline]
3199 #[track_caller]
3200 pub const fn div_euclid(self, rhs: Self) -> Self {
3201 let q = self / rhs;
3202 if self % rhs < 0 {
3203 return if rhs > 0 { q - 1 } else { q + 1 }
3204 }
3205 q
3206 }
3207
3208
3209 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
3225 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.rem_euclid(-1);")]
3236 #[doc(alias = "modulo", alias = "mod")]
3238 #[stable(feature = "euclidean_division", since = "1.38.0")]
3239 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3240 #[must_use = "this returns the result of the operation, \
3241 without modifying the original"]
3242 #[inline]
3243 #[track_caller]
3244 pub const fn rem_euclid(self, rhs: Self) -> Self {
3245 let r = self % rhs;
3246 if r < 0 {
3247 r.wrapping_add(rhs.wrapping_abs())
3256 } else {
3257 r
3258 }
3259 }
3260
3261 #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3273 #[unstable(feature = "int_roundings", issue = "88581")]
3281 #[must_use = "this returns the result of the operation, \
3282 without modifying the original"]
3283 #[inline]
3284 #[track_caller]
3285 pub const fn div_floor(self, rhs: Self) -> Self {
3286 let d = self / rhs;
3287 let r = self % rhs;
3288
3289 let correction = (self ^ rhs) >> (Self::BITS - 1);
3296 if r != 0 {
3297 d + correction
3298 } else {
3299 d
3300 }
3301 }
3302
3303 #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3315 #[unstable(feature = "int_roundings", issue = "88581")]
3323 #[must_use = "this returns the result of the operation, \
3324 without modifying the original"]
3325 #[inline]
3326 #[track_caller]
3327 pub const fn div_ceil(self, rhs: Self) -> Self {
3328 let d = self / rhs;
3329 let r = self % rhs;
3330
3331 let correction = 1 + ((self ^ rhs) >> (Self::BITS - 1));
3334 if r != 0 {
3335 d + correction
3336 } else {
3337 d
3338 }
3339 }
3340
3341 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(8), 16);")]
3360 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(8), 24);")]
3361 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
3362 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
3363 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
3364 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
3365 #[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 #[unstable(feature = "int_roundings", issue = "88581")]
3369 #[must_use = "this returns the result of the operation, \
3370 without modifying the original"]
3371 #[inline]
3372 #[rustc_inherit_overflow_checks]
3373 pub const fn next_multiple_of(self, rhs: Self) -> Self {
3374 if rhs == -1 {
3376 return self;
3377 }
3378
3379 let r = self % rhs;
3380 let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
3381 r + rhs
3382 } else {
3383 r
3384 };
3385
3386 if m == 0 {
3387 self
3388 } else {
3389 self + (rhs - m)
3390 }
3391 }
3392
3393 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(16));")]
3404 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(24));")]
3405 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
3406 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
3407 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
3408 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
3409 #[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!(1_", stringify!($SelfT), ".checked_next_multiple_of(0), None);")]
3412 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_next_multiple_of(2), None);")]
3413 #[unstable(feature = "int_roundings", issue = "88581")]
3415 #[must_use = "this returns the result of the operation, \
3416 without modifying the original"]
3417 #[inline]
3418 pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
3419 if rhs == -1 {
3421 return Some(self);
3422 }
3423
3424 let r = try_opt!(self.checked_rem(rhs));
3425 let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
3426 r + rhs
3428 } else {
3429 r
3430 };
3431
3432 if m == 0 {
3433 Some(self)
3434 } else {
3435 self.checked_add(rhs - m)
3437 }
3438 }
3439
3440 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".ilog(5), 1);")]
3456 #[stable(feature = "int_log", since = "1.67.0")]
3458 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3459 #[must_use = "this returns the result of the operation, \
3460 without modifying the original"]
3461 #[inline]
3462 #[track_caller]
3463 pub const fn ilog(self, base: Self) -> u32 {
3464 assert!(base >= 2, "base of integer logarithm must be at least 2");
3465 if let Some(log) = self.checked_ilog(base) {
3466 log
3467 } else {
3468 imp::int_log10::panic_for_nonpositive_argument()
3469 }
3470 }
3471
3472 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".ilog2(), 1);")]
3482 #[stable(feature = "int_log", since = "1.67.0")]
3484 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3485 #[must_use = "this returns the result of the operation, \
3486 without modifying the original"]
3487 #[inline]
3488 #[track_caller]
3489 pub const fn ilog2(self) -> u32 {
3490 if let Some(log) = self.checked_ilog2() {
3491 log
3492 } else {
3493 imp::int_log10::panic_for_nonpositive_argument()
3494 }
3495 }
3496
3497 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".ilog10(), 1);")]
3507 #[stable(feature = "int_log", since = "1.67.0")]
3509 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3510 #[must_use = "this returns the result of the operation, \
3511 without modifying the original"]
3512 #[inline]
3513 #[track_caller]
3514 pub const fn ilog10(self) -> u32 {
3515 if let Some(log) = self.checked_ilog10() {
3516 log
3517 } else {
3518 imp::int_log10::panic_for_nonpositive_argument()
3519 }
3520 }
3521
3522 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_ilog(5), Some(1));")]
3535 #[stable(feature = "int_log", since = "1.67.0")]
3537 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3538 #[must_use = "this returns the result of the operation, \
3539 without modifying the original"]
3540 #[inline]
3541 pub const fn checked_ilog(self, base: Self) -> Option<u32> {
3542 if self <= 0 || base <= 1 {
3543 None
3544 } else {
3545 (self as $UnsignedT).checked_ilog(base as $UnsignedT)
3548 }
3549 }
3550
3551 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_ilog2(), Some(1));")]
3562 #[stable(feature = "int_log", since = "1.67.0")]
3564 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3565 #[must_use = "this returns the result of the operation, \
3566 without modifying the original"]
3567 #[inline]
3568 pub const fn checked_ilog2(self) -> Option<u32> {
3569 if self <= 0 {
3570 None
3571 } else {
3572 let log = (Self::BITS - 1) - unsafe { intrinsics::ctlz_nonzero(self) as u32 };
3574 Some(log)
3575 }
3576 }
3577
3578 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_ilog10(), Some(1));")]
3586 #[stable(feature = "int_log", since = "1.67.0")]
3588 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3589 #[must_use = "this returns the result of the operation, \
3590 without modifying the original"]
3591 #[inline]
3592 pub const fn checked_ilog10(self) -> Option<u32> {
3593 imp::int_log10::$ActualT(self as $ActualT)
3594 }
3595
3596 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
3602 #[doc = concat!("`", stringify!($SelfT), "`,")]
3604 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
3608 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".abs(), 10);")]
3615 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").abs(), 10);")]
3616 #[stable(feature = "rust1", since = "1.0.0")]
3618 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3619 #[allow(unused_attributes)]
3620 #[must_use = "this returns the result of the operation, \
3621 without modifying the original"]
3622 #[inline]
3623 #[rustc_inherit_overflow_checks]
3624 pub const fn abs(self) -> Self {
3625 if self.is_negative() {
3629 -self
3630 } else {
3631 self
3632 }
3633 }
3634
3635 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(80), 20", stringify!($UnsignedT), ");")]
3644 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(110), 10", stringify!($UnsignedT), ");")]
3645 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(80), 180", stringify!($UnsignedT), ");")]
3646 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(-120), 20", stringify!($UnsignedT), ");")]
3647 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.abs_diff(", stringify!($SelfT), "::MAX), ", stringify!($UnsignedT), "::MAX);")]
3648 #[stable(feature = "int_abs_diff", since = "1.60.0")]
3650 #[rustc_const_stable(feature = "int_abs_diff", since = "1.60.0")]
3651 #[must_use = "this returns the result of the operation, \
3652 without modifying the original"]
3653 #[inline]
3654 pub const fn abs_diff(self, other: Self) -> $UnsignedT {
3655 if self < other {
3656 (other as $UnsignedT).wrapping_sub(self as $UnsignedT)
3670 } else {
3671 (self as $UnsignedT).wrapping_sub(other as $UnsignedT)
3672 }
3673 }
3674
3675 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".signum(), 1);")]
3685 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".signum(), 0);")]
3686 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").signum(), -1);")]
3687 #[stable(feature = "rust1", since = "1.0.0")]
3689 #[rustc_const_stable(feature = "const_int_sign", since = "1.47.0")]
3690 #[must_use = "this returns the result of the operation, \
3691 without modifying the original"]
3692 #[inline(always)]
3693 pub const fn signum(self) -> Self {
3694 crate::intrinsics::three_way_compare(self, 0) as Self
3700 }
3701
3702 #[doc = concat!("assert!(10", stringify!($SelfT), ".is_positive());")]
3709 #[doc = concat!("assert!(!(-10", stringify!($SelfT), ").is_positive());")]
3710 #[must_use]
3712 #[stable(feature = "rust1", since = "1.0.0")]
3713 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3714 #[inline(always)]
3715 pub const fn is_positive(self) -> bool { self > 0 }
3716
3717 #[doc = concat!("assert!((-10", stringify!($SelfT), ").is_negative());")]
3724 #[doc = concat!("assert!(!10", stringify!($SelfT), ".is_negative());")]
3725 #[must_use]
3727 #[stable(feature = "rust1", since = "1.0.0")]
3728 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3729 #[inline(always)]
3730 pub const fn is_negative(self) -> bool { self < 0 }
3731
3732 #[doc = $to_xe_bytes_doc]
3736 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();")]
3741 #[doc = concat!("assert_eq!(bytes, ", $be_bytes, ");")]
3742 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3744 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3745 #[must_use = "this returns the result of the operation, \
3746 without modifying the original"]
3747 #[inline]
3748 pub const fn to_be_bytes(self) -> [u8; size_of::<Self>()] {
3749 self.to_be().to_ne_bytes()
3750 }
3751
3752 #[doc = $to_xe_bytes_doc]
3756 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();")]
3761 #[doc = concat!("assert_eq!(bytes, ", $le_bytes, ");")]
3762 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3764 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3765 #[must_use = "this returns the result of the operation, \
3766 without modifying the original"]
3767 #[inline]
3768 pub const fn to_le_bytes(self) -> [u8; size_of::<Self>()] {
3769 self.to_le().to_ne_bytes()
3770 }
3771
3772 #[doc = $to_xe_bytes_doc]
3780 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_ne_bytes();")]
3788 #[doc = concat!(" ", $be_bytes)]
3792 #[doc = concat!(" ", $le_bytes)]
3794 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3798 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3799 #[allow(unnecessary_transmutes)]
3800 #[must_use = "this returns the result of the operation, \
3803 without modifying the original"]
3804 #[inline]
3805 pub const fn to_ne_bytes(self) -> [u8; size_of::<Self>()] {
3806 unsafe { mem::transmute(self) }
3809 }
3810
3811 #[doc = $from_xe_bytes_doc]
3815 #[doc = concat!("let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, ");")]
3820 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3821 #[doc = concat!("fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3827 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3828 #[doc = concat!(" ", stringify!($SelfT), "::from_be_bytes(int_bytes.try_into().unwrap())")]
3830 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3833 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3834 #[must_use]
3835 #[inline]
3836 pub const fn from_be_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3837 Self::from_be(Self::from_ne_bytes(bytes))
3838 }
3839
3840 #[doc = $from_xe_bytes_doc]
3844 #[doc = concat!("let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, ");")]
3849 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3850 #[doc = concat!("fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3856 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3857 #[doc = concat!(" ", stringify!($SelfT), "::from_le_bytes(int_bytes.try_into().unwrap())")]
3859 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3862 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3863 #[must_use]
3864 #[inline]
3865 pub const fn from_le_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3866 Self::from_le(Self::from_ne_bytes(bytes))
3867 }
3868
3869 #[doc = $from_xe_bytes_doc]
3880 #[doc = concat!("let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"big\") {")]
3885 #[doc = concat!(" ", $be_bytes)]
3886 #[doc = concat!(" ", $le_bytes)]
3888 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3890 #[doc = concat!("fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3896 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3897 #[doc = concat!(" ", stringify!($SelfT), "::from_ne_bytes(int_bytes.try_into().unwrap())")]
3899 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3902 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3903 #[allow(unnecessary_transmutes)]
3904 #[must_use]
3905 #[inline]
3908 pub const fn from_ne_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3909 unsafe { mem::transmute(bytes) }
3911 }
3912
3913 #[doc = concat!("[`", stringify!($SelfT), "::MIN", "`] instead.")]
3915 #[stable(feature = "rust1", since = "1.0.0")]
3918 #[inline(always)]
3919 #[rustc_promotable]
3920 #[rustc_const_stable(feature = "const_min_value", since = "1.32.0")]
3921 #[deprecated(since = "TBD", note = "replaced by the `MIN` associated constant on this type")]
3922 #[rustc_diagnostic_item = concat!(stringify!($SelfT), "_legacy_fn_min_value")]
3923 pub const fn min_value() -> Self {
3924 Self::MIN
3925 }
3926
3927 #[doc = concat!("[`", stringify!($SelfT), "::MAX", "`] instead.")]
3929 #[stable(feature = "rust1", since = "1.0.0")]
3932 #[inline(always)]
3933 #[rustc_promotable]
3934 #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
3935 #[deprecated(since = "TBD", note = "replaced by the `MAX` associated constant on this type")]
3936 #[rustc_diagnostic_item = concat!(stringify!($SelfT), "_legacy_fn_max_value")]
3937 pub const fn max_value() -> Self {
3938 Self::MAX
3939 }
3940
3941 #[doc = concat!("assert_eq!(120", stringify!($SelfT), ".clamp_magnitude(100), 100);")]
3953 #[doc = concat!("assert_eq!(-120", stringify!($SelfT), ".clamp_magnitude(100), -100);")]
3954 #[doc = concat!("assert_eq!(80", stringify!($SelfT), ".clamp_magnitude(100), 80);")]
3955 #[doc = concat!("assert_eq!(-80", stringify!($SelfT), ".clamp_magnitude(100), -80);")]
3956 #[must_use = "this returns the clamped value and does not modify the original"]
3958 #[unstable(feature = "clamp_magnitude", issue = "148519")]
3959 #[inline]
3960 pub fn clamp_magnitude(self, limit: $UnsignedT) -> Self {
3961 if let Ok(limit) = core::convert::TryInto::<$SelfT>::try_into(limit) {
3962 self.clamp(-limit, limit)
3963 } else {
3964 self
3965 }
3966 }
3967
3968 #[doc = concat!("assert_eq!(120i8, 120", stringify!($SelfT), ".truncate());")]
3976 #[doc = concat!("assert_eq!(-120i8, (-120", stringify!($SelfT), ").truncate());")]
3977 #[must_use = "this returns the truncated value and does not modify the original"]
3980 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
3981 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
3982 #[inline]
3983 pub const fn truncate<Target>(self) -> Target
3984 where Self: [const] traits::TruncateTarget<Target>
3985 {
3986 traits::TruncateTarget::internal_truncate(self)
3987 }
3988
3989 #[doc = concat!("assert_eq!(120i8, 120", stringify!($SelfT), ".saturating_truncate());")]
3997 #[doc = concat!("assert_eq!(-120i8, (-120", stringify!($SelfT), ").saturating_truncate());")]
3998 #[must_use = "this returns the truncated value and does not modify the original"]
4002 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
4003 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
4004 #[inline]
4005 pub const fn saturating_truncate<Target>(self) -> Target
4006 where Self: [const] traits::TruncateTarget<Target>
4007 {
4008 traits::TruncateTarget::internal_saturating_truncate(self)
4009 }
4010
4011 #[doc = concat!("assert_eq!(Some(120i8), 120", stringify!($SelfT), ".checked_truncate());")]
4019 #[doc = concat!("assert_eq!(Some(-120i8), (-120", stringify!($SelfT), ").checked_truncate());")]
4020 #[must_use = "this returns the truncated value and does not modify the original"]
4024 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
4025 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
4026 #[inline]
4027 pub const fn checked_truncate<Target>(self) -> Option<Target>
4028 where Self: [const] traits::TruncateTarget<Target>
4029 {
4030 traits::TruncateTarget::internal_checked_truncate(self)
4031 }
4032
4033 #[doc = concat!("assert_eq!(120i128, 120i8.widen());")]
4040 #[doc = concat!("assert_eq!(-120i128, (-120i8).widen());")]
4041 #[must_use = "this returns the widened value and does not modify the original"]
4043 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
4044 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
4045 #[inline]
4046 pub const fn widen<Target>(self) -> Target
4047 where Self: [const] traits::WidenTarget<Target>
4048 {
4049 traits::WidenTarget::internal_widen(self)
4050 }
4051
4052
4053 #[doc = concat!("assert_eq!(i8::MAX, ", stringify!($SelfT), "::MAX.saturating_cast());")]
4061 #[doc = concat!("assert_eq!(i8::MIN, ", stringify!($SelfT), "::MIN.saturating_cast());")]
4062 #[doc = concat!("assert_eq!(42u8, 42", stringify!($SelfT), ".saturating_cast());")]
4063 #[doc = concat!("assert_eq!(0u8, (-42", stringify!($SelfT), ").saturating_cast());")]
4064 #[must_use = "this returns the cast result and does not modify the original"]
4066 #[unstable(feature = "integer_casts", issue = "157388")]
4067 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4068 #[inline(always)]
4069 pub const fn saturating_cast<T: [const] BoundedCastFromInt<Self>>(self) -> T {
4070 T::saturating_cast_from(self)
4071 }
4072
4073 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX as i8, ", stringify!($SelfT), "::MAX.wrapping_cast());")]
4081 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN as i8, ", stringify!($SelfT), "::MIN.wrapping_cast());")]
4082 #[doc = concat!("assert_eq!(42u8, 42", stringify!($SelfT), ".wrapping_cast());")]
4083 #[doc = concat!("assert_eq!(u8::MAX - 41, (-42", stringify!($SelfT), ").wrapping_cast());")]
4084 #[must_use = "this returns the cast result and does not modify the original"]
4086 #[unstable(feature = "integer_casts", issue = "157388")]
4087 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4088 #[inline(always)]
4089 pub const fn wrapping_cast<T: [const] BoundedCastFromInt<Self>>(self) -> T {
4090 T::wrapping_cast_from(self)
4091 }
4092
4093 #[doc = concat!("assert_eq!(Some(42u8), 42", stringify!($SelfT), ".checked_cast());")]
4101 #[doc = concat!("assert_eq!((-42", stringify!($SelfT), ").checked_cast::<u8>(), None);")]
4102 #[must_use = "this returns the cast result and does not modify the original"]
4104 #[unstable(feature = "integer_casts", issue = "157388")]
4105 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4106 #[inline(always)]
4107 pub const fn checked_cast<T: [const] CheckedCastFromInt<Self>>(self) -> Option<T> {
4108 T::checked_cast_from(self)
4109 }
4110
4111 #[doc = concat!("assert_eq!(42u8, 42", stringify!($SelfT), ".strict_cast());")]
4123 #[doc = concat!("let _ = (-42", stringify!($SelfT), ").strict_cast::<u8>();")]
4130 #[must_use = "this returns the cast result and does not modify the original"]
4132 #[unstable(feature = "integer_casts", issue = "157388")]
4133 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4134 #[inline(always)]
4135 #[track_caller]
4136 pub const fn strict_cast<T: [const] CheckedCastFromInt<Self>>(self) -> T {
4137 T::strict_cast_from(self)
4138 }
4139
4140 #[must_use = "this returns the cast result and does not modify the original"]
4148 #[unstable(feature = "integer_casts", issue = "157388")]
4149 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4150 #[inline(always)]
4151 pub const unsafe fn unchecked_cast<T: [const] CheckedCastFromInt<Self>>(self) -> T {
4152 assert_unsafe_precondition!(
4153 check_language_ub,
4154 concat!(stringify!($SelfT), "::unchecked_cast must fit in the target type"),
4155 (
4156 in_bounds: bool = {
4158 let cast_val = self.checked_cast::<T>();
4159 let ret = cast_val.is_some();
4160 core::mem::forget(cast_val); ret
4162 },
4163 ) => in_bounds,
4164 );
4165
4166 unsafe { T::unchecked_cast_from(self) }
4168 }
4169 }
4170}