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