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