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);")]
217 #[doc = concat!("assert_eq!(0b1_", stringify!($SelfT), ".highest_one(), Some(0));")]
218 #[doc = concat!("assert_eq!(0b1_0000_", stringify!($SelfT), ".highest_one(), Some(4));")]
219 #[doc = concat!("assert_eq!(0b1_1111_", stringify!($SelfT), ".highest_one(), Some(4));")]
220 #[stable(feature = "int_lowest_highest_one", since = "1.97.0")]
222 #[rustc_const_stable(feature = "int_lowest_highest_one", since = "1.97.0")]
223 #[must_use = "this returns the result of the operation, \
224 without modifying the original"]
225 #[inline(always)]
226 pub const fn highest_one(self) -> Option<u32> {
227 (self as $UnsignedT).highest_one()
228 }
229
230 #[doc = concat!("assert_eq!(0b0_", stringify!($SelfT), ".lowest_one(), None);")]
237 #[doc = concat!("assert_eq!(0b1_", stringify!($SelfT), ".lowest_one(), Some(0));")]
238 #[doc = concat!("assert_eq!(0b1_0000_", stringify!($SelfT), ".lowest_one(), Some(4));")]
239 #[doc = concat!("assert_eq!(0b1_1111_", stringify!($SelfT), ".lowest_one(), Some(0));")]
240 #[stable(feature = "int_lowest_highest_one", since = "1.97.0")]
242 #[rustc_const_stable(feature = "int_lowest_highest_one", since = "1.97.0")]
243 #[must_use = "this returns the result of the operation, \
244 without modifying the original"]
245 #[inline(always)]
246 pub const fn lowest_one(self) -> Option<u32> {
247 (self as $UnsignedT).lowest_one()
248 }
249
250 #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
259 #[doc = concat!("assert_eq!(n.cast_unsigned(), ", stringify!($UnsignedT), "::MAX);")]
261 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
263 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
264 #[must_use = "this returns the result of the operation, \
265 without modifying the original"]
266 #[inline(always)]
267 pub const fn cast_unsigned(self) -> $UnsignedT {
268 self as $UnsignedT
269 }
270
271 #[doc = concat!("let n = ", stringify!($SelfT), "::MIN;")]
285 #[doc = concat!("assert_eq!(n.saturating_cast_unsigned(), 0", stringify!($UnsignedT), ");")]
287 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".saturating_cast_unsigned(), 64", stringify!($UnsignedT), ");")]
288 #[rustc_const_unstable(feature = "integer_cast_extras", issue = "154650")]
290 #[unstable(feature = "integer_cast_extras", issue = "154650")]
291 #[must_use = "this returns the result of the operation, \
292 without modifying the original"]
293 #[inline(always)]
294 pub const fn saturating_cast_unsigned(self) -> $UnsignedT {
295 if self >= 0 {
296 self.cast_unsigned()
297 } else {
298 0
299 }
300 }
301
302 #[doc = concat!("let n = ", stringify!($SelfT), "::MIN;")]
315 #[doc = concat!("assert_eq!(n.checked_cast_unsigned(), None);")]
317 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".checked_cast_unsigned(), Some(64", stringify!($UnsignedT), "));")]
318 #[rustc_const_unstable(feature = "integer_cast_extras", issue = "154650")]
320 #[unstable(feature = "integer_cast_extras", issue = "154650")]
321 #[must_use = "this returns the result of the operation, \
322 without modifying the original"]
323 #[inline(always)]
324 pub const fn checked_cast_unsigned(self) -> Option<$UnsignedT> {
325 if self >= 0 {
326 Some(self.cast_unsigned())
327 } else {
328 None
329 }
330 }
331
332 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_cast_unsigned();")]
345 #[rustc_const_unstable(feature = "integer_cast_extras", issue = "154650")]
347 #[unstable(feature = "integer_cast_extras", issue = "154650")]
348 #[must_use = "this returns the result of the operation, \
349 without modifying the original"]
350 #[inline]
351 #[track_caller]
352 pub const fn strict_cast_unsigned(self) -> $UnsignedT {
353 match self.checked_cast_unsigned() {
354 Some(n) => n,
355 None => imp::overflow_panic::cast_integer(),
356 }
357 }
358
359 #[doc = concat!("let n = ", $rot_op, stringify!($SelfT), ";")]
372 #[doc = concat!("let m = ", $rot_result, ";")]
373 #[doc = concat!("assert_eq!(n.rotate_left(", $rot, "), m);")]
375 #[doc = concat!("assert_eq!(n.rotate_left(1024), n);")]
376 #[stable(feature = "rust1", since = "1.0.0")]
378 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
379 #[must_use = "this returns the result of the operation, \
380 without modifying the original"]
381 #[inline(always)]
382 pub const fn rotate_left(self, n: u32) -> Self {
383 (self as $UnsignedT).rotate_left(n) as Self
384 }
385
386 #[doc = concat!("let n = ", $rot_result, stringify!($SelfT), ";")]
400 #[doc = concat!("let m = ", $rot_op, ";")]
401 #[doc = concat!("assert_eq!(n.rotate_right(", $rot, "), m);")]
403 #[doc = concat!("assert_eq!(n.rotate_right(1024), n);")]
404 #[stable(feature = "rust1", since = "1.0.0")]
406 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
407 #[must_use = "this returns the result of the operation, \
408 without modifying the original"]
409 #[inline(always)]
410 pub const fn rotate_right(self, n: u32) -> Self {
411 (self as $UnsignedT).rotate_right(n) as Self
412 }
413
414 #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
420 #[doc = concat!("assert_eq!(m, ", $swapped, ");")]
424 #[stable(feature = "rust1", since = "1.0.0")]
426 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
427 #[must_use = "this returns the result of the operation, \
428 without modifying the original"]
429 #[inline(always)]
430 pub const fn swap_bytes(self) -> Self {
431 (self as $UnsignedT).swap_bytes() as Self
432 }
433
434 #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
441 #[doc = concat!("assert_eq!(m, ", $reversed, ");")]
444 #[doc = concat!("assert_eq!(0, 0", stringify!($SelfT), ".reverse_bits());")]
445 #[stable(feature = "reverse_bits", since = "1.37.0")]
447 #[rustc_const_stable(feature = "reverse_bits", since = "1.37.0")]
448 #[must_use = "this returns the result of the operation, \
449 without modifying the original"]
450 #[inline(always)]
451 pub const fn reverse_bits(self) -> Self {
452 (self as $UnsignedT).reverse_bits() as Self
453 }
454
455 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
465 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_be(n), n)")]
468 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_be(n), n.swap_bytes())")]
470 #[stable(feature = "rust1", since = "1.0.0")]
473 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
474 #[must_use]
475 #[inline]
476 pub const fn from_be(x: Self) -> Self {
477 cfg_select! {
478 target_endian = "big" => x,
479 _ => x.swap_bytes(),
480 }
481 }
482
483 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
493 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_le(n), n)")]
496 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_le(n), n.swap_bytes())")]
498 #[stable(feature = "rust1", since = "1.0.0")]
501 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
502 #[must_use]
503 #[inline]
504 pub const fn from_le(x: Self) -> Self {
505 cfg_select! {
506 target_endian = "little" => x,
507 _ => x.swap_bytes(),
508 }
509 }
510
511 #[doc = concat!("`", stringify!($SelfT), "`.")]
518 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
525 #[stable(feature = "rust1", since = "1.0.0")]
533 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
534 #[must_use = "this returns the result of the operation, \
535 without modifying the original"]
536 #[inline]
537 pub const fn to_be(self) -> Self { cfg_select! {
539 target_endian = "big" => self,
540 _ => self.swap_bytes(),
541 }
542 }
543
544 #[doc = concat!("`", stringify!($SelfT), "`.")]
551 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
558 #[stable(feature = "rust1", since = "1.0.0")]
566 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
567 #[must_use = "this returns the result of the operation, \
568 without modifying the original"]
569 #[inline]
570 pub const fn to_le(self) -> Self {
571 cfg_select! {
572 target_endian = "little" => self,
573 _ => self.swap_bytes(),
574 }
575 }
576
577 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(1), Some(", stringify!($SelfT), "::MAX - 1));")]
584 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);")]
585 #[stable(feature = "rust1", since = "1.0.0")]
587 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
588 #[must_use = "this returns the result of the operation, \
589 without modifying the original"]
590 #[inline]
591 pub const fn checked_add(self, rhs: Self) -> Option<Self> {
592 let (a, b) = self.overflowing_add(rhs);
593 if intrinsics::unlikely(b) { None } else { Some(a) }
594 }
595
596 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).strict_add(1), ", stringify!($SelfT), "::MAX - 1);")]
609 #[doc = concat!("let _ = (", stringify!($SelfT), "::MAX - 2).strict_add(3);")]
615 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
617 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
618 #[must_use = "this returns the result of the operation, \
619 without modifying the original"]
620 #[inline]
621 #[track_caller]
622 pub const fn strict_add(self, rhs: Self) -> Self {
623 let (a, b) = self.overflowing_add(rhs);
624 if b { imp::overflow_panic::add() } else { a }
625 }
626
627 #[doc = concat!("`self + rhs > ", stringify!($SelfT), "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`,")]
640 #[doc = concat!("[`checked_add`]: ", stringify!($SelfT), "::checked_add")]
644 #[doc = concat!("[`wrapping_add`]: ", stringify!($SelfT), "::wrapping_add")]
645 #[stable(feature = "unchecked_math", since = "1.79.0")]
646 #[rustc_const_stable(feature = "unchecked_math", since = "1.79.0")]
647 #[must_use = "this returns the result of the operation, \
648 without modifying the original"]
649 #[inline(always)]
650 #[track_caller]
651 pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
652 assert_unsafe_precondition!(
653 check_language_ub,
654 concat!(stringify!($SelfT), "::unchecked_add cannot overflow"),
655 (
656 lhs: $SelfT = self,
657 rhs: $SelfT = rhs,
658 ) => !lhs.overflowing_add(rhs).1,
659 );
660
661 unsafe {
663 intrinsics::unchecked_add(self, rhs)
664 }
665 }
666
667 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_add_unsigned(2), Some(3));")]
674 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add_unsigned(3), None);")]
675 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
677 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
678 #[must_use = "this returns the result of the operation, \
679 without modifying the original"]
680 #[inline]
681 pub const fn checked_add_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
682 let (a, b) = self.overflowing_add_unsigned(rhs);
683 if intrinsics::unlikely(b) { None } else { Some(a) }
684 }
685
686 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".strict_add_unsigned(2), 3);")]
699 #[doc = concat!("let _ = (", stringify!($SelfT), "::MAX - 2).strict_add_unsigned(3);")]
705 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
707 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
708 #[must_use = "this returns the result of the operation, \
709 without modifying the original"]
710 #[inline]
711 #[track_caller]
712 pub const fn strict_add_unsigned(self, rhs: $UnsignedT) -> Self {
713 let (a, b) = self.overflowing_add_unsigned(rhs);
714 if b { imp::overflow_panic::add() } else { a }
715 }
716
717 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub(1), Some(", stringify!($SelfT), "::MIN + 1));")]
724 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub(3), None);")]
725 #[stable(feature = "rust1", since = "1.0.0")]
727 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
728 #[must_use = "this returns the result of the operation, \
729 without modifying the original"]
730 #[inline]
731 pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
732 let (a, b) = self.overflowing_sub(rhs);
733 if intrinsics::unlikely(b) { None } else { Some(a) }
734 }
735
736 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).strict_sub(1), ", stringify!($SelfT), "::MIN + 1);")]
749 #[doc = concat!("let _ = (", stringify!($SelfT), "::MIN + 2).strict_sub(3);")]
755 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
757 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
758 #[must_use = "this returns the result of the operation, \
759 without modifying the original"]
760 #[inline]
761 #[track_caller]
762 pub const fn strict_sub(self, rhs: Self) -> Self {
763 let (a, b) = self.overflowing_sub(rhs);
764 if b { imp::overflow_panic::sub() } else { a }
765 }
766
767 #[doc = concat!("`self - rhs > ", stringify!($SelfT), "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`,")]
780 #[doc = concat!("[`checked_sub`]: ", stringify!($SelfT), "::checked_sub")]
784 #[doc = concat!("[`wrapping_sub`]: ", stringify!($SelfT), "::wrapping_sub")]
785 #[stable(feature = "unchecked_math", since = "1.79.0")]
786 #[rustc_const_stable(feature = "unchecked_math", since = "1.79.0")]
787 #[must_use = "this returns the result of the operation, \
788 without modifying the original"]
789 #[inline(always)]
790 #[track_caller]
791 pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
792 assert_unsafe_precondition!(
793 check_language_ub,
794 concat!(stringify!($SelfT), "::unchecked_sub cannot overflow"),
795 (
796 lhs: $SelfT = self,
797 rhs: $SelfT = rhs,
798 ) => !lhs.overflowing_sub(rhs).1,
799 );
800
801 unsafe {
803 intrinsics::unchecked_sub(self, rhs)
804 }
805 }
806
807 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_sub_unsigned(2), Some(-1));")]
814 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub_unsigned(3), None);")]
815 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
817 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
818 #[must_use = "this returns the result of the operation, \
819 without modifying the original"]
820 #[inline]
821 pub const fn checked_sub_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
822 let (a, b) = self.overflowing_sub_unsigned(rhs);
823 if intrinsics::unlikely(b) { None } else { Some(a) }
824 }
825
826 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".strict_sub_unsigned(2), -1);")]
839 #[doc = concat!("let _ = (", stringify!($SelfT), "::MIN + 2).strict_sub_unsigned(3);")]
845 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
847 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
848 #[must_use = "this returns the result of the operation, \
849 without modifying the original"]
850 #[inline]
851 #[track_caller]
852 pub const fn strict_sub_unsigned(self, rhs: $UnsignedT) -> Self {
853 let (a, b) = self.overflowing_sub_unsigned(rhs);
854 if b { imp::overflow_panic::sub() } else { a }
855 }
856
857 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(1), Some(", stringify!($SelfT), "::MAX));")]
864 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);")]
865 #[stable(feature = "rust1", since = "1.0.0")]
867 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
868 #[must_use = "this returns the result of the operation, \
869 without modifying the original"]
870 #[inline]
871 pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
872 let (a, b) = self.overflowing_mul(rhs);
873 if intrinsics::unlikely(b) { None } else { Some(a) }
874 }
875
876 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.strict_mul(1), ", stringify!($SelfT), "::MAX);")]
889 #[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_mul(2);")]
895 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
897 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
898 #[must_use = "this returns the result of the operation, \
899 without modifying the original"]
900 #[inline]
901 #[track_caller]
902 pub const fn strict_mul(self, rhs: Self) -> Self {
903 let (a, b) = self.overflowing_mul(rhs);
904 if b { imp::overflow_panic::mul() } else { a }
905 }
906
907 #[doc = concat!("`self * rhs > ", stringify!($SelfT), "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`,")]
920 #[doc = concat!("[`checked_mul`]: ", stringify!($SelfT), "::checked_mul")]
924 #[doc = concat!("[`wrapping_mul`]: ", stringify!($SelfT), "::wrapping_mul")]
925 #[stable(feature = "unchecked_math", since = "1.79.0")]
926 #[rustc_const_stable(feature = "unchecked_math", since = "1.79.0")]
927 #[must_use = "this returns the result of the operation, \
928 without modifying the original"]
929 #[inline(always)]
930 #[track_caller]
931 pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
932 assert_unsafe_precondition!(
933 check_language_ub,
934 concat!(stringify!($SelfT), "::unchecked_mul cannot overflow"),
935 (
936 lhs: $SelfT = self,
937 rhs: $SelfT = rhs,
938 ) => !lhs.overflowing_mul(rhs).1,
939 );
940
941 unsafe {
943 intrinsics::unchecked_mul(self, rhs)
944 }
945 }
946
947 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div(-1), Some(", stringify!($Max), "));")]
954 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div(-1), None);")]
955 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div(0), None);")]
956 #[stable(feature = "rust1", since = "1.0.0")]
958 #[rustc_const_stable(feature = "const_checked_int_div", since = "1.52.0")]
959 #[must_use = "this returns the result of the operation, \
960 without modifying the original"]
961 #[inline]
962 pub const fn checked_div(self, rhs: Self) -> Option<Self> {
963 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
964 None
965 } else {
966 Some(unsafe { intrinsics::unchecked_div(self, rhs) })
968 }
969 }
970
971 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).strict_div(-1), ", stringify!($Max), ");")]
990 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_div(-1);")]
996 #[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div(0);")]
1002 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1004 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1005 #[must_use = "this returns the result of the operation, \
1006 without modifying the original"]
1007 #[inline]
1008 #[track_caller]
1009 pub const fn strict_div(self, rhs: Self) -> Self {
1010 let (a, b) = self.overflowing_div(rhs);
1011 if b { imp::overflow_panic::div() } else { a }
1012 }
1013
1014 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div_euclid(-1), Some(", stringify!($Max), "));")]
1021 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div_euclid(-1), None);")]
1022 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div_euclid(0), None);")]
1023 #[stable(feature = "euclidean_division", since = "1.38.0")]
1025 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1026 #[must_use = "this returns the result of the operation, \
1027 without modifying the original"]
1028 #[inline]
1029 pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
1030 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
1032 None
1033 } else {
1034 Some(self.div_euclid(rhs))
1035 }
1036 }
1037
1038 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).strict_div_euclid(-1), ", stringify!($Max), ");")]
1057 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_div_euclid(-1);")]
1063 #[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div_euclid(0);")]
1069 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1071 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1072 #[must_use = "this returns the result of the operation, \
1073 without modifying the original"]
1074 #[inline]
1075 #[track_caller]
1076 pub const fn strict_div_euclid(self, rhs: Self) -> Self {
1077 let (a, b) = self.overflowing_div_euclid(rhs);
1078 if b { imp::overflow_panic::div() } else { a }
1079 }
1080
1081 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div_exact(-1), Some(", stringify!($Max), "));")]
1090 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_div_exact(2), None);")]
1091 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div_exact(-1), None);")]
1092 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div_exact(0), None);")]
1093 #[unstable(
1095 feature = "exact_div",
1096 issue = "139911",
1097 )]
1098 #[must_use = "this returns the result of the operation, \
1099 without modifying the original"]
1100 #[inline]
1101 pub const fn checked_div_exact(self, rhs: Self) -> Option<Self> {
1102 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
1103 None
1104 } else {
1105 unsafe {
1107 if intrinsics::unlikely(intrinsics::unchecked_rem(self, rhs) != 0) {
1108 None
1109 } else {
1110 Some(intrinsics::exact_div(self, rhs))
1111 }
1112 }
1113 }
1114 }
1115
1116 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".div_exact(2), Some(32));")]
1132 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".div_exact(32), Some(2));")]
1133 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).div_exact(-1), Some(", stringify!($Max), "));")]
1134 #[doc = concat!("assert_eq!(65", stringify!($SelfT), ".div_exact(2), None);")]
1135 #[doc = concat!("let _ = 64", stringify!($SelfT),".div_exact(0);")]
1139 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.div_exact(-1);")]
1143 #[unstable(
1145 feature = "exact_div",
1146 issue = "139911",
1147 )]
1148 #[must_use = "this returns the result of the operation, \
1149 without modifying the original"]
1150 #[inline]
1151 #[rustc_inherit_overflow_checks]
1152 pub const fn div_exact(self, rhs: Self) -> Option<Self> {
1153 if self % rhs != 0 {
1154 None
1155 } else {
1156 Some(self / rhs)
1157 }
1158 }
1159
1160 #[doc = concat!("`self == ", stringify!($SelfT), "::MIN && rhs == -1`,")]
1166 #[unstable(
1168 feature = "exact_div",
1169 issue = "139911",
1170 )]
1171 #[must_use = "this returns the result of the operation, \
1172 without modifying the original"]
1173 #[inline]
1174 pub const unsafe fn unchecked_div_exact(self, rhs: Self) -> Self {
1175 assert_unsafe_precondition!(
1176 check_language_ub,
1177 concat!(stringify!($SelfT), "::unchecked_div_exact cannot overflow, divide by zero, or leave a remainder"),
1178 (
1179 lhs: $SelfT = self,
1180 rhs: $SelfT = rhs,
1181 ) => rhs > 0 && lhs % rhs == 0 && (lhs != <$SelfT>::MIN || rhs != -1),
1182 );
1183 unsafe { intrinsics::exact_div(self, rhs) }
1185 }
1186
1187 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(2), Some(1));")]
1194 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(0), None);")]
1195 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem(-1), None);")]
1196 #[stable(feature = "wrapping", since = "1.7.0")]
1198 #[rustc_const_stable(feature = "const_checked_int_div", since = "1.52.0")]
1199 #[must_use = "this returns the result of the operation, \
1200 without modifying the original"]
1201 #[inline]
1202 pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
1203 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
1204 None
1205 } else {
1206 Some(unsafe { intrinsics::unchecked_rem(self, rhs) })
1208 }
1209 }
1210
1211 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_rem(2), 1);")]
1229 #[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem(0);")]
1235 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_rem(-1);")]
1241 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1243 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1244 #[must_use = "this returns the result of the operation, \
1245 without modifying the original"]
1246 #[inline]
1247 #[track_caller]
1248 pub const fn strict_rem(self, rhs: Self) -> Self {
1249 let (a, b) = self.overflowing_rem(rhs);
1250 if b { imp::overflow_panic::rem() } else { a }
1251 }
1252
1253 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(2), Some(1));")]
1260 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(0), None);")]
1261 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem_euclid(-1), None);")]
1262 #[stable(feature = "euclidean_division", since = "1.38.0")]
1264 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1265 #[must_use = "this returns the result of the operation, \
1266 without modifying the original"]
1267 #[inline]
1268 pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
1269 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
1271 None
1272 } else {
1273 Some(self.rem_euclid(rhs))
1274 }
1275 }
1276
1277 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_rem_euclid(2), 1);")]
1295 #[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem_euclid(0);")]
1301 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_rem_euclid(-1);")]
1307 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1309 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1310 #[must_use = "this returns the result of the operation, \
1311 without modifying the original"]
1312 #[inline]
1313 #[track_caller]
1314 pub const fn strict_rem_euclid(self, rhs: Self) -> Self {
1315 let (a, b) = self.overflowing_rem_euclid(rhs);
1316 if b { imp::overflow_panic::rem() } else { a }
1317 }
1318
1319 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_neg(), Some(-5));")]
1325 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_neg(), None);")]
1326 #[stable(feature = "wrapping", since = "1.7.0")]
1328 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1329 #[must_use = "this returns the result of the operation, \
1330 without modifying the original"]
1331 #[inline]
1332 pub const fn checked_neg(self) -> Option<Self> {
1333 let (a, b) = self.overflowing_neg();
1334 if intrinsics::unlikely(b) { None } else { Some(a) }
1335 }
1336
1337 #[doc = concat!("`self == ", stringify!($SelfT), "::MIN`,")]
1343 #[doc = concat!("[`checked_neg`]: ", stringify!($SelfT), "::checked_neg")]
1346 #[stable(feature = "unchecked_neg", since = "1.93.0")]
1347 #[rustc_const_stable(feature = "unchecked_neg", since = "1.93.0")]
1348 #[must_use = "this returns the result of the operation, \
1349 without modifying the original"]
1350 #[inline(always)]
1351 #[track_caller]
1352 pub const unsafe fn unchecked_neg(self) -> Self {
1353 assert_unsafe_precondition!(
1354 check_language_ub,
1355 concat!(stringify!($SelfT), "::unchecked_neg cannot overflow"),
1356 (
1357 lhs: $SelfT = self,
1358 ) => !lhs.overflowing_neg().1,
1359 );
1360
1361 unsafe {
1363 intrinsics::unchecked_sub(0, self)
1364 }
1365 }
1366
1367 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_neg(), -5);")]
1379 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_neg();")]
1385 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1387 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1388 #[must_use = "this returns the result of the operation, \
1389 without modifying the original"]
1390 #[inline]
1391 #[track_caller]
1392 pub const fn strict_neg(self) -> Self {
1393 let (a, b) = self.overflowing_neg();
1394 if b { imp::overflow_panic::neg() } else { a }
1395 }
1396
1397 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(4), Some(0x10));")]
1404 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(129), None);")]
1405 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shl(", stringify!($BITS_MINUS_ONE), "), Some(0));")]
1406 #[stable(feature = "wrapping", since = "1.7.0")]
1408 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1409 #[must_use = "this returns the result of the operation, \
1410 without modifying the original"]
1411 #[inline]
1412 pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
1413 if rhs < Self::BITS {
1415 Some(unsafe { self.unchecked_shl(rhs) })
1417 } else {
1418 None
1419 }
1420 }
1421
1422 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".strict_shl(4), 0x10);")]
1435 #[doc = concat!("let _ = 0x1", stringify!($SelfT), ".strict_shl(129);")]
1441 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1443 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1444 #[must_use = "this returns the result of the operation, \
1445 without modifying the original"]
1446 #[inline]
1447 #[track_caller]
1448 pub const fn strict_shl(self, rhs: u32) -> Self {
1449 let (a, b) = self.overflowing_shl(rhs);
1450 if b { imp::overflow_panic::shl() } else { a }
1451 }
1452
1453 #[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
1463 #[stable(feature = "unchecked_shifts", since = "1.93.0")]
1464 #[rustc_const_stable(feature = "unchecked_shifts", since = "1.93.0")]
1465 #[must_use = "this returns the result of the operation, \
1466 without modifying the original"]
1467 #[inline(always)]
1468 #[track_caller]
1469 pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
1470 assert_unsafe_precondition!(
1471 check_language_ub,
1472 concat!(stringify!($SelfT), "::unchecked_shl cannot overflow"),
1473 (
1474 rhs: u32 = rhs,
1475 ) => rhs < <$ActualT>::BITS,
1476 );
1477
1478 unsafe {
1480 intrinsics::unchecked_shl(self, rhs)
1481 }
1482 }
1483
1484 #[doc = concat!("assert_eq!(0x1_", stringify!($SelfT), ".unbounded_shl(4), 0x10);")]
1493 #[doc = concat!("assert_eq!(0x1_", stringify!($SelfT), ".unbounded_shl(129), 0);")]
1494 #[doc = concat!("assert_eq!(0b101_", stringify!($SelfT), ".unbounded_shl(0), 0b101);")]
1495 #[doc = concat!("assert_eq!(0b101_", stringify!($SelfT), ".unbounded_shl(1), 0b1010);")]
1496 #[doc = concat!("assert_eq!(0b101_", stringify!($SelfT), ".unbounded_shl(2), 0b10100);")]
1497 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shl(", stringify!($BITS), "), 0);")]
1498 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shl(1).unbounded_shl(", stringify!($BITS_MINUS_ONE), "), 0);")]
1499 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shl(", stringify!($BITS), "), 0);")]
1500 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shl(1).unbounded_shl(", stringify!($BITS_MINUS_ONE), "), 0);")]
1501 #[stable(feature = "unbounded_shifts", since = "1.87.0")]
1503 #[rustc_const_stable(feature = "unbounded_shifts", since = "1.87.0")]
1504 #[must_use = "this returns the result of the operation, \
1505 without modifying the original"]
1506 #[inline]
1507 pub const fn unbounded_shl(self, rhs: u32) -> $SelfT{
1508 if rhs < Self::BITS {
1509 unsafe { self.unchecked_shl(rhs) }
1512 } else {
1513 0
1514 }
1515 }
1516
1517 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1522 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".shl_exact(4), Some(0x10));")]
1530 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".shl_exact(", stringify!($SelfT), "::BITS - 2), Some(1 << ", stringify!($SelfT), "::BITS - 2));")]
1531 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".shl_exact(", stringify!($SelfT), "::BITS - 1), None);")]
1532 #[doc = concat!("assert_eq!((-0x2", stringify!($SelfT), ").shl_exact(", stringify!($SelfT), "::BITS - 2), Some(-0x2 << ", stringify!($SelfT), "::BITS - 2));")]
1533 #[doc = concat!("assert_eq!((-0x2", stringify!($SelfT), ").shl_exact(", stringify!($SelfT), "::BITS - 1), None);")]
1534 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1536 #[must_use = "this returns the result of the operation, \
1537 without modifying the original"]
1538 #[inline]
1539 pub const fn shl_exact(self, rhs: u32) -> Option<$SelfT> {
1540 if rhs < self.leading_zeros() || rhs < self.leading_ones() {
1541 Some(unsafe { self.unchecked_shl(rhs) })
1543 } else {
1544 None
1545 }
1546 }
1547
1548 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1551 #[doc = concat!("[`", stringify!($SelfT), "::shl_exact`]")]
1557 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1559 #[must_use = "this returns the result of the operation, \
1560 without modifying the original"]
1561 #[inline]
1562 pub const unsafe fn unchecked_shl_exact(self, rhs: u32) -> $SelfT {
1563 assert_unsafe_precondition!(
1564 check_library_ub,
1565 concat!(stringify!($SelfT), "::unchecked_shl_exact cannot shift out bits that would change the value of the first bit"),
1566 (
1567 zeros: u32 = self.leading_zeros(),
1568 ones: u32 = self.leading_ones(),
1569 rhs: u32 = rhs,
1570 ) => rhs < zeros || rhs < ones,
1571 );
1572
1573 unsafe { self.unchecked_shl(rhs) }
1575 }
1576
1577 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(4), Some(0x1));")]
1584 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(128), None);")]
1585 #[stable(feature = "wrapping", since = "1.7.0")]
1587 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1588 #[must_use = "this returns the result of the operation, \
1589 without modifying the original"]
1590 #[inline]
1591 pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
1592 if rhs < Self::BITS {
1594 Some(unsafe { self.unchecked_shr(rhs) })
1596 } else {
1597 None
1598 }
1599 }
1600
1601 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".strict_shr(4), 0x1);")]
1614 #[doc = concat!("let _ = 0x10", stringify!($SelfT), ".strict_shr(128);")]
1620 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1622 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1623 #[must_use = "this returns the result of the operation, \
1624 without modifying the original"]
1625 #[inline]
1626 #[track_caller]
1627 pub const fn strict_shr(self, rhs: u32) -> Self {
1628 let (a, b) = self.overflowing_shr(rhs);
1629 if b { imp::overflow_panic::shr() } else { a }
1630 }
1631
1632 #[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
1642 #[stable(feature = "unchecked_shifts", since = "1.93.0")]
1643 #[rustc_const_stable(feature = "unchecked_shifts", since = "1.93.0")]
1644 #[must_use = "this returns the result of the operation, \
1645 without modifying the original"]
1646 #[inline(always)]
1647 #[track_caller]
1648 pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
1649 assert_unsafe_precondition!(
1650 check_language_ub,
1651 concat!(stringify!($SelfT), "::unchecked_shr cannot overflow"),
1652 (
1653 rhs: u32 = rhs,
1654 ) => rhs < <$ActualT>::BITS,
1655 );
1656
1657 unsafe {
1659 intrinsics::unchecked_shr(self, rhs)
1660 }
1661 }
1662
1663 #[doc = concat!("assert_eq!(0x10_", stringify!($SelfT), ".unbounded_shr(4), 0x1);")]
1673 #[doc = concat!("assert_eq!(0x10_", stringify!($SelfT), ".unbounded_shr(129), 0);")]
1674 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.unbounded_shr(129), -1);")]
1675 #[doc = concat!("assert_eq!(0b1010_", stringify!($SelfT), ".unbounded_shr(0), 0b1010);")]
1676 #[doc = concat!("assert_eq!(0b1010_", stringify!($SelfT), ".unbounded_shr(1), 0b101);")]
1677 #[doc = concat!("assert_eq!(0b1010_", stringify!($SelfT), ".unbounded_shr(2), 0b10);")]
1678 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shr(", stringify!($BITS), "), 0);")]
1679 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".unbounded_shr(1).unbounded_shr(", stringify!($BITS_MINUS_ONE), "), 0);")]
1680 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shr(", stringify!($BITS), "), -1);")]
1681 #[doc = concat!("assert_eq!((-13_", stringify!($SelfT), ").unbounded_shr(1).unbounded_shr(", stringify!($BITS_MINUS_ONE), "), -1);")]
1682 #[stable(feature = "unbounded_shifts", since = "1.87.0")]
1684 #[rustc_const_stable(feature = "unbounded_shifts", since = "1.87.0")]
1685 #[must_use = "this returns the result of the operation, \
1686 without modifying the original"]
1687 #[inline]
1688 pub const fn unbounded_shr(self, rhs: u32) -> $SelfT{
1689 if rhs < Self::BITS {
1690 unsafe { self.unchecked_shr(rhs) }
1693 } else {
1694 unsafe { self.unchecked_shr(Self::BITS - 1) }
1699 }
1700 }
1701
1702 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1706 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".shr_exact(4), Some(0x1));")]
1714 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".shr_exact(5), None);")]
1715 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1717 #[must_use = "this returns the result of the operation, \
1718 without modifying the original"]
1719 #[inline]
1720 pub const fn shr_exact(self, rhs: u32) -> Option<$SelfT> {
1721 if rhs <= self.trailing_zeros() && rhs < <$SelfT>::BITS {
1722 Some(unsafe { self.unchecked_shr(rhs) })
1724 } else {
1725 None
1726 }
1727 }
1728
1729 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1732 #[doc = concat!(stringify!($SelfT), "::BITS`")]
1737 #[doc = concat!("[`", stringify!($SelfT), "::shr_exact`]")]
1739 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1741 #[must_use = "this returns the result of the operation, \
1742 without modifying the original"]
1743 #[inline]
1744 pub const unsafe fn unchecked_shr_exact(self, rhs: u32) -> $SelfT {
1745 assert_unsafe_precondition!(
1746 check_library_ub,
1747 concat!(stringify!($SelfT), "::unchecked_shr_exact cannot shift out non-zero bits"),
1748 (
1749 zeros: u32 = self.trailing_zeros(),
1750 bits: u32 = <$SelfT>::BITS,
1751 rhs: u32 = rhs,
1752 ) => rhs <= zeros && rhs < bits,
1753 );
1754
1755 unsafe { self.unchecked_shr(rhs) }
1757 }
1758
1759 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_abs(), Some(5));")]
1766 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_abs(), None);")]
1767 #[stable(feature = "no_panic_abs", since = "1.13.0")]
1769 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1770 #[must_use = "this returns the result of the operation, \
1771 without modifying the original"]
1772 #[inline]
1773 pub const fn checked_abs(self) -> Option<Self> {
1774 if self.is_negative() {
1775 self.checked_neg()
1776 } else {
1777 Some(self)
1778 }
1779 }
1780
1781 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").strict_abs(), 5);")]
1794 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_abs();")]
1800 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1802 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1803 #[must_use = "this returns the result of the operation, \
1804 without modifying the original"]
1805 #[inline]
1806 #[track_caller]
1807 pub const fn strict_abs(self) -> Self {
1808 if self.is_negative() {
1809 self.strict_neg()
1810 } else {
1811 self
1812 }
1813 }
1814
1815 #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".checked_pow(2), Some(64));")]
1822 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".checked_pow(0), Some(1));")]
1823 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);")]
1824 #[stable(feature = "no_panic_pow", since = "1.34.0")]
1827 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1828 #[must_use = "this returns the result of the operation, \
1829 without modifying the original"]
1830 #[inline]
1831 pub const fn checked_pow(self, mut exp: u32) -> Option<Self> {
1832 let mut base = self;
1833 let mut acc: Self = 1;
1834
1835 if intrinsics::is_val_statically_known(base) && base.unsigned_abs().is_power_of_two() {
1836 let k = base.unsigned_abs().ilog2();
1837 let shift = try_opt!(k.checked_mul(exp));
1838 return if base < 0 && (exp % 2) == 1 {
1839 (-1 as Self).shl_exact(shift)
1840 } else {
1841 (1 as Self).shl_exact(shift)
1842 }
1843 }
1844
1845 if exp == 0 {
1846 return Some(1);
1847 }
1848
1849 if intrinsics::is_val_statically_known(exp) {
1850 while exp > 1 {
1851 if (exp & 1) == 1 {
1852 acc = try_opt!(acc.checked_mul(base));
1853 }
1854 exp /= 2;
1855 base = try_opt!(base.checked_mul(base));
1856 }
1857
1858 return acc.checked_mul(base);
1863 }
1864
1865 loop {
1866 if (exp & 1) == 1 {
1867 acc = try_opt!(acc.checked_mul(base));
1868 if exp == 1 {
1870 return Some(acc);
1871 }
1872 }
1873 exp /= 2;
1874 base = try_opt!(base.checked_mul(base));
1875 }
1876 }
1877
1878 #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".strict_pow(2), 64);")]
1891 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".strict_pow(0), 1);")]
1892 #[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_pow(2);")]
1898 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1900 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1901 #[must_use = "this returns the result of the operation, \
1902 without modifying the original"]
1903 #[inline]
1904 #[track_caller]
1905 pub const fn strict_pow(self, exp: u32) -> Self {
1906 match self.checked_pow(exp) {
1907 Some(x) => x,
1908 None => imp::overflow_panic::pow(),
1909 }
1910 }
1911
1912 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_isqrt(), Some(3));")]
1924 #[stable(feature = "isqrt", since = "1.84.0")]
1926 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
1927 #[must_use = "this returns the result of the operation, \
1928 without modifying the original"]
1929 #[inline]
1930 pub const fn checked_isqrt(self) -> Option<Self> {
1931 if self < 0 {
1932 None
1933 } else {
1934 let result = self.cast_unsigned().isqrt().cast_signed();
1937
1938 unsafe {
1950 const MAX_RESULT: $SelfT = <$SelfT>::MAX.cast_unsigned().isqrt().cast_signed();
1951 crate::hint::assert_unchecked(result <= MAX_RESULT);
1952 }
1953 Some(result)
1954 }
1955 }
1956
1957 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_add(1), 101);")]
1964 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add(100), ", stringify!($SelfT), "::MAX);")]
1965 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_add(-1), ", stringify!($SelfT), "::MIN);")]
1966 #[stable(feature = "rust1", since = "1.0.0")]
1969 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1970 #[must_use = "this returns the result of the operation, \
1971 without modifying the original"]
1972 #[inline(always)]
1973 pub const fn saturating_add(self, rhs: Self) -> Self {
1974 intrinsics::saturating_add(self, rhs)
1975 }
1976
1977 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".saturating_add_unsigned(2), 3);")]
1984 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add_unsigned(100), ", stringify!($SelfT), "::MAX);")]
1985 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1987 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1988 #[must_use = "this returns the result of the operation, \
1989 without modifying the original"]
1990 #[inline]
1991 pub const fn saturating_add_unsigned(self, rhs: $UnsignedT) -> Self {
1992 match self.checked_add_unsigned(rhs) {
1995 Some(x) => x,
1996 None => Self::MAX,
1997 }
1998 }
1999
2000 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub(127), -27);")]
2007 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub(100), ", stringify!($SelfT), "::MIN);")]
2008 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_sub(-1), ", stringify!($SelfT), "::MAX);")]
2009 #[stable(feature = "rust1", since = "1.0.0")]
2011 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2012 #[must_use = "this returns the result of the operation, \
2013 without modifying the original"]
2014 #[inline(always)]
2015 pub const fn saturating_sub(self, rhs: Self) -> Self {
2016 intrinsics::saturating_sub(self, rhs)
2017 }
2018
2019 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub_unsigned(127), -27);")]
2026 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub_unsigned(100), ", stringify!($SelfT), "::MIN);")]
2027 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2029 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2030 #[must_use = "this returns the result of the operation, \
2031 without modifying the original"]
2032 #[inline]
2033 pub const fn saturating_sub_unsigned(self, rhs: $UnsignedT) -> Self {
2034 match self.checked_sub_unsigned(rhs) {
2037 Some(x) => x,
2038 None => Self::MIN,
2039 }
2040 }
2041
2042 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_neg(), -100);")]
2049 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_neg(), 100);")]
2050 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_neg(), ", stringify!($SelfT), "::MAX);")]
2051 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_neg(), ", stringify!($SelfT), "::MIN + 1);")]
2052 #[stable(feature = "saturating_neg", since = "1.45.0")]
2055 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2056 #[must_use = "this returns the result of the operation, \
2057 without modifying the original"]
2058 #[inline(always)]
2059 pub const fn saturating_neg(self) -> Self {
2060 intrinsics::saturating_sub(0, self)
2061 }
2062
2063 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_abs(), 100);")]
2070 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_abs(), 100);")]
2071 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_abs(), ", stringify!($SelfT), "::MAX);")]
2072 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).saturating_abs(), ", stringify!($SelfT), "::MAX);")]
2073 #[stable(feature = "saturating_neg", since = "1.45.0")]
2076 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2077 #[must_use = "this returns the result of the operation, \
2078 without modifying the original"]
2079 #[inline]
2080 pub const fn saturating_abs(self) -> Self {
2081 if self.is_negative() {
2082 self.saturating_neg()
2083 } else {
2084 self
2085 }
2086 }
2087
2088 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".saturating_mul(12), 120);")]
2095 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_mul(10), ", stringify!($SelfT), "::MAX);")]
2096 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_mul(10), ", stringify!($SelfT), "::MIN);")]
2097 #[stable(feature = "wrapping", since = "1.7.0")]
2099 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2100 #[must_use = "this returns the result of the operation, \
2101 without modifying the original"]
2102 #[inline]
2103 pub const fn saturating_mul(self, rhs: Self) -> Self {
2104 match self.checked_mul(rhs) {
2105 Some(x) => x,
2106 None => if (self < 0) == (rhs < 0) {
2107 Self::MAX
2108 } else {
2109 Self::MIN
2110 }
2111 }
2112 }
2113
2114 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
2125 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_div(-1), ", stringify!($SelfT), "::MIN + 1);")]
2126 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_div(-1), ", stringify!($SelfT), "::MAX);")]
2127 #[stable(feature = "saturating_div", since = "1.58.0")]
2130 #[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
2131 #[must_use = "this returns the result of the operation, \
2132 without modifying the original"]
2133 #[inline]
2134 pub const fn saturating_div(self, rhs: Self) -> Self {
2135 match self.overflowing_div(rhs) {
2136 (result, false) => result,
2137 (_result, true) => Self::MAX, }
2139 }
2140
2141 #[doc = concat!("assert_eq!((-4", stringify!($SelfT), ").saturating_pow(3), -64);")]
2148 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".saturating_pow(0), 1);")]
2149 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(2), ", stringify!($SelfT), "::MAX);")]
2150 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(3), ", stringify!($SelfT), "::MIN);")]
2151 #[stable(feature = "no_panic_pow", since = "1.34.0")]
2153 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2154 #[must_use = "this returns the result of the operation, \
2155 without modifying the original"]
2156 #[inline]
2157 pub const fn saturating_pow(self, exp: u32) -> Self {
2158 match self.checked_pow(exp) {
2159 Some(x) => x,
2160 None if self < 0 && exp % 2 == 1 => Self::MIN,
2161 None => Self::MAX,
2162 }
2163 }
2164
2165 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add(27), 127);")]
2172 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add(2), ", stringify!($SelfT), "::MIN + 1);")]
2173 #[stable(feature = "rust1", since = "1.0.0")]
2175 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2176 #[must_use = "this returns the result of the operation, \
2177 without modifying the original"]
2178 #[inline(always)]
2179 pub const fn wrapping_add(self, rhs: Self) -> Self {
2180 intrinsics::wrapping_add(self, rhs)
2181 }
2182
2183 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add_unsigned(27), 127);")]
2190 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add_unsigned(2), ", stringify!($SelfT), "::MIN + 1);")]
2191 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2193 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2194 #[must_use = "this returns the result of the operation, \
2195 without modifying the original"]
2196 #[inline(always)]
2197 pub const fn wrapping_add_unsigned(self, rhs: $UnsignedT) -> Self {
2198 self.wrapping_add(rhs as Self)
2199 }
2200
2201 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub(127), -127);")]
2208 #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub(", stringify!($SelfT), "::MAX), ", stringify!($SelfT), "::MAX);")]
2209 #[stable(feature = "rust1", since = "1.0.0")]
2211 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2212 #[must_use = "this returns the result of the operation, \
2213 without modifying the original"]
2214 #[inline(always)]
2215 pub const fn wrapping_sub(self, rhs: Self) -> Self {
2216 intrinsics::wrapping_sub(self, rhs)
2217 }
2218
2219 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub_unsigned(127), -127);")]
2226 #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub_unsigned(", stringify!($UnsignedT), "::MAX), -1);")]
2227 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2229 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2230 #[must_use = "this returns the result of the operation, \
2231 without modifying the original"]
2232 #[inline(always)]
2233 pub const fn wrapping_sub_unsigned(self, rhs: $UnsignedT) -> Self {
2234 self.wrapping_sub(rhs as Self)
2235 }
2236
2237 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".wrapping_mul(12), 120);")]
2244 #[stable(feature = "rust1", since = "1.0.0")]
2247 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2248 #[must_use = "this returns the result of the operation, \
2249 without modifying the original"]
2250 #[inline(always)]
2251 pub const fn wrapping_mul(self, rhs: Self) -> Self {
2252 intrinsics::wrapping_mul(self, rhs)
2253 }
2254
2255 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div(10), 10);")]
2270 #[stable(feature = "num_wrapping", since = "1.2.0")]
2273 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
2274 #[must_use = "this returns the result of the operation, \
2275 without modifying the original"]
2276 #[inline]
2277 pub const fn wrapping_div(self, rhs: Self) -> Self {
2278 self.overflowing_div(rhs).0
2279 }
2280
2281 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div_euclid(10), 10);")]
2296 #[stable(feature = "euclidean_division", since = "1.38.0")]
2299 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2300 #[must_use = "this returns the result of the operation, \
2301 without modifying the original"]
2302 #[inline]
2303 pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
2304 self.overflowing_div_euclid(rhs).0
2305 }
2306
2307 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem(10), 0);")]
2322 #[stable(feature = "num_wrapping", since = "1.2.0")]
2325 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
2326 #[must_use = "this returns the result of the operation, \
2327 without modifying the original"]
2328 #[inline]
2329 pub const fn wrapping_rem(self, rhs: Self) -> Self {
2330 self.overflowing_rem(rhs).0
2331 }
2332
2333 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);")]
2347 #[stable(feature = "euclidean_division", since = "1.38.0")]
2350 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2351 #[must_use = "this returns the result of the operation, \
2352 without modifying the original"]
2353 #[inline]
2354 pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
2355 self.overflowing_rem_euclid(rhs).0
2356 }
2357
2358 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_neg(), -100);")]
2369 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_neg(), 100);")]
2370 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_neg(), ", stringify!($SelfT), "::MIN);")]
2371 #[stable(feature = "num_wrapping", since = "1.2.0")]
2373 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2374 #[must_use = "this returns the result of the operation, \
2375 without modifying the original"]
2376 #[inline(always)]
2377 pub const fn wrapping_neg(self) -> Self {
2378 (0 as $SelfT).wrapping_sub(self)
2379 }
2380
2381 #[doc = concat!("assert_eq!((-1_", stringify!($SelfT), ").wrapping_shl(7), -128);")]
2400 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shl(", stringify!($BITS), "), 42);")]
2401 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shl(1).wrapping_shl(", stringify!($BITS_MINUS_ONE), "), 0);")]
2402 #[doc = concat!("assert_eq!((-1_", stringify!($SelfT), ").wrapping_shl(128), -1);")]
2403 #[doc = concat!("assert_eq!(5_", stringify!($SelfT), ".wrapping_shl(1025), 10);")]
2404 #[stable(feature = "num_wrapping", since = "1.2.0")]
2406 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2407 #[must_use = "this returns the result of the operation, \
2408 without modifying the original"]
2409 #[inline(always)]
2410 pub const fn wrapping_shl(self, rhs: u32) -> Self {
2411 unsafe {
2414 self.unchecked_shl(rhs & (Self::BITS - 1))
2415 }
2416 }
2417
2418 #[doc = concat!("assert_eq!((-128_", stringify!($SelfT), ").wrapping_shr(7), -1);")]
2437 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shr(", stringify!($BITS), "), 42);")]
2438 #[doc = concat!("assert_eq!(42_", stringify!($SelfT), ".wrapping_shr(1).wrapping_shr(", stringify!($BITS_MINUS_ONE), "), 0);")]
2439 #[doc = concat!("assert_eq!(10_", stringify!($SelfT), ".wrapping_shr(1025), 5);")]
2441 #[stable(feature = "num_wrapping", since = "1.2.0")]
2443 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2444 #[must_use = "this returns the result of the operation, \
2445 without modifying the original"]
2446 #[inline(always)]
2447 pub const fn wrapping_shr(self, rhs: u32) -> Self {
2448 unsafe {
2451 self.unchecked_shr(rhs & (Self::BITS - 1))
2452 }
2453 }
2454
2455 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_abs(), 100);")]
2466 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_abs(), 100);")]
2467 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_abs(), ", stringify!($SelfT), "::MIN);")]
2468 #[stable(feature = "no_panic_abs", since = "1.13.0")]
2471 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2472 #[must_use = "this returns the result of the operation, \
2473 without modifying the original"]
2474 #[allow(unused_attributes)]
2475 #[inline]
2476 pub const fn wrapping_abs(self) -> Self {
2477 if self.is_negative() {
2478 self.wrapping_neg()
2479 } else {
2480 self
2481 }
2482 }
2483
2484 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".unsigned_abs(), 100", stringify!($UnsignedT), ");")]
2492 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").unsigned_abs(), 100", stringify!($UnsignedT), ");")]
2493 #[stable(feature = "unsigned_abs", since = "1.51.0")]
2496 #[rustc_const_stable(feature = "unsigned_abs", since = "1.51.0")]
2497 #[must_use = "this returns the result of the operation, \
2498 without modifying the original"]
2499 #[inline]
2500 pub const fn unsigned_abs(self) -> $UnsignedT {
2501 self.wrapping_abs() as $UnsignedT
2502 }
2503
2504 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".wrapping_pow(4), 81);")]
2511 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".wrapping_pow(0), 1);")]
2514 #[stable(feature = "no_panic_pow", since = "1.34.0")]
2516 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2517 #[must_use = "this returns the result of the operation, \
2518 without modifying the original"]
2519 #[inline]
2520 pub const fn wrapping_pow(self, exp: u32) -> Self {
2521 let (a, _) = self.overflowing_pow(exp);
2522 a
2523 }
2524
2525 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_add(2), (7, false));")]
2536 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.overflowing_add(1), (", stringify!($SelfT), "::MIN, true));")]
2537 #[stable(feature = "wrapping", since = "1.7.0")]
2539 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2540 #[must_use = "this returns the result of the operation, \
2541 without modifying the original"]
2542 #[inline(always)]
2543 pub const fn overflowing_add(self, rhs: Self) -> (Self, bool) {
2544 let (a, b) = intrinsics::add_with_overflow(self as $ActualT, rhs as $ActualT);
2545 (a as Self, b)
2546 }
2547
2548 #[doc = concat!("[`", stringify!($UnsignedT), "::carrying_add`]")]
2560 #[doc = concat!("// 10 MAX (a = 10 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")]
2578 #[doc = concat!("// + -5 9 (b = -5 × 2^", stringify!($BITS), " + 9)")]
2579 #[doc = concat!("// 6 8 (sum = 6 × 2^", stringify!($BITS), " + 8)")]
2581 #[doc = concat!("let (a1, a0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (10, ", stringify!($UnsignedT), "::MAX);")]
2583 #[doc = concat!("let (b1, b0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (-5, 9);")]
2584 #[doc = concat!("// ", stringify!($UnsignedT), "::carrying_add for the less significant words")]
2587 #[doc = concat!("// ", stringify!($SelfT), "::carrying_add for the most significant word")]
2591 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2597 #[must_use = "this returns the result of the operation, \
2598 without modifying the original"]
2599 #[inline]
2600 pub const fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool) {
2601 let (a, b) = self.overflowing_add(rhs);
2604 let (c, d) = a.overflowing_add(carry as $SelfT);
2605 (c, b != d)
2606 }
2607
2608 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_add_unsigned(2), (3, false));")]
2618 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_add_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MAX, false));")]
2619 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).overflowing_add_unsigned(3), (", stringify!($SelfT), "::MIN, true));")]
2620 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2622 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2623 #[must_use = "this returns the result of the operation, \
2624 without modifying the original"]
2625 #[inline]
2626 pub const fn overflowing_add_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
2627 let rhs = rhs as Self;
2628 let (res, overflowed) = self.overflowing_add(rhs);
2629 (res, overflowed ^ (rhs < 0))
2630 }
2631
2632 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_sub(2), (3, false));")]
2642 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_sub(1), (", stringify!($SelfT), "::MAX, true));")]
2643 #[stable(feature = "wrapping", since = "1.7.0")]
2645 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2646 #[must_use = "this returns the result of the operation, \
2647 without modifying the original"]
2648 #[inline(always)]
2649 pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
2650 let (a, b) = intrinsics::sub_with_overflow(self as $ActualT, rhs as $ActualT);
2651 (a as Self, b)
2652 }
2653
2654 #[doc = concat!("[`", stringify!($UnsignedT), "::borrowing_sub`]")]
2667 #[doc = concat!("// 6 8 (a = 6 × 2^", stringify!($BITS), " + 8)")]
2685 #[doc = concat!("// - -5 9 (b = -5 × 2^", stringify!($BITS), " + 9)")]
2686 #[doc = concat!("// 10 MAX (diff = 10 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")]
2688 #[doc = concat!("let (a1, a0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (6, 8);")]
2690 #[doc = concat!("let (b1, b0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (-5, 9);")]
2691 #[doc = concat!("// ", stringify!($UnsignedT), "::borrowing_sub for the less significant words")]
2694 #[doc = concat!("// ", stringify!($SelfT), "::borrowing_sub for the most significant word")]
2698 #[doc = concat!("assert_eq!((diff1, diff0), (10, ", stringify!($UnsignedT), "::MAX));")]
2702 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2704 #[must_use = "this returns the result of the operation, \
2705 without modifying the original"]
2706 #[inline]
2707 pub const fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
2708 let (a, b) = self.overflowing_sub(rhs);
2711 let (c, d) = a.overflowing_sub(borrow as $SelfT);
2712 (c, b != d)
2713 }
2714
2715 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_sub_unsigned(2), (-1, false));")]
2725 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX).overflowing_sub_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MIN, false));")]
2726 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).overflowing_sub_unsigned(3), (", stringify!($SelfT), "::MAX, true));")]
2727 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2729 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2730 #[must_use = "this returns the result of the operation, \
2731 without modifying the original"]
2732 #[inline]
2733 pub const fn overflowing_sub_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
2734 let rhs = rhs as Self;
2735 let (res, overflowed) = self.overflowing_sub(rhs);
2736 (res, overflowed ^ (rhs < 0))
2737 }
2738
2739 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_mul(2), (10, false));")]
2748 #[stable(feature = "wrapping", since = "1.7.0")]
2751 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2752 #[must_use = "this returns the result of the operation, \
2753 without modifying the original"]
2754 #[inline(always)]
2755 pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
2756 let (a, b) = intrinsics::mul_with_overflow(self as $ActualT, rhs as $ActualT);
2757 (a as Self, b)
2758 }
2759
2760 #[doc = concat!("assert_eq!(",
2781 stringify!($SelfT), "::MAX.carrying_mul(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
2782 "(", stringify!($SelfT), "::MAX.unsigned_abs() + 1, ", stringify!($SelfT), "::MAX / 2));"
2783 )]
2784 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2786 #[rustc_const_unstable(feature = "signed_bigint_helpers", issue = "151989")]
2787 #[must_use = "this returns the result of the operation, \
2788 without modifying the original"]
2789 #[inline]
2790 pub const fn carrying_mul(self, rhs: Self, carry: Self) -> ($UnsignedT, Self) {
2791 Self::carrying_mul_add(self, rhs, carry, 0)
2792 }
2793
2794 #[doc = concat!("assert_eq!(",
2817 stringify!($SelfT), "::MAX.carrying_mul_add(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
2818 "(", stringify!($UnsignedT), "::MAX, ", stringify!($SelfT), "::MAX / 2));"
2819 )]
2820 #[unstable(feature = "signed_bigint_helpers", issue = "151989")]
2822 #[rustc_const_unstable(feature = "signed_bigint_helpers", issue = "151989")]
2823 #[must_use = "this returns the result of the operation, \
2824 without modifying the original"]
2825 #[inline]
2826 pub const fn carrying_mul_add(self, rhs: Self, carry: Self, add: Self) -> ($UnsignedT, Self) {
2827 intrinsics::carrying_mul_add(self, rhs, carry, add)
2828 }
2829
2830 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div(2), (2, false));")]
2843 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div(-1), (", stringify!($SelfT), "::MIN, true));")]
2844 #[inline]
2846 #[stable(feature = "wrapping", since = "1.7.0")]
2847 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
2848 #[must_use = "this returns the result of the operation, \
2849 without modifying the original"]
2850 pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
2851 if intrinsics::unlikely((self == Self::MIN) & (rhs == -1)) {
2853 (self, true)
2854 } else {
2855 (self / rhs, false)
2856 }
2857 }
2858
2859 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div_euclid(2), (2, false));")]
2872 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringify!($SelfT), "::MIN, true));")]
2873 #[inline]
2875 #[stable(feature = "euclidean_division", since = "1.38.0")]
2876 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2877 #[must_use = "this returns the result of the operation, \
2878 without modifying the original"]
2879 pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
2880 if intrinsics::unlikely((self == Self::MIN) & (rhs == -1)) {
2882 (self, true)
2883 } else {
2884 (self.div_euclid(rhs), false)
2885 }
2886 }
2887
2888 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem(2), (1, false));")]
2901 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem(-1), (0, true));")]
2902 #[inline]
2904 #[stable(feature = "wrapping", since = "1.7.0")]
2905 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
2906 #[must_use = "this returns the result of the operation, \
2907 without modifying the original"]
2908 pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
2909 if intrinsics::unlikely(rhs == -1) {
2910 (0, self == Self::MIN)
2911 } else {
2912 (self % rhs, false)
2913 }
2914 }
2915
2916
2917 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem_euclid(2), (1, false));")]
2930 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));")]
2931 #[stable(feature = "euclidean_division", since = "1.38.0")]
2933 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2934 #[must_use = "this returns the result of the operation, \
2935 without modifying the original"]
2936 #[inline]
2937 #[track_caller]
2938 pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
2939 if intrinsics::unlikely(rhs == -1) {
2940 (0, self == Self::MIN)
2941 } else {
2942 (self.rem_euclid(rhs), false)
2943 }
2944 }
2945
2946
2947 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2, false));")]
2957 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($SelfT), "::MIN, true));")]
2958 #[inline]
2960 #[stable(feature = "wrapping", since = "1.7.0")]
2961 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2962 #[must_use = "this returns the result of the operation, \
2963 without modifying the original"]
2964 #[allow(unused_attributes)]
2965 pub const fn overflowing_neg(self) -> (Self, bool) {
2966 if intrinsics::unlikely(self == Self::MIN) {
2967 (Self::MIN, true)
2968 } else {
2969 (-self, false)
2970 }
2971 }
2972
2973 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT),".overflowing_shl(4), (0x10, false));")]
2983 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shl(", stringify!($BITS_MINUS_ONE), "), (0, false));")]
2985 #[stable(feature = "wrapping", since = "1.7.0")]
2987 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2988 #[must_use = "this returns the result of the operation, \
2989 without modifying the original"]
2990 #[inline]
2991 pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
2992 (self.wrapping_shl(rhs), rhs >= Self::BITS)
2993 }
2994
2995 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shr(4), (0x1, false));")]
3005 #[stable(feature = "wrapping", since = "1.7.0")]
3008 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3009 #[must_use = "this returns the result of the operation, \
3010 without modifying the original"]
3011 #[inline]
3012 pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
3013 (self.wrapping_shr(rhs), rhs >= Self::BITS)
3014 }
3015
3016 #[doc = concat!("(e.g., ", stringify!($SelfT), "::MIN for values of type ", stringify!($SelfT), "),")]
3021 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".overflowing_abs(), (10, false));")]
3028 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").overflowing_abs(), (10, false));")]
3029 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_abs(), (", stringify!($SelfT), "::MIN, true));")]
3030 #[stable(feature = "no_panic_abs", since = "1.13.0")]
3032 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3033 #[must_use = "this returns the result of the operation, \
3034 without modifying the original"]
3035 #[inline]
3036 pub const fn overflowing_abs(self) -> (Self, bool) {
3037 (self.wrapping_abs(), self == Self::MIN)
3038 }
3039
3040 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".overflowing_pow(4), (81, false));")]
3049 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".overflowing_pow(0), (1, false));")]
3050 #[stable(feature = "no_panic_pow", since = "1.34.0")]
3053 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
3054 #[must_use = "this returns the result of the operation, \
3055 without modifying the original"]
3056 #[inline]
3057 pub const fn overflowing_pow(self, mut exp: u32) -> (Self, bool) {
3058 let mut base = self;
3059 let mut acc: Self = 1;
3060 let mut overflow = false;
3061 let mut tmp_overflow;
3062
3063 if intrinsics::is_val_statically_known(base) && base.unsigned_abs().is_power_of_two() {
3064 let k = base.unsigned_abs().ilog2();
3065 let Some(shift) = k.checked_mul(exp) else {
3066 return (0, true)
3067 };
3068 let base: Self = if base < 0 && (exp % 2) != 0 { -1 } else { 1 };
3069 return (base.unbounded_shl(shift), base.shl_exact(shift).is_none());
3070 }
3071
3072 if exp == 0 {
3073 return (1, false);
3074 }
3075
3076 if intrinsics::is_val_statically_known(exp) {
3077 while exp > 1 {
3078 if (exp & 1) == 1 {
3079 (acc, tmp_overflow) = acc.overflowing_mul(base);
3080 overflow |= tmp_overflow;
3081 }
3082 exp /= 2;
3083 (base, tmp_overflow) = base.overflowing_mul(base);
3084 overflow |= tmp_overflow;
3085 }
3086
3087 (acc, tmp_overflow) = acc.overflowing_mul(base);
3092 overflow |= tmp_overflow;
3093 return (acc, overflow);
3094 }
3095
3096 loop {
3097 if (exp & 1) == 1 {
3098 (acc, tmp_overflow) = acc.overflowing_mul(base);
3099 overflow |= tmp_overflow;
3100 if exp == 1 {
3102 return (acc, overflow);
3103 }
3104 }
3105 exp /= 2;
3106 (base, tmp_overflow) = base.overflowing_mul(base);
3107 overflow |= tmp_overflow;
3108 }
3109 }
3110
3111 #[doc = concat!("let x: ", stringify!($SelfT), " = 2; // or any other integer type")]
3117 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".pow(0), 1);")]
3120 #[stable(feature = "rust1", since = "1.0.0")]
3122 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
3123 #[must_use = "this returns the result of the operation, \
3124 without modifying the original"]
3125 #[inline]
3126 #[rustc_inherit_overflow_checks]
3127 pub const fn pow(self, exp: u32) -> Self {
3128 if intrinsics::overflow_checks() {
3129 self.strict_pow(exp)
3130 } else {
3131 self.wrapping_pow(exp)
3132 }
3133 }
3134
3135 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".isqrt(), 3);")]
3149 #[stable(feature = "isqrt", since = "1.84.0")]
3151 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
3152 #[must_use = "this returns the result of the operation, \
3153 without modifying the original"]
3154 #[inline]
3155 #[track_caller]
3156 pub const fn isqrt(self) -> Self {
3157 match self.checked_isqrt() {
3158 Some(sqrt) => sqrt,
3159 None => imp::int_sqrt::panic_for_negative_argument(),
3160 }
3161 }
3162
3163 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
3184 #[stable(feature = "euclidean_division", since = "1.38.0")]
3192 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3193 #[must_use = "this returns the result of the operation, \
3194 without modifying the original"]
3195 #[inline]
3196 #[track_caller]
3197 pub const fn div_euclid(self, rhs: Self) -> Self {
3198 let q = self / rhs;
3199 if self % rhs < 0 {
3200 return if rhs > 0 { q - 1 } else { q + 1 }
3201 }
3202 q
3203 }
3204
3205
3206 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
3222 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.rem_euclid(-1);")]
3233 #[doc(alias = "modulo", alias = "mod")]
3235 #[stable(feature = "euclidean_division", since = "1.38.0")]
3236 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3237 #[must_use = "this returns the result of the operation, \
3238 without modifying the original"]
3239 #[inline]
3240 #[track_caller]
3241 pub const fn rem_euclid(self, rhs: Self) -> Self {
3242 let r = self % rhs;
3243 if r < 0 {
3244 r.wrapping_add(rhs.wrapping_abs())
3253 } else {
3254 r
3255 }
3256 }
3257
3258 #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3270 #[unstable(feature = "int_roundings", issue = "88581")]
3278 #[must_use = "this returns the result of the operation, \
3279 without modifying the original"]
3280 #[inline]
3281 #[track_caller]
3282 pub const fn div_floor(self, rhs: Self) -> Self {
3283 let d = self / rhs;
3284 let r = self % rhs;
3285
3286 let correction = (self ^ rhs) >> (Self::BITS - 1);
3293 if r != 0 {
3294 d + correction
3295 } else {
3296 d
3297 }
3298 }
3299
3300 #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3312 #[unstable(feature = "int_roundings", issue = "88581")]
3320 #[must_use = "this returns the result of the operation, \
3321 without modifying the original"]
3322 #[inline]
3323 #[track_caller]
3324 pub const fn div_ceil(self, rhs: Self) -> Self {
3325 let d = self / rhs;
3326 let r = self % rhs;
3327
3328 let correction = 1 + ((self ^ rhs) >> (Self::BITS - 1));
3331 if r != 0 {
3332 d + correction
3333 } else {
3334 d
3335 }
3336 }
3337
3338 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(8), 16);")]
3357 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(8), 24);")]
3358 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
3359 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
3360 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
3361 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
3362 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(-8), -16);")]
3363 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(-8), -24);")]
3364 #[unstable(feature = "int_roundings", issue = "88581")]
3366 #[must_use = "this returns the result of the operation, \
3367 without modifying the original"]
3368 #[inline]
3369 #[rustc_inherit_overflow_checks]
3370 pub const fn next_multiple_of(self, rhs: Self) -> Self {
3371 if rhs == -1 {
3373 return self;
3374 }
3375
3376 let r = self % rhs;
3377 let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
3378 r + rhs
3379 } else {
3380 r
3381 };
3382
3383 if m == 0 {
3384 self
3385 } else {
3386 self + (rhs - m)
3387 }
3388 }
3389
3390 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(16));")]
3401 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(24));")]
3402 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
3403 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
3404 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
3405 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
3406 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-16));")]
3407 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-24));")]
3408 #[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".checked_next_multiple_of(0), None);")]
3409 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_next_multiple_of(2), None);")]
3410 #[unstable(feature = "int_roundings", issue = "88581")]
3412 #[must_use = "this returns the result of the operation, \
3413 without modifying the original"]
3414 #[inline]
3415 pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
3416 if rhs == -1 {
3418 return Some(self);
3419 }
3420
3421 let r = try_opt!(self.checked_rem(rhs));
3422 let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
3423 r + rhs
3425 } else {
3426 r
3427 };
3428
3429 if m == 0 {
3430 Some(self)
3431 } else {
3432 self.checked_add(rhs - m)
3434 }
3435 }
3436
3437 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".ilog(5), 1);")]
3453 #[stable(feature = "int_log", since = "1.67.0")]
3455 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3456 #[must_use = "this returns the result of the operation, \
3457 without modifying the original"]
3458 #[inline]
3459 #[track_caller]
3460 pub const fn ilog(self, base: Self) -> u32 {
3461 assert!(base >= 2, "base of integer logarithm must be at least 2");
3462 if let Some(log) = self.checked_ilog(base) {
3463 log
3464 } else {
3465 imp::int_log10::panic_for_nonpositive_argument()
3466 }
3467 }
3468
3469 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".ilog2(), 1);")]
3479 #[stable(feature = "int_log", since = "1.67.0")]
3481 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3482 #[must_use = "this returns the result of the operation, \
3483 without modifying the original"]
3484 #[inline]
3485 #[track_caller]
3486 pub const fn ilog2(self) -> u32 {
3487 if let Some(log) = self.checked_ilog2() {
3488 log
3489 } else {
3490 imp::int_log10::panic_for_nonpositive_argument()
3491 }
3492 }
3493
3494 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".ilog10(), 1);")]
3504 #[stable(feature = "int_log", since = "1.67.0")]
3506 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3507 #[must_use = "this returns the result of the operation, \
3508 without modifying the original"]
3509 #[inline]
3510 #[track_caller]
3511 pub const fn ilog10(self) -> u32 {
3512 if let Some(log) = self.checked_ilog10() {
3513 log
3514 } else {
3515 imp::int_log10::panic_for_nonpositive_argument()
3516 }
3517 }
3518
3519 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_ilog(5), Some(1));")]
3532 #[stable(feature = "int_log", since = "1.67.0")]
3534 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3535 #[must_use = "this returns the result of the operation, \
3536 without modifying the original"]
3537 #[inline]
3538 pub const fn checked_ilog(self, base: Self) -> Option<u32> {
3539 if self <= 0 || base <= 1 {
3540 None
3541 } else {
3542 (self as $UnsignedT).checked_ilog(base as $UnsignedT)
3545 }
3546 }
3547
3548 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_ilog2(), Some(1));")]
3556 #[stable(feature = "int_log", since = "1.67.0")]
3558 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3559 #[must_use = "this returns the result of the operation, \
3560 without modifying the original"]
3561 #[inline]
3562 pub const fn checked_ilog2(self) -> Option<u32> {
3563 if self <= 0 {
3564 None
3565 } else {
3566 let log = (Self::BITS - 1) - unsafe { intrinsics::ctlz_nonzero(self) as u32 };
3568 Some(log)
3569 }
3570 }
3571
3572 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_ilog10(), Some(1));")]
3580 #[stable(feature = "int_log", since = "1.67.0")]
3582 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3583 #[must_use = "this returns the result of the operation, \
3584 without modifying the original"]
3585 #[inline]
3586 pub const fn checked_ilog10(self) -> Option<u32> {
3587 imp::int_log10::$ActualT(self as $ActualT)
3588 }
3589
3590 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
3596 #[doc = concat!("`", stringify!($SelfT), "`,")]
3598 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
3602 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".abs(), 10);")]
3609 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").abs(), 10);")]
3610 #[stable(feature = "rust1", since = "1.0.0")]
3612 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3613 #[allow(unused_attributes)]
3614 #[must_use = "this returns the result of the operation, \
3615 without modifying the original"]
3616 #[inline]
3617 #[rustc_inherit_overflow_checks]
3618 pub const fn abs(self) -> Self {
3619 if self.is_negative() {
3623 -self
3624 } else {
3625 self
3626 }
3627 }
3628
3629 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(80), 20", stringify!($UnsignedT), ");")]
3638 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(110), 10", stringify!($UnsignedT), ");")]
3639 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(80), 180", stringify!($UnsignedT), ");")]
3640 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(-120), 20", stringify!($UnsignedT), ");")]
3641 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.abs_diff(", stringify!($SelfT), "::MAX), ", stringify!($UnsignedT), "::MAX);")]
3642 #[stable(feature = "int_abs_diff", since = "1.60.0")]
3644 #[rustc_const_stable(feature = "int_abs_diff", since = "1.60.0")]
3645 #[must_use = "this returns the result of the operation, \
3646 without modifying the original"]
3647 #[inline]
3648 pub const fn abs_diff(self, other: Self) -> $UnsignedT {
3649 if self < other {
3650 (other as $UnsignedT).wrapping_sub(self as $UnsignedT)
3664 } else {
3665 (self as $UnsignedT).wrapping_sub(other as $UnsignedT)
3666 }
3667 }
3668
3669 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".signum(), 1);")]
3679 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".signum(), 0);")]
3680 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").signum(), -1);")]
3681 #[stable(feature = "rust1", since = "1.0.0")]
3683 #[rustc_const_stable(feature = "const_int_sign", since = "1.47.0")]
3684 #[must_use = "this returns the result of the operation, \
3685 without modifying the original"]
3686 #[inline(always)]
3687 pub const fn signum(self) -> Self {
3688 crate::intrinsics::three_way_compare(self, 0) as Self
3694 }
3695
3696 #[doc = concat!("assert!(10", stringify!($SelfT), ".is_positive());")]
3703 #[doc = concat!("assert!(!(-10", stringify!($SelfT), ").is_positive());")]
3704 #[must_use]
3706 #[stable(feature = "rust1", since = "1.0.0")]
3707 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3708 #[inline(always)]
3709 pub const fn is_positive(self) -> bool { self > 0 }
3710
3711 #[doc = concat!("assert!((-10", stringify!($SelfT), ").is_negative());")]
3718 #[doc = concat!("assert!(!10", stringify!($SelfT), ".is_negative());")]
3719 #[must_use]
3721 #[stable(feature = "rust1", since = "1.0.0")]
3722 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3723 #[inline(always)]
3724 pub const fn is_negative(self) -> bool { self < 0 }
3725
3726 #[doc = $to_xe_bytes_doc]
3730 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();")]
3735 #[doc = concat!("assert_eq!(bytes, ", $be_bytes, ");")]
3736 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3738 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3739 #[must_use = "this returns the result of the operation, \
3740 without modifying the original"]
3741 #[inline]
3742 pub const fn to_be_bytes(self) -> [u8; size_of::<Self>()] {
3743 self.to_be().to_ne_bytes()
3744 }
3745
3746 #[doc = $to_xe_bytes_doc]
3750 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();")]
3755 #[doc = concat!("assert_eq!(bytes, ", $le_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_le_bytes(self) -> [u8; size_of::<Self>()] {
3763 self.to_le().to_ne_bytes()
3764 }
3765
3766 #[doc = $to_xe_bytes_doc]
3774 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_ne_bytes();")]
3782 #[doc = concat!(" ", $be_bytes)]
3786 #[doc = concat!(" ", $le_bytes)]
3788 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3792 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3793 #[allow(unnecessary_transmutes)]
3794 #[must_use = "this returns the result of the operation, \
3797 without modifying the original"]
3798 #[inline]
3799 pub const fn to_ne_bytes(self) -> [u8; size_of::<Self>()] {
3800 unsafe { mem::transmute(self) }
3803 }
3804
3805 #[doc = $from_xe_bytes_doc]
3809 #[doc = concat!("let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, ");")]
3814 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3815 #[doc = concat!("fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3821 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3822 #[doc = concat!(" ", stringify!($SelfT), "::from_be_bytes(int_bytes.try_into().unwrap())")]
3824 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3827 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3828 #[must_use]
3829 #[inline]
3830 pub const fn from_be_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3831 Self::from_be(Self::from_ne_bytes(bytes))
3832 }
3833
3834 #[doc = $from_xe_bytes_doc]
3838 #[doc = concat!("let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, ");")]
3843 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3844 #[doc = concat!("fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3850 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3851 #[doc = concat!(" ", stringify!($SelfT), "::from_le_bytes(int_bytes.try_into().unwrap())")]
3853 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3856 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3857 #[must_use]
3858 #[inline]
3859 pub const fn from_le_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3860 Self::from_le(Self::from_ne_bytes(bytes))
3861 }
3862
3863 #[doc = $from_xe_bytes_doc]
3874 #[doc = concat!("let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"big\") {")]
3879 #[doc = concat!(" ", $be_bytes)]
3880 #[doc = concat!(" ", $le_bytes)]
3882 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3884 #[doc = concat!("fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3890 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3891 #[doc = concat!(" ", stringify!($SelfT), "::from_ne_bytes(int_bytes.try_into().unwrap())")]
3893 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3896 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3897 #[allow(unnecessary_transmutes)]
3898 #[must_use]
3899 #[inline]
3902 pub const fn from_ne_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3903 unsafe { mem::transmute(bytes) }
3905 }
3906
3907 #[doc = concat!("[`", stringify!($SelfT), "::MIN", "`] instead.")]
3909 #[stable(feature = "rust1", since = "1.0.0")]
3912 #[inline(always)]
3913 #[rustc_promotable]
3914 #[rustc_const_stable(feature = "const_min_value", since = "1.32.0")]
3915 #[deprecated(since = "TBD", note = "replaced by the `MIN` associated constant on this type")]
3916 #[rustc_diagnostic_item = concat!(stringify!($SelfT), "_legacy_fn_min_value")]
3917 pub const fn min_value() -> Self {
3918 Self::MIN
3919 }
3920
3921 #[doc = concat!("[`", stringify!($SelfT), "::MAX", "`] instead.")]
3923 #[stable(feature = "rust1", since = "1.0.0")]
3926 #[inline(always)]
3927 #[rustc_promotable]
3928 #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
3929 #[deprecated(since = "TBD", note = "replaced by the `MAX` associated constant on this type")]
3930 #[rustc_diagnostic_item = concat!(stringify!($SelfT), "_legacy_fn_max_value")]
3931 pub const fn max_value() -> Self {
3932 Self::MAX
3933 }
3934
3935 #[doc = concat!("assert_eq!(120", stringify!($SelfT), ".clamp_magnitude(100), 100);")]
3947 #[doc = concat!("assert_eq!(-120", stringify!($SelfT), ".clamp_magnitude(100), -100);")]
3948 #[doc = concat!("assert_eq!(80", stringify!($SelfT), ".clamp_magnitude(100), 80);")]
3949 #[doc = concat!("assert_eq!(-80", stringify!($SelfT), ".clamp_magnitude(100), -80);")]
3950 #[must_use = "this returns the clamped value and does not modify the original"]
3952 #[unstable(feature = "clamp_magnitude", issue = "148519")]
3953 #[inline]
3954 pub fn clamp_magnitude(self, limit: $UnsignedT) -> Self {
3955 if let Ok(limit) = core::convert::TryInto::<$SelfT>::try_into(limit) {
3956 self.clamp(-limit, limit)
3957 } else {
3958 self
3959 }
3960 }
3961
3962 #[doc = concat!("assert_eq!(120i8, 120", stringify!($SelfT), ".truncate());")]
3970 #[doc = concat!("assert_eq!(-120i8, (-120", stringify!($SelfT), ").truncate());")]
3971 #[must_use = "this returns the truncated value and does not modify the original"]
3974 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
3975 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
3976 #[inline]
3977 pub const fn truncate<Target>(self) -> Target
3978 where Self: [const] traits::TruncateTarget<Target>
3979 {
3980 traits::TruncateTarget::internal_truncate(self)
3981 }
3982
3983 #[doc = concat!("assert_eq!(120i8, 120", stringify!($SelfT), ".saturating_truncate());")]
3991 #[doc = concat!("assert_eq!(-120i8, (-120", stringify!($SelfT), ").saturating_truncate());")]
3992 #[must_use = "this returns the truncated value and does not modify the original"]
3996 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
3997 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
3998 #[inline]
3999 pub const fn saturating_truncate<Target>(self) -> Target
4000 where Self: [const] traits::TruncateTarget<Target>
4001 {
4002 traits::TruncateTarget::internal_saturating_truncate(self)
4003 }
4004
4005 #[doc = concat!("assert_eq!(Some(120i8), 120", stringify!($SelfT), ".checked_truncate());")]
4013 #[doc = concat!("assert_eq!(Some(-120i8), (-120", stringify!($SelfT), ").checked_truncate());")]
4014 #[must_use = "this returns the truncated value and does not modify the original"]
4018 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
4019 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
4020 #[inline]
4021 pub const fn checked_truncate<Target>(self) -> Option<Target>
4022 where Self: [const] traits::TruncateTarget<Target>
4023 {
4024 traits::TruncateTarget::internal_checked_truncate(self)
4025 }
4026
4027 #[doc = concat!("assert_eq!(120i128, 120i8.widen());")]
4034 #[doc = concat!("assert_eq!(-120i128, (-120i8).widen());")]
4035 #[must_use = "this returns the widened value and does not modify the original"]
4037 #[unstable(feature = "integer_widen_truncate", issue = "154330")]
4038 #[rustc_const_unstable(feature = "integer_widen_truncate", issue = "154330")]
4039 #[inline]
4040 pub const fn widen<Target>(self) -> Target
4041 where Self: [const] traits::WidenTarget<Target>
4042 {
4043 traits::WidenTarget::internal_widen(self)
4044 }
4045 }
4046}