core/num/
int_macros.rs

1macro_rules! int_impl {
2    (
3        Self = $SelfT:ty,
4        ActualT = $ActualT:ident,
5        UnsignedT = $UnsignedT:ty,
6
7        // These are all for use *only* in doc comments.
8        // As such, they're all passed as literals -- passing them as a string
9        // literal is fine if they need to be multiple code tokens.
10        // In non-comments, use the associated constants rather than these.
11        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        /// The smallest value that can be represented by this integer type
28        #[doc = concat!("(&minus;2<sup>", $BITS_MINUS_ONE, "</sup>", $bound_condition, ").")]
29        ///
30        /// # Examples
31        ///
32        /// ```
33        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN, ", stringify!($Min), ");")]
34        /// ```
35        #[stable(feature = "assoc_int_consts", since = "1.43.0")]
36        pub const MIN: Self = !Self::MAX;
37
38        /// The largest value that can be represented by this integer type
39        #[doc = concat!("(2<sup>", $BITS_MINUS_ONE, "</sup> &minus; 1", $bound_condition, ").")]
40        ///
41        /// # Examples
42        ///
43        /// ```
44        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX, ", stringify!($Max), ");")]
45        /// ```
46        #[stable(feature = "assoc_int_consts", since = "1.43.0")]
47        pub const MAX: Self = (<$UnsignedT>::MAX >> 1) as Self;
48
49        /// The size of this integer type in bits.
50        ///
51        /// # Examples
52        ///
53        /// ```
54        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::BITS, ", stringify!($BITS), ");")]
55        /// ```
56        #[stable(feature = "int_bits_const", since = "1.53.0")]
57        #[cfg(not(feature = "ferrocene_certified"))]
58        pub const BITS: u32 = <$UnsignedT>::BITS;
59
60        /// Returns the number of ones in the binary representation of `self`.
61        ///
62        /// # Examples
63        ///
64        /// ```
65        #[doc = concat!("let n = 0b100_0000", stringify!($SelfT), ";")]
66        ///
67        /// assert_eq!(n.count_ones(), 1);
68        /// ```
69        ///
70        #[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        /// Returns the number of zeros in the binary representation of `self`.
81        ///
82        /// # Examples
83        ///
84        /// ```
85        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.count_zeros(), 1);")]
86        /// ```
87        #[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        /// Returns the number of leading zeros in the binary representation of `self`.
98        ///
99        /// Depending on what you're doing with the value, you might also be interested in the
100        /// [`ilog2`] function which returns a consistent number, even if the type widens.
101        ///
102        /// # Examples
103        ///
104        /// ```
105        #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
106        ///
107        /// assert_eq!(n.leading_zeros(), 0);
108        /// ```
109        #[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        /// Returns the number of trailing zeros in the binary representation of `self`.
121        ///
122        /// # Examples
123        ///
124        /// ```
125        #[doc = concat!("let n = -4", stringify!($SelfT), ";")]
126        ///
127        /// assert_eq!(n.trailing_zeros(), 2);
128        /// ```
129        #[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        /// Returns the number of leading ones in the binary representation of `self`.
140        ///
141        /// # Examples
142        ///
143        /// ```
144        #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
145        ///
146        #[doc = concat!("assert_eq!(n.leading_ones(), ", stringify!($BITS), ");")]
147        /// ```
148        #[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        /// Returns the number of trailing ones in the binary representation of `self`.
159        ///
160        /// # Examples
161        ///
162        /// ```
163        #[doc = concat!("let n = 3", stringify!($SelfT), ";")]
164        ///
165        /// assert_eq!(n.trailing_ones(), 2);
166        /// ```
167        #[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        /// Returns `self` with only the most significant bit set, or `0` if
178        /// the input is `0`.
179        ///
180        /// # Examples
181        ///
182        /// ```
183        /// #![feature(isolate_most_least_significant_one)]
184        ///
185        #[doc = concat!("let n: ", stringify!($SelfT), " = 0b_01100100;")]
186        ///
187        /// assert_eq!(n.isolate_highest_one(), 0b_01000000);
188        #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".isolate_highest_one(), 0);")]
189        /// ```
190        #[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        /// Returns `self` with only the least significant bit set, or `0` if
200        /// the input is `0`.
201        ///
202        /// # Examples
203        ///
204        /// ```
205        /// #![feature(isolate_most_least_significant_one)]
206        ///
207        #[doc = concat!("let n: ", stringify!($SelfT), " = 0b_01100100;")]
208        ///
209        /// assert_eq!(n.isolate_lowest_one(), 0b_00000100);
210        #[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".isolate_lowest_one(), 0);")]
211        /// ```
212        #[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        /// Returns the index of the highest bit set to one in `self`, or `None`
222        /// if `self` is `0`.
223        ///
224        /// # Examples
225        ///
226        /// ```
227        /// #![feature(int_lowest_highest_one)]
228        ///
229        #[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        /// ```
234        #[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        /// Returns the index of the lowest bit set to one in `self`, or `None`
244        /// if `self` is `0`.
245        ///
246        /// # Examples
247        ///
248        /// ```
249        /// #![feature(int_lowest_highest_one)]
250        ///
251        #[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        /// ```
256        #[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        /// Returns the bit pattern of `self` reinterpreted as an unsigned integer of the same size.
266        ///
267        /// This produces the same result as an `as` cast, but ensures that the bit-width remains
268        /// the same.
269        ///
270        /// # Examples
271        ///
272        /// ```
273        #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
274        ///
275        #[doc = concat!("assert_eq!(n.cast_unsigned(), ", stringify!($UnsignedT), "::MAX);")]
276        /// ```
277        #[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        /// Shifts the bits to the left by a specified amount, `n`,
288        /// wrapping the truncated bits to the end of the resulting integer.
289        ///
290        /// Please note this isn't the same operation as the `<<` shifting operator!
291        ///
292        /// # Examples
293        ///
294        /// ```
295        #[doc = concat!("let n = ", $rot_op, stringify!($SelfT), ";")]
296        #[doc = concat!("let m = ", $rot_result, ";")]
297        ///
298        #[doc = concat!("assert_eq!(n.rotate_left(", $rot, "), m);")]
299        /// ```
300        #[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        /// Shifts the bits to the right by a specified amount, `n`,
311        /// wrapping the truncated bits to the beginning of the resulting
312        /// integer.
313        ///
314        /// Please note this isn't the same operation as the `>>` shifting operator!
315        ///
316        /// # Examples
317        ///
318        /// ```
319        #[doc = concat!("let n = ", $rot_result, stringify!($SelfT), ";")]
320        #[doc = concat!("let m = ", $rot_op, ";")]
321        ///
322        #[doc = concat!("assert_eq!(n.rotate_right(", $rot, "), m);")]
323        /// ```
324        #[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        /// Reverses the byte order of the integer.
335        ///
336        /// # Examples
337        ///
338        /// ```
339        #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
340        ///
341        /// let m = n.swap_bytes();
342        ///
343        #[doc = concat!("assert_eq!(m, ", $swapped, ");")]
344        /// ```
345        #[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        /// Reverses the order of bits in the integer. The least significant bit becomes the most significant bit,
356        ///                 second least-significant bit becomes second most-significant bit, etc.
357        ///
358        /// # Examples
359        ///
360        /// ```
361        #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
362        /// let m = n.reverse_bits();
363        ///
364        #[doc = concat!("assert_eq!(m, ", $reversed, ");")]
365        #[doc = concat!("assert_eq!(0, 0", stringify!($SelfT), ".reverse_bits());")]
366        /// ```
367        #[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        /// Converts an integer from big endian to the target's endianness.
378        ///
379        /// On big endian this is a no-op. On little endian the bytes are swapped.
380        ///
381        /// # Examples
382        ///
383        /// ```
384        #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
385        ///
386        /// if cfg!(target_endian = "big") {
387        #[doc = concat!("    assert_eq!(", stringify!($SelfT), "::from_be(n), n)")]
388        /// } else {
389        #[doc = concat!("    assert_eq!(", stringify!($SelfT), "::from_be(n), n.swap_bytes())")]
390        /// }
391        /// ```
392        #[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        /// Converts an integer from little endian to the target's endianness.
409        ///
410        /// On little endian this is a no-op. On big endian the bytes are swapped.
411        ///
412        /// # Examples
413        ///
414        /// ```
415        #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
416        ///
417        /// if cfg!(target_endian = "little") {
418        #[doc = concat!("    assert_eq!(", stringify!($SelfT), "::from_le(n), n)")]
419        /// } else {
420        #[doc = concat!("    assert_eq!(", stringify!($SelfT), "::from_le(n), n.swap_bytes())")]
421        /// }
422        /// ```
423        #[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        /// Converts `self` to big endian from the target's endianness.
440        ///
441        /// On big endian this is a no-op. On little endian the bytes are swapped.
442        ///
443        /// # Examples
444        ///
445        /// ```
446        #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
447        ///
448        /// if cfg!(target_endian = "big") {
449        ///     assert_eq!(n.to_be(), n)
450        /// } else {
451        ///     assert_eq!(n.to_be(), n.swap_bytes())
452        /// }
453        /// ```
454        #[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 { // or not to be?
461            #[cfg(target_endian = "big")]
462            {
463                self
464            }
465            #[cfg(not(target_endian = "big"))]
466            {
467                self.swap_bytes()
468            }
469        }
470
471        /// Converts `self` to little endian from the target's endianness.
472        ///
473        /// On little endian this is a no-op. On big endian the bytes are swapped.
474        ///
475        /// # Examples
476        ///
477        /// ```
478        #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
479        ///
480        /// if cfg!(target_endian = "little") {
481        ///     assert_eq!(n.to_le(), n)
482        /// } else {
483        ///     assert_eq!(n.to_le(), n.swap_bytes())
484        /// }
485        /// ```
486        #[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        /// Checked integer addition. Computes `self + rhs`, returning `None`
504        /// if overflow occurred.
505        ///
506        /// # Examples
507        ///
508        /// ```
509        #[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        /// ```
512        #[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        /// Strict integer addition. Computes `self + rhs`, panicking
524        /// if overflow occurred.
525        ///
526        /// # Panics
527        ///
528        /// ## Overflow behavior
529        ///
530        /// This function will always panic on overflow, regardless of whether overflow checks are enabled.
531        ///
532        /// # Examples
533        ///
534        /// ```
535        #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).strict_add(1), ", stringify!($SelfT), "::MAX - 1);")]
536        /// ```
537        ///
538        /// The following panics because of overflow:
539        ///
540        /// ```should_panic
541        #[doc = concat!("let _ = (", stringify!($SelfT), "::MAX - 2).strict_add(3);")]
542        /// ```
543        #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
544        #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
545        #[must_use = "this returns the result of the operation, \
546                      without modifying the original"]
547        #[inline]
548        #[track_caller]
549        #[cfg(not(feature = "ferrocene_certified"))]
550        pub const fn strict_add(self, rhs: Self) -> Self {
551            let (a, b) = self.overflowing_add(rhs);
552            if b { overflow_panic::add() } else { a }
553        }
554
555        /// Unchecked integer addition. Computes `self + rhs`, assuming overflow
556        /// cannot occur.
557        ///
558        /// Calling `x.unchecked_add(y)` is semantically equivalent to calling
559        /// `x.`[`checked_add`]`(y).`[`unwrap_unchecked`]`()`.
560        ///
561        /// If you're just trying to avoid the panic in debug mode, then **do not**
562        /// use this.  Instead, you're looking for [`wrapping_add`].
563        ///
564        /// # Safety
565        ///
566        /// This results in undefined behavior when
567        #[doc = concat!("`self + rhs > ", stringify!($SelfT), "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`,")]
568        /// i.e. when [`checked_add`] would return `None`.
569        ///
570        /// [`unwrap_unchecked`]: option/enum.Option.html#method.unwrap_unchecked
571        #[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            // SAFETY: this is guaranteed to be safe by the caller.
591            unsafe {
592                intrinsics::unchecked_add(self, rhs)
593            }
594        }
595
596        /// Checked addition with an unsigned integer. Computes `self + rhs`,
597        /// returning `None` if overflow occurred.
598        ///
599        /// # Examples
600        ///
601        /// ```
602        #[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        /// ```
605        #[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        /// Strict addition with an unsigned integer. Computes `self + rhs`,
617        /// panicking if overflow occurred.
618        ///
619        /// # Panics
620        ///
621        /// ## Overflow behavior
622        ///
623        /// This function will always panic on overflow, regardless of whether overflow checks are enabled.
624        ///
625        /// # Examples
626        ///
627        /// ```
628        #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".strict_add_unsigned(2), 3);")]
629        /// ```
630        ///
631        /// The following panics because of overflow:
632        ///
633        /// ```should_panic
634        #[doc = concat!("let _ = (", stringify!($SelfT), "::MAX - 2).strict_add_unsigned(3);")]
635        /// ```
636        #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
637        #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
638        #[must_use = "this returns the result of the operation, \
639                      without modifying the original"]
640        #[inline]
641        #[track_caller]
642        #[cfg(not(feature = "ferrocene_certified"))]
643        pub const fn strict_add_unsigned(self, rhs: $UnsignedT) -> Self {
644            let (a, b) = self.overflowing_add_unsigned(rhs);
645            if b { overflow_panic::add() } else { a }
646        }
647
648        /// Checked integer subtraction. Computes `self - rhs`, returning `None` if
649        /// overflow occurred.
650        ///
651        /// # Examples
652        ///
653        /// ```
654        #[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        /// ```
657        #[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        /// Strict integer subtraction. Computes `self - rhs`, panicking if
669        /// overflow occurred.
670        ///
671        /// # Panics
672        ///
673        /// ## Overflow behavior
674        ///
675        /// This function will always panic on overflow, regardless of whether overflow checks are enabled.
676        ///
677        /// # Examples
678        ///
679        /// ```
680        #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).strict_sub(1), ", stringify!($SelfT), "::MIN + 1);")]
681        /// ```
682        ///
683        /// The following panics because of overflow:
684        ///
685        /// ```should_panic
686        #[doc = concat!("let _ = (", stringify!($SelfT), "::MIN + 2).strict_sub(3);")]
687        /// ```
688        #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
689        #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
690        #[must_use = "this returns the result of the operation, \
691                      without modifying the original"]
692        #[inline]
693        #[track_caller]
694        #[cfg(not(feature = "ferrocene_certified"))]
695        pub const fn strict_sub(self, rhs: Self) -> Self {
696            let (a, b) = self.overflowing_sub(rhs);
697            if b { overflow_panic::sub() } else { a }
698        }
699
700        /// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow
701        /// cannot occur.
702        ///
703        /// Calling `x.unchecked_sub(y)` is semantically equivalent to calling
704        /// `x.`[`checked_sub`]`(y).`[`unwrap_unchecked`]`()`.
705        ///
706        /// If you're just trying to avoid the panic in debug mode, then **do not**
707        /// use this.  Instead, you're looking for [`wrapping_sub`].
708        ///
709        /// # Safety
710        ///
711        /// This results in undefined behavior when
712        #[doc = concat!("`self - rhs > ", stringify!($SelfT), "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`,")]
713        /// i.e. when [`checked_sub`] would return `None`.
714        ///
715        /// [`unwrap_unchecked`]: option/enum.Option.html#method.unwrap_unchecked
716        #[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            // SAFETY: this is guaranteed to be safe by the caller.
736            unsafe {
737                intrinsics::unchecked_sub(self, rhs)
738            }
739        }
740
741        /// Checked subtraction with an unsigned integer. Computes `self - rhs`,
742        /// returning `None` if overflow occurred.
743        ///
744        /// # Examples
745        ///
746        /// ```
747        #[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        /// ```
750        #[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        /// Strict subtraction with an unsigned integer. Computes `self - rhs`,
762        /// panicking if overflow occurred.
763        ///
764        /// # Panics
765        ///
766        /// ## Overflow behavior
767        ///
768        /// This function will always panic on overflow, regardless of whether overflow checks are enabled.
769        ///
770        /// # Examples
771        ///
772        /// ```
773        #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".strict_sub_unsigned(2), -1);")]
774        /// ```
775        ///
776        /// The following panics because of overflow:
777        ///
778        /// ```should_panic
779        #[doc = concat!("let _ = (", stringify!($SelfT), "::MIN + 2).strict_sub_unsigned(3);")]
780        /// ```
781        #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
782        #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
783        #[must_use = "this returns the result of the operation, \
784                      without modifying the original"]
785        #[inline]
786        #[track_caller]
787        #[cfg(not(feature = "ferrocene_certified"))]
788        pub const fn strict_sub_unsigned(self, rhs: $UnsignedT) -> Self {
789            let (a, b) = self.overflowing_sub_unsigned(rhs);
790            if b { overflow_panic::sub() } else { a }
791        }
792
793        /// Checked integer multiplication. Computes `self * rhs`, returning `None` if
794        /// overflow occurred.
795        ///
796        /// # Examples
797        ///
798        /// ```
799        #[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        /// ```
802        #[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        /// Strict integer multiplication. Computes `self * rhs`, panicking if
814        /// overflow occurred.
815        ///
816        /// # Panics
817        ///
818        /// ## Overflow behavior
819        ///
820        /// This function will always panic on overflow, regardless of whether overflow checks are enabled.
821        ///
822        /// # Examples
823        ///
824        /// ```
825        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.strict_mul(1), ", stringify!($SelfT), "::MAX);")]
826        /// ```
827        ///
828        /// The following panics because of overflow:
829        ///
830        /// ``` should_panic
831        #[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_mul(2);")]
832        /// ```
833        #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
834        #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
835        #[must_use = "this returns the result of the operation, \
836                      without modifying the original"]
837        #[inline]
838        #[track_caller]
839        #[cfg(not(feature = "ferrocene_certified"))]
840        pub const fn strict_mul(self, rhs: Self) -> Self {
841            let (a, b) = self.overflowing_mul(rhs);
842            if b { overflow_panic::mul() } else { a }
843        }
844
845        /// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow
846        /// cannot occur.
847        ///
848        /// Calling `x.unchecked_mul(y)` is semantically equivalent to calling
849        /// `x.`[`checked_mul`]`(y).`[`unwrap_unchecked`]`()`.
850        ///
851        /// If you're just trying to avoid the panic in debug mode, then **do not**
852        /// use this.  Instead, you're looking for [`wrapping_mul`].
853        ///
854        /// # Safety
855        ///
856        /// This results in undefined behavior when
857        #[doc = concat!("`self * rhs > ", stringify!($SelfT), "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`,")]
858        /// i.e. when [`checked_mul`] would return `None`.
859        ///
860        /// [`unwrap_unchecked`]: option/enum.Option.html#method.unwrap_unchecked
861        #[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            // SAFETY: this is guaranteed to be safe by the caller.
881            unsafe {
882                intrinsics::unchecked_mul(self, rhs)
883            }
884        }
885
886        /// Checked integer division. Computes `self / rhs`, returning `None` if `rhs == 0`
887        /// or the division results in overflow.
888        ///
889        /// # Examples
890        ///
891        /// ```
892        #[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        /// ```
896        #[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                // SAFETY: div by zero and by INT_MIN have been checked above
907                Some(unsafe { intrinsics::unchecked_div(self, rhs) })
908            }
909        }
910
911        /// Strict integer division. Computes `self / rhs`, panicking
912        /// if overflow occurred.
913        ///
914        /// # Panics
915        ///
916        /// This function will panic if `rhs` is zero.
917        ///
918        /// ## Overflow behavior
919        ///
920        /// This function will always panic on overflow, regardless of whether overflow checks are enabled.
921        ///
922        /// The only case where such an overflow can occur is when one divides `MIN / -1` on a signed type (where
923        /// `MIN` is the negative minimal value for the type); this is equivalent to `-MIN`, a positive value
924        /// that is too large to represent in the type.
925        ///
926        /// # Examples
927        ///
928        /// ```
929        #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).strict_div(-1), ", stringify!($Max), ");")]
930        /// ```
931        ///
932        /// The following panics because of overflow:
933        ///
934        /// ```should_panic
935        #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_div(-1);")]
936        /// ```
937        ///
938        /// The following panics because of division by zero:
939        ///
940        /// ```should_panic
941        #[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div(0);")]
942        /// ```
943        #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
944        #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
945        #[must_use = "this returns the result of the operation, \
946                      without modifying the original"]
947        #[inline]
948        #[track_caller]
949        #[cfg(not(feature = "ferrocene_certified"))]
950        pub const fn strict_div(self, rhs: Self) -> Self {
951            let (a, b) = self.overflowing_div(rhs);
952            if b { overflow_panic::div() } else { a }
953        }
954
955        /// Checked Euclidean division. Computes `self.div_euclid(rhs)`,
956        /// returning `None` if `rhs == 0` or the division results in overflow.
957        ///
958        /// # Examples
959        ///
960        /// ```
961        #[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        /// ```
965        #[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            // Using `&` helps LLVM see that it is the same check made in division.
973            if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
974                None
975            } else {
976                Some(self.div_euclid(rhs))
977            }
978        }
979
980        /// Strict Euclidean division. Computes `self.div_euclid(rhs)`, panicking
981        /// if overflow occurred.
982        ///
983        /// # Panics
984        ///
985        /// This function will panic if `rhs` is zero.
986        ///
987        /// ## Overflow behavior
988        ///
989        /// This function will always panic on overflow, regardless of whether overflow checks are enabled.
990        ///
991        /// The only case where such an overflow can occur is when one divides `MIN / -1` on a signed type (where
992        /// `MIN` is the negative minimal value for the type); this is equivalent to `-MIN`, a positive value
993        /// that is too large to represent in the type.
994        ///
995        /// # Examples
996        ///
997        /// ```
998        #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).strict_div_euclid(-1), ", stringify!($Max), ");")]
999        /// ```
1000        ///
1001        /// The following panics because of overflow:
1002        ///
1003        /// ```should_panic
1004        #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_div_euclid(-1);")]
1005        /// ```
1006        ///
1007        /// The following panics because of division by zero:
1008        ///
1009        /// ```should_panic
1010        #[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div_euclid(0);")]
1011        /// ```
1012        #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1013        #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1014        #[must_use = "this returns the result of the operation, \
1015                      without modifying the original"]
1016        #[inline]
1017        #[track_caller]
1018        #[cfg(not(feature = "ferrocene_certified"))]
1019        pub const fn strict_div_euclid(self, rhs: Self) -> Self {
1020            let (a, b) = self.overflowing_div_euclid(rhs);
1021            if b { overflow_panic::div() } else { a }
1022        }
1023
1024        /// Checked integer division without remainder. Computes `self / rhs`,
1025        /// returning `None` if `rhs == 0`, the division results in overflow,
1026        /// or `self % rhs != 0`.
1027        ///
1028        /// # Examples
1029        ///
1030        /// ```
1031        /// #![feature(exact_div)]
1032        #[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        /// ```
1037        #[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                // SAFETY: division by zero and overflow are checked above
1050                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        /// Checked integer division without remainder. Computes `self / rhs`.
1061        ///
1062        /// # Panics
1063        ///
1064        /// This function will panic  if `rhs == 0`, the division results in overflow,
1065        /// or `self % rhs != 0`.
1066        ///
1067        /// # Examples
1068        ///
1069        /// ```
1070        /// #![feature(exact_div)]
1071        #[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        /// ```
1075        ///
1076        /// ```should_panic
1077        /// #![feature(exact_div)]
1078        #[doc = concat!("let _ = 65", stringify!($SelfT), ".exact_div(2);")]
1079        /// ```
1080        /// ```should_panic
1081        /// #![feature(exact_div)]
1082        #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.exact_div(-1);")]
1083        /// ```
1084        #[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        /// Unchecked integer division without remainder. Computes `self / rhs`.
1100        ///
1101        /// # Safety
1102        ///
1103        /// This results in undefined behavior when `rhs == 0`, `self % rhs != 0`, or
1104        #[doc = concat!("`self == ", stringify!($SelfT), "::MIN && rhs == -1`,")]
1105        /// i.e. when [`checked_exact_div`](Self::checked_exact_div) would return `None`.
1106        #[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            // SAFETY: Same precondition
1124            unsafe { intrinsics::exact_div(self, rhs) }
1125        }
1126
1127        /// Checked integer remainder. Computes `self % rhs`, returning `None` if
1128        /// `rhs == 0` or the division results in overflow.
1129        ///
1130        /// # Examples
1131        ///
1132        /// ```
1133        #[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        /// ```
1137        #[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                // SAFETY: div by zero and by INT_MIN have been checked above
1148                Some(unsafe { intrinsics::unchecked_rem(self, rhs) })
1149            }
1150        }
1151
1152        /// Strict integer remainder. Computes `self % rhs`, panicking if
1153        /// the division results in overflow.
1154        ///
1155        /// # Panics
1156        ///
1157        /// This function will panic if `rhs` is zero.
1158        ///
1159        /// ## Overflow behavior
1160        ///
1161        /// This function will always panic on overflow, regardless of whether overflow checks are enabled.
1162        ///
1163        /// The only case where such an overflow can occur is `x % y` for `MIN / -1` on a
1164        /// signed type (where `MIN` is the negative minimal value), which is invalid due to implementation artifacts.
1165        ///
1166        /// # Examples
1167        ///
1168        /// ```
1169        #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_rem(2), 1);")]
1170        /// ```
1171        ///
1172        /// The following panics because of division by zero:
1173        ///
1174        /// ```should_panic
1175        #[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem(0);")]
1176        /// ```
1177        ///
1178        /// The following panics because of overflow:
1179        ///
1180        /// ```should_panic
1181        #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_rem(-1);")]
1182        /// ```
1183        #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1184        #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1185        #[must_use = "this returns the result of the operation, \
1186                      without modifying the original"]
1187        #[inline]
1188        #[track_caller]
1189        #[cfg(not(feature = "ferrocene_certified"))]
1190        pub const fn strict_rem(self, rhs: Self) -> Self {
1191            let (a, b) = self.overflowing_rem(rhs);
1192            if b { overflow_panic::rem() } else { a }
1193        }
1194
1195        /// Checked Euclidean remainder. Computes `self.rem_euclid(rhs)`, returning `None`
1196        /// if `rhs == 0` or the division results in overflow.
1197        ///
1198        /// # Examples
1199        ///
1200        /// ```
1201        #[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        /// ```
1205        #[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            // Using `&` helps LLVM see that it is the same check made in division.
1213            if intrinsics::unlikely(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
1214                None
1215            } else {
1216                Some(self.rem_euclid(rhs))
1217            }
1218        }
1219
1220        /// Strict Euclidean remainder. Computes `self.rem_euclid(rhs)`, panicking if
1221        /// the division results in overflow.
1222        ///
1223        /// # Panics
1224        ///
1225        /// This function will panic if `rhs` is zero.
1226        ///
1227        /// ## Overflow behavior
1228        ///
1229        /// This function will always panic on overflow, regardless of whether overflow checks are enabled.
1230        ///
1231        /// The only case where such an overflow can occur is `x % y` for `MIN / -1` on a
1232        /// signed type (where `MIN` is the negative minimal value), which is invalid due to implementation artifacts.
1233        ///
1234        /// # Examples
1235        ///
1236        /// ```
1237        #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_rem_euclid(2), 1);")]
1238        /// ```
1239        ///
1240        /// The following panics because of division by zero:
1241        ///
1242        /// ```should_panic
1243        #[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem_euclid(0);")]
1244        /// ```
1245        ///
1246        /// The following panics because of overflow:
1247        ///
1248        /// ```should_panic
1249        #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_rem_euclid(-1);")]
1250        /// ```
1251        #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1252        #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1253        #[must_use = "this returns the result of the operation, \
1254                      without modifying the original"]
1255        #[inline]
1256        #[track_caller]
1257        #[cfg(not(feature = "ferrocene_certified"))]
1258        pub const fn strict_rem_euclid(self, rhs: Self) -> Self {
1259            let (a, b) = self.overflowing_rem_euclid(rhs);
1260            if b { overflow_panic::rem() } else { a }
1261        }
1262
1263        /// Checked negation. Computes `-self`, returning `None` if `self == MIN`.
1264        ///
1265        /// # Examples
1266        ///
1267        /// ```
1268        #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_neg(), Some(-5));")]
1269        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_neg(), None);")]
1270        /// ```
1271        #[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        /// Unchecked negation. Computes `-self`, assuming overflow cannot occur.
1283        ///
1284        /// # Safety
1285        ///
1286        /// This results in undefined behavior when
1287        #[doc = concat!("`self == ", stringify!($SelfT), "::MIN`,")]
1288        /// i.e. when [`checked_neg`] would return `None`.
1289        ///
1290        #[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            // SAFETY: this is guaranteed to be safe by the caller.
1311            unsafe {
1312                intrinsics::unchecked_sub(0, self)
1313            }
1314        }
1315
1316        /// Strict negation. Computes `-self`, panicking if `self == MIN`.
1317        ///
1318        /// # Panics
1319        ///
1320        /// ## Overflow behavior
1321        ///
1322        /// This function will always panic on overflow, regardless of whether overflow checks are enabled.
1323        ///
1324        /// # Examples
1325        ///
1326        /// ```
1327        #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_neg(), -5);")]
1328        /// ```
1329        ///
1330        /// The following panics because of overflow:
1331        ///
1332        /// ```should_panic
1333        #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_neg();")]
1334        ///
1335        #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1336        #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1337        #[must_use = "this returns the result of the operation, \
1338                      without modifying the original"]
1339        #[inline]
1340        #[track_caller]
1341        #[cfg(not(feature = "ferrocene_certified"))]
1342        pub const fn strict_neg(self) -> Self {
1343            let (a, b) = self.overflowing_neg();
1344            if b { overflow_panic::neg() } else { a }
1345        }
1346
1347        /// Checked shift left. Computes `self << rhs`, returning `None` if `rhs` is larger
1348        /// than or equal to the number of bits in `self`.
1349        ///
1350        /// # Examples
1351        ///
1352        /// ```
1353        #[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        /// ```
1357        #[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            // Not using overflowing_shl as that's a wrapping shift
1365            if rhs < Self::BITS {
1366                // SAFETY: just checked the RHS is in-range
1367                Some(unsafe { self.unchecked_shl(rhs) })
1368            } else {
1369                None
1370            }
1371        }
1372
1373        /// Strict shift left. Computes `self << rhs`, panicking if `rhs` is larger
1374        /// than or equal to the number of bits in `self`.
1375        ///
1376        /// # Panics
1377        ///
1378        /// ## Overflow behavior
1379        ///
1380        /// This function will always panic on overflow, regardless of whether overflow checks are enabled.
1381        ///
1382        /// # Examples
1383        ///
1384        /// ```
1385        #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".strict_shl(4), 0x10);")]
1386        /// ```
1387        ///
1388        /// The following panics because of overflow:
1389        ///
1390        /// ```should_panic
1391        #[doc = concat!("let _ = 0x1", stringify!($SelfT), ".strict_shl(129);")]
1392        /// ```
1393        #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1394        #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1395        #[must_use = "this returns the result of the operation, \
1396                      without modifying the original"]
1397        #[inline]
1398        #[track_caller]
1399        #[cfg(not(feature = "ferrocene_certified"))]
1400        pub const fn strict_shl(self, rhs: u32) -> Self {
1401            let (a, b) = self.overflowing_shl(rhs);
1402            if b { overflow_panic::shl() } else { a }
1403        }
1404
1405        /// Unchecked shift left. Computes `self << rhs`, assuming that
1406        /// `rhs` is less than the number of bits in `self`.
1407        ///
1408        /// # Safety
1409        ///
1410        /// This results in undefined behavior if `rhs` is larger than
1411        /// or equal to the number of bits in `self`,
1412        /// i.e. when [`checked_shl`] would return `None`.
1413        ///
1414        #[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            // SAFETY: this is guaranteed to be safe by the caller.
1435            unsafe {
1436                intrinsics::unchecked_shl(self, rhs)
1437            }
1438        }
1439
1440        /// Unbounded shift left. Computes `self << rhs`, without bounding the value of `rhs`.
1441        ///
1442        /// If `rhs` is larger or equal to the number of bits in `self`,
1443        /// the entire value is shifted out, and `0` is returned.
1444        ///
1445        /// # Examples
1446        ///
1447        /// ```
1448        #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".unbounded_shl(4), 0x10);")]
1449        #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".unbounded_shl(129), 0);")]
1450        /// ```
1451        #[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                // SAFETY:
1460                // rhs is just checked to be in-range above
1461                unsafe { self.unchecked_shl(rhs) }
1462            } else {
1463                0
1464            }
1465        }
1466
1467        /// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
1468        /// larger than or equal to the number of bits in `self`.
1469        ///
1470        /// # Examples
1471        ///
1472        /// ```
1473        #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(4), Some(0x1));")]
1474        #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(128), None);")]
1475        /// ```
1476        #[stable(feature = "wrapping", since = "1.7.0")]
1477        #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1478        #[must_use = "this returns the result of the operation, \
1479                      without modifying the original"]
1480        #[inline]
1481        #[cfg(not(feature = "ferrocene_certified"))]
1482        pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
1483            // Not using overflowing_shr as that's a wrapping shift
1484            if rhs < Self::BITS {
1485                // SAFETY: just checked the RHS is in-range
1486                Some(unsafe { self.unchecked_shr(rhs) })
1487            } else {
1488                None
1489            }
1490        }
1491
1492        /// Strict shift right. Computes `self >> rhs`, panicking `rhs` is
1493        /// larger than or equal to the number of bits in `self`.
1494        ///
1495        /// # Panics
1496        ///
1497        /// ## Overflow behavior
1498        ///
1499        /// This function will always panic on overflow, regardless of whether overflow checks are enabled.
1500        ///
1501        /// # Examples
1502        ///
1503        /// ```
1504        #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".strict_shr(4), 0x1);")]
1505        /// ```
1506        ///
1507        /// The following panics because of overflow:
1508        ///
1509        /// ```should_panic
1510        #[doc = concat!("let _ = 0x10", stringify!($SelfT), ".strict_shr(128);")]
1511        /// ```
1512        #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1513        #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1514        #[must_use = "this returns the result of the operation, \
1515                      without modifying the original"]
1516        #[inline]
1517        #[track_caller]
1518        #[cfg(not(feature = "ferrocene_certified"))]
1519        pub const fn strict_shr(self, rhs: u32) -> Self {
1520            let (a, b) = self.overflowing_shr(rhs);
1521            if b { overflow_panic::shr() } else { a }
1522        }
1523
1524        /// Unchecked shift right. Computes `self >> rhs`, assuming that
1525        /// `rhs` is less than the number of bits in `self`.
1526        ///
1527        /// # Safety
1528        ///
1529        /// This results in undefined behavior if `rhs` is larger than
1530        /// or equal to the number of bits in `self`,
1531        /// i.e. when [`checked_shr`] would return `None`.
1532        ///
1533        #[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
1534        #[unstable(
1535            feature = "unchecked_shifts",
1536            reason = "niche optimization path",
1537            issue = "85122",
1538        )]
1539        #[must_use = "this returns the result of the operation, \
1540                      without modifying the original"]
1541        #[inline(always)]
1542        #[track_caller]
1543        #[cfg(not(feature = "ferrocene_certified"))]
1544        pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
1545            assert_unsafe_precondition!(
1546                check_language_ub,
1547                concat!(stringify!($SelfT), "::unchecked_shr cannot overflow"),
1548                (
1549                    rhs: u32 = rhs,
1550                ) => rhs < <$ActualT>::BITS,
1551            );
1552
1553            // SAFETY: this is guaranteed to be safe by the caller.
1554            unsafe {
1555                intrinsics::unchecked_shr(self, rhs)
1556            }
1557        }
1558
1559        /// Unbounded shift right. Computes `self >> rhs`, without bounding the value of `rhs`.
1560        ///
1561        /// If `rhs` is larger or equal to the number of bits in `self`,
1562        /// the entire value is shifted out, which yields `0` for a positive number,
1563        /// and `-1` for a negative number.
1564        ///
1565        /// # Examples
1566        ///
1567        /// ```
1568        #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".unbounded_shr(4), 0x1);")]
1569        #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".unbounded_shr(129), 0);")]
1570        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.unbounded_shr(129), -1);")]
1571        /// ```
1572        #[stable(feature = "unbounded_shifts", since = "1.87.0")]
1573        #[rustc_const_stable(feature = "unbounded_shifts", since = "1.87.0")]
1574        #[must_use = "this returns the result of the operation, \
1575                      without modifying the original"]
1576        #[inline]
1577        #[cfg(not(feature = "ferrocene_certified"))]
1578        pub const fn unbounded_shr(self, rhs: u32) -> $SelfT{
1579            if rhs < Self::BITS {
1580                // SAFETY:
1581                // rhs is just checked to be in-range above
1582                unsafe { self.unchecked_shr(rhs) }
1583            } else {
1584                // A shift by `Self::BITS-1` suffices for signed integers, because the sign bit is copied for each of the shifted bits.
1585
1586                // SAFETY:
1587                // `Self::BITS-1` is guaranteed to be less than `Self::BITS`
1588                unsafe { self.unchecked_shr(Self::BITS - 1) }
1589            }
1590        }
1591
1592        /// Checked absolute value. Computes `self.abs()`, returning `None` if
1593        /// `self == MIN`.
1594        ///
1595        /// # Examples
1596        ///
1597        /// ```
1598        #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_abs(), Some(5));")]
1599        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_abs(), None);")]
1600        /// ```
1601        #[stable(feature = "no_panic_abs", since = "1.13.0")]
1602        #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
1603        #[must_use = "this returns the result of the operation, \
1604                      without modifying the original"]
1605        #[inline]
1606        #[cfg(not(feature = "ferrocene_certified"))]
1607        pub const fn checked_abs(self) -> Option<Self> {
1608            if self.is_negative() {
1609                self.checked_neg()
1610            } else {
1611                Some(self)
1612            }
1613        }
1614
1615        /// Strict absolute value. Computes `self.abs()`, panicking if
1616        /// `self == MIN`.
1617        ///
1618        /// # Panics
1619        ///
1620        /// ## Overflow behavior
1621        ///
1622        /// This function will always panic on overflow, regardless of whether overflow checks are enabled.
1623        ///
1624        /// # Examples
1625        ///
1626        /// ```
1627        #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").strict_abs(), 5);")]
1628        /// ```
1629        ///
1630        /// The following panics because of overflow:
1631        ///
1632        /// ```should_panic
1633        #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_abs();")]
1634        /// ```
1635        #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1636        #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1637        #[must_use = "this returns the result of the operation, \
1638                      without modifying the original"]
1639        #[inline]
1640        #[track_caller]
1641        #[cfg(not(feature = "ferrocene_certified"))]
1642        pub const fn strict_abs(self) -> Self {
1643            if self.is_negative() {
1644                self.strict_neg()
1645            } else {
1646                self
1647            }
1648        }
1649
1650        /// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if
1651        /// overflow occurred.
1652        ///
1653        /// # Examples
1654        ///
1655        /// ```
1656        #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".checked_pow(2), Some(64));")]
1657        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);")]
1658        /// ```
1659
1660        #[stable(feature = "no_panic_pow", since = "1.34.0")]
1661        #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1662        #[must_use = "this returns the result of the operation, \
1663                      without modifying the original"]
1664        #[inline]
1665        #[cfg(not(feature = "ferrocene_certified"))]
1666        pub const fn checked_pow(self, mut exp: u32) -> Option<Self> {
1667            if exp == 0 {
1668                return Some(1);
1669            }
1670            let mut base = self;
1671            let mut acc: Self = 1;
1672
1673            loop {
1674                if (exp & 1) == 1 {
1675                    acc = try_opt!(acc.checked_mul(base));
1676                    // since exp!=0, finally the exp must be 1.
1677                    if exp == 1 {
1678                        return Some(acc);
1679                    }
1680                }
1681                exp /= 2;
1682                base = try_opt!(base.checked_mul(base));
1683            }
1684        }
1685
1686        /// Strict exponentiation. Computes `self.pow(exp)`, panicking if
1687        /// overflow occurred.
1688        ///
1689        /// # Panics
1690        ///
1691        /// ## Overflow behavior
1692        ///
1693        /// This function will always panic on overflow, regardless of whether overflow checks are enabled.
1694        ///
1695        /// # Examples
1696        ///
1697        /// ```
1698        #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".strict_pow(2), 64);")]
1699        /// ```
1700        ///
1701        /// The following panics because of overflow:
1702        ///
1703        /// ```should_panic
1704        #[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_pow(2);")]
1705        /// ```
1706        #[stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1707        #[rustc_const_stable(feature = "strict_overflow_ops", since = "CURRENT_RUSTC_VERSION")]
1708        #[must_use = "this returns the result of the operation, \
1709                      without modifying the original"]
1710        #[inline]
1711        #[track_caller]
1712        #[cfg(not(feature = "ferrocene_certified"))]
1713        pub const fn strict_pow(self, mut exp: u32) -> Self {
1714            if exp == 0 {
1715                return 1;
1716            }
1717            let mut base = self;
1718            let mut acc: Self = 1;
1719
1720            loop {
1721                if (exp & 1) == 1 {
1722                    acc = acc.strict_mul(base);
1723                    // since exp!=0, finally the exp must be 1.
1724                    if exp == 1 {
1725                        return acc;
1726                    }
1727                }
1728                exp /= 2;
1729                base = base.strict_mul(base);
1730            }
1731        }
1732
1733        /// Returns the square root of the number, rounded down.
1734        ///
1735        /// Returns `None` if `self` is negative.
1736        ///
1737        /// # Examples
1738        ///
1739        /// ```
1740        #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_isqrt(), Some(3));")]
1741        /// ```
1742        #[stable(feature = "isqrt", since = "1.84.0")]
1743        #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
1744        #[must_use = "this returns the result of the operation, \
1745                      without modifying the original"]
1746        #[inline]
1747        #[cfg(not(feature = "ferrocene_certified"))]
1748        pub const fn checked_isqrt(self) -> Option<Self> {
1749            if self < 0 {
1750                None
1751            } else {
1752                // SAFETY: Input is nonnegative in this `else` branch.
1753                let result = unsafe {
1754                    crate::num::int_sqrt::$ActualT(self as $ActualT) as $SelfT
1755                };
1756
1757                // Inform the optimizer what the range of outputs is. If
1758                // testing `core` crashes with no panic message and a
1759                // `num::int_sqrt::i*` test failed, it's because your edits
1760                // caused these assertions to become false.
1761                //
1762                // SAFETY: Integer square root is a monotonically nondecreasing
1763                // function, which means that increasing the input will never
1764                // cause the output to decrease. Thus, since the input for
1765                // nonnegative signed integers is bounded by
1766                // `[0, <$ActualT>::MAX]`, sqrt(n) will be bounded by
1767                // `[sqrt(0), sqrt(<$ActualT>::MAX)]`.
1768                unsafe {
1769                    // SAFETY: `<$ActualT>::MAX` is nonnegative.
1770                    const MAX_RESULT: $SelfT = unsafe {
1771                        crate::num::int_sqrt::$ActualT(<$ActualT>::MAX) as $SelfT
1772                    };
1773
1774                    crate::hint::assert_unchecked(result >= 0);
1775                    crate::hint::assert_unchecked(result <= MAX_RESULT);
1776                }
1777
1778                Some(result)
1779            }
1780        }
1781
1782        /// Saturating integer addition. Computes `self + rhs`, saturating at the numeric
1783        /// bounds instead of overflowing.
1784        ///
1785        /// # Examples
1786        ///
1787        /// ```
1788        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_add(1), 101);")]
1789        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add(100), ", stringify!($SelfT), "::MAX);")]
1790        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_add(-1), ", stringify!($SelfT), "::MIN);")]
1791        /// ```
1792
1793        #[stable(feature = "rust1", since = "1.0.0")]
1794        #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1795        #[must_use = "this returns the result of the operation, \
1796                      without modifying the original"]
1797        #[inline(always)]
1798        #[cfg(not(feature = "ferrocene_certified"))]
1799        pub const fn saturating_add(self, rhs: Self) -> Self {
1800            intrinsics::saturating_add(self, rhs)
1801        }
1802
1803        /// Saturating addition with an unsigned integer. Computes `self + rhs`,
1804        /// saturating at the numeric bounds instead of overflowing.
1805        ///
1806        /// # Examples
1807        ///
1808        /// ```
1809        #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".saturating_add_unsigned(2), 3);")]
1810        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add_unsigned(100), ", stringify!($SelfT), "::MAX);")]
1811        /// ```
1812        #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1813        #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1814        #[must_use = "this returns the result of the operation, \
1815                      without modifying the original"]
1816        #[inline]
1817        #[cfg(not(feature = "ferrocene_certified"))]
1818        pub const fn saturating_add_unsigned(self, rhs: $UnsignedT) -> Self {
1819            // Overflow can only happen at the upper bound
1820            // We cannot use `unwrap_or` here because it is not `const`
1821            match self.checked_add_unsigned(rhs) {
1822                Some(x) => x,
1823                None => Self::MAX,
1824            }
1825        }
1826
1827        /// Saturating integer subtraction. Computes `self - rhs`, saturating at the
1828        /// numeric bounds instead of overflowing.
1829        ///
1830        /// # Examples
1831        ///
1832        /// ```
1833        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub(127), -27);")]
1834        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub(100), ", stringify!($SelfT), "::MIN);")]
1835        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_sub(-1), ", stringify!($SelfT), "::MAX);")]
1836        /// ```
1837        #[stable(feature = "rust1", since = "1.0.0")]
1838        #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1839        #[must_use = "this returns the result of the operation, \
1840                      without modifying the original"]
1841        #[inline(always)]
1842        #[cfg(not(feature = "ferrocene_certified"))]
1843        pub const fn saturating_sub(self, rhs: Self) -> Self {
1844            intrinsics::saturating_sub(self, rhs)
1845        }
1846
1847        /// Saturating subtraction with an unsigned integer. Computes `self - rhs`,
1848        /// saturating at the numeric bounds instead of overflowing.
1849        ///
1850        /// # Examples
1851        ///
1852        /// ```
1853        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub_unsigned(127), -27);")]
1854        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub_unsigned(100), ", stringify!($SelfT), "::MIN);")]
1855        /// ```
1856        #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1857        #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1858        #[must_use = "this returns the result of the operation, \
1859                      without modifying the original"]
1860        #[inline]
1861        #[cfg(not(feature = "ferrocene_certified"))]
1862        pub const fn saturating_sub_unsigned(self, rhs: $UnsignedT) -> Self {
1863            // Overflow can only happen at the lower bound
1864            // We cannot use `unwrap_or` here because it is not `const`
1865            match self.checked_sub_unsigned(rhs) {
1866                Some(x) => x,
1867                None => Self::MIN,
1868            }
1869        }
1870
1871        /// Saturating integer negation. Computes `-self`, returning `MAX` if `self == MIN`
1872        /// instead of overflowing.
1873        ///
1874        /// # Examples
1875        ///
1876        /// ```
1877        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_neg(), -100);")]
1878        #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_neg(), 100);")]
1879        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_neg(), ", stringify!($SelfT), "::MAX);")]
1880        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_neg(), ", stringify!($SelfT), "::MIN + 1);")]
1881        /// ```
1882
1883        #[stable(feature = "saturating_neg", since = "1.45.0")]
1884        #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1885        #[must_use = "this returns the result of the operation, \
1886                      without modifying the original"]
1887        #[inline(always)]
1888        #[cfg(not(feature = "ferrocene_certified"))]
1889        pub const fn saturating_neg(self) -> Self {
1890            intrinsics::saturating_sub(0, self)
1891        }
1892
1893        /// Saturating absolute value. Computes `self.abs()`, returning `MAX` if `self ==
1894        /// MIN` instead of overflowing.
1895        ///
1896        /// # Examples
1897        ///
1898        /// ```
1899        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_abs(), 100);")]
1900        #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_abs(), 100);")]
1901        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_abs(), ", stringify!($SelfT), "::MAX);")]
1902        #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).saturating_abs(), ", stringify!($SelfT), "::MAX);")]
1903        /// ```
1904
1905        #[stable(feature = "saturating_neg", since = "1.45.0")]
1906        #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1907        #[must_use = "this returns the result of the operation, \
1908                      without modifying the original"]
1909        #[inline]
1910        #[cfg(not(feature = "ferrocene_certified"))]
1911        pub const fn saturating_abs(self) -> Self {
1912            if self.is_negative() {
1913                self.saturating_neg()
1914            } else {
1915                self
1916            }
1917        }
1918
1919        /// Saturating integer multiplication. Computes `self * rhs`, saturating at the
1920        /// numeric bounds instead of overflowing.
1921        ///
1922        /// # Examples
1923        ///
1924        /// ```
1925        #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".saturating_mul(12), 120);")]
1926        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_mul(10), ", stringify!($SelfT), "::MAX);")]
1927        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_mul(10), ", stringify!($SelfT), "::MIN);")]
1928        /// ```
1929        #[stable(feature = "wrapping", since = "1.7.0")]
1930        #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1931        #[must_use = "this returns the result of the operation, \
1932                      without modifying the original"]
1933        #[inline]
1934        #[cfg(not(feature = "ferrocene_certified"))]
1935        pub const fn saturating_mul(self, rhs: Self) -> Self {
1936            match self.checked_mul(rhs) {
1937                Some(x) => x,
1938                None => if (self < 0) == (rhs < 0) {
1939                    Self::MAX
1940                } else {
1941                    Self::MIN
1942                }
1943            }
1944        }
1945
1946        /// Saturating integer division. Computes `self / rhs`, saturating at the
1947        /// numeric bounds instead of overflowing.
1948        ///
1949        /// # Panics
1950        ///
1951        /// This function will panic if `rhs` is zero.
1952        ///
1953        /// # Examples
1954        ///
1955        /// ```
1956        #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
1957        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_div(-1), ", stringify!($SelfT), "::MIN + 1);")]
1958        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_div(-1), ", stringify!($SelfT), "::MAX);")]
1959        ///
1960        /// ```
1961        #[stable(feature = "saturating_div", since = "1.58.0")]
1962        #[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
1963        #[must_use = "this returns the result of the operation, \
1964                      without modifying the original"]
1965        #[inline]
1966        #[cfg(not(feature = "ferrocene_certified"))]
1967        pub const fn saturating_div(self, rhs: Self) -> Self {
1968            match self.overflowing_div(rhs) {
1969                (result, false) => result,
1970                (_result, true) => Self::MAX, // MIN / -1 is the only possible saturating overflow
1971            }
1972        }
1973
1974        /// Saturating integer exponentiation. Computes `self.pow(exp)`,
1975        /// saturating at the numeric bounds instead of overflowing.
1976        ///
1977        /// # Examples
1978        ///
1979        /// ```
1980        #[doc = concat!("assert_eq!((-4", stringify!($SelfT), ").saturating_pow(3), -64);")]
1981        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(2), ", stringify!($SelfT), "::MAX);")]
1982        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(3), ", stringify!($SelfT), "::MIN);")]
1983        /// ```
1984        #[stable(feature = "no_panic_pow", since = "1.34.0")]
1985        #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1986        #[must_use = "this returns the result of the operation, \
1987                      without modifying the original"]
1988        #[inline]
1989        #[cfg(not(feature = "ferrocene_certified"))]
1990        pub const fn saturating_pow(self, exp: u32) -> Self {
1991            match self.checked_pow(exp) {
1992                Some(x) => x,
1993                None if self < 0 && exp % 2 == 1 => Self::MIN,
1994                None => Self::MAX,
1995            }
1996        }
1997
1998        /// Wrapping (modular) addition. Computes `self + rhs`, wrapping around at the
1999        /// boundary of the type.
2000        ///
2001        /// # Examples
2002        ///
2003        /// ```
2004        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add(27), 127);")]
2005        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add(2), ", stringify!($SelfT), "::MIN + 1);")]
2006        /// ```
2007        #[stable(feature = "rust1", since = "1.0.0")]
2008        #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2009        #[must_use = "this returns the result of the operation, \
2010                      without modifying the original"]
2011        #[inline(always)]
2012        #[cfg(not(feature = "ferrocene_certified"))]
2013        pub const fn wrapping_add(self, rhs: Self) -> Self {
2014            intrinsics::wrapping_add(self, rhs)
2015        }
2016
2017        /// Wrapping (modular) addition with an unsigned integer. Computes
2018        /// `self + rhs`, wrapping around at the boundary of the type.
2019        ///
2020        /// # Examples
2021        ///
2022        /// ```
2023        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add_unsigned(27), 127);")]
2024        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add_unsigned(2), ", stringify!($SelfT), "::MIN + 1);")]
2025        /// ```
2026        #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2027        #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2028        #[must_use = "this returns the result of the operation, \
2029                      without modifying the original"]
2030        #[inline(always)]
2031        #[cfg(not(feature = "ferrocene_certified"))]
2032        pub const fn wrapping_add_unsigned(self, rhs: $UnsignedT) -> Self {
2033            self.wrapping_add(rhs as Self)
2034        }
2035
2036        /// Wrapping (modular) subtraction. Computes `self - rhs`, wrapping around at the
2037        /// boundary of the type.
2038        ///
2039        /// # Examples
2040        ///
2041        /// ```
2042        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub(127), -127);")]
2043        #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub(", stringify!($SelfT), "::MAX), ", stringify!($SelfT), "::MAX);")]
2044        /// ```
2045        #[stable(feature = "rust1", since = "1.0.0")]
2046        #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2047        #[must_use = "this returns the result of the operation, \
2048                      without modifying the original"]
2049        #[inline(always)]
2050        #[cfg(not(feature = "ferrocene_certified"))]
2051        pub const fn wrapping_sub(self, rhs: Self) -> Self {
2052            intrinsics::wrapping_sub(self, rhs)
2053        }
2054
2055        /// Wrapping (modular) subtraction with an unsigned integer. Computes
2056        /// `self - rhs`, wrapping around at the boundary of the type.
2057        ///
2058        /// # Examples
2059        ///
2060        /// ```
2061        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub_unsigned(127), -127);")]
2062        #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub_unsigned(", stringify!($UnsignedT), "::MAX), -1);")]
2063        /// ```
2064        #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2065        #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2066        #[must_use = "this returns the result of the operation, \
2067                      without modifying the original"]
2068        #[inline(always)]
2069        #[cfg(not(feature = "ferrocene_certified"))]
2070        pub const fn wrapping_sub_unsigned(self, rhs: $UnsignedT) -> Self {
2071            self.wrapping_sub(rhs as Self)
2072        }
2073
2074        /// Wrapping (modular) multiplication. Computes `self * rhs`, wrapping around at
2075        /// the boundary of the type.
2076        ///
2077        /// # Examples
2078        ///
2079        /// ```
2080        #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".wrapping_mul(12), 120);")]
2081        /// assert_eq!(11i8.wrapping_mul(12), -124);
2082        /// ```
2083        #[stable(feature = "rust1", since = "1.0.0")]
2084        #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2085        #[must_use = "this returns the result of the operation, \
2086                      without modifying the original"]
2087        #[inline(always)]
2088        #[cfg(not(feature = "ferrocene_certified"))]
2089        pub const fn wrapping_mul(self, rhs: Self) -> Self {
2090            intrinsics::wrapping_mul(self, rhs)
2091        }
2092
2093        /// Wrapping (modular) division. Computes `self / rhs`, wrapping around at the
2094        /// boundary of the type.
2095        ///
2096        /// The only case where such wrapping can occur is when one divides `MIN / -1` on a signed type (where
2097        /// `MIN` is the negative minimal value for the type); this is equivalent to `-MIN`, a positive value
2098        /// that is too large to represent in the type. In such a case, this function returns `MIN` itself.
2099        ///
2100        /// # Panics
2101        ///
2102        /// This function will panic if `rhs` is zero.
2103        ///
2104        /// # Examples
2105        ///
2106        /// ```
2107        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div(10), 10);")]
2108        /// assert_eq!((-128i8).wrapping_div(-1), -128);
2109        /// ```
2110        #[stable(feature = "num_wrapping", since = "1.2.0")]
2111        #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
2112        #[must_use = "this returns the result of the operation, \
2113                      without modifying the original"]
2114        #[inline]
2115        #[cfg(not(feature = "ferrocene_certified"))]
2116        pub const fn wrapping_div(self, rhs: Self) -> Self {
2117            self.overflowing_div(rhs).0
2118        }
2119
2120        /// Wrapping Euclidean division. Computes `self.div_euclid(rhs)`,
2121        /// wrapping around at the boundary of the type.
2122        ///
2123        /// Wrapping will only occur in `MIN / -1` on a signed type (where `MIN` is the negative minimal value
2124        /// for the type). This is equivalent to `-MIN`, a positive value that is too large to represent in the
2125        /// type. In this case, this method returns `MIN` itself.
2126        ///
2127        /// # Panics
2128        ///
2129        /// This function will panic if `rhs` is zero.
2130        ///
2131        /// # Examples
2132        ///
2133        /// ```
2134        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div_euclid(10), 10);")]
2135        /// assert_eq!((-128i8).wrapping_div_euclid(-1), -128);
2136        /// ```
2137        #[stable(feature = "euclidean_division", since = "1.38.0")]
2138        #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2139        #[must_use = "this returns the result of the operation, \
2140                      without modifying the original"]
2141        #[inline]
2142        #[cfg(not(feature = "ferrocene_certified"))]
2143        pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
2144            self.overflowing_div_euclid(rhs).0
2145        }
2146
2147        /// Wrapping (modular) remainder. Computes `self % rhs`, wrapping around at the
2148        /// boundary of the type.
2149        ///
2150        /// Such wrap-around never actually occurs mathematically; implementation artifacts make `x % y`
2151        /// invalid for `MIN / -1` on a signed type (where `MIN` is the negative minimal value). In such a case,
2152        /// this function returns `0`.
2153        ///
2154        /// # Panics
2155        ///
2156        /// This function will panic if `rhs` is zero.
2157        ///
2158        /// # Examples
2159        ///
2160        /// ```
2161        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem(10), 0);")]
2162        /// assert_eq!((-128i8).wrapping_rem(-1), 0);
2163        /// ```
2164        #[stable(feature = "num_wrapping", since = "1.2.0")]
2165        #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
2166        #[must_use = "this returns the result of the operation, \
2167                      without modifying the original"]
2168        #[inline]
2169        #[cfg(not(feature = "ferrocene_certified"))]
2170        pub const fn wrapping_rem(self, rhs: Self) -> Self {
2171            self.overflowing_rem(rhs).0
2172        }
2173
2174        /// Wrapping Euclidean remainder. Computes `self.rem_euclid(rhs)`, wrapping around
2175        /// at the boundary of the type.
2176        ///
2177        /// Wrapping will only occur in `MIN % -1` on a signed type (where `MIN` is the negative minimal value
2178        /// for the type). In this case, this method returns 0.
2179        ///
2180        /// # Panics
2181        ///
2182        /// This function will panic if `rhs` is zero.
2183        ///
2184        /// # Examples
2185        ///
2186        /// ```
2187        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);")]
2188        /// assert_eq!((-128i8).wrapping_rem_euclid(-1), 0);
2189        /// ```
2190        #[stable(feature = "euclidean_division", since = "1.38.0")]
2191        #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2192        #[must_use = "this returns the result of the operation, \
2193                      without modifying the original"]
2194        #[inline]
2195        #[cfg(not(feature = "ferrocene_certified"))]
2196        pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
2197            self.overflowing_rem_euclid(rhs).0
2198        }
2199
2200        /// Wrapping (modular) negation. Computes `-self`, wrapping around at the boundary
2201        /// of the type.
2202        ///
2203        /// The only case where such wrapping can occur is when one negates `MIN` on a signed type (where `MIN`
2204        /// is the negative minimal value for the type); this is a positive value that is too large to represent
2205        /// in the type. In such a case, this function returns `MIN` itself.
2206        ///
2207        /// # Examples
2208        ///
2209        /// ```
2210        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_neg(), -100);")]
2211        #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_neg(), 100);")]
2212        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_neg(), ", stringify!($SelfT), "::MIN);")]
2213        /// ```
2214        #[stable(feature = "num_wrapping", since = "1.2.0")]
2215        #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2216        #[must_use = "this returns the result of the operation, \
2217                      without modifying the original"]
2218        #[inline(always)]
2219        #[cfg(not(feature = "ferrocene_certified"))]
2220        pub const fn wrapping_neg(self) -> Self {
2221            (0 as $SelfT).wrapping_sub(self)
2222        }
2223
2224        /// Panic-free bitwise shift-left; yields `self << mask(rhs)`, where `mask` removes
2225        /// any high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type.
2226        ///
2227        /// Note that this is *not* the same as a rotate-left; the RHS of a wrapping shift-left is restricted to
2228        /// the range of the type, rather than the bits shifted out of the LHS being returned to the other end.
2229        /// The primitive integer types all implement a [`rotate_left`](Self::rotate_left) function,
2230        /// which may be what you want instead.
2231        ///
2232        /// # Examples
2233        ///
2234        /// ```
2235        #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(7), -128);")]
2236        #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(128), -1);")]
2237        /// ```
2238        #[stable(feature = "num_wrapping", since = "1.2.0")]
2239        #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2240        #[must_use = "this returns the result of the operation, \
2241                      without modifying the original"]
2242        #[inline(always)]
2243        #[cfg(not(feature = "ferrocene_certified"))]
2244        pub const fn wrapping_shl(self, rhs: u32) -> Self {
2245            // SAFETY: the masking by the bitsize of the type ensures that we do not shift
2246            // out of bounds
2247            unsafe {
2248                self.unchecked_shl(rhs & (Self::BITS - 1))
2249            }
2250        }
2251
2252        /// Panic-free bitwise shift-right; yields `self >> mask(rhs)`, where `mask`
2253        /// removes any high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type.
2254        ///
2255        /// Note that this is *not* the same as a rotate-right; the RHS of a wrapping shift-right is restricted
2256        /// to the range of the type, rather than the bits shifted out of the LHS being returned to the other
2257        /// end. The primitive integer types all implement a [`rotate_right`](Self::rotate_right) function,
2258        /// which may be what you want instead.
2259        ///
2260        /// # Examples
2261        ///
2262        /// ```
2263        #[doc = concat!("assert_eq!((-128", stringify!($SelfT), ").wrapping_shr(7), -1);")]
2264        /// assert_eq!((-128i16).wrapping_shr(64), -128);
2265        /// ```
2266        #[stable(feature = "num_wrapping", since = "1.2.0")]
2267        #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2268        #[must_use = "this returns the result of the operation, \
2269                      without modifying the original"]
2270        #[inline(always)]
2271        #[cfg(not(feature = "ferrocene_certified"))]
2272        pub const fn wrapping_shr(self, rhs: u32) -> Self {
2273            // SAFETY: the masking by the bitsize of the type ensures that we do not shift
2274            // out of bounds
2275            unsafe {
2276                self.unchecked_shr(rhs & (Self::BITS - 1))
2277            }
2278        }
2279
2280        /// Wrapping (modular) absolute value. Computes `self.abs()`, wrapping around at
2281        /// the boundary of the type.
2282        ///
2283        /// The only case where such wrapping can occur is when one takes the absolute value of the negative
2284        /// minimal value for the type; this is a positive value that is too large to represent in the type. In
2285        /// such a case, this function returns `MIN` itself.
2286        ///
2287        /// # Examples
2288        ///
2289        /// ```
2290        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_abs(), 100);")]
2291        #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_abs(), 100);")]
2292        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_abs(), ", stringify!($SelfT), "::MIN);")]
2293        /// assert_eq!((-128i8).wrapping_abs() as u8, 128);
2294        /// ```
2295        #[stable(feature = "no_panic_abs", since = "1.13.0")]
2296        #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2297        #[must_use = "this returns the result of the operation, \
2298                      without modifying the original"]
2299        #[allow(unused_attributes)]
2300        #[inline]
2301        #[cfg(not(feature = "ferrocene_certified"))]
2302        pub const fn wrapping_abs(self) -> Self {
2303             if self.is_negative() {
2304                 self.wrapping_neg()
2305             } else {
2306                 self
2307             }
2308        }
2309
2310        /// Computes the absolute value of `self` without any wrapping
2311        /// or panicking.
2312        ///
2313        ///
2314        /// # Examples
2315        ///
2316        /// ```
2317        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".unsigned_abs(), 100", stringify!($UnsignedT), ");")]
2318        #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").unsigned_abs(), 100", stringify!($UnsignedT), ");")]
2319        /// assert_eq!((-128i8).unsigned_abs(), 128u8);
2320        /// ```
2321        #[stable(feature = "unsigned_abs", since = "1.51.0")]
2322        #[rustc_const_stable(feature = "unsigned_abs", since = "1.51.0")]
2323        #[must_use = "this returns the result of the operation, \
2324                      without modifying the original"]
2325        #[inline]
2326        #[cfg(not(feature = "ferrocene_certified"))]
2327        pub const fn unsigned_abs(self) -> $UnsignedT {
2328             self.wrapping_abs() as $UnsignedT
2329        }
2330
2331        /// Wrapping (modular) exponentiation. Computes `self.pow(exp)`,
2332        /// wrapping around at the boundary of the type.
2333        ///
2334        /// # Examples
2335        ///
2336        /// ```
2337        #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".wrapping_pow(4), 81);")]
2338        /// assert_eq!(3i8.wrapping_pow(5), -13);
2339        /// assert_eq!(3i8.wrapping_pow(6), -39);
2340        /// ```
2341        #[stable(feature = "no_panic_pow", since = "1.34.0")]
2342        #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2343        #[must_use = "this returns the result of the operation, \
2344                      without modifying the original"]
2345        #[inline]
2346        #[cfg(not(feature = "ferrocene_certified"))]
2347        pub const fn wrapping_pow(self, mut exp: u32) -> Self {
2348            if exp == 0 {
2349                return 1;
2350            }
2351            let mut base = self;
2352            let mut acc: Self = 1;
2353
2354            if intrinsics::is_val_statically_known(exp) {
2355                while exp > 1 {
2356                    if (exp & 1) == 1 {
2357                        acc = acc.wrapping_mul(base);
2358                    }
2359                    exp /= 2;
2360                    base = base.wrapping_mul(base);
2361                }
2362
2363                // since exp!=0, finally the exp must be 1.
2364                // Deal with the final bit of the exponent separately, since
2365                // squaring the base afterwards is not necessary.
2366                acc.wrapping_mul(base)
2367            } else {
2368                // This is faster than the above when the exponent is not known
2369                // at compile time. We can't use the same code for the constant
2370                // exponent case because LLVM is currently unable to unroll
2371                // this loop.
2372                loop {
2373                    if (exp & 1) == 1 {
2374                        acc = acc.wrapping_mul(base);
2375                        // since exp!=0, finally the exp must be 1.
2376                        if exp == 1 {
2377                            return acc;
2378                        }
2379                    }
2380                    exp /= 2;
2381                    base = base.wrapping_mul(base);
2382                }
2383            }
2384        }
2385
2386        /// Calculates `self` + `rhs`.
2387        ///
2388        /// Returns a tuple of the addition along with a boolean indicating
2389        /// whether an arithmetic overflow would occur. If an overflow would have
2390        /// occurred then the wrapped value is returned.
2391        ///
2392        /// # Examples
2393        ///
2394        /// ```
2395        #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_add(2), (7, false));")]
2396        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.overflowing_add(1), (", stringify!($SelfT), "::MIN, true));")]
2397        /// ```
2398        #[stable(feature = "wrapping", since = "1.7.0")]
2399        #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2400        #[must_use = "this returns the result of the operation, \
2401                      without modifying the original"]
2402        #[inline(always)]
2403        #[cfg(not(feature = "ferrocene_certified"))]
2404        pub const fn overflowing_add(self, rhs: Self) -> (Self, bool) {
2405            let (a, b) = intrinsics::add_with_overflow(self as $ActualT, rhs as $ActualT);
2406            (a as Self, b)
2407        }
2408
2409        /// Calculates `self` + `rhs` + `carry` and checks for overflow.
2410        ///
2411        /// Performs "ternary addition" of two integer operands and a carry-in
2412        /// bit, and returns a tuple of the sum along with a boolean indicating
2413        /// whether an arithmetic overflow would occur. On overflow, the wrapped
2414        /// value is returned.
2415        ///
2416        /// This allows chaining together multiple additions to create a wider
2417        /// addition, and can be useful for bignum addition. This method should
2418        /// only be used for the most significant word; for the less significant
2419        /// words the unsigned method
2420        #[doc = concat!("[`", stringify!($UnsignedT), "::carrying_add`]")]
2421        /// should be used.
2422        ///
2423        /// The output boolean returned by this method is *not* a carry flag,
2424        /// and should *not* be added to a more significant word.
2425        ///
2426        /// If the input carry is false, this method is equivalent to
2427        /// [`overflowing_add`](Self::overflowing_add).
2428        ///
2429        /// # Examples
2430        ///
2431        /// ```
2432        /// #![feature(bigint_helper_methods)]
2433        /// // Only the most significant word is signed.
2434        /// //
2435        #[doc = concat!("//   10  MAX    (a = 10 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")]
2436        #[doc = concat!("// + -5    9    (b = -5 × 2^", stringify!($BITS), " + 9)")]
2437        /// // ---------
2438        #[doc = concat!("//    6    8    (sum = 6 × 2^", stringify!($BITS), " + 8)")]
2439        ///
2440        #[doc = concat!("let (a1, a0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (10, ", stringify!($UnsignedT), "::MAX);")]
2441        #[doc = concat!("let (b1, b0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (-5, 9);")]
2442        /// let carry0 = false;
2443        ///
2444        #[doc = concat!("// ", stringify!($UnsignedT), "::carrying_add for the less significant words")]
2445        /// let (sum0, carry1) = a0.carrying_add(b0, carry0);
2446        /// assert_eq!(carry1, true);
2447        ///
2448        #[doc = concat!("// ", stringify!($SelfT), "::carrying_add for the most significant word")]
2449        /// let (sum1, overflow) = a1.carrying_add(b1, carry1);
2450        /// assert_eq!(overflow, false);
2451        ///
2452        /// assert_eq!((sum1, sum0), (6, 8));
2453        /// ```
2454        #[unstable(feature = "bigint_helper_methods", issue = "85532")]
2455        #[must_use = "this returns the result of the operation, \
2456                      without modifying the original"]
2457        #[inline]
2458        #[cfg(not(feature = "ferrocene_certified"))]
2459        pub const fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool) {
2460            // note: longer-term this should be done via an intrinsic.
2461            // note: no intermediate overflow is required (https://github.com/rust-lang/rust/issues/85532#issuecomment-1032214946).
2462            let (a, b) = self.overflowing_add(rhs);
2463            let (c, d) = a.overflowing_add(carry as $SelfT);
2464            (c, b != d)
2465        }
2466
2467        /// Calculates `self` + `rhs` with an unsigned `rhs`.
2468        ///
2469        /// Returns a tuple of the addition along with a boolean indicating
2470        /// whether an arithmetic overflow would occur. If an overflow would
2471        /// have occurred then the wrapped value is returned.
2472        ///
2473        /// # Examples
2474        ///
2475        /// ```
2476        #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_add_unsigned(2), (3, false));")]
2477        #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_add_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MAX, false));")]
2478        #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).overflowing_add_unsigned(3), (", stringify!($SelfT), "::MIN, true));")]
2479        /// ```
2480        #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2481        #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2482        #[must_use = "this returns the result of the operation, \
2483                      without modifying the original"]
2484        #[inline]
2485        #[cfg(not(feature = "ferrocene_certified"))]
2486        pub const fn overflowing_add_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
2487            let rhs = rhs as Self;
2488            let (res, overflowed) = self.overflowing_add(rhs);
2489            (res, overflowed ^ (rhs < 0))
2490        }
2491
2492        /// Calculates `self` - `rhs`.
2493        ///
2494        /// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow
2495        /// would occur. If an overflow would have occurred then the wrapped value is returned.
2496        ///
2497        /// # Examples
2498        ///
2499        /// ```
2500        #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_sub(2), (3, false));")]
2501        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_sub(1), (", stringify!($SelfT), "::MAX, true));")]
2502        /// ```
2503        #[stable(feature = "wrapping", since = "1.7.0")]
2504        #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2505        #[must_use = "this returns the result of the operation, \
2506                      without modifying the original"]
2507        #[inline(always)]
2508        #[cfg(not(feature = "ferrocene_certified"))]
2509        pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
2510            let (a, b) = intrinsics::sub_with_overflow(self as $ActualT, rhs as $ActualT);
2511            (a as Self, b)
2512        }
2513
2514        /// Calculates `self` &minus; `rhs` &minus; `borrow` and checks for
2515        /// overflow.
2516        ///
2517        /// Performs "ternary subtraction" by subtracting both an integer
2518        /// operand and a borrow-in bit from `self`, and returns a tuple of the
2519        /// difference along with a boolean indicating whether an arithmetic
2520        /// overflow would occur. On overflow, the wrapped value is returned.
2521        ///
2522        /// This allows chaining together multiple subtractions to create a
2523        /// wider subtraction, and can be useful for bignum subtraction. This
2524        /// method should only be used for the most significant word; for the
2525        /// less significant words the unsigned method
2526        #[doc = concat!("[`", stringify!($UnsignedT), "::borrowing_sub`]")]
2527        /// should be used.
2528        ///
2529        /// The output boolean returned by this method is *not* a borrow flag,
2530        /// and should *not* be subtracted from a more significant word.
2531        ///
2532        /// If the input borrow is false, this method is equivalent to
2533        /// [`overflowing_sub`](Self::overflowing_sub).
2534        ///
2535        /// # Examples
2536        ///
2537        /// ```
2538        /// #![feature(bigint_helper_methods)]
2539        /// // Only the most significant word is signed.
2540        /// //
2541        #[doc = concat!("//    6    8    (a = 6 × 2^", stringify!($BITS), " + 8)")]
2542        #[doc = concat!("// - -5    9    (b = -5 × 2^", stringify!($BITS), " + 9)")]
2543        /// // ---------
2544        #[doc = concat!("//   10  MAX    (diff = 10 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")]
2545        ///
2546        #[doc = concat!("let (a1, a0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (6, 8);")]
2547        #[doc = concat!("let (b1, b0): (", stringify!($SelfT), ", ", stringify!($UnsignedT), ") = (-5, 9);")]
2548        /// let borrow0 = false;
2549        ///
2550        #[doc = concat!("// ", stringify!($UnsignedT), "::borrowing_sub for the less significant words")]
2551        /// let (diff0, borrow1) = a0.borrowing_sub(b0, borrow0);
2552        /// assert_eq!(borrow1, true);
2553        ///
2554        #[doc = concat!("// ", stringify!($SelfT), "::borrowing_sub for the most significant word")]
2555        /// let (diff1, overflow) = a1.borrowing_sub(b1, borrow1);
2556        /// assert_eq!(overflow, false);
2557        ///
2558        #[doc = concat!("assert_eq!((diff1, diff0), (10, ", stringify!($UnsignedT), "::MAX));")]
2559        /// ```
2560        #[unstable(feature = "bigint_helper_methods", issue = "85532")]
2561        #[must_use = "this returns the result of the operation, \
2562                      without modifying the original"]
2563        #[inline]
2564        #[cfg(not(feature = "ferrocene_certified"))]
2565        pub const fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
2566            // note: longer-term this should be done via an intrinsic.
2567            // note: no intermediate overflow is required (https://github.com/rust-lang/rust/issues/85532#issuecomment-1032214946).
2568            let (a, b) = self.overflowing_sub(rhs);
2569            let (c, d) = a.overflowing_sub(borrow as $SelfT);
2570            (c, b != d)
2571        }
2572
2573        /// Calculates `self` - `rhs` with an unsigned `rhs`.
2574        ///
2575        /// Returns a tuple of the subtraction along with a boolean indicating
2576        /// whether an arithmetic overflow would occur. If an overflow would
2577        /// have occurred then the wrapped value is returned.
2578        ///
2579        /// # Examples
2580        ///
2581        /// ```
2582        #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_sub_unsigned(2), (-1, false));")]
2583        #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX).overflowing_sub_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MIN, false));")]
2584        #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).overflowing_sub_unsigned(3), (", stringify!($SelfT), "::MAX, true));")]
2585        /// ```
2586        #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
2587        #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
2588        #[must_use = "this returns the result of the operation, \
2589                      without modifying the original"]
2590        #[inline]
2591        #[cfg(not(feature = "ferrocene_certified"))]
2592        pub const fn overflowing_sub_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
2593            let rhs = rhs as Self;
2594            let (res, overflowed) = self.overflowing_sub(rhs);
2595            (res, overflowed ^ (rhs < 0))
2596        }
2597
2598        /// Calculates the multiplication of `self` and `rhs`.
2599        ///
2600        /// Returns a tuple of the multiplication along with a boolean indicating whether an arithmetic overflow
2601        /// would occur. If an overflow would have occurred then the wrapped value is returned.
2602        ///
2603        /// # Examples
2604        ///
2605        /// ```
2606        #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_mul(2), (10, false));")]
2607        /// assert_eq!(1_000_000_000i32.overflowing_mul(10), (1410065408, true));
2608        /// ```
2609        #[stable(feature = "wrapping", since = "1.7.0")]
2610        #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2611        #[must_use = "this returns the result of the operation, \
2612                      without modifying the original"]
2613        #[inline(always)]
2614        #[cfg(not(feature = "ferrocene_certified"))]
2615        pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
2616            let (a, b) = intrinsics::mul_with_overflow(self as $ActualT, rhs as $ActualT);
2617            (a as Self, b)
2618        }
2619
2620        /// Calculates the complete product `self * rhs` without the possibility to overflow.
2621        ///
2622        /// This returns the low-order (wrapping) bits and the high-order (overflow) bits
2623        /// of the result as two separate values, in that order.
2624        ///
2625        /// If you also need to add a carry to the wide result, then you want
2626        /// [`Self::carrying_mul`] instead.
2627        ///
2628        /// # Examples
2629        ///
2630        /// Please note that this example is shared among integer types, which is why `i32` is used.
2631        ///
2632        /// ```
2633        /// #![feature(bigint_helper_methods)]
2634        /// assert_eq!(5i32.widening_mul(-2), (4294967286, -1));
2635        /// assert_eq!(1_000_000_000i32.widening_mul(-10), (2884901888, -3));
2636        /// ```
2637        #[unstable(feature = "bigint_helper_methods", issue = "85532")]
2638        #[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
2639        #[must_use = "this returns the result of the operation, \
2640                      without modifying the original"]
2641        #[inline]
2642        #[cfg(not(feature = "ferrocene_certified"))]
2643        pub const fn widening_mul(self, rhs: Self) -> ($UnsignedT, Self) {
2644            Self::carrying_mul_add(self, rhs, 0, 0)
2645        }
2646
2647        /// Calculates the "full multiplication" `self * rhs + carry`
2648        /// without the possibility to overflow.
2649        ///
2650        /// This returns the low-order (wrapping) bits and the high-order (overflow) bits
2651        /// of the result as two separate values, in that order.
2652        ///
2653        /// Performs "long multiplication" which takes in an extra amount to add, and may return an
2654        /// additional amount of overflow. This allows for chaining together multiple
2655        /// multiplications to create "big integers" which represent larger values.
2656        ///
2657        /// If you don't need the `carry`, then you can use [`Self::widening_mul`] instead.
2658        ///
2659        /// # Examples
2660        ///
2661        /// Please note that this example is shared among integer types, which is why `i32` is used.
2662        ///
2663        /// ```
2664        /// #![feature(bigint_helper_methods)]
2665        /// assert_eq!(5i32.carrying_mul(-2, 0), (4294967286, -1));
2666        /// assert_eq!(5i32.carrying_mul(-2, 10), (0, 0));
2667        /// assert_eq!(1_000_000_000i32.carrying_mul(-10, 0), (2884901888, -3));
2668        /// assert_eq!(1_000_000_000i32.carrying_mul(-10, 10), (2884901898, -3));
2669        #[doc = concat!("assert_eq!(",
2670            stringify!($SelfT), "::MAX.carrying_mul(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
2671            "(", stringify!($SelfT), "::MAX.unsigned_abs() + 1, ", stringify!($SelfT), "::MAX / 2));"
2672        )]
2673        /// ```
2674        #[unstable(feature = "bigint_helper_methods", issue = "85532")]
2675        #[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
2676        #[must_use = "this returns the result of the operation, \
2677                      without modifying the original"]
2678        #[inline]
2679        #[cfg(not(feature = "ferrocene_certified"))]
2680        pub const fn carrying_mul(self, rhs: Self, carry: Self) -> ($UnsignedT, Self) {
2681            Self::carrying_mul_add(self, rhs, carry, 0)
2682        }
2683
2684        /// Calculates the "full multiplication" `self * rhs + carry1 + carry2`
2685        /// without the possibility to overflow.
2686        ///
2687        /// This returns the low-order (wrapping) bits and the high-order (overflow) bits
2688        /// of the result as two separate values, in that order.
2689        ///
2690        /// Performs "long multiplication" which takes in an extra amount to add, and may return an
2691        /// additional amount of overflow. This allows for chaining together multiple
2692        /// multiplications to create "big integers" which represent larger values.
2693        ///
2694        /// If you don't need either `carry`, then you can use [`Self::widening_mul`] instead,
2695        /// and if you only need one `carry`, then you can use [`Self::carrying_mul`] instead.
2696        ///
2697        /// # Examples
2698        ///
2699        /// Please note that this example is shared among integer types, which is why `i32` is used.
2700        ///
2701        /// ```
2702        /// #![feature(bigint_helper_methods)]
2703        /// assert_eq!(5i32.carrying_mul_add(-2, 0, 0), (4294967286, -1));
2704        /// assert_eq!(5i32.carrying_mul_add(-2, 10, 10), (10, 0));
2705        /// assert_eq!(1_000_000_000i32.carrying_mul_add(-10, 0, 0), (2884901888, -3));
2706        /// assert_eq!(1_000_000_000i32.carrying_mul_add(-10, 10, 10), (2884901908, -3));
2707        #[doc = concat!("assert_eq!(",
2708            stringify!($SelfT), "::MAX.carrying_mul_add(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
2709            "(", stringify!($UnsignedT), "::MAX, ", stringify!($SelfT), "::MAX / 2));"
2710        )]
2711        /// ```
2712        #[unstable(feature = "bigint_helper_methods", issue = "85532")]
2713        #[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")]
2714        #[must_use = "this returns the result of the operation, \
2715                      without modifying the original"]
2716        #[inline]
2717        #[cfg(not(feature = "ferrocene_certified"))]
2718        pub const fn carrying_mul_add(self, rhs: Self, carry: Self, add: Self) -> ($UnsignedT, Self) {
2719            intrinsics::carrying_mul_add(self, rhs, carry, add)
2720        }
2721
2722        /// Calculates the divisor when `self` is divided by `rhs`.
2723        ///
2724        /// Returns a tuple of the divisor along with a boolean indicating whether an arithmetic overflow would
2725        /// occur. If an overflow would occur then self is returned.
2726        ///
2727        /// # Panics
2728        ///
2729        /// This function will panic if `rhs` is zero.
2730        ///
2731        /// # Examples
2732        ///
2733        /// ```
2734        #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div(2), (2, false));")]
2735        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div(-1), (", stringify!($SelfT), "::MIN, true));")]
2736        /// ```
2737        #[inline]
2738        #[stable(feature = "wrapping", since = "1.7.0")]
2739        #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
2740        #[must_use = "this returns the result of the operation, \
2741                      without modifying the original"]
2742        #[cfg(not(feature = "ferrocene_certified"))]
2743        pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
2744            // Using `&` helps LLVM see that it is the same check made in division.
2745            if intrinsics::unlikely((self == Self::MIN) & (rhs == -1)) {
2746                (self, true)
2747            } else {
2748                (self / rhs, false)
2749            }
2750        }
2751
2752        /// Calculates the quotient of Euclidean division `self.div_euclid(rhs)`.
2753        ///
2754        /// Returns a tuple of the divisor along with a boolean indicating whether an arithmetic overflow would
2755        /// occur. If an overflow would occur then `self` is returned.
2756        ///
2757        /// # Panics
2758        ///
2759        /// This function will panic if `rhs` is zero.
2760        ///
2761        /// # Examples
2762        ///
2763        /// ```
2764        #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div_euclid(2), (2, false));")]
2765        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringify!($SelfT), "::MIN, true));")]
2766        /// ```
2767        #[inline]
2768        #[stable(feature = "euclidean_division", since = "1.38.0")]
2769        #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2770        #[must_use = "this returns the result of the operation, \
2771                      without modifying the original"]
2772        #[cfg(not(feature = "ferrocene_certified"))]
2773        pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
2774            // Using `&` helps LLVM see that it is the same check made in division.
2775            if intrinsics::unlikely((self == Self::MIN) & (rhs == -1)) {
2776                (self, true)
2777            } else {
2778                (self.div_euclid(rhs), false)
2779            }
2780        }
2781
2782        /// Calculates the remainder when `self` is divided by `rhs`.
2783        ///
2784        /// Returns a tuple of the remainder after dividing along with a boolean indicating whether an
2785        /// arithmetic overflow would occur. If an overflow would occur then 0 is returned.
2786        ///
2787        /// # Panics
2788        ///
2789        /// This function will panic if `rhs` is zero.
2790        ///
2791        /// # Examples
2792        ///
2793        /// ```
2794        #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem(2), (1, false));")]
2795        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem(-1), (0, true));")]
2796        /// ```
2797        #[inline]
2798        #[stable(feature = "wrapping", since = "1.7.0")]
2799        #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
2800        #[must_use = "this returns the result of the operation, \
2801                      without modifying the original"]
2802        #[cfg(not(feature = "ferrocene_certified"))]
2803        pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
2804            if intrinsics::unlikely(rhs == -1) {
2805                (0, self == Self::MIN)
2806            } else {
2807                (self % rhs, false)
2808            }
2809        }
2810
2811
2812        /// Overflowing Euclidean remainder. Calculates `self.rem_euclid(rhs)`.
2813        ///
2814        /// Returns a tuple of the remainder after dividing along with a boolean indicating whether an
2815        /// arithmetic overflow would occur. If an overflow would occur then 0 is returned.
2816        ///
2817        /// # Panics
2818        ///
2819        /// This function will panic if `rhs` is zero.
2820        ///
2821        /// # Examples
2822        ///
2823        /// ```
2824        #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem_euclid(2), (1, false));")]
2825        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));")]
2826        /// ```
2827        #[stable(feature = "euclidean_division", since = "1.38.0")]
2828        #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2829        #[must_use = "this returns the result of the operation, \
2830                      without modifying the original"]
2831        #[inline]
2832        #[track_caller]
2833        #[cfg(not(feature = "ferrocene_certified"))]
2834        pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
2835            if intrinsics::unlikely(rhs == -1) {
2836                (0, self == Self::MIN)
2837            } else {
2838                (self.rem_euclid(rhs), false)
2839            }
2840        }
2841
2842
2843        /// Negates self, overflowing if this is equal to the minimum value.
2844        ///
2845        /// Returns a tuple of the negated version of self along with a boolean indicating whether an overflow
2846        /// happened. If `self` is the minimum value (e.g., `i32::MIN` for values of type `i32`), then the
2847        /// minimum value will be returned again and `true` will be returned for an overflow happening.
2848        ///
2849        /// # Examples
2850        ///
2851        /// ```
2852        #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2, false));")]
2853        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($SelfT), "::MIN, true));")]
2854        /// ```
2855        #[inline]
2856        #[stable(feature = "wrapping", since = "1.7.0")]
2857        #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2858        #[must_use = "this returns the result of the operation, \
2859                      without modifying the original"]
2860        #[allow(unused_attributes)]
2861        #[cfg(not(feature = "ferrocene_certified"))]
2862        pub const fn overflowing_neg(self) -> (Self, bool) {
2863            if intrinsics::unlikely(self == Self::MIN) {
2864                (Self::MIN, true)
2865            } else {
2866                (-self, false)
2867            }
2868        }
2869
2870        /// Shifts self left by `rhs` bits.
2871        ///
2872        /// Returns a tuple of the shifted version of self along with a boolean indicating whether the shift
2873        /// value was larger than or equal to the number of bits. If the shift value is too large, then value is
2874        /// masked (N-1) where N is the number of bits, and this value is then used to perform the shift.
2875        ///
2876        /// # Examples
2877        ///
2878        /// ```
2879        #[doc = concat!("assert_eq!(0x1", stringify!($SelfT),".overflowing_shl(4), (0x10, false));")]
2880        /// assert_eq!(0x1i32.overflowing_shl(36), (0x10, true));
2881        #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shl(", stringify!($BITS_MINUS_ONE), "), (0, false));")]
2882        /// ```
2883        #[stable(feature = "wrapping", since = "1.7.0")]
2884        #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2885        #[must_use = "this returns the result of the operation, \
2886                      without modifying the original"]
2887        #[inline]
2888        #[cfg(not(feature = "ferrocene_certified"))]
2889        pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
2890            (self.wrapping_shl(rhs), rhs >= Self::BITS)
2891        }
2892
2893        /// Shifts self right by `rhs` bits.
2894        ///
2895        /// Returns a tuple of the shifted version of self along with a boolean indicating whether the shift
2896        /// value was larger than or equal to the number of bits. If the shift value is too large, then value is
2897        /// masked (N-1) where N is the number of bits, and this value is then used to perform the shift.
2898        ///
2899        /// # Examples
2900        ///
2901        /// ```
2902        #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shr(4), (0x1, false));")]
2903        /// assert_eq!(0x10i32.overflowing_shr(36), (0x1, true));
2904        /// ```
2905        #[stable(feature = "wrapping", since = "1.7.0")]
2906        #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2907        #[must_use = "this returns the result of the operation, \
2908                      without modifying the original"]
2909        #[inline]
2910        #[cfg(not(feature = "ferrocene_certified"))]
2911        pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
2912            (self.wrapping_shr(rhs), rhs >= Self::BITS)
2913        }
2914
2915        /// Computes the absolute value of `self`.
2916        ///
2917        /// Returns a tuple of the absolute version of self along with a boolean indicating whether an overflow
2918        /// happened. If self is the minimum value
2919        #[doc = concat!("(e.g., ", stringify!($SelfT), "::MIN for values of type ", stringify!($SelfT), "),")]
2920        /// then the minimum value will be returned again and true will be returned
2921        /// for an overflow happening.
2922        ///
2923        /// # Examples
2924        ///
2925        /// ```
2926        #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".overflowing_abs(), (10, false));")]
2927        #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").overflowing_abs(), (10, false));")]
2928        #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_abs(), (", stringify!($SelfT), "::MIN, true));")]
2929        /// ```
2930        #[stable(feature = "no_panic_abs", since = "1.13.0")]
2931        #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2932        #[must_use = "this returns the result of the operation, \
2933                      without modifying the original"]
2934        #[inline]
2935        #[cfg(not(feature = "ferrocene_certified"))]
2936        pub const fn overflowing_abs(self) -> (Self, bool) {
2937            (self.wrapping_abs(), self == Self::MIN)
2938        }
2939
2940        /// Raises self to the power of `exp`, using exponentiation by squaring.
2941        ///
2942        /// Returns a tuple of the exponentiation along with a bool indicating
2943        /// whether an overflow happened.
2944        ///
2945        /// # Examples
2946        ///
2947        /// ```
2948        #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".overflowing_pow(4), (81, false));")]
2949        /// assert_eq!(3i8.overflowing_pow(5), (-13, true));
2950        /// ```
2951        #[stable(feature = "no_panic_pow", since = "1.34.0")]
2952        #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2953        #[must_use = "this returns the result of the operation, \
2954                      without modifying the original"]
2955        #[inline]
2956        #[cfg(not(feature = "ferrocene_certified"))]
2957        pub const fn overflowing_pow(self, mut exp: u32) -> (Self, bool) {
2958            if exp == 0 {
2959                return (1,false);
2960            }
2961            let mut base = self;
2962            let mut acc: Self = 1;
2963            let mut overflown = false;
2964            // Scratch space for storing results of overflowing_mul.
2965            let mut r;
2966
2967            loop {
2968                if (exp & 1) == 1 {
2969                    r = acc.overflowing_mul(base);
2970                    // since exp!=0, finally the exp must be 1.
2971                    if exp == 1 {
2972                        r.1 |= overflown;
2973                        return r;
2974                    }
2975                    acc = r.0;
2976                    overflown |= r.1;
2977                }
2978                exp /= 2;
2979                r = base.overflowing_mul(base);
2980                base = r.0;
2981                overflown |= r.1;
2982            }
2983        }
2984
2985        /// Raises self to the power of `exp`, using exponentiation by squaring.
2986        ///
2987        /// # Examples
2988        ///
2989        /// ```
2990        #[doc = concat!("let x: ", stringify!($SelfT), " = 2; // or any other integer type")]
2991        ///
2992        /// assert_eq!(x.pow(5), 32);
2993        /// ```
2994        #[stable(feature = "rust1", since = "1.0.0")]
2995        #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
2996        #[must_use = "this returns the result of the operation, \
2997                      without modifying the original"]
2998        #[inline]
2999        #[rustc_inherit_overflow_checks]
3000        #[cfg(not(feature = "ferrocene_certified"))]
3001        pub const fn pow(self, mut exp: u32) -> Self {
3002            if exp == 0 {
3003                return 1;
3004            }
3005            let mut base = self;
3006            let mut acc = 1;
3007
3008            if intrinsics::is_val_statically_known(exp) {
3009                while exp > 1 {
3010                    if (exp & 1) == 1 {
3011                        acc = acc * base;
3012                    }
3013                    exp /= 2;
3014                    base = base * base;
3015                }
3016
3017                // since exp!=0, finally the exp must be 1.
3018                // Deal with the final bit of the exponent separately, since
3019                // squaring the base afterwards is not necessary and may cause a
3020                // needless overflow.
3021                acc * base
3022            } else {
3023                // This is faster than the above when the exponent is not known
3024                // at compile time. We can't use the same code for the constant
3025                // exponent case because LLVM is currently unable to unroll
3026                // this loop.
3027                loop {
3028                    if (exp & 1) == 1 {
3029                        acc = acc * base;
3030                        // since exp!=0, finally the exp must be 1.
3031                        if exp == 1 {
3032                            return acc;
3033                        }
3034                    }
3035                    exp /= 2;
3036                    base = base * base;
3037                }
3038            }
3039        }
3040
3041        /// Returns the square root of the number, rounded down.
3042        ///
3043        /// # Panics
3044        ///
3045        /// This function will panic if `self` is negative.
3046        ///
3047        /// # Examples
3048        ///
3049        /// ```
3050        #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".isqrt(), 3);")]
3051        /// ```
3052        #[stable(feature = "isqrt", since = "1.84.0")]
3053        #[rustc_const_stable(feature = "isqrt", since = "1.84.0")]
3054        #[must_use = "this returns the result of the operation, \
3055                      without modifying the original"]
3056        #[inline]
3057        #[track_caller]
3058        #[cfg(not(feature = "ferrocene_certified"))]
3059        pub const fn isqrt(self) -> Self {
3060            match self.checked_isqrt() {
3061                Some(sqrt) => sqrt,
3062                None => crate::num::int_sqrt::panic_for_negative_argument(),
3063            }
3064        }
3065
3066        /// Calculates the quotient of Euclidean division of `self` by `rhs`.
3067        ///
3068        /// This computes the integer `q` such that `self = q * rhs + r`, with
3069        /// `r = self.rem_euclid(rhs)` and `0 <= r < abs(rhs)`.
3070        ///
3071        /// In other words, the result is `self / rhs` rounded to the integer `q`
3072        /// such that `self >= q * rhs`.
3073        /// If `self > 0`, this is equal to rounding towards zero (the default in Rust);
3074        /// if `self < 0`, this is equal to rounding away from zero (towards +/- infinity).
3075        /// If `rhs > 0`, this is equal to rounding towards -infinity;
3076        /// if `rhs < 0`, this is equal to rounding towards +infinity.
3077        ///
3078        /// # Panics
3079        ///
3080        /// This function will panic if `rhs` is zero or if `self` is `Self::MIN`
3081        /// and `rhs` is -1. This behavior is not affected by the `overflow-checks` flag.
3082        ///
3083        /// # Examples
3084        ///
3085        /// ```
3086        #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
3087        /// let b = 4;
3088        ///
3089        /// assert_eq!(a.div_euclid(b), 1); // 7 >= 4 * 1
3090        /// assert_eq!(a.div_euclid(-b), -1); // 7 >= -4 * -1
3091        /// assert_eq!((-a).div_euclid(b), -2); // -7 >= 4 * -2
3092        /// assert_eq!((-a).div_euclid(-b), 2); // -7 >= -4 * 2
3093        /// ```
3094        #[stable(feature = "euclidean_division", since = "1.38.0")]
3095        #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3096        #[must_use = "this returns the result of the operation, \
3097                      without modifying the original"]
3098        #[inline]
3099        #[track_caller]
3100        #[cfg(not(feature = "ferrocene_certified"))]
3101        pub const fn div_euclid(self, rhs: Self) -> Self {
3102            let q = self / rhs;
3103            if self % rhs < 0 {
3104                return if rhs > 0 { q - 1 } else { q + 1 }
3105            }
3106            q
3107        }
3108
3109
3110        /// Calculates the least nonnegative remainder of `self (mod rhs)`.
3111        ///
3112        /// This is done as if by the Euclidean division algorithm -- given
3113        /// `r = self.rem_euclid(rhs)`, the result satisfies
3114        /// `self = rhs * self.div_euclid(rhs) + r` and `0 <= r < abs(rhs)`.
3115        ///
3116        /// # Panics
3117        ///
3118        /// This function will panic if `rhs` is zero or if `self` is `Self::MIN` and
3119        /// `rhs` is -1. This behavior is not affected by the `overflow-checks` flag.
3120        ///
3121        /// # Examples
3122        ///
3123        /// ```
3124        #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
3125        /// let b = 4;
3126        ///
3127        /// assert_eq!(a.rem_euclid(b), 3);
3128        /// assert_eq!((-a).rem_euclid(b), 1);
3129        /// assert_eq!(a.rem_euclid(-b), 3);
3130        /// assert_eq!((-a).rem_euclid(-b), 1);
3131        /// ```
3132        ///
3133        /// This will panic:
3134        /// ```should_panic
3135        #[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.rem_euclid(-1);")]
3136        /// ```
3137        #[doc(alias = "modulo", alias = "mod")]
3138        #[stable(feature = "euclidean_division", since = "1.38.0")]
3139        #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
3140        #[must_use = "this returns the result of the operation, \
3141                      without modifying the original"]
3142        #[inline]
3143        #[track_caller]
3144        #[cfg(not(feature = "ferrocene_certified"))]
3145        pub const fn rem_euclid(self, rhs: Self) -> Self {
3146            let r = self % rhs;
3147            if r < 0 {
3148                // Semantically equivalent to `if rhs < 0 { r - rhs } else { r + rhs }`.
3149                // If `rhs` is not `Self::MIN`, then `r + abs(rhs)` will not overflow
3150                // and is clearly equivalent, because `r` is negative.
3151                // Otherwise, `rhs` is `Self::MIN`, then we have
3152                // `r.wrapping_add(Self::MIN.wrapping_abs())`, which evaluates
3153                // to `r.wrapping_add(Self::MIN)`, which is equivalent to
3154                // `r - Self::MIN`, which is what we wanted (and will not overflow
3155                // for negative `r`).
3156                r.wrapping_add(rhs.wrapping_abs())
3157            } else {
3158                r
3159            }
3160        }
3161
3162        /// Calculates the quotient of `self` and `rhs`, rounding the result towards negative infinity.
3163        ///
3164        /// # Panics
3165        ///
3166        /// This function will panic if `rhs` is zero or if `self` is `Self::MIN`
3167        /// and `rhs` is -1. This behavior is not affected by the `overflow-checks` flag.
3168        ///
3169        /// # Examples
3170        ///
3171        /// ```
3172        /// #![feature(int_roundings)]
3173        #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3174        /// let b = 3;
3175        ///
3176        /// assert_eq!(a.div_floor(b), 2);
3177        /// assert_eq!(a.div_floor(-b), -3);
3178        /// assert_eq!((-a).div_floor(b), -3);
3179        /// assert_eq!((-a).div_floor(-b), 2);
3180        /// ```
3181        #[unstable(feature = "int_roundings", issue = "88581")]
3182        #[must_use = "this returns the result of the operation, \
3183                      without modifying the original"]
3184        #[inline]
3185        #[track_caller]
3186        #[cfg(not(feature = "ferrocene_certified"))]
3187        pub const fn div_floor(self, rhs: Self) -> Self {
3188            let d = self / rhs;
3189            let r = self % rhs;
3190
3191            // If the remainder is non-zero, we need to subtract one if the
3192            // signs of self and rhs differ, as this means we rounded upwards
3193            // instead of downwards. We do this branchlessly by creating a mask
3194            // which is all-ones iff the signs differ, and 0 otherwise. Then by
3195            // adding this mask (which corresponds to the signed value -1), we
3196            // get our correction.
3197            let correction = (self ^ rhs) >> (Self::BITS - 1);
3198            if r != 0 {
3199                d + correction
3200            } else {
3201                d
3202            }
3203        }
3204
3205        /// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
3206        ///
3207        /// # Panics
3208        ///
3209        /// This function will panic if `rhs` is zero or if `self` is `Self::MIN`
3210        /// and `rhs` is -1. This behavior is not affected by the `overflow-checks` flag.
3211        ///
3212        /// # Examples
3213        ///
3214        /// ```
3215        /// #![feature(int_roundings)]
3216        #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
3217        /// let b = 3;
3218        ///
3219        /// assert_eq!(a.div_ceil(b), 3);
3220        /// assert_eq!(a.div_ceil(-b), -2);
3221        /// assert_eq!((-a).div_ceil(b), -2);
3222        /// assert_eq!((-a).div_ceil(-b), 3);
3223        /// ```
3224        #[unstable(feature = "int_roundings", issue = "88581")]
3225        #[must_use = "this returns the result of the operation, \
3226                      without modifying the original"]
3227        #[inline]
3228        #[track_caller]
3229        #[cfg(not(feature = "ferrocene_certified"))]
3230        pub const fn div_ceil(self, rhs: Self) -> Self {
3231            let d = self / rhs;
3232            let r = self % rhs;
3233
3234            // When remainder is non-zero we have a.div_ceil(b) == 1 + a.div_floor(b),
3235            // so we can re-use the algorithm from div_floor, just adding 1.
3236            let correction = 1 + ((self ^ rhs) >> (Self::BITS - 1));
3237            if r != 0 {
3238                d + correction
3239            } else {
3240                d
3241            }
3242        }
3243
3244        /// If `rhs` is positive, calculates the smallest value greater than or
3245        /// equal to `self` that is a multiple of `rhs`. If `rhs` is negative,
3246        /// calculates the largest value less than or equal to `self` that is a
3247        /// multiple of `rhs`.
3248        ///
3249        /// # Panics
3250        ///
3251        /// This function will panic if `rhs` is zero.
3252        ///
3253        /// ## Overflow behavior
3254        ///
3255        /// On overflow, this function will panic if overflow checks are enabled (default in debug
3256        /// mode) and wrap if overflow checks are disabled (default in release mode).
3257        ///
3258        /// # Examples
3259        ///
3260        /// ```
3261        /// #![feature(int_roundings)]
3262        #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(8), 16);")]
3263        #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(8), 24);")]
3264        #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
3265        #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
3266        #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
3267        #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
3268        #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(-8), -16);")]
3269        #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(-8), -24);")]
3270        /// ```
3271        #[unstable(feature = "int_roundings", issue = "88581")]
3272        #[must_use = "this returns the result of the operation, \
3273                      without modifying the original"]
3274        #[inline]
3275        #[rustc_inherit_overflow_checks]
3276        #[cfg(not(feature = "ferrocene_certified"))]
3277        pub const fn next_multiple_of(self, rhs: Self) -> Self {
3278            // This would otherwise fail when calculating `r` when self == T::MIN.
3279            if rhs == -1 {
3280                return self;
3281            }
3282
3283            let r = self % rhs;
3284            let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
3285                r + rhs
3286            } else {
3287                r
3288            };
3289
3290            if m == 0 {
3291                self
3292            } else {
3293                self + (rhs - m)
3294            }
3295        }
3296
3297        /// If `rhs` is positive, calculates the smallest value greater than or
3298        /// equal to `self` that is a multiple of `rhs`. If `rhs` is negative,
3299        /// calculates the largest value less than or equal to `self` that is a
3300        /// multiple of `rhs`. Returns `None` if `rhs` is zero or the operation
3301        /// would result in overflow.
3302        ///
3303        /// # Examples
3304        ///
3305        /// ```
3306        /// #![feature(int_roundings)]
3307        #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(16));")]
3308        #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(24));")]
3309        #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
3310        #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
3311        #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
3312        #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
3313        #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-16));")]
3314        #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-24));")]
3315        #[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".checked_next_multiple_of(0), None);")]
3316        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_next_multiple_of(2), None);")]
3317        /// ```
3318        #[unstable(feature = "int_roundings", issue = "88581")]
3319        #[must_use = "this returns the result of the operation, \
3320                      without modifying the original"]
3321        #[inline]
3322        #[cfg(not(feature = "ferrocene_certified"))]
3323        pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
3324            // This would otherwise fail when calculating `r` when self == T::MIN.
3325            if rhs == -1 {
3326                return Some(self);
3327            }
3328
3329            let r = try_opt!(self.checked_rem(rhs));
3330            let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
3331                // r + rhs cannot overflow because they have opposite signs
3332                r + rhs
3333            } else {
3334                r
3335            };
3336
3337            if m == 0 {
3338                Some(self)
3339            } else {
3340                // rhs - m cannot overflow because m has the same sign as rhs
3341                self.checked_add(rhs - m)
3342            }
3343        }
3344
3345        /// Returns the logarithm of the number with respect to an arbitrary base,
3346        /// rounded down.
3347        ///
3348        /// This method might not be optimized owing to implementation details;
3349        /// `ilog2` can produce results more efficiently for base 2, and `ilog10`
3350        /// can produce results more efficiently for base 10.
3351        ///
3352        /// # Panics
3353        ///
3354        /// This function will panic if `self` is less than or equal to zero,
3355        /// or if `base` is less than 2.
3356        ///
3357        /// # Examples
3358        ///
3359        /// ```
3360        #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".ilog(5), 1);")]
3361        /// ```
3362        #[stable(feature = "int_log", since = "1.67.0")]
3363        #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3364        #[must_use = "this returns the result of the operation, \
3365                      without modifying the original"]
3366        #[inline]
3367        #[track_caller]
3368        #[cfg(not(feature = "ferrocene_certified"))]
3369        pub const fn ilog(self, base: Self) -> u32 {
3370            assert!(base >= 2, "base of integer logarithm must be at least 2");
3371            if let Some(log) = self.checked_ilog(base) {
3372                log
3373            } else {
3374                int_log10::panic_for_nonpositive_argument()
3375            }
3376        }
3377
3378        /// Returns the base 2 logarithm of the number, rounded down.
3379        ///
3380        /// # Panics
3381        ///
3382        /// This function will panic if `self` is less than or equal to zero.
3383        ///
3384        /// # Examples
3385        ///
3386        /// ```
3387        #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".ilog2(), 1);")]
3388        /// ```
3389        #[stable(feature = "int_log", since = "1.67.0")]
3390        #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3391        #[must_use = "this returns the result of the operation, \
3392                      without modifying the original"]
3393        #[inline]
3394        #[track_caller]
3395        #[cfg(not(feature = "ferrocene_certified"))]
3396        pub const fn ilog2(self) -> u32 {
3397            if let Some(log) = self.checked_ilog2() {
3398                log
3399            } else {
3400                int_log10::panic_for_nonpositive_argument()
3401            }
3402        }
3403
3404        /// Returns the base 10 logarithm of the number, rounded down.
3405        ///
3406        /// # Panics
3407        ///
3408        /// This function will panic if `self` is less than or equal to zero.
3409        ///
3410        /// # Example
3411        ///
3412        /// ```
3413        #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".ilog10(), 1);")]
3414        /// ```
3415        #[stable(feature = "int_log", since = "1.67.0")]
3416        #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3417        #[must_use = "this returns the result of the operation, \
3418                      without modifying the original"]
3419        #[inline]
3420        #[track_caller]
3421        #[cfg(not(feature = "ferrocene_certified"))]
3422        pub const fn ilog10(self) -> u32 {
3423            if let Some(log) = self.checked_ilog10() {
3424                log
3425            } else {
3426                int_log10::panic_for_nonpositive_argument()
3427            }
3428        }
3429
3430        /// Returns the logarithm of the number with respect to an arbitrary base,
3431        /// rounded down.
3432        ///
3433        /// Returns `None` if the number is negative or zero, or if the base is not at least 2.
3434        ///
3435        /// This method might not be optimized owing to implementation details;
3436        /// `checked_ilog2` can produce results more efficiently for base 2, and
3437        /// `checked_ilog10` can produce results more efficiently for base 10.
3438        ///
3439        /// # Examples
3440        ///
3441        /// ```
3442        #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_ilog(5), Some(1));")]
3443        /// ```
3444        #[stable(feature = "int_log", since = "1.67.0")]
3445        #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3446        #[must_use = "this returns the result of the operation, \
3447                      without modifying the original"]
3448        #[inline]
3449        #[cfg(not(feature = "ferrocene_certified"))]
3450        pub const fn checked_ilog(self, base: Self) -> Option<u32> {
3451            if self <= 0 || base <= 1 {
3452                None
3453            } else {
3454                // Delegate to the unsigned implementation.
3455                // The condition makes sure that both casts are exact.
3456                (self as $UnsignedT).checked_ilog(base as $UnsignedT)
3457            }
3458        }
3459
3460        /// Returns the base 2 logarithm of the number, rounded down.
3461        ///
3462        /// Returns `None` if the number is negative or zero.
3463        ///
3464        /// # Examples
3465        ///
3466        /// ```
3467        #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_ilog2(), Some(1));")]
3468        /// ```
3469        #[stable(feature = "int_log", since = "1.67.0")]
3470        #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3471        #[must_use = "this returns the result of the operation, \
3472                      without modifying the original"]
3473        #[inline]
3474        #[cfg(not(feature = "ferrocene_certified"))]
3475        pub const fn checked_ilog2(self) -> Option<u32> {
3476            if self <= 0 {
3477                None
3478            } else {
3479                // SAFETY: We just checked that this number is positive
3480                let log = (Self::BITS - 1) - unsafe { intrinsics::ctlz_nonzero(self) as u32 };
3481                Some(log)
3482            }
3483        }
3484
3485        /// Returns the base 10 logarithm of the number, rounded down.
3486        ///
3487        /// Returns `None` if the number is negative or zero.
3488        ///
3489        /// # Example
3490        ///
3491        /// ```
3492        #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_ilog10(), Some(1));")]
3493        /// ```
3494        #[stable(feature = "int_log", since = "1.67.0")]
3495        #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
3496        #[must_use = "this returns the result of the operation, \
3497                      without modifying the original"]
3498        #[inline]
3499        #[cfg(not(feature = "ferrocene_certified"))]
3500        pub const fn checked_ilog10(self) -> Option<u32> {
3501            if self > 0 {
3502                Some(int_log10::$ActualT(self as $ActualT))
3503            } else {
3504                None
3505            }
3506        }
3507
3508        /// Computes the absolute value of `self`.
3509        ///
3510        /// # Overflow behavior
3511        ///
3512        /// The absolute value of
3513        #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
3514        /// cannot be represented as an
3515        #[doc = concat!("`", stringify!($SelfT), "`,")]
3516        /// and attempting to calculate it will cause an overflow. This means
3517        /// that code in debug mode will trigger a panic on this case and
3518        /// optimized code will return
3519        #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
3520        /// without a panic. If you do not want this behavior, consider
3521        /// using [`unsigned_abs`](Self::unsigned_abs) instead.
3522        ///
3523        /// # Examples
3524        ///
3525        /// ```
3526        #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".abs(), 10);")]
3527        #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").abs(), 10);")]
3528        /// ```
3529        #[stable(feature = "rust1", since = "1.0.0")]
3530        #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3531        #[allow(unused_attributes)]
3532        #[must_use = "this returns the result of the operation, \
3533                      without modifying the original"]
3534        #[inline]
3535        #[rustc_inherit_overflow_checks]
3536        #[cfg(not(feature = "ferrocene_certified"))]
3537        pub const fn abs(self) -> Self {
3538            // Note that the #[rustc_inherit_overflow_checks] and #[inline]
3539            // above mean that the overflow semantics of the subtraction
3540            // depend on the crate we're being called from.
3541            if self.is_negative() {
3542                -self
3543            } else {
3544                self
3545            }
3546        }
3547
3548        /// Computes the absolute difference between `self` and `other`.
3549        ///
3550        /// This function always returns the correct answer without overflow or
3551        /// panics by returning an unsigned integer.
3552        ///
3553        /// # Examples
3554        ///
3555        /// ```
3556        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(80), 20", stringify!($UnsignedT), ");")]
3557        #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(110), 10", stringify!($UnsignedT), ");")]
3558        #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(80), 180", stringify!($UnsignedT), ");")]
3559        #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(-120), 20", stringify!($UnsignedT), ");")]
3560        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.abs_diff(", stringify!($SelfT), "::MAX), ", stringify!($UnsignedT), "::MAX);")]
3561        /// ```
3562        #[stable(feature = "int_abs_diff", since = "1.60.0")]
3563        #[rustc_const_stable(feature = "int_abs_diff", since = "1.60.0")]
3564        #[must_use = "this returns the result of the operation, \
3565                      without modifying the original"]
3566        #[inline]
3567        #[cfg(not(feature = "ferrocene_certified"))]
3568        pub const fn abs_diff(self, other: Self) -> $UnsignedT {
3569            if self < other {
3570                // Converting a non-negative x from signed to unsigned by using
3571                // `x as U` is left unchanged, but a negative x is converted
3572                // to value x + 2^N. Thus if `s` and `o` are binary variables
3573                // respectively indicating whether `self` and `other` are
3574                // negative, we are computing the mathematical value:
3575                //
3576                //    (other + o*2^N) - (self + s*2^N)    mod  2^N
3577                //    other - self + (o-s)*2^N            mod  2^N
3578                //    other - self                        mod  2^N
3579                //
3580                // Finally, taking the mod 2^N of the mathematical value of
3581                // `other - self` does not change it as it already is
3582                // in the range [0, 2^N).
3583                (other as $UnsignedT).wrapping_sub(self as $UnsignedT)
3584            } else {
3585                (self as $UnsignedT).wrapping_sub(other as $UnsignedT)
3586            }
3587        }
3588
3589        /// Returns a number representing sign of `self`.
3590        ///
3591        ///  - `0` if the number is zero
3592        ///  - `1` if the number is positive
3593        ///  - `-1` if the number is negative
3594        ///
3595        /// # Examples
3596        ///
3597        /// ```
3598        #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".signum(), 1);")]
3599        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".signum(), 0);")]
3600        #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").signum(), -1);")]
3601        /// ```
3602        #[stable(feature = "rust1", since = "1.0.0")]
3603        #[rustc_const_stable(feature = "const_int_sign", since = "1.47.0")]
3604        #[must_use = "this returns the result of the operation, \
3605                      without modifying the original"]
3606        #[inline(always)]
3607        #[cfg(not(feature = "ferrocene_certified"))]
3608        pub const fn signum(self) -> Self {
3609            // Picking the right way to phrase this is complicated
3610            // (<https://graphics.stanford.edu/~seander/bithacks.html#CopyIntegerSign>)
3611            // so delegate it to `Ord` which is already producing -1/0/+1
3612            // exactly like we need and can be the place to deal with the complexity.
3613
3614            crate::intrinsics::three_way_compare(self, 0) as Self
3615        }
3616
3617        /// Returns `true` if `self` is positive and `false` if the number is zero or
3618        /// negative.
3619        ///
3620        /// # Examples
3621        ///
3622        /// ```
3623        #[doc = concat!("assert!(10", stringify!($SelfT), ".is_positive());")]
3624        #[doc = concat!("assert!(!(-10", stringify!($SelfT), ").is_positive());")]
3625        /// ```
3626        #[must_use]
3627        #[stable(feature = "rust1", since = "1.0.0")]
3628        #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3629        #[inline(always)]
3630        #[cfg(not(feature = "ferrocene_certified"))]
3631        pub const fn is_positive(self) -> bool { self > 0 }
3632
3633        /// Returns `true` if `self` is negative and `false` if the number is zero or
3634        /// positive.
3635        ///
3636        /// # Examples
3637        ///
3638        /// ```
3639        #[doc = concat!("assert!((-10", stringify!($SelfT), ").is_negative());")]
3640        #[doc = concat!("assert!(!10", stringify!($SelfT), ".is_negative());")]
3641        /// ```
3642        #[must_use]
3643        #[stable(feature = "rust1", since = "1.0.0")]
3644        #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
3645        #[inline(always)]
3646        #[cfg(not(feature = "ferrocene_certified"))]
3647        pub const fn is_negative(self) -> bool { self < 0 }
3648
3649        /// Returns the memory representation of this integer as a byte array in
3650        /// big-endian (network) byte order.
3651        ///
3652        #[doc = $to_xe_bytes_doc]
3653        ///
3654        /// # Examples
3655        ///
3656        /// ```
3657        #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();")]
3658        #[doc = concat!("assert_eq!(bytes, ", $be_bytes, ");")]
3659        /// ```
3660        #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3661        #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3662        #[must_use = "this returns the result of the operation, \
3663                      without modifying the original"]
3664        #[inline]
3665        #[cfg(not(feature = "ferrocene_certified"))]
3666        pub const fn to_be_bytes(self) -> [u8; size_of::<Self>()] {
3667            self.to_be().to_ne_bytes()
3668        }
3669
3670        /// Returns the memory representation of this integer as a byte array in
3671        /// little-endian byte order.
3672        ///
3673        #[doc = $to_xe_bytes_doc]
3674        ///
3675        /// # Examples
3676        ///
3677        /// ```
3678        #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();")]
3679        #[doc = concat!("assert_eq!(bytes, ", $le_bytes, ");")]
3680        /// ```
3681        #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3682        #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3683        #[must_use = "this returns the result of the operation, \
3684                      without modifying the original"]
3685        #[inline]
3686        #[cfg(not(feature = "ferrocene_certified"))]
3687        pub const fn to_le_bytes(self) -> [u8; size_of::<Self>()] {
3688            self.to_le().to_ne_bytes()
3689        }
3690
3691        /// Returns the memory representation of this integer as a byte array in
3692        /// native byte order.
3693        ///
3694        /// As the target platform's native endianness is used, portable code
3695        /// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate,
3696        /// instead.
3697        ///
3698        #[doc = $to_xe_bytes_doc]
3699        ///
3700        /// [`to_be_bytes`]: Self::to_be_bytes
3701        /// [`to_le_bytes`]: Self::to_le_bytes
3702        ///
3703        /// # Examples
3704        ///
3705        /// ```
3706        #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_ne_bytes();")]
3707        /// assert_eq!(
3708        ///     bytes,
3709        ///     if cfg!(target_endian = "big") {
3710        #[doc = concat!("        ", $be_bytes)]
3711        ///     } else {
3712        #[doc = concat!("        ", $le_bytes)]
3713        ///     }
3714        /// );
3715        /// ```
3716        #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3717        #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3718        #[allow(unnecessary_transmutes)]
3719        // SAFETY: const sound because integers are plain old datatypes so we can always
3720        // transmute them to arrays of bytes
3721        #[must_use = "this returns the result of the operation, \
3722                      without modifying the original"]
3723        #[inline]
3724        #[cfg(not(feature = "ferrocene_certified"))]
3725        pub const fn to_ne_bytes(self) -> [u8; size_of::<Self>()] {
3726            // SAFETY: integers are plain old datatypes so we can always transmute them to
3727            // arrays of bytes
3728            unsafe { mem::transmute(self) }
3729        }
3730
3731        /// Creates an integer value from its representation as a byte array in
3732        /// big endian.
3733        ///
3734        #[doc = $from_xe_bytes_doc]
3735        ///
3736        /// # Examples
3737        ///
3738        /// ```
3739        #[doc = concat!("let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, ");")]
3740        #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3741        /// ```
3742        ///
3743        /// When starting from a slice rather than an array, fallible conversion APIs can be used:
3744        ///
3745        /// ```
3746        #[doc = concat!("fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3747        #[doc = concat!("    let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3748        ///     *input = rest;
3749        #[doc = concat!("    ", stringify!($SelfT), "::from_be_bytes(int_bytes.try_into().unwrap())")]
3750        /// }
3751        /// ```
3752        #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3753        #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3754        #[must_use]
3755        #[inline]
3756        #[cfg(not(feature = "ferrocene_certified"))]
3757        pub const fn from_be_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3758            Self::from_be(Self::from_ne_bytes(bytes))
3759        }
3760
3761        /// Creates an integer value from its representation as a byte array in
3762        /// little endian.
3763        ///
3764        #[doc = $from_xe_bytes_doc]
3765        ///
3766        /// # Examples
3767        ///
3768        /// ```
3769        #[doc = concat!("let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, ");")]
3770        #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3771        /// ```
3772        ///
3773        /// When starting from a slice rather than an array, fallible conversion APIs can be used:
3774        ///
3775        /// ```
3776        #[doc = concat!("fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3777        #[doc = concat!("    let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3778        ///     *input = rest;
3779        #[doc = concat!("    ", stringify!($SelfT), "::from_le_bytes(int_bytes.try_into().unwrap())")]
3780        /// }
3781        /// ```
3782        #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3783        #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3784        #[must_use]
3785        #[inline]
3786        #[cfg(not(feature = "ferrocene_certified"))]
3787        pub const fn from_le_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3788            Self::from_le(Self::from_ne_bytes(bytes))
3789        }
3790
3791        /// Creates an integer value from its memory representation as a byte
3792        /// array in native endianness.
3793        ///
3794        /// As the target platform's native endianness is used, portable code
3795        /// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as
3796        /// appropriate instead.
3797        ///
3798        /// [`from_be_bytes`]: Self::from_be_bytes
3799        /// [`from_le_bytes`]: Self::from_le_bytes
3800        ///
3801        #[doc = $from_xe_bytes_doc]
3802        ///
3803        /// # Examples
3804        ///
3805        /// ```
3806        #[doc = concat!("let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"big\") {")]
3807        #[doc = concat!("    ", $be_bytes)]
3808        /// } else {
3809        #[doc = concat!("    ", $le_bytes)]
3810        /// });
3811        #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
3812        /// ```
3813        ///
3814        /// When starting from a slice rather than an array, fallible conversion APIs can be used:
3815        ///
3816        /// ```
3817        #[doc = concat!("fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
3818        #[doc = concat!("    let (int_bytes, rest) = input.split_at(size_of::<", stringify!($SelfT), ">());")]
3819        ///     *input = rest;
3820        #[doc = concat!("    ", stringify!($SelfT), "::from_ne_bytes(int_bytes.try_into().unwrap())")]
3821        /// }
3822        /// ```
3823        #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
3824        #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
3825        #[allow(unnecessary_transmutes)]
3826        #[must_use]
3827        // SAFETY: const sound because integers are plain old datatypes so we can always
3828        // transmute to them
3829        #[inline]
3830        #[cfg(not(feature = "ferrocene_certified"))]
3831        pub const fn from_ne_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
3832            // SAFETY: integers are plain old datatypes so we can always transmute to them
3833            unsafe { mem::transmute(bytes) }
3834        }
3835
3836        /// New code should prefer to use
3837        #[doc = concat!("[`", stringify!($SelfT), "::MIN", "`] instead.")]
3838        ///
3839        /// Returns the smallest value that can be represented by this integer type.
3840        #[stable(feature = "rust1", since = "1.0.0")]
3841        #[inline(always)]
3842        #[rustc_promotable]
3843        #[rustc_const_stable(feature = "const_min_value", since = "1.32.0")]
3844        #[deprecated(since = "TBD", note = "replaced by the `MIN` associated constant on this type")]
3845        #[rustc_diagnostic_item = concat!(stringify!($SelfT), "_legacy_fn_min_value")]
3846        #[cfg(not(feature = "ferrocene_certified"))]
3847        pub const fn min_value() -> Self {
3848            Self::MIN
3849        }
3850
3851        /// New code should prefer to use
3852        #[doc = concat!("[`", stringify!($SelfT), "::MAX", "`] instead.")]
3853        ///
3854        /// Returns the largest value that can be represented by this integer type.
3855        #[stable(feature = "rust1", since = "1.0.0")]
3856        #[inline(always)]
3857        #[rustc_promotable]
3858        #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
3859        #[deprecated(since = "TBD", note = "replaced by the `MAX` associated constant on this type")]
3860        #[rustc_diagnostic_item = concat!(stringify!($SelfT), "_legacy_fn_max_value")]
3861        #[cfg(not(feature = "ferrocene_certified"))]
3862        pub const fn max_value() -> Self {
3863            Self::MAX
3864        }
3865    }
3866}