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 #[cfg(not(feature = "ferrocene_certified"))]
58 pub const BITS: u32 = <$UnsignedT>::BITS;
59
60 #[doc = concat!("let n = 0b100_0000", stringify!($SelfT), ";")]
66 #[stable(feature = "rust1", since = "1.0.0")]
71 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
72 #[doc(alias = "popcount")]
73 #[doc(alias = "popcnt")]
74 #[must_use = "this returns the result of the operation, \
75 without modifying the original"]
76 #[inline(always)]
77 #[cfg(not(feature = "ferrocene_certified"))]
78 pub const fn count_ones(self) -> u32 { (self as $UnsignedT).count_ones() }
79
80 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.count_zeros(), 1);")]
86 #[stable(feature = "rust1", since = "1.0.0")]
88 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
89 #[must_use = "this returns the result of the operation, \
90 without modifying the original"]
91 #[inline(always)]
92 #[cfg(not(feature = "ferrocene_certified"))]
93 pub const fn count_zeros(self) -> u32 {
94 (!self).count_ones()
95 }
96
97 #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
106 #[doc = concat!("[`ilog2`]: ", stringify!($SelfT), "::ilog2")]
110 #[stable(feature = "rust1", since = "1.0.0")]
111 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
112 #[must_use = "this returns the result of the operation, \
113 without modifying the original"]
114 #[inline(always)]
115 #[cfg(not(feature = "ferrocene_certified"))]
116 pub const fn leading_zeros(self) -> u32 {
117 (self as $UnsignedT).leading_zeros()
118 }
119
120 #[doc = concat!("let n = -4", stringify!($SelfT), ";")]
126 #[stable(feature = "rust1", since = "1.0.0")]
130 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
131 #[must_use = "this returns the result of the operation, \
132 without modifying the original"]
133 #[inline(always)]
134 #[cfg(not(feature = "ferrocene_certified"))]
135 pub const fn trailing_zeros(self) -> u32 {
136 (self as $UnsignedT).trailing_zeros()
137 }
138
139 #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
145 #[doc = concat!("assert_eq!(n.leading_ones(), ", stringify!($BITS), ");")]
147 #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
149 #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
150 #[must_use = "this returns the result of the operation, \
151 without modifying the original"]
152 #[inline(always)]
153 #[cfg(not(feature = "ferrocene_certified"))]
154 pub const fn leading_ones(self) -> u32 {
155 (self as $UnsignedT).leading_ones()
156 }
157
158 #[doc = concat!("let n = 3", stringify!($SelfT), ";")]
164 #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
168 #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
169 #[must_use = "this returns the result of the operation, \
170 without modifying the original"]
171 #[inline(always)]
172 #[cfg(not(feature = "ferrocene_certified"))]
173 pub const fn trailing_ones(self) -> u32 {
174 (self as $UnsignedT).trailing_ones()
175 }
176
177 #[doc = concat!("let n: ", stringify!($SelfT), " = 0b_01100100;")]
186 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".isolate_highest_one(), 0);")]
189 #[unstable(feature = "isolate_most_least_significant_one", issue = "136909")]
191 #[must_use = "this returns the result of the operation, \
192 without modifying the original"]
193 #[inline(always)]
194 #[cfg(not(feature = "ferrocene_certified"))]
195 pub const fn isolate_highest_one(self) -> Self {
196 self & (((1 as $SelfT) << (<$SelfT>::BITS - 1)).wrapping_shr(self.leading_zeros()))
197 }
198
199 #[doc = concat!("let n: ", stringify!($SelfT), " = 0b_01100100;")]
208 #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".isolate_lowest_one(), 0);")]
211 #[unstable(feature = "isolate_most_least_significant_one", issue = "136909")]
213 #[must_use = "this returns the result of the operation, \
214 without modifying the original"]
215 #[inline(always)]
216 #[cfg(not(feature = "ferrocene_certified"))]
217 pub const fn isolate_lowest_one(self) -> Self {
218 self & self.wrapping_neg()
219 }
220
221 #[doc = concat!("assert_eq!(0x0_", stringify!($SelfT), ".highest_one(), None);")]
230 #[doc = concat!("assert_eq!(0x1_", stringify!($SelfT), ".highest_one(), Some(0));")]
231 #[doc = concat!("assert_eq!(0x10_", stringify!($SelfT), ".highest_one(), Some(4));")]
232 #[doc = concat!("assert_eq!(0x1f_", stringify!($SelfT), ".highest_one(), Some(4));")]
233 #[unstable(feature = "int_lowest_highest_one", issue = "145203")]
235 #[must_use = "this returns the result of the operation, \
236 without modifying the original"]
237 #[inline(always)]
238 #[cfg(not(feature = "ferrocene_certified"))]
239 pub const fn highest_one(self) -> Option<u32> {
240 (self as $UnsignedT).highest_one()
241 }
242
243 #[doc = concat!("assert_eq!(0x0_", stringify!($SelfT), ".lowest_one(), None);")]
252 #[doc = concat!("assert_eq!(0x1_", stringify!($SelfT), ".lowest_one(), Some(0));")]
253 #[doc = concat!("assert_eq!(0x10_", stringify!($SelfT), ".lowest_one(), Some(4));")]
254 #[doc = concat!("assert_eq!(0x1f_", stringify!($SelfT), ".lowest_one(), Some(0));")]
255 #[unstable(feature = "int_lowest_highest_one", issue = "145203")]
257 #[must_use = "this returns the result of the operation, \
258 without modifying the original"]
259 #[inline(always)]
260 #[cfg(not(feature = "ferrocene_certified"))]
261 pub const fn lowest_one(self) -> Option<u32> {
262 (self as $UnsignedT).lowest_one()
263 }
264
265 #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
274 #[doc = concat!("assert_eq!(n.cast_unsigned(), ", stringify!($UnsignedT), "::MAX);")]
276 #[stable(feature = "integer_sign_cast", since = "1.87.0")]
278 #[rustc_const_stable(feature = "integer_sign_cast", since = "1.87.0")]
279 #[must_use = "this returns the result of the operation, \
280 without modifying the original"]
281 #[inline(always)]
282 #[cfg(not(feature = "ferrocene_certified"))]
283 pub const fn cast_unsigned(self) -> $UnsignedT {
284 self as $UnsignedT
285 }
286
287 #[doc = concat!("let n = ", $rot_op, stringify!($SelfT), ";")]
296 #[doc = concat!("let m = ", $rot_result, ";")]
297 #[doc = concat!("assert_eq!(n.rotate_left(", $rot, "), m);")]
299 #[stable(feature = "rust1", since = "1.0.0")]
301 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
302 #[must_use = "this returns the result of the operation, \
303 without modifying the original"]
304 #[inline(always)]
305 #[cfg(not(feature = "ferrocene_certified"))]
306 pub const fn rotate_left(self, n: u32) -> Self {
307 (self as $UnsignedT).rotate_left(n) as Self
308 }
309
310 #[doc = concat!("let n = ", $rot_result, stringify!($SelfT), ";")]
320 #[doc = concat!("let m = ", $rot_op, ";")]
321 #[doc = concat!("assert_eq!(n.rotate_right(", $rot, "), m);")]
323 #[stable(feature = "rust1", since = "1.0.0")]
325 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
326 #[must_use = "this returns the result of the operation, \
327 without modifying the original"]
328 #[inline(always)]
329 #[cfg(not(feature = "ferrocene_certified"))]
330 pub const fn rotate_right(self, n: u32) -> Self {
331 (self as $UnsignedT).rotate_right(n) as Self
332 }
333
334 #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
340 #[doc = concat!("assert_eq!(m, ", $swapped, ");")]
344 #[stable(feature = "rust1", since = "1.0.0")]
346 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
347 #[must_use = "this returns the result of the operation, \
348 without modifying the original"]
349 #[inline(always)]
350 #[cfg(not(feature = "ferrocene_certified"))]
351 pub const fn swap_bytes(self) -> Self {
352 (self as $UnsignedT).swap_bytes() as Self
353 }
354
355 #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
362 #[doc = concat!("assert_eq!(m, ", $reversed, ");")]
365 #[doc = concat!("assert_eq!(0, 0", stringify!($SelfT), ".reverse_bits());")]
366 #[stable(feature = "reverse_bits", since = "1.37.0")]
368 #[rustc_const_stable(feature = "reverse_bits", since = "1.37.0")]
369 #[must_use = "this returns the result of the operation, \
370 without modifying the original"]
371 #[inline(always)]
372 #[cfg(not(feature = "ferrocene_certified"))]
373 pub const fn reverse_bits(self) -> Self {
374 (self as $UnsignedT).reverse_bits() as Self
375 }
376
377 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
385 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_be(n), n)")]
388 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_be(n), n.swap_bytes())")]
390 #[stable(feature = "rust1", since = "1.0.0")]
393 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
394 #[must_use]
395 #[inline]
396 #[cfg(not(feature = "ferrocene_certified"))]
397 pub const fn from_be(x: Self) -> Self {
398 #[cfg(target_endian = "big")]
399 {
400 x
401 }
402 #[cfg(not(target_endian = "big"))]
403 {
404 x.swap_bytes()
405 }
406 }
407
408 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
416 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_le(n), n)")]
419 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_le(n), n.swap_bytes())")]
421 #[stable(feature = "rust1", since = "1.0.0")]
424 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
425 #[must_use]
426 #[inline]
427 #[cfg(not(feature = "ferrocene_certified"))]
428 pub const fn from_le(x: Self) -> Self {
429 #[cfg(target_endian = "little")]
430 {
431 x
432 }
433 #[cfg(not(target_endian = "little"))]
434 {
435 x.swap_bytes()
436 }
437 }
438
439 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
447 #[stable(feature = "rust1", since = "1.0.0")]
455 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
456 #[must_use = "this returns the result of the operation, \
457 without modifying the original"]
458 #[inline]
459 #[cfg(not(feature = "ferrocene_certified"))]
460 pub const fn to_be(self) -> Self { #[cfg(target_endian = "big")]
462 {
463 self
464 }
465 #[cfg(not(target_endian = "big"))]
466 {
467 self.swap_bytes()
468 }
469 }
470
471 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
479 #[stable(feature = "rust1", since = "1.0.0")]
487 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
488 #[must_use = "this returns the result of the operation, \
489 without modifying the original"]
490 #[inline]
491 #[cfg(not(feature = "ferrocene_certified"))]
492 pub const fn to_le(self) -> Self {
493 #[cfg(target_endian = "little")]
494 {
495 self
496 }
497 #[cfg(not(target_endian = "little"))]
498 {
499 self.swap_bytes()
500 }
501 }
502
503 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(1), Some(", stringify!($SelfT), "::MAX - 1));")]
510 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);")]
511 #[stable(feature = "rust1", since = "1.0.0")]
513 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
514 #[must_use = "this returns the result of the operation, \
515 without modifying the original"]
516 #[inline]
517 #[cfg(not(feature = "ferrocene_certified"))]
518 pub const fn checked_add(self, rhs: Self) -> Option<Self> {
519 let (a, b) = self.overflowing_add(rhs);
520 if intrinsics::unlikely(b) { None } else { Some(a) }
521 }
522
523 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).strict_add(1), ", stringify!($SelfT), "::MAX - 1);")]
536 #[doc = concat!("let _ = (", stringify!($SelfT), "::MAX - 2).strict_add(3);")]
542 #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
544 #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
545 #[must_use = "this returns the result of the operation, \
546 without modifying the original"]
547 #[inline]
548 #[track_caller]
549 #[cfg(not(feature = "ferrocene_certified"))]
550 pub const fn strict_add(self, rhs: Self) -> Self {
551 let (a, b) = self.overflowing_add(rhs);
552 if b { overflow_panic::add() } else { a }
553 }
554
555 #[doc = concat!("`self + rhs > ", stringify!($SelfT), "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`,")]
568 #[doc = concat!("[`checked_add`]: ", stringify!($SelfT), "::checked_add")]
572 #[doc = concat!("[`wrapping_add`]: ", stringify!($SelfT), "::wrapping_add")]
573 #[stable(feature = "unchecked_math", since = "1.79.0")]
574 #[rustc_const_stable(feature = "unchecked_math", since = "1.79.0")]
575 #[must_use = "this returns the result of the operation, \
576 without modifying the original"]
577 #[inline(always)]
578 #[track_caller]
579 #[cfg(not(feature = "ferrocene_certified"))]
580 pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
581 assert_unsafe_precondition!(
582 check_language_ub,
583 concat!(stringify!($SelfT), "::unchecked_add cannot overflow"),
584 (
585 lhs: $SelfT = self,
586 rhs: $SelfT = rhs,
587 ) => !lhs.overflowing_add(rhs).1,
588 );
589
590 unsafe {
592 intrinsics::unchecked_add(self, rhs)
593 }
594 }
595
596 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_add_unsigned(2), Some(3));")]
603 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add_unsigned(3), None);")]
604 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
606 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
607 #[must_use = "this returns the result of the operation, \
608 without modifying the original"]
609 #[inline]
610 #[cfg(not(feature = "ferrocene_certified"))]
611 pub const fn checked_add_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
612 let (a, b) = self.overflowing_add_unsigned(rhs);
613 if intrinsics::unlikely(b) { None } else { Some(a) }
614 }
615
616 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".strict_add_unsigned(2), 3);")]
629 #[doc = concat!("let _ = (", stringify!($SelfT), "::MAX - 2).strict_add_unsigned(3);")]
635 #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
637 #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
638 #[must_use = "this returns the result of the operation, \
639 without modifying the original"]
640 #[inline]
641 #[track_caller]
642 #[cfg(not(feature = "ferrocene_certified"))]
643 pub const fn strict_add_unsigned(self, rhs: $UnsignedT) -> Self {
644 let (a, b) = self.overflowing_add_unsigned(rhs);
645 if b { overflow_panic::add() } else { a }
646 }
647
648 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub(1), Some(", stringify!($SelfT), "::MIN + 1));")]
655 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub(3), None);")]
656 #[stable(feature = "rust1", since = "1.0.0")]
658 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
659 #[must_use = "this returns the result of the operation, \
660 without modifying the original"]
661 #[inline]
662 #[cfg(not(feature = "ferrocene_certified"))]
663 pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
664 let (a, b) = self.overflowing_sub(rhs);
665 if intrinsics::unlikely(b) { None } else { Some(a) }
666 }
667
668 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).strict_sub(1), ", stringify!($SelfT), "::MIN + 1);")]
681 #[doc = concat!("let _ = (", stringify!($SelfT), "::MIN + 2).strict_sub(3);")]
687 #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
689 #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
690 #[must_use = "this returns the result of the operation, \
691 without modifying the original"]
692 #[inline]
693 #[track_caller]
694 #[cfg(not(feature = "ferrocene_certified"))]
695 pub const fn strict_sub(self, rhs: Self) -> Self {
696 let (a, b) = self.overflowing_sub(rhs);
697 if b { overflow_panic::sub() } else { a }
698 }
699
700 #[doc = concat!("`self - rhs > ", stringify!($SelfT), "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`,")]
713 #[doc = concat!("[`checked_sub`]: ", stringify!($SelfT), "::checked_sub")]
717 #[doc = concat!("[`wrapping_sub`]: ", stringify!($SelfT), "::wrapping_sub")]
718 #[stable(feature = "unchecked_math", since = "1.79.0")]
719 #[rustc_const_stable(feature = "unchecked_math", since = "1.79.0")]
720 #[must_use = "this returns the result of the operation, \
721 without modifying the original"]
722 #[inline(always)]
723 #[track_caller]
724 #[cfg(not(feature = "ferrocene_certified"))]
725 pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
726 assert_unsafe_precondition!(
727 check_language_ub,
728 concat!(stringify!($SelfT), "::unchecked_sub cannot overflow"),
729 (
730 lhs: $SelfT = self,
731 rhs: $SelfT = rhs,
732 ) => !lhs.overflowing_sub(rhs).1,
733 );
734
735 unsafe {
737 intrinsics::unchecked_sub(self, rhs)
738 }
739 }
740
741 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_sub_unsigned(2), Some(-1));")]
748 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub_unsigned(3), None);")]
749 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
751 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
752 #[must_use = "this returns the result of the operation, \
753 without modifying the original"]
754 #[inline]
755 #[cfg(not(feature = "ferrocene_certified"))]
756 pub const fn checked_sub_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
757 let (a, b) = self.overflowing_sub_unsigned(rhs);
758 if intrinsics::unlikely(b) { None } else { Some(a) }
759 }
760
761 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".strict_sub_unsigned(2), -1);")]
774 #[doc = concat!("let _ = (", stringify!($SelfT), "::MIN + 2).strict_sub_unsigned(3);")]
780 #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
782 #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
783 #[must_use = "this returns the result of the operation, \
784 without modifying the original"]
785 #[inline]
786 #[track_caller]
787 #[cfg(not(feature = "ferrocene_certified"))]
788 pub const fn strict_sub_unsigned(self, rhs: $UnsignedT) -> Self {
789 let (a, b) = self.overflowing_sub_unsigned(rhs);
790 if b { overflow_panic::sub() } else { a }
791 }
792
793 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(1), Some(", stringify!($SelfT), "::MAX));")]
800 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);")]
801 #[stable(feature = "rust1", since = "1.0.0")]
803 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
804 #[must_use = "this returns the result of the operation, \
805 without modifying the original"]
806 #[inline]
807 #[cfg(not(feature = "ferrocene_certified"))]
808 pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
809 let (a, b) = self.overflowing_mul(rhs);
810 if intrinsics::unlikely(b) { None } else { Some(a) }
811 }
812
813 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.strict_mul(1), ", stringify!($SelfT), "::MAX);")]
826 #[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_mul(2);")]
832 #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
834 #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
835 #[must_use = "this returns the result of the operation, \
836 without modifying the original"]
837 #[inline]
838 #[track_caller]
839 #[cfg(not(feature = "ferrocene_certified"))]
840 pub const fn strict_mul(self, rhs: Self) -> Self {
841 let (a, b) = self.overflowing_mul(rhs);
842 if b { overflow_panic::mul() } else { a }
843 }
844
845 #[doc = concat!("`self * rhs > ", stringify!($SelfT), "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`,")]
858 #[doc = concat!("[`checked_mul`]: ", stringify!($SelfT), "::checked_mul")]
862 #[doc = concat!("[`wrapping_mul`]: ", stringify!($SelfT), "::wrapping_mul")]
863 #[stable(feature = "unchecked_math", since = "1.79.0")]
864 #[rustc_const_stable(feature = "unchecked_math", since = "1.79.0")]
865 #[must_use = "this returns the result of the operation, \
866 without modifying the original"]
867 #[inline(always)]
868 #[track_caller]
869 #[cfg(not(feature = "ferrocene_certified"))]
870 pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
871 assert_unsafe_precondition!(
872 check_language_ub,
873 concat!(stringify!($SelfT), "::unchecked_mul cannot overflow"),
874 (
875 lhs: $SelfT = self,
876 rhs: $SelfT = rhs,
877 ) => !lhs.overflowing_mul(rhs).1,
878 );
879
880 unsafe {
882 intrinsics::unchecked_mul(self, rhs)
883 }
884 }
885
886 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div(-1), Some(", stringify!($Max), "));")]
893 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div(-1), None);")]
894 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div(0), None);")]
895 #[stable(feature = "rust1", since = "1.0.0")]
897 #[rustc_const_stable(feature = "const_checked_int_div", since = "1.52.0")]
898 #[must_use = "this returns the result of the operation, \
899 without modifying the original"]
900 #[inline]
901 #[cfg(not(feature = "ferrocene_certified"))]
902 pub const fn checked_div(self, rhs: Self) -> Option<Self> {
903 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
904 None
905 } else {
906 Some(unsafe { intrinsics::unchecked_div(self, rhs) })
908 }
909 }
910
911 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).strict_div(-1), ", stringify!($Max), ");")]
930 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_div(-1);")]
936 #[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div(0);")]
942 #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
944 #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
945 #[must_use = "this returns the result of the operation, \
946 without modifying the original"]
947 #[inline]
948 #[track_caller]
949 #[cfg(not(feature = "ferrocene_certified"))]
950 pub const fn strict_div(self, rhs: Self) -> Self {
951 let (a, b) = self.overflowing_div(rhs);
952 if b { overflow_panic::div() } else { a }
953 }
954
955 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div_euclid(-1), Some(", stringify!($Max), "));")]
962 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div_euclid(-1), None);")]
963 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div_euclid(0), None);")]
964 #[stable(feature = "euclidean_division", since = "1.38.0")]
966 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
967 #[must_use = "this returns the result of the operation, \
968 without modifying the original"]
969 #[inline]
970 #[cfg(not(feature = "ferrocene_certified"))]
971 pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
972 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
974 None
975 } else {
976 Some(self.div_euclid(rhs))
977 }
978 }
979
980 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).strict_div_euclid(-1), ", stringify!($Max), ");")]
999 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_div_euclid(-1);")]
1005 #[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div_euclid(0);")]
1011 #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1013 #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1014 #[must_use = "this returns the result of the operation, \
1015 without modifying the original"]
1016 #[inline]
1017 #[track_caller]
1018 #[cfg(not(feature = "ferrocene_certified"))]
1019 pub const fn strict_div_euclid(self, rhs: Self) -> Self {
1020 let (a, b) = self.overflowing_div_euclid(rhs);
1021 if b { overflow_panic::div() } else { a }
1022 }
1023
1024 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_exact_div(-1), Some(", stringify!($Max), "));")]
1033 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_exact_div(2), None);")]
1034 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_exact_div(-1), None);")]
1035 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_exact_div(0), None);")]
1036 #[unstable(
1038 feature = "exact_div",
1039 issue = "139911",
1040 )]
1041 #[must_use = "this returns the result of the operation, \
1042 without modifying the original"]
1043 #[inline]
1044 #[cfg(not(feature = "ferrocene_certified"))]
1045 pub const fn checked_exact_div(self, rhs: Self) -> Option<Self> {
1046 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
1047 None
1048 } else {
1049 unsafe {
1051 if intrinsics::unlikely(intrinsics::unchecked_rem(self, rhs) != 0) {
1052 None
1053 } else {
1054 Some(intrinsics::exact_div(self, rhs))
1055 }
1056 }
1057 }
1058 }
1059
1060 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".exact_div(2), 32);")]
1072 #[doc = concat!("assert_eq!(64", stringify!($SelfT), ".exact_div(32), 2);")]
1073 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).exact_div(-1), ", stringify!($Max), ");")]
1074 #[doc = concat!("let _ = 65", stringify!($SelfT), ".exact_div(2);")]
1079 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.exact_div(-1);")]
1083 #[unstable(
1085 feature = "exact_div",
1086 issue = "139911",
1087 )]
1088 #[must_use = "this returns the result of the operation, \
1089 without modifying the original"]
1090 #[inline]
1091 #[cfg(not(feature = "ferrocene_certified"))]
1092 pub const fn exact_div(self, rhs: Self) -> Self {
1093 match self.checked_exact_div(rhs) {
1094 Some(x) => x,
1095 None => panic!("Failed to divide without remainder"),
1096 }
1097 }
1098
1099 #[doc = concat!("`self == ", stringify!($SelfT), "::MIN && rhs == -1`,")]
1105 #[unstable(
1107 feature = "exact_div",
1108 issue = "139911",
1109 )]
1110 #[must_use = "this returns the result of the operation, \
1111 without modifying the original"]
1112 #[inline]
1113 #[cfg(not(feature = "ferrocene_certified"))]
1114 pub const unsafe fn unchecked_exact_div(self, rhs: Self) -> Self {
1115 assert_unsafe_precondition!(
1116 check_language_ub,
1117 concat!(stringify!($SelfT), "::unchecked_exact_div cannot overflow, divide by zero, or leave a remainder"),
1118 (
1119 lhs: $SelfT = self,
1120 rhs: $SelfT = rhs,
1121 ) => rhs > 0 && lhs % rhs == 0 && (lhs != <$SelfT>::MIN || rhs != -1),
1122 );
1123 unsafe { intrinsics::exact_div(self, rhs) }
1125 }
1126
1127 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(2), Some(1));")]
1134 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(0), None);")]
1135 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem(-1), None);")]
1136 #[stable(feature = "wrapping", since = "1.7.0")]
1138 #[rustc_const_stable(feature = "const_checked_int_div", since = "1.52.0")]
1139 #[must_use = "this returns the result of the operation, \
1140 without modifying the original"]
1141 #[inline]
1142 #[cfg(not(feature = "ferrocene_certified"))]
1143 pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
1144 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
1145 None
1146 } else {
1147 Some(unsafe { intrinsics::unchecked_rem(self, rhs) })
1149 }
1150 }
1151
1152 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_rem(2), 1);")]
1170 #[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem(0);")]
1176 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_rem(-1);")]
1182 #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1184 #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1185 #[must_use = "this returns the result of the operation, \
1186 without modifying the original"]
1187 #[inline]
1188 #[track_caller]
1189 #[cfg(not(feature = "ferrocene_certified"))]
1190 pub const fn strict_rem(self, rhs: Self) -> Self {
1191 let (a, b) = self.overflowing_rem(rhs);
1192 if b { overflow_panic::rem() } else { a }
1193 }
1194
1195 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(2), Some(1));")]
1202 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(0), None);")]
1203 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem_euclid(-1), None);")]
1204 #[stable(feature = "euclidean_division", since = "1.38.0")]
1206 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1207 #[must_use = "this returns the result of the operation, \
1208 without modifying the original"]
1209 #[inline]
1210 #[cfg(not(feature = "ferrocene_certified"))]
1211 pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
1212 if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
1214 None
1215 } else {
1216 Some(self.rem_euclid(rhs))
1217 }
1218 }
1219
1220 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_rem_euclid(2), 1);")]
1238 #[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem_euclid(0);")]
1244 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_rem_euclid(-1);")]
1250 #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1252 #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1253 #[must_use = "this returns the result of the operation, \
1254 without modifying the original"]
1255 #[inline]
1256 #[track_caller]
1257 #[cfg(not(feature = "ferrocene_certified"))]
1258 pub const fn strict_rem_euclid(self, rhs: Self) -> Self {
1259 let (a, b) = self.overflowing_rem_euclid(rhs);
1260 if b { overflow_panic::rem() } else { a }
1261 }
1262
1263 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_neg(), Some(-5));")]
1269 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_neg(), None);")]
1270 #[stable(feature = "wrapping", since = "1.7.0")]
1272 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1273 #[must_use = "this returns the result of the operation, \
1274 without modifying the original"]
1275 #[inline]
1276 #[cfg(not(feature = "ferrocene_certified"))]
1277 pub const fn checked_neg(self) -> Option<Self> {
1278 let (a, b) = self.overflowing_neg();
1279 if intrinsics::unlikely(b) { None } else { Some(a) }
1280 }
1281
1282 #[doc = concat!("`self == ", stringify!($SelfT), "::MIN`,")]
1288 #[doc = concat!("[`checked_neg`]: ", stringify!($SelfT), "::checked_neg")]
1291 #[unstable(
1292 feature = "unchecked_neg",
1293 reason = "niche optimization path",
1294 issue = "85122",
1295 )]
1296 #[must_use = "this returns the result of the operation, \
1297 without modifying the original"]
1298 #[inline(always)]
1299 #[track_caller]
1300 #[cfg(not(feature = "ferrocene_certified"))]
1301 pub const unsafe fn unchecked_neg(self) -> Self {
1302 assert_unsafe_precondition!(
1303 check_language_ub,
1304 concat!(stringify!($SelfT), "::unchecked_neg cannot overflow"),
1305 (
1306 lhs: $SelfT = self,
1307 ) => !lhs.overflowing_neg().1,
1308 );
1309
1310 unsafe {
1312 intrinsics::unchecked_sub(0, self)
1313 }
1314 }
1315
1316 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_neg(), -5);")]
1328 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_neg();")]
1334 #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1336 #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1337 #[must_use = "this returns the result of the operation, \
1338 without modifying the original"]
1339 #[inline]
1340 #[track_caller]
1341 #[cfg(not(feature = "ferrocene_certified"))]
1342 pub const fn strict_neg(self) -> Self {
1343 let (a, b) = self.overflowing_neg();
1344 if b { overflow_panic::neg() } else { a }
1345 }
1346
1347 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(4), Some(0x10));")]
1354 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(129), None);")]
1355 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shl(", stringify!($BITS_MINUS_ONE), "), Some(0));")]
1356 #[stable(feature = "wrapping", since = "1.7.0")]
1358 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1359 #[must_use = "this returns the result of the operation, \
1360 without modifying the original"]
1361 #[inline]
1362 #[cfg(not(feature = "ferrocene_certified"))]
1363 pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
1364 if rhs < Self::BITS {
1366 Some(unsafe { self.unchecked_shl(rhs) })
1368 } else {
1369 None
1370 }
1371 }
1372
1373 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".strict_shl(4), 0x10);")]
1386 #[doc = concat!("let _ = 0x1", stringify!($SelfT), ".strict_shl(129);")]
1392 #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1394 #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1395 #[must_use = "this returns the result of the operation, \
1396 without modifying the original"]
1397 #[inline]
1398 #[track_caller]
1399 #[cfg(not(feature = "ferrocene_certified"))]
1400 pub const fn strict_shl(self, rhs: u32) -> Self {
1401 let (a, b) = self.overflowing_shl(rhs);
1402 if b { overflow_panic::shl() } else { a }
1403 }
1404
1405 #[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
1415 #[unstable(
1416 feature = "unchecked_shifts",
1417 reason = "niche optimization path",
1418 issue = "85122",
1419 )]
1420 #[must_use = "this returns the result of the operation, \
1421 without modifying the original"]
1422 #[inline(always)]
1423 #[track_caller]
1424 #[cfg(not(feature = "ferrocene_certified"))]
1425 pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
1426 assert_unsafe_precondition!(
1427 check_language_ub,
1428 concat!(stringify!($SelfT), "::unchecked_shl cannot overflow"),
1429 (
1430 rhs: u32 = rhs,
1431 ) => rhs < <$ActualT>::BITS,
1432 );
1433
1434 unsafe {
1436 intrinsics::unchecked_shl(self, rhs)
1437 }
1438 }
1439
1440 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".unbounded_shl(4), 0x10);")]
1449 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".unbounded_shl(129), 0);")]
1450 #[stable(feature = "unbounded_shifts", since = "1.87.0")]
1452 #[rustc_const_stable(feature = "unbounded_shifts", since = "1.87.0")]
1453 #[must_use = "this returns the result of the operation, \
1454 without modifying the original"]
1455 #[inline]
1456 #[cfg(not(feature = "ferrocene_certified"))]
1457 pub const fn unbounded_shl(self, rhs: u32) -> $SelfT{
1458 if rhs < Self::BITS {
1459 unsafe { self.unchecked_shl(rhs) }
1462 } else {
1463 0
1464 }
1465 }
1466
1467 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(4), Some(0x1));")]
1474 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(128), None);")]
1475 #[stable(feature = "wrapping", since = "1.7.0")]
1477 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1478 #[must_use = "this returns the result of the operation, \
1479 without modifying the original"]
1480 #[inline]
1481 #[cfg(not(feature = "ferrocene_certified"))]
1482 pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
1483 if rhs < Self::BITS {
1485 Some(unsafe { self.unchecked_shr(rhs) })
1487 } else {
1488 None
1489 }
1490 }
1491
1492 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".strict_shr(4), 0x1);")]
1505 #[doc = concat!("let _ = 0x10", stringify!($SelfT), ".strict_shr(128);")]
1511 #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1513 #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1514 #[must_use = "this returns the result of the operation, \
1515 without modifying the original"]
1516 #[inline]
1517 #[track_caller]
1518 #[cfg(not(feature = "ferrocene_certified"))]
1519 pub const fn strict_shr(self, rhs: u32) -> Self {
1520 let (a, b) = self.overflowing_shr(rhs);
1521 if b { overflow_panic::shr() } else { a }
1522 }
1523
1524 #[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
1534 #[unstable(
1535 feature = "unchecked_shifts",
1536 reason = "niche optimization path",
1537 issue = "85122",
1538 )]
1539 #[must_use = "this returns the result of the operation, \
1540 without modifying the original"]
1541 #[inline(always)]
1542 #[track_caller]
1543 #[cfg(not(feature = "ferrocene_certified"))]
1544 pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
1545 assert_unsafe_precondition!(
1546 check_language_ub,
1547 concat!(stringify!($SelfT), "::unchecked_shr cannot overflow"),
1548 (
1549 rhs: u32 = rhs,
1550 ) => rhs < <$ActualT>::BITS,
1551 );
1552
1553 unsafe {
1555 intrinsics::unchecked_shr(self, rhs)
1556 }
1557 }
1558
1559 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".unbounded_shr(4), 0x1);")]
1569 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".unbounded_shr(129), 0);")]
1570 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.unbounded_shr(129), -1);")]
1571 #[stable(feature = "unbounded_shifts", since = "1.87.0")]
1573 #[rustc_const_stable(feature = "unbounded_shifts", since = "1.87.0")]
1574 #[must_use = "this returns the result of the operation, \
1575 without modifying the original"]
1576 #[inline]
1577 #[cfg(not(feature = "ferrocene_certified"))]
1578 pub const fn unbounded_shr(self, rhs: u32) -> $SelfT{
1579 if rhs < Self::BITS {
1580 unsafe { self.unchecked_shr(rhs) }
1583 } else {
1584 unsafe { self.unchecked_shr(Self::BITS - 1) }
1589 }
1590 }
1591
1592 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_abs(), Some(5));")]
1599 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_abs(), None);")]
1600 #[stable(feature = "no_panic_abs", since = "1.13.0")]
1602 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1603 #[must_use = "this returns the result of the operation, \
1604 without modifying the original"]
1605 #[inline]
1606 #[cfg(not(feature = "ferrocene_certified"))]
1607 pub const fn checked_abs(self) -> Option<Self> {
1608 if self.is_negative() {
1609 self.checked_neg()
1610 } else {
1611 Some(self)
1612 }
1613 }
1614
1615 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").strict_abs(), 5);")]
1628 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_abs();")]
1634 #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1636 #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1637 #[must_use = "this returns the result of the operation, \
1638 without modifying the original"]
1639 #[inline]
1640 #[track_caller]
1641 #[cfg(not(feature = "ferrocene_certified"))]
1642 pub const fn strict_abs(self) -> Self {
1643 if self.is_negative() {
1644 self.strict_neg()
1645 } else {
1646 self
1647 }
1648 }
1649
1650 #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".checked_pow(2), Some(64));")]
1657 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);")]
1658 #[stable(feature = "no_panic_pow", since = "1.34.0")]
1661 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1662 #[must_use = "this returns the result of the operation, \
1663 without modifying the original"]
1664 #[inline]
1665 #[cfg(not(feature = "ferrocene_certified"))]
1666 pub const fn checked_pow(self, mut exp: u32) -> Option<Self> {
1667 if exp == 0 {
1668 return Some(1);
1669 }
1670 let mut base = self;
1671 let mut acc: Self = 1;
1672
1673 loop {
1674 if (exp & 1) == 1 {
1675 acc = try_opt!(acc.checked_mul(base));
1676 if exp == 1 {
1678 return Some(acc);
1679 }
1680 }
1681 exp /= 2;
1682 base = try_opt!(base.checked_mul(base));
1683 }
1684 }
1685
1686 #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".strict_pow(2), 64);")]
1699 #[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_pow(2);")]
1705 #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1707 #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1708 #[must_use = "this returns the result of the operation, \
1709 without modifying the original"]
1710 #[inline]
1711 #[track_caller]
1712 #[cfg(not(feature = "ferrocene_certified"))]
1713 pub const fn strict_pow(self, mut exp: u32) -> Self {
1714 if exp == 0 {
1715 return 1;
1716 }
1717 let mut base = self;
1718 let mut acc: Self = 1;
1719
1720 loop {
1721 if (exp & 1) == 1 {
1722 acc = acc.strict_mul(base);
1723 if exp == 1 {
1725 return acc;
1726 }
1727 }
1728 exp /= 2;
1729 base = base.strict_mul(base);
1730 }
1731 }
1732
1733 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_isqrt(), Some(3));")]
1741 #[stable(feature = "isqrt", since = "1.84.0")]
1743 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
1744 #[must_use = "this returns the result of the operation, \
1745 without modifying the original"]
1746 #[inline]
1747 #[cfg(not(feature = "ferrocene_certified"))]
1748 pub const fn checked_isqrt(self) -> Option<Self> {
1749 if self < 0 {
1750 None
1751 } else {
1752 let result = unsafe {
1754 crate::num::int_sqrt::$ActualT(self as $ActualT) as $SelfT
1755 };
1756
1757 unsafe {
1769 const MAX_RESULT: $SelfT = unsafe {
1771 crate::num::int_sqrt::$ActualT(<$ActualT>::MAX) as $SelfT
1772 };
1773
1774 crate::hint::assert_unchecked(result >= 0);
1775 crate::hint::assert_unchecked(result <= MAX_RESULT);
1776 }
1777
1778 Some(result)
1779 }
1780 }
1781
1782 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_add(1), 101);")]
1789 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add(100), ", stringify!($SelfT), "::MAX);")]
1790 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_add(-1), ", stringify!($SelfT), "::MIN);")]
1791 #[stable(feature = "rust1", since = "1.0.0")]
1794 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1795 #[must_use = "this returns the result of the operation, \
1796 without modifying the original"]
1797 #[inline(always)]
1798 #[cfg(not(feature = "ferrocene_certified"))]
1799 pub const fn saturating_add(self, rhs: Self) -> Self {
1800 intrinsics::saturating_add(self, rhs)
1801 }
1802
1803 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".saturating_add_unsigned(2), 3);")]
1810 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add_unsigned(100), ", stringify!($SelfT), "::MAX);")]
1811 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1813 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1814 #[must_use = "this returns the result of the operation, \
1815 without modifying the original"]
1816 #[inline]
1817 #[cfg(not(feature = "ferrocene_certified"))]
1818 pub const fn saturating_add_unsigned(self, rhs: $UnsignedT) -> Self {
1819 match self.checked_add_unsigned(rhs) {
1822 Some(x) => x,
1823 None => Self::MAX,
1824 }
1825 }
1826
1827 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub(127), -27);")]
1834 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub(100), ", stringify!($SelfT), "::MIN);")]
1835 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_sub(-1), ", stringify!($SelfT), "::MAX);")]
1836 #[stable(feature = "rust1", since = "1.0.0")]
1838 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1839 #[must_use = "this returns the result of the operation, \
1840 without modifying the original"]
1841 #[inline(always)]
1842 #[cfg(not(feature = "ferrocene_certified"))]
1843 pub const fn saturating_sub(self, rhs: Self) -> Self {
1844 intrinsics::saturating_sub(self, rhs)
1845 }
1846
1847 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub_unsigned(127), -27);")]
1854 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub_unsigned(100), ", stringify!($SelfT), "::MIN);")]
1855 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1857 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1858 #[must_use = "this returns the result of the operation, \
1859 without modifying the original"]
1860 #[inline]
1861 #[cfg(not(feature = "ferrocene_certified"))]
1862 pub const fn saturating_sub_unsigned(self, rhs: $UnsignedT) -> Self {
1863 match self.checked_sub_unsigned(rhs) {
1866 Some(x) => x,
1867 None => Self::MIN,
1868 }
1869 }
1870
1871 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_neg(), -100);")]
1878 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_neg(), 100);")]
1879 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_neg(), ", stringify!($SelfT), "::MAX);")]
1880 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_neg(), ", stringify!($SelfT), "::MIN + 1);")]
1881 #[stable(feature = "saturating_neg", since = "1.45.0")]
1884 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1885 #[must_use = "this returns the result of the operation, \
1886 without modifying the original"]
1887 #[inline(always)]
1888 #[cfg(not(feature = "ferrocene_certified"))]
1889 pub const fn saturating_neg(self) -> Self {
1890 intrinsics::saturating_sub(0, self)
1891 }
1892
1893 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_abs(), 100);")]
1900 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_abs(), 100);")]
1901 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_abs(), ", stringify!($SelfT), "::MAX);")]
1902 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).saturating_abs(), ", stringify!($SelfT), "::MAX);")]
1903 #[stable(feature = "saturating_neg", since = "1.45.0")]
1906 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1907 #[must_use = "this returns the result of the operation, \
1908 without modifying the original"]
1909 #[inline]
1910 #[cfg(not(feature = "ferrocene_certified"))]
1911 pub const fn saturating_abs(self) -> Self {
1912 if self.is_negative() {
1913 self.saturating_neg()
1914 } else {
1915 self
1916 }
1917 }
1918
1919 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".saturating_mul(12), 120);")]
1926 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_mul(10), ", stringify!($SelfT), "::MAX);")]
1927 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_mul(10), ", stringify!($SelfT), "::MIN);")]
1928 #[stable(feature = "wrapping", since = "1.7.0")]
1930 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1931 #[must_use = "this returns the result of the operation, \
1932 without modifying the original"]
1933 #[inline]
1934 #[cfg(not(feature = "ferrocene_certified"))]
1935 pub const fn saturating_mul(self, rhs: Self) -> Self {
1936 match self.checked_mul(rhs) {
1937 Some(x) => x,
1938 None => if (self < 0) == (rhs < 0) {
1939 Self::MAX
1940 } else {
1941 Self::MIN
1942 }
1943 }
1944 }
1945
1946 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
1957 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_div(-1), ", stringify!($SelfT), "::MIN + 1);")]
1958 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_div(-1), ", stringify!($SelfT), "::MAX);")]
1959 #[stable(feature = "saturating_div", since = "1.58.0")]
1962 #[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
1963 #[must_use = "this returns the result of the operation, \
1964 without modifying the original"]
1965 #[inline]
1966 #[cfg(not(feature = "ferrocene_certified"))]
1967 pub const fn saturating_div(self, rhs: Self) -> Self {
1968 match self.overflowing_div(rhs) {
1969 (result, false) => result,
1970 (_result, true) => Self::MAX, }
1972 }
1973
1974 #[doc = concat!("assert_eq!((-4", stringify!($SelfT), ").saturating_pow(3), -64);")]
1981 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(2), ", stringify!($SelfT), "::MAX);")]
1982 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(3), ", stringify!($SelfT), "::MIN);")]
1983 #[stable(feature = "no_panic_pow", since = "1.34.0")]
1985 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1986 #[must_use = "this returns the result of the operation, \
1987 without modifying the original"]
1988 #[inline]
1989 #[cfg(not(feature = "ferrocene_certified"))]
1990 pub const fn saturating_pow(self, exp: u32) -> Self {
1991 match self.checked_pow(exp) {
1992 Some(x) => x,
1993 None if self < 0 && exp % 2 == 1 => Self::MIN,
1994 None => Self::MAX,
1995 }
1996 }
1997
1998 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add(27), 127);")]
2005 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add(2), ", stringify!($SelfT), "::MIN + 1);")]
2006 #[stable(feature = "rust1", since = "1.0.0")]
2008 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2009 #[must_use = "this returns the result of the operation, \
2010 without modifying the original"]
2011 #[inline(always)]
2012 #[cfg(not(feature = "ferrocene_certified"))]
2013 pub const fn wrapping_add(self, rhs: Self) -> Self {
2014 intrinsics::wrapping_add(self, rhs)
2015 }
2016
2017 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add_unsigned(27), 127);")]
2024 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add_unsigned(2), ", stringify!($SelfT), "::MIN + 1);")]
2025 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2027 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2028 #[must_use = "this returns the result of the operation, \
2029 without modifying the original"]
2030 #[inline(always)]
2031 #[cfg(not(feature = "ferrocene_certified"))]
2032 pub const fn wrapping_add_unsigned(self, rhs: $UnsignedT) -> Self {
2033 self.wrapping_add(rhs as Self)
2034 }
2035
2036 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub(127), -127);")]
2043 #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub(", stringify!($SelfT), "::MAX), ", stringify!($SelfT), "::MAX);")]
2044 #[stable(feature = "rust1", since = "1.0.0")]
2046 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2047 #[must_use = "this returns the result of the operation, \
2048 without modifying the original"]
2049 #[inline(always)]
2050 #[cfg(not(feature = "ferrocene_certified"))]
2051 pub const fn wrapping_sub(self, rhs: Self) -> Self {
2052 intrinsics::wrapping_sub(self, rhs)
2053 }
2054
2055 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub_unsigned(127), -127);")]
2062 #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub_unsigned(", stringify!($UnsignedT), "::MAX), -1);")]
2063 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2065 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2066 #[must_use = "this returns the result of the operation, \
2067 without modifying the original"]
2068 #[inline(always)]
2069 #[cfg(not(feature = "ferrocene_certified"))]
2070 pub const fn wrapping_sub_unsigned(self, rhs: $UnsignedT) -> Self {
2071 self.wrapping_sub(rhs as Self)
2072 }
2073
2074 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".wrapping_mul(12), 120);")]
2081 #[stable(feature = "rust1", since = "1.0.0")]
2084 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2085 #[must_use = "this returns the result of the operation, \
2086 without modifying the original"]
2087 #[inline(always)]
2088 #[cfg(not(feature = "ferrocene_certified"))]
2089 pub const fn wrapping_mul(self, rhs: Self) -> Self {
2090 intrinsics::wrapping_mul(self, rhs)
2091 }
2092
2093 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div(10), 10);")]
2108 #[stable(feature = "num_wrapping", since = "1.2.0")]
2111 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
2112 #[must_use = "this returns the result of the operation, \
2113 without modifying the original"]
2114 #[inline]
2115 #[cfg(not(feature = "ferrocene_certified"))]
2116 pub const fn wrapping_div(self, rhs: Self) -> Self {
2117 self.overflowing_div(rhs).0
2118 }
2119
2120 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div_euclid(10), 10);")]
2135 #[stable(feature = "euclidean_division", since = "1.38.0")]
2138 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2139 #[must_use = "this returns the result of the operation, \
2140 without modifying the original"]
2141 #[inline]
2142 #[cfg(not(feature = "ferrocene_certified"))]
2143 pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
2144 self.overflowing_div_euclid(rhs).0
2145 }
2146
2147 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem(10), 0);")]
2162 #[stable(feature = "num_wrapping", since = "1.2.0")]
2165 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
2166 #[must_use = "this returns the result of the operation, \
2167 without modifying the original"]
2168 #[inline]
2169 #[cfg(not(feature = "ferrocene_certified"))]
2170 pub const fn wrapping_rem(self, rhs: Self) -> Self {
2171 self.overflowing_rem(rhs).0
2172 }
2173
2174 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);")]
2188 #[stable(feature = "euclidean_division", since = "1.38.0")]
2191 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2192 #[must_use = "this returns the result of the operation, \
2193 without modifying the original"]
2194 #[inline]
2195 #[cfg(not(feature = "ferrocene_certified"))]
2196 pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
2197 self.overflowing_rem_euclid(rhs).0
2198 }
2199
2200 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_neg(), -100);")]
2211 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_neg(), 100);")]
2212 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_neg(), ", stringify!($SelfT), "::MIN);")]
2213 #[stable(feature = "num_wrapping", since = "1.2.0")]
2215 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2216 #[must_use = "this returns the result of the operation, \
2217 without modifying the original"]
2218 #[inline(always)]
2219 #[cfg(not(feature = "ferrocene_certified"))]
2220 pub const fn wrapping_neg(self) -> Self {
2221 (0 as $SelfT).wrapping_sub(self)
2222 }
2223
2224 #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(7), -128);")]
2236 #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(128), -1);")]
2237 #[stable(feature = "num_wrapping", since = "1.2.0")]
2239 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2240 #[must_use = "this returns the result of the operation, \
2241 without modifying the original"]
2242 #[inline(always)]
2243 #[cfg(not(feature = "ferrocene_certified"))]
2244 pub const fn wrapping_shl(self, rhs: u32) -> Self {
2245 unsafe {
2248 self.unchecked_shl(rhs & (Self::BITS - 1))
2249 }
2250 }
2251
2252 #[doc = concat!("assert_eq!((-128", stringify!($SelfT), ").wrapping_shr(7), -1);")]
2264 #[stable(feature = "num_wrapping", since = "1.2.0")]
2267 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2268 #[must_use = "this returns the result of the operation, \
2269 without modifying the original"]
2270 #[inline(always)]
2271 #[cfg(not(feature = "ferrocene_certified"))]
2272 pub const fn wrapping_shr(self, rhs: u32) -> Self {
2273 unsafe {
2276 self.unchecked_shr(rhs & (Self::BITS - 1))
2277 }
2278 }
2279
2280 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_abs(), 100);")]
2291 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_abs(), 100);")]
2292 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_abs(), ", stringify!($SelfT), "::MIN);")]
2293 #[stable(feature = "no_panic_abs", since = "1.13.0")]
2296 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2297 #[must_use = "this returns the result of the operation, \
2298 without modifying the original"]
2299 #[allow(unused_attributes)]
2300 #[inline]
2301 #[cfg(not(feature = "ferrocene_certified"))]
2302 pub const fn wrapping_abs(self) -> Self {
2303 if self.is_negative() {
2304 self.wrapping_neg()
2305 } else {
2306 self
2307 }
2308 }
2309
2310 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".unsigned_abs(), 100", stringify!($UnsignedT), ");")]
2318 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").unsigned_abs(), 100", stringify!($UnsignedT), ");")]
2319 #[stable(feature = "unsigned_abs", since = "1.51.0")]
2322 #[rustc_const_stable(feature = "unsigned_abs", since = "1.51.0")]
2323 #[must_use = "this returns the result of the operation, \
2324 without modifying the original"]
2325 #[inline]
2326 #[cfg(not(feature = "ferrocene_certified"))]
2327 pub const fn unsigned_abs(self) -> $UnsignedT {
2328 self.wrapping_abs() as $UnsignedT
2329 }
2330
2331 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".wrapping_pow(4), 81);")]
2338 #[stable(feature = "no_panic_pow", since = "1.34.0")]
2342 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2343 #[must_use = "this returns the result of the operation, \
2344 without modifying the original"]
2345 #[inline]
2346 #[cfg(not(feature = "ferrocene_certified"))]
2347 pub const fn wrapping_pow(self, mut exp: u32) -> Self {
2348 if exp == 0 {
2349 return 1;
2350 }
2351 let mut base = self;
2352 let mut acc: Self = 1;
2353
2354 if intrinsics::is_val_statically_known(exp) {
2355 while exp > 1 {
2356 if (exp & 1) == 1 {
2357 acc = acc.wrapping_mul(base);
2358 }
2359 exp /= 2;
2360 base = base.wrapping_mul(base);
2361 }
2362
2363 acc.wrapping_mul(base)
2367 } else {
2368 loop {
2373 if (exp & 1) == 1 {
2374 acc = acc.wrapping_mul(base);
2375 if exp == 1 {
2377 return acc;
2378 }
2379 }
2380 exp /= 2;
2381 base = base.wrapping_mul(base);
2382 }
2383 }
2384 }
2385
2386 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_add(2), (7, false));")]
2396 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.overflowing_add(1), (", stringify!($SelfT), "::MIN, true));")]
2397 #[stable(feature = "wrapping", since = "1.7.0")]
2399 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2400 #[must_use = "this returns the result of the operation, \
2401 without modifying the original"]
2402 #[inline(always)]
2403 #[cfg(not(feature = "ferrocene_certified"))]
2404 pub const fn overflowing_add(self, rhs: Self) -> (Self, bool) {
2405 let (a, b) = intrinsics::add_with_overflow(self as $ActualT, rhs as $ActualT);
2406 (a as Self, b)
2407 }
2408
2409 #[doc = concat!("[`", stringify!($UnsignedT), "::carrying_add`]")]
2421 #[doc = concat!("// 10 MAX (a = 10 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")]
2436 #[doc = concat!("// + -5 9 (b = -5 × 2^", stringify!($BITS), " + 9)")]
2437 #[doc = concat!("// 6 8 (sum = 6 × 2^", stringify!($BITS), " + 8)")]
2439 #[doc = concat!("let (a1, a0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (10, ", stringify!($UnsignedT), "::MAX);")]
2441 #[doc = concat!("let (b1, b0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (-5, 9);")]
2442 #[doc = concat!("// ", stringify!($UnsignedT), "::carrying_add for the less significant words")]
2445 #[doc = concat!("// ", stringify!($SelfT), "::carrying_add for the most significant word")]
2449 #[unstable(feature = "bigint_helper_methods", issue = "85532")]
2455 #[must_use = "this returns the result of the operation, \
2456 without modifying the original"]
2457 #[inline]
2458 #[cfg(not(feature = "ferrocene_certified"))]
2459 pub const fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool) {
2460 let (a, b) = self.overflowing_add(rhs);
2463 let (c, d) = a.overflowing_add(carry as $SelfT);
2464 (c, b != d)
2465 }
2466
2467 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_add_unsigned(2), (3, false));")]
2477 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_add_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MAX, false));")]
2478 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).overflowing_add_unsigned(3), (", stringify!($SelfT), "::MIN, true));")]
2479 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2481 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2482 #[must_use = "this returns the result of the operation, \
2483 without modifying the original"]
2484 #[inline]
2485 #[cfg(not(feature = "ferrocene_certified"))]
2486 pub const fn overflowing_add_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
2487 let rhs = rhs as Self;
2488 let (res, overflowed) = self.overflowing_add(rhs);
2489 (res, overflowed ^ (rhs < 0))
2490 }
2491
2492 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_sub(2), (3, false));")]
2501 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_sub(1), (", stringify!($SelfT), "::MAX, true));")]
2502 #[stable(feature = "wrapping", since = "1.7.0")]
2504 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2505 #[must_use = "this returns the result of the operation, \
2506 without modifying the original"]
2507 #[inline(always)]
2508 #[cfg(not(feature = "ferrocene_certified"))]
2509 pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
2510 let (a, b) = intrinsics::sub_with_overflow(self as $ActualT, rhs as $ActualT);
2511 (a as Self, b)
2512 }
2513
2514 #[doc = concat!("[`", stringify!($UnsignedT), "::borrowing_sub`]")]
2527 #[doc = concat!("// 6 8 (a = 6 × 2^", stringify!($BITS), " + 8)")]
2542 #[doc = concat!("// - -5 9 (b = -5 × 2^", stringify!($BITS), " + 9)")]
2543 #[doc = concat!("// 10 MAX (diff = 10 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")]
2545 #[doc = concat!("let (a1, a0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (6, 8);")]
2547 #[doc = concat!("let (b1, b0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (-5, 9);")]
2548 #[doc = concat!("// ", stringify!($UnsignedT), "::borrowing_sub for the less significant words")]
2551 #[doc = concat!("// ", stringify!($SelfT), "::borrowing_sub for the most significant word")]
2555 #[doc = concat!("assert_eq!((diff1, diff0), (10, ", stringify!($UnsignedT), "::MAX));")]
2559 #[unstable(feature = "bigint_helper_methods", issue = "85532")]
2561 #[must_use = "this returns the result of the operation, \
2562 without modifying the original"]
2563 #[inline]
2564 #[cfg(not(feature = "ferrocene_certified"))]
2565 pub const fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
2566 let (a, b) = self.overflowing_sub(rhs);
2569 let (c, d) = a.overflowing_sub(borrow as $SelfT);
2570 (c, b != d)
2571 }
2572
2573 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_sub_unsigned(2), (-1, false));")]
2583 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX).overflowing_sub_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MIN, false));")]
2584 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).overflowing_sub_unsigned(3), (", stringify!($SelfT), "::MAX, true));")]
2585 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2587 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2588 #[must_use = "this returns the result of the operation, \
2589 without modifying the original"]
2590 #[inline]
2591 #[cfg(not(feature = "ferrocene_certified"))]
2592 pub const fn overflowing_sub_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
2593 let rhs = rhs as Self;
2594 let (res, overflowed) = self.overflowing_sub(rhs);
2595 (res, overflowed ^ (rhs < 0))
2596 }
2597
2598 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_mul(2), (10, false));")]
2607 #[stable(feature = "wrapping", since = "1.7.0")]
2610 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2611 #[must_use = "this returns the result of the operation, \
2612 without modifying the original"]
2613 #[inline(always)]
2614 #[cfg(not(feature = "ferrocene_certified"))]
2615 pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
2616 let (a, b) = intrinsics::mul_with_overflow(self as $ActualT, rhs as $ActualT);
2617 (a as Self, b)
2618 }
2619
2620 #[unstable(feature = "bigint_helper_methods", issue = "85532")]
2638 #[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
2639 #[must_use = "this returns the result of the operation, \
2640 without modifying the original"]
2641 #[inline]
2642 #[cfg(not(feature = "ferrocene_certified"))]
2643 pub const fn widening_mul(self, rhs: Self) -> ($UnsignedT, Self) {
2644 Self::carrying_mul_add(self, rhs, 0, 0)
2645 }
2646
2647 #[doc = concat!("assert_eq!(",
2670 stringify!($SelfT), "::MAX.carrying_mul(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
2671 "(", stringify!($SelfT), "::MAX.unsigned_abs() + 1, ", stringify!($SelfT), "::MAX / 2));"
2672 )]
2673 #[unstable(feature = "bigint_helper_methods", issue = "85532")]
2675 #[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
2676 #[must_use = "this returns the result of the operation, \
2677 without modifying the original"]
2678 #[inline]
2679 #[cfg(not(feature = "ferrocene_certified"))]
2680 pub const fn carrying_mul(self, rhs: Self, carry: Self) -> ($UnsignedT, Self) {
2681 Self::carrying_mul_add(self, rhs, carry, 0)
2682 }
2683
2684 #[doc = concat!("assert_eq!(",
2708 stringify!($SelfT), "::MAX.carrying_mul_add(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
2709 "(", stringify!($UnsignedT), "::MAX, ", stringify!($SelfT), "::MAX / 2));"
2710 )]
2711 #[unstable(feature = "bigint_helper_methods", issue = "85532")]
2713 #[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
2714 #[must_use = "this returns the result of the operation, \
2715 without modifying the original"]
2716 #[inline]
2717 #[cfg(not(feature = "ferrocene_certified"))]
2718 pub const fn carrying_mul_add(self, rhs: Self, carry: Self, add: Self) -> ($UnsignedT, Self) {
2719 intrinsics::carrying_mul_add(self, rhs, carry, add)
2720 }
2721
2722 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div(2), (2, false));")]
2735 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div(-1), (", stringify!($SelfT), "::MIN, true));")]
2736 #[inline]
2738 #[stable(feature = "wrapping", since = "1.7.0")]
2739 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
2740 #[must_use = "this returns the result of the operation, \
2741 without modifying the original"]
2742 #[cfg(not(feature = "ferrocene_certified"))]
2743 pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
2744 if intrinsics::unlikely((self == Self::MIN) & (rhs == -1)) {
2746 (self, true)
2747 } else {
2748 (self / rhs, false)
2749 }
2750 }
2751
2752 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div_euclid(2), (2, false));")]
2765 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringify!($SelfT), "::MIN, true));")]
2766 #[inline]
2768 #[stable(feature = "euclidean_division", since = "1.38.0")]
2769 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2770 #[must_use = "this returns the result of the operation, \
2771 without modifying the original"]
2772 #[cfg(not(feature = "ferrocene_certified"))]
2773 pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
2774 if intrinsics::unlikely((self == Self::MIN) & (rhs == -1)) {
2776 (self, true)
2777 } else {
2778 (self.div_euclid(rhs), false)
2779 }
2780 }
2781
2782 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem(2), (1, false));")]
2795 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem(-1), (0, true));")]
2796 #[inline]
2798 #[stable(feature = "wrapping", since = "1.7.0")]
2799 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
2800 #[must_use = "this returns the result of the operation, \
2801 without modifying the original"]
2802 #[cfg(not(feature = "ferrocene_certified"))]
2803 pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
2804 if intrinsics::unlikely(rhs == -1) {
2805 (0, self == Self::MIN)
2806 } else {
2807 (self % rhs, false)
2808 }
2809 }
2810
2811
2812 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem_euclid(2), (1, false));")]
2825 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));")]
2826 #[stable(feature = "euclidean_division", since = "1.38.0")]
2828 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2829 #[must_use = "this returns the result of the operation, \
2830 without modifying the original"]
2831 #[inline]
2832 #[track_caller]
2833 #[cfg(not(feature = "ferrocene_certified"))]
2834 pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
2835 if intrinsics::unlikely(rhs == -1) {
2836 (0, self == Self::MIN)
2837 } else {
2838 (self.rem_euclid(rhs), false)
2839 }
2840 }
2841
2842
2843 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2, false));")]
2853 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($SelfT), "::MIN, true));")]
2854 #[inline]
2856 #[stable(feature = "wrapping", since = "1.7.0")]
2857 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2858 #[must_use = "this returns the result of the operation, \
2859 without modifying the original"]
2860 #[allow(unused_attributes)]
2861 #[cfg(not(feature = "ferrocene_certified"))]
2862 pub const fn overflowing_neg(self) -> (Self, bool) {
2863 if intrinsics::unlikely(self == Self::MIN) {
2864 (Self::MIN, true)
2865 } else {
2866 (-self, false)
2867 }
2868 }
2869
2870 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT),".overflowing_shl(4), (0x10, false));")]
2880 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shl(", stringify!($BITS_MINUS_ONE), "), (0, false));")]
2882 #[stable(feature = "wrapping", since = "1.7.0")]
2884 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2885 #[must_use = "this returns the result of the operation, \
2886 without modifying the original"]
2887 #[inline]
2888 #[cfg(not(feature = "ferrocene_certified"))]
2889 pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
2890 (self.wrapping_shl(rhs), rhs >= Self::BITS)
2891 }
2892
2893 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shr(4), (0x1, false));")]
2903 #[stable(feature = "wrapping", since = "1.7.0")]
2906 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2907 #[must_use = "this returns the result of the operation, \
2908 without modifying the original"]
2909 #[inline]
2910 #[cfg(not(feature = "ferrocene_certified"))]
2911 pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
2912 (self.wrapping_shr(rhs), rhs >= Self::BITS)
2913 }
2914
2915 #[doc = concat!("(e.g., ", stringify!($SelfT), "::MIN for values of type ", stringify!($SelfT), "),")]
2920 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".overflowing_abs(), (10, false));")]
2927 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").overflowing_abs(), (10, false));")]
2928 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_abs(), (", stringify!($SelfT), "::MIN, true));")]
2929 #[stable(feature = "no_panic_abs", since = "1.13.0")]
2931 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2932 #[must_use = "this returns the result of the operation, \
2933 without modifying the original"]
2934 #[inline]
2935 #[cfg(not(feature = "ferrocene_certified"))]
2936 pub const fn overflowing_abs(self) -> (Self, bool) {
2937 (self.wrapping_abs(), self == Self::MIN)
2938 }
2939
2940 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".overflowing_pow(4), (81, false));")]
2949 #[stable(feature = "no_panic_pow", since = "1.34.0")]
2952 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2953 #[must_use = "this returns the result of the operation, \
2954 without modifying the original"]
2955 #[inline]
2956 #[cfg(not(feature = "ferrocene_certified"))]
2957 pub const fn overflowing_pow(self, mut exp: u32) -> (Self, bool) {
2958 if exp == 0 {
2959 return (1,false);
2960 }
2961 let mut base = self;
2962 let mut acc: Self = 1;
2963 let mut overflown = false;
2964 let mut r;
2966
2967 loop {
2968 if (exp & 1) == 1 {
2969 r = acc.overflowing_mul(base);
2970 if exp == 1 {
2972 r.1 |= overflown;
2973 return r;
2974 }
2975 acc = r.0;
2976 overflown |= r.1;
2977 }
2978 exp /= 2;
2979 r = base.overflowing_mul(base);
2980 base = r.0;
2981 overflown |= r.1;
2982 }
2983 }
2984
2985 #[doc = concat!("let x: ", stringify!($SelfT), " = 2; // or any other integer type")]
2991 #[stable(feature = "rust1", since = "1.0.0")]
2995 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2996 #[must_use = "this returns the result of the operation, \
2997 without modifying the original"]
2998 #[inline]
2999 #[rustc_inherit_overflow_checks]
3000 #[cfg(not(feature = "ferrocene_certified"))]
3001 pub const fn pow(self, mut exp: u32) -> Self {
3002 if exp == 0 {
3003 return 1;
3004 }
3005 let mut base = self;
3006 let mut acc = 1;
3007
3008 if intrinsics::is_val_statically_known(exp) {
3009 while exp > 1 {
3010 if (exp & 1) == 1 {
3011 acc = acc * base;
3012 }
3013 exp /= 2;
3014 base = base * base;
3015 }
3016
3017 acc * base
3022 } else {
3023 loop {
3028 if (exp & 1) == 1 {
3029 acc = acc * base;
3030 if exp == 1 {
3032 return acc;
3033 }
3034 }
3035 exp /= 2;
3036 base = base * base;
3037 }
3038 }
3039 }
3040
3041 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".isqrt(), 3);")]
3051 #[stable(feature = "isqrt", since = "1.84.0")]
3053 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
3054 #[must_use = "this returns the result of the operation, \
3055 without modifying the original"]
3056 #[inline]
3057 #[track_caller]
3058 #[cfg(not(feature = "ferrocene_certified"))]
3059 pub const fn isqrt(self) -> Self {
3060 match self.checked_isqrt() {
3061 Some(sqrt) => sqrt,
3062 None => crate::num::int_sqrt::panic_for_negative_argument(),
3063 }
3064 }
3065
3066 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
3087 #[stable(feature = "euclidean_division", since = "1.38.0")]
3095 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3096 #[must_use = "this returns the result of the operation, \
3097 without modifying the original"]
3098 #[inline]
3099 #[track_caller]
3100 #[cfg(not(feature = "ferrocene_certified"))]
3101 pub const fn div_euclid(self, rhs: Self) -> Self {
3102 let q = self / rhs;
3103 if self % rhs < 0 {
3104 return if rhs > 0 { q - 1 } else { q + 1 }
3105 }
3106 q
3107 }
3108
3109
3110 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
3125 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.rem_euclid(-1);")]
3136 #[doc(alias = "modulo", alias = "mod")]
3138 #[stable(feature = "euclidean_division", since = "1.38.0")]
3139 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3140 #[must_use = "this returns the result of the operation, \
3141 without modifying the original"]
3142 #[inline]
3143 #[track_caller]
3144 #[cfg(not(feature = "ferrocene_certified"))]
3145 pub const fn rem_euclid(self, rhs: Self) -> Self {
3146 let r = self % rhs;
3147 if r < 0 {
3148 r.wrapping_add(rhs.wrapping_abs())
3157 } else {
3158 r
3159 }
3160 }
3161
3162 #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3174 #[unstable(feature = "int_roundings", issue = "88581")]
3182 #[must_use = "this returns the result of the operation, \
3183 without modifying the original"]
3184 #[inline]
3185 #[track_caller]
3186 #[cfg(not(feature = "ferrocene_certified"))]
3187 pub const fn div_floor(self, rhs: Self) -> Self {
3188 let d = self / rhs;
3189 let r = self % rhs;
3190
3191 let correction = (self ^ rhs) >> (Self::BITS - 1);
3198 if r != 0 {
3199 d + correction
3200 } else {
3201 d
3202 }
3203 }
3204
3205 #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3217 #[unstable(feature = "int_roundings", issue = "88581")]
3225 #[must_use = "this returns the result of the operation, \
3226 without modifying the original"]
3227 #[inline]
3228 #[track_caller]
3229 #[cfg(not(feature = "ferrocene_certified"))]
3230 pub const fn div_ceil(self, rhs: Self) -> Self {
3231 let d = self / rhs;
3232 let r = self % rhs;
3233
3234 let correction = 1 + ((self ^ rhs) >> (Self::BITS - 1));
3237 if r != 0 {
3238 d + correction
3239 } else {
3240 d
3241 }
3242 }
3243
3244 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(8), 16);")]
3263 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(8), 24);")]
3264 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
3265 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
3266 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
3267 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
3268 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(-8), -16);")]
3269 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(-8), -24);")]
3270 #[unstable(feature = "int_roundings", issue = "88581")]
3272 #[must_use = "this returns the result of the operation, \
3273 without modifying the original"]
3274 #[inline]
3275 #[rustc_inherit_overflow_checks]
3276 #[cfg(not(feature = "ferrocene_certified"))]
3277 pub const fn next_multiple_of(self, rhs: Self) -> Self {
3278 if rhs == -1 {
3280 return self;
3281 }
3282
3283 let r = self % rhs;
3284 let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
3285 r + rhs
3286 } else {
3287 r
3288 };
3289
3290 if m == 0 {
3291 self
3292 } else {
3293 self + (rhs - m)
3294 }
3295 }
3296
3297 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(16));")]
3308 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(24));")]
3309 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
3310 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
3311 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
3312 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
3313 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-16));")]
3314 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-24));")]
3315 #[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".checked_next_multiple_of(0), None);")]
3316 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_next_multiple_of(2), None);")]
3317 #[unstable(feature = "int_roundings", issue = "88581")]
3319 #[must_use = "this returns the result of the operation, \
3320 without modifying the original"]
3321 #[inline]
3322 #[cfg(not(feature = "ferrocene_certified"))]
3323 pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
3324 if rhs == -1 {
3326 return Some(self);
3327 }
3328
3329 let r = try_opt!(self.checked_rem(rhs));
3330 let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
3331 r + rhs
3333 } else {
3334 r
3335 };
3336
3337 if m == 0 {
3338 Some(self)
3339 } else {
3340 self.checked_add(rhs - m)
3342 }
3343 }
3344
3345 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".ilog(5), 1);")]
3361 #[stable(feature = "int_log", since = "1.67.0")]
3363 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3364 #[must_use = "this returns the result of the operation, \
3365 without modifying the original"]
3366 #[inline]
3367 #[track_caller]
3368 #[cfg(not(feature = "ferrocene_certified"))]
3369 pub const fn ilog(self, base: Self) -> u32 {
3370 assert!(base >= 2, "base of integer logarithm must be at least 2");
3371 if let Some(log) = self.checked_ilog(base) {
3372 log
3373 } else {
3374 int_log10::panic_for_nonpositive_argument()
3375 }
3376 }
3377
3378 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".ilog2(), 1);")]
3388 #[stable(feature = "int_log", since = "1.67.0")]
3390 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3391 #[must_use = "this returns the result of the operation, \
3392 without modifying the original"]
3393 #[inline]
3394 #[track_caller]
3395 #[cfg(not(feature = "ferrocene_certified"))]
3396 pub const fn ilog2(self) -> u32 {
3397 if let Some(log) = self.checked_ilog2() {
3398 log
3399 } else {
3400 int_log10::panic_for_nonpositive_argument()
3401 }
3402 }
3403
3404 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".ilog10(), 1);")]
3414 #[stable(feature = "int_log", since = "1.67.0")]
3416 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3417 #[must_use = "this returns the result of the operation, \
3418 without modifying the original"]
3419 #[inline]
3420 #[track_caller]
3421 #[cfg(not(feature = "ferrocene_certified"))]
3422 pub const fn ilog10(self) -> u32 {
3423 if let Some(log) = self.checked_ilog10() {
3424 log
3425 } else {
3426 int_log10::panic_for_nonpositive_argument()
3427 }
3428 }
3429
3430 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_ilog(5), Some(1));")]
3443 #[stable(feature = "int_log", since = "1.67.0")]
3445 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3446 #[must_use = "this returns the result of the operation, \
3447 without modifying the original"]
3448 #[inline]
3449 #[cfg(not(feature = "ferrocene_certified"))]
3450 pub const fn checked_ilog(self, base: Self) -> Option<u32> {
3451 if self <= 0 || base <= 1 {
3452 None
3453 } else {
3454 (self as $UnsignedT).checked_ilog(base as $UnsignedT)
3457 }
3458 }
3459
3460 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_ilog2(), Some(1));")]
3468 #[stable(feature = "int_log", since = "1.67.0")]
3470 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3471 #[must_use = "this returns the result of the operation, \
3472 without modifying the original"]
3473 #[inline]
3474 #[cfg(not(feature = "ferrocene_certified"))]
3475 pub const fn checked_ilog2(self) -> Option<u32> {
3476 if self <= 0 {
3477 None
3478 } else {
3479 let log = (Self::BITS - 1) - unsafe { intrinsics::ctlz_nonzero(self) as u32 };
3481 Some(log)
3482 }
3483 }
3484
3485 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_ilog10(), Some(1));")]
3493 #[stable(feature = "int_log", since = "1.67.0")]
3495 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3496 #[must_use = "this returns the result of the operation, \
3497 without modifying the original"]
3498 #[inline]
3499 #[cfg(not(feature = "ferrocene_certified"))]
3500 pub const fn checked_ilog10(self) -> Option<u32> {
3501 if self > 0 {
3502 Some(int_log10::$ActualT(self as $ActualT))
3503 } else {
3504 None
3505 }
3506 }
3507
3508 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
3514 #[doc = concat!("`", stringify!($SelfT), "`,")]
3516 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
3520 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".abs(), 10);")]
3527 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").abs(), 10);")]
3528 #[stable(feature = "rust1", since = "1.0.0")]
3530 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3531 #[allow(unused_attributes)]
3532 #[must_use = "this returns the result of the operation, \
3533 without modifying the original"]
3534 #[inline]
3535 #[rustc_inherit_overflow_checks]
3536 #[cfg(not(feature = "ferrocene_certified"))]
3537 pub const fn abs(self) -> Self {
3538 if self.is_negative() {
3542 -self
3543 } else {
3544 self
3545 }
3546 }
3547
3548 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(80), 20", stringify!($UnsignedT), ");")]
3557 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(110), 10", stringify!($UnsignedT), ");")]
3558 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(80), 180", stringify!($UnsignedT), ");")]
3559 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(-120), 20", stringify!($UnsignedT), ");")]
3560 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.abs_diff(", stringify!($SelfT), "::MAX), ", stringify!($UnsignedT), "::MAX);")]
3561 #[stable(feature = "int_abs_diff", since = "1.60.0")]
3563 #[rustc_const_stable(feature = "int_abs_diff", since = "1.60.0")]
3564 #[must_use = "this returns the result of the operation, \
3565 without modifying the original"]
3566 #[inline]
3567 #[cfg(not(feature = "ferrocene_certified"))]
3568 pub const fn abs_diff(self, other: Self) -> $UnsignedT {
3569 if self < other {
3570 (other as $UnsignedT).wrapping_sub(self as $UnsignedT)
3584 } else {
3585 (self as $UnsignedT).wrapping_sub(other as $UnsignedT)
3586 }
3587 }
3588
3589 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".signum(), 1);")]
3599 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".signum(), 0);")]
3600 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").signum(), -1);")]
3601 #[stable(feature = "rust1", since = "1.0.0")]
3603 #[rustc_const_stable(feature = "const_int_sign", since = "1.47.0")]
3604 #[must_use = "this returns the result of the operation, \
3605 without modifying the original"]
3606 #[inline(always)]
3607 #[cfg(not(feature = "ferrocene_certified"))]
3608 pub const fn signum(self) -> Self {
3609 crate::intrinsics::three_way_compare(self, 0) as Self
3615 }
3616
3617 #[doc = concat!("assert!(10", stringify!($SelfT), ".is_positive());")]
3624 #[doc = concat!("assert!(!(-10", stringify!($SelfT), ").is_positive());")]
3625 #[must_use]
3627 #[stable(feature = "rust1", since = "1.0.0")]
3628 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3629 #[inline(always)]
3630 #[cfg(not(feature = "ferrocene_certified"))]
3631 pub const fn is_positive(self) -> bool { self > 0 }
3632
3633 #[doc = concat!("assert!((-10", stringify!($SelfT), ").is_negative());")]
3640 #[doc = concat!("assert!(!10", stringify!($SelfT), ".is_negative());")]
3641 #[must_use]
3643 #[stable(feature = "rust1", since = "1.0.0")]
3644 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3645 #[inline(always)]
3646 #[cfg(not(feature = "ferrocene_certified"))]
3647 pub const fn is_negative(self) -> bool { self < 0 }
3648
3649 #[doc = $to_xe_bytes_doc]
3653 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();")]
3658 #[doc = concat!("assert_eq!(bytes, ", $be_bytes, ");")]
3659 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3661 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3662 #[must_use = "this returns the result of the operation, \
3663 without modifying the original"]
3664 #[inline]
3665 #[cfg(not(feature = "ferrocene_certified"))]
3666 pub const fn to_be_bytes(self) -> [u8; size_of::<Self>()] {
3667 self.to_be().to_ne_bytes()
3668 }
3669
3670 #[doc = $to_xe_bytes_doc]
3674 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();")]
3679 #[doc = concat!("assert_eq!(bytes, ", $le_bytes, ");")]
3680 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3682 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3683 #[must_use = "this returns the result of the operation, \
3684 without modifying the original"]
3685 #[inline]
3686 #[cfg(not(feature = "ferrocene_certified"))]
3687 pub const fn to_le_bytes(self) -> [u8; size_of::<Self>()] {
3688 self.to_le().to_ne_bytes()
3689 }
3690
3691 #[doc = $to_xe_bytes_doc]
3699 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_ne_bytes();")]
3707 #[doc = concat!(" ", $be_bytes)]
3711 #[doc = concat!(" ", $le_bytes)]
3713 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3717 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3718 #[allow(unnecessary_transmutes)]
3719 #[must_use = "this returns the result of the operation, \
3722 without modifying the original"]
3723 #[inline]
3724 #[cfg(not(feature = "ferrocene_certified"))]
3725 pub const fn to_ne_bytes(self) -> [u8; size_of::<Self>()] {
3726 unsafe { mem::transmute(self) }
3729 }
3730
3731 #[doc = $from_xe_bytes_doc]
3735 #[doc = concat!("let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, ");")]
3740 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3741 #[doc = concat!("fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3747 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3748 #[doc = concat!(" ", stringify!($SelfT), "::from_be_bytes(int_bytes.try_into().unwrap())")]
3750 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3753 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3754 #[must_use]
3755 #[inline]
3756 #[cfg(not(feature = "ferrocene_certified"))]
3757 pub const fn from_be_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3758 Self::from_be(Self::from_ne_bytes(bytes))
3759 }
3760
3761 #[doc = $from_xe_bytes_doc]
3765 #[doc = concat!("let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, ");")]
3770 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3771 #[doc = concat!("fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3777 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3778 #[doc = concat!(" ", stringify!($SelfT), "::from_le_bytes(int_bytes.try_into().unwrap())")]
3780 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3783 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3784 #[must_use]
3785 #[inline]
3786 #[cfg(not(feature = "ferrocene_certified"))]
3787 pub const fn from_le_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3788 Self::from_le(Self::from_ne_bytes(bytes))
3789 }
3790
3791 #[doc = $from_xe_bytes_doc]
3802 #[doc = concat!("let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"big\") {")]
3807 #[doc = concat!(" ", $be_bytes)]
3808 #[doc = concat!(" ", $le_bytes)]
3810 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3812 #[doc = concat!("fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3818 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3819 #[doc = concat!(" ", stringify!($SelfT), "::from_ne_bytes(int_bytes.try_into().unwrap())")]
3821 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3824 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3825 #[allow(unnecessary_transmutes)]
3826 #[must_use]
3827 #[inline]
3830 #[cfg(not(feature = "ferrocene_certified"))]
3831 pub const fn from_ne_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3832 unsafe { mem::transmute(bytes) }
3834 }
3835
3836 #[doc = concat!("[`", stringify!($SelfT), "::MIN", "`] instead.")]
3838 #[stable(feature = "rust1", since = "1.0.0")]
3841 #[inline(always)]
3842 #[rustc_promotable]
3843 #[rustc_const_stable(feature = "const_min_value", since = "1.32.0")]
3844 #[deprecated(since = "TBD", note = "replaced by the `MIN` associated constant on this type")]
3845 #[rustc_diagnostic_item = concat!(stringify!($SelfT), "_legacy_fn_min_value")]
3846 #[cfg(not(feature = "ferrocene_certified"))]
3847 pub const fn min_value() -> Self {
3848 Self::MIN
3849 }
3850
3851 #[doc = concat!("[`", stringify!($SelfT), "::MAX", "`] instead.")]
3853 #[stable(feature = "rust1", since = "1.0.0")]
3856 #[inline(always)]
3857 #[rustc_promotable]
3858 #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
3859 #[deprecated(since = "TBD", note = "replaced by the `MAX` associated constant on this type")]
3860 #[rustc_diagnostic_item = concat!(stringify!($SelfT), "_legacy_fn_max_value")]
3861 #[cfg(not(feature = "ferrocene_certified"))]
3862 pub const fn max_value() -> Self {
3863 Self::MAX
3864 }
3865 }
3866}