]> git.lizzy.rs Git - rust.git/blob - library/core/src/num/int_macros.rs
Auto merge of #105409 - compiler-errors:closure-infer-cycle, r=jackh726
[rust.git] / library / core / src / num / int_macros.rs
1 macro_rules! int_impl {
2     ($SelfT:ty, $ActualT:ident, $UnsignedT:ty, $BITS:expr, $BITS_MINUS_ONE:expr, $Min:expr, $Max:expr,
3      $rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
4      $reversed:expr, $le_bytes:expr, $be_bytes:expr,
5      $to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr,
6      $bound_condition:expr) => {
7         /// The smallest value that can be represented by this integer type
8         #[doc = concat!("(&minus;2<sup>", $BITS_MINUS_ONE, "</sup>", $bound_condition, ")")]
9         ///
10         /// # Examples
11         ///
12         /// Basic usage:
13         ///
14         /// ```
15         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN, ", stringify!($Min), ");")]
16         /// ```
17         #[stable(feature = "assoc_int_consts", since = "1.43.0")]
18         pub const MIN: Self = !0 ^ ((!0 as $UnsignedT) >> 1) as Self;
19
20         /// The largest value that can be represented by this integer type
21         #[doc = concat!("(2<sup>", $BITS_MINUS_ONE, "</sup> &minus; 1", $bound_condition, ")")]
22         ///
23         /// # Examples
24         ///
25         /// Basic usage:
26         ///
27         /// ```
28         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX, ", stringify!($Max), ");")]
29         /// ```
30         #[stable(feature = "assoc_int_consts", since = "1.43.0")]
31         pub const MAX: Self = !Self::MIN;
32
33         /// The size of this integer type in bits.
34         ///
35         /// # Examples
36         ///
37         /// ```
38         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::BITS, ", stringify!($BITS), ");")]
39         /// ```
40         #[stable(feature = "int_bits_const", since = "1.53.0")]
41         pub const BITS: u32 = $BITS;
42
43         /// Converts a string slice in a given base to an integer.
44         ///
45         /// The string is expected to be an optional `+` or `-` sign followed by digits.
46         /// Leading and trailing whitespace represent an error. Digits are a subset of these characters,
47         /// depending on `radix`:
48         ///
49         ///  * `0-9`
50         ///  * `a-z`
51         ///  * `A-Z`
52         ///
53         /// # Panics
54         ///
55         /// This function panics if `radix` is not in the range from 2 to 36.
56         ///
57         /// # Examples
58         ///
59         /// Basic usage:
60         ///
61         /// ```
62         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::from_str_radix(\"A\", 16), Ok(10));")]
63         /// ```
64         #[stable(feature = "rust1", since = "1.0.0")]
65         pub fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> {
66             from_str_radix(src, radix)
67         }
68
69         /// Returns the number of ones in the binary representation of `self`.
70         ///
71         /// # Examples
72         ///
73         /// Basic usage:
74         ///
75         /// ```
76         #[doc = concat!("let n = 0b100_0000", stringify!($SelfT), ";")]
77         ///
78         /// assert_eq!(n.count_ones(), 1);
79         /// ```
80         ///
81         #[stable(feature = "rust1", since = "1.0.0")]
82         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
83         #[doc(alias = "popcount")]
84         #[doc(alias = "popcnt")]
85         #[must_use = "this returns the result of the operation, \
86                       without modifying the original"]
87         #[inline(always)]
88         pub const fn count_ones(self) -> u32 { (self as $UnsignedT).count_ones() }
89
90         /// Returns the number of zeros in the binary representation of `self`.
91         ///
92         /// # Examples
93         ///
94         /// Basic usage:
95         ///
96         /// ```
97         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.count_zeros(), 1);")]
98         /// ```
99         #[stable(feature = "rust1", since = "1.0.0")]
100         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
101         #[must_use = "this returns the result of the operation, \
102                       without modifying the original"]
103         #[inline(always)]
104         pub const fn count_zeros(self) -> u32 {
105             (!self).count_ones()
106         }
107
108         /// Returns the number of leading zeros in the binary representation of `self`.
109         ///
110         /// Depending on what you're doing with the value, you might also be interested in the
111         /// [`ilog2`] function which returns a consistent number, even if the type widens.
112         ///
113         /// # Examples
114         ///
115         /// Basic usage:
116         ///
117         /// ```
118         #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
119         ///
120         /// assert_eq!(n.leading_zeros(), 0);
121         /// ```
122         #[doc = concat!("[`ilog2`]: ", stringify!($SelfT), "::ilog2")]
123         #[stable(feature = "rust1", since = "1.0.0")]
124         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
125         #[must_use = "this returns the result of the operation, \
126                       without modifying the original"]
127         #[inline(always)]
128         pub const fn leading_zeros(self) -> u32 {
129             (self as $UnsignedT).leading_zeros()
130         }
131
132         /// Returns the number of trailing zeros in the binary representation of `self`.
133         ///
134         /// # Examples
135         ///
136         /// Basic usage:
137         ///
138         /// ```
139         #[doc = concat!("let n = -4", stringify!($SelfT), ";")]
140         ///
141         /// assert_eq!(n.trailing_zeros(), 2);
142         /// ```
143         #[stable(feature = "rust1", since = "1.0.0")]
144         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
145         #[must_use = "this returns the result of the operation, \
146                       without modifying the original"]
147         #[inline(always)]
148         pub const fn trailing_zeros(self) -> u32 {
149             (self as $UnsignedT).trailing_zeros()
150         }
151
152         /// Returns the number of leading ones in the binary representation of `self`.
153         ///
154         /// # Examples
155         ///
156         /// Basic usage:
157         ///
158         /// ```
159         #[doc = concat!("let n = -1", stringify!($SelfT), ";")]
160         ///
161         #[doc = concat!("assert_eq!(n.leading_ones(), ", stringify!($BITS), ");")]
162         /// ```
163         #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
164         #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
165         #[must_use = "this returns the result of the operation, \
166                       without modifying the original"]
167         #[inline(always)]
168         pub const fn leading_ones(self) -> u32 {
169             (self as $UnsignedT).leading_ones()
170         }
171
172         /// Returns the number of trailing ones in the binary representation of `self`.
173         ///
174         /// # Examples
175         ///
176         /// Basic usage:
177         ///
178         /// ```
179         #[doc = concat!("let n = 3", stringify!($SelfT), ";")]
180         ///
181         /// assert_eq!(n.trailing_ones(), 2);
182         /// ```
183         #[stable(feature = "leading_trailing_ones", since = "1.46.0")]
184         #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")]
185         #[must_use = "this returns the result of the operation, \
186                       without modifying the original"]
187         #[inline(always)]
188         pub const fn trailing_ones(self) -> u32 {
189             (self as $UnsignedT).trailing_ones()
190         }
191
192         /// Shifts the bits to the left by a specified amount, `n`,
193         /// wrapping the truncated bits to the end of the resulting integer.
194         ///
195         /// Please note this isn't the same operation as the `<<` shifting operator!
196         ///
197         /// # Examples
198         ///
199         /// Basic usage:
200         ///
201         /// ```
202         #[doc = concat!("let n = ", $rot_op, stringify!($SelfT), ";")]
203         #[doc = concat!("let m = ", $rot_result, ";")]
204         ///
205         #[doc = concat!("assert_eq!(n.rotate_left(", $rot, "), m);")]
206         /// ```
207         #[stable(feature = "rust1", since = "1.0.0")]
208         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
209         #[must_use = "this returns the result of the operation, \
210                       without modifying the original"]
211         #[inline(always)]
212         pub const fn rotate_left(self, n: u32) -> Self {
213             (self as $UnsignedT).rotate_left(n) as Self
214         }
215
216         /// Shifts the bits to the right by a specified amount, `n`,
217         /// wrapping the truncated bits to the beginning of the resulting
218         /// integer.
219         ///
220         /// Please note this isn't the same operation as the `>>` shifting operator!
221         ///
222         /// # Examples
223         ///
224         /// Basic usage:
225         ///
226         /// ```
227         #[doc = concat!("let n = ", $rot_result, stringify!($SelfT), ";")]
228         #[doc = concat!("let m = ", $rot_op, ";")]
229         ///
230         #[doc = concat!("assert_eq!(n.rotate_right(", $rot, "), m);")]
231         /// ```
232         #[stable(feature = "rust1", since = "1.0.0")]
233         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
234         #[must_use = "this returns the result of the operation, \
235                       without modifying the original"]
236         #[inline(always)]
237         pub const fn rotate_right(self, n: u32) -> Self {
238             (self as $UnsignedT).rotate_right(n) as Self
239         }
240
241         /// Reverses the byte order of the integer.
242         ///
243         /// # Examples
244         ///
245         /// Basic usage:
246         ///
247         /// ```
248         #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
249         ///
250         /// let m = n.swap_bytes();
251         ///
252         #[doc = concat!("assert_eq!(m, ", $swapped, ");")]
253         /// ```
254         #[stable(feature = "rust1", since = "1.0.0")]
255         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
256         #[must_use = "this returns the result of the operation, \
257                       without modifying the original"]
258         #[inline(always)]
259         pub const fn swap_bytes(self) -> Self {
260             (self as $UnsignedT).swap_bytes() as Self
261         }
262
263         /// Reverses the order of bits in the integer. The least significant bit becomes the most significant bit,
264         ///                 second least-significant bit becomes second most-significant bit, etc.
265         ///
266         /// # Examples
267         ///
268         /// Basic usage:
269         ///
270         /// ```
271         #[doc = concat!("let n = ", $swap_op, stringify!($SelfT), ";")]
272         /// let m = n.reverse_bits();
273         ///
274         #[doc = concat!("assert_eq!(m, ", $reversed, ");")]
275         #[doc = concat!("assert_eq!(0, 0", stringify!($SelfT), ".reverse_bits());")]
276         /// ```
277         #[stable(feature = "reverse_bits", since = "1.37.0")]
278         #[rustc_const_stable(feature = "reverse_bits", since = "1.37.0")]
279         #[must_use = "this returns the result of the operation, \
280                       without modifying the original"]
281         #[inline(always)]
282         pub const fn reverse_bits(self) -> Self {
283             (self as $UnsignedT).reverse_bits() as Self
284         }
285
286         /// Converts an integer from big endian to the target's endianness.
287         ///
288         /// On big endian this is a no-op. On little endian the bytes are swapped.
289         ///
290         /// # Examples
291         ///
292         /// Basic usage:
293         ///
294         /// ```
295         #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
296         ///
297         /// if cfg!(target_endian = "big") {
298         #[doc = concat!("    assert_eq!(", stringify!($SelfT), "::from_be(n), n)")]
299         /// } else {
300         #[doc = concat!("    assert_eq!(", stringify!($SelfT), "::from_be(n), n.swap_bytes())")]
301         /// }
302         /// ```
303         #[stable(feature = "rust1", since = "1.0.0")]
304         #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
305         #[must_use]
306         #[inline]
307         pub const fn from_be(x: Self) -> Self {
308             #[cfg(target_endian = "big")]
309             {
310                 x
311             }
312             #[cfg(not(target_endian = "big"))]
313             {
314                 x.swap_bytes()
315             }
316         }
317
318         /// Converts an integer from little endian to the target's endianness.
319         ///
320         /// On little endian this is a no-op. On big endian the bytes are swapped.
321         ///
322         /// # Examples
323         ///
324         /// Basic usage:
325         ///
326         /// ```
327         #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
328         ///
329         /// if cfg!(target_endian = "little") {
330         #[doc = concat!("    assert_eq!(", stringify!($SelfT), "::from_le(n), n)")]
331         /// } else {
332         #[doc = concat!("    assert_eq!(", stringify!($SelfT), "::from_le(n), n.swap_bytes())")]
333         /// }
334         /// ```
335         #[stable(feature = "rust1", since = "1.0.0")]
336         #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
337         #[must_use]
338         #[inline]
339         pub const fn from_le(x: Self) -> Self {
340             #[cfg(target_endian = "little")]
341             {
342                 x
343             }
344             #[cfg(not(target_endian = "little"))]
345             {
346                 x.swap_bytes()
347             }
348         }
349
350         /// Converts `self` to big endian from the target's endianness.
351         ///
352         /// On big endian this is a no-op. On little endian the bytes are swapped.
353         ///
354         /// # Examples
355         ///
356         /// Basic usage:
357         ///
358         /// ```
359         #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
360         ///
361         /// if cfg!(target_endian = "big") {
362         ///     assert_eq!(n.to_be(), n)
363         /// } else {
364         ///     assert_eq!(n.to_be(), n.swap_bytes())
365         /// }
366         /// ```
367         #[stable(feature = "rust1", since = "1.0.0")]
368         #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
369         #[must_use = "this returns the result of the operation, \
370                       without modifying the original"]
371         #[inline]
372         pub const fn to_be(self) -> Self { // or not to be?
373             #[cfg(target_endian = "big")]
374             {
375                 self
376             }
377             #[cfg(not(target_endian = "big"))]
378             {
379                 self.swap_bytes()
380             }
381         }
382
383         /// Converts `self` to little endian from the target's endianness.
384         ///
385         /// On little endian this is a no-op. On big endian the bytes are swapped.
386         ///
387         /// # Examples
388         ///
389         /// Basic usage:
390         ///
391         /// ```
392         #[doc = concat!("let n = 0x1A", stringify!($SelfT), ";")]
393         ///
394         /// if cfg!(target_endian = "little") {
395         ///     assert_eq!(n.to_le(), n)
396         /// } else {
397         ///     assert_eq!(n.to_le(), n.swap_bytes())
398         /// }
399         /// ```
400         #[stable(feature = "rust1", since = "1.0.0")]
401         #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")]
402         #[must_use = "this returns the result of the operation, \
403                       without modifying the original"]
404         #[inline]
405         pub const fn to_le(self) -> Self {
406             #[cfg(target_endian = "little")]
407             {
408                 self
409             }
410             #[cfg(not(target_endian = "little"))]
411             {
412                 self.swap_bytes()
413             }
414         }
415
416         /// Checked integer addition. Computes `self + rhs`, returning `None`
417         /// if overflow occurred.
418         ///
419         /// # Examples
420         ///
421         /// Basic usage:
422         ///
423         /// ```
424         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(1), Some(", stringify!($SelfT), "::MAX - 1));")]
425         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);")]
426         /// ```
427         #[stable(feature = "rust1", since = "1.0.0")]
428         #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
429         #[must_use = "this returns the result of the operation, \
430                       without modifying the original"]
431         #[inline]
432         pub const fn checked_add(self, rhs: Self) -> Option<Self> {
433             let (a, b) = self.overflowing_add(rhs);
434             if unlikely!(b) {None} else {Some(a)}
435         }
436
437         /// Unchecked integer addition. Computes `self + rhs`, assuming overflow
438         /// cannot occur.
439         ///
440         /// # Safety
441         ///
442         /// This results in undefined behavior when
443         #[doc = concat!("`self + rhs > ", stringify!($SelfT), "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`,")]
444         /// i.e. when [`checked_add`] would return `None`.
445         ///
446         #[doc = concat!("[`checked_add`]: ", stringify!($SelfT), "::checked_add")]
447         #[unstable(
448             feature = "unchecked_math",
449             reason = "niche optimization path",
450             issue = "85122",
451         )]
452         #[must_use = "this returns the result of the operation, \
453                       without modifying the original"]
454         #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
455         #[inline(always)]
456         #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
457         pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
458             // SAFETY: the caller must uphold the safety contract for
459             // `unchecked_add`.
460             unsafe { intrinsics::unchecked_add(self, rhs) }
461         }
462
463         /// Checked addition with an unsigned integer. Computes `self + rhs`,
464         /// returning `None` if overflow occurred.
465         ///
466         /// # Examples
467         ///
468         /// Basic usage:
469         ///
470         /// ```
471         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_add_unsigned(2), Some(3));")]
472         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add_unsigned(3), None);")]
473         /// ```
474         #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
475         #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
476         #[must_use = "this returns the result of the operation, \
477                       without modifying the original"]
478         #[inline]
479         pub const fn checked_add_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
480             let (a, b) = self.overflowing_add_unsigned(rhs);
481             if unlikely!(b) {None} else {Some(a)}
482         }
483
484         /// Checked integer subtraction. Computes `self - rhs`, returning `None` if
485         /// overflow occurred.
486         ///
487         /// # Examples
488         ///
489         /// Basic usage:
490         ///
491         /// ```
492         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub(1), Some(", stringify!($SelfT), "::MIN + 1));")]
493         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub(3), None);")]
494         /// ```
495         #[stable(feature = "rust1", since = "1.0.0")]
496         #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
497         #[must_use = "this returns the result of the operation, \
498                       without modifying the original"]
499         #[inline]
500         pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
501             let (a, b) = self.overflowing_sub(rhs);
502             if unlikely!(b) {None} else {Some(a)}
503         }
504
505         /// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow
506         /// cannot occur.
507         ///
508         /// # Safety
509         ///
510         /// This results in undefined behavior when
511         #[doc = concat!("`self - rhs > ", stringify!($SelfT), "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`,")]
512         /// i.e. when [`checked_sub`] would return `None`.
513         ///
514         #[doc = concat!("[`checked_sub`]: ", stringify!($SelfT), "::checked_sub")]
515         #[unstable(
516             feature = "unchecked_math",
517             reason = "niche optimization path",
518             issue = "85122",
519         )]
520         #[must_use = "this returns the result of the operation, \
521                       without modifying the original"]
522         #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
523         #[inline(always)]
524         #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
525         pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
526             // SAFETY: the caller must uphold the safety contract for
527             // `unchecked_sub`.
528             unsafe { intrinsics::unchecked_sub(self, rhs) }
529         }
530
531         /// Checked subtraction with an unsigned integer. Computes `self - rhs`,
532         /// returning `None` if overflow occurred.
533         ///
534         /// # Examples
535         ///
536         /// Basic usage:
537         ///
538         /// ```
539         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".checked_sub_unsigned(2), Some(-1));")]
540         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub_unsigned(3), None);")]
541         /// ```
542         #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
543         #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
544         #[must_use = "this returns the result of the operation, \
545                       without modifying the original"]
546         #[inline]
547         pub const fn checked_sub_unsigned(self, rhs: $UnsignedT) -> Option<Self> {
548             let (a, b) = self.overflowing_sub_unsigned(rhs);
549             if unlikely!(b) {None} else {Some(a)}
550         }
551
552         /// Checked integer multiplication. Computes `self * rhs`, returning `None` if
553         /// overflow occurred.
554         ///
555         /// # Examples
556         ///
557         /// Basic usage:
558         ///
559         /// ```
560         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(1), Some(", stringify!($SelfT), "::MAX));")]
561         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);")]
562         /// ```
563         #[stable(feature = "rust1", since = "1.0.0")]
564         #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
565         #[must_use = "this returns the result of the operation, \
566                       without modifying the original"]
567         #[inline]
568         pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
569             let (a, b) = self.overflowing_mul(rhs);
570             if unlikely!(b) {None} else {Some(a)}
571         }
572
573         /// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow
574         /// cannot occur.
575         ///
576         /// # Safety
577         ///
578         /// This results in undefined behavior when
579         #[doc = concat!("`self * rhs > ", stringify!($SelfT), "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`,")]
580         /// i.e. when [`checked_mul`] would return `None`.
581         ///
582         #[doc = concat!("[`checked_mul`]: ", stringify!($SelfT), "::checked_mul")]
583         #[unstable(
584             feature = "unchecked_math",
585             reason = "niche optimization path",
586             issue = "85122",
587         )]
588         #[must_use = "this returns the result of the operation, \
589                       without modifying the original"]
590         #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
591         #[inline(always)]
592         #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
593         pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
594             // SAFETY: the caller must uphold the safety contract for
595             // `unchecked_mul`.
596             unsafe { intrinsics::unchecked_mul(self, rhs) }
597         }
598
599         /// Checked integer division. Computes `self / rhs`, returning `None` if `rhs == 0`
600         /// or the division results in overflow.
601         ///
602         /// # Examples
603         ///
604         /// Basic usage:
605         ///
606         /// ```
607         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div(-1), Some(", stringify!($Max), "));")]
608         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div(-1), None);")]
609         #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div(0), None);")]
610         /// ```
611         #[stable(feature = "rust1", since = "1.0.0")]
612         #[rustc_const_stable(feature = "const_checked_int_div", since = "1.52.0")]
613         #[must_use = "this returns the result of the operation, \
614                       without modifying the original"]
615         #[inline]
616         pub const fn checked_div(self, rhs: Self) -> Option<Self> {
617             if unlikely!(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
618                 None
619             } else {
620                 // SAFETY: div by zero and by INT_MIN have been checked above
621                 Some(unsafe { intrinsics::unchecked_div(self, rhs) })
622             }
623         }
624
625         /// Checked Euclidean division. Computes `self.div_euclid(rhs)`,
626         /// returning `None` if `rhs == 0` or the division results in overflow.
627         ///
628         /// # Examples
629         ///
630         /// Basic usage:
631         ///
632         /// ```
633         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).checked_div_euclid(-1), Some(", stringify!($Max), "));")]
634         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_div_euclid(-1), None);")]
635         #[doc = concat!("assert_eq!((1", stringify!($SelfT), ").checked_div_euclid(0), None);")]
636         /// ```
637         #[stable(feature = "euclidean_division", since = "1.38.0")]
638         #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
639         #[must_use = "this returns the result of the operation, \
640                       without modifying the original"]
641         #[inline]
642         pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
643             // Using `&` helps LLVM see that it is the same check made in division.
644             if unlikely!(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
645                 None
646             } else {
647                 Some(self.div_euclid(rhs))
648             }
649         }
650
651         /// Checked integer remainder. Computes `self % rhs`, returning `None` if
652         /// `rhs == 0` or the division results in overflow.
653         ///
654         /// # Examples
655         ///
656         /// Basic usage:
657         ///
658         /// ```
659         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(2), Some(1));")]
660         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem(0), None);")]
661         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem(-1), None);")]
662         /// ```
663         #[stable(feature = "wrapping", since = "1.7.0")]
664         #[rustc_const_stable(feature = "const_checked_int_div", since = "1.52.0")]
665         #[must_use = "this returns the result of the operation, \
666                       without modifying the original"]
667         #[inline]
668         pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
669             if unlikely!(rhs == 0 || ((self == Self::MIN) && (rhs == -1))) {
670                 None
671             } else {
672                 // SAFETY: div by zero and by INT_MIN have been checked above
673                 Some(unsafe { intrinsics::unchecked_rem(self, rhs) })
674             }
675         }
676
677         /// Checked Euclidean remainder. Computes `self.rem_euclid(rhs)`, returning `None`
678         /// if `rhs == 0` or the division results in overflow.
679         ///
680         /// # Examples
681         ///
682         /// Basic usage:
683         ///
684         /// ```
685         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(2), Some(1));")]
686         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(0), None);")]
687         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_rem_euclid(-1), None);")]
688         /// ```
689         #[stable(feature = "euclidean_division", since = "1.38.0")]
690         #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
691         #[must_use = "this returns the result of the operation, \
692                       without modifying the original"]
693         #[inline]
694         pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
695             // Using `&` helps LLVM see that it is the same check made in division.
696             if unlikely!(rhs == 0 || ((self == Self::MIN) & (rhs == -1))) {
697                 None
698             } else {
699                 Some(self.rem_euclid(rhs))
700             }
701         }
702
703         /// Checked negation. Computes `-self`, returning `None` if `self == MIN`.
704         ///
705         /// # Examples
706         ///
707         /// Basic usage:
708         ///
709         /// ```
710         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_neg(), Some(-5));")]
711         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_neg(), None);")]
712         /// ```
713         #[stable(feature = "wrapping", since = "1.7.0")]
714         #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
715         #[must_use = "this returns the result of the operation, \
716                       without modifying the original"]
717         #[inline]
718         pub const fn checked_neg(self) -> Option<Self> {
719             let (a, b) = self.overflowing_neg();
720             if unlikely!(b) {None} else {Some(a)}
721         }
722
723         /// Checked shift left. Computes `self << rhs`, returning `None` if `rhs` is larger
724         /// than or equal to the number of bits in `self`.
725         ///
726         /// # Examples
727         ///
728         /// Basic usage:
729         ///
730         /// ```
731         #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(4), Some(0x10));")]
732         #[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".checked_shl(129), None);")]
733         /// ```
734         #[stable(feature = "wrapping", since = "1.7.0")]
735         #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
736         #[must_use = "this returns the result of the operation, \
737                       without modifying the original"]
738         #[inline]
739         pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
740             let (a, b) = self.overflowing_shl(rhs);
741             if unlikely!(b) {None} else {Some(a)}
742         }
743
744         /// Unchecked shift left. Computes `self << rhs`, assuming that
745         /// `rhs` is less than the number of bits in `self`.
746         ///
747         /// # Safety
748         ///
749         /// This results in undefined behavior if `rhs` is larger than
750         /// or equal to the number of bits in `self`,
751         /// i.e. when [`checked_shl`] would return `None`.
752         ///
753         #[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
754         #[unstable(
755             feature = "unchecked_math",
756             reason = "niche optimization path",
757             issue = "85122",
758         )]
759         #[must_use = "this returns the result of the operation, \
760                       without modifying the original"]
761         #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
762         #[inline(always)]
763         #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
764         pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
765             // SAFETY: the caller must uphold the safety contract for
766             // `unchecked_shl`.
767             // Any legal shift amount is losslessly representable in the self type.
768             unsafe { intrinsics::unchecked_shl(self, rhs.try_into().ok().unwrap_unchecked()) }
769         }
770
771         /// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
772         /// larger than or equal to the number of bits in `self`.
773         ///
774         /// # Examples
775         ///
776         /// Basic usage:
777         ///
778         /// ```
779         #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(4), Some(0x1));")]
780         #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".checked_shr(128), None);")]
781         /// ```
782         #[stable(feature = "wrapping", since = "1.7.0")]
783         #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
784         #[must_use = "this returns the result of the operation, \
785                       without modifying the original"]
786         #[inline]
787         pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
788             let (a, b) = self.overflowing_shr(rhs);
789             if unlikely!(b) {None} else {Some(a)}
790         }
791
792         /// Unchecked shift right. Computes `self >> rhs`, assuming that
793         /// `rhs` is less than the number of bits in `self`.
794         ///
795         /// # Safety
796         ///
797         /// This results in undefined behavior if `rhs` is larger than
798         /// or equal to the number of bits in `self`,
799         /// i.e. when [`checked_shr`] would return `None`.
800         ///
801         #[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
802         #[unstable(
803             feature = "unchecked_math",
804             reason = "niche optimization path",
805             issue = "85122",
806         )]
807         #[must_use = "this returns the result of the operation, \
808                       without modifying the original"]
809         #[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
810         #[inline(always)]
811         #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
812         pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
813             // SAFETY: the caller must uphold the safety contract for
814             // `unchecked_shr`.
815             // Any legal shift amount is losslessly representable in the self type.
816             unsafe { intrinsics::unchecked_shr(self, rhs.try_into().ok().unwrap_unchecked()) }
817         }
818
819         /// Checked absolute value. Computes `self.abs()`, returning `None` if
820         /// `self == MIN`.
821         ///
822         /// # Examples
823         ///
824         /// Basic usage:
825         ///
826         /// ```
827         #[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").checked_abs(), Some(5));")]
828         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.checked_abs(), None);")]
829         /// ```
830         #[stable(feature = "no_panic_abs", since = "1.13.0")]
831         #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")]
832         #[must_use = "this returns the result of the operation, \
833                       without modifying the original"]
834         #[inline]
835         pub const fn checked_abs(self) -> Option<Self> {
836             if self.is_negative() {
837                 self.checked_neg()
838             } else {
839                 Some(self)
840             }
841         }
842
843         /// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if
844         /// overflow occurred.
845         ///
846         /// # Examples
847         ///
848         /// Basic usage:
849         ///
850         /// ```
851         #[doc = concat!("assert_eq!(8", stringify!($SelfT), ".checked_pow(2), Some(64));")]
852         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);")]
853         /// ```
854
855         #[stable(feature = "no_panic_pow", since = "1.34.0")]
856         #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
857         #[must_use = "this returns the result of the operation, \
858                       without modifying the original"]
859         #[inline]
860         pub const fn checked_pow(self, mut exp: u32) -> Option<Self> {
861             if exp == 0 {
862                 return Some(1);
863             }
864             let mut base = self;
865             let mut acc: Self = 1;
866
867             while exp > 1 {
868                 if (exp & 1) == 1 {
869                     acc = try_opt!(acc.checked_mul(base));
870                 }
871                 exp /= 2;
872                 base = try_opt!(base.checked_mul(base));
873             }
874             // since exp!=0, finally the exp must be 1.
875             // Deal with the final bit of the exponent separately, since
876             // squaring the base afterwards is not necessary and may cause a
877             // needless overflow.
878             acc.checked_mul(base)
879         }
880
881         /// Saturating integer addition. Computes `self + rhs`, saturating at the numeric
882         /// bounds instead of overflowing.
883         ///
884         /// # Examples
885         ///
886         /// Basic usage:
887         ///
888         /// ```
889         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_add(1), 101);")]
890         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add(100), ", stringify!($SelfT), "::MAX);")]
891         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_add(-1), ", stringify!($SelfT), "::MIN);")]
892         /// ```
893
894         #[stable(feature = "rust1", since = "1.0.0")]
895         #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
896         #[must_use = "this returns the result of the operation, \
897                       without modifying the original"]
898         #[inline(always)]
899         pub const fn saturating_add(self, rhs: Self) -> Self {
900             intrinsics::saturating_add(self, rhs)
901         }
902
903         /// Saturating addition with an unsigned integer. Computes `self + rhs`,
904         /// saturating at the numeric bounds instead of overflowing.
905         ///
906         /// # Examples
907         ///
908         /// Basic usage:
909         ///
910         /// ```
911         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".saturating_add_unsigned(2), 3);")]
912         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_add_unsigned(100), ", stringify!($SelfT), "::MAX);")]
913         /// ```
914         #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
915         #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
916         #[must_use = "this returns the result of the operation, \
917                       without modifying the original"]
918         #[inline]
919         pub const fn saturating_add_unsigned(self, rhs: $UnsignedT) -> Self {
920             // Overflow can only happen at the upper bound
921             // We cannot use `unwrap_or` here because it is not `const`
922             match self.checked_add_unsigned(rhs) {
923                 Some(x) => x,
924                 None => Self::MAX,
925             }
926         }
927
928         /// Saturating integer subtraction. Computes `self - rhs`, saturating at the
929         /// numeric bounds instead of overflowing.
930         ///
931         /// # Examples
932         ///
933         /// Basic usage:
934         ///
935         /// ```
936         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub(127), -27);")]
937         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub(100), ", stringify!($SelfT), "::MIN);")]
938         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_sub(-1), ", stringify!($SelfT), "::MAX);")]
939         /// ```
940         #[stable(feature = "rust1", since = "1.0.0")]
941         #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
942         #[must_use = "this returns the result of the operation, \
943                       without modifying the original"]
944         #[inline(always)]
945         pub const fn saturating_sub(self, rhs: Self) -> Self {
946             intrinsics::saturating_sub(self, rhs)
947         }
948
949         /// Saturating subtraction with an unsigned integer. Computes `self - rhs`,
950         /// saturating at the numeric bounds instead of overflowing.
951         ///
952         /// # Examples
953         ///
954         /// Basic usage:
955         ///
956         /// ```
957         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_sub_unsigned(127), -27);")]
958         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub_unsigned(100), ", stringify!($SelfT), "::MIN);")]
959         /// ```
960         #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
961         #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
962         #[must_use = "this returns the result of the operation, \
963                       without modifying the original"]
964         #[inline]
965         pub const fn saturating_sub_unsigned(self, rhs: $UnsignedT) -> Self {
966             // Overflow can only happen at the lower bound
967             // We cannot use `unwrap_or` here because it is not `const`
968             match self.checked_sub_unsigned(rhs) {
969                 Some(x) => x,
970                 None => Self::MIN,
971             }
972         }
973
974         /// Saturating integer negation. Computes `-self`, returning `MAX` if `self == MIN`
975         /// instead of overflowing.
976         ///
977         /// # Examples
978         ///
979         /// Basic usage:
980         ///
981         /// ```
982         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_neg(), -100);")]
983         #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_neg(), 100);")]
984         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_neg(), ", stringify!($SelfT), "::MAX);")]
985         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_neg(), ", stringify!($SelfT), "::MIN + 1);")]
986         /// ```
987
988         #[stable(feature = "saturating_neg", since = "1.45.0")]
989         #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
990         #[must_use = "this returns the result of the operation, \
991                       without modifying the original"]
992         #[inline(always)]
993         pub const fn saturating_neg(self) -> Self {
994             intrinsics::saturating_sub(0, self)
995         }
996
997         /// Saturating absolute value. Computes `self.abs()`, returning `MAX` if `self ==
998         /// MIN` instead of overflowing.
999         ///
1000         /// # Examples
1001         ///
1002         /// Basic usage:
1003         ///
1004         /// ```
1005         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".saturating_abs(), 100);")]
1006         #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").saturating_abs(), 100);")]
1007         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_abs(), ", stringify!($SelfT), "::MAX);")]
1008         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).saturating_abs(), ", stringify!($SelfT), "::MAX);")]
1009         /// ```
1010
1011         #[stable(feature = "saturating_neg", since = "1.45.0")]
1012         #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1013         #[must_use = "this returns the result of the operation, \
1014                       without modifying the original"]
1015         #[inline]
1016         pub const fn saturating_abs(self) -> Self {
1017             if self.is_negative() {
1018                 self.saturating_neg()
1019             } else {
1020                 self
1021             }
1022         }
1023
1024         /// Saturating integer multiplication. Computes `self * rhs`, saturating at the
1025         /// numeric bounds instead of overflowing.
1026         ///
1027         /// # Examples
1028         ///
1029         /// Basic usage:
1030         ///
1031         /// ```
1032         #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".saturating_mul(12), 120);")]
1033         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_mul(10), ", stringify!($SelfT), "::MAX);")]
1034         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_mul(10), ", stringify!($SelfT), "::MIN);")]
1035         /// ```
1036         #[stable(feature = "wrapping", since = "1.7.0")]
1037         #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")]
1038         #[must_use = "this returns the result of the operation, \
1039                       without modifying the original"]
1040         #[inline]
1041         pub const fn saturating_mul(self, rhs: Self) -> Self {
1042             match self.checked_mul(rhs) {
1043                 Some(x) => x,
1044                 None => if (self < 0) == (rhs < 0) {
1045                     Self::MAX
1046                 } else {
1047                     Self::MIN
1048                 }
1049             }
1050         }
1051
1052         /// Saturating integer division. Computes `self / rhs`, saturating at the
1053         /// numeric bounds instead of overflowing.
1054         ///
1055         /// # Examples
1056         ///
1057         /// Basic usage:
1058         ///
1059         /// ```
1060         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
1061         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_div(-1), ", stringify!($SelfT), "::MIN + 1);")]
1062         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_div(-1), ", stringify!($SelfT), "::MAX);")]
1063         ///
1064         /// ```
1065         ///
1066         /// ```should_panic
1067         #[doc = concat!("let _ = 1", stringify!($SelfT), ".saturating_div(0);")]
1068         ///
1069         /// ```
1070         #[stable(feature = "saturating_div", since = "1.58.0")]
1071         #[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
1072         #[must_use = "this returns the result of the operation, \
1073                       without modifying the original"]
1074         #[inline]
1075         pub const fn saturating_div(self, rhs: Self) -> Self {
1076             match self.overflowing_div(rhs) {
1077                 (result, false) => result,
1078                 (_result, true) => Self::MAX, // MIN / -1 is the only possible saturating overflow
1079             }
1080         }
1081
1082         /// Saturating integer exponentiation. Computes `self.pow(exp)`,
1083         /// saturating at the numeric bounds instead of overflowing.
1084         ///
1085         /// # Examples
1086         ///
1087         /// Basic usage:
1088         ///
1089         /// ```
1090         #[doc = concat!("assert_eq!((-4", stringify!($SelfT), ").saturating_pow(3), -64);")]
1091         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(2), ", stringify!($SelfT), "::MAX);")]
1092         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(3), ", stringify!($SelfT), "::MIN);")]
1093         /// ```
1094         #[stable(feature = "no_panic_pow", since = "1.34.0")]
1095         #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1096         #[must_use = "this returns the result of the operation, \
1097                       without modifying the original"]
1098         #[inline]
1099         pub const fn saturating_pow(self, exp: u32) -> Self {
1100             match self.checked_pow(exp) {
1101                 Some(x) => x,
1102                 None if self < 0 && exp % 2 == 1 => Self::MIN,
1103                 None => Self::MAX,
1104             }
1105         }
1106
1107         /// Wrapping (modular) addition. Computes `self + rhs`, wrapping around at the
1108         /// boundary of the type.
1109         ///
1110         /// # Examples
1111         ///
1112         /// Basic usage:
1113         ///
1114         /// ```
1115         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add(27), 127);")]
1116         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add(2), ", stringify!($SelfT), "::MIN + 1);")]
1117         /// ```
1118         #[stable(feature = "rust1", since = "1.0.0")]
1119         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1120         #[must_use = "this returns the result of the operation, \
1121                       without modifying the original"]
1122         #[inline(always)]
1123         pub const fn wrapping_add(self, rhs: Self) -> Self {
1124             intrinsics::wrapping_add(self, rhs)
1125         }
1126
1127         /// Wrapping (modular) addition with an unsigned integer. Computes
1128         /// `self + rhs`, wrapping around at the boundary of the type.
1129         ///
1130         /// # Examples
1131         ///
1132         /// Basic usage:
1133         ///
1134         /// ```
1135         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_add_unsigned(27), 127);")]
1136         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add_unsigned(2), ", stringify!($SelfT), "::MIN + 1);")]
1137         /// ```
1138         #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1139         #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1140         #[must_use = "this returns the result of the operation, \
1141                       without modifying the original"]
1142         #[inline(always)]
1143         pub const fn wrapping_add_unsigned(self, rhs: $UnsignedT) -> Self {
1144             self.wrapping_add(rhs as Self)
1145         }
1146
1147         /// Wrapping (modular) subtraction. Computes `self - rhs`, wrapping around at the
1148         /// boundary of the type.
1149         ///
1150         /// # Examples
1151         ///
1152         /// Basic usage:
1153         ///
1154         /// ```
1155         #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub(127), -127);")]
1156         #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub(", stringify!($SelfT), "::MAX), ", stringify!($SelfT), "::MAX);")]
1157         /// ```
1158         #[stable(feature = "rust1", since = "1.0.0")]
1159         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1160         #[must_use = "this returns the result of the operation, \
1161                       without modifying the original"]
1162         #[inline(always)]
1163         pub const fn wrapping_sub(self, rhs: Self) -> Self {
1164             intrinsics::wrapping_sub(self, rhs)
1165         }
1166
1167         /// Wrapping (modular) subtraction with an unsigned integer. Computes
1168         /// `self - rhs`, wrapping around at the boundary of the type.
1169         ///
1170         /// # Examples
1171         ///
1172         /// Basic usage:
1173         ///
1174         /// ```
1175         #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".wrapping_sub_unsigned(127), -127);")]
1176         #[doc = concat!("assert_eq!((-2", stringify!($SelfT), ").wrapping_sub_unsigned(", stringify!($UnsignedT), "::MAX), -1);")]
1177         /// ```
1178         #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1179         #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1180         #[must_use = "this returns the result of the operation, \
1181                       without modifying the original"]
1182         #[inline(always)]
1183         pub const fn wrapping_sub_unsigned(self, rhs: $UnsignedT) -> Self {
1184             self.wrapping_sub(rhs as Self)
1185         }
1186
1187         /// Wrapping (modular) multiplication. Computes `self * rhs`, wrapping around at
1188         /// the boundary of the type.
1189         ///
1190         /// # Examples
1191         ///
1192         /// Basic usage:
1193         ///
1194         /// ```
1195         #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".wrapping_mul(12), 120);")]
1196         /// assert_eq!(11i8.wrapping_mul(12), -124);
1197         /// ```
1198         #[stable(feature = "rust1", since = "1.0.0")]
1199         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1200         #[must_use = "this returns the result of the operation, \
1201                       without modifying the original"]
1202         #[inline(always)]
1203         pub const fn wrapping_mul(self, rhs: Self) -> Self {
1204             intrinsics::wrapping_mul(self, rhs)
1205         }
1206
1207         /// Wrapping (modular) division. Computes `self / rhs`, wrapping around at the
1208         /// boundary of the type.
1209         ///
1210         /// The only case where such wrapping can occur is when one divides `MIN / -1` on a signed type (where
1211         /// `MIN` is the negative minimal value for the type); this is equivalent to `-MIN`, a positive value
1212         /// that is too large to represent in the type. In such a case, this function returns `MIN` itself.
1213         ///
1214         /// # Panics
1215         ///
1216         /// This function will panic if `rhs` is 0.
1217         ///
1218         /// # Examples
1219         ///
1220         /// Basic usage:
1221         ///
1222         /// ```
1223         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div(10), 10);")]
1224         /// assert_eq!((-128i8).wrapping_div(-1), -128);
1225         /// ```
1226         #[stable(feature = "num_wrapping", since = "1.2.0")]
1227         #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
1228         #[must_use = "this returns the result of the operation, \
1229                       without modifying the original"]
1230         #[inline]
1231         pub const fn wrapping_div(self, rhs: Self) -> Self {
1232             self.overflowing_div(rhs).0
1233         }
1234
1235         /// Wrapping Euclidean division. Computes `self.div_euclid(rhs)`,
1236         /// wrapping around at the boundary of the type.
1237         ///
1238         /// Wrapping will only occur in `MIN / -1` on a signed type (where `MIN` is the negative minimal value
1239         /// for the type). This is equivalent to `-MIN`, a positive value that is too large to represent in the
1240         /// type. In this case, this method returns `MIN` itself.
1241         ///
1242         /// # Panics
1243         ///
1244         /// This function will panic if `rhs` is 0.
1245         ///
1246         /// # Examples
1247         ///
1248         /// Basic usage:
1249         ///
1250         /// ```
1251         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_div_euclid(10), 10);")]
1252         /// assert_eq!((-128i8).wrapping_div_euclid(-1), -128);
1253         /// ```
1254         #[stable(feature = "euclidean_division", since = "1.38.0")]
1255         #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1256         #[must_use = "this returns the result of the operation, \
1257                       without modifying the original"]
1258         #[inline]
1259         pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
1260             self.overflowing_div_euclid(rhs).0
1261         }
1262
1263         /// Wrapping (modular) remainder. Computes `self % rhs`, wrapping around at the
1264         /// boundary of the type.
1265         ///
1266         /// Such wrap-around never actually occurs mathematically; implementation artifacts make `x % y`
1267         /// invalid for `MIN / -1` on a signed type (where `MIN` is the negative minimal value). In such a case,
1268         /// this function returns `0`.
1269         ///
1270         /// # Panics
1271         ///
1272         /// This function will panic if `rhs` is 0.
1273         ///
1274         /// # Examples
1275         ///
1276         /// Basic usage:
1277         ///
1278         /// ```
1279         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem(10), 0);")]
1280         /// assert_eq!((-128i8).wrapping_rem(-1), 0);
1281         /// ```
1282         #[stable(feature = "num_wrapping", since = "1.2.0")]
1283         #[rustc_const_stable(feature = "const_wrapping_int_methods", since = "1.52.0")]
1284         #[must_use = "this returns the result of the operation, \
1285                       without modifying the original"]
1286         #[inline]
1287         pub const fn wrapping_rem(self, rhs: Self) -> Self {
1288             self.overflowing_rem(rhs).0
1289         }
1290
1291         /// Wrapping Euclidean remainder. Computes `self.rem_euclid(rhs)`, wrapping around
1292         /// at the boundary of the type.
1293         ///
1294         /// Wrapping will only occur in `MIN % -1` on a signed type (where `MIN` is the negative minimal value
1295         /// for the type). In this case, this method returns 0.
1296         ///
1297         /// # Panics
1298         ///
1299         /// This function will panic if `rhs` is 0.
1300         ///
1301         /// # Examples
1302         ///
1303         /// Basic usage:
1304         ///
1305         /// ```
1306         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);")]
1307         /// assert_eq!((-128i8).wrapping_rem_euclid(-1), 0);
1308         /// ```
1309         #[stable(feature = "euclidean_division", since = "1.38.0")]
1310         #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1311         #[must_use = "this returns the result of the operation, \
1312                       without modifying the original"]
1313         #[inline]
1314         pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
1315             self.overflowing_rem_euclid(rhs).0
1316         }
1317
1318         /// Wrapping (modular) negation. Computes `-self`, wrapping around at the boundary
1319         /// of the type.
1320         ///
1321         /// The only case where such wrapping can occur is when one negates `MIN` on a signed type (where `MIN`
1322         /// is the negative minimal value for the type); this is a positive value that is too large to represent
1323         /// in the type. In such a case, this function returns `MIN` itself.
1324         ///
1325         /// # Examples
1326         ///
1327         /// Basic usage:
1328         ///
1329         /// ```
1330         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_neg(), -100);")]
1331         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_neg(), ", stringify!($SelfT), "::MIN);")]
1332         /// ```
1333         #[stable(feature = "num_wrapping", since = "1.2.0")]
1334         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1335         #[must_use = "this returns the result of the operation, \
1336                       without modifying the original"]
1337         #[inline(always)]
1338         pub const fn wrapping_neg(self) -> Self {
1339             (0 as $SelfT).wrapping_sub(self)
1340         }
1341
1342         /// Panic-free bitwise shift-left; yields `self << mask(rhs)`, where `mask` removes
1343         /// any high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type.
1344         ///
1345         /// Note that this is *not* the same as a rotate-left; the RHS of a wrapping shift-left is restricted to
1346         /// the range of the type, rather than the bits shifted out of the LHS being returned to the other end.
1347         /// The primitive integer types all implement a [`rotate_left`](Self::rotate_left) function,
1348         /// which may be what you want instead.
1349         ///
1350         /// # Examples
1351         ///
1352         /// Basic usage:
1353         ///
1354         /// ```
1355         #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(7), -128);")]
1356         #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(128), -1);")]
1357         /// ```
1358         #[stable(feature = "num_wrapping", since = "1.2.0")]
1359         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1360         #[must_use = "this returns the result of the operation, \
1361                       without modifying the original"]
1362         #[inline(always)]
1363         #[rustc_allow_const_fn_unstable(const_inherent_unchecked_arith)]
1364         pub const fn wrapping_shl(self, rhs: u32) -> Self {
1365             // SAFETY: the masking by the bitsize of the type ensures that we do not shift
1366             // out of bounds
1367             unsafe {
1368                 self.unchecked_shl(rhs & ($BITS - 1))
1369             }
1370         }
1371
1372         /// Panic-free bitwise shift-right; yields `self >> mask(rhs)`, where `mask`
1373         /// removes any high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type.
1374         ///
1375         /// Note that this is *not* the same as a rotate-right; the RHS of a wrapping shift-right is restricted
1376         /// to the range of the type, rather than the bits shifted out of the LHS being returned to the other
1377         /// end. The primitive integer types all implement a [`rotate_right`](Self::rotate_right) function,
1378         /// which may be what you want instead.
1379         ///
1380         /// # Examples
1381         ///
1382         /// Basic usage:
1383         ///
1384         /// ```
1385         #[doc = concat!("assert_eq!((-128", stringify!($SelfT), ").wrapping_shr(7), -1);")]
1386         /// assert_eq!((-128i16).wrapping_shr(64), -128);
1387         /// ```
1388         #[stable(feature = "num_wrapping", since = "1.2.0")]
1389         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1390         #[must_use = "this returns the result of the operation, \
1391                       without modifying the original"]
1392         #[inline(always)]
1393         #[rustc_allow_const_fn_unstable(const_inherent_unchecked_arith)]
1394         pub const fn wrapping_shr(self, rhs: u32) -> Self {
1395             // SAFETY: the masking by the bitsize of the type ensures that we do not shift
1396             // out of bounds
1397             unsafe {
1398                 self.unchecked_shr(rhs & ($BITS - 1))
1399             }
1400         }
1401
1402         /// Wrapping (modular) absolute value. Computes `self.abs()`, wrapping around at
1403         /// the boundary of the type.
1404         ///
1405         /// The only case where such wrapping can occur is when one takes the absolute value of the negative
1406         /// minimal value for the type; this is a positive value that is too large to represent in the type. In
1407         /// such a case, this function returns `MIN` itself.
1408         ///
1409         /// # Examples
1410         ///
1411         /// Basic usage:
1412         ///
1413         /// ```
1414         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".wrapping_abs(), 100);")]
1415         #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").wrapping_abs(), 100);")]
1416         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.wrapping_abs(), ", stringify!($SelfT), "::MIN);")]
1417         /// assert_eq!((-128i8).wrapping_abs() as u8, 128);
1418         /// ```
1419         #[stable(feature = "no_panic_abs", since = "1.13.0")]
1420         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1421         #[must_use = "this returns the result of the operation, \
1422                       without modifying the original"]
1423         #[allow(unused_attributes)]
1424         #[inline]
1425         pub const fn wrapping_abs(self) -> Self {
1426              if self.is_negative() {
1427                  self.wrapping_neg()
1428              } else {
1429                  self
1430              }
1431         }
1432
1433         /// Computes the absolute value of `self` without any wrapping
1434         /// or panicking.
1435         ///
1436         ///
1437         /// # Examples
1438         ///
1439         /// Basic usage:
1440         ///
1441         /// ```
1442         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".unsigned_abs(), 100", stringify!($UnsignedT), ");")]
1443         #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").unsigned_abs(), 100", stringify!($UnsignedT), ");")]
1444         /// assert_eq!((-128i8).unsigned_abs(), 128u8);
1445         /// ```
1446         #[stable(feature = "unsigned_abs", since = "1.51.0")]
1447         #[rustc_const_stable(feature = "unsigned_abs", since = "1.51.0")]
1448         #[must_use = "this returns the result of the operation, \
1449                       without modifying the original"]
1450         #[inline]
1451         pub const fn unsigned_abs(self) -> $UnsignedT {
1452              self.wrapping_abs() as $UnsignedT
1453         }
1454
1455         /// Wrapping (modular) exponentiation. Computes `self.pow(exp)`,
1456         /// wrapping around at the boundary of the type.
1457         ///
1458         /// # Examples
1459         ///
1460         /// Basic usage:
1461         ///
1462         /// ```
1463         #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".wrapping_pow(4), 81);")]
1464         /// assert_eq!(3i8.wrapping_pow(5), -13);
1465         /// assert_eq!(3i8.wrapping_pow(6), -39);
1466         /// ```
1467         #[stable(feature = "no_panic_pow", since = "1.34.0")]
1468         #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1469         #[must_use = "this returns the result of the operation, \
1470                       without modifying the original"]
1471         #[inline]
1472         pub const fn wrapping_pow(self, mut exp: u32) -> Self {
1473             if exp == 0 {
1474                 return 1;
1475             }
1476             let mut base = self;
1477             let mut acc: Self = 1;
1478
1479             while exp > 1 {
1480                 if (exp & 1) == 1 {
1481                     acc = acc.wrapping_mul(base);
1482                 }
1483                 exp /= 2;
1484                 base = base.wrapping_mul(base);
1485             }
1486
1487             // since exp!=0, finally the exp must be 1.
1488             // Deal with the final bit of the exponent separately, since
1489             // squaring the base afterwards is not necessary and may cause a
1490             // needless overflow.
1491             acc.wrapping_mul(base)
1492         }
1493
1494         /// Calculates `self` + `rhs`
1495         ///
1496         /// Returns a tuple of the addition along with a boolean indicating whether an arithmetic overflow would
1497         /// occur. If an overflow would have occurred then the wrapped value is returned.
1498         ///
1499         /// # Examples
1500         ///
1501         /// Basic usage:
1502         ///
1503         /// ```
1504         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_add(2), (7, false));")]
1505         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.overflowing_add(1), (", stringify!($SelfT), "::MIN, true));")]
1506         /// ```
1507         #[stable(feature = "wrapping", since = "1.7.0")]
1508         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1509         #[must_use = "this returns the result of the operation, \
1510                       without modifying the original"]
1511         #[inline(always)]
1512         pub const fn overflowing_add(self, rhs: Self) -> (Self, bool) {
1513             let (a, b) = intrinsics::add_with_overflow(self as $ActualT, rhs as $ActualT);
1514             (a as Self, b)
1515         }
1516
1517         /// Calculates `self + rhs + carry` without the ability to overflow.
1518         ///
1519         /// Performs "signed ternary addition" which takes in an extra bit to add, and may return an
1520         /// additional bit of overflow. This signed function is used only on the highest-ordered data,
1521         /// for which the signed overflow result indicates whether the big integer overflowed or not.
1522         ///
1523         /// # Examples
1524         ///
1525         /// Basic usage:
1526         ///
1527         /// ```
1528         /// #![feature(bigint_helper_methods)]
1529         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".carrying_add(2, false), (7, false));")]
1530         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".carrying_add(2, true), (8, false));")]
1531         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.carrying_add(1, false), (", stringify!($SelfT), "::MIN, true));")]
1532         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.carrying_add(0, true), (", stringify!($SelfT), "::MIN, true));")]
1533         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.carrying_add(1, true), (", stringify!($SelfT), "::MIN + 1, true));")]
1534         #[doc = concat!("assert_eq!(",
1535             stringify!($SelfT), "::MAX.carrying_add(", stringify!($SelfT), "::MAX, true), ",
1536             "(-1, true));"
1537         )]
1538         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.carrying_add(-1, true), (", stringify!($SelfT), "::MIN, false));")]
1539         #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".carrying_add(", stringify!($SelfT), "::MAX, true), (", stringify!($SelfT), "::MIN, true));")]
1540         /// ```
1541         ///
1542         /// If `carry` is false, this method is equivalent to [`overflowing_add`](Self::overflowing_add):
1543         ///
1544         /// ```
1545         /// #![feature(bigint_helper_methods)]
1546         #[doc = concat!("assert_eq!(5_", stringify!($SelfT), ".carrying_add(2, false), 5_", stringify!($SelfT), ".overflowing_add(2));")]
1547         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.carrying_add(1, false), ", stringify!($SelfT), "::MAX.overflowing_add(1));")]
1548         /// ```
1549         #[unstable(feature = "bigint_helper_methods", issue = "85532")]
1550         #[rustc_const_unstable(feature = "const_bigint_helper_methods", issue = "85532")]
1551         #[must_use = "this returns the result of the operation, \
1552                       without modifying the original"]
1553         #[inline]
1554         pub const fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool) {
1555             // note: longer-term this should be done via an intrinsic.
1556             // note: no intermediate overflow is required (https://github.com/rust-lang/rust/issues/85532#issuecomment-1032214946).
1557             let (a, b) = self.overflowing_add(rhs);
1558             let (c, d) = a.overflowing_add(carry as $SelfT);
1559             (c, b != d)
1560         }
1561
1562         /// Calculates `self` + `rhs` with an unsigned `rhs`
1563         ///
1564         /// Returns a tuple of the addition along with a boolean indicating
1565         /// whether an arithmetic overflow would occur. If an overflow would
1566         /// have occurred then the wrapped value is returned.
1567         ///
1568         /// # Examples
1569         ///
1570         /// Basic usage:
1571         ///
1572         /// ```
1573         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_add_unsigned(2), (3, false));")]
1574         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_add_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MAX, false));")]
1575         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).overflowing_add_unsigned(3), (", stringify!($SelfT), "::MIN, true));")]
1576         /// ```
1577         #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1578         #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1579         #[must_use = "this returns the result of the operation, \
1580                       without modifying the original"]
1581         #[inline]
1582         pub const fn overflowing_add_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
1583             let rhs = rhs as Self;
1584             let (res, overflowed) = self.overflowing_add(rhs);
1585             (res, overflowed ^ (rhs < 0))
1586         }
1587
1588         /// Calculates `self` - `rhs`
1589         ///
1590         /// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow
1591         /// would occur. If an overflow would have occurred then the wrapped value is returned.
1592         ///
1593         /// # Examples
1594         ///
1595         /// Basic usage:
1596         ///
1597         /// ```
1598         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_sub(2), (3, false));")]
1599         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_sub(1), (", stringify!($SelfT), "::MAX, true));")]
1600         /// ```
1601         #[stable(feature = "wrapping", since = "1.7.0")]
1602         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1603         #[must_use = "this returns the result of the operation, \
1604                       without modifying the original"]
1605         #[inline(always)]
1606         pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
1607             let (a, b) = intrinsics::sub_with_overflow(self as $ActualT, rhs as $ActualT);
1608             (a as Self, b)
1609         }
1610
1611         /// Calculates `self - rhs - borrow` without the ability to overflow.
1612         ///
1613         /// Performs "signed ternary subtraction" which takes in an extra bit to subtract, and may return an
1614         /// additional bit of overflow. This signed function is used only on the highest-ordered data,
1615         /// for which the signed overflow result indicates whether the big integer overflowed or not.
1616         ///
1617         /// # Examples
1618         ///
1619         /// Basic usage:
1620         ///
1621         /// ```
1622         /// #![feature(bigint_helper_methods)]
1623         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".borrowing_sub(2, false), (3, false));")]
1624         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".borrowing_sub(2, true), (2, false));")]
1625         #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".borrowing_sub(1, false), (-1, false));")]
1626         #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".borrowing_sub(1, true), (-2, false));")]
1627         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.borrowing_sub(1, true), (", stringify!($SelfT), "::MAX - 1, true));")]
1628         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.borrowing_sub(-1, false), (", stringify!($SelfT), "::MIN, true));")]
1629         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.borrowing_sub(-1, true), (", stringify!($SelfT), "::MAX, false));")]
1630         /// ```
1631         #[unstable(feature = "bigint_helper_methods", issue = "85532")]
1632         #[rustc_const_unstable(feature = "const_bigint_helper_methods", issue = "85532")]
1633         #[must_use = "this returns the result of the operation, \
1634                       without modifying the original"]
1635         #[inline]
1636         pub const fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
1637             // note: longer-term this should be done via an intrinsic.
1638             // note: no intermediate overflow is required (https://github.com/rust-lang/rust/issues/85532#issuecomment-1032214946).
1639             let (a, b) = self.overflowing_sub(rhs);
1640             let (c, d) = a.overflowing_sub(borrow as $SelfT);
1641             (c, b != d)
1642         }
1643
1644         /// Calculates `self` - `rhs` with an unsigned `rhs`
1645         ///
1646         /// Returns a tuple of the subtraction along with a boolean indicating
1647         /// whether an arithmetic overflow would occur. If an overflow would
1648         /// have occurred then the wrapped value is returned.
1649         ///
1650         /// # Examples
1651         ///
1652         /// Basic usage:
1653         ///
1654         /// ```
1655         #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".overflowing_sub_unsigned(2), (-1, false));")]
1656         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX).overflowing_sub_unsigned(", stringify!($UnsignedT), "::MAX), (", stringify!($SelfT), "::MIN, false));")]
1657         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).overflowing_sub_unsigned(3), (", stringify!($SelfT), "::MAX, true));")]
1658         /// ```
1659         #[stable(feature = "mixed_integer_ops", since = "1.66.0")]
1660         #[rustc_const_stable(feature = "mixed_integer_ops", since = "1.66.0")]
1661         #[must_use = "this returns the result of the operation, \
1662                       without modifying the original"]
1663         #[inline]
1664         pub const fn overflowing_sub_unsigned(self, rhs: $UnsignedT) -> (Self, bool) {
1665             let rhs = rhs as Self;
1666             let (res, overflowed) = self.overflowing_sub(rhs);
1667             (res, overflowed ^ (rhs < 0))
1668         }
1669
1670         /// Calculates the multiplication of `self` and `rhs`.
1671         ///
1672         /// Returns a tuple of the multiplication along with a boolean indicating whether an arithmetic overflow
1673         /// would occur. If an overflow would have occurred then the wrapped value is returned.
1674         ///
1675         /// # Examples
1676         ///
1677         /// Basic usage:
1678         ///
1679         /// ```
1680         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_mul(2), (10, false));")]
1681         /// assert_eq!(1_000_000_000i32.overflowing_mul(10), (1410065408, true));
1682         /// ```
1683         #[stable(feature = "wrapping", since = "1.7.0")]
1684         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1685         #[must_use = "this returns the result of the operation, \
1686                       without modifying the original"]
1687         #[inline(always)]
1688         pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
1689             let (a, b) = intrinsics::mul_with_overflow(self as $ActualT, rhs as $ActualT);
1690             (a as Self, b)
1691         }
1692
1693         /// Calculates the divisor when `self` is divided by `rhs`.
1694         ///
1695         /// Returns a tuple of the divisor along with a boolean indicating whether an arithmetic overflow would
1696         /// occur. If an overflow would occur then self is returned.
1697         ///
1698         /// # Panics
1699         ///
1700         /// This function will panic if `rhs` is 0.
1701         ///
1702         /// # Examples
1703         ///
1704         /// Basic usage:
1705         ///
1706         /// ```
1707         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div(2), (2, false));")]
1708         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div(-1), (", stringify!($SelfT), "::MIN, true));")]
1709         /// ```
1710         #[inline]
1711         #[stable(feature = "wrapping", since = "1.7.0")]
1712         #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
1713         #[must_use = "this returns the result of the operation, \
1714                       without modifying the original"]
1715         pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
1716             // Using `&` helps LLVM see that it is the same check made in division.
1717             if unlikely!((self == Self::MIN) & (rhs == -1)) {
1718                 (self, true)
1719             } else {
1720                 (self / rhs, false)
1721             }
1722         }
1723
1724         /// Calculates the quotient of Euclidean division `self.div_euclid(rhs)`.
1725         ///
1726         /// Returns a tuple of the divisor along with a boolean indicating whether an arithmetic overflow would
1727         /// occur. If an overflow would occur then `self` is returned.
1728         ///
1729         /// # Panics
1730         ///
1731         /// This function will panic if `rhs` is 0.
1732         ///
1733         /// # Examples
1734         ///
1735         /// Basic usage:
1736         ///
1737         /// ```
1738         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_div_euclid(2), (2, false));")]
1739         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringify!($SelfT), "::MIN, true));")]
1740         /// ```
1741         #[inline]
1742         #[stable(feature = "euclidean_division", since = "1.38.0")]
1743         #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1744         #[must_use = "this returns the result of the operation, \
1745                       without modifying the original"]
1746         pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
1747             // Using `&` helps LLVM see that it is the same check made in division.
1748             if unlikely!((self == Self::MIN) & (rhs == -1)) {
1749                 (self, true)
1750             } else {
1751                 (self.div_euclid(rhs), false)
1752             }
1753         }
1754
1755         /// Calculates the remainder when `self` is divided by `rhs`.
1756         ///
1757         /// Returns a tuple of the remainder after dividing along with a boolean indicating whether an
1758         /// arithmetic overflow would occur. If an overflow would occur then 0 is returned.
1759         ///
1760         /// # Panics
1761         ///
1762         /// This function will panic if `rhs` is 0.
1763         ///
1764         /// # Examples
1765         ///
1766         /// Basic usage:
1767         ///
1768         /// ```
1769         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem(2), (1, false));")]
1770         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem(-1), (0, true));")]
1771         /// ```
1772         #[inline]
1773         #[stable(feature = "wrapping", since = "1.7.0")]
1774         #[rustc_const_stable(feature = "const_overflowing_int_methods", since = "1.52.0")]
1775         #[must_use = "this returns the result of the operation, \
1776                       without modifying the original"]
1777         pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
1778             if unlikely!(rhs == -1) {
1779                 (0, self == Self::MIN)
1780             } else {
1781                 (self % rhs, false)
1782             }
1783         }
1784
1785
1786         /// Overflowing Euclidean remainder. Calculates `self.rem_euclid(rhs)`.
1787         ///
1788         /// Returns a tuple of the remainder after dividing along with a boolean indicating whether an
1789         /// arithmetic overflow would occur. If an overflow would occur then 0 is returned.
1790         ///
1791         /// # Panics
1792         ///
1793         /// This function will panic if `rhs` is 0.
1794         ///
1795         /// # Examples
1796         ///
1797         /// Basic usage:
1798         ///
1799         /// ```
1800         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".overflowing_rem_euclid(2), (1, false));")]
1801         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));")]
1802         /// ```
1803         #[stable(feature = "euclidean_division", since = "1.38.0")]
1804         #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
1805         #[must_use = "this returns the result of the operation, \
1806                       without modifying the original"]
1807         #[inline]
1808         pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
1809             if unlikely!(rhs == -1) {
1810                 (0, self == Self::MIN)
1811             } else {
1812                 (self.rem_euclid(rhs), false)
1813             }
1814         }
1815
1816
1817         /// Negates self, overflowing if this is equal to the minimum value.
1818         ///
1819         /// Returns a tuple of the negated version of self along with a boolean indicating whether an overflow
1820         /// happened. If `self` is the minimum value (e.g., `i32::MIN` for values of type `i32`), then the
1821         /// minimum value will be returned again and `true` will be returned for an overflow happening.
1822         ///
1823         /// # Examples
1824         ///
1825         /// Basic usage:
1826         ///
1827         /// ```
1828         #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2, false));")]
1829         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($SelfT), "::MIN, true));")]
1830         /// ```
1831         #[inline]
1832         #[stable(feature = "wrapping", since = "1.7.0")]
1833         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1834         #[must_use = "this returns the result of the operation, \
1835                       without modifying the original"]
1836         #[allow(unused_attributes)]
1837         pub const fn overflowing_neg(self) -> (Self, bool) {
1838             if unlikely!(self == Self::MIN) {
1839                 (Self::MIN, true)
1840             } else {
1841                 (-self, false)
1842             }
1843         }
1844
1845         /// Shifts self left by `rhs` bits.
1846         ///
1847         /// Returns a tuple of the shifted version of self along with a boolean indicating whether the shift
1848         /// value was larger than or equal to the number of bits. If the shift value is too large, then value is
1849         /// masked (N-1) where N is the number of bits, and this value is then used to perform the shift.
1850         ///
1851         /// # Examples
1852         ///
1853         /// Basic usage:
1854         ///
1855         /// ```
1856         #[doc = concat!("assert_eq!(0x1", stringify!($SelfT),".overflowing_shl(4), (0x10, false));")]
1857         /// assert_eq!(0x1i32.overflowing_shl(36), (0x10, true));
1858         /// ```
1859         #[stable(feature = "wrapping", since = "1.7.0")]
1860         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1861         #[must_use = "this returns the result of the operation, \
1862                       without modifying the original"]
1863         #[inline]
1864         pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
1865             (self.wrapping_shl(rhs), (rhs > ($BITS - 1)))
1866         }
1867
1868         /// Shifts self right by `rhs` bits.
1869         ///
1870         /// Returns a tuple of the shifted version of self along with a boolean indicating whether the shift
1871         /// value was larger than or equal to the number of bits. If the shift value is too large, then value is
1872         /// masked (N-1) where N is the number of bits, and this value is then used to perform the shift.
1873         ///
1874         /// # Examples
1875         ///
1876         /// Basic usage:
1877         ///
1878         /// ```
1879         #[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".overflowing_shr(4), (0x1, false));")]
1880         /// assert_eq!(0x10i32.overflowing_shr(36), (0x1, true));
1881         /// ```
1882         #[stable(feature = "wrapping", since = "1.7.0")]
1883         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1884         #[must_use = "this returns the result of the operation, \
1885                       without modifying the original"]
1886         #[inline]
1887         pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
1888             (self.wrapping_shr(rhs), (rhs > ($BITS - 1)))
1889         }
1890
1891         /// Computes the absolute value of `self`.
1892         ///
1893         /// Returns a tuple of the absolute version of self along with a boolean indicating whether an overflow
1894         /// happened. If self is the minimum value
1895         #[doc = concat!("(e.g., ", stringify!($SelfT), "::MIN for values of type ", stringify!($SelfT), "),")]
1896         /// then the minimum value will be returned again and true will be returned
1897         /// for an overflow happening.
1898         ///
1899         /// # Examples
1900         ///
1901         /// Basic usage:
1902         ///
1903         /// ```
1904         #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".overflowing_abs(), (10, false));")]
1905         #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").overflowing_abs(), (10, false));")]
1906         #[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN).overflowing_abs(), (", stringify!($SelfT), "::MIN, true));")]
1907         /// ```
1908         #[stable(feature = "no_panic_abs", since = "1.13.0")]
1909         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
1910         #[must_use = "this returns the result of the operation, \
1911                       without modifying the original"]
1912         #[inline]
1913         pub const fn overflowing_abs(self) -> (Self, bool) {
1914             (self.wrapping_abs(), self == Self::MIN)
1915         }
1916
1917         /// Raises self to the power of `exp`, using exponentiation by squaring.
1918         ///
1919         /// Returns a tuple of the exponentiation along with a bool indicating
1920         /// whether an overflow happened.
1921         ///
1922         /// # Examples
1923         ///
1924         /// Basic usage:
1925         ///
1926         /// ```
1927         #[doc = concat!("assert_eq!(3", stringify!($SelfT), ".overflowing_pow(4), (81, false));")]
1928         /// assert_eq!(3i8.overflowing_pow(5), (-13, true));
1929         /// ```
1930         #[stable(feature = "no_panic_pow", since = "1.34.0")]
1931         #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1932         #[must_use = "this returns the result of the operation, \
1933                       without modifying the original"]
1934         #[inline]
1935         pub const fn overflowing_pow(self, mut exp: u32) -> (Self, bool) {
1936             if exp == 0 {
1937                 return (1,false);
1938             }
1939             let mut base = self;
1940             let mut acc: Self = 1;
1941             let mut overflown = false;
1942             // Scratch space for storing results of overflowing_mul.
1943             let mut r;
1944
1945             while exp > 1 {
1946                 if (exp & 1) == 1 {
1947                     r = acc.overflowing_mul(base);
1948                     acc = r.0;
1949                     overflown |= r.1;
1950                 }
1951                 exp /= 2;
1952                 r = base.overflowing_mul(base);
1953                 base = r.0;
1954                 overflown |= r.1;
1955             }
1956
1957             // since exp!=0, finally the exp must be 1.
1958             // Deal with the final bit of the exponent separately, since
1959             // squaring the base afterwards is not necessary and may cause a
1960             // needless overflow.
1961             r = acc.overflowing_mul(base);
1962             r.1 |= overflown;
1963             r
1964         }
1965
1966         /// Raises self to the power of `exp`, using exponentiation by squaring.
1967         ///
1968         /// # Examples
1969         ///
1970         /// Basic usage:
1971         ///
1972         /// ```
1973         #[doc = concat!("let x: ", stringify!($SelfT), " = 2; // or any other integer type")]
1974         ///
1975         /// assert_eq!(x.pow(5), 32);
1976         /// ```
1977         #[stable(feature = "rust1", since = "1.0.0")]
1978         #[rustc_const_stable(feature = "const_int_pow", since = "1.50.0")]
1979         #[must_use = "this returns the result of the operation, \
1980                       without modifying the original"]
1981         #[inline]
1982         #[rustc_inherit_overflow_checks]
1983         pub const fn pow(self, mut exp: u32) -> Self {
1984             if exp == 0 {
1985                 return 1;
1986             }
1987             let mut base = self;
1988             let mut acc = 1;
1989
1990             while exp > 1 {
1991                 if (exp & 1) == 1 {
1992                     acc = acc * base;
1993                 }
1994                 exp /= 2;
1995                 base = base * base;
1996             }
1997
1998             // since exp!=0, finally the exp must be 1.
1999             // Deal with the final bit of the exponent separately, since
2000             // squaring the base afterwards is not necessary and may cause a
2001             // needless overflow.
2002             acc * base
2003         }
2004
2005         /// Calculates the quotient of Euclidean division of `self` by `rhs`.
2006         ///
2007         /// This computes the integer `q` such that `self = q * rhs + r`, with
2008         /// `r = self.rem_euclid(rhs)` and `0 <= r < abs(rhs)`.
2009         ///
2010         /// In other words, the result is `self / rhs` rounded to the integer `q`
2011         /// such that `self >= q * rhs`.
2012         /// If `self > 0`, this is equal to round towards zero (the default in Rust);
2013         /// if `self < 0`, this is equal to round towards +/- infinity.
2014         ///
2015         /// # Panics
2016         ///
2017         /// This function will panic if `rhs` is 0 or the division results in overflow.
2018         ///
2019         /// # Examples
2020         ///
2021         /// Basic usage:
2022         ///
2023         /// ```
2024         #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
2025         /// let b = 4;
2026         ///
2027         /// assert_eq!(a.div_euclid(b), 1); // 7 >= 4 * 1
2028         /// assert_eq!(a.div_euclid(-b), -1); // 7 >= -4 * -1
2029         /// assert_eq!((-a).div_euclid(b), -2); // -7 >= 4 * -2
2030         /// assert_eq!((-a).div_euclid(-b), 2); // -7 >= -4 * 2
2031         /// ```
2032         #[stable(feature = "euclidean_division", since = "1.38.0")]
2033         #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2034         #[must_use = "this returns the result of the operation, \
2035                       without modifying the original"]
2036         #[inline]
2037         #[rustc_inherit_overflow_checks]
2038         pub const fn div_euclid(self, rhs: Self) -> Self {
2039             let q = self / rhs;
2040             if self % rhs < 0 {
2041                 return if rhs > 0 { q - 1 } else { q + 1 }
2042             }
2043             q
2044         }
2045
2046
2047         /// Calculates the least nonnegative remainder of `self (mod rhs)`.
2048         ///
2049         /// This is done as if by the Euclidean division algorithm -- given
2050         /// `r = self.rem_euclid(rhs)`, `self = rhs * self.div_euclid(rhs) + r`, and
2051         /// `0 <= r < abs(rhs)`.
2052         ///
2053         /// # Panics
2054         ///
2055         /// This function will panic if `rhs` is 0 or the division results in overflow.
2056         ///
2057         /// # Examples
2058         ///
2059         /// Basic usage:
2060         ///
2061         /// ```
2062         #[doc = concat!("let a: ", stringify!($SelfT), " = 7; // or any other integer type")]
2063         /// let b = 4;
2064         ///
2065         /// assert_eq!(a.rem_euclid(b), 3);
2066         /// assert_eq!((-a).rem_euclid(b), 1);
2067         /// assert_eq!(a.rem_euclid(-b), 3);
2068         /// assert_eq!((-a).rem_euclid(-b), 1);
2069         /// ```
2070         #[stable(feature = "euclidean_division", since = "1.38.0")]
2071         #[rustc_const_stable(feature = "const_euclidean_int_methods", since = "1.52.0")]
2072         #[must_use = "this returns the result of the operation, \
2073                       without modifying the original"]
2074         #[inline]
2075         #[rustc_inherit_overflow_checks]
2076         pub const fn rem_euclid(self, rhs: Self) -> Self {
2077             let r = self % rhs;
2078             if r < 0 {
2079                 // Semantically equivalent to `if rhs < 0 { r - rhs } else { r + rhs }`.
2080                 // If `rhs` is not `Self::MIN`, then `r + abs(rhs)` will not overflow
2081                 // and is clearly equivalent, because `r` is negative.
2082                 // Otherwise, `rhs` is `Self::MIN`, then we have
2083                 // `r.wrapping_add(Self::MIN.wrapping_abs())`, which evaluates
2084                 // to `r.wrapping_add(Self::MIN)`, which is equivalent to
2085                 // `r - Self::MIN`, which is what we wanted (and will not overflow
2086                 // for negative `r`).
2087                 r.wrapping_add(rhs.wrapping_abs())
2088             } else {
2089                 r
2090             }
2091         }
2092
2093         /// Calculates the quotient of `self` and `rhs`, rounding the result towards negative infinity.
2094         ///
2095         /// # Panics
2096         ///
2097         /// This function will panic if `rhs` is zero.
2098         ///
2099         /// ## Overflow behavior
2100         ///
2101         /// On overflow, this function will panic if overflow checks are enabled (default in debug
2102         /// mode) and wrap if overflow checks are disabled (default in release mode).
2103         ///
2104         /// # Examples
2105         ///
2106         /// Basic usage:
2107         ///
2108         /// ```
2109         /// #![feature(int_roundings)]
2110         #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
2111         /// let b = 3;
2112         ///
2113         /// assert_eq!(a.div_floor(b), 2);
2114         /// assert_eq!(a.div_floor(-b), -3);
2115         /// assert_eq!((-a).div_floor(b), -3);
2116         /// assert_eq!((-a).div_floor(-b), 2);
2117         /// ```
2118         #[unstable(feature = "int_roundings", issue = "88581")]
2119         #[must_use = "this returns the result of the operation, \
2120                       without modifying the original"]
2121         #[inline]
2122         #[rustc_inherit_overflow_checks]
2123         pub const fn div_floor(self, rhs: Self) -> Self {
2124             let d = self / rhs;
2125             let r = self % rhs;
2126             if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
2127                 d - 1
2128             } else {
2129                 d
2130             }
2131         }
2132
2133         /// Calculates the quotient of `self` and `rhs`, rounding the result towards positive infinity.
2134         ///
2135         /// # Panics
2136         ///
2137         /// This function will panic if `rhs` is zero.
2138         ///
2139         /// ## Overflow behavior
2140         ///
2141         /// On overflow, this function will panic if overflow checks are enabled (default in debug
2142         /// mode) and wrap if overflow checks are disabled (default in release mode).
2143         ///
2144         /// # Examples
2145         ///
2146         /// Basic usage:
2147         ///
2148         /// ```
2149         /// #![feature(int_roundings)]
2150         #[doc = concat!("let a: ", stringify!($SelfT)," = 8;")]
2151         /// let b = 3;
2152         ///
2153         /// assert_eq!(a.div_ceil(b), 3);
2154         /// assert_eq!(a.div_ceil(-b), -2);
2155         /// assert_eq!((-a).div_ceil(b), -2);
2156         /// assert_eq!((-a).div_ceil(-b), 3);
2157         /// ```
2158         #[unstable(feature = "int_roundings", issue = "88581")]
2159         #[must_use = "this returns the result of the operation, \
2160                       without modifying the original"]
2161         #[inline]
2162         #[rustc_inherit_overflow_checks]
2163         pub const fn div_ceil(self, rhs: Self) -> Self {
2164             let d = self / rhs;
2165             let r = self % rhs;
2166             if (r > 0 && rhs > 0) || (r < 0 && rhs < 0) {
2167                 d + 1
2168             } else {
2169                 d
2170             }
2171         }
2172
2173         /// If `rhs` is positive, calculates the smallest value greater than or
2174         /// equal to `self` that is a multiple of `rhs`. If `rhs` is negative,
2175         /// calculates the largest value less than or equal to `self` that is a
2176         /// multiple of `rhs`.
2177         ///
2178         /// # Panics
2179         ///
2180         /// This function will panic if `rhs` is zero.
2181         ///
2182         /// ## Overflow behavior
2183         ///
2184         /// On overflow, this function will panic if overflow checks are enabled (default in debug
2185         /// mode) and wrap if overflow checks are disabled (default in release mode).
2186         ///
2187         /// # Examples
2188         ///
2189         /// Basic usage:
2190         ///
2191         /// ```
2192         /// #![feature(int_roundings)]
2193         #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(8), 16);")]
2194         #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(8), 24);")]
2195         #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
2196         #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".next_multiple_of(-8), 16);")]
2197         #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
2198         #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(8), -16);")]
2199         #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").next_multiple_of(-8), -16);")]
2200         #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").next_multiple_of(-8), -24);")]
2201         /// ```
2202         #[unstable(feature = "int_roundings", issue = "88581")]
2203         #[must_use = "this returns the result of the operation, \
2204                       without modifying the original"]
2205         #[inline]
2206         #[rustc_inherit_overflow_checks]
2207         pub const fn next_multiple_of(self, rhs: Self) -> Self {
2208             // This would otherwise fail when calculating `r` when self == T::MIN.
2209             if rhs == -1 {
2210                 return self;
2211             }
2212
2213             let r = self % rhs;
2214             let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
2215                 r + rhs
2216             } else {
2217                 r
2218             };
2219
2220             if m == 0 {
2221                 self
2222             } else {
2223                 self + (rhs - m)
2224             }
2225         }
2226
2227         /// If `rhs` is positive, calculates the smallest value greater than or
2228         /// equal to `self` that is a multiple of `rhs`. If `rhs` is negative,
2229         /// calculates the largest value less than or equal to `self` that is a
2230         /// multiple of `rhs`. Returns `None` if `rhs` is zero or the operation
2231         /// would result in overflow.
2232         ///
2233         /// # Examples
2234         ///
2235         /// Basic usage:
2236         ///
2237         /// ```
2238         /// #![feature(int_roundings)]
2239         #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(16));")]
2240         #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(8), Some(24));")]
2241         #[doc = concat!("assert_eq!(16_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
2242         #[doc = concat!("assert_eq!(23_", stringify!($SelfT), ".checked_next_multiple_of(-8), Some(16));")]
2243         #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
2244         #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(8), Some(-16));")]
2245         #[doc = concat!("assert_eq!((-16_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-16));")]
2246         #[doc = concat!("assert_eq!((-23_", stringify!($SelfT), ").checked_next_multiple_of(-8), Some(-24));")]
2247         #[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".checked_next_multiple_of(0), None);")]
2248         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.checked_next_multiple_of(2), None);")]
2249         /// ```
2250         #[unstable(feature = "int_roundings", issue = "88581")]
2251         #[must_use = "this returns the result of the operation, \
2252                       without modifying the original"]
2253         #[inline]
2254         pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
2255             // This would otherwise fail when calculating `r` when self == T::MIN.
2256             if rhs == -1 {
2257                 return Some(self);
2258             }
2259
2260             let r = try_opt!(self.checked_rem(rhs));
2261             let m = if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) {
2262                 // r + rhs cannot overflow because they have opposite signs
2263                 r + rhs
2264             } else {
2265                 r
2266             };
2267
2268             if m == 0 {
2269                 Some(self)
2270             } else {
2271                 // rhs - m cannot overflow because m has the same sign as rhs
2272                 self.checked_add(rhs - m)
2273             }
2274         }
2275
2276         /// Returns the logarithm of the number with respect to an arbitrary base,
2277         /// rounded down.
2278         ///
2279         /// This method might not be optimized owing to implementation details;
2280         /// `ilog2` can produce results more efficiently for base 2, and `ilog10`
2281         /// can produce results more efficiently for base 10.
2282         ///
2283         /// # Panics
2284         ///
2285         /// This function will panic if `self` is less than or equal to zero,
2286         /// or if `base` is less than 2.
2287         ///
2288         /// # Examples
2289         ///
2290         /// ```
2291         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".ilog(5), 1);")]
2292         /// ```
2293         #[stable(feature = "int_log", since = "1.67.0")]
2294         #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
2295         #[rustc_allow_const_fn_unstable(const_option)]
2296         #[must_use = "this returns the result of the operation, \
2297                       without modifying the original"]
2298         #[inline]
2299         #[track_caller]
2300         pub const fn ilog(self, base: Self) -> u32 {
2301             assert!(base >= 2, "base of integer logarithm must be at least 2");
2302             self.checked_ilog(base).expect("argument of integer logarithm must be positive")
2303         }
2304
2305         /// Returns the base 2 logarithm of the number, rounded down.
2306         ///
2307         /// # Panics
2308         ///
2309         /// This function will panic if `self` is less than or equal to zero.
2310         ///
2311         /// # Examples
2312         ///
2313         /// ```
2314         #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".ilog2(), 1);")]
2315         /// ```
2316         #[stable(feature = "int_log", since = "1.67.0")]
2317         #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
2318         #[rustc_allow_const_fn_unstable(const_option)]
2319         #[must_use = "this returns the result of the operation, \
2320                       without modifying the original"]
2321         #[inline]
2322         #[track_caller]
2323         pub const fn ilog2(self) -> u32 {
2324             self.checked_ilog2().expect("argument of integer logarithm must be positive")
2325         }
2326
2327         /// Returns the base 10 logarithm of the number, rounded down.
2328         ///
2329         /// # Panics
2330         ///
2331         /// This function will panic if `self` is less than or equal to zero.
2332         ///
2333         /// # Example
2334         ///
2335         /// ```
2336         #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".ilog10(), 1);")]
2337         /// ```
2338         #[stable(feature = "int_log", since = "1.67.0")]
2339         #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
2340         #[rustc_allow_const_fn_unstable(const_option)]
2341         #[must_use = "this returns the result of the operation, \
2342                       without modifying the original"]
2343         #[inline]
2344         #[track_caller]
2345         pub const fn ilog10(self) -> u32 {
2346             self.checked_ilog10().expect("argument of integer logarithm must be positive")
2347         }
2348
2349         /// Returns the logarithm of the number with respect to an arbitrary base,
2350         /// rounded down.
2351         ///
2352         /// Returns `None` if the number is negative or zero, or if the base is not at least 2.
2353         ///
2354         /// This method might not be optimized owing to implementation details;
2355         /// `checked_ilog2` can produce results more efficiently for base 2, and
2356         /// `checked_ilog10` can produce results more efficiently for base 10.
2357         ///
2358         /// # Examples
2359         ///
2360         /// ```
2361         #[doc = concat!("assert_eq!(5", stringify!($SelfT), ".checked_ilog(5), Some(1));")]
2362         /// ```
2363         #[stable(feature = "int_log", since = "1.67.0")]
2364         #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
2365         #[must_use = "this returns the result of the operation, \
2366                       without modifying the original"]
2367         #[inline]
2368         pub const fn checked_ilog(self, base: Self) -> Option<u32> {
2369             if self <= 0 || base <= 1 {
2370                 None
2371             } else {
2372                 let mut n = 0;
2373                 let mut r = self;
2374
2375                 // Optimization for 128 bit wide integers.
2376                 if Self::BITS == 128 {
2377                     let b = Self::ilog2(self) / (Self::ilog2(base) + 1);
2378                     n += b;
2379                     r /= base.pow(b as u32);
2380                 }
2381
2382                 while r >= base {
2383                     r /= base;
2384                     n += 1;
2385                 }
2386                 Some(n)
2387             }
2388         }
2389
2390         /// Returns the base 2 logarithm of the number, rounded down.
2391         ///
2392         /// Returns `None` if the number is negative or zero.
2393         ///
2394         /// # Examples
2395         ///
2396         /// ```
2397         #[doc = concat!("assert_eq!(2", stringify!($SelfT), ".checked_ilog2(), Some(1));")]
2398         /// ```
2399         #[stable(feature = "int_log", since = "1.67.0")]
2400         #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
2401         #[must_use = "this returns the result of the operation, \
2402                       without modifying the original"]
2403         #[inline]
2404         pub const fn checked_ilog2(self) -> Option<u32> {
2405             if self <= 0 {
2406                 None
2407             } else {
2408                 // SAFETY: We just checked that this number is positive
2409                 let log = (Self::BITS - 1) - unsafe { intrinsics::ctlz_nonzero(self) as u32 };
2410                 Some(log)
2411             }
2412         }
2413
2414         /// Returns the base 10 logarithm of the number, rounded down.
2415         ///
2416         /// Returns `None` if the number is negative or zero.
2417         ///
2418         /// # Example
2419         ///
2420         /// ```
2421         #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".checked_ilog10(), Some(1));")]
2422         /// ```
2423         #[stable(feature = "int_log", since = "1.67.0")]
2424         #[rustc_const_stable(feature = "int_log", since = "1.67.0")]
2425         #[must_use = "this returns the result of the operation, \
2426                       without modifying the original"]
2427         #[inline]
2428         pub const fn checked_ilog10(self) -> Option<u32> {
2429             if self > 0 {
2430                 Some(int_log10::$ActualT(self as $ActualT))
2431             } else {
2432                 None
2433             }
2434         }
2435
2436         /// Computes the absolute value of `self`.
2437         ///
2438         /// # Overflow behavior
2439         ///
2440         /// The absolute value of
2441         #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
2442         /// cannot be represented as an
2443         #[doc = concat!("`", stringify!($SelfT), "`,")]
2444         /// and attempting to calculate it will cause an overflow. This means
2445         /// that code in debug mode will trigger a panic on this case and
2446         /// optimized code will return
2447         #[doc = concat!("`", stringify!($SelfT), "::MIN`")]
2448         /// without a panic.
2449         ///
2450         /// # Examples
2451         ///
2452         /// Basic usage:
2453         ///
2454         /// ```
2455         #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".abs(), 10);")]
2456         #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").abs(), 10);")]
2457         /// ```
2458         #[stable(feature = "rust1", since = "1.0.0")]
2459         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2460         #[allow(unused_attributes)]
2461         #[must_use = "this returns the result of the operation, \
2462                       without modifying the original"]
2463         #[inline]
2464         #[rustc_inherit_overflow_checks]
2465         pub const fn abs(self) -> Self {
2466             // Note that the #[rustc_inherit_overflow_checks] and #[inline]
2467             // above mean that the overflow semantics of the subtraction
2468             // depend on the crate we're being called from.
2469             if self.is_negative() {
2470                 -self
2471             } else {
2472                 self
2473             }
2474         }
2475
2476         /// Computes the absolute difference between `self` and `other`.
2477         ///
2478         /// This function always returns the correct answer without overflow or
2479         /// panics by returning an unsigned integer.
2480         ///
2481         /// # Examples
2482         ///
2483         /// Basic usage:
2484         ///
2485         /// ```
2486         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(80), 20", stringify!($UnsignedT), ");")]
2487         #[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(110), 10", stringify!($UnsignedT), ");")]
2488         #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(80), 180", stringify!($UnsignedT), ");")]
2489         #[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(-120), 20", stringify!($UnsignedT), ");")]
2490         #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.abs_diff(", stringify!($SelfT), "::MAX), ", stringify!($UnsignedT), "::MAX);")]
2491         /// ```
2492         #[stable(feature = "int_abs_diff", since = "1.60.0")]
2493         #[rustc_const_stable(feature = "int_abs_diff", since = "1.60.0")]
2494         #[must_use = "this returns the result of the operation, \
2495                       without modifying the original"]
2496         #[inline]
2497         pub const fn abs_diff(self, other: Self) -> $UnsignedT {
2498             if self < other {
2499                 // Converting a non-negative x from signed to unsigned by using
2500                 // `x as U` is left unchanged, but a negative x is converted
2501                 // to value x + 2^N. Thus if `s` and `o` are binary variables
2502                 // respectively indicating whether `self` and `other` are
2503                 // negative, we are computing the mathematical value:
2504                 //
2505                 //    (other + o*2^N) - (self + s*2^N)    mod  2^N
2506                 //    other - self + (o-s)*2^N            mod  2^N
2507                 //    other - self                        mod  2^N
2508                 //
2509                 // Finally, taking the mod 2^N of the mathematical value of
2510                 // `other - self` does not change it as it already is
2511                 // in the range [0, 2^N).
2512                 (other as $UnsignedT).wrapping_sub(self as $UnsignedT)
2513             } else {
2514                 (self as $UnsignedT).wrapping_sub(other as $UnsignedT)
2515             }
2516         }
2517
2518         /// Returns a number representing sign of `self`.
2519         ///
2520         ///  - `0` if the number is zero
2521         ///  - `1` if the number is positive
2522         ///  - `-1` if the number is negative
2523         ///
2524         /// # Examples
2525         ///
2526         /// Basic usage:
2527         ///
2528         /// ```
2529         #[doc = concat!("assert_eq!(10", stringify!($SelfT), ".signum(), 1);")]
2530         #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".signum(), 0);")]
2531         #[doc = concat!("assert_eq!((-10", stringify!($SelfT), ").signum(), -1);")]
2532         /// ```
2533         #[stable(feature = "rust1", since = "1.0.0")]
2534         #[rustc_const_stable(feature = "const_int_sign", since = "1.47.0")]
2535         #[must_use = "this returns the result of the operation, \
2536                       without modifying the original"]
2537         #[inline(always)]
2538         pub const fn signum(self) -> Self {
2539             match self {
2540                 n if n > 0 =>  1,
2541                 0          =>  0,
2542                 _          => -1,
2543             }
2544         }
2545
2546         /// Returns `true` if `self` is positive and `false` if the number is zero or
2547         /// negative.
2548         ///
2549         /// # Examples
2550         ///
2551         /// Basic usage:
2552         ///
2553         /// ```
2554         #[doc = concat!("assert!(10", stringify!($SelfT), ".is_positive());")]
2555         #[doc = concat!("assert!(!(-10", stringify!($SelfT), ").is_positive());")]
2556         /// ```
2557         #[must_use]
2558         #[stable(feature = "rust1", since = "1.0.0")]
2559         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2560         #[inline(always)]
2561         pub const fn is_positive(self) -> bool { self > 0 }
2562
2563         /// Returns `true` if `self` is negative and `false` if the number is zero or
2564         /// positive.
2565         ///
2566         /// # Examples
2567         ///
2568         /// Basic usage:
2569         ///
2570         /// ```
2571         #[doc = concat!("assert!((-10", stringify!($SelfT), ").is_negative());")]
2572         #[doc = concat!("assert!(!10", stringify!($SelfT), ".is_negative());")]
2573         /// ```
2574         #[must_use]
2575         #[stable(feature = "rust1", since = "1.0.0")]
2576         #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
2577         #[inline(always)]
2578         pub const fn is_negative(self) -> bool { self < 0 }
2579
2580         /// Return the memory representation of this integer as a byte array in
2581         /// big-endian (network) byte order.
2582         ///
2583         #[doc = $to_xe_bytes_doc]
2584         ///
2585         /// # Examples
2586         ///
2587         /// ```
2588         #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes();")]
2589         #[doc = concat!("assert_eq!(bytes, ", $be_bytes, ");")]
2590         /// ```
2591         #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2592         #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2593         #[must_use = "this returns the result of the operation, \
2594                       without modifying the original"]
2595         #[inline]
2596         pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()] {
2597             self.to_be().to_ne_bytes()
2598         }
2599
2600         /// Return the memory representation of this integer as a byte array in
2601         /// little-endian byte order.
2602         ///
2603         #[doc = $to_xe_bytes_doc]
2604         ///
2605         /// # Examples
2606         ///
2607         /// ```
2608         #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes();")]
2609         #[doc = concat!("assert_eq!(bytes, ", $le_bytes, ");")]
2610         /// ```
2611         #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2612         #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2613         #[must_use = "this returns the result of the operation, \
2614                       without modifying the original"]
2615         #[inline]
2616         pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()] {
2617             self.to_le().to_ne_bytes()
2618         }
2619
2620         /// Return the memory representation of this integer as a byte array in
2621         /// native byte order.
2622         ///
2623         /// As the target platform's native endianness is used, portable code
2624         /// should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate,
2625         /// instead.
2626         ///
2627         #[doc = $to_xe_bytes_doc]
2628         ///
2629         /// [`to_be_bytes`]: Self::to_be_bytes
2630         /// [`to_le_bytes`]: Self::to_le_bytes
2631         ///
2632         /// # Examples
2633         ///
2634         /// ```
2635         #[doc = concat!("let bytes = ", $swap_op, stringify!($SelfT), ".to_ne_bytes();")]
2636         /// assert_eq!(
2637         ///     bytes,
2638         ///     if cfg!(target_endian = "big") {
2639         #[doc = concat!("        ", $be_bytes)]
2640         ///     } else {
2641         #[doc = concat!("        ", $le_bytes)]
2642         ///     }
2643         /// );
2644         /// ```
2645         #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2646         #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2647         // SAFETY: const sound because integers are plain old datatypes so we can always
2648         // transmute them to arrays of bytes
2649         #[must_use = "this returns the result of the operation, \
2650                       without modifying the original"]
2651         #[inline]
2652         pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] {
2653             // SAFETY: integers are plain old datatypes so we can always transmute them to
2654             // arrays of bytes
2655             unsafe { mem::transmute(self) }
2656         }
2657
2658         /// Create an integer value from its representation as a byte array in
2659         /// big endian.
2660         ///
2661         #[doc = $from_xe_bytes_doc]
2662         ///
2663         /// # Examples
2664         ///
2665         /// ```
2666         #[doc = concat!("let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, ");")]
2667         #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
2668         /// ```
2669         ///
2670         /// When starting from a slice rather than an array, fallible conversion APIs can be used:
2671         ///
2672         /// ```
2673         #[doc = concat!("fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
2674         #[doc = concat!("    let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">());")]
2675         ///     *input = rest;
2676         #[doc = concat!("    ", stringify!($SelfT), "::from_be_bytes(int_bytes.try_into().unwrap())")]
2677         /// }
2678         /// ```
2679         #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2680         #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2681         #[must_use]
2682         #[inline]
2683         pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
2684             Self::from_be(Self::from_ne_bytes(bytes))
2685         }
2686
2687         /// Create an integer value from its representation as a byte array in
2688         /// little endian.
2689         ///
2690         #[doc = $from_xe_bytes_doc]
2691         ///
2692         /// # Examples
2693         ///
2694         /// ```
2695         #[doc = concat!("let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, ");")]
2696         #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
2697         /// ```
2698         ///
2699         /// When starting from a slice rather than an array, fallible conversion APIs can be used:
2700         ///
2701         /// ```
2702         #[doc = concat!("fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
2703         #[doc = concat!("    let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">());")]
2704         ///     *input = rest;
2705         #[doc = concat!("    ", stringify!($SelfT), "::from_le_bytes(int_bytes.try_into().unwrap())")]
2706         /// }
2707         /// ```
2708         #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2709         #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2710         #[must_use]
2711         #[inline]
2712         pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
2713             Self::from_le(Self::from_ne_bytes(bytes))
2714         }
2715
2716         /// Create an integer value from its memory representation as a byte
2717         /// array in native endianness.
2718         ///
2719         /// As the target platform's native endianness is used, portable code
2720         /// likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as
2721         /// appropriate instead.
2722         ///
2723         /// [`from_be_bytes`]: Self::from_be_bytes
2724         /// [`from_le_bytes`]: Self::from_le_bytes
2725         ///
2726         #[doc = $from_xe_bytes_doc]
2727         ///
2728         /// # Examples
2729         ///
2730         /// ```
2731         #[doc = concat!("let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"big\") {")]
2732         #[doc = concat!("    ", $be_bytes)]
2733         /// } else {
2734         #[doc = concat!("    ", $le_bytes)]
2735         /// });
2736         #[doc = concat!("assert_eq!(value, ", $swap_op, ");")]
2737         /// ```
2738         ///
2739         /// When starting from a slice rather than an array, fallible conversion APIs can be used:
2740         ///
2741         /// ```
2742         #[doc = concat!("fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " {")]
2743         #[doc = concat!("    let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">());")]
2744         ///     *input = rest;
2745         #[doc = concat!("    ", stringify!($SelfT), "::from_ne_bytes(int_bytes.try_into().unwrap())")]
2746         /// }
2747         /// ```
2748         #[stable(feature = "int_to_from_bytes", since = "1.32.0")]
2749         #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")]
2750         #[must_use]
2751         // SAFETY: const sound because integers are plain old datatypes so we can always
2752         // transmute to them
2753         #[inline]
2754         pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self {
2755             // SAFETY: integers are plain old datatypes so we can always transmute to them
2756             unsafe { mem::transmute(bytes) }
2757         }
2758
2759         /// New code should prefer to use
2760         #[doc = concat!("[`", stringify!($SelfT), "::MIN", "`] instead.")]
2761         ///
2762         /// Returns the smallest value that can be represented by this integer type.
2763         #[stable(feature = "rust1", since = "1.0.0")]
2764         #[inline(always)]
2765         #[rustc_promotable]
2766         #[rustc_const_stable(feature = "const_min_value", since = "1.32.0")]
2767         #[deprecated(since = "TBD", note = "replaced by the `MIN` associated constant on this type")]
2768         pub const fn min_value() -> Self {
2769             Self::MIN
2770         }
2771
2772         /// New code should prefer to use
2773         #[doc = concat!("[`", stringify!($SelfT), "::MAX", "`] instead.")]
2774         ///
2775         /// Returns the largest value that can be represented by this integer type.
2776         #[stable(feature = "rust1", since = "1.0.0")]
2777         #[inline(always)]
2778         #[rustc_promotable]
2779         #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")]
2780         #[deprecated(since = "TBD", note = "replaced by the `MAX` associated constant on this type")]
2781         pub const fn max_value() -> Self {
2782             Self::MAX
2783         }
2784     }
2785 }