1 macro_rules! int_impl {
2 ($SelfT:ty, $ActualT:ident, $UnsignedT:ty, $BITS:expr, $BITS_MINUS_ONE:expr, $Min:expr, $Max:expr,
3 $rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
4 $reversed:expr, $le_bytes:expr, $be_bytes:expr,
5 $to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr,
6 $bound_condition:expr) => {
7 /// The smallest value that can be represented by this integer type
8 #[doc = concat!("(−2<sup>", $BITS_MINUS_ONE, "</sup>", $bound_condition, ")")]
15 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN, ", stringify!($Min), ");")]
17 #[stable(feature = "assoc_int_consts", since = "1.43.0")]
18 pub const MIN: Self = !0 ^ ((!0 as $UnsignedT) >> 1) as Self;
20 /// The largest value that can be represented by this integer type
21 #[doc = concat!("(2<sup>", $BITS_MINUS_ONE, "</sup> − 1", $bound_condition, ")")]
28 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX, ", stringify!($Max), ");")]
30 #[stable(feature = "assoc_int_consts", since = "1.43.0")]
31 pub const MAX: Self = !Self::MIN;
33 /// The size of this integer type in bits.
38 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::BITS, ", stringify!($BITS), ");")]
40 #[stable(feature = "int_bits_const", since = "1.53.0")]
41 pub const BITS: u32 = $BITS;
43 /// Converts a string slice in a given base to an integer.
45 /// The string is expected to be an optional `+` or `-` sign followed by digits.
46 /// Leading and trailing whitespace represent an error. Digits are a subset of these characters,
47 /// depending on `radix`:
55 /// This function panics if `radix` is not in the range from 2 to 36.
62 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::from_str_radix(\"A\", 16), Ok(10));")]
64 #[stable(feature = "rust1", since = "1.0.0")]
65 pub fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> {
66 from_str_radix(src, radix)
69 /// Returns the number of ones in the binary representation of `self`.
76 #[doc = concat!("let n = 0b100_0000", stringify!($SelfT), ";")]
78 /// assert_eq!(n.count_ones(), 1);
81 #[stable(feature = "rust1", since = "1.0.0")]
82 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
83 #[doc(alias = "popcount")]
84 #[doc(alias = "popcnt")]
85 #[must_use = "this returns the result of the operation, \
86 without modifying the original"]
88 pub const fn count_ones(self) -> u32 { (self as $UnsignedT).count_ones() }
90 /// Returns the number of zeros in the binary representation of `self`.
97 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.count_zeros(), 1);")]
99 #[stable(feature = "rust1", since = "1.0.0")]
100 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
101 #[must_use = "this returns the result of the operation, \
102 without modifying the original"]
104 pub const fn count_zeros(self) -> u32 {
108 /// Returns the number of leading zeros in the binary representation of `self`.
110 /// Depending on what you're doing with the value, you might also be interested in the
111 /// [`ilog2`] function which returns a consistent number, even if the type widens.
118 #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
120 /// assert_eq!(n.leading_zeros(), 0);
122 #[doc = concat!("[`ilog2`]: ", stringify!($SelfT), "::ilog2")]
123 #[stable(feature = "rust1", since = "1.0.0")]
124 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
125 #[must_use = "this returns the result of the operation, \
126 without modifying the original"]
128 pub const fn leading_zeros(self) -> u32 {
129 (self as $UnsignedT).leading_zeros()
132 /// Returns the number of trailing zeros in the binary representation of `self`.
139 #[doc = concat!("let n = -4", stringify!($SelfT), ";")]
141 /// assert_eq!(n.trailing_zeros(), 2);
143 #[stable(feature = "rust1", since = "1.0.0")]
144 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
145 #[must_use = "this returns the result of the operation, \
146 without modifying the original"]
148 pub const fn trailing_zeros(self) -> u32 {
149 (self as $UnsignedT).trailing_zeros()
152 /// Returns the number of leading ones in the binary representation of `self`.
159 #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
161 #[doc = concat!("assert_eq!(n.leading_ones(), ", stringify!($BITS), ");")]
163 #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
164 #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
165 #[must_use = "this returns the result of the operation, \
166 without modifying the original"]
168 pub const fn leading_ones(self) -> u32 {
169 (self as $UnsignedT).leading_ones()
172 /// Returns the number of trailing ones in the binary representation of `self`.
179 #[doc = concat!("let n = 3", stringify!($SelfT), ";")]
181 /// assert_eq!(n.trailing_ones(), 2);
183 #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
184 #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
185 #[must_use = "this returns the result of the operation, \
186 without modifying the original"]
188 pub const fn trailing_ones(self) -> u32 {
189 (self as $UnsignedT).trailing_ones()
192 /// Shifts the bits to the left by a specified amount, `n`,
193 /// wrapping the truncated bits to the end of the resulting integer.
195 /// Please note this isn't the same operation as the `<<` shifting operator!
202 #[doc = concat!("let n = ", $rot_op, stringify!($SelfT), ";")]
203 #[doc = concat!("let m = ", $rot_result, ";")]
205 #[doc = concat!("assert_eq!(n.rotate_left(", $rot, "), m);")]
207 #[stable(feature = "rust1", since = "1.0.0")]
208 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
209 #[must_use = "this returns the result of the operation, \
210 without modifying the original"]
212 pub const fn rotate_left(self, n: u32) -> Self {
213 (self as $UnsignedT).rotate_left(n) as Self
216 /// Shifts the bits to the right by a specified amount, `n`,
217 /// wrapping the truncated bits to the beginning of the resulting
220 /// Please note this isn't the same operation as the `>>` shifting operator!
227 #[doc = concat!("let n = ", $rot_result, stringify!($SelfT), ";")]
228 #[doc = concat!("let m = ", $rot_op, ";")]
230 #[doc = concat!("assert_eq!(n.rotate_right(", $rot, "), m);")]
232 #[stable(feature = "rust1", since = "1.0.0")]
233 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
234 #[must_use = "this returns the result of the operation, \
235 without modifying the original"]
237 pub const fn rotate_right(self, n: u32) -> Self {
238 (self as $UnsignedT).rotate_right(n) as Self
241 /// Reverses the byte order of the integer.
248 #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
250 /// let m = n.swap_bytes();
252 #[doc = concat!("assert_eq!(m, ", $swapped, ");")]
254 #[stable(feature = "rust1", since = "1.0.0")]
255 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
256 #[must_use = "this returns the result of the operation, \
257 without modifying the original"]
259 pub const fn swap_bytes(self) -> Self {
260 (self as $UnsignedT).swap_bytes() as Self
263 /// Reverses the order of bits in the integer. The least significant bit becomes the most significant bit,
264 /// second least-significant bit becomes second most-significant bit, etc.
271 #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
272 /// let m = n.reverse_bits();
274 #[doc = concat!("assert_eq!(m, ", $reversed, ");")]
275 #[doc = concat!("assert_eq!(0, 0", stringify!($SelfT), ".reverse_bits());")]
277 #[stable(feature = "reverse_bits", since = "1.37.0")]
278 #[rustc_const_stable(feature = "reverse_bits", since = "1.37.0")]
279 #[must_use = "this returns the result of the operation, \
280 without modifying the original"]
282 pub const fn reverse_bits(self) -> Self {
283 (self as $UnsignedT).reverse_bits() as Self
286 /// Converts an integer from big endian to the target's endianness.
288 /// On big endian this is a no-op. On little endian the bytes are swapped.
295 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
297 /// if cfg!(target_endian = "big") {
298 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_be(n), n)")]
300 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_be(n), n.swap_bytes())")]
303 #[stable(feature = "rust1", since = "1.0.0")]
304 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
307 pub const fn from_be(x: Self) -> Self {
308 #[cfg(target_endian = "big")]
312 #[cfg(not(target_endian = "big"))]
318 /// Converts an integer from little endian to the target's endianness.
320 /// On little endian this is a no-op. On big endian the bytes are swapped.
327 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
329 /// if cfg!(target_endian = "little") {
330 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_le(n), n)")]
332 #[doc = concat!(" assert_eq!(", stringify!($SelfT), "::from_le(n), n.swap_bytes())")]
335 #[stable(feature = "rust1", since = "1.0.0")]
336 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
339 pub const fn from_le(x: Self) -> Self {
340 #[cfg(target_endian = "little")]
344 #[cfg(not(target_endian = "little"))]
350 /// Converts `self` to big endian from the target's endianness.
352 /// On big endian this is a no-op. On little endian the bytes are swapped.
359 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
361 /// if cfg!(target_endian = "big") {
362 /// assert_eq!(n.to_be(), n)
364 /// assert_eq!(n.to_be(), n.swap_bytes())
367 #[stable(feature = "rust1", since = "1.0.0")]
368 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
369 #[must_use = "this returns the result of the operation, \
370 without modifying the original"]
372 pub const fn to_be(self) -> Self { // or not to be?
373 #[cfg(target_endian = "big")]
377 #[cfg(not(target_endian = "big"))]
383 /// Converts `self` to little endian from the target's endianness.
385 /// On little endian this is a no-op. On big endian the bytes are swapped.
392 #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
394 /// if cfg!(target_endian = "little") {
395 /// assert_eq!(n.to_le(), n)
397 /// assert_eq!(n.to_le(), n.swap_bytes())
400 #[stable(feature = "rust1", since = "1.0.0")]
401 #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
402 #[must_use = "this returns the result of the operation, \
403 without modifying the original"]
405 pub const fn to_le(self) -> Self {
406 #[cfg(target_endian = "little")]
410 #[cfg(not(target_endian = "little"))]
416 /// Checked integer addition. Computes `self + rhs`, returning `None`
417 /// if overflow occurred.
424 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(1), Some(", stringify!($SelfT), "::MAX - 1));")]
425 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);")]
427 #[stable(feature = "rust1", since = "1.0.0")]
428 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
429 #[must_use = "this returns the result of the operation, \
430 without modifying the original"]
432 pub const fn checked_add(self, rhs: Self) -> Option<Self> {
433 let (a, b) = self.overflowing_add(rhs);
434 if unlikely!(b) {None} else {Some(a)}
437 /// Unchecked integer addition. Computes `self + rhs`, assuming overflow
442 /// This results in undefined behavior when
443 #[doc = concat!("`self + rhs > ", stringify!($SelfT), "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`,")]
444 /// i.e. when [`checked_add`] would return `None`.
446 #[doc = concat!("[`checked_add`]: ", stringify!($SelfT), "::checked_add")]
448 feature = "unchecked_math",
449 reason = "niche optimization path",
452 #[must_use = "this returns the result of the operation, \
453 without modifying the original"]
454 #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
456 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
457 pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
458 // SAFETY: the caller must uphold the safety contract for
460 unsafe { intrinsics::unchecked_add(self, rhs) }
463 /// Checked addition with an unsigned integer. Computes `self + rhs`,
464 /// returning `None` if overflow occurred.
471 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_add_unsigned(2), Some(3));")]
472 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add_unsigned(3), None);")]
474 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
475 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
476 #[must_use = "this returns the result of the operation, \
477 without modifying the original"]
479 pub const fn checked_add_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
480 let (a, b) = self.overflowing_add_unsigned(rhs);
481 if unlikely!(b) {None} else {Some(a)}
484 /// Checked integer subtraction. Computes `self - rhs`, returning `None` if
485 /// overflow occurred.
492 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub(1), Some(", stringify!($SelfT), "::MIN + 1));")]
493 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub(3), None);")]
495 #[stable(feature = "rust1", since = "1.0.0")]
496 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
497 #[must_use = "this returns the result of the operation, \
498 without modifying the original"]
500 pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
501 let (a, b) = self.overflowing_sub(rhs);
502 if unlikely!(b) {None} else {Some(a)}
505 /// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow
510 /// This results in undefined behavior when
511 #[doc = concat!("`self - rhs > ", stringify!($SelfT), "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`,")]
512 /// i.e. when [`checked_sub`] would return `None`.
514 #[doc = concat!("[`checked_sub`]: ", stringify!($SelfT), "::checked_sub")]
516 feature = "unchecked_math",
517 reason = "niche optimization path",
520 #[must_use = "this returns the result of the operation, \
521 without modifying the original"]
522 #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
524 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
525 pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
526 // SAFETY: the caller must uphold the safety contract for
528 unsafe { intrinsics::unchecked_sub(self, rhs) }
531 /// Checked subtraction with an unsigned integer. Computes `self - rhs`,
532 /// returning `None` if overflow occurred.
539 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_sub_unsigned(2), Some(-1));")]
540 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub_unsigned(3), None);")]
542 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
543 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
544 #[must_use = "this returns the result of the operation, \
545 without modifying the original"]
547 pub const fn checked_sub_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
548 let (a, b) = self.overflowing_sub_unsigned(rhs);
549 if unlikely!(b) {None} else {Some(a)}
552 /// Checked integer multiplication. Computes `self * rhs`, returning `None` if
553 /// overflow occurred.
560 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(1), Some(", stringify!($SelfT), "::MAX));")]
561 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);")]
563 #[stable(feature = "rust1", since = "1.0.0")]
564 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
565 #[must_use = "this returns the result of the operation, \
566 without modifying the original"]
568 pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
569 let (a, b) = self.overflowing_mul(rhs);
570 if unlikely!(b) {None} else {Some(a)}
573 /// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow
578 /// This results in undefined behavior when
579 #[doc = concat!("`self * rhs > ", stringify!($SelfT), "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`,")]
580 /// i.e. when [`checked_mul`] would return `None`.
582 #[doc = concat!("[`checked_mul`]: ", stringify!($SelfT), "::checked_mul")]
584 feature = "unchecked_math",
585 reason = "niche optimization path",
588 #[must_use = "this returns the result of the operation, \
589 without modifying the original"]
590 #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
592 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
593 pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
594 // SAFETY: the caller must uphold the safety contract for
596 unsafe { intrinsics::unchecked_mul(self, rhs) }
599 /// Checked integer division. Computes `self / rhs`, returning `None` if `rhs == 0`
600 /// or the division results in overflow.
607 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div(-1), Some(", stringify!($Max), "));")]
608 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div(-1), None);")]
609 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div(0), None);")]
611 #[stable(feature = "rust1", since = "1.0.0")]
612 #[rustc_const_stable(feature = "const_checked_int_div", since = "1.52.0")]
613 #[must_use = "this returns the result of the operation, \
614 without modifying the original"]
616 pub const fn checked_div(self, rhs: Self) -> Option<Self> {
617 if unlikely!(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
620 // SAFETY: div by zero and by INT_MIN have been checked above
621 Some(unsafe { intrinsics::unchecked_div(self, rhs) })
625 /// Checked Euclidean division. Computes `self.div_euclid(rhs)`,
626 /// returning `None` if `rhs == 0` or the division results in overflow.
633 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div_euclid(-1), Some(", stringify!($Max), "));")]
634 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div_euclid(-1), None);")]
635 #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div_euclid(0), None);")]
637 #[stable(feature = "euclidean_division", since = "1.38.0")]
638 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
639 #[must_use = "this returns the result of the operation, \
640 without modifying the original"]
642 pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
643 // Using `&` helps LLVM see that it is the same check made in division.
644 if unlikely!(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
647 Some(self.div_euclid(rhs))
651 /// Checked integer remainder. Computes `self % rhs`, returning `None` if
652 /// `rhs == 0` or the division results in overflow.
659 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(2), Some(1));")]
660 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(0), None);")]
661 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem(-1), None);")]
663 #[stable(feature = "wrapping", since = "1.7.0")]
664 #[rustc_const_stable(feature = "const_checked_int_div", since = "1.52.0")]
665 #[must_use = "this returns the result of the operation, \
666 without modifying the original"]
668 pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
669 if unlikely!(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
672 // SAFETY: div by zero and by INT_MIN have been checked above
673 Some(unsafe { intrinsics::unchecked_rem(self, rhs) })
677 /// Checked Euclidean remainder. Computes `self.rem_euclid(rhs)`, returning `None`
678 /// if `rhs == 0` or the division results in overflow.
685 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(2), Some(1));")]
686 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(0), None);")]
687 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem_euclid(-1), None);")]
689 #[stable(feature = "euclidean_division", since = "1.38.0")]
690 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
691 #[must_use = "this returns the result of the operation, \
692 without modifying the original"]
694 pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
695 // Using `&` helps LLVM see that it is the same check made in division.
696 if unlikely!(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
699 Some(self.rem_euclid(rhs))
703 /// Checked negation. Computes `-self`, returning `None` if `self == MIN`.
710 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_neg(), Some(-5));")]
711 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_neg(), None);")]
713 #[stable(feature = "wrapping", since = "1.7.0")]
714 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
715 #[must_use = "this returns the result of the operation, \
716 without modifying the original"]
718 pub const fn checked_neg(self) -> Option<Self> {
719 let (a, b) = self.overflowing_neg();
720 if unlikely!(b) {None} else {Some(a)}
723 /// Checked shift left. Computes `self << rhs`, returning `None` if `rhs` is larger
724 /// than or equal to the number of bits in `self`.
731 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(4), Some(0x10));")]
732 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(129), None);")]
734 #[stable(feature = "wrapping", since = "1.7.0")]
735 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
736 #[must_use = "this returns the result of the operation, \
737 without modifying the original"]
739 pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
740 let (a, b) = self.overflowing_shl(rhs);
741 if unlikely!(b) {None} else {Some(a)}
744 /// Unchecked shift left. Computes `self << rhs`, assuming that
745 /// `rhs` is less than the number of bits in `self`.
749 /// This results in undefined behavior if `rhs` is larger than
750 /// or equal to the number of bits in `self`,
751 /// i.e. when [`checked_shl`] would return `None`.
753 #[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
755 feature = "unchecked_math",
756 reason = "niche optimization path",
759 #[must_use = "this returns the result of the operation, \
760 without modifying the original"]
761 #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
763 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
764 pub const unsafe fn unchecked_shl(self, rhs: Self) -> Self {
765 // SAFETY: the caller must uphold the safety contract for
767 unsafe { intrinsics::unchecked_shl(self, rhs) }
770 /// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
771 /// larger than or equal to the number of bits in `self`.
778 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(4), Some(0x1));")]
779 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(128), None);")]
781 #[stable(feature = "wrapping", since = "1.7.0")]
782 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
783 #[must_use = "this returns the result of the operation, \
784 without modifying the original"]
786 pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
787 let (a, b) = self.overflowing_shr(rhs);
788 if unlikely!(b) {None} else {Some(a)}
791 /// Unchecked shift right. Computes `self >> rhs`, assuming that
792 /// `rhs` is less than the number of bits in `self`.
796 /// This results in undefined behavior if `rhs` is larger than
797 /// or equal to the number of bits in `self`,
798 /// i.e. when [`checked_shr`] would return `None`.
800 #[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
802 feature = "unchecked_math",
803 reason = "niche optimization path",
806 #[must_use = "this returns the result of the operation, \
807 without modifying the original"]
808 #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
810 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
811 pub const unsafe fn unchecked_shr(self, rhs: Self) -> Self {
812 // SAFETY: the caller must uphold the safety contract for
814 unsafe { intrinsics::unchecked_shr(self, rhs) }
817 /// Checked absolute value. Computes `self.abs()`, returning `None` if
825 #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_abs(), Some(5));")]
826 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_abs(), None);")]
828 #[stable(feature = "no_panic_abs", since = "1.13.0")]
829 #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
830 #[must_use = "this returns the result of the operation, \
831 without modifying the original"]
833 pub const fn checked_abs(self) -> Option<Self> {
834 if self.is_negative() {
841 /// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if
842 /// overflow occurred.
849 #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".checked_pow(2), Some(64));")]
850 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);")]
853 #[stable(feature = "no_panic_pow", since = "1.34.0")]
854 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
855 #[must_use = "this returns the result of the operation, \
856 without modifying the original"]
858 pub const fn checked_pow(self, mut exp: u32) -> Option<Self> {
863 let mut acc: Self = 1;
867 acc = try_opt!(acc.checked_mul(base));
870 base = try_opt!(base.checked_mul(base));
872 // since exp!=0, finally the exp must be 1.
873 // Deal with the final bit of the exponent separately, since
874 // squaring the base afterwards is not necessary and may cause a
875 // needless overflow.
876 acc.checked_mul(base)
879 /// Saturating integer addition. Computes `self + rhs`, saturating at the numeric
880 /// bounds instead of overflowing.
887 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_add(1), 101);")]
888 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add(100), ", stringify!($SelfT), "::MAX);")]
889 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_add(-1), ", stringify!($SelfT), "::MIN);")]
892 #[stable(feature = "rust1", since = "1.0.0")]
893 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
894 #[must_use = "this returns the result of the operation, \
895 without modifying the original"]
897 pub const fn saturating_add(self, rhs: Self) -> Self {
898 intrinsics::saturating_add(self, rhs)
901 /// Saturating addition with an unsigned integer. Computes `self + rhs`,
902 /// saturating at the numeric bounds instead of overflowing.
909 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".saturating_add_unsigned(2), 3);")]
910 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add_unsigned(100), ", stringify!($SelfT), "::MAX);")]
912 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
913 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
914 #[must_use = "this returns the result of the operation, \
915 without modifying the original"]
917 pub const fn saturating_add_unsigned(self, rhs: $UnsignedT) -> Self {
918 // Overflow can only happen at the upper bound
919 // We cannot use `unwrap_or` here because it is not `const`
920 match self.checked_add_unsigned(rhs) {
926 /// Saturating integer subtraction. Computes `self - rhs`, saturating at the
927 /// numeric bounds instead of overflowing.
934 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub(127), -27);")]
935 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub(100), ", stringify!($SelfT), "::MIN);")]
936 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_sub(-1), ", stringify!($SelfT), "::MAX);")]
938 #[stable(feature = "rust1", since = "1.0.0")]
939 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
940 #[must_use = "this returns the result of the operation, \
941 without modifying the original"]
943 pub const fn saturating_sub(self, rhs: Self) -> Self {
944 intrinsics::saturating_sub(self, rhs)
947 /// Saturating subtraction with an unsigned integer. Computes `self - rhs`,
948 /// saturating at the numeric bounds instead of overflowing.
955 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub_unsigned(127), -27);")]
956 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub_unsigned(100), ", stringify!($SelfT), "::MIN);")]
958 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
959 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
960 #[must_use = "this returns the result of the operation, \
961 without modifying the original"]
963 pub const fn saturating_sub_unsigned(self, rhs: $UnsignedT) -> Self {
964 // Overflow can only happen at the lower bound
965 // We cannot use `unwrap_or` here because it is not `const`
966 match self.checked_sub_unsigned(rhs) {
972 /// Saturating integer negation. Computes `-self`, returning `MAX` if `self == MIN`
973 /// instead of overflowing.
980 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_neg(), -100);")]
981 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_neg(), 100);")]
982 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_neg(), ", stringify!($SelfT), "::MAX);")]
983 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_neg(), ", stringify!($SelfT), "::MIN + 1);")]
986 #[stable(feature = "saturating_neg", since = "1.45.0")]
987 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
988 #[must_use = "this returns the result of the operation, \
989 without modifying the original"]
991 pub const fn saturating_neg(self) -> Self {
992 intrinsics::saturating_sub(0, self)
995 /// Saturating absolute value. Computes `self.abs()`, returning `MAX` if `self ==
996 /// MIN` instead of overflowing.
1003 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_abs(), 100);")]
1004 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_abs(), 100);")]
1005 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_abs(), ", stringify!($SelfT), "::MAX);")]
1006 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).saturating_abs(), ", stringify!($SelfT), "::MAX);")]
1009 #[stable(feature = "saturating_neg", since = "1.45.0")]
1010 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1011 #[must_use = "this returns the result of the operation, \
1012 without modifying the original"]
1014 pub const fn saturating_abs(self) -> Self {
1015 if self.is_negative() {
1016 self.saturating_neg()
1022 /// Saturating integer multiplication. Computes `self * rhs`, saturating at the
1023 /// numeric bounds instead of overflowing.
1030 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".saturating_mul(12), 120);")]
1031 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_mul(10), ", stringify!($SelfT), "::MAX);")]
1032 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_mul(10), ", stringify!($SelfT), "::MIN);")]
1034 #[stable(feature = "wrapping", since = "1.7.0")]
1035 #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1036 #[must_use = "this returns the result of the operation, \
1037 without modifying the original"]
1039 pub const fn saturating_mul(self, rhs: Self) -> Self {
1040 match self.checked_mul(rhs) {
1042 None => if (self < 0) == (rhs < 0) {
1050 /// Saturating integer division. Computes `self / rhs`, saturating at the
1051 /// numeric bounds instead of overflowing.
1058 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
1059 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_div(-1), ", stringify!($SelfT), "::MIN + 1);")]
1060 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_div(-1), ", stringify!($SelfT), "::MAX);")]
1065 #[doc = concat!("let _ = 1", stringify!($SelfT), ".saturating_div(0);")]
1068 #[stable(feature = "saturating_div", since = "1.58.0")]
1069 #[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
1070 #[must_use = "this returns the result of the operation, \
1071 without modifying the original"]
1073 pub const fn saturating_div(self, rhs: Self) -> Self {
1074 match self.overflowing_div(rhs) {
1075 (result, false) => result,
1076 (_result, true) => Self::MAX, // MIN / -1 is the only possible saturating overflow
1080 /// Saturating integer exponentiation. Computes `self.pow(exp)`,
1081 /// saturating at the numeric bounds instead of overflowing.
1088 #[doc = concat!("assert_eq!((-4", stringify!($SelfT), ").saturating_pow(3), -64);")]
1089 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(2), ", stringify!($SelfT), "::MAX);")]
1090 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(3), ", stringify!($SelfT), "::MIN);")]
1092 #[stable(feature = "no_panic_pow", since = "1.34.0")]
1093 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1094 #[must_use = "this returns the result of the operation, \
1095 without modifying the original"]
1097 pub const fn saturating_pow(self, exp: u32) -> Self {
1098 match self.checked_pow(exp) {
1100 None if self < 0 && exp % 2 == 1 => Self::MIN,
1105 /// Wrapping (modular) addition. Computes `self + rhs`, wrapping around at the
1106 /// boundary of the type.
1113 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add(27), 127);")]
1114 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add(2), ", stringify!($SelfT), "::MIN + 1);")]
1116 #[stable(feature = "rust1", since = "1.0.0")]
1117 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1118 #[must_use = "this returns the result of the operation, \
1119 without modifying the original"]
1121 pub const fn wrapping_add(self, rhs: Self) -> Self {
1122 intrinsics::wrapping_add(self, rhs)
1125 /// Wrapping (modular) addition with an unsigned integer. Computes
1126 /// `self + rhs`, wrapping around at the boundary of the type.
1133 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add_unsigned(27), 127);")]
1134 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add_unsigned(2), ", stringify!($SelfT), "::MIN + 1);")]
1136 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1137 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1138 #[must_use = "this returns the result of the operation, \
1139 without modifying the original"]
1141 pub const fn wrapping_add_unsigned(self, rhs: $UnsignedT) -> Self {
1142 self.wrapping_add(rhs as Self)
1145 /// Wrapping (modular) subtraction. Computes `self - rhs`, wrapping around at the
1146 /// boundary of the type.
1153 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub(127), -127);")]
1154 #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub(", stringify!($SelfT), "::MAX), ", stringify!($SelfT), "::MAX);")]
1156 #[stable(feature = "rust1", since = "1.0.0")]
1157 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1158 #[must_use = "this returns the result of the operation, \
1159 without modifying the original"]
1161 pub const fn wrapping_sub(self, rhs: Self) -> Self {
1162 intrinsics::wrapping_sub(self, rhs)
1165 /// Wrapping (modular) subtraction with an unsigned integer. Computes
1166 /// `self - rhs`, wrapping around at the boundary of the type.
1173 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub_unsigned(127), -127);")]
1174 #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub_unsigned(", stringify!($UnsignedT), "::MAX), -1);")]
1176 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1177 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1178 #[must_use = "this returns the result of the operation, \
1179 without modifying the original"]
1181 pub const fn wrapping_sub_unsigned(self, rhs: $UnsignedT) -> Self {
1182 self.wrapping_sub(rhs as Self)
1185 /// Wrapping (modular) multiplication. Computes `self * rhs`, wrapping around at
1186 /// the boundary of the type.
1193 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".wrapping_mul(12), 120);")]
1194 /// assert_eq!(11i8.wrapping_mul(12), -124);
1196 #[stable(feature = "rust1", since = "1.0.0")]
1197 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1198 #[must_use = "this returns the result of the operation, \
1199 without modifying the original"]
1201 pub const fn wrapping_mul(self, rhs: Self) -> Self {
1202 intrinsics::wrapping_mul(self, rhs)
1205 /// Wrapping (modular) division. Computes `self / rhs`, wrapping around at the
1206 /// boundary of the type.
1208 /// The only case where such wrapping can occur is when one divides `MIN / -1` on a signed type (where
1209 /// `MIN` is the negative minimal value for the type); this is equivalent to `-MIN`, a positive value
1210 /// that is too large to represent in the type. In such a case, this function returns `MIN` itself.
1214 /// This function will panic if `rhs` is 0.
1221 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div(10), 10);")]
1222 /// assert_eq!((-128i8).wrapping_div(-1), -128);
1224 #[stable(feature = "num_wrapping", since = "1.2.0")]
1225 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
1226 #[must_use = "this returns the result of the operation, \
1227 without modifying the original"]
1229 pub const fn wrapping_div(self, rhs: Self) -> Self {
1230 self.overflowing_div(rhs).0
1233 /// Wrapping Euclidean division. Computes `self.div_euclid(rhs)`,
1234 /// wrapping around at the boundary of the type.
1236 /// Wrapping will only occur in `MIN / -1` on a signed type (where `MIN` is the negative minimal value
1237 /// for the type). This is equivalent to `-MIN`, a positive value that is too large to represent in the
1238 /// type. In this case, this method returns `MIN` itself.
1242 /// This function will panic if `rhs` is 0.
1249 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div_euclid(10), 10);")]
1250 /// assert_eq!((-128i8).wrapping_div_euclid(-1), -128);
1252 #[stable(feature = "euclidean_division", since = "1.38.0")]
1253 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1254 #[must_use = "this returns the result of the operation, \
1255 without modifying the original"]
1257 pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
1258 self.overflowing_div_euclid(rhs).0
1261 /// Wrapping (modular) remainder. Computes `self % rhs`, wrapping around at the
1262 /// boundary of the type.
1264 /// Such wrap-around never actually occurs mathematically; implementation artifacts make `x % y`
1265 /// invalid for `MIN / -1` on a signed type (where `MIN` is the negative minimal value). In such a case,
1266 /// this function returns `0`.
1270 /// This function will panic if `rhs` is 0.
1277 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem(10), 0);")]
1278 /// assert_eq!((-128i8).wrapping_rem(-1), 0);
1280 #[stable(feature = "num_wrapping", since = "1.2.0")]
1281 #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
1282 #[must_use = "this returns the result of the operation, \
1283 without modifying the original"]
1285 pub const fn wrapping_rem(self, rhs: Self) -> Self {
1286 self.overflowing_rem(rhs).0
1289 /// Wrapping Euclidean remainder. Computes `self.rem_euclid(rhs)`, wrapping around
1290 /// at the boundary of the type.
1292 /// Wrapping will only occur in `MIN % -1` on a signed type (where `MIN` is the negative minimal value
1293 /// for the type). In this case, this method returns 0.
1297 /// This function will panic if `rhs` is 0.
1304 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);")]
1305 /// assert_eq!((-128i8).wrapping_rem_euclid(-1), 0);
1307 #[stable(feature = "euclidean_division", since = "1.38.0")]
1308 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1309 #[must_use = "this returns the result of the operation, \
1310 without modifying the original"]
1312 pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
1313 self.overflowing_rem_euclid(rhs).0
1316 /// Wrapping (modular) negation. Computes `-self`, wrapping around at the boundary
1319 /// The only case where such wrapping can occur is when one negates `MIN` on a signed type (where `MIN`
1320 /// is the negative minimal value for the type); this is a positive value that is too large to represent
1321 /// in the type. In such a case, this function returns `MIN` itself.
1328 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_neg(), -100);")]
1329 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_neg(), ", stringify!($SelfT), "::MIN);")]
1331 #[stable(feature = "num_wrapping", since = "1.2.0")]
1332 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1333 #[must_use = "this returns the result of the operation, \
1334 without modifying the original"]
1336 pub const fn wrapping_neg(self) -> Self {
1337 (0 as $SelfT).wrapping_sub(self)
1340 /// Panic-free bitwise shift-left; yields `self << mask(rhs)`, where `mask` removes
1341 /// any high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type.
1343 /// Note that this is *not* the same as a rotate-left; the RHS of a wrapping shift-left is restricted to
1344 /// the range of the type, rather than the bits shifted out of the LHS being returned to the other end.
1345 /// The primitive integer types all implement a [`rotate_left`](Self::rotate_left) function,
1346 /// which may be what you want instead.
1353 #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(7), -128);")]
1354 #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(128), -1);")]
1356 #[stable(feature = "num_wrapping", since = "1.2.0")]
1357 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1358 #[must_use = "this returns the result of the operation, \
1359 without modifying the original"]
1361 pub const fn wrapping_shl(self, rhs: u32) -> Self {
1362 // SAFETY: the masking by the bitsize of the type ensures that we do not shift
1365 intrinsics::unchecked_shl(self, (rhs & ($BITS - 1)) as $SelfT)
1369 /// Panic-free bitwise shift-right; yields `self >> mask(rhs)`, where `mask`
1370 /// removes any high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type.
1372 /// Note that this is *not* the same as a rotate-right; the RHS of a wrapping shift-right is restricted
1373 /// to the range of the type, rather than the bits shifted out of the LHS being returned to the other
1374 /// end. The primitive integer types all implement a [`rotate_right`](Self::rotate_right) function,
1375 /// which may be what you want instead.
1382 #[doc = concat!("assert_eq!((-128", stringify!($SelfT), ").wrapping_shr(7), -1);")]
1383 /// assert_eq!((-128i16).wrapping_shr(64), -128);
1385 #[stable(feature = "num_wrapping", since = "1.2.0")]
1386 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1387 #[must_use = "this returns the result of the operation, \
1388 without modifying the original"]
1390 pub const fn wrapping_shr(self, rhs: u32) -> Self {
1391 // SAFETY: the masking by the bitsize of the type ensures that we do not shift
1394 intrinsics::unchecked_shr(self, (rhs & ($BITS - 1)) as $SelfT)
1398 /// Wrapping (modular) absolute value. Computes `self.abs()`, wrapping around at
1399 /// the boundary of the type.
1401 /// The only case where such wrapping can occur is when one takes the absolute value of the negative
1402 /// minimal value for the type; this is a positive value that is too large to represent in the type. In
1403 /// such a case, this function returns `MIN` itself.
1410 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_abs(), 100);")]
1411 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_abs(), 100);")]
1412 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_abs(), ", stringify!($SelfT), "::MIN);")]
1413 /// assert_eq!((-128i8).wrapping_abs() as u8, 128);
1415 #[stable(feature = "no_panic_abs", since = "1.13.0")]
1416 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1417 #[must_use = "this returns the result of the operation, \
1418 without modifying the original"]
1419 #[allow(unused_attributes)]
1421 pub const fn wrapping_abs(self) -> Self {
1422 if self.is_negative() {
1429 /// Computes the absolute value of `self` without any wrapping
1438 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".unsigned_abs(), 100", stringify!($UnsignedT), ");")]
1439 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").unsigned_abs(), 100", stringify!($UnsignedT), ");")]
1440 /// assert_eq!((-128i8).unsigned_abs(), 128u8);
1442 #[stable(feature = "unsigned_abs", since = "1.51.0")]
1443 #[rustc_const_stable(feature = "unsigned_abs", since = "1.51.0")]
1444 #[must_use = "this returns the result of the operation, \
1445 without modifying the original"]
1447 pub const fn unsigned_abs(self) -> $UnsignedT {
1448 self.wrapping_abs() as $UnsignedT
1451 /// Wrapping (modular) exponentiation. Computes `self.pow(exp)`,
1452 /// wrapping around at the boundary of the type.
1459 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".wrapping_pow(4), 81);")]
1460 /// assert_eq!(3i8.wrapping_pow(5), -13);
1461 /// assert_eq!(3i8.wrapping_pow(6), -39);
1463 #[stable(feature = "no_panic_pow", since = "1.34.0")]
1464 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1465 #[must_use = "this returns the result of the operation, \
1466 without modifying the original"]
1468 pub const fn wrapping_pow(self, mut exp: u32) -> Self {
1472 let mut base = self;
1473 let mut acc: Self = 1;
1477 acc = acc.wrapping_mul(base);
1480 base = base.wrapping_mul(base);
1483 // since exp!=0, finally the exp must be 1.
1484 // Deal with the final bit of the exponent separately, since
1485 // squaring the base afterwards is not necessary and may cause a
1486 // needless overflow.
1487 acc.wrapping_mul(base)
1490 /// Calculates `self` + `rhs`
1492 /// Returns a tuple of the addition along with a boolean indicating whether an arithmetic overflow would
1493 /// occur. If an overflow would have occurred then the wrapped value is returned.
1500 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_add(2), (7, false));")]
1501 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.overflowing_add(1), (", stringify!($SelfT), "::MIN, true));")]
1503 #[stable(feature = "wrapping", since = "1.7.0")]
1504 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1505 #[must_use = "this returns the result of the operation, \
1506 without modifying the original"]
1508 pub const fn overflowing_add(self, rhs: Self) -> (Self, bool) {
1509 let (a, b) = intrinsics::add_with_overflow(self as $ActualT, rhs as $ActualT);
1513 /// Calculates `self + rhs + carry` without the ability to overflow.
1515 /// Performs "signed ternary addition" which takes in an extra bit to add, and may return an
1516 /// additional bit of overflow. This signed function is used only on the highest-ordered data,
1517 /// for which the signed overflow result indicates whether the big integer overflowed or not.
1524 /// #![feature(bigint_helper_methods)]
1525 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".carrying_add(2, false), (7, false));")]
1526 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".carrying_add(2, true), (8, false));")]
1527 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.carrying_add(1, false), (", stringify!($SelfT), "::MIN, true));")]
1528 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.carrying_add(0, true), (", stringify!($SelfT), "::MIN, true));")]
1529 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.carrying_add(1, true), (", stringify!($SelfT), "::MIN + 1, true));")]
1530 #[doc = concat!("assert_eq!(",
1531 stringify!($SelfT), "::MAX.carrying_add(", stringify!($SelfT), "::MAX, true), ",
1534 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.carrying_add(-1, true), (", stringify!($SelfT), "::MIN, false));")]
1535 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".carrying_add(", stringify!($SelfT), "::MAX, true), (", stringify!($SelfT), "::MIN, true));")]
1538 /// If `carry` is false, this method is equivalent to [`overflowing_add`](Self::overflowing_add):
1541 /// #![feature(bigint_helper_methods)]
1542 #[doc = concat!("assert_eq!(5_", stringify!($SelfT), ".carrying_add(2, false), 5_", stringify!($SelfT), ".overflowing_add(2));")]
1543 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.carrying_add(1, false), ", stringify!($SelfT), "::MAX.overflowing_add(1));")]
1545 #[unstable(feature = "bigint_helper_methods", issue = "85532")]
1546 #[rustc_const_unstable(feature = "const_bigint_helper_methods", issue = "85532")]
1547 #[must_use = "this returns the result of the operation, \
1548 without modifying the original"]
1550 pub const fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool) {
1551 // note: longer-term this should be done via an intrinsic.
1552 // note: no intermediate overflow is required (https://github.com/rust-lang/rust/issues/85532#issuecomment-1032214946).
1553 let (a, b) = self.overflowing_add(rhs);
1554 let (c, d) = a.overflowing_add(carry as $SelfT);
1558 /// Calculates `self` + `rhs` with an unsigned `rhs`
1560 /// Returns a tuple of the addition along with a boolean indicating
1561 /// whether an arithmetic overflow would occur. If an overflow would
1562 /// have occurred then the wrapped value is returned.
1569 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_add_unsigned(2), (3, false));")]
1570 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_add_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MAX, false));")]
1571 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).overflowing_add_unsigned(3), (", stringify!($SelfT), "::MIN, true));")]
1573 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1574 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1575 #[must_use = "this returns the result of the operation, \
1576 without modifying the original"]
1578 pub const fn overflowing_add_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
1579 let rhs = rhs as Self;
1580 let (res, overflowed) = self.overflowing_add(rhs);
1581 (res, overflowed ^ (rhs < 0))
1584 /// Calculates `self` - `rhs`
1586 /// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow
1587 /// would occur. If an overflow would have occurred then the wrapped value is returned.
1594 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_sub(2), (3, false));")]
1595 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_sub(1), (", stringify!($SelfT), "::MAX, true));")]
1597 #[stable(feature = "wrapping", since = "1.7.0")]
1598 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1599 #[must_use = "this returns the result of the operation, \
1600 without modifying the original"]
1602 pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
1603 let (a, b) = intrinsics::sub_with_overflow(self as $ActualT, rhs as $ActualT);
1607 /// Calculates `self - rhs - borrow` without the ability to overflow.
1609 /// Performs "signed ternary subtraction" which takes in an extra bit to subtract, and may return an
1610 /// additional bit of overflow. This signed function is used only on the highest-ordered data,
1611 /// for which the signed overflow result indicates whether the big integer overflowed or not.
1618 /// #![feature(bigint_helper_methods)]
1619 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".borrowing_sub(2, false), (3, false));")]
1620 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".borrowing_sub(2, true), (2, false));")]
1621 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".borrowing_sub(1, false), (-1, false));")]
1622 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".borrowing_sub(1, true), (-2, false));")]
1623 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.borrowing_sub(1, true), (", stringify!($SelfT), "::MAX - 1, true));")]
1624 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.borrowing_sub(-1, false), (", stringify!($SelfT), "::MIN, true));")]
1625 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.borrowing_sub(-1, true), (", stringify!($SelfT), "::MAX, false));")]
1627 #[unstable(feature = "bigint_helper_methods", issue = "85532")]
1628 #[rustc_const_unstable(feature = "const_bigint_helper_methods", issue = "85532")]
1629 #[must_use = "this returns the result of the operation, \
1630 without modifying the original"]
1632 pub const fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
1633 // note: longer-term this should be done via an intrinsic.
1634 // note: no intermediate overflow is required (https://github.com/rust-lang/rust/issues/85532#issuecomment-1032214946).
1635 let (a, b) = self.overflowing_sub(rhs);
1636 let (c, d) = a.overflowing_sub(borrow as $SelfT);
1640 /// Calculates `self` - `rhs` with an unsigned `rhs`
1642 /// Returns a tuple of the subtraction along with a boolean indicating
1643 /// whether an arithmetic overflow would occur. If an overflow would
1644 /// have occurred then the wrapped value is returned.
1651 #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_sub_unsigned(2), (-1, false));")]
1652 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX).overflowing_sub_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MIN, false));")]
1653 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).overflowing_sub_unsigned(3), (", stringify!($SelfT), "::MAX, true));")]
1655 #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1656 #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1657 #[must_use = "this returns the result of the operation, \
1658 without modifying the original"]
1660 pub const fn overflowing_sub_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
1661 let rhs = rhs as Self;
1662 let (res, overflowed) = self.overflowing_sub(rhs);
1663 (res, overflowed ^ (rhs < 0))
1666 /// Calculates the multiplication of `self` and `rhs`.
1668 /// Returns a tuple of the multiplication along with a boolean indicating whether an arithmetic overflow
1669 /// would occur. If an overflow would have occurred then the wrapped value is returned.
1676 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_mul(2), (10, false));")]
1677 /// assert_eq!(1_000_000_000i32.overflowing_mul(10), (1410065408, true));
1679 #[stable(feature = "wrapping", since = "1.7.0")]
1680 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1681 #[must_use = "this returns the result of the operation, \
1682 without modifying the original"]
1684 pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
1685 let (a, b) = intrinsics::mul_with_overflow(self as $ActualT, rhs as $ActualT);
1689 /// Calculates the divisor when `self` is divided by `rhs`.
1691 /// Returns a tuple of the divisor along with a boolean indicating whether an arithmetic overflow would
1692 /// occur. If an overflow would occur then self is returned.
1696 /// This function will panic if `rhs` is 0.
1703 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div(2), (2, false));")]
1704 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div(-1), (", stringify!($SelfT), "::MIN, true));")]
1707 #[stable(feature = "wrapping", since = "1.7.0")]
1708 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
1709 #[must_use = "this returns the result of the operation, \
1710 without modifying the original"]
1711 pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
1712 // Using `&` helps LLVM see that it is the same check made in division.
1713 if unlikely!((self == Self::MIN) & (rhs == -1)) {
1720 /// Calculates the quotient of Euclidean division `self.div_euclid(rhs)`.
1722 /// Returns a tuple of the divisor along with a boolean indicating whether an arithmetic overflow would
1723 /// occur. If an overflow would occur then `self` is returned.
1727 /// This function will panic if `rhs` is 0.
1734 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div_euclid(2), (2, false));")]
1735 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringify!($SelfT), "::MIN, true));")]
1738 #[stable(feature = "euclidean_division", since = "1.38.0")]
1739 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1740 #[must_use = "this returns the result of the operation, \
1741 without modifying the original"]
1742 pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
1743 // Using `&` helps LLVM see that it is the same check made in division.
1744 if unlikely!((self == Self::MIN) & (rhs == -1)) {
1747 (self.div_euclid(rhs), false)
1751 /// Calculates the remainder when `self` is divided by `rhs`.
1753 /// Returns a tuple of the remainder after dividing along with a boolean indicating whether an
1754 /// arithmetic overflow would occur. If an overflow would occur then 0 is returned.
1758 /// This function will panic if `rhs` is 0.
1765 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem(2), (1, false));")]
1766 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem(-1), (0, true));")]
1769 #[stable(feature = "wrapping", since = "1.7.0")]
1770 #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
1771 #[must_use = "this returns the result of the operation, \
1772 without modifying the original"]
1773 pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
1774 if unlikely!(rhs == -1) {
1775 (0, self == Self::MIN)
1782 /// Overflowing Euclidean remainder. Calculates `self.rem_euclid(rhs)`.
1784 /// Returns a tuple of the remainder after dividing along with a boolean indicating whether an
1785 /// arithmetic overflow would occur. If an overflow would occur then 0 is returned.
1789 /// This function will panic if `rhs` is 0.
1796 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem_euclid(2), (1, false));")]
1797 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));")]
1799 #[stable(feature = "euclidean_division", since = "1.38.0")]
1800 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1801 #[must_use = "this returns the result of the operation, \
1802 without modifying the original"]
1804 pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
1805 if unlikely!(rhs == -1) {
1806 (0, self == Self::MIN)
1808 (self.rem_euclid(rhs), false)
1813 /// Negates self, overflowing if this is equal to the minimum value.
1815 /// Returns a tuple of the negated version of self along with a boolean indicating whether an overflow
1816 /// happened. If `self` is the minimum value (e.g., `i32::MIN` for values of type `i32`), then the
1817 /// minimum value will be returned again and `true` will be returned for an overflow happening.
1824 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2, false));")]
1825 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($SelfT), "::MIN, true));")]
1828 #[stable(feature = "wrapping", since = "1.7.0")]
1829 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1830 #[must_use = "this returns the result of the operation, \
1831 without modifying the original"]
1832 #[allow(unused_attributes)]
1833 pub const fn overflowing_neg(self) -> (Self, bool) {
1834 if unlikely!(self == Self::MIN) {
1841 /// Shifts self left by `rhs` bits.
1843 /// Returns a tuple of the shifted version of self along with a boolean indicating whether the shift
1844 /// value was larger than or equal to the number of bits. If the shift value is too large, then value is
1845 /// masked (N-1) where N is the number of bits, and this value is then used to perform the shift.
1852 #[doc = concat!("assert_eq!(0x1", stringify!($SelfT),".overflowing_shl(4), (0x10, false));")]
1853 /// assert_eq!(0x1i32.overflowing_shl(36), (0x10, true));
1855 #[stable(feature = "wrapping", since = "1.7.0")]
1856 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1857 #[must_use = "this returns the result of the operation, \
1858 without modifying the original"]
1860 pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
1861 (self.wrapping_shl(rhs), (rhs > ($BITS - 1)))
1864 /// Shifts self right by `rhs` bits.
1866 /// Returns a tuple of the shifted version of self along with a boolean indicating whether the shift
1867 /// value was larger than or equal to the number of bits. If the shift value is too large, then value is
1868 /// masked (N-1) where N is the number of bits, and this value is then used to perform the shift.
1875 #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shr(4), (0x1, false));")]
1876 /// assert_eq!(0x10i32.overflowing_shr(36), (0x1, true));
1878 #[stable(feature = "wrapping", since = "1.7.0")]
1879 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1880 #[must_use = "this returns the result of the operation, \
1881 without modifying the original"]
1883 pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
1884 (self.wrapping_shr(rhs), (rhs > ($BITS - 1)))
1887 /// Computes the absolute value of `self`.
1889 /// Returns a tuple of the absolute version of self along with a boolean indicating whether an overflow
1890 /// happened. If self is the minimum value
1891 #[doc = concat!("(e.g., ", stringify!($SelfT), "::MIN for values of type ", stringify!($SelfT), "),")]
1892 /// then the minimum value will be returned again and true will be returned
1893 /// for an overflow happening.
1900 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".overflowing_abs(), (10, false));")]
1901 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").overflowing_abs(), (10, false));")]
1902 #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_abs(), (", stringify!($SelfT), "::MIN, true));")]
1904 #[stable(feature = "no_panic_abs", since = "1.13.0")]
1905 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1906 #[must_use = "this returns the result of the operation, \
1907 without modifying the original"]
1909 pub const fn overflowing_abs(self) -> (Self, bool) {
1910 (self.wrapping_abs(), self == Self::MIN)
1913 /// Raises self to the power of `exp`, using exponentiation by squaring.
1915 /// Returns a tuple of the exponentiation along with a bool indicating
1916 /// whether an overflow happened.
1923 #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".overflowing_pow(4), (81, false));")]
1924 /// assert_eq!(3i8.overflowing_pow(5), (-13, true));
1926 #[stable(feature = "no_panic_pow", since = "1.34.0")]
1927 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1928 #[must_use = "this returns the result of the operation, \
1929 without modifying the original"]
1931 pub const fn overflowing_pow(self, mut exp: u32) -> (Self, bool) {
1935 let mut base = self;
1936 let mut acc: Self = 1;
1937 let mut overflown = false;
1938 // Scratch space for storing results of overflowing_mul.
1943 r = acc.overflowing_mul(base);
1948 r = base.overflowing_mul(base);
1953 // since exp!=0, finally the exp must be 1.
1954 // Deal with the final bit of the exponent separately, since
1955 // squaring the base afterwards is not necessary and may cause a
1956 // needless overflow.
1957 r = acc.overflowing_mul(base);
1962 /// Raises self to the power of `exp`, using exponentiation by squaring.
1969 #[doc = concat!("let x: ", stringify!($SelfT), " = 2; // or any other integer type")]
1971 /// assert_eq!(x.pow(5), 32);
1973 #[stable(feature = "rust1", since = "1.0.0")]
1974 #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1975 #[must_use = "this returns the result of the operation, \
1976 without modifying the original"]
1978 #[rustc_inherit_overflow_checks]
1979 pub const fn pow(self, mut exp: u32) -> Self {
1983 let mut base = self;
1994 // since exp!=0, finally the exp must be 1.
1995 // Deal with the final bit of the exponent separately, since
1996 // squaring the base afterwards is not necessary and may cause a
1997 // needless overflow.
2001 /// Calculates the quotient of Euclidean division of `self` by `rhs`.
2003 /// This computes the integer `q` such that `self = q * rhs + r`, with
2004 /// `r = self.rem_euclid(rhs)` and `0 <= r < abs(rhs)`.
2006 /// In other words, the result is `self / rhs` rounded to the integer `q`
2007 /// such that `self >= q * rhs`.
2008 /// If `self > 0`, this is equal to round towards zero (the default in Rust);
2009 /// if `self < 0`, this is equal to round towards +/- infinity.
2013 /// This function will panic if `rhs` is 0 or the division results in overflow.
2020 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
2023 /// assert_eq!(a.div_euclid(b), 1); // 7 >= 4 * 1
2024 /// assert_eq!(a.div_euclid(-b), -1); // 7 >= -4 * -1
2025 /// assert_eq!((-a).div_euclid(b), -2); // -7 >= 4 * -2
2026 /// assert_eq!((-a).div_euclid(-b), 2); // -7 >= -4 * 2
2028 #[stable(feature = "euclidean_division", since = "1.38.0")]
2029 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2030 #[must_use = "this returns the result of the operation, \
2031 without modifying the original"]
2033 #[rustc_inherit_overflow_checks]
2034 pub const fn div_euclid(self, rhs: Self) -> Self {
2037 return if rhs > 0 { q - 1 } else { q + 1 }
2043 /// Calculates the least nonnegative remainder of `self (mod rhs)`.
2045 /// This is done as if by the Euclidean division algorithm -- given
2046 /// `r = self.rem_euclid(rhs)`, `self = rhs * self.div_euclid(rhs) + r`, and
2047 /// `0 <= r < abs(rhs)`.
2051 /// This function will panic if `rhs` is 0 or the division results in overflow.
2058 #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
2061 /// assert_eq!(a.rem_euclid(b), 3);
2062 /// assert_eq!((-a).rem_euclid(b), 1);
2063 /// assert_eq!(a.rem_euclid(-b), 3);
2064 /// assert_eq!((-a).rem_euclid(-b), 1);
2066 #[stable(feature = "euclidean_division", since = "1.38.0")]
2067 #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2068 #[must_use = "this returns the result of the operation, \
2069 without modifying the original"]
2071 #[rustc_inherit_overflow_checks]
2072 pub const fn rem_euclid(self, rhs: Self) -> Self {
2075 // Semantically equivalent to `if rhs < 0 { r - rhs } else { r + rhs }`.
2076 // If `rhs` is not `Self::MIN`, then `r + abs(rhs)` will not overflow
2077 // and is clearly equivalent, because `r` is negative.
2078 // Otherwise, `rhs` is `Self::MIN`, then we have
2079 // `r.wrapping_add(Self::MIN.wrapping_abs())`, which evaluates
2080 // to `r.wrapping_add(Self::MIN)`, which is equivalent to
2081 // `r - Self::MIN`, which is what we wanted (and will not overflow
2082 // for negative `r`).
2083 r.wrapping_add(rhs.wrapping_abs())
2089 /// Calculates the quotient of `self` and `rhs`, rounding the result towards negative infinity.
2093 /// This function will panic if `rhs` is zero.
2095 /// ## Overflow behavior
2097 /// On overflow, this function will panic if overflow checks are enabled (default in debug
2098 /// mode) and wrap if overflow checks are disabled (default in release mode).
2105 /// #![feature(int_roundings)]
2106 #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
2109 /// assert_eq!(a.div_floor(b), 2);
2110 /// assert_eq!(a.div_floor(-b), -3);
2111 /// assert_eq!((-a).div_floor(b), -3);
2112 /// assert_eq!((-a).div_floor(-b), 2);
2114 #[unstable(feature = "int_roundings", issue = "88581")]
2115 #[must_use = "this returns the result of the operation, \
2116 without modifying the original"]
2118 #[rustc_inherit_overflow_checks]
2119 pub const fn div_floor(self, rhs: Self) -> Self {
2122 if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
2129 /// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
2133 /// This function will panic if `rhs` is zero.
2135 /// ## Overflow behavior
2137 /// On overflow, this function will panic if overflow checks are enabled (default in debug
2138 /// mode) and wrap if overflow checks are disabled (default in release mode).
2145 /// #![feature(int_roundings)]
2146 #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
2149 /// assert_eq!(a.div_ceil(b), 3);
2150 /// assert_eq!(a.div_ceil(-b), -2);
2151 /// assert_eq!((-a).div_ceil(b), -2);
2152 /// assert_eq!((-a).div_ceil(-b), 3);
2154 #[unstable(feature = "int_roundings", issue = "88581")]
2155 #[must_use = "this returns the result of the operation, \
2156 without modifying the original"]
2158 #[rustc_inherit_overflow_checks]
2159 pub const fn div_ceil(self, rhs: Self) -> Self {
2162 if (r > 0 && rhs > 0) || (r < 0 && rhs < 0) {
2169 /// If `rhs` is positive, calculates the smallest value greater than or
2170 /// equal to `self` that is a multiple of `rhs`. If `rhs` is negative,
2171 /// calculates the largest value less than or equal to `self` that is a
2172 /// multiple of `rhs`.
2176 /// This function will panic if `rhs` is zero.
2178 /// ## Overflow behavior
2180 /// On overflow, this function will panic if overflow checks are enabled (default in debug
2181 /// mode) and wrap if overflow checks are disabled (default in release mode).
2188 /// #![feature(int_roundings)]
2189 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(8), 16);")]
2190 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(8), 24);")]
2191 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
2192 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
2193 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
2194 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
2195 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(-8), -16);")]
2196 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(-8), -24);")]
2198 #[unstable(feature = "int_roundings", issue = "88581")]
2199 #[must_use = "this returns the result of the operation, \
2200 without modifying the original"]
2202 #[rustc_inherit_overflow_checks]
2203 pub const fn next_multiple_of(self, rhs: Self) -> Self {
2204 // This would otherwise fail when calculating `r` when self == T::MIN.
2210 let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
2223 /// If `rhs` is positive, calculates the smallest value greater than or
2224 /// equal to `self` that is a multiple of `rhs`. If `rhs` is negative,
2225 /// calculates the largest value less than or equal to `self` that is a
2226 /// multiple of `rhs`. Returns `None` if `rhs` is zero or the operation
2227 /// would result in overflow.
2234 /// #![feature(int_roundings)]
2235 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(16));")]
2236 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(24));")]
2237 #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
2238 #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
2239 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
2240 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
2241 #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-16));")]
2242 #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-24));")]
2243 #[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".checked_next_multiple_of(0), None);")]
2244 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_next_multiple_of(2), None);")]
2246 #[unstable(feature = "int_roundings", issue = "88581")]
2247 #[must_use = "this returns the result of the operation, \
2248 without modifying the original"]
2250 pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
2251 // This would otherwise fail when calculating `r` when self == T::MIN.
2256 let r = try_opt!(self.checked_rem(rhs));
2257 let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
2258 // r + rhs cannot overflow because they have opposite signs
2267 // rhs - m cannot overflow because m has the same sign as rhs
2268 self.checked_add(rhs - m)
2272 /// Returns the logarithm of the number with respect to an arbitrary base,
2275 /// This method might not be optimized owing to implementation details;
2276 /// `ilog2` can produce results more efficiently for base 2, and `ilog10`
2277 /// can produce results more efficiently for base 10.
2281 /// This function will panic if `self` is less than or equal to zero,
2282 /// or if `base` is less than 2.
2287 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".ilog(5), 1);")]
2289 #[stable(feature = "int_log", since = "CURRENT_RUSTC_VERSION")]
2290 #[rustc_const_stable(feature = "int_log", since = "CURRENT_RUSTC_VERSION")]
2291 #[rustc_allow_const_fn_unstable(const_option)]
2292 #[must_use = "this returns the result of the operation, \
2293 without modifying the original"]
2296 pub const fn ilog(self, base: Self) -> u32 {
2297 assert!(base >= 2, "base of integer logarithm must be at least 2");
2298 self.checked_ilog(base).expect("argument of integer logarithm must be positive")
2301 /// Returns the base 2 logarithm of the number, rounded down.
2305 /// This function will panic if `self` is less than or equal to zero.
2310 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".ilog2(), 1);")]
2312 #[stable(feature = "int_log", since = "CURRENT_RUSTC_VERSION")]
2313 #[rustc_const_stable(feature = "int_log", since = "CURRENT_RUSTC_VERSION")]
2314 #[rustc_allow_const_fn_unstable(const_option)]
2315 #[must_use = "this returns the result of the operation, \
2316 without modifying the original"]
2319 pub const fn ilog2(self) -> u32 {
2320 self.checked_ilog2().expect("argument of integer logarithm must be positive")
2323 /// Returns the base 10 logarithm of the number, rounded down.
2327 /// This function will panic if `self` is less than or equal to zero.
2332 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".ilog10(), 1);")]
2334 #[stable(feature = "int_log", since = "CURRENT_RUSTC_VERSION")]
2335 #[rustc_const_stable(feature = "int_log", since = "CURRENT_RUSTC_VERSION")]
2336 #[rustc_allow_const_fn_unstable(const_option)]
2337 #[must_use = "this returns the result of the operation, \
2338 without modifying the original"]
2341 pub const fn ilog10(self) -> u32 {
2342 self.checked_ilog10().expect("argument of integer logarithm must be positive")
2345 /// Returns the logarithm of the number with respect to an arbitrary base,
2348 /// Returns `None` if the number is negative or zero, or if the base is not at least 2.
2350 /// This method might not be optimized owing to implementation details;
2351 /// `checked_ilog2` can produce results more efficiently for base 2, and
2352 /// `checked_ilog10` can produce results more efficiently for base 10.
2357 #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_ilog(5), Some(1));")]
2359 #[stable(feature = "int_log", since = "CURRENT_RUSTC_VERSION")]
2360 #[rustc_const_stable(feature = "int_log", since = "CURRENT_RUSTC_VERSION")]
2361 #[must_use = "this returns the result of the operation, \
2362 without modifying the original"]
2364 pub const fn checked_ilog(self, base: Self) -> Option<u32> {
2365 if self <= 0 || base <= 1 {
2371 // Optimization for 128 bit wide integers.
2372 if Self::BITS == 128 {
2373 let b = Self::ilog2(self) / (Self::ilog2(base) + 1);
2375 r /= base.pow(b as u32);
2386 /// Returns the base 2 logarithm of the number, rounded down.
2388 /// Returns `None` if the number is negative or zero.
2393 #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_ilog2(), Some(1));")]
2395 #[stable(feature = "int_log", since = "CURRENT_RUSTC_VERSION")]
2396 #[rustc_const_stable(feature = "int_log", since = "CURRENT_RUSTC_VERSION")]
2397 #[must_use = "this returns the result of the operation, \
2398 without modifying the original"]
2400 pub const fn checked_ilog2(self) -> Option<u32> {
2404 // SAFETY: We just checked that this number is positive
2405 let log = (Self::BITS - 1) - unsafe { intrinsics::ctlz_nonzero(self) as u32 };
2410 /// Returns the base 10 logarithm of the number, rounded down.
2412 /// Returns `None` if the number is negative or zero.
2417 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_ilog10(), Some(1));")]
2419 #[stable(feature = "int_log", since = "CURRENT_RUSTC_VERSION")]
2420 #[rustc_const_stable(feature = "int_log", since = "CURRENT_RUSTC_VERSION")]
2421 #[must_use = "this returns the result of the operation, \
2422 without modifying the original"]
2424 pub const fn checked_ilog10(self) -> Option<u32> {
2426 Some(int_log10::$ActualT(self as $ActualT))
2432 /// Computes the absolute value of `self`.
2434 /// # Overflow behavior
2436 /// The absolute value of
2437 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
2438 /// cannot be represented as an
2439 #[doc = concat!("`", stringify!($SelfT), "`,")]
2440 /// and attempting to calculate it will cause an overflow. This means
2441 /// that code in debug mode will trigger a panic on this case and
2442 /// optimized code will return
2443 #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
2444 /// without a panic.
2451 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".abs(), 10);")]
2452 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").abs(), 10);")]
2454 #[stable(feature = "rust1", since = "1.0.0")]
2455 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2456 #[allow(unused_attributes)]
2457 #[must_use = "this returns the result of the operation, \
2458 without modifying the original"]
2460 #[rustc_inherit_overflow_checks]
2461 pub const fn abs(self) -> Self {
2462 // Note that the #[rustc_inherit_overflow_checks] and #[inline]
2463 // above mean that the overflow semantics of the subtraction
2464 // depend on the crate we're being called from.
2465 if self.is_negative() {
2472 /// Computes the absolute difference between `self` and `other`.
2474 /// This function always returns the correct answer without overflow or
2475 /// panics by returning an unsigned integer.
2482 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(80), 20", stringify!($UnsignedT), ");")]
2483 #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(110), 10", stringify!($UnsignedT), ");")]
2484 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(80), 180", stringify!($UnsignedT), ");")]
2485 #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(-120), 20", stringify!($UnsignedT), ");")]
2486 #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.abs_diff(", stringify!($SelfT), "::MAX), ", stringify!($UnsignedT), "::MAX);")]
2488 #[stable(feature = "int_abs_diff", since = "1.60.0")]
2489 #[rustc_const_stable(feature = "int_abs_diff", since = "1.60.0")]
2490 #[must_use = "this returns the result of the operation, \
2491 without modifying the original"]
2493 pub const fn abs_diff(self, other: Self) -> $UnsignedT {
2495 // Converting a non-negative x from signed to unsigned by using
2496 // `x as U` is left unchanged, but a negative x is converted
2497 // to value x + 2^N. Thus if `s` and `o` are binary variables
2498 // respectively indicating whether `self` and `other` are
2499 // negative, we are computing the mathematical value:
2501 // (other + o*2^N) - (self + s*2^N) mod 2^N
2502 // other - self + (o-s)*2^N mod 2^N
2503 // other - self mod 2^N
2505 // Finally, taking the mod 2^N of the mathematical value of
2506 // `other - self` does not change it as it already is
2507 // in the range [0, 2^N).
2508 (other as $UnsignedT).wrapping_sub(self as $UnsignedT)
2510 (self as $UnsignedT).wrapping_sub(other as $UnsignedT)
2514 /// Returns a number representing sign of `self`.
2516 /// - `0` if the number is zero
2517 /// - `1` if the number is positive
2518 /// - `-1` if the number is negative
2525 #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".signum(), 1);")]
2526 #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".signum(), 0);")]
2527 #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").signum(), -1);")]
2529 #[stable(feature = "rust1", since = "1.0.0")]
2530 #[rustc_const_stable(feature = "const_int_sign", since = "1.47.0")]
2531 #[must_use = "this returns the result of the operation, \
2532 without modifying the original"]
2534 pub const fn signum(self) -> Self {
2542 /// Returns `true` if `self` is positive and `false` if the number is zero or
2550 #[doc = concat!("assert!(10", stringify!($SelfT), ".is_positive());")]
2551 #[doc = concat!("assert!(!(-10", stringify!($SelfT), ").is_positive());")]
2554 #[stable(feature = "rust1", since = "1.0.0")]
2555 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2557 pub const fn is_positive(self) -> bool { self > 0 }
2559 /// Returns `true` if `self` is negative and `false` if the number is zero or
2567 #[doc = concat!("assert!((-10", stringify!($SelfT), ").is_negative());")]
2568 #[doc = concat!("assert!(!10", stringify!($SelfT), ".is_negative());")]
2571 #[stable(feature = "rust1", since = "1.0.0")]
2572 #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2574 pub const fn is_negative(self) -> bool { self < 0 }
2576 /// Return the memory representation of this integer as a byte array in
2577 /// big-endian (network) byte order.
2579 #[doc = $to_xe_bytes_doc]
2584 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();")]
2585 #[doc = concat!("assert_eq!(bytes, ", $be_bytes, ");")]
2587 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2588 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2589 #[must_use = "this returns the result of the operation, \
2590 without modifying the original"]
2592 pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()] {
2593 self.to_be().to_ne_bytes()
2596 /// Return the memory representation of this integer as a byte array in
2597 /// little-endian byte order.
2599 #[doc = $to_xe_bytes_doc]
2604 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();")]
2605 #[doc = concat!("assert_eq!(bytes, ", $le_bytes, ");")]
2607 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2608 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2609 #[must_use = "this returns the result of the operation, \
2610 without modifying the original"]
2612 pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()] {
2613 self.to_le().to_ne_bytes()
2616 /// Return the memory representation of this integer as a byte array in
2617 /// native byte order.
2619 /// As the target platform's native endianness is used, portable code
2620 /// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate,
2623 #[doc = $to_xe_bytes_doc]
2625 /// [`to_be_bytes`]: Self::to_be_bytes
2626 /// [`to_le_bytes`]: Self::to_le_bytes
2631 #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_ne_bytes();")]
2634 /// if cfg!(target_endian = "big") {
2635 #[doc = concat!(" ", $be_bytes)]
2637 #[doc = concat!(" ", $le_bytes)]
2641 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2642 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2643 // SAFETY: const sound because integers are plain old datatypes so we can always
2644 // transmute them to arrays of bytes
2645 #[must_use = "this returns the result of the operation, \
2646 without modifying the original"]
2648 pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
2649 // SAFETY: integers are plain old datatypes so we can always transmute them to
2651 unsafe { mem::transmute(self) }
2654 /// Create an integer value from its representation as a byte array in
2657 #[doc = $from_xe_bytes_doc]
2662 #[doc = concat!("let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, ");")]
2663 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
2666 /// When starting from a slice rather than an array, fallible conversion APIs can be used:
2669 #[doc = concat!("fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
2670 #[doc = concat!(" let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">());")]
2672 #[doc = concat!(" ", stringify!($SelfT), "::from_be_bytes(int_bytes.try_into().unwrap())")]
2675 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2676 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2679 pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
2680 Self::from_be(Self::from_ne_bytes(bytes))
2683 /// Create an integer value from its representation as a byte array in
2686 #[doc = $from_xe_bytes_doc]
2691 #[doc = concat!("let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, ");")]
2692 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
2695 /// When starting from a slice rather than an array, fallible conversion APIs can be used:
2698 #[doc = concat!("fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
2699 #[doc = concat!(" let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">());")]
2701 #[doc = concat!(" ", stringify!($SelfT), "::from_le_bytes(int_bytes.try_into().unwrap())")]
2704 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2705 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2708 pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
2709 Self::from_le(Self::from_ne_bytes(bytes))
2712 /// Create an integer value from its memory representation as a byte
2713 /// array in native endianness.
2715 /// As the target platform's native endianness is used, portable code
2716 /// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as
2717 /// appropriate instead.
2719 /// [`from_be_bytes`]: Self::from_be_bytes
2720 /// [`from_le_bytes`]: Self::from_le_bytes
2722 #[doc = $from_xe_bytes_doc]
2727 #[doc = concat!("let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"big\") {")]
2728 #[doc = concat!(" ", $be_bytes)]
2730 #[doc = concat!(" ", $le_bytes)]
2732 #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
2735 /// When starting from a slice rather than an array, fallible conversion APIs can be used:
2738 #[doc = concat!("fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
2739 #[doc = concat!(" let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">());")]
2741 #[doc = concat!(" ", stringify!($SelfT), "::from_ne_bytes(int_bytes.try_into().unwrap())")]
2744 #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2745 #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2747 // SAFETY: const sound because integers are plain old datatypes so we can always
2748 // transmute to them
2750 pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
2751 // SAFETY: integers are plain old datatypes so we can always transmute to them
2752 unsafe { mem::transmute(bytes) }
2755 /// New code should prefer to use
2756 #[doc = concat!("[`", stringify!($SelfT), "::MIN", "`] instead.")]
2758 /// Returns the smallest value that can be represented by this integer type.
2759 #[stable(feature = "rust1", since = "1.0.0")]
2762 #[rustc_const_stable(feature = "const_min_value", since = "1.32.0")]
2763 #[deprecated(since = "TBD", note = "replaced by the `MIN` associated constant on this type")]
2764 pub const fn min_value() -> Self {
2768 /// New code should prefer to use
2769 #[doc = concat!("[`", stringify!($SelfT), "::MAX", "`] instead.")]
2771 /// Returns the largest value that can be represented by this integer type.
2772 #[stable(feature = "rust1", since = "1.0.0")]
2775 #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
2776 #[deprecated(since = "TBD", note = "replaced by the `MAX` associated constant on this type")]
2777 pub const fn max_value() -> Self {