1macro_rules! int_impl {
2 (
3 Self = $SelfT:ty,
4 ActualT = $ActualT:ident,
5 UnsignedT = $UnsignedT:ty,
6
7 BITS = $BITS:literal,
12 BITS_MINUS_ONE = $BITS_MINUS_ONE:literal,
13 Min = $Min:literal,
14 Max = $Max:literal,
15 rot = $rot:literal,
16 rot_op = $rot_op:literal,
17 rot_result = $rot_result:literal,
18 swap_op = $swap_op:literal,
19 swapped = $swapped:literal,
20 reversed = $reversed:literal,
21 le_bytes = $le_bytes:literal,
22 be_bytes = $be_bytes:literal,
23 to_xe_bytes_doc = $to_xe_bytes_doc:expr,
24 from_xe_bytes_doc = $from_xe_bytes_doc:expr,
25 bound_condition = $bound_condition:literal,
26 ) => {
27 #[doc = concat!("(−2<sup>", $BITS_MINUS_ONE, "</sup>", $bound_condition, ").")]
29 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN, ", stringify!($Min), ");")]
34 #[stable(feature = "assoc_int_consts", since = "1.43.0")]
36 pub const MIN: Self = !Self::MAX;
37
38 #[doc = concat!("(2<sup>", $BITS_MINUS_ONE, "</sup> − 1", $bound_condition, ").")]
40 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX, ", stringify!($Max), ");")]
45 #[stable(feature = "assoc_int_consts", since = "1.43.0")]
47 pub const MAX: Self = (<$UnsignedT>::MAX >> 1) as Self;
48
49 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::BITS, ", stringify!($BITS), ");")]
55 #[stable(feature = "int_bits_const", since = "1.53.0")]
57 pub const BITS: u32 = <$UnsignedT>::BITS;
58
59 #[doc = concat!("let n = 0b100_0000", stringify!($SelfT), ";")]
65 #[stable(feature = "rust1", since = "1.0.0")]
70 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
71 #[doc(alias = "popcount")]
72 #[doc(alias = "popcnt")]
73 #[must_use = "this returns the result of the operation, \
74 without modifying the original"]
75 #[inline(always)]
76 pub const fn count_ones(self) -> u32 { (self as $UnsignedT).count_ones() }
77
78 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.count_zeros(), 1);")]
84 #[stable(feature = "rust1", since = "1.0.0")]
86 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
87 #[must_use = "this returns the result of the operation, \
88 without modifying the original"]
89 #[inline(always)]
90 pub const fn count_zeros(self) -> u32 {
91 (!self).count_ones()
92 }
93
94 #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
103 #[doc = concat!("[`ilog2`]: ", stringify!($SelfT), "::ilog2")]
107 #[stable(feature = "rust1", since = "1.0.0")]
108 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
109 #[must_use = "this returns the result of the operation, \
110 without modifying the original"]
111 #[inline(always)]
112 pub const fn leading_zeros(self) -> u32 {
113 (self as $UnsignedT).leading_zeros()
114 }
115
116 #[doc = concat!("let n = -4", stringify!($SelfT), ";")]
122 #[stable(feature = "rust1", since = "1.0.0")]
126 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
127 #[must_use = "this returns the result of the operation, \
128 without modifying the original"]
129 #[inline(always)]
130 pub const fn trailing_zeros(self) -> u32 {
131 (self as $UnsignedT).trailing_zeros()
132 }
133
134 #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
140 #[doc = concat!("assert_eq!(n.leading_ones(), ", stringify!($BITS), ");")]
142 #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
144 #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
145 #[must_use = "this returns the result of the operation, \
146 without modifying the original"]
147 #[inline(always)]
148 pub const fn leading_ones(self) -> u32 {
149 (self as $UnsignedT).leading_ones()
150 }
151
152 #[doc = concat!("let n = 3", stringify!($SelfT), ";")]
158 #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
162 #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
163 #[must_use = "this returns the result of the operation, \
164 without modifying the original"]
165 #[inline(always)]
166 pub const fn trailing_ones(self) -> u32 {
167 (self as $UnsignedT).trailing_ones()
168 }
169
170 #[doc = concat!("let n: ", stringify!($SelfT), " = 0b_01100100;")]
177 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".isolate_highest_one(), 0);")]
180 #[stable(feature = "isolate_most_least_significant_one", since = "1.97.0")]
182 #[rustc_const_stable(feature = "isolate_most_least_significant_one", since = "1.97.0")]
183 #[must_use = "this returns the result of the operation, \
184 without modifying the original"]
185 #[inline(always)]
186 pub const fn isolate_highest_one(self) -> Self {
187 self & (((1 as $SelfT) << (<$SelfT>::BITS - 1)).wrapping_shr(self.leading_zeros()))
188 }
189
190 #[doc = concat!("let n: ", stringify!($SelfT), " = 0b_01100100;")]
197 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".isolate_lowest_one(), 0);")]
200 #[stable(feature = "isolate_most_least_significant_one", since = "1.97.0")]
202 #[rustc_const_stable(feature = "isolate_most_least_significant_one", since = "1.97.0")]
203 #[must_use = "this returns the result of the operation, \
204 without modifying the original"]
205 #[inline(always)]
206 pub const fn isolate_lowest_one(self) -> Self {
207 self & self.wrapping_neg()
208 }
209
210 #[doc = concat!("assert_eq!(0b0_", stringify!($SelfT), ".highest_one(), None);")]
220 #[doc = concat!("assert_eq!(0b1_", stringify!($SelfT), ".highest_one(), Some(0));")]
221 #[doc = concat!("assert_eq!(0b1_0000_", stringify!($SelfT), ".highest_one(), Some(4));")]
222 #[doc = concat!("assert_eq!(0b1_1111_", stringify!($SelfT), ".highest_one(), Some(4));")]
223 #[stable(feature = "int_lowest_highest_one", since = "1.97.0")]
225 #[rustc_const_stable(feature = "int_lowest_highest_one", since = "1.97.0")]
226 #[must_use = "this returns the result of the operation, \
227 without modifying the original"]
228 #[inline(always)]
229 pub const fn highest_one(self) -> Option<u32> {
230 (self as $UnsignedT).highest_one()
231 }
232
233 #[doc = concat!("assert_eq!(0b0_", stringify!($SelfT), ".lowest_one(), None);")]
240 #[doc = concat!("assert_eq!(0b1_", stringify!($SelfT), ".lowest_one(), Some(0));")]
241 #[doc = concat!("assert_eq!(0b1_0000_", stringify!($SelfT), ".lowest_one(), Some(4));")]
242 #[doc = concat!("assert_eq!(0b1_1111_", stringify!($SelfT), ".lowest_one(), Some(0));")]
243 #[stable(feature = "int_lowest_highest_one", since = "1.97.0")]
245 #[rustc_const_stable(feature = "int_lowest_highest_one", since = "1.97.0")]
246 #[must_use = "this returns the result of the operation, \
247 without modifying the original"]
248 #[inline(always)]
249 pub const fn lowest_one(self) -> Option<u32> {
250 (self as $UnsignedT).lowest_one()
251 }
252
253 #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
262 #[doc = concat!("assert_eq!(n.cast_unsigned(), ", stringify!($UnsignedT), "::MAX);")]
264 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
266 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
267 #[must_use = "this returns the result of the operation, \
268 without modifying the original"]
269 #[inline(always)]
270 pub const fn cast_unsigned(self) -> $UnsignedT {
271 self as $UnsignedT
272 }
273
274 #[doc = concat!("let n = ", stringify!($SelfT), "::MIN;")]
288 #[doc = concat!("assert_eq!(n.saturating_cast_unsigned(), 0", stringify!($UnsignedT), ");")]
290 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".saturating_cast_unsigned(), 64", stringify!($UnsignedT), ");")]
291 #[rustc_const_unstable(feature = "integer_cast_extras", issue = "154650")]
293 #[unstable(feature = "integer_cast_extras", issue = "154650")]
294 #[must_use = "this returns the result of the operation, \
295 without modifying the original"]
296 #[inline(always)]
297 pub const fn saturating_cast_unsigned(self) -> $UnsignedT {
298 if self >= 0 {
299 self.cast_unsigned()
300 } else {
301 0
302 }
303 }
304
305 #[doc = concat!("let n = ", stringify!($SelfT), "::MIN;")]
318 #[doc = concat!("assert_eq!(n.checked_cast_unsigned(), None);")]
320 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".checked_cast_unsigned(), Some(64", stringify!($UnsignedT), "));")]
321 #[rustc_const_unstable(feature = "integer_cast_extras", issue = "154650")]
323 #[unstable(feature = "integer_cast_extras", issue = "154650")]
324 #[must_use = "this returns the result of the operation, \
325 without modifying the original"]
326 #[inline(always)]
327 pub const fn checked_cast_unsigned(self) -> Option<$UnsignedT> {
328 if self >= 0 {
329 Some(self.cast_unsigned())
330 } else {
331 None
332 }
333 }
334
335 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_cast_unsigned();")]
348 #[rustc_const_unstable(feature = "integer_cast_extras", issue = "154650")]
350 #[unstable(feature = "integer_cast_extras", issue = "154650")]
351 #[must_use = "this returns the result of the operation, \
352 without modifying the original"]
353 #[inline]
354 #[track_caller]
355 pub const fn strict_cast_unsigned(self) -> $UnsignedT {
356 match self.checked_cast_unsigned() {
357 Some(n) => n,
358 None => imp::overflow_panic::cast_integer(),
359 }
360 }
361
362 #[doc = concat!("let n = ", $rot_op, stringify!($SelfT), ";")]
375 #[doc = concat!("let m = ", $rot_result, ";")]
376 #[doc = concat!("assert_eq!(n.rotate_left(", $rot, "), m);")]
378 #[doc = concat!("assert_eq!(n.rotate_left(1024), n);")]
379 #[stable(feature = "rust1", since = "1.0.0")]
381 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
382 #[must_use = "this returns the result of the operation, \
383 without modifying the original"]
384 #[inline(always)]
385 pub const fn rotate_left(self, n: u32) -> Self {
386 (self as $UnsignedT).rotate_left(n) as Self
387 }
388
389 #[doc = concat!("let n = ", $rot_result, stringify!($SelfT), ";")]
403 #[doc = concat!("let m = ", $rot_op, ";")]
404 #[doc = concat!("assert_eq!(n.rotate_right(", $rot, "), m);")]
406 #[doc = concat!("assert_eq!(n.rotate_right(1024), n);")]
407 #[stable(feature = "rust1", since = "1.0.0")]
409 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
410 #[must_use = "this returns the result of the operation, \
411 without modifying the original"]
412 #[inline(always)]
413 pub const fn rotate_right(self, n: u32) -> Self {
414 (self as $UnsignedT).rotate_right(n) as Self
415 }
416
417 #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
423 #[doc = concat!("assert_eq!(m, ", $swapped, ");")]
427 #[stable(feature = "rust1", since = "1.0.0")]
429 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
430 #[must_use = "this returns the result of the operation, \
431 without modifying the original"]
432 #[inline(always)]
433 pub const fn swap_bytes(self) -> Self {
434 (self as $UnsignedT).swap_bytes() as Self
435 }
436
437 #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
444 #[doc = concat!("assert_eq!(m, ", $reversed, ");")]
447 #[doc = concat!("assert_eq!(0, 0", stringify!($SelfT), ".reverse_bits());")]
448 #[stable(feature = "reverse_bits", since = "1.37.0")]
450 #[rustc_const_stable(feature = "reverse_bits", since = "1.37.0")]
451 #[must_use = "this returns the result of the operation, \
452 without modifying the original"]
453 #[inline(always)]
454 pub const fn reverse_bits(self) -> Self {
455 (self as $UnsignedT).reverse_bits() as Self
456 }
457
458 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
468 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_be(n), n)")]
471 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_be(n), n.swap_bytes())")]
473 #[stable(feature = "rust1", since = "1.0.0")]
476 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
477 #[must_use]
478 #[inline]
479 pub const fn from_be(x: Self) -> Self {
480 cfg_select! {
481 target_endian = "big" => x,
482 _ => x.swap_bytes(),
483 }
484 }
485
486 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
496 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_le(n), n)")]
499 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_le(n), n.swap_bytes())")]
501 #[stable(feature = "rust1", since = "1.0.0")]
504 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
505 #[must_use]
506 #[inline]
507 pub const fn from_le(x: Self) -> Self {
508 cfg_select! {
509 target_endian = "little" => x,
510 _ => x.swap_bytes(),
511 }
512 }
513
514 #[doc = concat!("`", stringify!($SelfT), "`.")]
521 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
528 #[stable(feature = "rust1", since = "1.0.0")]
536 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
537 #[must_use = "this returns the result of the operation, \
538 without modifying the original"]
539 #[inline]
540 pub const fn to_be(self) -> Self { cfg_select! {
542 target_endian = "big" => self,
543 _ => self.swap_bytes(),
544 }
545 }
546
547 #[doc = concat!("`", stringify!($SelfT), "`.")]
554 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
561 #[stable(feature = "rust1", since = "1.0.0")]
569 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
570 #[must_use = "this returns the result of the operation, \
571 without modifying the original"]
572 #[inline]
573 pub const fn to_le(self) -> Self {
574 cfg_select! {
575 target_endian = "little" => self,
576 _ => self.swap_bytes(),
577 }
578 }
579
580 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(1), Some(", stringify!($SelfT), "::MAX - 1));")]
587 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);")]
588 #[stable(feature = "rust1", since = "1.0.0")]
590 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
591 #[must_use = "this returns the result of the operation, \
592 without modifying the original"]
593 #[inline]
594 pub const fn checked_add(self, rhs: Self) -> Option<Self> {
595 let (a, b) = self.overflowing_add(rhs);
596 if intrinsics::unlikely(b) { None } else { Some(a) }
597 }
598
599 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).strict_add(1), ", stringify!($SelfT), "::MAX - 1);")]
612 #[doc = concat!("let _ = (", stringify!($SelfT), "::MAX - 2).strict_add(3);")]
618 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
620 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
621 #[must_use = "this returns the result of the operation, \
622 without modifying the original"]
623 #[inline]
624 #[track_caller]
625 pub const fn strict_add(self, rhs: Self) -> Self {
626 let (a, b) = self.overflowing_add(rhs);
627 if b { imp::overflow_panic::add() } else { a }
628 }
629
630 #[doc = concat!("`self + rhs > ", stringify!($SelfT), "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`,")]
643 #[doc = concat!("[`checked_add`]: ", stringify!($SelfT), "::checked_add")]
647 #[doc = concat!("[`wrapping_add`]: ", stringify!($SelfT), "::wrapping_add")]
648 #[stable(feature = "unchecked_math", since = "1.79.0")]
649 #[rustc_const_stable(feature = "unchecked_math", since = "1.79.0")]
650 #[must_use = "this returns the result of the operation, \
651 without modifying the original"]
652 #[inline(always)]
653 #[track_caller]
654 pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
655 assert_unsafe_precondition!(
656 check_language_ub,
657 concat!(stringify!($SelfT), "::unchecked_add cannot overflow"),
658 (
659 lhs: $SelfT = self,
660 rhs: $SelfT = rhs,
661 ) => !lhs.overflowing_add(rhs).1,
662 );
663
664 unsafe {
666 intrinsics::unchecked_add(self, rhs)
667 }
668 }
669
670 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_add_unsigned(2), Some(3));")]
677 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add_unsigned(3), None);")]
678 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
680 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
681 #[must_use = "this returns the result of the operation, \
682 without modifying the original"]
683 #[inline]
684 pub const fn checked_add_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
685 let (a, b) = self.overflowing_add_unsigned(rhs);
686 if intrinsics::unlikely(b) { None } else { Some(a) }
687 }
688
689 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".strict_add_unsigned(2), 3);")]
702 #[doc = concat!("let _ = (", stringify!($SelfT), "::MAX - 2).strict_add_unsigned(3);")]
708 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
710 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
711 #[must_use = "this returns the result of the operation, \
712 without modifying the original"]
713 #[inline]
714 #[track_caller]
715 pub const fn strict_add_unsigned(self, rhs: $UnsignedT) -> Self {
716 let (a, b) = self.overflowing_add_unsigned(rhs);
717 if b { imp::overflow_panic::add() } else { a }
718 }
719
720 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub(1), Some(", stringify!($SelfT), "::MIN + 1));")]
727 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub(3), None);")]
728 #[stable(feature = "rust1", since = "1.0.0")]
730 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
731 #[must_use = "this returns the result of the operation, \
732 without modifying the original"]
733 #[inline]
734 pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
735 let (a, b) = self.overflowing_sub(rhs);
736 if intrinsics::unlikely(b) { None } else { Some(a) }
737 }
738
739 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).strict_sub(1), ", stringify!($SelfT), "::MIN + 1);")]
752 #[doc = concat!("let _ = (", stringify!($SelfT), "::MIN + 2).strict_sub(3);")]
758 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
760 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
761 #[must_use = "this returns the result of the operation, \
762 without modifying the original"]
763 #[inline]
764 #[track_caller]
765 pub const fn strict_sub(self, rhs: Self) -> Self {
766 let (a, b) = self.overflowing_sub(rhs);
767 if b { imp::overflow_panic::sub() } else { a }
768 }
769
770 #[doc = concat!("`self - rhs > ", stringify!($SelfT), "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`,")]
783 #[doc = concat!("[`checked_sub`]: ", stringify!($SelfT), "::checked_sub")]
787 #[doc = concat!("[`wrapping_sub`]: ", stringify!($SelfT), "::wrapping_sub")]
788 #[stable(feature = "unchecked_math", since = "1.79.0")]
789 #[rustc_const_stable(feature = "unchecked_math", since = "1.79.0")]
790 #[must_use = "this returns the result of the operation, \
791 without modifying the original"]
792 #[inline(always)]
793 #[track_caller]
794 pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
795 assert_unsafe_precondition!(
796 check_language_ub,
797 concat!(stringify!($SelfT), "::unchecked_sub cannot overflow"),
798 (
799 lhs: $SelfT = self,
800 rhs: $SelfT = rhs,
801 ) => !lhs.overflowing_sub(rhs).1,
802 );
803
804 unsafe {
806 intrinsics::unchecked_sub(self, rhs)
807 }
808 }
809
810 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_sub_unsigned(2), Some(-1));")]
817 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub_unsigned(3), None);")]
818 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
820 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
821 #[must_use = "this returns the result of the operation, \
822 without modifying the original"]
823 #[inline]
824 pub const fn checked_sub_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
825 let (a, b) = self.overflowing_sub_unsigned(rhs);
826 if intrinsics::unlikely(b) { None } else { Some(a) }
827 }
828
829 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".strict_sub_unsigned(2), -1);")]
842 #[doc = concat!("let _ = (", stringify!($SelfT), "::MIN + 2).strict_sub_unsigned(3);")]
848 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
850 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
851 #[must_use = "this returns the result of the operation, \
852 without modifying the original"]
853 #[inline]
854 #[track_caller]
855 pub const fn strict_sub_unsigned(self, rhs: $UnsignedT) -> Self {
856 let (a, b) = self.overflowing_sub_unsigned(rhs);
857 if b { imp::overflow_panic::sub() } else { a }
858 }
859
860 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(1), Some(", stringify!($SelfT), "::MAX));")]
867 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);")]
868 #[stable(feature = "rust1", since = "1.0.0")]
870 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
871 #[must_use = "this returns the result of the operation, \
872 without modifying the original"]
873 #[inline]
874 pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
875 let (a, b) = self.overflowing_mul(rhs);
876 if intrinsics::unlikely(b) { None } else { Some(a) }
877 }
878
879 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.strict_mul(1), ", stringify!($SelfT), "::MAX);")]
892 #[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_mul(2);")]
898 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
900 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
901 #[must_use = "this returns the result of the operation, \
902 without modifying the original"]
903 #[inline]
904 #[track_caller]
905 pub const fn strict_mul(self, rhs: Self) -> Self {
906 let (a, b) = self.overflowing_mul(rhs);
907 if b { imp::overflow_panic::mul() } else { a }
908 }
909
910 #[doc = concat!("`self * rhs > ", stringify!($SelfT), "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`,")]
923 #[doc = concat!("[`checked_mul`]: ", stringify!($SelfT), "::checked_mul")]
927 #[doc = concat!("[`wrapping_mul`]: ", stringify!($SelfT), "::wrapping_mul")]
928 #[stable(feature = "unchecked_math", since = "1.79.0")]
929 #[rustc_const_stable(feature = "unchecked_math", since = "1.79.0")]
930 #[must_use = "this returns the result of the operation, \
931 without modifying the original"]
932 #[inline(always)]
933 #[track_caller]
934 pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
935 assert_unsafe_precondition!(
936 check_language_ub,
937 concat!(stringify!($SelfT), "::unchecked_mul cannot overflow"),
938 (
939 lhs: $SelfT = self,
940 rhs: $SelfT = rhs,
941 ) => !lhs.overflowing_mul(rhs).1,
942 );
943
944 unsafe {
946 intrinsics::unchecked_mul(self, rhs)
947 }
948 }
949
950 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div(-1), Some(", stringify!($Max), "));")]
957 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div(-1), None);")]
958 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div(0), None);")]
959 #[stable(feature = "rust1", since = "1.0.0")]
961 #[rustc_const_stable(feature = "const_checked_int_div", since = "1.52.0")]
962 #[must_use = "this returns the result of the operation, \
963 without modifying the original"]
964 #[inline]
965 pub const fn checked_div(self, rhs: Self) -> Option<Self> {
966 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
967 None
968 } else {
969 Some(unsafe { intrinsics::unchecked_div(self, rhs) })
971 }
972 }
973
974 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).strict_div(-1), ", stringify!($Max), ");")]
996 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_div(-1);")]
1002 #[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div(0);")]
1008 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1010 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1011 #[must_use = "this returns the result of the operation, \
1012 without modifying the original"]
1013 #[inline]
1014 #[track_caller]
1015 pub const fn strict_div(self, rhs: Self) -> Self {
1016 self / rhs
1018 }
1019
1020 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div_euclid(-1), Some(", stringify!($Max), "));")]
1027 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div_euclid(-1), None);")]
1028 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div_euclid(0), None);")]
1029 #[stable(feature = "euclidean_division", since = "1.38.0")]
1031 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1032 #[must_use = "this returns the result of the operation, \
1033 without modifying the original"]
1034 #[inline]
1035 pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
1036 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
1038 None
1039 } else {
1040 Some(self.div_euclid(rhs))
1041 }
1042 }
1043
1044 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).strict_div_euclid(-1), ", stringify!($Max), ");")]
1066 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_div_euclid(-1);")]
1072 #[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div_euclid(0);")]
1078 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1080 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1081 #[must_use = "this returns the result of the operation, \
1082 without modifying the original"]
1083 #[inline]
1084 #[track_caller]
1085 pub const fn strict_div_euclid(self, rhs: Self) -> Self {
1086 self.div_euclid(rhs)
1088 }
1089
1090 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div_exact(-1), Some(", stringify!($Max), "));")]
1099 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_div_exact(2), None);")]
1100 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div_exact(-1), None);")]
1101 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div_exact(0), None);")]
1102 #[unstable(
1104 feature = "exact_div",
1105 issue = "139911",
1106 )]
1107 #[must_use = "this returns the result of the operation, \
1108 without modifying the original"]
1109 #[inline]
1110 pub const fn checked_div_exact(self, rhs: Self) -> Option<Self> {
1111 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
1112 None
1113 } else {
1114 unsafe {
1116 if intrinsics::unlikely(intrinsics::unchecked_rem(self, rhs) != 0) {
1117 None
1118 } else {
1119 Some(intrinsics::exact_div(self, rhs))
1120 }
1121 }
1122 }
1123 }
1124
1125 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".div_exact(2), Some(32));")]
1141 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".div_exact(32), Some(2));")]
1142 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).div_exact(-1), Some(", stringify!($Max), "));")]
1143 #[doc = concat!("assert_eq!(65", stringify!($SelfT), ".div_exact(2), None);")]
1144 #[doc = concat!("let _ = 64", stringify!($SelfT),".div_exact(0);")]
1148 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.div_exact(-1);")]
1152 #[unstable(
1154 feature = "exact_div",
1155 issue = "139911",
1156 )]
1157 #[must_use = "this returns the result of the operation, \
1158 without modifying the original"]
1159 #[inline]
1160 #[rustc_inherit_overflow_checks]
1161 pub const fn div_exact(self, rhs: Self) -> Option<Self> {
1162 if self % rhs != 0 {
1163 None
1164 } else {
1165 Some(self / rhs)
1166 }
1167 }
1168
1169 #[doc = concat!("`self == ", stringify!($SelfT), "::MIN && rhs == -1`,")]
1175 #[unstable(
1177 feature = "exact_div",
1178 issue = "139911",
1179 )]
1180 #[must_use = "this returns the result of the operation, \
1181 without modifying the original"]
1182 #[inline]
1183 pub const unsafe fn unchecked_div_exact(self, rhs: Self) -> Self {
1184 assert_unsafe_precondition!(
1185 check_language_ub,
1186 concat!(stringify!($SelfT), "::unchecked_div_exact cannot overflow, divide by zero, or leave a remainder"),
1187 (
1188 lhs: $SelfT = self,
1189 rhs: $SelfT = rhs,
1190 ) => rhs != 0 && lhs % rhs == 0 && (lhs != <$SelfT>::MIN || rhs != -1),
1191 );
1192 unsafe { intrinsics::exact_div(self, rhs) }
1194 }
1195
1196 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(2), Some(1));")]
1203 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(0), None);")]
1204 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem(-1), None);")]
1205 #[stable(feature = "wrapping", since = "1.7.0")]
1207 #[rustc_const_stable(feature = "const_checked_int_div", since = "1.52.0")]
1208 #[must_use = "this returns the result of the operation, \
1209 without modifying the original"]
1210 #[inline]
1211 pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
1212 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
1213 None
1214 } else {
1215 Some(unsafe { intrinsics::unchecked_rem(self, rhs) })
1217 }
1218 }
1219
1220 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_rem(2), 1);")]
1238 #[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem(0);")]
1244 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_rem(-1);")]
1250 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1252 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1253 #[must_use = "this returns the result of the operation, \
1254 without modifying the original"]
1255 #[inline]
1256 #[track_caller]
1257 pub const fn strict_rem(self, rhs: Self) -> Self {
1258 let (a, b) = self.overflowing_rem(rhs);
1259 if b { imp::overflow_panic::rem() } else { a }
1260 }
1261
1262 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(2), Some(1));")]
1269 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(0), None);")]
1270 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem_euclid(-1), None);")]
1271 #[stable(feature = "euclidean_division", since = "1.38.0")]
1273 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1274 #[must_use = "this returns the result of the operation, \
1275 without modifying the original"]
1276 #[inline]
1277 pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
1278 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
1280 None
1281 } else {
1282 Some(self.rem_euclid(rhs))
1283 }
1284 }
1285
1286 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_rem_euclid(2), 1);")]
1304 #[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem_euclid(0);")]
1310 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_rem_euclid(-1);")]
1316 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1318 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1319 #[must_use = "this returns the result of the operation, \
1320 without modifying the original"]
1321 #[inline]
1322 #[track_caller]
1323 pub const fn strict_rem_euclid(self, rhs: Self) -> Self {
1324 let (a, b) = self.overflowing_rem_euclid(rhs);
1325 if b { imp::overflow_panic::rem() } else { a }
1326 }
1327
1328 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_neg(), Some(-5));")]
1334 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_neg(), None);")]
1335 #[stable(feature = "wrapping", since = "1.7.0")]
1337 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1338 #[must_use = "this returns the result of the operation, \
1339 without modifying the original"]
1340 #[inline]
1341 pub const fn checked_neg(self) -> Option<Self> {
1342 let (a, b) = self.overflowing_neg();
1343 if intrinsics::unlikely(b) { None } else { Some(a) }
1344 }
1345
1346 #[doc = concat!("`self == ", stringify!($SelfT), "::MIN`,")]
1352 #[doc = concat!("[`checked_neg`]: ", stringify!($SelfT), "::checked_neg")]
1355 #[stable(feature = "unchecked_neg", since = "1.93.0")]
1356 #[rustc_const_stable(feature = "unchecked_neg", since = "1.93.0")]
1357 #[must_use = "this returns the result of the operation, \
1358 without modifying the original"]
1359 #[inline(always)]
1360 #[track_caller]
1361 pub const unsafe fn unchecked_neg(self) -> Self {
1362 assert_unsafe_precondition!(
1363 check_language_ub,
1364 concat!(stringify!($SelfT), "::unchecked_neg cannot overflow"),
1365 (
1366 lhs: $SelfT = self,
1367 ) => !lhs.overflowing_neg().1,
1368 );
1369
1370 unsafe {
1372 intrinsics::unchecked_sub(0, self)
1373 }
1374 }
1375
1376 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_neg(), -5);")]
1388 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_neg();")]
1394 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1396 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1397 #[must_use = "this returns the result of the operation, \
1398 without modifying the original"]
1399 #[inline]
1400 #[track_caller]
1401 pub const fn strict_neg(self) -> Self {
1402 let (a, b) = self.overflowing_neg();
1403 if b { imp::overflow_panic::neg() } else { a }
1404 }
1405
1406 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(4), Some(0x10));")]
1413 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(129), None);")]
1414 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shl(", stringify!($BITS_MINUS_ONE), "), Some(0));")]
1415 #[stable(feature = "wrapping", since = "1.7.0")]
1417 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1418 #[must_use = "this returns the result of the operation, \
1419 without modifying the original"]
1420 #[inline]
1421 pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
1422 if rhs < Self::BITS {
1424 Some(unsafe { self.unchecked_shl(rhs) })
1426 } else {
1427 None
1428 }
1429 }
1430
1431 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".strict_shl(4), 0x10);")]
1444 #[doc = concat!("let _ = 0x1", stringify!($SelfT), ".strict_shl(129);")]
1450 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1452 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1453 #[must_use = "this returns the result of the operation, \
1454 without modifying the original"]
1455 #[inline]
1456 #[track_caller]
1457 pub const fn strict_shl(self, rhs: u32) -> Self {
1458 let (a, b) = self.overflowing_shl(rhs);
1459 if b { imp::overflow_panic::shl() } else { a }
1460 }
1461
1462 #[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
1472 #[stable(feature = "unchecked_shifts", since = "1.93.0")]
1473 #[rustc_const_stable(feature = "unchecked_shifts", since = "1.93.0")]
1474 #[must_use = "this returns the result of the operation, \
1475 without modifying the original"]
1476 #[inline(always)]
1477 #[track_caller]
1478 pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
1479 assert_unsafe_precondition!(
1480 check_language_ub,
1481 concat!(stringify!($SelfT), "::unchecked_shl cannot overflow"),
1482 (
1483 rhs: u32 = rhs,
1484 ) => rhs < <$ActualT>::BITS,
1485 );
1486
1487 unsafe {
1489 intrinsics::unchecked_shl(self, rhs)
1490 }
1491 }
1492
1493 #[doc = concat!("assert_eq!(0x1_", stringify!($SelfT), ".unbounded_shl(4), 0x10);")]
1502 #[doc = concat!("assert_eq!(0x1_", stringify!($SelfT), ".unbounded_shl(129), 0);")]
1503 #[doc = concat!("assert_eq!(0b101_", stringify!($SelfT), ".unbounded_shl(0), 0b101);")]
1504 #[doc = concat!("assert_eq!(0b101_", stringify!($SelfT), ".unbounded_shl(1), 0b1010);")]
1505 #[doc = concat!("assert_eq!(0b101_", stringify!($SelfT), ".unbounded_shl(2), 0b10100);")]
1506 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shl(", stringify!($BITS), "), 0);")]
1507 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shl(1).unbounded_shl(", stringify!($BITS_MINUS_ONE), "), 0);")]
1508 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shl(", stringify!($BITS), "), 0);")]
1509 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shl(1).unbounded_shl(", stringify!($BITS_MINUS_ONE), "), 0);")]
1510 #[stable(feature = "unbounded_shifts", since = "1.87.0")]
1512 #[rustc_const_stable(feature = "unbounded_shifts", since = "1.87.0")]
1513 #[must_use = "this returns the result of the operation, \
1514 without modifying the original"]
1515 #[inline]
1516 pub const fn unbounded_shl(self, rhs: u32) -> $SelfT{
1517 if rhs < Self::BITS {
1518 unsafe { self.unchecked_shl(rhs) }
1521 } else {
1522 0
1523 }
1524 }
1525
1526 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1531 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".shl_exact(4), Some(0x10));")]
1539 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".shl_exact(", stringify!($SelfT), "::BITS - 2), Some(1 << ", stringify!($SelfT), "::BITS - 2));")]
1540 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".shl_exact(", stringify!($SelfT), "::BITS - 1), None);")]
1541 #[doc = concat!("assert_eq!((-0x2", stringify!($SelfT), ").shl_exact(", stringify!($SelfT), "::BITS - 2), Some(-0x2 << ", stringify!($SelfT), "::BITS - 2));")]
1542 #[doc = concat!("assert_eq!((-0x2", stringify!($SelfT), ").shl_exact(", stringify!($SelfT), "::BITS - 1), None);")]
1543 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1545 #[must_use = "this returns the result of the operation, \
1546 without modifying the original"]
1547 #[inline]
1548 pub const fn shl_exact(self, rhs: u32) -> Option<$SelfT> {
1549 if rhs < self.leading_zeros() || rhs < self.leading_ones() {
1550 Some(unsafe { self.unchecked_shl(rhs) })
1552 } else {
1553 None
1554 }
1555 }
1556
1557 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1560 #[doc = concat!("[`", stringify!($SelfT), "::shl_exact`]")]
1566 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1568 #[must_use = "this returns the result of the operation, \
1569 without modifying the original"]
1570 #[inline]
1571 pub const unsafe fn unchecked_shl_exact(self, rhs: u32) -> $SelfT {
1572 assert_unsafe_precondition!(
1573 check_library_ub,
1574 concat!(stringify!($SelfT), "::unchecked_shl_exact cannot shift out bits that would change the value of the first bit"),
1575 (
1576 zeros: u32 = self.leading_zeros(),
1577 ones: u32 = self.leading_ones(),
1578 rhs: u32 = rhs,
1579 ) => rhs < zeros || rhs < ones,
1580 );
1581
1582 unsafe { self.unchecked_shl(rhs) }
1584 }
1585
1586 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(4), Some(0x1));")]
1593 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(128), None);")]
1594 #[stable(feature = "wrapping", since = "1.7.0")]
1596 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1597 #[must_use = "this returns the result of the operation, \
1598 without modifying the original"]
1599 #[inline]
1600 pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
1601 if rhs < Self::BITS {
1603 Some(unsafe { self.unchecked_shr(rhs) })
1605 } else {
1606 None
1607 }
1608 }
1609
1610 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".strict_shr(4), 0x1);")]
1623 #[doc = concat!("let _ = 0x10", stringify!($SelfT), ".strict_shr(128);")]
1629 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1631 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1632 #[must_use = "this returns the result of the operation, \
1633 without modifying the original"]
1634 #[inline]
1635 #[track_caller]
1636 pub const fn strict_shr(self, rhs: u32) -> Self {
1637 let (a, b) = self.overflowing_shr(rhs);
1638 if b { imp::overflow_panic::shr() } else { a }
1639 }
1640
1641 #[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
1651 #[stable(feature = "unchecked_shifts", since = "1.93.0")]
1652 #[rustc_const_stable(feature = "unchecked_shifts", since = "1.93.0")]
1653 #[must_use = "this returns the result of the operation, \
1654 without modifying the original"]
1655 #[inline(always)]
1656 #[track_caller]
1657 pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
1658 assert_unsafe_precondition!(
1659 check_language_ub,
1660 concat!(stringify!($SelfT), "::unchecked_shr cannot overflow"),
1661 (
1662 rhs: u32 = rhs,
1663 ) => rhs < <$ActualT>::BITS,
1664 );
1665
1666 unsafe {
1668 intrinsics::unchecked_shr(self, rhs)
1669 }
1670 }
1671
1672 #[doc = concat!("assert_eq!(0x10_", stringify!($SelfT), ".unbounded_shr(4), 0x1);")]
1682 #[doc = concat!("assert_eq!(0x10_", stringify!($SelfT), ".unbounded_shr(129), 0);")]
1683 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.unbounded_shr(129), -1);")]
1684 #[doc = concat!("assert_eq!(0b1010_", stringify!($SelfT), ".unbounded_shr(0), 0b1010);")]
1685 #[doc = concat!("assert_eq!(0b1010_", stringify!($SelfT), ".unbounded_shr(1), 0b101);")]
1686 #[doc = concat!("assert_eq!(0b1010_", stringify!($SelfT), ".unbounded_shr(2), 0b10);")]
1687 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shr(", stringify!($BITS), "), 0);")]
1688 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shr(1).unbounded_shr(", stringify!($BITS_MINUS_ONE), "), 0);")]
1689 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shr(", stringify!($BITS), "), -1);")]
1690 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shr(1).unbounded_shr(", stringify!($BITS_MINUS_ONE), "), -1);")]
1691 #[stable(feature = "unbounded_shifts", since = "1.87.0")]
1693 #[rustc_const_stable(feature = "unbounded_shifts", since = "1.87.0")]
1694 #[must_use = "this returns the result of the operation, \
1695 without modifying the original"]
1696 #[inline]
1697 pub const fn unbounded_shr(self, rhs: u32) -> $SelfT{
1698 if rhs < Self::BITS {
1699 unsafe { self.unchecked_shr(rhs) }
1702 } else {
1703 unsafe { self.unchecked_shr(Self::BITS - 1) }
1708 }
1709 }
1710
1711 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1715 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".shr_exact(4), Some(0x1));")]
1723 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".shr_exact(5), None);")]
1724 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1726 #[must_use = "this returns the result of the operation, \
1727 without modifying the original"]
1728 #[inline]
1729 pub const fn shr_exact(self, rhs: u32) -> Option<$SelfT> {
1730 if rhs <= self.trailing_zeros() && rhs < <$SelfT>::BITS {
1731 Some(unsafe { self.unchecked_shr(rhs) })
1733 } else {
1734 None
1735 }
1736 }
1737
1738 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1741 #[doc = concat!(stringify!($SelfT), "::BITS`")]
1746 #[doc = concat!("[`", stringify!($SelfT), "::shr_exact`]")]
1748 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1750 #[must_use = "this returns the result of the operation, \
1751 without modifying the original"]
1752 #[inline]
1753 pub const unsafe fn unchecked_shr_exact(self, rhs: u32) -> $SelfT {
1754 assert_unsafe_precondition!(
1755 check_library_ub,
1756 concat!(stringify!($SelfT), "::unchecked_shr_exact cannot shift out non-zero bits"),
1757 (
1758 zeros: u32 = self.trailing_zeros(),
1759 bits: u32 = <$SelfT>::BITS,
1760 rhs: u32 = rhs,
1761 ) => rhs <= zeros && rhs < bits,
1762 );
1763
1764 unsafe { self.unchecked_shr(rhs) }
1766 }
1767
1768 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_abs(), Some(5));")]
1775 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_abs(), None);")]
1776 #[stable(feature = "no_panic_abs", since = "1.13.0")]
1778 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1779 #[must_use = "this returns the result of the operation, \
1780 without modifying the original"]
1781 #[inline]
1782 pub const fn checked_abs(self) -> Option<Self> {
1783 if self.is_negative() {
1784 self.checked_neg()
1785 } else {
1786 Some(self)
1787 }
1788 }
1789
1790 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").strict_abs(), 5);")]
1803 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_abs();")]
1809 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1811 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1812 #[must_use = "this returns the result of the operation, \
1813 without modifying the original"]
1814 #[inline]
1815 #[track_caller]
1816 pub const fn strict_abs(self) -> Self {
1817 if self.is_negative() {
1818 self.strict_neg()
1819 } else {
1820 self
1821 }
1822 }
1823
1824 #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".checked_pow(2), Some(64));")]
1831 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".checked_pow(0), Some(1));")]
1832 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);")]
1833 #[stable(feature = "no_panic_pow", since = "1.34.0")]
1836 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1837 #[must_use = "this returns the result of the operation, \
1838 without modifying the original"]
1839 #[inline]
1840 pub const fn checked_pow(self, mut exp: u32) -> Option<Self> {
1841 let mut base = self;
1842 let mut acc: Self = 1;
1843
1844 if intrinsics::is_val_statically_known(base) && base.unsigned_abs().is_power_of_two() {
1845 let k = base.unsigned_abs().ilog2();
1846 let shift = try_opt!(k.checked_mul(exp));
1847 return if base < 0 && (exp % 2) == 1 {
1848 (-1 as Self).shl_exact(shift)
1849 } else {
1850 (1 as Self).shl_exact(shift)
1851 }
1852 }
1853
1854 if exp == 0 {
1855 return Some(1);
1856 }
1857
1858 if intrinsics::is_val_statically_known(exp) {
1859 while exp > 1 {
1860 if (exp & 1) == 1 {
1861 acc = try_opt!(acc.checked_mul(base));
1862 }
1863 exp /= 2;
1864 base = try_opt!(base.checked_mul(base));
1865 }
1866
1867 return acc.checked_mul(base);
1872 }
1873
1874 loop {
1875 if (exp & 1) == 1 {
1876 acc = try_opt!(acc.checked_mul(base));
1877 if exp == 1 {
1879 return Some(acc);
1880 }
1881 }
1882 exp /= 2;
1883 base = try_opt!(base.checked_mul(base));
1884 }
1885 }
1886
1887 #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".strict_pow(2), 64);")]
1900 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".strict_pow(0), 1);")]
1901 #[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_pow(2);")]
1907 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1909 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1910 #[must_use = "this returns the result of the operation, \
1911 without modifying the original"]
1912 #[inline]
1913 #[track_caller]
1914 pub const fn strict_pow(self, exp: u32) -> Self {
1915 match self.checked_pow(exp) {
1916 Some(x) => x,
1917 None => imp::overflow_panic::pow(),
1918 }
1919 }
1920
1921 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_isqrt(), Some(3));")]
1933 #[stable(feature = "isqrt", since = "1.84.0")]
1935 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
1936 #[must_use = "this returns the result of the operation, \
1937 without modifying the original"]
1938 #[inline]
1939 pub const fn checked_isqrt(self) -> Option<Self> {
1940 if self < 0 {
1941 None
1942 } else {
1943 let result = self.cast_unsigned().isqrt().cast_signed();
1946
1947 unsafe {
1959 const MAX_RESULT: $SelfT = <$SelfT>::MAX.cast_unsigned().isqrt().cast_signed();
1960 crate::hint::assert_unchecked(result <= MAX_RESULT);
1961 }
1962 Some(result)
1963 }
1964 }
1965
1966 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_add(1), 101);")]
1973 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add(100), ", stringify!($SelfT), "::MAX);")]
1974 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_add(-1), ", stringify!($SelfT), "::MIN);")]
1975 #[stable(feature = "rust1", since = "1.0.0")]
1978 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1979 #[must_use = "this returns the result of the operation, \
1980 without modifying the original"]
1981 #[inline(always)]
1982 pub const fn saturating_add(self, rhs: Self) -> Self {
1983 intrinsics::saturating_add(self, rhs)
1984 }
1985
1986 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".saturating_add_unsigned(2), 3);")]
1993 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add_unsigned(100), ", stringify!($SelfT), "::MAX);")]
1994 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1996 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1997 #[must_use = "this returns the result of the operation, \
1998 without modifying the original"]
1999 #[inline]
2000 pub const fn saturating_add_unsigned(self, rhs: $UnsignedT) -> Self {
2001 match self.checked_add_unsigned(rhs) {
2004 Some(x) => x,
2005 None => Self::MAX,
2006 }
2007 }
2008
2009 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub(127), -27);")]
2016 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub(100), ", stringify!($SelfT), "::MIN);")]
2017 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_sub(-1), ", stringify!($SelfT), "::MAX);")]
2018 #[stable(feature = "rust1", since = "1.0.0")]
2020 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2021 #[must_use = "this returns the result of the operation, \
2022 without modifying the original"]
2023 #[inline(always)]
2024 pub const fn saturating_sub(self, rhs: Self) -> Self {
2025 intrinsics::saturating_sub(self, rhs)
2026 }
2027
2028 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub_unsigned(127), -27);")]
2035 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub_unsigned(100), ", stringify!($SelfT), "::MIN);")]
2036 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2038 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2039 #[must_use = "this returns the result of the operation, \
2040 without modifying the original"]
2041 #[inline]
2042 pub const fn saturating_sub_unsigned(self, rhs: $UnsignedT) -> Self {
2043 match self.checked_sub_unsigned(rhs) {
2046 Some(x) => x,
2047 None => Self::MIN,
2048 }
2049 }
2050
2051 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_neg(), -100);")]
2058 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_neg(), 100);")]
2059 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_neg(), ", stringify!($SelfT), "::MAX);")]
2060 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_neg(), ", stringify!($SelfT), "::MIN + 1);")]
2061 #[stable(feature = "saturating_neg", since = "1.45.0")]
2064 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2065 #[must_use = "this returns the result of the operation, \
2066 without modifying the original"]
2067 #[inline(always)]
2068 pub const fn saturating_neg(self) -> Self {
2069 intrinsics::saturating_sub(0, self)
2070 }
2071
2072 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_abs(), 100);")]
2079 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_abs(), 100);")]
2080 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_abs(), ", stringify!($SelfT), "::MAX);")]
2081 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).saturating_abs(), ", stringify!($SelfT), "::MAX);")]
2082 #[stable(feature = "saturating_neg", since = "1.45.0")]
2085 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2086 #[must_use = "this returns the result of the operation, \
2087 without modifying the original"]
2088 #[inline]
2089 pub const fn saturating_abs(self) -> Self {
2090 if self.is_negative() {
2091 self.saturating_neg()
2092 } else {
2093 self
2094 }
2095 }
2096
2097 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".saturating_mul(12), 120);")]
2104 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_mul(10), ", stringify!($SelfT), "::MAX);")]
2105 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_mul(10), ", stringify!($SelfT), "::MIN);")]
2106 #[stable(feature = "wrapping", since = "1.7.0")]
2108 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2109 #[must_use = "this returns the result of the operation, \
2110 without modifying the original"]
2111 #[inline]
2112 pub const fn saturating_mul(self, rhs: Self) -> Self {
2113 match self.checked_mul(rhs) {
2114 Some(x) => x,
2115 None => if (self < 0) == (rhs < 0) {
2116 Self::MAX
2117 } else {
2118 Self::MIN
2119 }
2120 }
2121 }
2122
2123 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
2134 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_div(-1), ", stringify!($SelfT), "::MIN + 1);")]
2135 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_div(-1), ", stringify!($SelfT), "::MAX);")]
2136 #[stable(feature = "saturating_div", since = "1.58.0")]
2139 #[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
2140 #[must_use = "this returns the result of the operation, \
2141 without modifying the original"]
2142 #[inline]
2143 #[track_caller]
2144 pub const fn saturating_div(self, rhs: Self) -> Self {
2145 match self.overflowing_div(rhs) {
2146 (result, false) => result,
2147 (_result, true) => Self::MAX, }
2149 }
2150
2151 #[doc = concat!("assert_eq!((-4", stringify!($SelfT), ").saturating_pow(3), -64);")]
2158 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".saturating_pow(0), 1);")]
2159 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(2), ", stringify!($SelfT), "::MAX);")]
2160 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(3), ", stringify!($SelfT), "::MIN);")]
2161 #[stable(feature = "no_panic_pow", since = "1.34.0")]
2163 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2164 #[must_use = "this returns the result of the operation, \
2165 without modifying the original"]
2166 #[inline]
2167 pub const fn saturating_pow(self, exp: u32) -> Self {
2168 match self.checked_pow(exp) {
2169 Some(x) => x,
2170 None if self < 0 && exp % 2 == 1 => Self::MIN,
2171 None => Self::MAX,
2172 }
2173 }
2174
2175 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add(27), 127);")]
2182 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add(2), ", stringify!($SelfT), "::MIN + 1);")]
2183 #[stable(feature = "rust1", since = "1.0.0")]
2185 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2186 #[must_use = "this returns the result of the operation, \
2187 without modifying the original"]
2188 #[inline(always)]
2189 pub const fn wrapping_add(self, rhs: Self) -> Self {
2190 intrinsics::wrapping_add(self, rhs)
2191 }
2192
2193 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add_unsigned(27), 127);")]
2200 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add_unsigned(2), ", stringify!($SelfT), "::MIN + 1);")]
2201 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2203 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2204 #[must_use = "this returns the result of the operation, \
2205 without modifying the original"]
2206 #[inline(always)]
2207 pub const fn wrapping_add_unsigned(self, rhs: $UnsignedT) -> Self {
2208 self.wrapping_add(rhs as Self)
2209 }
2210
2211 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub(127), -127);")]
2218 #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub(", stringify!($SelfT), "::MAX), ", stringify!($SelfT), "::MAX);")]
2219 #[stable(feature = "rust1", since = "1.0.0")]
2221 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2222 #[must_use = "this returns the result of the operation, \
2223 without modifying the original"]
2224 #[inline(always)]
2225 pub const fn wrapping_sub(self, rhs: Self) -> Self {
2226 intrinsics::wrapping_sub(self, rhs)
2227 }
2228
2229 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub_unsigned(127), -127);")]
2236 #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub_unsigned(", stringify!($UnsignedT), "::MAX), -1);")]
2237 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2239 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2240 #[must_use = "this returns the result of the operation, \
2241 without modifying the original"]
2242 #[inline(always)]
2243 pub const fn wrapping_sub_unsigned(self, rhs: $UnsignedT) -> Self {
2244 self.wrapping_sub(rhs as Self)
2245 }
2246
2247 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".wrapping_mul(12), 120);")]
2254 #[stable(feature = "rust1", since = "1.0.0")]
2257 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2258 #[must_use = "this returns the result of the operation, \
2259 without modifying the original"]
2260 #[inline(always)]
2261 pub const fn wrapping_mul(self, rhs: Self) -> Self {
2262 intrinsics::wrapping_mul(self, rhs)
2263 }
2264
2265 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div(10), 10);")]
2280 #[stable(feature = "num_wrapping", since = "1.2.0")]
2283 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
2284 #[must_use = "this returns the result of the operation, \
2285 without modifying the original"]
2286 #[inline]
2287 #[track_caller]
2288 pub const fn wrapping_div(self, rhs: Self) -> Self {
2289 self.overflowing_div(rhs).0
2290 }
2291
2292 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div_euclid(10), 10);")]
2307 #[stable(feature = "euclidean_division", since = "1.38.0")]
2310 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2311 #[must_use = "this returns the result of the operation, \
2312 without modifying the original"]
2313 #[inline]
2314 #[track_caller]
2315 pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
2316 self.overflowing_div_euclid(rhs).0
2317 }
2318
2319 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem(10), 0);")]
2334 #[stable(feature = "num_wrapping", since = "1.2.0")]
2337 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
2338 #[must_use = "this returns the result of the operation, \
2339 without modifying the original"]
2340 #[inline]
2341 #[track_caller]
2342 pub const fn wrapping_rem(self, rhs: Self) -> Self {
2343 self.overflowing_rem(rhs).0
2344 }
2345
2346 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);")]
2360 #[stable(feature = "euclidean_division", since = "1.38.0")]
2363 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2364 #[must_use = "this returns the result of the operation, \
2365 without modifying the original"]
2366 #[inline]
2367 #[track_caller]
2368 pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
2369 self.overflowing_rem_euclid(rhs).0
2370 }
2371
2372 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_neg(), -100);")]
2383 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_neg(), 100);")]
2384 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_neg(), ", stringify!($SelfT), "::MIN);")]
2385 #[stable(feature = "num_wrapping", since = "1.2.0")]
2387 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2388 #[must_use = "this returns the result of the operation, \
2389 without modifying the original"]
2390 #[inline(always)]
2391 pub const fn wrapping_neg(self) -> Self {
2392 (0 as $SelfT).wrapping_sub(self)
2393 }
2394
2395 #[doc = concat!("assert_eq!((-1_", stringify!($SelfT), ").wrapping_shl(7), -128);")]
2414 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shl(", stringify!($BITS), "), 42);")]
2415 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shl(1).wrapping_shl(", stringify!($BITS_MINUS_ONE), "), 0);")]
2416 #[doc = concat!("assert_eq!((-1_", stringify!($SelfT), ").wrapping_shl(128), -1);")]
2417 #[doc = concat!("assert_eq!(5_", stringify!($SelfT), ".wrapping_shl(1025), 10);")]
2418 #[stable(feature = "num_wrapping", since = "1.2.0")]
2420 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2421 #[must_use = "this returns the result of the operation, \
2422 without modifying the original"]
2423 #[inline(always)]
2424 pub const fn wrapping_shl(self, rhs: u32) -> Self {
2425 unsafe {
2428 self.unchecked_shl(rhs & (Self::BITS - 1))
2429 }
2430 }
2431
2432 #[doc = concat!("assert_eq!((-128_", stringify!($SelfT), ").wrapping_shr(7), -1);")]
2451 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shr(", stringify!($BITS), "), 42);")]
2452 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shr(1).wrapping_shr(", stringify!($BITS_MINUS_ONE), "), 0);")]
2453 #[doc = concat!("assert_eq!(10_", stringify!($SelfT), ".wrapping_shr(1025), 5);")]
2455 #[stable(feature = "num_wrapping", since = "1.2.0")]
2457 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2458 #[must_use = "this returns the result of the operation, \
2459 without modifying the original"]
2460 #[inline(always)]
2461 pub const fn wrapping_shr(self, rhs: u32) -> Self {
2462 unsafe {
2465 self.unchecked_shr(rhs & (Self::BITS - 1))
2466 }
2467 }
2468
2469 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_abs(), 100);")]
2480 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_abs(), 100);")]
2481 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_abs(), ", stringify!($SelfT), "::MIN);")]
2482 #[stable(feature = "no_panic_abs", since = "1.13.0")]
2485 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2486 #[must_use = "this returns the result of the operation, \
2487 without modifying the original"]
2488 #[allow(unused_attributes)]
2489 #[inline]
2490 pub const fn wrapping_abs(self) -> Self {
2491 if self.is_negative() {
2492 self.wrapping_neg()
2493 } else {
2494 self
2495 }
2496 }
2497
2498 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".unsigned_abs(), 100", stringify!($UnsignedT), ");")]
2506 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").unsigned_abs(), 100", stringify!($UnsignedT), ");")]
2507 #[stable(feature = "unsigned_abs", since = "1.51.0")]
2510 #[rustc_const_stable(feature = "unsigned_abs", since = "1.51.0")]
2511 #[must_use = "this returns the result of the operation, \
2512 without modifying the original"]
2513 #[inline]
2514 pub const fn unsigned_abs(self) -> $UnsignedT {
2515 self.wrapping_abs() as $UnsignedT
2516 }
2517
2518 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".wrapping_pow(4), 81);")]
2525 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".wrapping_pow(0), 1);")]
2528 #[stable(feature = "no_panic_pow", since = "1.34.0")]
2530 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2531 #[must_use = "this returns the result of the operation, \
2532 without modifying the original"]
2533 #[inline]
2534 pub const fn wrapping_pow(self, exp: u32) -> Self {
2535 let (a, _) = self.overflowing_pow(exp);
2536 a
2537 }
2538
2539 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_add(2), (7, false));")]
2550 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.overflowing_add(1), (", stringify!($SelfT), "::MIN, true));")]
2551 #[stable(feature = "wrapping", since = "1.7.0")]
2553 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2554 #[must_use = "this returns the result of the operation, \
2555 without modifying the original"]
2556 #[inline(always)]
2557 pub const fn overflowing_add(self, rhs: Self) -> (Self, bool) {
2558 let (a, b) = intrinsics::add_with_overflow(self as $ActualT, rhs as $ActualT);
2559 (a as Self, b)
2560 }
2561
2562 #[doc = concat!("[`", stringify!($UnsignedT), "::carrying_add`]")]
2574 #[doc = concat!("// 10 MAX (a = 10 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")]
2592 #[doc = concat!("// + -5 9 (b = -5 × 2^", stringify!($BITS), " + 9)")]
2593 #[doc = concat!("// 6 8 (sum = 6 × 2^", stringify!($BITS), " + 8)")]
2595 #[doc = concat!("let (a1, a0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (10, ", stringify!($UnsignedT), "::MAX);")]
2597 #[doc = concat!("let (b1, b0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (-5, 9);")]
2598 #[doc = concat!("// ", stringify!($UnsignedT), "::carrying_add for the less significant words")]
2601 #[doc = concat!("// ", stringify!($SelfT), "::carrying_add for the most significant word")]
2605 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2611 #[must_use = "this returns the result of the operation, \
2612 without modifying the original"]
2613 #[inline]
2614 pub const fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool) {
2615 let (a, b) = self.overflowing_add(rhs);
2618 let (c, d) = a.overflowing_add(carry as $SelfT);
2619 (c, b != d)
2620 }
2621
2622 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_add_unsigned(2), (3, false));")]
2632 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_add_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MAX, false));")]
2633 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).overflowing_add_unsigned(3), (", stringify!($SelfT), "::MIN, true));")]
2634 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2636 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2637 #[must_use = "this returns the result of the operation, \
2638 without modifying the original"]
2639 #[inline]
2640 pub const fn overflowing_add_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
2641 let rhs = rhs as Self;
2642 let (res, overflowed) = self.overflowing_add(rhs);
2643 (res, overflowed ^ (rhs < 0))
2644 }
2645
2646 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_sub(2), (3, false));")]
2656 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_sub(1), (", stringify!($SelfT), "::MAX, true));")]
2657 #[stable(feature = "wrapping", since = "1.7.0")]
2659 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2660 #[must_use = "this returns the result of the operation, \
2661 without modifying the original"]
2662 #[inline(always)]
2663 pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
2664 let (a, b) = intrinsics::sub_with_overflow(self as $ActualT, rhs as $ActualT);
2665 (a as Self, b)
2666 }
2667
2668 #[doc = concat!("[`", stringify!($UnsignedT), "::borrowing_sub`]")]
2681 #[doc = concat!("// 6 8 (a = 6 × 2^", stringify!($BITS), " + 8)")]
2699 #[doc = concat!("// - -5 9 (b = -5 × 2^", stringify!($BITS), " + 9)")]
2700 #[doc = concat!("// 10 MAX (diff = 10 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")]
2702 #[doc = concat!("let (a1, a0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (6, 8);")]
2704 #[doc = concat!("let (b1, b0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (-5, 9);")]
2705 #[doc = concat!("// ", stringify!($UnsignedT), "::borrowing_sub for the less significant words")]
2708 #[doc = concat!("// ", stringify!($SelfT), "::borrowing_sub for the most significant word")]
2712 #[doc = concat!("assert_eq!((diff1, diff0), (10, ", stringify!($UnsignedT), "::MAX));")]
2716 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2718 #[must_use = "this returns the result of the operation, \
2719 without modifying the original"]
2720 #[inline]
2721 pub const fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
2722 let (a, b) = self.overflowing_sub(rhs);
2725 let (c, d) = a.overflowing_sub(borrow as $SelfT);
2726 (c, b != d)
2727 }
2728
2729 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_sub_unsigned(2), (-1, false));")]
2739 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX).overflowing_sub_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MIN, false));")]
2740 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).overflowing_sub_unsigned(3), (", stringify!($SelfT), "::MAX, true));")]
2741 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2743 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2744 #[must_use = "this returns the result of the operation, \
2745 without modifying the original"]
2746 #[inline]
2747 pub const fn overflowing_sub_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
2748 let rhs = rhs as Self;
2749 let (res, overflowed) = self.overflowing_sub(rhs);
2750 (res, overflowed ^ (rhs < 0))
2751 }
2752
2753 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_mul(2), (10, false));")]
2762 #[stable(feature = "wrapping", since = "1.7.0")]
2765 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2766 #[must_use = "this returns the result of the operation, \
2767 without modifying the original"]
2768 #[inline(always)]
2769 pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
2770 let (a, b) = intrinsics::mul_with_overflow(self as $ActualT, rhs as $ActualT);
2771 (a as Self, b)
2772 }
2773
2774 #[doc = concat!("assert_eq!(",
2795 stringify!($SelfT), "::MAX.carrying_mul(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
2796 "(", stringify!($SelfT), "::MAX.unsigned_abs() + 1, ", stringify!($SelfT), "::MAX / 2));"
2797 )]
2798 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2800 #[rustc_const_unstable(feature = "signed_bigint_helpers", issue = "151989")]
2801 #[must_use = "this returns the result of the operation, \
2802 without modifying the original"]
2803 #[inline]
2804 pub const fn carrying_mul(self, rhs: Self, carry: Self) -> ($UnsignedT, Self) {
2805 Self::carrying_mul_add(self, rhs, carry, 0)
2806 }
2807
2808 #[doc = concat!("assert_eq!(",
2831 stringify!($SelfT), "::MAX.carrying_mul_add(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
2832 "(", stringify!($UnsignedT), "::MAX, ", stringify!($SelfT), "::MAX / 2));"
2833 )]
2834 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2836 #[rustc_const_unstable(feature = "signed_bigint_helpers", issue = "151989")]
2837 #[must_use = "this returns the result of the operation, \
2838 without modifying the original"]
2839 #[inline]
2840 pub const fn carrying_mul_add(self, rhs: Self, carry: Self, add: Self) -> ($UnsignedT, Self) {
2841 intrinsics::carrying_mul_add(self, rhs, carry, add)
2842 }
2843
2844 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div(2), (2, false));")]
2857 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div(-1), (", stringify!($SelfT), "::MIN, true));")]
2858 #[inline]
2860 #[stable(feature = "wrapping", since = "1.7.0")]
2861 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
2862 #[must_use = "this returns the result of the operation, \
2863 without modifying the original"]
2864 #[track_caller]
2865 pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
2866 if intrinsics::unlikely((self == Self::MIN) & (rhs == -1)) {
2868 (self, true)
2869 } else {
2870 (self / rhs, false)
2871 }
2872 }
2873
2874 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div_euclid(2), (2, false));")]
2887 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringify!($SelfT), "::MIN, true));")]
2888 #[inline]
2890 #[stable(feature = "euclidean_division", since = "1.38.0")]
2891 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2892 #[must_use = "this returns the result of the operation, \
2893 without modifying the original"]
2894 #[track_caller]
2895 pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
2896 if intrinsics::unlikely((self == Self::MIN) & (rhs == -1)) {
2898 (self, true)
2899 } else {
2900 (self.div_euclid(rhs), false)
2901 }
2902 }
2903
2904 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem(2), (1, false));")]
2917 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem(-1), (0, true));")]
2918 #[inline]
2920 #[stable(feature = "wrapping", since = "1.7.0")]
2921 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
2922 #[must_use = "this returns the result of the operation, \
2923 without modifying the original"]
2924 #[track_caller]
2925 pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
2926 if intrinsics::unlikely(rhs == -1) {
2927 (0, self == Self::MIN)
2928 } else {
2929 (self % rhs, false)
2930 }
2931 }
2932
2933
2934 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem_euclid(2), (1, false));")]
2947 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));")]
2948 #[stable(feature = "euclidean_division", since = "1.38.0")]
2950 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2951 #[must_use = "this returns the result of the operation, \
2952 without modifying the original"]
2953 #[inline]
2954 #[track_caller]
2955 pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
2956 if intrinsics::unlikely(rhs == -1) {
2957 (0, self == Self::MIN)
2958 } else {
2959 (self.rem_euclid(rhs), false)
2960 }
2961 }
2962
2963
2964 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2, false));")]
2974 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($SelfT), "::MIN, true));")]
2975 #[inline]
2977 #[stable(feature = "wrapping", since = "1.7.0")]
2978 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2979 #[must_use = "this returns the result of the operation, \
2980 without modifying the original"]
2981 #[allow(unused_attributes)]
2982 pub const fn overflowing_neg(self) -> (Self, bool) {
2983 if intrinsics::unlikely(self == Self::MIN) {
2984 (Self::MIN, true)
2985 } else {
2986 (-self, false)
2987 }
2988 }
2989
2990 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT),".overflowing_shl(4), (0x10, false));")]
3000 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shl(", stringify!($BITS_MINUS_ONE), "), (0, false));")]
3002 #[stable(feature = "wrapping", since = "1.7.0")]
3004 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3005 #[must_use = "this returns the result of the operation, \
3006 without modifying the original"]
3007 #[inline]
3008 pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
3009 (self.wrapping_shl(rhs), rhs >= Self::BITS)
3010 }
3011
3012 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shr(4), (0x1, false));")]
3022 #[stable(feature = "wrapping", since = "1.7.0")]
3025 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3026 #[must_use = "this returns the result of the operation, \
3027 without modifying the original"]
3028 #[inline]
3029 pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
3030 (self.wrapping_shr(rhs), rhs >= Self::BITS)
3031 }
3032
3033 #[doc = concat!("(e.g., [`", stringify!($SelfT), "::MIN`] for values of type [`", stringify!($SelfT), "`]),")]
3038 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".overflowing_abs(), (10, false));")]
3045 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").overflowing_abs(), (10, false));")]
3046 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_abs(), (", stringify!($SelfT), "::MIN, true));")]
3047 #[stable(feature = "no_panic_abs", since = "1.13.0")]
3049 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3050 #[must_use = "this returns the result of the operation, \
3051 without modifying the original"]
3052 #[inline]
3053 pub const fn overflowing_abs(self) -> (Self, bool) {
3054 (self.wrapping_abs(), self == Self::MIN)
3055 }
3056
3057 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".overflowing_pow(4), (81, false));")]
3066 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".overflowing_pow(0), (1, false));")]
3067 #[stable(feature = "no_panic_pow", since = "1.34.0")]
3070 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
3071 #[must_use = "this returns the result of the operation, \
3072 without modifying the original"]
3073 #[inline]
3074 pub const fn overflowing_pow(self, mut exp: u32) -> (Self, bool) {
3075 let mut base = self;
3076 let mut acc: Self = 1;
3077 let mut overflow = false;
3078 let mut tmp_overflow;
3079
3080 if intrinsics::is_val_statically_known(base) && base.unsigned_abs().is_power_of_two() {
3081 let k = base.unsigned_abs().ilog2();
3082 let Some(shift) = k.checked_mul(exp) else {
3083 return (0, true)
3084 };
3085 let base: Self = if base < 0 && (exp % 2) != 0 { -1 } else { 1 };
3086 return (base.unbounded_shl(shift), base.shl_exact(shift).is_none());
3087 }
3088
3089 if exp == 0 {
3090 return (1, false);
3091 }
3092
3093 if intrinsics::is_val_statically_known(exp) {
3094 while exp > 1 {
3095 if (exp & 1) == 1 {
3096 (acc, tmp_overflow) = acc.overflowing_mul(base);
3097 overflow |= tmp_overflow;
3098 }
3099 exp /= 2;
3100 (base, tmp_overflow) = base.overflowing_mul(base);
3101 overflow |= tmp_overflow;
3102 }
3103
3104 (acc, tmp_overflow) = acc.overflowing_mul(base);
3109 overflow |= tmp_overflow;
3110 return (acc, overflow);
3111 }
3112
3113 loop {
3114 if (exp & 1) == 1 {
3115 (acc, tmp_overflow) = acc.overflowing_mul(base);
3116 overflow |= tmp_overflow;
3117 if exp == 1 {
3119 return (acc, overflow);
3120 }
3121 }
3122 exp /= 2;
3123 (base, tmp_overflow) = base.overflowing_mul(base);
3124 overflow |= tmp_overflow;
3125 }
3126 }
3127
3128 #[doc = concat!("let x: ", stringify!($SelfT), " = 2; // or any other integer type")]
3134 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".pow(0), 1);")]
3137 #[stable(feature = "rust1", since = "1.0.0")]
3139 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
3140 #[must_use = "this returns the result of the operation, \
3141 without modifying the original"]
3142 #[inline]
3143 #[rustc_inherit_overflow_checks]
3144 pub const fn pow(self, exp: u32) -> Self {
3145 if intrinsics::overflow_checks() {
3146 self.strict_pow(exp)
3147 } else {
3148 self.wrapping_pow(exp)
3149 }
3150 }
3151
3152 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".isqrt(), 3);")]
3166 #[stable(feature = "isqrt", since = "1.84.0")]
3168 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
3169 #[must_use = "this returns the result of the operation, \
3170 without modifying the original"]
3171 #[inline]
3172 #[track_caller]
3173 pub const fn isqrt(self) -> Self {
3174 match self.checked_isqrt() {
3175 Some(sqrt) => sqrt,
3176 None => imp::int_sqrt::panic_for_negative_argument(),
3177 }
3178 }
3179
3180 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
3201 #[stable(feature = "euclidean_division", since = "1.38.0")]
3209 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3210 #[must_use = "this returns the result of the operation, \
3211 without modifying the original"]
3212 #[inline]
3213 #[track_caller]
3214 pub const fn div_euclid(self, rhs: Self) -> Self {
3215 let q = self / rhs;
3216 if self % rhs < 0 {
3217 return if rhs > 0 { q - 1 } else { q + 1 }
3218 }
3219 q
3220 }
3221
3222
3223 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
3239 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.rem_euclid(-1);")]
3250 #[doc(alias = "modulo", alias = "mod")]
3252 #[stable(feature = "euclidean_division", since = "1.38.0")]
3253 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3254 #[must_use = "this returns the result of the operation, \
3255 without modifying the original"]
3256 #[inline]
3257 #[track_caller]
3258 pub const fn rem_euclid(self, rhs: Self) -> Self {
3259 let r = self % rhs;
3260 if r < 0 {
3261 r.wrapping_add(rhs.wrapping_abs())
3270 } else {
3271 r
3272 }
3273 }
3274
3275 #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3287 #[unstable(feature = "int_roundings", issue = "88581")]
3295 #[must_use = "this returns the result of the operation, \
3296 without modifying the original"]
3297 #[inline]
3298 #[track_caller]
3299 pub const fn div_floor(self, rhs: Self) -> Self {
3300 let d = self / rhs;
3301 let r = self % rhs;
3302
3303 let correction = (self ^ rhs) >> (Self::BITS - 1);
3310 if r != 0 {
3311 d + correction
3312 } else {
3313 d
3314 }
3315 }
3316
3317 #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3329 #[unstable(feature = "int_roundings", issue = "88581")]
3337 #[must_use = "this returns the result of the operation, \
3338 without modifying the original"]
3339 #[inline]
3340 #[track_caller]
3341 pub const fn div_ceil(self, rhs: Self) -> Self {
3342 let d = self / rhs;
3343 let r = self % rhs;
3344
3345 let correction = 1 + ((self ^ rhs) >> (Self::BITS - 1));
3348 if r != 0 {
3349 d + correction
3350 } else {
3351 d
3352 }
3353 }
3354
3355 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(8), 16);")]
3374 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(8), 24);")]
3375 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
3376 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
3377 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
3378 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
3379 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(-8), -16);")]
3380 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(-8), -24);")]
3381 #[unstable(feature = "int_roundings", issue = "88581")]
3383 #[must_use = "this returns the result of the operation, \
3384 without modifying the original"]
3385 #[inline]
3386 #[rustc_inherit_overflow_checks]
3387 pub const fn next_multiple_of(self, rhs: Self) -> Self {
3388 if rhs == -1 {
3390 return self;
3391 }
3392
3393 let r = self % rhs;
3394 let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
3395 r + rhs
3396 } else {
3397 r
3398 };
3399
3400 if m == 0 {
3401 self
3402 } else {
3403 self + (rhs - m)
3404 }
3405 }
3406
3407 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(16));")]
3418 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(24));")]
3419 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
3420 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
3421 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
3422 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
3423 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-16));")]
3424 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-24));")]
3425 #[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".checked_next_multiple_of(0), None);")]
3426 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_next_multiple_of(2), None);")]
3427 #[unstable(feature = "int_roundings", issue = "88581")]
3429 #[must_use = "this returns the result of the operation, \
3430 without modifying the original"]
3431 #[inline]
3432 pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
3433 if rhs == -1 {
3435 return Some(self);
3436 }
3437
3438 let r = try_opt!(self.checked_rem(rhs));
3439 let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
3440 r + rhs
3442 } else {
3443 r
3444 };
3445
3446 if m == 0 {
3447 Some(self)
3448 } else {
3449 self.checked_add(rhs - m)
3451 }
3452 }
3453
3454 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".ilog(5), 1);")]
3470 #[stable(feature = "int_log", since = "1.67.0")]
3472 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3473 #[must_use = "this returns the result of the operation, \
3474 without modifying the original"]
3475 #[inline]
3476 #[track_caller]
3477 pub const fn ilog(self, base: Self) -> u32 {
3478 assert!(base >= 2, "base of integer logarithm must be at least 2");
3479 if let Some(log) = self.checked_ilog(base) {
3480 log
3481 } else {
3482 imp::int_log10::panic_for_nonpositive_argument()
3483 }
3484 }
3485
3486 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".ilog2(), 1);")]
3496 #[stable(feature = "int_log", since = "1.67.0")]
3498 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3499 #[must_use = "this returns the result of the operation, \
3500 without modifying the original"]
3501 #[inline]
3502 #[track_caller]
3503 pub const fn ilog2(self) -> u32 {
3504 if let Some(log) = self.checked_ilog2() {
3505 log
3506 } else {
3507 imp::int_log10::panic_for_nonpositive_argument()
3508 }
3509 }
3510
3511 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".ilog10(), 1);")]
3521 #[stable(feature = "int_log", since = "1.67.0")]
3523 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3524 #[must_use = "this returns the result of the operation, \
3525 without modifying the original"]
3526 #[inline]
3527 #[track_caller]
3528 pub const fn ilog10(self) -> u32 {
3529 if let Some(log) = self.checked_ilog10() {
3530 log
3531 } else {
3532 imp::int_log10::panic_for_nonpositive_argument()
3533 }
3534 }
3535
3536 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_ilog(5), Some(1));")]
3549 #[stable(feature = "int_log", since = "1.67.0")]
3551 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3552 #[must_use = "this returns the result of the operation, \
3553 without modifying the original"]
3554 #[inline]
3555 pub const fn checked_ilog(self, base: Self) -> Option<u32> {
3556 if self <= 0 || base <= 1 {
3557 None
3558 } else {
3559 (self as $UnsignedT).checked_ilog(base as $UnsignedT)
3562 }
3563 }
3564
3565 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_ilog2(), Some(1));")]
3576 #[stable(feature = "int_log", since = "1.67.0")]
3578 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3579 #[must_use = "this returns the result of the operation, \
3580 without modifying the original"]
3581 #[inline]
3582 pub const fn checked_ilog2(self) -> Option<u32> {
3583 if self <= 0 {
3584 None
3585 } else {
3586 let log = (Self::BITS - 1) - unsafe { intrinsics::ctlz_nonzero(self) as u32 };
3588 Some(log)
3589 }
3590 }
3591
3592 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_ilog10(), Some(1));")]
3600 #[stable(feature = "int_log", since = "1.67.0")]
3602 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3603 #[must_use = "this returns the result of the operation, \
3604 without modifying the original"]
3605 #[inline]
3606 pub const fn checked_ilog10(self) -> Option<u32> {
3607 imp::int_log10::$ActualT(self as $ActualT)
3608 }
3609
3610 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
3616 #[doc = concat!("`", stringify!($SelfT), "`,")]
3618 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
3622 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".abs(), 10);")]
3629 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").abs(), 10);")]
3630 #[stable(feature = "rust1", since = "1.0.0")]
3632 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3633 #[allow(unused_attributes)]
3634 #[must_use = "this returns the result of the operation, \
3635 without modifying the original"]
3636 #[inline]
3637 #[rustc_inherit_overflow_checks]
3638 pub const fn abs(self) -> Self {
3639 if self.is_negative() {
3643 -self
3644 } else {
3645 self
3646 }
3647 }
3648
3649 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(80), 20", stringify!($UnsignedT), ");")]
3658 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(110), 10", stringify!($UnsignedT), ");")]
3659 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(80), 180", stringify!($UnsignedT), ");")]
3660 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(-120), 20", stringify!($UnsignedT), ");")]
3661 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.abs_diff(", stringify!($SelfT), "::MAX), ", stringify!($UnsignedT), "::MAX);")]
3662 #[stable(feature = "int_abs_diff", since = "1.60.0")]
3664 #[rustc_const_stable(feature = "int_abs_diff", since = "1.60.0")]
3665 #[must_use = "this returns the result of the operation, \
3666 without modifying the original"]
3667 #[inline]
3668 pub const fn abs_diff(self, other: Self) -> $UnsignedT {
3669 if self < other {
3670 (other as $UnsignedT).wrapping_sub(self as $UnsignedT)
3684 } else {
3685 (self as $UnsignedT).wrapping_sub(other as $UnsignedT)
3686 }
3687 }
3688
3689 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".signum(), 1);")]
3699 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".signum(), 0);")]
3700 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").signum(), -1);")]
3701 #[stable(feature = "rust1", since = "1.0.0")]
3703 #[rustc_const_stable(feature = "const_int_sign", since = "1.47.0")]
3704 #[must_use = "this returns the result of the operation, \
3705 without modifying the original"]
3706 #[inline(always)]
3707 pub const fn signum(self) -> Self {
3708 crate::intrinsics::three_way_compare(self, 0) as Self
3714 }
3715
3716 #[doc = concat!("assert!(10", stringify!($SelfT), ".is_positive());")]
3723 #[doc = concat!("assert!(!(-10", stringify!($SelfT), ").is_positive());")]
3724 #[must_use]
3726 #[stable(feature = "rust1", since = "1.0.0")]
3727 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3728 #[inline(always)]
3729 pub const fn is_positive(self) -> bool { self > 0 }
3730
3731 #[doc = concat!("assert!((-10", stringify!($SelfT), ").is_negative());")]
3738 #[doc = concat!("assert!(!10", stringify!($SelfT), ".is_negative());")]
3739 #[must_use]
3741 #[stable(feature = "rust1", since = "1.0.0")]
3742 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3743 #[inline(always)]
3744 pub const fn is_negative(self) -> bool { self < 0 }
3745
3746 #[doc = $to_xe_bytes_doc]
3750 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();")]
3755 #[doc = concat!("assert_eq!(bytes, ", $be_bytes, ");")]
3756 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3758 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3759 #[must_use = "this returns the result of the operation, \
3760 without modifying the original"]
3761 #[inline]
3762 pub const fn to_be_bytes(self) -> [u8; size_of::<Self>()] {
3763 self.to_be().to_ne_bytes()
3764 }
3765
3766 #[doc = $to_xe_bytes_doc]
3770 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();")]
3775 #[doc = concat!("assert_eq!(bytes, ", $le_bytes, ");")]
3776 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3778 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3779 #[must_use = "this returns the result of the operation, \
3780 without modifying the original"]
3781 #[inline]
3782 pub const fn to_le_bytes(self) -> [u8; size_of::<Self>()] {
3783 self.to_le().to_ne_bytes()
3784 }
3785
3786 #[doc = $to_xe_bytes_doc]
3794 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_ne_bytes();")]
3802 #[doc = concat!(" ", $be_bytes)]
3806 #[doc = concat!(" ", $le_bytes)]
3808 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3812 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3813 #[allow(unnecessary_transmutes)]
3814 #[must_use = "this returns the result of the operation, \
3817 without modifying the original"]
3818 #[inline]
3819 pub const fn to_ne_bytes(self) -> [u8; size_of::<Self>()] {
3820 unsafe { mem::transmute(self) }
3823 }
3824
3825 #[doc = $from_xe_bytes_doc]
3829 #[doc = concat!("let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, ");")]
3834 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3835 #[doc = concat!("fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3841 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3842 #[doc = concat!(" ", stringify!($SelfT), "::from_be_bytes(int_bytes.try_into().unwrap())")]
3844 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3847 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3848 #[must_use]
3849 #[inline]
3850 pub const fn from_be_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3851 Self::from_be(Self::from_ne_bytes(bytes))
3852 }
3853
3854 #[doc = $from_xe_bytes_doc]
3858 #[doc = concat!("let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, ");")]
3863 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3864 #[doc = concat!("fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3870 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3871 #[doc = concat!(" ", stringify!($SelfT), "::from_le_bytes(int_bytes.try_into().unwrap())")]
3873 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3876 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3877 #[must_use]
3878 #[inline]
3879 pub const fn from_le_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3880 Self::from_le(Self::from_ne_bytes(bytes))
3881 }
3882
3883 #[doc = $from_xe_bytes_doc]
3894 #[doc = concat!("let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"big\") {")]
3899 #[doc = concat!(" ", $be_bytes)]
3900 #[doc = concat!(" ", $le_bytes)]
3902 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3904 #[doc = concat!("fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3910 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3911 #[doc = concat!(" ", stringify!($SelfT), "::from_ne_bytes(int_bytes.try_into().unwrap())")]
3913 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3916 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3917 #[allow(unnecessary_transmutes)]
3918 #[must_use]
3919 #[inline]
3922 pub const fn from_ne_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3923 unsafe { mem::transmute(bytes) }
3925 }
3926
3927 #[doc = concat!("[`", stringify!($SelfT), "::MIN", "`] instead.")]
3929 #[stable(feature = "rust1", since = "1.0.0")]
3932 #[inline(always)]
3933 #[rustc_promotable]
3934 #[rustc_const_stable(feature = "const_min_value", since = "1.32.0")]
3935 #[deprecated(since = "1.99.0", note = "replaced by the `MIN` associated constant on this type")]
3936 #[rustc_diagnostic_item = concat!(stringify!($SelfT), "_legacy_fn_min_value")]
3937 pub const fn min_value() -> Self {
3938 Self::MIN
3939 }
3940
3941 #[doc = concat!("[`", stringify!($SelfT), "::MAX", "`] instead.")]
3943 #[stable(feature = "rust1", since = "1.0.0")]
3946 #[inline(always)]
3947 #[rustc_promotable]
3948 #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
3949 #[deprecated(since = "1.99.0", note = "replaced by the `MAX` associated constant on this type")]
3950 #[rustc_diagnostic_item = concat!(stringify!($SelfT), "_legacy_fn_max_value")]
3951 pub const fn max_value() -> Self {
3952 Self::MAX
3953 }
3954
3955 #[doc = concat!("assert_eq!(120", stringify!($SelfT), ".clamp_magnitude(100), 100);")]
3967 #[doc = concat!("assert_eq!(-120", stringify!($SelfT), ".clamp_magnitude(100), -100);")]
3968 #[doc = concat!("assert_eq!(80", stringify!($SelfT), ".clamp_magnitude(100), 80);")]
3969 #[doc = concat!("assert_eq!(-80", stringify!($SelfT), ".clamp_magnitude(100), -80);")]
3970 #[must_use = "this returns the clamped value and does not modify the original"]
3972 #[unstable(feature = "clamp_magnitude", issue = "148519")]
3973 #[inline]
3974 pub fn clamp_magnitude(self, limit: $UnsignedT) -> Self {
3975 if let Ok(limit) = core::convert::TryInto::<$SelfT>::try_into(limit) {
3976 self.clamp(-limit, limit)
3977 } else {
3978 self
3979 }
3980 }
3981
3982 #[doc = concat!("assert_eq!(120i8, 120", stringify!($SelfT), ".truncate());")]
3990 #[doc = concat!("assert_eq!(-120i8, (-120", stringify!($SelfT), ").truncate());")]
3991 #[must_use = "this returns the truncated value and does not modify the original"]
3994 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
3995 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
3996 #[inline]
3997 pub const fn truncate<Target>(self) -> Target
3998 where Self: [const] traits::TruncateTarget<Target>
3999 {
4000 traits::TruncateTarget::internal_truncate(self)
4001 }
4002
4003 #[doc = concat!("assert_eq!(120i8, 120", stringify!($SelfT), ".saturating_truncate());")]
4011 #[doc = concat!("assert_eq!(-120i8, (-120", stringify!($SelfT), ").saturating_truncate());")]
4012 #[must_use = "this returns the truncated value and does not modify the original"]
4016 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
4017 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
4018 #[inline]
4019 pub const fn saturating_truncate<Target>(self) -> Target
4020 where Self: [const] traits::TruncateTarget<Target>
4021 {
4022 traits::TruncateTarget::internal_saturating_truncate(self)
4023 }
4024
4025 #[doc = concat!("assert_eq!(Some(120i8), 120", stringify!($SelfT), ".checked_truncate());")]
4033 #[doc = concat!("assert_eq!(Some(-120i8), (-120", stringify!($SelfT), ").checked_truncate());")]
4034 #[must_use = "this returns the truncated value and does not modify the original"]
4038 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
4039 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
4040 #[inline]
4041 pub const fn checked_truncate<Target>(self) -> Option<Target>
4042 where Self: [const] traits::TruncateTarget<Target>
4043 {
4044 traits::TruncateTarget::internal_checked_truncate(self)
4045 }
4046
4047 #[doc = concat!("assert_eq!(120i128, 120i8.widen());")]
4054 #[doc = concat!("assert_eq!(-120i128, (-120i8).widen());")]
4055 #[must_use = "this returns the widened value and does not modify the original"]
4057 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
4058 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
4059 #[inline]
4060 pub const fn widen<Target>(self) -> Target
4061 where Self: [const] traits::WidenTarget<Target>
4062 {
4063 traits::WidenTarget::internal_widen(self)
4064 }
4065
4066
4067 #[doc = concat!("assert_eq!(i8::MAX, ", stringify!($SelfT), "::MAX.saturating_cast());")]
4075 #[doc = concat!("assert_eq!(i8::MIN, ", stringify!($SelfT), "::MIN.saturating_cast());")]
4076 #[doc = concat!("assert_eq!(42u8, 42", stringify!($SelfT), ".saturating_cast());")]
4077 #[doc = concat!("assert_eq!(0u8, (-42", stringify!($SelfT), ").saturating_cast());")]
4078 #[must_use = "this returns the cast result and does not modify the original"]
4080 #[unstable(feature = "integer_casts", issue = "157388")]
4081 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4082 #[inline(always)]
4083 pub const fn saturating_cast<T: [const] BoundedCastFromInt<Self>>(self) -> T {
4084 T::saturating_cast_from(self)
4085 }
4086
4087 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX as i8, ", stringify!($SelfT), "::MAX.wrapping_cast());")]
4095 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN as i8, ", stringify!($SelfT), "::MIN.wrapping_cast());")]
4096 #[doc = concat!("assert_eq!(42u8, 42", stringify!($SelfT), ".wrapping_cast());")]
4097 #[doc = concat!("assert_eq!(u8::MAX - 41, (-42", stringify!($SelfT), ").wrapping_cast());")]
4098 #[must_use = "this returns the cast result and does not modify the original"]
4100 #[unstable(feature = "integer_casts", issue = "157388")]
4101 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4102 #[inline(always)]
4103 pub const fn wrapping_cast<T: [const] BoundedCastFromInt<Self>>(self) -> T {
4104 T::wrapping_cast_from(self)
4105 }
4106
4107 #[doc = concat!("assert_eq!(Some(42u8), 42", stringify!($SelfT), ".checked_cast());")]
4115 #[doc = concat!("assert_eq!((-42", stringify!($SelfT), ").checked_cast::<u8>(), None);")]
4116 #[must_use = "this returns the cast result and does not modify the original"]
4118 #[unstable(feature = "integer_casts", issue = "157388")]
4119 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4120 #[inline(always)]
4121 pub const fn checked_cast<T: [const] CheckedCastFromInt<Self>>(self) -> Option<T> {
4122 T::checked_cast_from(self)
4123 }
4124
4125 #[doc = concat!("assert_eq!(42u8, 42", stringify!($SelfT), ".strict_cast());")]
4137 #[doc = concat!("let _ = (-42", stringify!($SelfT), ").strict_cast::<u8>();")]
4144 #[must_use = "this returns the cast result and does not modify the original"]
4146 #[unstable(feature = "integer_casts", issue = "157388")]
4147 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4148 #[inline(always)]
4149 #[track_caller]
4150 pub const fn strict_cast<T: [const] CheckedCastFromInt<Self>>(self) -> T {
4151 T::strict_cast_from(self)
4152 }
4153
4154 #[must_use = "this returns the cast result and does not modify the original"]
4162 #[unstable(feature = "integer_casts", issue = "157388")]
4163 #[rustc_const_unstable(feature = "integer_casts", issue = "157388")]
4164 #[inline(always)]
4165 pub const unsafe fn unchecked_cast<T: [const] CheckedCastFromInt<Self>>(self) -> T {
4166 assert_unsafe_precondition!(
4167 check_language_ub,
4168 concat!(stringify!($SelfT), "::unchecked_cast must fit in the target type"),
4169 (
4170 in_bounds: bool = {
4172 let cast_val = self.checked_cast::<T>();
4173 let ret = cast_val.is_some();
4174 core::mem::forget(cast_val); ret
4176 },
4177 ) => in_bounds,
4178 );
4179
4180 unsafe { T::unchecked_cast_from(self) }
4182 }
4183 }
4184}