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 = "1.91.0")]
544 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
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 = "1.91.0")]
637 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
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 = "1.91.0")]
689 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
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 = "1.91.0")]
782 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
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 = "1.91.0")]
834 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
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 = "1.91.0")]
944 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
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 = "1.91.0")]
1013 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
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 = "1.91.0")]
1184 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
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 = "1.91.0")]
1252 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1253 #[must_use = "this returns the result of the operation, \
1254 without modifying the original"]
1255 #[inline]
1256 #[track_caller]
1257 #[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 = "1.91.0")]
1336 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
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 = "1.91.0")]
1394 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
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!("`", stringify!($SelfT), "::BITS`.")]
1472 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".exact_shl(4), Some(0x10));")]
1480 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".exact_shl(", stringify!($SelfT), "::BITS - 2), Some(1 << ", stringify!($SelfT), "::BITS - 2));")]
1481 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".exact_shl(", stringify!($SelfT), "::BITS - 1), None);")]
1482 #[doc = concat!("assert_eq!((-0x2", stringify!($SelfT), ").exact_shl(", stringify!($SelfT), "::BITS - 2), Some(-0x2 << ", stringify!($SelfT), "::BITS - 2));")]
1483 #[doc = concat!("assert_eq!((-0x2", stringify!($SelfT), ").exact_shl(", stringify!($SelfT), "::BITS - 1), None);")]
1484 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1486 #[must_use = "this returns the result of the operation, \
1487 without modifying the original"]
1488 #[inline]
1489 #[cfg(not(feature = "ferrocene_certified"))]
1490 pub const fn exact_shl(self, rhs: u32) -> Option<$SelfT> {
1491 if rhs < self.leading_zeros() || rhs < self.leading_ones() {
1492 Some(unsafe { self.unchecked_shl(rhs) })
1494 } else {
1495 None
1496 }
1497 }
1498
1499 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1502 #[doc = concat!("[`", stringify!($SelfT), "::exact_shl`]")]
1508 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1510 #[must_use = "this returns the result of the operation, \
1511 without modifying the original"]
1512 #[inline]
1513 #[cfg(not(feature = "ferrocene_certified"))]
1514 pub const unsafe fn unchecked_exact_shl(self, rhs: u32) -> $SelfT {
1515 assert_unsafe_precondition!(
1516 check_library_ub,
1517 concat!(stringify!($SelfT), "::unchecked_exact_shl cannot shift out bits that would change the value of the first bit"),
1518 (
1519 zeros: u32 = self.leading_zeros(),
1520 ones: u32 = self.leading_ones(),
1521 rhs: u32 = rhs,
1522 ) => rhs < zeros || rhs < ones,
1523 );
1524
1525 unsafe { self.unchecked_shl(rhs) }
1527 }
1528
1529 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(4), Some(0x1));")]
1536 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(128), None);")]
1537 #[stable(feature = "wrapping", since = "1.7.0")]
1539 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1540 #[must_use = "this returns the result of the operation, \
1541 without modifying the original"]
1542 #[inline]
1543 #[cfg(not(feature = "ferrocene_certified"))]
1544 pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
1545 if rhs < Self::BITS {
1547 Some(unsafe { self.unchecked_shr(rhs) })
1549 } else {
1550 None
1551 }
1552 }
1553
1554 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".strict_shr(4), 0x1);")]
1567 #[doc = concat!("let _ = 0x10", stringify!($SelfT), ".strict_shr(128);")]
1573 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1575 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1576 #[must_use = "this returns the result of the operation, \
1577 without modifying the original"]
1578 #[inline]
1579 #[track_caller]
1580 #[cfg(not(feature = "ferrocene_certified"))]
1581 pub const fn strict_shr(self, rhs: u32) -> Self {
1582 let (a, b) = self.overflowing_shr(rhs);
1583 if b { overflow_panic::shr() } else { a }
1584 }
1585
1586 #[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
1596 #[unstable(
1597 feature = "unchecked_shifts",
1598 reason = "niche optimization path",
1599 issue = "85122",
1600 )]
1601 #[must_use = "this returns the result of the operation, \
1602 without modifying the original"]
1603 #[inline(always)]
1604 #[track_caller]
1605 #[cfg(not(feature = "ferrocene_certified"))]
1606 pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
1607 assert_unsafe_precondition!(
1608 check_language_ub,
1609 concat!(stringify!($SelfT), "::unchecked_shr cannot overflow"),
1610 (
1611 rhs: u32 = rhs,
1612 ) => rhs < <$ActualT>::BITS,
1613 );
1614
1615 unsafe {
1617 intrinsics::unchecked_shr(self, rhs)
1618 }
1619 }
1620
1621 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".unbounded_shr(4), 0x1);")]
1631 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".unbounded_shr(129), 0);")]
1632 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.unbounded_shr(129), -1);")]
1633 #[stable(feature = "unbounded_shifts", since = "1.87.0")]
1635 #[rustc_const_stable(feature = "unbounded_shifts", since = "1.87.0")]
1636 #[must_use = "this returns the result of the operation, \
1637 without modifying the original"]
1638 #[inline]
1639 #[cfg(not(feature = "ferrocene_certified"))]
1640 pub const fn unbounded_shr(self, rhs: u32) -> $SelfT{
1641 if rhs < Self::BITS {
1642 unsafe { self.unchecked_shr(rhs) }
1645 } else {
1646 unsafe { self.unchecked_shr(Self::BITS - 1) }
1651 }
1652 }
1653
1654 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1658 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".exact_shr(4), Some(0x1));")]
1666 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".exact_shr(5), None);")]
1667 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1669 #[must_use = "this returns the result of the operation, \
1670 without modifying the original"]
1671 #[inline]
1672 #[cfg(not(feature = "ferrocene_certified"))]
1673 pub const fn exact_shr(self, rhs: u32) -> Option<$SelfT> {
1674 if rhs <= self.trailing_zeros() && rhs < <$SelfT>::BITS {
1675 Some(unsafe { self.unchecked_shr(rhs) })
1677 } else {
1678 None
1679 }
1680 }
1681
1682 #[doc = concat!("`", stringify!($SelfT), "::BITS`.")]
1685 #[doc = concat!(stringify!($SelfT), "::BITS`")]
1690 #[doc = concat!("[`", stringify!($SelfT), "::exact_shr`]")]
1692 #[unstable(feature = "exact_bitshifts", issue = "144336")]
1694 #[must_use = "this returns the result of the operation, \
1695 without modifying the original"]
1696 #[inline]
1697 #[cfg(not(feature = "ferrocene_certified"))]
1698 pub const unsafe fn unchecked_exact_shr(self, rhs: u32) -> $SelfT {
1699 assert_unsafe_precondition!(
1700 check_library_ub,
1701 concat!(stringify!($SelfT), "::unchecked_exact_shr cannot shift out non-zero bits"),
1702 (
1703 zeros: u32 = self.trailing_zeros(),
1704 bits: u32 = <$SelfT>::BITS,
1705 rhs: u32 = rhs,
1706 ) => rhs <= zeros && rhs < bits,
1707 );
1708
1709 unsafe { self.unchecked_shr(rhs) }
1711 }
1712
1713 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_abs(), Some(5));")]
1720 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_abs(), None);")]
1721 #[stable(feature = "no_panic_abs", since = "1.13.0")]
1723 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1724 #[must_use = "this returns the result of the operation, \
1725 without modifying the original"]
1726 #[inline]
1727 #[cfg(not(feature = "ferrocene_certified"))]
1728 pub const fn checked_abs(self) -> Option<Self> {
1729 if self.is_negative() {
1730 self.checked_neg()
1731 } else {
1732 Some(self)
1733 }
1734 }
1735
1736 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").strict_abs(), 5);")]
1749 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_abs();")]
1755 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1757 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1758 #[must_use = "this returns the result of the operation, \
1759 without modifying the original"]
1760 #[inline]
1761 #[track_caller]
1762 #[cfg(not(feature = "ferrocene_certified"))]
1763 pub const fn strict_abs(self) -> Self {
1764 if self.is_negative() {
1765 self.strict_neg()
1766 } else {
1767 self
1768 }
1769 }
1770
1771 #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".checked_pow(2), Some(64));")]
1778 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);")]
1779 #[stable(feature = "no_panic_pow", since = "1.34.0")]
1782 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1783 #[must_use = "this returns the result of the operation, \
1784 without modifying the original"]
1785 #[inline]
1786 #[cfg(not(feature = "ferrocene_certified"))]
1787 pub const fn checked_pow(self, mut exp: u32) -> Option<Self> {
1788 if exp == 0 {
1789 return Some(1);
1790 }
1791 let mut base = self;
1792 let mut acc: Self = 1;
1793
1794 loop {
1795 if (exp & 1) == 1 {
1796 acc = try_opt!(acc.checked_mul(base));
1797 if exp == 1 {
1799 return Some(acc);
1800 }
1801 }
1802 exp /= 2;
1803 base = try_opt!(base.checked_mul(base));
1804 }
1805 }
1806
1807 #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".strict_pow(2), 64);")]
1820 #[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_pow(2);")]
1826 #[stable(feature = "strict_overflow_ops", since = "1.91.0")]
1828 #[rustc_const_stable(feature = "strict_overflow_ops", since = "1.91.0")]
1829 #[must_use = "this returns the result of the operation, \
1830 without modifying the original"]
1831 #[inline]
1832 #[track_caller]
1833 #[cfg(not(feature = "ferrocene_certified"))]
1834 pub const fn strict_pow(self, mut exp: u32) -> Self {
1835 if exp == 0 {
1836 return 1;
1837 }
1838 let mut base = self;
1839 let mut acc: Self = 1;
1840
1841 loop {
1842 if (exp & 1) == 1 {
1843 acc = acc.strict_mul(base);
1844 if exp == 1 {
1846 return acc;
1847 }
1848 }
1849 exp /= 2;
1850 base = base.strict_mul(base);
1851 }
1852 }
1853
1854 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_isqrt(), Some(3));")]
1862 #[stable(feature = "isqrt", since = "1.84.0")]
1864 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
1865 #[must_use = "this returns the result of the operation, \
1866 without modifying the original"]
1867 #[inline]
1868 #[cfg(not(feature = "ferrocene_certified"))]
1869 pub const fn checked_isqrt(self) -> Option<Self> {
1870 if self < 0 {
1871 None
1872 } else {
1873 let result = unsafe {
1875 crate::num::int_sqrt::$ActualT(self as $ActualT) as $SelfT
1876 };
1877
1878 unsafe {
1890 const MAX_RESULT: $SelfT = unsafe {
1892 crate::num::int_sqrt::$ActualT(<$ActualT>::MAX) as $SelfT
1893 };
1894
1895 crate::hint::assert_unchecked(result >= 0);
1896 crate::hint::assert_unchecked(result <= MAX_RESULT);
1897 }
1898
1899 Some(result)
1900 }
1901 }
1902
1903 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_add(1), 101);")]
1910 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add(100), ", stringify!($SelfT), "::MAX);")]
1911 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_add(-1), ", stringify!($SelfT), "::MIN);")]
1912 #[stable(feature = "rust1", since = "1.0.0")]
1915 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1916 #[must_use = "this returns the result of the operation, \
1917 without modifying the original"]
1918 #[inline(always)]
1919 #[cfg(not(feature = "ferrocene_certified"))]
1920 pub const fn saturating_add(self, rhs: Self) -> Self {
1921 intrinsics::saturating_add(self, rhs)
1922 }
1923
1924 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".saturating_add_unsigned(2), 3);")]
1931 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add_unsigned(100), ", stringify!($SelfT), "::MAX);")]
1932 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1934 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1935 #[must_use = "this returns the result of the operation, \
1936 without modifying the original"]
1937 #[inline]
1938 #[cfg(not(feature = "ferrocene_certified"))]
1939 pub const fn saturating_add_unsigned(self, rhs: $UnsignedT) -> Self {
1940 match self.checked_add_unsigned(rhs) {
1943 Some(x) => x,
1944 None => Self::MAX,
1945 }
1946 }
1947
1948 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub(127), -27);")]
1955 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub(100), ", stringify!($SelfT), "::MIN);")]
1956 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_sub(-1), ", stringify!($SelfT), "::MAX);")]
1957 #[stable(feature = "rust1", since = "1.0.0")]
1959 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1960 #[must_use = "this returns the result of the operation, \
1961 without modifying the original"]
1962 #[inline(always)]
1963 #[cfg(not(feature = "ferrocene_certified"))]
1964 pub const fn saturating_sub(self, rhs: Self) -> Self {
1965 intrinsics::saturating_sub(self, rhs)
1966 }
1967
1968 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub_unsigned(127), -27);")]
1975 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub_unsigned(100), ", stringify!($SelfT), "::MIN);")]
1976 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1978 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1979 #[must_use = "this returns the result of the operation, \
1980 without modifying the original"]
1981 #[inline]
1982 #[cfg(not(feature = "ferrocene_certified"))]
1983 pub const fn saturating_sub_unsigned(self, rhs: $UnsignedT) -> Self {
1984 match self.checked_sub_unsigned(rhs) {
1987 Some(x) => x,
1988 None => Self::MIN,
1989 }
1990 }
1991
1992 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_neg(), -100);")]
1999 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_neg(), 100);")]
2000 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_neg(), ", stringify!($SelfT), "::MAX);")]
2001 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_neg(), ", stringify!($SelfT), "::MIN + 1);")]
2002 #[stable(feature = "saturating_neg", since = "1.45.0")]
2005 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2006 #[must_use = "this returns the result of the operation, \
2007 without modifying the original"]
2008 #[inline(always)]
2009 #[cfg(not(feature = "ferrocene_certified"))]
2010 pub const fn saturating_neg(self) -> Self {
2011 intrinsics::saturating_sub(0, self)
2012 }
2013
2014 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_abs(), 100);")]
2021 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_abs(), 100);")]
2022 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_abs(), ", stringify!($SelfT), "::MAX);")]
2023 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).saturating_abs(), ", stringify!($SelfT), "::MAX);")]
2024 #[stable(feature = "saturating_neg", since = "1.45.0")]
2027 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2028 #[must_use = "this returns the result of the operation, \
2029 without modifying the original"]
2030 #[inline]
2031 #[cfg(not(feature = "ferrocene_certified"))]
2032 pub const fn saturating_abs(self) -> Self {
2033 if self.is_negative() {
2034 self.saturating_neg()
2035 } else {
2036 self
2037 }
2038 }
2039
2040 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".saturating_mul(12), 120);")]
2047 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_mul(10), ", stringify!($SelfT), "::MAX);")]
2048 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_mul(10), ", stringify!($SelfT), "::MIN);")]
2049 #[stable(feature = "wrapping", since = "1.7.0")]
2051 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
2052 #[must_use = "this returns the result of the operation, \
2053 without modifying the original"]
2054 #[inline]
2055 #[cfg(not(feature = "ferrocene_certified"))]
2056 pub const fn saturating_mul(self, rhs: Self) -> Self {
2057 match self.checked_mul(rhs) {
2058 Some(x) => x,
2059 None => if (self < 0) == (rhs < 0) {
2060 Self::MAX
2061 } else {
2062 Self::MIN
2063 }
2064 }
2065 }
2066
2067 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
2078 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_div(-1), ", stringify!($SelfT), "::MIN + 1);")]
2079 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_div(-1), ", stringify!($SelfT), "::MAX);")]
2080 #[stable(feature = "saturating_div", since = "1.58.0")]
2083 #[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
2084 #[must_use = "this returns the result of the operation, \
2085 without modifying the original"]
2086 #[inline]
2087 #[cfg(not(feature = "ferrocene_certified"))]
2088 pub const fn saturating_div(self, rhs: Self) -> Self {
2089 match self.overflowing_div(rhs) {
2090 (result, false) => result,
2091 (_result, true) => Self::MAX, }
2093 }
2094
2095 #[doc = concat!("assert_eq!((-4", stringify!($SelfT), ").saturating_pow(3), -64);")]
2102 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(2), ", stringify!($SelfT), "::MAX);")]
2103 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(3), ", stringify!($SelfT), "::MIN);")]
2104 #[stable(feature = "no_panic_pow", since = "1.34.0")]
2106 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2107 #[must_use = "this returns the result of the operation, \
2108 without modifying the original"]
2109 #[inline]
2110 #[cfg(not(feature = "ferrocene_certified"))]
2111 pub const fn saturating_pow(self, exp: u32) -> Self {
2112 match self.checked_pow(exp) {
2113 Some(x) => x,
2114 None if self < 0 && exp % 2 == 1 => Self::MIN,
2115 None => Self::MAX,
2116 }
2117 }
2118
2119 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add(27), 127);")]
2126 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add(2), ", stringify!($SelfT), "::MIN + 1);")]
2127 #[stable(feature = "rust1", since = "1.0.0")]
2129 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2130 #[must_use = "this returns the result of the operation, \
2131 without modifying the original"]
2132 #[inline(always)]
2133 #[cfg(not(feature = "ferrocene_certified"))]
2134 pub const fn wrapping_add(self, rhs: Self) -> Self {
2135 intrinsics::wrapping_add(self, rhs)
2136 }
2137
2138 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add_unsigned(27), 127);")]
2145 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add_unsigned(2), ", stringify!($SelfT), "::MIN + 1);")]
2146 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2148 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2149 #[must_use = "this returns the result of the operation, \
2150 without modifying the original"]
2151 #[inline(always)]
2152 #[cfg(not(feature = "ferrocene_certified"))]
2153 pub const fn wrapping_add_unsigned(self, rhs: $UnsignedT) -> Self {
2154 self.wrapping_add(rhs as Self)
2155 }
2156
2157 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub(127), -127);")]
2164 #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub(", stringify!($SelfT), "::MAX), ", stringify!($SelfT), "::MAX);")]
2165 #[stable(feature = "rust1", since = "1.0.0")]
2167 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2168 #[must_use = "this returns the result of the operation, \
2169 without modifying the original"]
2170 #[inline(always)]
2171 #[cfg(not(feature = "ferrocene_certified"))]
2172 pub const fn wrapping_sub(self, rhs: Self) -> Self {
2173 intrinsics::wrapping_sub(self, rhs)
2174 }
2175
2176 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub_unsigned(127), -127);")]
2183 #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub_unsigned(", stringify!($UnsignedT), "::MAX), -1);")]
2184 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2186 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2187 #[must_use = "this returns the result of the operation, \
2188 without modifying the original"]
2189 #[inline(always)]
2190 #[cfg(not(feature = "ferrocene_certified"))]
2191 pub const fn wrapping_sub_unsigned(self, rhs: $UnsignedT) -> Self {
2192 self.wrapping_sub(rhs as Self)
2193 }
2194
2195 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".wrapping_mul(12), 120);")]
2202 #[stable(feature = "rust1", since = "1.0.0")]
2205 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2206 #[must_use = "this returns the result of the operation, \
2207 without modifying the original"]
2208 #[inline(always)]
2209 #[cfg(not(feature = "ferrocene_certified"))]
2210 pub const fn wrapping_mul(self, rhs: Self) -> Self {
2211 intrinsics::wrapping_mul(self, rhs)
2212 }
2213
2214 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div(10), 10);")]
2229 #[stable(feature = "num_wrapping", since = "1.2.0")]
2232 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
2233 #[must_use = "this returns the result of the operation, \
2234 without modifying the original"]
2235 #[inline]
2236 #[cfg(not(feature = "ferrocene_certified"))]
2237 pub const fn wrapping_div(self, rhs: Self) -> Self {
2238 self.overflowing_div(rhs).0
2239 }
2240
2241 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div_euclid(10), 10);")]
2256 #[stable(feature = "euclidean_division", since = "1.38.0")]
2259 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2260 #[must_use = "this returns the result of the operation, \
2261 without modifying the original"]
2262 #[inline]
2263 #[cfg(not(feature = "ferrocene_certified"))]
2264 pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
2265 self.overflowing_div_euclid(rhs).0
2266 }
2267
2268 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem(10), 0);")]
2283 #[stable(feature = "num_wrapping", since = "1.2.0")]
2286 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
2287 #[must_use = "this returns the result of the operation, \
2288 without modifying the original"]
2289 #[inline]
2290 #[cfg(not(feature = "ferrocene_certified"))]
2291 pub const fn wrapping_rem(self, rhs: Self) -> Self {
2292 self.overflowing_rem(rhs).0
2293 }
2294
2295 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);")]
2309 #[stable(feature = "euclidean_division", since = "1.38.0")]
2312 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2313 #[must_use = "this returns the result of the operation, \
2314 without modifying the original"]
2315 #[inline]
2316 #[cfg(not(feature = "ferrocene_certified"))]
2317 pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
2318 self.overflowing_rem_euclid(rhs).0
2319 }
2320
2321 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_neg(), -100);")]
2332 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_neg(), 100);")]
2333 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_neg(), ", stringify!($SelfT), "::MIN);")]
2334 #[stable(feature = "num_wrapping", since = "1.2.0")]
2336 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2337 #[must_use = "this returns the result of the operation, \
2338 without modifying the original"]
2339 #[inline(always)]
2340 #[cfg(not(feature = "ferrocene_certified"))]
2341 pub const fn wrapping_neg(self) -> Self {
2342 (0 as $SelfT).wrapping_sub(self)
2343 }
2344
2345 #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(7), -128);")]
2357 #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(128), -1);")]
2358 #[stable(feature = "num_wrapping", since = "1.2.0")]
2360 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2361 #[must_use = "this returns the result of the operation, \
2362 without modifying the original"]
2363 #[inline(always)]
2364 #[cfg(not(feature = "ferrocene_certified"))]
2365 pub const fn wrapping_shl(self, rhs: u32) -> Self {
2366 unsafe {
2369 self.unchecked_shl(rhs & (Self::BITS - 1))
2370 }
2371 }
2372
2373 #[doc = concat!("assert_eq!((-128", stringify!($SelfT), ").wrapping_shr(7), -1);")]
2385 #[stable(feature = "num_wrapping", since = "1.2.0")]
2388 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2389 #[must_use = "this returns the result of the operation, \
2390 without modifying the original"]
2391 #[inline(always)]
2392 #[cfg(not(feature = "ferrocene_certified"))]
2393 pub const fn wrapping_shr(self, rhs: u32) -> Self {
2394 unsafe {
2397 self.unchecked_shr(rhs & (Self::BITS - 1))
2398 }
2399 }
2400
2401 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_abs(), 100);")]
2412 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_abs(), 100);")]
2413 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_abs(), ", stringify!($SelfT), "::MIN);")]
2414 #[stable(feature = "no_panic_abs", since = "1.13.0")]
2417 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2418 #[must_use = "this returns the result of the operation, \
2419 without modifying the original"]
2420 #[allow(unused_attributes)]
2421 #[inline]
2422 #[cfg(not(feature = "ferrocene_certified"))]
2423 pub const fn wrapping_abs(self) -> Self {
2424 if self.is_negative() {
2425 self.wrapping_neg()
2426 } else {
2427 self
2428 }
2429 }
2430
2431 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".unsigned_abs(), 100", stringify!($UnsignedT), ");")]
2439 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").unsigned_abs(), 100", stringify!($UnsignedT), ");")]
2440 #[stable(feature = "unsigned_abs", since = "1.51.0")]
2443 #[rustc_const_stable(feature = "unsigned_abs", since = "1.51.0")]
2444 #[must_use = "this returns the result of the operation, \
2445 without modifying the original"]
2446 #[inline]
2447 #[cfg(not(feature = "ferrocene_certified"))]
2448 pub const fn unsigned_abs(self) -> $UnsignedT {
2449 self.wrapping_abs() as $UnsignedT
2450 }
2451
2452 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".wrapping_pow(4), 81);")]
2459 #[stable(feature = "no_panic_pow", since = "1.34.0")]
2463 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2464 #[must_use = "this returns the result of the operation, \
2465 without modifying the original"]
2466 #[inline]
2467 #[cfg(not(feature = "ferrocene_certified"))]
2468 pub const fn wrapping_pow(self, mut exp: u32) -> Self {
2469 if exp == 0 {
2470 return 1;
2471 }
2472 let mut base = self;
2473 let mut acc: Self = 1;
2474
2475 if intrinsics::is_val_statically_known(exp) {
2476 while exp > 1 {
2477 if (exp & 1) == 1 {
2478 acc = acc.wrapping_mul(base);
2479 }
2480 exp /= 2;
2481 base = base.wrapping_mul(base);
2482 }
2483
2484 acc.wrapping_mul(base)
2488 } else {
2489 loop {
2494 if (exp & 1) == 1 {
2495 acc = acc.wrapping_mul(base);
2496 if exp == 1 {
2498 return acc;
2499 }
2500 }
2501 exp /= 2;
2502 base = base.wrapping_mul(base);
2503 }
2504 }
2505 }
2506
2507 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_add(2), (7, false));")]
2517 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.overflowing_add(1), (", stringify!($SelfT), "::MIN, true));")]
2518 #[stable(feature = "wrapping", since = "1.7.0")]
2520 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2521 #[must_use = "this returns the result of the operation, \
2522 without modifying the original"]
2523 #[inline(always)]
2524 #[cfg(not(feature = "ferrocene_certified"))]
2525 pub const fn overflowing_add(self, rhs: Self) -> (Self, bool) {
2526 let (a, b) = intrinsics::add_with_overflow(self as $ActualT, rhs as $ActualT);
2527 (a as Self, b)
2528 }
2529
2530 #[doc = concat!("[`", stringify!($UnsignedT), "::carrying_add`]")]
2542 #[doc = concat!("// 10 MAX (a = 10 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")]
2557 #[doc = concat!("// + -5 9 (b = -5 × 2^", stringify!($BITS), " + 9)")]
2558 #[doc = concat!("// 6 8 (sum = 6 × 2^", stringify!($BITS), " + 8)")]
2560 #[doc = concat!("let (a1, a0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (10, ", stringify!($UnsignedT), "::MAX);")]
2562 #[doc = concat!("let (b1, b0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (-5, 9);")]
2563 #[doc = concat!("// ", stringify!($UnsignedT), "::carrying_add for the less significant words")]
2566 #[doc = concat!("// ", stringify!($SelfT), "::carrying_add for the most significant word")]
2570 #[unstable(feature = "bigint_helper_methods", issue = "85532")]
2576 #[must_use = "this returns the result of the operation, \
2577 without modifying the original"]
2578 #[inline]
2579 #[cfg(not(feature = "ferrocene_certified"))]
2580 pub const fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool) {
2581 let (a, b) = self.overflowing_add(rhs);
2584 let (c, d) = a.overflowing_add(carry as $SelfT);
2585 (c, b != d)
2586 }
2587
2588 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_add_unsigned(2), (3, false));")]
2598 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_add_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MAX, false));")]
2599 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).overflowing_add_unsigned(3), (", stringify!($SelfT), "::MIN, true));")]
2600 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2602 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2603 #[must_use = "this returns the result of the operation, \
2604 without modifying the original"]
2605 #[inline]
2606 #[cfg(not(feature = "ferrocene_certified"))]
2607 pub const fn overflowing_add_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
2608 let rhs = rhs as Self;
2609 let (res, overflowed) = self.overflowing_add(rhs);
2610 (res, overflowed ^ (rhs < 0))
2611 }
2612
2613 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_sub(2), (3, false));")]
2622 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_sub(1), (", stringify!($SelfT), "::MAX, true));")]
2623 #[stable(feature = "wrapping", since = "1.7.0")]
2625 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2626 #[must_use = "this returns the result of the operation, \
2627 without modifying the original"]
2628 #[inline(always)]
2629 #[cfg(not(feature = "ferrocene_certified"))]
2630 pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
2631 let (a, b) = intrinsics::sub_with_overflow(self as $ActualT, rhs as $ActualT);
2632 (a as Self, b)
2633 }
2634
2635 #[doc = concat!("[`", stringify!($UnsignedT), "::borrowing_sub`]")]
2648 #[doc = concat!("// 6 8 (a = 6 × 2^", stringify!($BITS), " + 8)")]
2663 #[doc = concat!("// - -5 9 (b = -5 × 2^", stringify!($BITS), " + 9)")]
2664 #[doc = concat!("// 10 MAX (diff = 10 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")]
2666 #[doc = concat!("let (a1, a0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (6, 8);")]
2668 #[doc = concat!("let (b1, b0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (-5, 9);")]
2669 #[doc = concat!("// ", stringify!($UnsignedT), "::borrowing_sub for the less significant words")]
2672 #[doc = concat!("// ", stringify!($SelfT), "::borrowing_sub for the most significant word")]
2676 #[doc = concat!("assert_eq!((diff1, diff0), (10, ", stringify!($UnsignedT), "::MAX));")]
2680 #[unstable(feature = "bigint_helper_methods", issue = "85532")]
2682 #[must_use = "this returns the result of the operation, \
2683 without modifying the original"]
2684 #[inline]
2685 #[cfg(not(feature = "ferrocene_certified"))]
2686 pub const fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
2687 let (a, b) = self.overflowing_sub(rhs);
2690 let (c, d) = a.overflowing_sub(borrow as $SelfT);
2691 (c, b != d)
2692 }
2693
2694 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_sub_unsigned(2), (-1, false));")]
2704 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX).overflowing_sub_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MIN, false));")]
2705 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).overflowing_sub_unsigned(3), (", stringify!($SelfT), "::MAX, true));")]
2706 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2708 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2709 #[must_use = "this returns the result of the operation, \
2710 without modifying the original"]
2711 #[inline]
2712 #[cfg(not(feature = "ferrocene_certified"))]
2713 pub const fn overflowing_sub_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
2714 let rhs = rhs as Self;
2715 let (res, overflowed) = self.overflowing_sub(rhs);
2716 (res, overflowed ^ (rhs < 0))
2717 }
2718
2719 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_mul(2), (10, false));")]
2728 #[stable(feature = "wrapping", since = "1.7.0")]
2731 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2732 #[must_use = "this returns the result of the operation, \
2733 without modifying the original"]
2734 #[inline(always)]
2735 #[cfg(not(feature = "ferrocene_certified"))]
2736 pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
2737 let (a, b) = intrinsics::mul_with_overflow(self as $ActualT, rhs as $ActualT);
2738 (a as Self, b)
2739 }
2740
2741 #[unstable(feature = "bigint_helper_methods", issue = "85532")]
2759 #[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
2760 #[must_use = "this returns the result of the operation, \
2761 without modifying the original"]
2762 #[inline]
2763 #[cfg(not(feature = "ferrocene_certified"))]
2764 pub const fn widening_mul(self, rhs: Self) -> ($UnsignedT, Self) {
2765 Self::carrying_mul_add(self, rhs, 0, 0)
2766 }
2767
2768 #[doc = concat!("assert_eq!(",
2791 stringify!($SelfT), "::MAX.carrying_mul(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
2792 "(", stringify!($SelfT), "::MAX.unsigned_abs() + 1, ", stringify!($SelfT), "::MAX / 2));"
2793 )]
2794 #[unstable(feature = "bigint_helper_methods", issue = "85532")]
2796 #[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
2797 #[must_use = "this returns the result of the operation, \
2798 without modifying the original"]
2799 #[inline]
2800 #[cfg(not(feature = "ferrocene_certified"))]
2801 pub const fn carrying_mul(self, rhs: Self, carry: Self) -> ($UnsignedT, Self) {
2802 Self::carrying_mul_add(self, rhs, carry, 0)
2803 }
2804
2805 #[doc = concat!("assert_eq!(",
2829 stringify!($SelfT), "::MAX.carrying_mul_add(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
2830 "(", stringify!($UnsignedT), "::MAX, ", stringify!($SelfT), "::MAX / 2));"
2831 )]
2832 #[unstable(feature = "bigint_helper_methods", issue = "85532")]
2834 #[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
2835 #[must_use = "this returns the result of the operation, \
2836 without modifying the original"]
2837 #[inline]
2838 #[cfg(not(feature = "ferrocene_certified"))]
2839 pub const fn carrying_mul_add(self, rhs: Self, carry: Self, add: Self) -> ($UnsignedT, Self) {
2840 intrinsics::carrying_mul_add(self, rhs, carry, add)
2841 }
2842
2843 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div(2), (2, false));")]
2856 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div(-1), (", stringify!($SelfT), "::MIN, true));")]
2857 #[inline]
2859 #[stable(feature = "wrapping", since = "1.7.0")]
2860 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
2861 #[must_use = "this returns the result of the operation, \
2862 without modifying the original"]
2863 #[cfg(not(feature = "ferrocene_certified"))]
2864 pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
2865 if intrinsics::unlikely((self == Self::MIN) & (rhs == -1)) {
2867 (self, true)
2868 } else {
2869 (self / rhs, false)
2870 }
2871 }
2872
2873 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div_euclid(2), (2, false));")]
2886 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringify!($SelfT), "::MIN, true));")]
2887 #[inline]
2889 #[stable(feature = "euclidean_division", since = "1.38.0")]
2890 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2891 #[must_use = "this returns the result of the operation, \
2892 without modifying the original"]
2893 #[cfg(not(feature = "ferrocene_certified"))]
2894 pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
2895 if intrinsics::unlikely((self == Self::MIN) & (rhs == -1)) {
2897 (self, true)
2898 } else {
2899 (self.div_euclid(rhs), false)
2900 }
2901 }
2902
2903 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem(2), (1, false));")]
2916 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem(-1), (0, true));")]
2917 #[inline]
2919 #[stable(feature = "wrapping", since = "1.7.0")]
2920 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
2921 #[must_use = "this returns the result of the operation, \
2922 without modifying the original"]
2923 #[cfg(not(feature = "ferrocene_certified"))]
2924 pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
2925 if intrinsics::unlikely(rhs == -1) {
2926 (0, self == Self::MIN)
2927 } else {
2928 (self % rhs, false)
2929 }
2930 }
2931
2932
2933 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem_euclid(2), (1, false));")]
2946 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));")]
2947 #[stable(feature = "euclidean_division", since = "1.38.0")]
2949 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2950 #[must_use = "this returns the result of the operation, \
2951 without modifying the original"]
2952 #[inline]
2953 #[track_caller]
2954 #[cfg(not(feature = "ferrocene_certified"))]
2955 pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
2956 if intrinsics::unlikely(rhs == -1) {
2957 (0, self == Self::MIN)
2958 } else {
2959 (self.rem_euclid(rhs), false)
2960 }
2961 }
2962
2963
2964 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2, false));")]
2974 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($SelfT), "::MIN, true));")]
2975 #[inline]
2977 #[stable(feature = "wrapping", since = "1.7.0")]
2978 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2979 #[must_use = "this returns the result of the operation, \
2980 without modifying the original"]
2981 #[allow(unused_attributes)]
2982 #[cfg(not(feature = "ferrocene_certified"))]
2983 pub const fn overflowing_neg(self) -> (Self, bool) {
2984 if intrinsics::unlikely(self == Self::MIN) {
2985 (Self::MIN, true)
2986 } else {
2987 (-self, false)
2988 }
2989 }
2990
2991 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT),".overflowing_shl(4), (0x10, false));")]
3001 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shl(", stringify!($BITS_MINUS_ONE), "), (0, false));")]
3003 #[stable(feature = "wrapping", since = "1.7.0")]
3005 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3006 #[must_use = "this returns the result of the operation, \
3007 without modifying the original"]
3008 #[inline]
3009 #[cfg(not(feature = "ferrocene_certified"))]
3010 pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
3011 (self.wrapping_shl(rhs), rhs >= Self::BITS)
3012 }
3013
3014 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shr(4), (0x1, false));")]
3024 #[stable(feature = "wrapping", since = "1.7.0")]
3027 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3028 #[must_use = "this returns the result of the operation, \
3029 without modifying the original"]
3030 #[inline]
3031 #[cfg(not(feature = "ferrocene_certified"))]
3032 pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
3033 (self.wrapping_shr(rhs), rhs >= Self::BITS)
3034 }
3035
3036 #[doc = concat!("(e.g., ", stringify!($SelfT), "::MIN for values of type ", stringify!($SelfT), "),")]
3041 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".overflowing_abs(), (10, false));")]
3048 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").overflowing_abs(), (10, false));")]
3049 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_abs(), (", stringify!($SelfT), "::MIN, true));")]
3050 #[stable(feature = "no_panic_abs", since = "1.13.0")]
3052 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3053 #[must_use = "this returns the result of the operation, \
3054 without modifying the original"]
3055 #[inline]
3056 #[cfg(not(feature = "ferrocene_certified"))]
3057 pub const fn overflowing_abs(self) -> (Self, bool) {
3058 (self.wrapping_abs(), self == Self::MIN)
3059 }
3060
3061 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".overflowing_pow(4), (81, false));")]
3070 #[stable(feature = "no_panic_pow", since = "1.34.0")]
3073 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
3074 #[must_use = "this returns the result of the operation, \
3075 without modifying the original"]
3076 #[inline]
3077 #[cfg(not(feature = "ferrocene_certified"))]
3078 pub const fn overflowing_pow(self, mut exp: u32) -> (Self, bool) {
3079 if exp == 0 {
3080 return (1,false);
3081 }
3082 let mut base = self;
3083 let mut acc: Self = 1;
3084 let mut overflown = false;
3085 let mut r;
3087
3088 loop {
3089 if (exp & 1) == 1 {
3090 r = acc.overflowing_mul(base);
3091 if exp == 1 {
3093 r.1 |= overflown;
3094 return r;
3095 }
3096 acc = r.0;
3097 overflown |= r.1;
3098 }
3099 exp /= 2;
3100 r = base.overflowing_mul(base);
3101 base = r.0;
3102 overflown |= r.1;
3103 }
3104 }
3105
3106 #[doc = concat!("let x: ", stringify!($SelfT), " = 2; // or any other integer type")]
3112 #[stable(feature = "rust1", since = "1.0.0")]
3116 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
3117 #[must_use = "this returns the result of the operation, \
3118 without modifying the original"]
3119 #[inline]
3120 #[rustc_inherit_overflow_checks]
3121 #[cfg(not(feature = "ferrocene_certified"))]
3122 pub const fn pow(self, mut exp: u32) -> Self {
3123 if exp == 0 {
3124 return 1;
3125 }
3126 let mut base = self;
3127 let mut acc = 1;
3128
3129 if intrinsics::is_val_statically_known(exp) {
3130 while exp > 1 {
3131 if (exp & 1) == 1 {
3132 acc = acc * base;
3133 }
3134 exp /= 2;
3135 base = base * base;
3136 }
3137
3138 acc * base
3143 } else {
3144 loop {
3149 if (exp & 1) == 1 {
3150 acc = acc * base;
3151 if exp == 1 {
3153 return acc;
3154 }
3155 }
3156 exp /= 2;
3157 base = base * base;
3158 }
3159 }
3160 }
3161
3162 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".isqrt(), 3);")]
3172 #[stable(feature = "isqrt", since = "1.84.0")]
3174 #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
3175 #[must_use = "this returns the result of the operation, \
3176 without modifying the original"]
3177 #[inline]
3178 #[track_caller]
3179 #[cfg(not(feature = "ferrocene_certified"))]
3180 pub const fn isqrt(self) -> Self {
3181 match self.checked_isqrt() {
3182 Some(sqrt) => sqrt,
3183 None => crate::num::int_sqrt::panic_for_negative_argument(),
3184 }
3185 }
3186
3187 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
3208 #[stable(feature = "euclidean_division", since = "1.38.0")]
3216 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3217 #[must_use = "this returns the result of the operation, \
3218 without modifying the original"]
3219 #[inline]
3220 #[track_caller]
3221 #[cfg(not(feature = "ferrocene_certified"))]
3222 pub const fn div_euclid(self, rhs: Self) -> Self {
3223 let q = self / rhs;
3224 if self % rhs < 0 {
3225 return if rhs > 0 { q - 1 } else { q + 1 }
3226 }
3227 q
3228 }
3229
3230
3231 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
3246 #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.rem_euclid(-1);")]
3257 #[doc(alias = "modulo", alias = "mod")]
3259 #[stable(feature = "euclidean_division", since = "1.38.0")]
3260 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3261 #[must_use = "this returns the result of the operation, \
3262 without modifying the original"]
3263 #[inline]
3264 #[track_caller]
3265 #[cfg(not(feature = "ferrocene_certified"))]
3266 pub const fn rem_euclid(self, rhs: Self) -> Self {
3267 let r = self % rhs;
3268 if r < 0 {
3269 r.wrapping_add(rhs.wrapping_abs())
3278 } else {
3279 r
3280 }
3281 }
3282
3283 #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3295 #[unstable(feature = "int_roundings", issue = "88581")]
3303 #[must_use = "this returns the result of the operation, \
3304 without modifying the original"]
3305 #[inline]
3306 #[track_caller]
3307 #[cfg(not(feature = "ferrocene_certified"))]
3308 pub const fn div_floor(self, rhs: Self) -> Self {
3309 let d = self / rhs;
3310 let r = self % rhs;
3311
3312 let correction = (self ^ rhs) >> (Self::BITS - 1);
3319 if r != 0 {
3320 d + correction
3321 } else {
3322 d
3323 }
3324 }
3325
3326 #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3338 #[unstable(feature = "int_roundings", issue = "88581")]
3346 #[must_use = "this returns the result of the operation, \
3347 without modifying the original"]
3348 #[inline]
3349 #[track_caller]
3350 #[cfg(not(feature = "ferrocene_certified"))]
3351 pub const fn div_ceil(self, rhs: Self) -> Self {
3352 let d = self / rhs;
3353 let r = self % rhs;
3354
3355 let correction = 1 + ((self ^ rhs) >> (Self::BITS - 1));
3358 if r != 0 {
3359 d + correction
3360 } else {
3361 d
3362 }
3363 }
3364
3365 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(8), 16);")]
3384 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(8), 24);")]
3385 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
3386 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
3387 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
3388 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
3389 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(-8), -16);")]
3390 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(-8), -24);")]
3391 #[unstable(feature = "int_roundings", issue = "88581")]
3393 #[must_use = "this returns the result of the operation, \
3394 without modifying the original"]
3395 #[inline]
3396 #[rustc_inherit_overflow_checks]
3397 #[cfg(not(feature = "ferrocene_certified"))]
3398 pub const fn next_multiple_of(self, rhs: Self) -> Self {
3399 if rhs == -1 {
3401 return self;
3402 }
3403
3404 let r = self % rhs;
3405 let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
3406 r + rhs
3407 } else {
3408 r
3409 };
3410
3411 if m == 0 {
3412 self
3413 } else {
3414 self + (rhs - m)
3415 }
3416 }
3417
3418 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(16));")]
3429 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(24));")]
3430 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
3431 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
3432 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
3433 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
3434 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-16));")]
3435 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-24));")]
3436 #[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".checked_next_multiple_of(0), None);")]
3437 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_next_multiple_of(2), None);")]
3438 #[unstable(feature = "int_roundings", issue = "88581")]
3440 #[must_use = "this returns the result of the operation, \
3441 without modifying the original"]
3442 #[inline]
3443 #[cfg(not(feature = "ferrocene_certified"))]
3444 pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
3445 if rhs == -1 {
3447 return Some(self);
3448 }
3449
3450 let r = try_opt!(self.checked_rem(rhs));
3451 let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
3452 r + rhs
3454 } else {
3455 r
3456 };
3457
3458 if m == 0 {
3459 Some(self)
3460 } else {
3461 self.checked_add(rhs - m)
3463 }
3464 }
3465
3466 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".ilog(5), 1);")]
3482 #[stable(feature = "int_log", since = "1.67.0")]
3484 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3485 #[must_use = "this returns the result of the operation, \
3486 without modifying the original"]
3487 #[inline]
3488 #[track_caller]
3489 #[cfg(not(feature = "ferrocene_certified"))]
3490 pub const fn ilog(self, base: Self) -> u32 {
3491 assert!(base >= 2, "base of integer logarithm must be at least 2");
3492 if let Some(log) = self.checked_ilog(base) {
3493 log
3494 } else {
3495 int_log10::panic_for_nonpositive_argument()
3496 }
3497 }
3498
3499 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".ilog2(), 1);")]
3509 #[stable(feature = "int_log", since = "1.67.0")]
3511 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3512 #[must_use = "this returns the result of the operation, \
3513 without modifying the original"]
3514 #[inline]
3515 #[track_caller]
3516 #[cfg(not(feature = "ferrocene_certified"))]
3517 pub const fn ilog2(self) -> u32 {
3518 if let Some(log) = self.checked_ilog2() {
3519 log
3520 } else {
3521 int_log10::panic_for_nonpositive_argument()
3522 }
3523 }
3524
3525 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".ilog10(), 1);")]
3535 #[stable(feature = "int_log", since = "1.67.0")]
3537 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3538 #[must_use = "this returns the result of the operation, \
3539 without modifying the original"]
3540 #[inline]
3541 #[track_caller]
3542 #[cfg(not(feature = "ferrocene_certified"))]
3543 pub const fn ilog10(self) -> u32 {
3544 if let Some(log) = self.checked_ilog10() {
3545 log
3546 } else {
3547 int_log10::panic_for_nonpositive_argument()
3548 }
3549 }
3550
3551 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_ilog(5), Some(1));")]
3564 #[stable(feature = "int_log", since = "1.67.0")]
3566 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3567 #[must_use = "this returns the result of the operation, \
3568 without modifying the original"]
3569 #[inline]
3570 #[cfg(not(feature = "ferrocene_certified"))]
3571 pub const fn checked_ilog(self, base: Self) -> Option<u32> {
3572 if self <= 0 || base <= 1 {
3573 None
3574 } else {
3575 (self as $UnsignedT).checked_ilog(base as $UnsignedT)
3578 }
3579 }
3580
3581 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_ilog2(), Some(1));")]
3589 #[stable(feature = "int_log", since = "1.67.0")]
3591 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3592 #[must_use = "this returns the result of the operation, \
3593 without modifying the original"]
3594 #[inline]
3595 #[cfg(not(feature = "ferrocene_certified"))]
3596 pub const fn checked_ilog2(self) -> Option<u32> {
3597 if self <= 0 {
3598 None
3599 } else {
3600 let log = (Self::BITS - 1) - unsafe { intrinsics::ctlz_nonzero(self) as u32 };
3602 Some(log)
3603 }
3604 }
3605
3606 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_ilog10(), Some(1));")]
3614 #[stable(feature = "int_log", since = "1.67.0")]
3616 #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3617 #[must_use = "this returns the result of the operation, \
3618 without modifying the original"]
3619 #[inline]
3620 #[cfg(not(feature = "ferrocene_certified"))]
3621 pub const fn checked_ilog10(self) -> Option<u32> {
3622 if self > 0 {
3623 Some(int_log10::$ActualT(self as $ActualT))
3624 } else {
3625 None
3626 }
3627 }
3628
3629 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
3635 #[doc = concat!("`", stringify!($SelfT), "`,")]
3637 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
3641 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".abs(), 10);")]
3648 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").abs(), 10);")]
3649 #[stable(feature = "rust1", since = "1.0.0")]
3651 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3652 #[allow(unused_attributes)]
3653 #[must_use = "this returns the result of the operation, \
3654 without modifying the original"]
3655 #[inline]
3656 #[rustc_inherit_overflow_checks]
3657 #[cfg(not(feature = "ferrocene_certified"))]
3658 pub const fn abs(self) -> Self {
3659 if self.is_negative() {
3663 -self
3664 } else {
3665 self
3666 }
3667 }
3668
3669 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(80), 20", stringify!($UnsignedT), ");")]
3678 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(110), 10", stringify!($UnsignedT), ");")]
3679 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(80), 180", stringify!($UnsignedT), ");")]
3680 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(-120), 20", stringify!($UnsignedT), ");")]
3681 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.abs_diff(", stringify!($SelfT), "::MAX), ", stringify!($UnsignedT), "::MAX);")]
3682 #[stable(feature = "int_abs_diff", since = "1.60.0")]
3684 #[rustc_const_stable(feature = "int_abs_diff", since = "1.60.0")]
3685 #[must_use = "this returns the result of the operation, \
3686 without modifying the original"]
3687 #[inline]
3688 #[cfg(not(feature = "ferrocene_certified"))]
3689 pub const fn abs_diff(self, other: Self) -> $UnsignedT {
3690 if self < other {
3691 (other as $UnsignedT).wrapping_sub(self as $UnsignedT)
3705 } else {
3706 (self as $UnsignedT).wrapping_sub(other as $UnsignedT)
3707 }
3708 }
3709
3710 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".signum(), 1);")]
3720 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".signum(), 0);")]
3721 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").signum(), -1);")]
3722 #[stable(feature = "rust1", since = "1.0.0")]
3724 #[rustc_const_stable(feature = "const_int_sign", since = "1.47.0")]
3725 #[must_use = "this returns the result of the operation, \
3726 without modifying the original"]
3727 #[inline(always)]
3728 #[cfg(not(feature = "ferrocene_certified"))]
3729 pub const fn signum(self) -> Self {
3730 crate::intrinsics::three_way_compare(self, 0) as Self
3736 }
3737
3738 #[doc = concat!("assert!(10", stringify!($SelfT), ".is_positive());")]
3745 #[doc = concat!("assert!(!(-10", stringify!($SelfT), ").is_positive());")]
3746 #[must_use]
3748 #[stable(feature = "rust1", since = "1.0.0")]
3749 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3750 #[inline(always)]
3751 #[cfg(not(feature = "ferrocene_certified"))]
3752 pub const fn is_positive(self) -> bool { self > 0 }
3753
3754 #[doc = concat!("assert!((-10", stringify!($SelfT), ").is_negative());")]
3761 #[doc = concat!("assert!(!10", stringify!($SelfT), ".is_negative());")]
3762 #[must_use]
3764 #[stable(feature = "rust1", since = "1.0.0")]
3765 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3766 #[inline(always)]
3767 #[cfg(not(feature = "ferrocene_certified"))]
3768 pub const fn is_negative(self) -> bool { self < 0 }
3769
3770 #[doc = $to_xe_bytes_doc]
3774 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();")]
3779 #[doc = concat!("assert_eq!(bytes, ", $be_bytes, ");")]
3780 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3782 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3783 #[must_use = "this returns the result of the operation, \
3784 without modifying the original"]
3785 #[inline]
3786 #[cfg(not(feature = "ferrocene_certified"))]
3787 pub const fn to_be_bytes(self) -> [u8; size_of::<Self>()] {
3788 self.to_be().to_ne_bytes()
3789 }
3790
3791 #[doc = $to_xe_bytes_doc]
3795 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();")]
3800 #[doc = concat!("assert_eq!(bytes, ", $le_bytes, ");")]
3801 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3803 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3804 #[must_use = "this returns the result of the operation, \
3805 without modifying the original"]
3806 #[inline]
3807 #[cfg(not(feature = "ferrocene_certified"))]
3808 pub const fn to_le_bytes(self) -> [u8; size_of::<Self>()] {
3809 self.to_le().to_ne_bytes()
3810 }
3811
3812 #[doc = $to_xe_bytes_doc]
3820 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_ne_bytes();")]
3828 #[doc = concat!(" ", $be_bytes)]
3832 #[doc = concat!(" ", $le_bytes)]
3834 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3838 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3839 #[allow(unnecessary_transmutes)]
3840 #[must_use = "this returns the result of the operation, \
3843 without modifying the original"]
3844 #[inline]
3845 #[cfg(not(feature = "ferrocene_certified"))]
3846 pub const fn to_ne_bytes(self) -> [u8; size_of::<Self>()] {
3847 unsafe { mem::transmute(self) }
3850 }
3851
3852 #[doc = $from_xe_bytes_doc]
3856 #[doc = concat!("let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, ");")]
3861 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3862 #[doc = concat!("fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3868 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3869 #[doc = concat!(" ", stringify!($SelfT), "::from_be_bytes(int_bytes.try_into().unwrap())")]
3871 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3874 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3875 #[must_use]
3876 #[inline]
3877 #[cfg(not(feature = "ferrocene_certified"))]
3878 pub const fn from_be_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3879 Self::from_be(Self::from_ne_bytes(bytes))
3880 }
3881
3882 #[doc = $from_xe_bytes_doc]
3886 #[doc = concat!("let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, ");")]
3891 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3892 #[doc = concat!("fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3898 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3899 #[doc = concat!(" ", stringify!($SelfT), "::from_le_bytes(int_bytes.try_into().unwrap())")]
3901 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3904 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3905 #[must_use]
3906 #[inline]
3907 #[cfg(not(feature = "ferrocene_certified"))]
3908 pub const fn from_le_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3909 Self::from_le(Self::from_ne_bytes(bytes))
3910 }
3911
3912 #[doc = $from_xe_bytes_doc]
3923 #[doc = concat!("let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"big\") {")]
3928 #[doc = concat!(" ", $be_bytes)]
3929 #[doc = concat!(" ", $le_bytes)]
3931 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3933 #[doc = concat!("fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3939 #[doc = concat!(" let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3940 #[doc = concat!(" ", stringify!($SelfT), "::from_ne_bytes(int_bytes.try_into().unwrap())")]
3942 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3945 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3946 #[allow(unnecessary_transmutes)]
3947 #[must_use]
3948 #[inline]
3951 #[cfg(not(feature = "ferrocene_certified"))]
3952 pub const fn from_ne_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3953 unsafe { mem::transmute(bytes) }
3955 }
3956
3957 #[doc = concat!("[`", stringify!($SelfT), "::MIN", "`] instead.")]
3959 #[stable(feature = "rust1", since = "1.0.0")]
3962 #[inline(always)]
3963 #[rustc_promotable]
3964 #[rustc_const_stable(feature = "const_min_value", since = "1.32.0")]
3965 #[deprecated(since = "TBD", note = "replaced by the `MIN` associated constant on this type")]
3966 #[rustc_diagnostic_item = concat!(stringify!($SelfT), "_legacy_fn_min_value")]
3967 #[cfg(not(feature = "ferrocene_certified"))]
3968 pub const fn min_value() -> Self {
3969 Self::MIN
3970 }
3971
3972 #[doc = concat!("[`", stringify!($SelfT), "::MAX", "`] instead.")]
3974 #[stable(feature = "rust1", since = "1.0.0")]
3977 #[inline(always)]
3978 #[rustc_promotable]
3979 #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
3980 #[deprecated(since = "TBD", note = "replaced by the `MAX` associated constant on this type")]
3981 #[rustc_diagnostic_item = concat!(stringify!($SelfT), "_legacy_fn_max_value")]
3982 #[cfg(not(feature = "ferrocene_certified"))]
3983 pub const fn max_value() -> Self {
3984 Self::MAX
3985 }
3986 }
3987}