Skip to main content

core/num/
mod.rs

1//! Numeric traits and functions for the built-in numeric types.
2
3#![stable(feature = "rust1", since = "1.0.0")]
4
5use crate::convert::{BoundedCastFromInt, CheckedCastFromInt};
6use crate::panic::const_panic;
7use crate::str::FromStr;
8use crate::ub_checks::assert_unsafe_precondition;
9use crate::{ascii, intrinsics, mem};
10
11// FIXME(const-hack): Used because the `?` operator is not allowed in a const context.
12macro_rules! try_opt {
13    ($e:expr) => {
14        match $e {
15            Some(x) => x,
16            None => return None,
17        }
18    };
19}
20
21// Use this when the generated code should differ between signed and unsigned types.
22macro_rules! sign_dependent_expr {
23    (signed ? if signed { $signed_case:expr } if unsigned { $unsigned_case:expr } ) => {
24        $signed_case
25    };
26    (unsigned ? if signed { $signed_case:expr } if unsigned { $unsigned_case:expr } ) => {
27        $unsigned_case
28    };
29}
30
31// These modules are public only for testing.
32#[doc(hidden)]
33#[unstable(
34    feature = "num_internals",
35    reason = "internal routines only exposed for testing",
36    issue = "none"
37)]
38pub mod imp;
39
40#[macro_use]
41mod int_macros; // import int_impl!
42#[macro_use]
43mod uint_macros; // import uint_impl!
44
45mod error;
46#[cfg(not(no_fp_fmt_parse))]
47mod float_parse;
48mod nonzero;
49mod saturating;
50mod traits;
51mod wrapping;
52
53/// 100% perma-unstable
54#[doc(hidden)]
55pub mod niche_types;
56
57#[stable(feature = "int_error_matching", since = "1.55.0")]
58pub use error::IntErrorKind;
59#[stable(feature = "rust1", since = "1.0.0")]
60pub use error::ParseIntError;
61#[stable(feature = "try_from", since = "1.34.0")]
62pub use error::TryFromIntError;
63#[stable(feature = "rust1", since = "1.0.0")]
64#[cfg(not(no_fp_fmt_parse))]
65pub use float_parse::ParseFloatError;
66#[stable(feature = "generic_nonzero", since = "1.79.0")]
67pub use nonzero::NonZero;
68#[unstable(
69    feature = "nonzero_internals",
70    reason = "implementation detail which may disappear or be replaced at any time",
71    issue = "none"
72)]
73pub use nonzero::ZeroablePrimitive;
74#[stable(feature = "signed_nonzero", since = "1.34.0")]
75pub use nonzero::{NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize};
76#[stable(feature = "nonzero", since = "1.28.0")]
77pub use nonzero::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize};
78#[stable(feature = "saturating_int_impl", since = "1.74.0")]
79pub use saturating::Saturating;
80#[stable(feature = "rust1", since = "1.0.0")]
81pub use wrapping::Wrapping;
82
83macro_rules! u8_xe_bytes_doc {
84    () => {
85        "
86
87**Note**: This function is meaningless on `u8`. Byte order does not exist as a
88concept for byte-sized integers. This function is only provided in symmetry
89with larger integer types.
90
91"
92    };
93}
94
95macro_rules! i8_xe_bytes_doc {
96    () => {
97        "
98
99**Note**: This function is meaningless on `i8`. Byte order does not exist as a
100concept for byte-sized integers. This function is only provided in symmetry
101with larger integer types. You can cast from and to `u8` using
102[`cast_signed`](u8::cast_signed) and [`cast_unsigned`](Self::cast_unsigned).
103
104"
105    };
106}
107
108macro_rules! usize_isize_to_xe_bytes_doc {
109    () => {
110        "
111
112**Note**: This function returns an array of length 2, 4 or 8 bytes
113depending on the target pointer size.
114
115"
116    };
117}
118
119macro_rules! usize_isize_from_xe_bytes_doc {
120    () => {
121        "
122
123**Note**: This function takes an array of length 2, 4 or 8 bytes
124depending on the target pointer size.
125
126"
127    };
128}
129
130macro_rules! midpoint_impl {
131    ($SelfT:ty, unsigned) => {
132        /// Calculates the midpoint (average) between `self` and `rhs`.
133        ///
134        /// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a
135        /// sufficiently-large unsigned integral type. This implies that the result is
136        /// always rounded towards zero and that no overflow will ever occur.
137        ///
138        /// # Examples
139        ///
140        /// ```
141        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
142        #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".midpoint(4), 2);")]
143        /// ```
144        #[stable(feature = "num_midpoint", since = "1.85.0")]
145        #[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
146        #[must_use = "this returns the result of the operation, \
147                      without modifying the original"]
148        #[doc(alias = "average_floor")]
149        #[doc(alias = "average")]
150        #[inline]
151        pub const fn midpoint(self, rhs: $SelfT) -> $SelfT {
152            // Use the well known branchless algorithm from Hacker's Delight to compute
153            // `(a + b) / 2` without overflowing: `((a ^ b) >> 1) + (a & b)`.
154            ((self ^ rhs) >> 1) + (self & rhs)
155        }
156    };
157    ($SelfT:ty, signed) => {
158        /// Calculates the midpoint (average) between `self` and `rhs`.
159        ///
160        /// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a
161        /// sufficiently-large signed integral type. This implies that the result is
162        /// always rounded towards zero and that no overflow will ever occur.
163        ///
164        /// # Examples
165        ///
166        /// ```
167        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
168        #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").midpoint(2), 0);")]
169        #[doc = concat!("assert_eq!((-7", stringify!($SelfT), ").midpoint(0), -3);")]
170        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(-7), -3);")]
171        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(7), 3);")]
172        /// ```
173        #[stable(feature = "num_midpoint_signed", since = "1.87.0")]
174        #[rustc_const_stable(feature = "num_midpoint_signed", since = "1.87.0")]
175        #[must_use = "this returns the result of the operation, \
176                      without modifying the original"]
177        #[doc(alias = "average_floor")]
178        #[doc(alias = "average_ceil")]
179        #[doc(alias = "average")]
180        #[inline]
181        pub const fn midpoint(self, rhs: Self) -> Self {
182            // Use the well known branchless algorithm from Hacker's Delight to compute
183            // `(a + b) / 2` without overflowing: `((a ^ b) >> 1) + (a & b)`.
184            let t = ((self ^ rhs) >> 1) + (self & rhs);
185            // Except that it fails for integers whose sum is an odd negative number as
186            // their floor is one less than their average. So we adjust the result.
187            t + (if t < 0 { 1 } else { 0 } & (self ^ rhs))
188        }
189    };
190    ($SelfT:ty, $WideT:ty, unsigned) => {
191        /// Calculates the midpoint (average) between `self` and `rhs`.
192        ///
193        /// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a
194        /// sufficiently-large unsigned integral type. This implies that the result is
195        /// always rounded towards zero and that no overflow will ever occur.
196        ///
197        /// # Examples
198        ///
199        /// ```
200        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
201        #[doc = concat!("assert_eq!(1", stringify!($SelfT), ".midpoint(4), 2);")]
202        /// ```
203        #[stable(feature = "num_midpoint", since = "1.85.0")]
204        #[rustc_const_stable(feature = "num_midpoint", since = "1.85.0")]
205        #[must_use = "this returns the result of the operation, \
206                      without modifying the original"]
207        #[doc(alias = "average_floor")]
208        #[doc(alias = "average")]
209        #[inline]
210        pub const fn midpoint(self, rhs: $SelfT) -> $SelfT {
211            ((self as $WideT + rhs as $WideT) / 2) as $SelfT
212        }
213    };
214    ($SelfT:ty, $WideT:ty, signed) => {
215        /// Calculates the midpoint (average) between `self` and `rhs`.
216        ///
217        /// `midpoint(a, b)` is `(a + b) / 2` as if it were performed in a
218        /// sufficiently-large signed integral type. This implies that the result is
219        /// always rounded towards zero and that no overflow will ever occur.
220        ///
221        /// # Examples
222        ///
223        /// ```
224        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(4), 2);")]
225        #[doc = concat!("assert_eq!((-1", stringify!($SelfT), ").midpoint(2), 0);")]
226        #[doc = concat!("assert_eq!((-7", stringify!($SelfT), ").midpoint(0), -3);")]
227        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(-7), -3);")]
228        #[doc = concat!("assert_eq!(0", stringify!($SelfT), ".midpoint(7), 3);")]
229        /// ```
230        #[stable(feature = "num_midpoint_signed", since = "1.87.0")]
231        #[rustc_const_stable(feature = "num_midpoint_signed", since = "1.87.0")]
232        #[must_use = "this returns the result of the operation, \
233                      without modifying the original"]
234        #[doc(alias = "average_floor")]
235        #[doc(alias = "average_ceil")]
236        #[doc(alias = "average")]
237        #[inline]
238        pub const fn midpoint(self, rhs: $SelfT) -> $SelfT {
239            ((self as $WideT + rhs as $WideT) / 2) as $SelfT
240        }
241    };
242}
243
244macro_rules! widening_mul_impl {
245    ($SelfT:ty, $WideT:ty) => {
246        /// Widening multiplication. Computes `self * rhs`, widening to a larger integer.
247        ///
248        /// The returned value is always exact and can never overflow.
249        ///
250        /// Note that this method is semantically equivalent to [`carrying_mul`] with a
251        /// carry of zero, with the latter instead returning a tuple denoting the low and
252        /// high parts of the result. Consider using it instead if you need
253        /// interoperability with other big int helper functions, or if this method isn't
254        /// available for a given type.
255        ///
256        /// [`carrying_mul`]: Self::carrying_mul
257        ///
258        /// # Examples
259        ///
260        /// ```
261        /// #![feature(widening_mul)]
262        ///
263        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.widening_mul(0_", stringify!($SelfT), "), 0);")]
264        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.widening_mul(", stringify!($SelfT), "::MAX), ", stringify!($SelfT), "::MAX as ", stringify!($WideT), " * ", stringify!($SelfT), "::MAX as ", stringify!($WideT), ");")]
265        /// ```
266        #[unstable(feature = "widening_mul", issue = "152016")]
267        #[rustc_const_unstable(feature = "widening_mul", issue = "152016")]
268        #[must_use = "this returns the result of the operation, \
269                      without modifying the original"]
270        #[inline]
271        pub const fn widening_mul(self, rhs: Self) -> $WideT {
272            self as $WideT * rhs as $WideT
273        }
274    }
275}
276
277macro_rules! widening_carryless_mul_impl {
278    ($SelfT:ty, $WideT:ty) => {
279        /// Performs a widening carry-less multiplication.
280        ///
281        /// # Examples
282        ///
283        /// ```
284        /// #![feature(uint_carryless_mul)]
285        ///
286        #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.widening_carryless_mul(",
287                                stringify!($SelfT), "::MAX), ", stringify!($WideT), "::MAX / 3);")]
288        /// ```
289        #[rustc_const_unstable(feature = "uint_carryless_mul", issue = "152080")]
290        #[doc(alias = "clmul")]
291        #[unstable(feature = "uint_carryless_mul", issue = "152080")]
292        #[must_use = "this returns the result of the operation, \
293                      without modifying the original"]
294        #[inline]
295        pub const fn widening_carryless_mul(self, rhs: $SelfT) -> $WideT {
296            (self as $WideT).carryless_mul(rhs as $WideT)
297        }
298    }
299}
300
301macro_rules! carrying_carryless_mul_impl {
302    (u128, u256) => {
303        carrying_carryless_mul_impl! { @internal u128 =>
304            pub const fn carrying_carryless_mul(self, rhs: Self, carry: Self) -> (Self, Self) {
305                let x0 = self as u64;
306                let x1 = (self >> 64) as u64;
307                let y0 = rhs as u64;
308                let y1 = (rhs >> 64) as u64;
309
310                let z0 = u64::widening_carryless_mul(x0, y0);
311                let z2 = u64::widening_carryless_mul(x1, y1);
312
313                // The grade school algorithm would compute:
314                // z1 = x0y1 ^ x1y0
315
316                // Instead, Karatsuba first computes:
317                let z3 = u64::widening_carryless_mul(x0 ^ x1, y0 ^ y1);
318                // Since it distributes over XOR,
319                // z3 == x0y0 ^ x0y1 ^ x1y0 ^ x1y1
320                //       |--|   |---------|   |--|
321                //    ==  z0  ^     z1      ^  z2
322                // so we can compute z1 as
323                let z1 = z3 ^ z0 ^ z2;
324
325                let lo = z0 ^ (z1 << 64);
326                let hi = z2 ^ (z1 >> 64);
327
328                (lo ^ carry, hi)
329            }
330        }
331    };
332    ($SelfT:ty, $WideT:ty) => {
333        carrying_carryless_mul_impl! { @internal $SelfT =>
334            pub const fn carrying_carryless_mul(self, rhs: Self, carry: Self) -> (Self, Self) {
335                // Can't use widening_carryless_mul because it's not implemented for usize.
336                let p = (self as $WideT).carryless_mul(rhs as $WideT);
337
338                let lo = (p as $SelfT);
339                let hi = (p  >> Self::BITS) as $SelfT;
340
341                (lo ^ carry, hi)
342            }
343        }
344    };
345    (@internal $SelfT:ty => $($fn:tt)*) => {
346        /// Calculates the "full carryless multiplication" without the possibility to overflow.
347        ///
348        /// This returns the low-order (wrapping) bits and the high-order (overflow) bits
349        /// of the result as two separate values, in that order.
350        ///
351        /// # Examples
352        ///
353        /// Please note that this example is shared among integer types, which is why `u8` is used.
354        ///
355        /// ```
356        /// #![feature(uint_carryless_mul)]
357        ///
358        /// assert_eq!(0b1000_0000u8.carrying_carryless_mul(0b1000_0000, 0b0000), (0, 0b0100_0000));
359        /// assert_eq!(0b1000_0000u8.carrying_carryless_mul(0b1000_0000, 0b1111), (0b1111, 0b0100_0000));
360        #[doc = concat!("assert_eq!(",
361            stringify!($SelfT), "::MAX.carrying_carryless_mul(", stringify!($SelfT), "::MAX, ", stringify!($SelfT), "::MAX), ",
362            "(!(", stringify!($SelfT), "::MAX / 3), ", stringify!($SelfT), "::MAX / 3));"
363        )]
364        /// ```
365        #[rustc_const_unstable(feature = "uint_carryless_mul", issue = "152080")]
366        #[doc(alias = "clmul")]
367        #[unstable(feature = "uint_carryless_mul", issue = "152080")]
368        #[must_use = "this returns the result of the operation, \
369                      without modifying the original"]
370        #[inline]
371        $($fn)*
372    }
373}
374
375impl i8 {
376    int_impl! {
377        Self = i8,
378        ActualT = i8,
379        UnsignedT = u8,
380        BITS = 8,
381        BITS_MINUS_ONE = 7,
382        Min = -128,
383        Max = 127,
384        rot = 2,
385        rot_op = "-0x7e",
386        rot_result = "0xa",
387        swap_op = "0x12",
388        swapped = "0x12",
389        reversed = "0x48",
390        le_bytes = "[0x12]",
391        be_bytes = "[0x12]",
392        to_xe_bytes_doc = i8_xe_bytes_doc!(),
393        from_xe_bytes_doc = i8_xe_bytes_doc!(),
394        bound_condition = "",
395    }
396    midpoint_impl! { i8, i16, signed }
397    widening_mul_impl! { i8, i16 }
398}
399
400impl i16 {
401    int_impl! {
402        Self = i16,
403        ActualT = i16,
404        UnsignedT = u16,
405        BITS = 16,
406        BITS_MINUS_ONE = 15,
407        Min = -32768,
408        Max = 32767,
409        rot = 4,
410        rot_op = "-0x5ffd",
411        rot_result = "0x3a",
412        swap_op = "0x1234",
413        swapped = "0x3412",
414        reversed = "0x2c48",
415        le_bytes = "[0x34, 0x12]",
416        be_bytes = "[0x12, 0x34]",
417        to_xe_bytes_doc = "",
418        from_xe_bytes_doc = "",
419        bound_condition = "",
420    }
421    midpoint_impl! { i16, i32, signed }
422    widening_mul_impl! { i16, i32 }
423}
424
425impl i32 {
426    int_impl! {
427        Self = i32,
428        ActualT = i32,
429        UnsignedT = u32,
430        BITS = 32,
431        BITS_MINUS_ONE = 31,
432        Min = -2147483648,
433        Max = 2147483647,
434        rot = 8,
435        rot_op = "0x10000b3",
436        rot_result = "0xb301",
437        swap_op = "0x12345678",
438        swapped = "0x78563412",
439        reversed = "0x1e6a2c48",
440        le_bytes = "[0x78, 0x56, 0x34, 0x12]",
441        be_bytes = "[0x12, 0x34, 0x56, 0x78]",
442        to_xe_bytes_doc = "",
443        from_xe_bytes_doc = "",
444        bound_condition = "",
445    }
446    midpoint_impl! { i32, i64, signed }
447    widening_mul_impl! { i32, i64 }
448}
449
450impl i64 {
451    int_impl! {
452        Self = i64,
453        ActualT = i64,
454        UnsignedT = u64,
455        BITS = 64,
456        BITS_MINUS_ONE = 63,
457        Min = -9223372036854775808,
458        Max = 9223372036854775807,
459        rot = 12,
460        rot_op = "0xaa00000000006e1",
461        rot_result = "0x6e10aa",
462        swap_op = "0x1234567890123456",
463        swapped = "0x5634129078563412",
464        reversed = "0x6a2c48091e6a2c48",
465        le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
466        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
467        to_xe_bytes_doc = "",
468        from_xe_bytes_doc = "",
469        bound_condition = "",
470    }
471    midpoint_impl! { i64, signed }
472    widening_mul_impl! { i64, i128 }
473}
474
475impl i128 {
476    int_impl! {
477        Self = i128,
478        ActualT = i128,
479        UnsignedT = u128,
480        BITS = 128,
481        BITS_MINUS_ONE = 127,
482        Min = -170141183460469231731687303715884105728,
483        Max = 170141183460469231731687303715884105727,
484        rot = 16,
485        rot_op = "0x13f40000000000000000000000004f76",
486        rot_result = "0x4f7613f4",
487        swap_op = "0x12345678901234567890123456789012",
488        swapped = "0x12907856341290785634129078563412",
489        reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
490        le_bytes = "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
491            0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
492        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
493            0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
494        to_xe_bytes_doc = "",
495        from_xe_bytes_doc = "",
496        bound_condition = "",
497    }
498    midpoint_impl! { i128, signed }
499}
500
501#[doc(auto_cfg = false)]
502#[cfg(target_pointer_width = "16")]
503impl isize {
504    int_impl! {
505        Self = isize,
506        ActualT = i16,
507        UnsignedT = usize,
508        BITS = 16,
509        BITS_MINUS_ONE = 15,
510        Min = -32768,
511        Max = 32767,
512        rot = 4,
513        rot_op = "-0x5ffd",
514        rot_result = "0x3a",
515        swap_op = "0x1234",
516        swapped = "0x3412",
517        reversed = "0x2c48",
518        le_bytes = "[0x34, 0x12]",
519        be_bytes = "[0x12, 0x34]",
520        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
521        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
522        bound_condition = " on 16-bit targets",
523    }
524    midpoint_impl! { isize, i32, signed }
525}
526
527#[doc(auto_cfg = false)]
528#[cfg(target_pointer_width = "32")]
529impl isize {
530    int_impl! {
531        Self = isize,
532        ActualT = i32,
533        UnsignedT = usize,
534        BITS = 32,
535        BITS_MINUS_ONE = 31,
536        Min = -2147483648,
537        Max = 2147483647,
538        rot = 8,
539        rot_op = "0x10000b3",
540        rot_result = "0xb301",
541        swap_op = "0x12345678",
542        swapped = "0x78563412",
543        reversed = "0x1e6a2c48",
544        le_bytes = "[0x78, 0x56, 0x34, 0x12]",
545        be_bytes = "[0x12, 0x34, 0x56, 0x78]",
546        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
547        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
548        bound_condition = " on 32-bit targets",
549    }
550    midpoint_impl! { isize, i64, signed }
551}
552
553#[doc(auto_cfg = false)]
554#[cfg(target_pointer_width = "64")]
555impl isize {
556    int_impl! {
557        Self = isize,
558        ActualT = i64,
559        UnsignedT = usize,
560        BITS = 64,
561        BITS_MINUS_ONE = 63,
562        Min = -9223372036854775808,
563        Max = 9223372036854775807,
564        rot = 12,
565        rot_op = "0xaa00000000006e1",
566        rot_result = "0x6e10aa",
567        swap_op = "0x1234567890123456",
568        swapped = "0x5634129078563412",
569        reversed = "0x6a2c48091e6a2c48",
570        le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
571        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
572        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
573        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
574        bound_condition = " on 64-bit targets",
575    }
576    midpoint_impl! { isize, signed }
577}
578
579/// If the bit selected by this mask is set, ascii is lower case.
580const ASCII_CASE_MASK: u8 = 0b0010_0000;
581
582impl u8 {
583    uint_impl! {
584        Self = u8,
585        ActualT = u8,
586        SignedT = i8,
587        BITS = 8,
588        BITS_MINUS_ONE = 7,
589        MAX = 255,
590        rot = 2,
591        rot_op = "0x82",
592        rot_result = "0xa",
593        fsh_op = "0x36",
594        fshl_result = "0x8",
595        fshr_result = "0x8d",
596        clmul_lhs = "0x12",
597        clmul_rhs = "0x34",
598        clmul_result = "0x28",
599        swap_op = "0x12",
600        swapped = "0x12",
601        reversed = "0x48",
602        le_bytes = "[0x12]",
603        be_bytes = "[0x12]",
604        to_xe_bytes_doc = u8_xe_bytes_doc!(),
605        from_xe_bytes_doc = u8_xe_bytes_doc!(),
606        bound_condition = "",
607    }
608    midpoint_impl! { u8, u16, unsigned }
609    widening_mul_impl! { u8, u16 }
610    widening_carryless_mul_impl! { u8, u16 }
611    carrying_carryless_mul_impl! { u8, u16 }
612
613    /// Checks if the value is within the ASCII range.
614    ///
615    /// # Examples
616    ///
617    /// ```
618    /// let ascii = 97u8;
619    /// let non_ascii = 150u8;
620    ///
621    /// assert!(ascii.is_ascii());
622    /// assert!(!non_ascii.is_ascii());
623    /// ```
624    #[must_use]
625    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
626    #[rustc_const_stable(feature = "const_u8_is_ascii", since = "1.43.0")]
627    #[inline]
628    #[ferrocene::prevalidated]
629    pub const fn is_ascii(&self) -> bool {
630        *self <= 127
631    }
632
633    /// If the value of this byte is within the ASCII range, returns it as an
634    /// [ASCII character](ascii::Char).  Otherwise, returns `None`.
635    #[must_use]
636    #[unstable(feature = "ascii_char", issue = "110998")]
637    #[inline]
638    pub const fn as_ascii(&self) -> Option<ascii::Char> {
639        ascii::Char::from_u8(*self)
640    }
641
642    /// Converts this byte to an [ASCII character](ascii::Char), without
643    /// checking whether or not it's valid.
644    ///
645    /// # Safety
646    ///
647    /// This byte must be valid ASCII, or else this is UB.
648    #[must_use]
649    #[unstable(feature = "ascii_char", issue = "110998")]
650    #[inline]
651    pub const unsafe fn as_ascii_unchecked(&self) -> ascii::Char {
652        assert_unsafe_precondition!(
653            check_library_ub,
654            "as_ascii_unchecked requires that the byte is valid ASCII",
655            (it: &u8 = self) => it.is_ascii()
656        );
657
658        // SAFETY: the caller promised that this byte is ASCII.
659        unsafe { ascii::Char::from_u8_unchecked(*self) }
660    }
661
662    /// Makes a copy of the value in its ASCII upper case equivalent.
663    ///
664    /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
665    /// but non-ASCII letters are unchanged.
666    ///
667    /// To uppercase the value in-place, use [`make_ascii_uppercase`].
668    ///
669    /// # Examples
670    ///
671    /// ```
672    /// let lowercase_a = 97u8;
673    ///
674    /// assert_eq!(65, lowercase_a.to_ascii_uppercase());
675    /// ```
676    ///
677    /// [`make_ascii_uppercase`]: Self::make_ascii_uppercase
678    #[must_use = "to uppercase the value in-place, use `make_ascii_uppercase()`"]
679    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
680    #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
681    #[inline]
682    pub const fn to_ascii_uppercase(&self) -> u8 {
683        // Toggle the 6th bit if this is a lowercase letter
684        *self ^ ((self.is_ascii_lowercase() as u8) * ASCII_CASE_MASK)
685    }
686
687    /// Makes a copy of the value in its ASCII lower case equivalent.
688    ///
689    /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
690    /// but non-ASCII letters are unchanged.
691    ///
692    /// To lowercase the value in-place, use [`make_ascii_lowercase`].
693    ///
694    /// # Examples
695    ///
696    /// ```
697    /// let uppercase_a = 65u8;
698    ///
699    /// assert_eq!(97, uppercase_a.to_ascii_lowercase());
700    /// ```
701    ///
702    /// [`make_ascii_lowercase`]: Self::make_ascii_lowercase
703    #[must_use = "to lowercase the value in-place, use `make_ascii_lowercase()`"]
704    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
705    #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
706    #[inline]
707    #[ferrocene::prevalidated]
708    pub const fn to_ascii_lowercase(&self) -> u8 {
709        // Set the 6th bit if this is an uppercase letter
710        *self | (self.is_ascii_uppercase() as u8 * ASCII_CASE_MASK)
711    }
712
713    /// Assumes self is ascii
714    #[inline]
715    pub(crate) const fn ascii_change_case_unchecked(&self) -> u8 {
716        *self ^ ASCII_CASE_MASK
717    }
718
719    /// Checks that two values are an ASCII case-insensitive match.
720    ///
721    /// This is equivalent to `to_ascii_lowercase(a) == to_ascii_lowercase(b)`.
722    ///
723    /// # Examples
724    ///
725    /// ```
726    /// let lowercase_a = 97u8;
727    /// let uppercase_a = 65u8;
728    ///
729    /// assert!(lowercase_a.eq_ignore_ascii_case(&uppercase_a));
730    /// ```
731    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
732    #[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
733    #[inline]
734    #[ferrocene::prevalidated]
735    pub const fn eq_ignore_ascii_case(&self, other: &u8) -> bool {
736        self.to_ascii_lowercase() == other.to_ascii_lowercase()
737    }
738
739    /// Converts this value to its ASCII upper case equivalent in-place.
740    ///
741    /// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',
742    /// but non-ASCII letters are unchanged.
743    ///
744    /// To return a new uppercased value without modifying the existing one, use
745    /// [`to_ascii_uppercase`].
746    ///
747    /// # Examples
748    ///
749    /// ```
750    /// let mut byte = b'a';
751    ///
752    /// byte.make_ascii_uppercase();
753    ///
754    /// assert_eq!(b'A', byte);
755    /// ```
756    ///
757    /// [`to_ascii_uppercase`]: Self::to_ascii_uppercase
758    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
759    #[rustc_const_stable(feature = "const_make_ascii", since = "1.84.0")]
760    #[inline]
761    pub const fn make_ascii_uppercase(&mut self) {
762        *self = self.to_ascii_uppercase();
763    }
764
765    /// Converts this value to its ASCII lower case equivalent in-place.
766    ///
767    /// ASCII letters 'A' to 'Z' are mapped to 'a' to 'z',
768    /// but non-ASCII letters are unchanged.
769    ///
770    /// To return a new lowercased value without modifying the existing one, use
771    /// [`to_ascii_lowercase`].
772    ///
773    /// # Examples
774    ///
775    /// ```
776    /// let mut byte = b'A';
777    ///
778    /// byte.make_ascii_lowercase();
779    ///
780    /// assert_eq!(b'a', byte);
781    /// ```
782    ///
783    /// [`to_ascii_lowercase`]: Self::to_ascii_lowercase
784    #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
785    #[rustc_const_stable(feature = "const_make_ascii", since = "1.84.0")]
786    #[inline]
787    pub const fn make_ascii_lowercase(&mut self) {
788        *self = self.to_ascii_lowercase();
789    }
790
791    /// Checks if the value is an ASCII alphabetic character:
792    ///
793    /// - U+0041 'A' ..= U+005A 'Z', or
794    /// - U+0061 'a' ..= U+007A 'z'.
795    ///
796    /// # Examples
797    ///
798    /// ```
799    /// let uppercase_a = b'A';
800    /// let uppercase_g = b'G';
801    /// let a = b'a';
802    /// let g = b'g';
803    /// let zero = b'0';
804    /// let percent = b'%';
805    /// let space = b' ';
806    /// let lf = b'\n';
807    /// let esc = b'\x1b';
808    ///
809    /// assert!(uppercase_a.is_ascii_alphabetic());
810    /// assert!(uppercase_g.is_ascii_alphabetic());
811    /// assert!(a.is_ascii_alphabetic());
812    /// assert!(g.is_ascii_alphabetic());
813    /// assert!(!zero.is_ascii_alphabetic());
814    /// assert!(!percent.is_ascii_alphabetic());
815    /// assert!(!space.is_ascii_alphabetic());
816    /// assert!(!lf.is_ascii_alphabetic());
817    /// assert!(!esc.is_ascii_alphabetic());
818    /// ```
819    #[must_use]
820    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
821    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
822    #[inline]
823    #[ferrocene::prevalidated]
824    pub const fn is_ascii_alphabetic(&self) -> bool {
825        matches!(*self, b'A'..=b'Z' | b'a'..=b'z')
826    }
827
828    /// Checks if the value is an ASCII uppercase character:
829    /// U+0041 'A' ..= U+005A 'Z'.
830    ///
831    /// # Examples
832    ///
833    /// ```
834    /// let uppercase_a = b'A';
835    /// let uppercase_g = b'G';
836    /// let a = b'a';
837    /// let g = b'g';
838    /// let zero = b'0';
839    /// let percent = b'%';
840    /// let space = b' ';
841    /// let lf = b'\n';
842    /// let esc = b'\x1b';
843    ///
844    /// assert!(uppercase_a.is_ascii_uppercase());
845    /// assert!(uppercase_g.is_ascii_uppercase());
846    /// assert!(!a.is_ascii_uppercase());
847    /// assert!(!g.is_ascii_uppercase());
848    /// assert!(!zero.is_ascii_uppercase());
849    /// assert!(!percent.is_ascii_uppercase());
850    /// assert!(!space.is_ascii_uppercase());
851    /// assert!(!lf.is_ascii_uppercase());
852    /// assert!(!esc.is_ascii_uppercase());
853    /// ```
854    #[must_use]
855    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
856    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
857    #[inline]
858    #[ferrocene::prevalidated]
859    pub const fn is_ascii_uppercase(&self) -> bool {
860        matches!(*self, b'A'..=b'Z')
861    }
862
863    /// Checks if the value is an ASCII lowercase character:
864    /// U+0061 'a' ..= U+007A 'z'.
865    ///
866    /// # Examples
867    ///
868    /// ```
869    /// let uppercase_a = b'A';
870    /// let uppercase_g = b'G';
871    /// let a = b'a';
872    /// let g = b'g';
873    /// let zero = b'0';
874    /// let percent = b'%';
875    /// let space = b' ';
876    /// let lf = b'\n';
877    /// let esc = b'\x1b';
878    ///
879    /// assert!(!uppercase_a.is_ascii_lowercase());
880    /// assert!(!uppercase_g.is_ascii_lowercase());
881    /// assert!(a.is_ascii_lowercase());
882    /// assert!(g.is_ascii_lowercase());
883    /// assert!(!zero.is_ascii_lowercase());
884    /// assert!(!percent.is_ascii_lowercase());
885    /// assert!(!space.is_ascii_lowercase());
886    /// assert!(!lf.is_ascii_lowercase());
887    /// assert!(!esc.is_ascii_lowercase());
888    /// ```
889    #[must_use]
890    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
891    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
892    #[inline]
893    pub const fn is_ascii_lowercase(&self) -> bool {
894        matches!(*self, b'a'..=b'z')
895    }
896
897    /// Checks if the value is an ASCII alphanumeric character:
898    ///
899    /// - U+0041 'A' ..= U+005A 'Z', or
900    /// - U+0061 'a' ..= U+007A 'z', or
901    /// - U+0030 '0' ..= U+0039 '9'.
902    ///
903    /// # Examples
904    ///
905    /// ```
906    /// let uppercase_a = b'A';
907    /// let uppercase_g = b'G';
908    /// let a = b'a';
909    /// let g = b'g';
910    /// let zero = b'0';
911    /// let percent = b'%';
912    /// let space = b' ';
913    /// let lf = b'\n';
914    /// let esc = b'\x1b';
915    ///
916    /// assert!(uppercase_a.is_ascii_alphanumeric());
917    /// assert!(uppercase_g.is_ascii_alphanumeric());
918    /// assert!(a.is_ascii_alphanumeric());
919    /// assert!(g.is_ascii_alphanumeric());
920    /// assert!(zero.is_ascii_alphanumeric());
921    /// assert!(!percent.is_ascii_alphanumeric());
922    /// assert!(!space.is_ascii_alphanumeric());
923    /// assert!(!lf.is_ascii_alphanumeric());
924    /// assert!(!esc.is_ascii_alphanumeric());
925    /// ```
926    #[must_use]
927    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
928    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
929    #[inline]
930    pub const fn is_ascii_alphanumeric(&self) -> bool {
931        matches!(*self, b'0'..=b'9') | matches!(*self, b'A'..=b'Z') | matches!(*self, b'a'..=b'z')
932    }
933
934    /// Checks if the value is an ASCII decimal digit:
935    /// U+0030 '0' ..= U+0039 '9'.
936    ///
937    /// # Examples
938    ///
939    /// ```
940    /// let uppercase_a = b'A';
941    /// let uppercase_g = b'G';
942    /// let a = b'a';
943    /// let g = b'g';
944    /// let zero = b'0';
945    /// let percent = b'%';
946    /// let space = b' ';
947    /// let lf = b'\n';
948    /// let esc = b'\x1b';
949    ///
950    /// assert!(!uppercase_a.is_ascii_digit());
951    /// assert!(!uppercase_g.is_ascii_digit());
952    /// assert!(!a.is_ascii_digit());
953    /// assert!(!g.is_ascii_digit());
954    /// assert!(zero.is_ascii_digit());
955    /// assert!(!percent.is_ascii_digit());
956    /// assert!(!space.is_ascii_digit());
957    /// assert!(!lf.is_ascii_digit());
958    /// assert!(!esc.is_ascii_digit());
959    /// ```
960    #[must_use]
961    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
962    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
963    #[inline]
964    pub const fn is_ascii_digit(&self) -> bool {
965        matches!(*self, b'0'..=b'9')
966    }
967
968    /// Checks if the value is an ASCII octal digit:
969    /// U+0030 '0' ..= U+0037 '7'.
970    ///
971    /// # Examples
972    ///
973    /// ```
974    /// #![feature(is_ascii_octdigit)]
975    ///
976    /// let uppercase_a = b'A';
977    /// let a = b'a';
978    /// let zero = b'0';
979    /// let seven = b'7';
980    /// let nine = b'9';
981    /// let percent = b'%';
982    /// let lf = b'\n';
983    ///
984    /// assert!(!uppercase_a.is_ascii_octdigit());
985    /// assert!(!a.is_ascii_octdigit());
986    /// assert!(zero.is_ascii_octdigit());
987    /// assert!(seven.is_ascii_octdigit());
988    /// assert!(!nine.is_ascii_octdigit());
989    /// assert!(!percent.is_ascii_octdigit());
990    /// assert!(!lf.is_ascii_octdigit());
991    /// ```
992    #[must_use]
993    #[unstable(feature = "is_ascii_octdigit", issue = "101288")]
994    #[inline]
995    pub const fn is_ascii_octdigit(&self) -> bool {
996        matches!(*self, b'0'..=b'7')
997    }
998
999    /// Checks if the value is an ASCII hexadecimal digit:
1000    ///
1001    /// - U+0030 '0' ..= U+0039 '9', or
1002    /// - U+0041 'A' ..= U+0046 'F', or
1003    /// - U+0061 'a' ..= U+0066 'f'.
1004    ///
1005    /// # Examples
1006    ///
1007    /// ```
1008    /// let uppercase_a = b'A';
1009    /// let uppercase_g = b'G';
1010    /// let a = b'a';
1011    /// let g = b'g';
1012    /// let zero = b'0';
1013    /// let percent = b'%';
1014    /// let space = b' ';
1015    /// let lf = b'\n';
1016    /// let esc = b'\x1b';
1017    ///
1018    /// assert!(uppercase_a.is_ascii_hexdigit());
1019    /// assert!(!uppercase_g.is_ascii_hexdigit());
1020    /// assert!(a.is_ascii_hexdigit());
1021    /// assert!(!g.is_ascii_hexdigit());
1022    /// assert!(zero.is_ascii_hexdigit());
1023    /// assert!(!percent.is_ascii_hexdigit());
1024    /// assert!(!space.is_ascii_hexdigit());
1025    /// assert!(!lf.is_ascii_hexdigit());
1026    /// assert!(!esc.is_ascii_hexdigit());
1027    /// ```
1028    #[must_use]
1029    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
1030    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
1031    #[inline]
1032    pub const fn is_ascii_hexdigit(&self) -> bool {
1033        matches!(*self, b'0'..=b'9') | matches!(*self, b'A'..=b'F') | matches!(*self, b'a'..=b'f')
1034    }
1035
1036    /// Checks if the value is an ASCII punctuation or symbol character
1037    /// (i.e. not alphanumeric, whitespace, or control):
1038    ///
1039    /// - U+0021 ..= U+002F `! " # $ % & ' ( ) * + , - . /`, or
1040    /// - U+003A ..= U+0040 `: ; < = > ? @`, or
1041    /// - U+005B ..= U+0060 `` [ \ ] ^ _ ` ``, or
1042    /// - U+007B ..= U+007E `{ | } ~`
1043    ///
1044    /// # Examples
1045    ///
1046    /// ```
1047    /// let uppercase_a = b'A';
1048    /// let uppercase_g = b'G';
1049    /// let a = b'a';
1050    /// let g = b'g';
1051    /// let zero = b'0';
1052    /// let percent = b'%';
1053    /// let space = b' ';
1054    /// let lf = b'\n';
1055    /// let esc = b'\x1b';
1056    ///
1057    /// assert!(!uppercase_a.is_ascii_punctuation());
1058    /// assert!(!uppercase_g.is_ascii_punctuation());
1059    /// assert!(!a.is_ascii_punctuation());
1060    /// assert!(!g.is_ascii_punctuation());
1061    /// assert!(!zero.is_ascii_punctuation());
1062    /// assert!(percent.is_ascii_punctuation());
1063    /// assert!(!space.is_ascii_punctuation());
1064    /// assert!(!lf.is_ascii_punctuation());
1065    /// assert!(!esc.is_ascii_punctuation());
1066    /// ```
1067    #[must_use]
1068    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
1069    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
1070    #[inline]
1071    pub const fn is_ascii_punctuation(&self) -> bool {
1072        matches!(*self, b'!'..=b'/')
1073            | matches!(*self, b':'..=b'@')
1074            | matches!(*self, b'['..=b'`')
1075            | matches!(*self, b'{'..=b'~')
1076    }
1077
1078    /// Checks if the value is an ASCII graphic character
1079    /// (i.e. not whitespace or control):
1080    /// U+0021 '!' ..= U+007E '~'.
1081    ///
1082    /// # Examples
1083    ///
1084    /// ```
1085    /// let uppercase_a = b'A';
1086    /// let uppercase_g = b'G';
1087    /// let a = b'a';
1088    /// let g = b'g';
1089    /// let zero = b'0';
1090    /// let percent = b'%';
1091    /// let space = b' ';
1092    /// let lf = b'\n';
1093    /// let esc = b'\x1b';
1094    ///
1095    /// assert!(uppercase_a.is_ascii_graphic());
1096    /// assert!(uppercase_g.is_ascii_graphic());
1097    /// assert!(a.is_ascii_graphic());
1098    /// assert!(g.is_ascii_graphic());
1099    /// assert!(zero.is_ascii_graphic());
1100    /// assert!(percent.is_ascii_graphic());
1101    /// assert!(!space.is_ascii_graphic());
1102    /// assert!(!lf.is_ascii_graphic());
1103    /// assert!(!esc.is_ascii_graphic());
1104    /// ```
1105    #[must_use]
1106    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
1107    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
1108    #[inline]
1109    pub const fn is_ascii_graphic(&self) -> bool {
1110        matches!(*self, b'!'..=b'~')
1111    }
1112
1113    /// Checks if the value is an ASCII whitespace character:
1114    /// U+0020 SPACE, U+0009 HORIZONTAL TAB, U+000A LINE FEED,
1115    /// U+000C FORM FEED, or U+000D CARRIAGE RETURN.
1116    ///
1117    /// **Warning:** Because the list above excludes U+000B VERTICAL TAB,
1118    /// `b.is_ascii_whitespace()` is **not** equivalent to `char::from(b).is_whitespace()`.
1119    ///
1120    /// Rust uses the WhatWG Infra Standard's [definition of ASCII
1121    /// whitespace][infra-aw]. There are several other definitions in
1122    /// wide use. For instance, [the POSIX locale][pct] includes
1123    /// U+000B VERTICAL TAB as well as all the above characters,
1124    /// but—from the very same specification—[the default rule for
1125    /// "field splitting" in the Bourne shell][bfs] considers *only*
1126    /// SPACE, HORIZONTAL TAB, and LINE FEED as whitespace.
1127    ///
1128    /// If you are writing a program that will process an existing
1129    /// file format, check what that format's definition of whitespace is
1130    /// before using this function.
1131    ///
1132    /// [infra-aw]: https://infra.spec.whatwg.org/#ascii-whitespace
1133    /// [pct]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html#tag_07_03_01
1134    /// [bfs]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05
1135    ///
1136    /// # Examples
1137    ///
1138    /// ```
1139    /// let uppercase_a = b'A';
1140    /// let uppercase_g = b'G';
1141    /// let a = b'a';
1142    /// let g = b'g';
1143    /// let zero = b'0';
1144    /// let percent = b'%';
1145    /// let space = b' ';
1146    /// let lf = b'\n';
1147    /// let esc = b'\x1b';
1148    ///
1149    /// assert!(!uppercase_a.is_ascii_whitespace());
1150    /// assert!(!uppercase_g.is_ascii_whitespace());
1151    /// assert!(!a.is_ascii_whitespace());
1152    /// assert!(!g.is_ascii_whitespace());
1153    /// assert!(!zero.is_ascii_whitespace());
1154    /// assert!(!percent.is_ascii_whitespace());
1155    /// assert!(space.is_ascii_whitespace());
1156    /// assert!(lf.is_ascii_whitespace());
1157    /// assert!(!esc.is_ascii_whitespace());
1158    /// ```
1159    #[must_use]
1160    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
1161    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
1162    #[inline]
1163    #[ferrocene::prevalidated]
1164    pub const fn is_ascii_whitespace(&self) -> bool {
1165        matches!(*self, b'\t' | b'\n' | b'\x0C' | b'\r' | b' ')
1166    }
1167
1168    /// Checks if the value is an ASCII control character:
1169    /// U+0000 NUL ..= U+001F UNIT SEPARATOR, or U+007F DELETE.
1170    /// Note that most ASCII whitespace characters are control
1171    /// characters, but SPACE is not.
1172    ///
1173    /// # Examples
1174    ///
1175    /// ```
1176    /// let uppercase_a = b'A';
1177    /// let uppercase_g = b'G';
1178    /// let a = b'a';
1179    /// let g = b'g';
1180    /// let zero = b'0';
1181    /// let percent = b'%';
1182    /// let space = b' ';
1183    /// let lf = b'\n';
1184    /// let esc = b'\x1b';
1185    ///
1186    /// assert!(!uppercase_a.is_ascii_control());
1187    /// assert!(!uppercase_g.is_ascii_control());
1188    /// assert!(!a.is_ascii_control());
1189    /// assert!(!g.is_ascii_control());
1190    /// assert!(!zero.is_ascii_control());
1191    /// assert!(!percent.is_ascii_control());
1192    /// assert!(!space.is_ascii_control());
1193    /// assert!(lf.is_ascii_control());
1194    /// assert!(esc.is_ascii_control());
1195    /// ```
1196    #[must_use]
1197    #[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
1198    #[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
1199    #[inline]
1200    #[ferrocene::prevalidated]
1201    pub const fn is_ascii_control(&self) -> bool {
1202        matches!(*self, b'\0'..=b'\x1F' | b'\x7F')
1203    }
1204
1205    /// Returns an iterator that produces an escaped version of a `u8`,
1206    /// treating it as an ASCII character.
1207    ///
1208    /// The behavior is identical to [`ascii::escape_default`].
1209    ///
1210    /// # Examples
1211    ///
1212    /// ```
1213    /// assert_eq!("0", b'0'.escape_ascii().to_string());
1214    /// assert_eq!("\\t", b'\t'.escape_ascii().to_string());
1215    /// assert_eq!("\\r", b'\r'.escape_ascii().to_string());
1216    /// assert_eq!("\\n", b'\n'.escape_ascii().to_string());
1217    /// assert_eq!("\\'", b'\''.escape_ascii().to_string());
1218    /// assert_eq!("\\\"", b'"'.escape_ascii().to_string());
1219    /// assert_eq!("\\\\", b'\\'.escape_ascii().to_string());
1220    /// assert_eq!("\\x9d", b'\x9d'.escape_ascii().to_string());
1221    /// ```
1222    #[must_use = "this returns the escaped byte as an iterator, \
1223                  without modifying the original"]
1224    #[stable(feature = "inherent_ascii_escape", since = "1.60.0")]
1225    #[inline]
1226    #[ferrocene::prevalidated]
1227    pub fn escape_ascii(self) -> ascii::EscapeDefault {
1228        ascii::escape_default(self)
1229    }
1230
1231    #[inline]
1232    #[ferrocene::prevalidated]
1233    pub(crate) const fn is_utf8_char_boundary(self) -> bool {
1234        // This is bit magic equivalent to: b < 128 || b >= 192
1235        (self as i8) >= -0x40
1236    }
1237}
1238
1239impl u16 {
1240    uint_impl! {
1241        Self = u16,
1242        ActualT = u16,
1243        SignedT = i16,
1244        BITS = 16,
1245        BITS_MINUS_ONE = 15,
1246        MAX = 65535,
1247        rot = 4,
1248        rot_op = "0xa003",
1249        rot_result = "0x3a",
1250        fsh_op = "0x2de",
1251        fshl_result = "0x30",
1252        fshr_result = "0x302d",
1253        clmul_lhs = "0x9012",
1254        clmul_rhs = "0xcd34",
1255        clmul_result = "0x928",
1256        swap_op = "0x1234",
1257        swapped = "0x3412",
1258        reversed = "0x2c48",
1259        le_bytes = "[0x34, 0x12]",
1260        be_bytes = "[0x12, 0x34]",
1261        to_xe_bytes_doc = "",
1262        from_xe_bytes_doc = "",
1263        bound_condition = "",
1264    }
1265    midpoint_impl! { u16, u32, unsigned }
1266    widening_mul_impl! { u16, u32 }
1267    widening_carryless_mul_impl! { u16, u32 }
1268    carrying_carryless_mul_impl! { u16, u32 }
1269
1270    /// Checks if the value is a Unicode surrogate code point, which are disallowed values for [`char`].
1271    ///
1272    /// # Examples
1273    ///
1274    /// ```
1275    /// #![feature(utf16_extra)]
1276    ///
1277    /// let low_non_surrogate = 0xA000u16;
1278    /// let low_surrogate = 0xD800u16;
1279    /// let high_surrogate = 0xDC00u16;
1280    /// let high_non_surrogate = 0xE000u16;
1281    ///
1282    /// assert!(!low_non_surrogate.is_utf16_surrogate());
1283    /// assert!(low_surrogate.is_utf16_surrogate());
1284    /// assert!(high_surrogate.is_utf16_surrogate());
1285    /// assert!(!high_non_surrogate.is_utf16_surrogate());
1286    /// ```
1287    #[must_use]
1288    #[unstable(feature = "utf16_extra", issue = "94919")]
1289    #[inline]
1290    #[ferrocene::prevalidated]
1291    pub const fn is_utf16_surrogate(self) -> bool {
1292        matches!(self, 0xD800..=0xDFFF)
1293    }
1294}
1295
1296impl u32 {
1297    uint_impl! {
1298        Self = u32,
1299        ActualT = u32,
1300        SignedT = i32,
1301        BITS = 32,
1302        BITS_MINUS_ONE = 31,
1303        MAX = 4294967295,
1304        rot = 8,
1305        rot_op = "0x10000b3",
1306        rot_result = "0xb301",
1307        fsh_op = "0x2fe78e45",
1308        fshl_result = "0xb32f",
1309        fshr_result = "0xb32fe78e",
1310        clmul_lhs = "0x56789012",
1311        clmul_rhs = "0xf52ecd34",
1312        clmul_result = "0x9b980928",
1313        swap_op = "0x12345678",
1314        swapped = "0x78563412",
1315        reversed = "0x1e6a2c48",
1316        le_bytes = "[0x78, 0x56, 0x34, 0x12]",
1317        be_bytes = "[0x12, 0x34, 0x56, 0x78]",
1318        to_xe_bytes_doc = "",
1319        from_xe_bytes_doc = "",
1320        bound_condition = "",
1321    }
1322    midpoint_impl! { u32, u64, unsigned }
1323    widening_mul_impl! { u32, u64 }
1324    widening_carryless_mul_impl! { u32, u64 }
1325    carrying_carryless_mul_impl! { u32, u64 }
1326}
1327
1328impl u64 {
1329    uint_impl! {
1330        Self = u64,
1331        ActualT = u64,
1332        SignedT = i64,
1333        BITS = 64,
1334        BITS_MINUS_ONE = 63,
1335        MAX = 18446744073709551615,
1336        rot = 12,
1337        rot_op = "0xaa00000000006e1",
1338        rot_result = "0x6e10aa",
1339        fsh_op = "0x2fe78e45983acd98",
1340        fshl_result = "0x6e12fe",
1341        fshr_result = "0x6e12fe78e45983ac",
1342        clmul_lhs = "0x7890123456789012",
1343        clmul_rhs = "0xdd358416f52ecd34",
1344        clmul_result = "0xa6299579b980928",
1345        swap_op = "0x1234567890123456",
1346        swapped = "0x5634129078563412",
1347        reversed = "0x6a2c48091e6a2c48",
1348        le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
1349        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
1350        to_xe_bytes_doc = "",
1351        from_xe_bytes_doc = "",
1352        bound_condition = "",
1353    }
1354    midpoint_impl! { u64, u128, unsigned }
1355    widening_mul_impl! { u64, u128 }
1356    widening_carryless_mul_impl! { u64, u128 }
1357    carrying_carryless_mul_impl! { u64, u128 }
1358}
1359
1360impl u128 {
1361    uint_impl! {
1362        Self = u128,
1363        ActualT = u128,
1364        SignedT = i128,
1365        BITS = 128,
1366        BITS_MINUS_ONE = 127,
1367        MAX = 340282366920938463463374607431768211455,
1368        rot = 16,
1369        rot_op = "0x13f40000000000000000000000004f76",
1370        rot_result = "0x4f7613f4",
1371        fsh_op = "0x2fe78e45983acd98039000008736273",
1372        fshl_result = "0x4f7602fe",
1373        fshr_result = "0x4f7602fe78e45983acd9803900000873",
1374        clmul_lhs = "0x12345678901234567890123456789012",
1375        clmul_rhs = "0x4317e40ab4ddcf05dd358416f52ecd34",
1376        clmul_result = "0xb9cf660de35d0c170a6299579b980928",
1377        swap_op = "0x12345678901234567890123456789012",
1378        swapped = "0x12907856341290785634129078563412",
1379        reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
1380        le_bytes = "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
1381            0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
1382        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
1383            0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
1384        to_xe_bytes_doc = "",
1385        from_xe_bytes_doc = "",
1386        bound_condition = "",
1387    }
1388    midpoint_impl! { u128, unsigned }
1389    carrying_carryless_mul_impl! { u128, u256 }
1390}
1391
1392#[doc(auto_cfg = false)]
1393#[cfg(target_pointer_width = "16")]
1394impl usize {
1395    uint_impl! {
1396        Self = usize,
1397        ActualT = u16,
1398        SignedT = isize,
1399        BITS = 16,
1400        BITS_MINUS_ONE = 15,
1401        MAX = 65535,
1402        rot = 4,
1403        rot_op = "0xa003",
1404        rot_result = "0x3a",
1405        fsh_op = "0x2de",
1406        fshl_result = "0x30",
1407        fshr_result = "0x302d",
1408        clmul_lhs = "0x9012",
1409        clmul_rhs = "0xcd34",
1410        clmul_result = "0x928",
1411        swap_op = "0x1234",
1412        swapped = "0x3412",
1413        reversed = "0x2c48",
1414        le_bytes = "[0x34, 0x12]",
1415        be_bytes = "[0x12, 0x34]",
1416        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
1417        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
1418        bound_condition = " on 16-bit targets",
1419    }
1420    midpoint_impl! { usize, u32, unsigned }
1421    carrying_carryless_mul_impl! { usize, u32 }
1422}
1423
1424#[doc(auto_cfg = false)]
1425#[cfg(target_pointer_width = "32")]
1426impl usize {
1427    uint_impl! {
1428        Self = usize,
1429        ActualT = u32,
1430        SignedT = isize,
1431        BITS = 32,
1432        BITS_MINUS_ONE = 31,
1433        MAX = 4294967295,
1434        rot = 8,
1435        rot_op = "0x10000b3",
1436        rot_result = "0xb301",
1437        fsh_op = "0x2fe78e45",
1438        fshl_result = "0xb32f",
1439        fshr_result = "0xb32fe78e",
1440        clmul_lhs = "0x56789012",
1441        clmul_rhs = "0xf52ecd34",
1442        clmul_result = "0x9b980928",
1443        swap_op = "0x12345678",
1444        swapped = "0x78563412",
1445        reversed = "0x1e6a2c48",
1446        le_bytes = "[0x78, 0x56, 0x34, 0x12]",
1447        be_bytes = "[0x12, 0x34, 0x56, 0x78]",
1448        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
1449        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
1450        bound_condition = " on 32-bit targets",
1451    }
1452    midpoint_impl! { usize, u64, unsigned }
1453    carrying_carryless_mul_impl! { usize, u64 }
1454}
1455
1456#[doc(auto_cfg = false)]
1457#[cfg(target_pointer_width = "64")]
1458impl usize {
1459    uint_impl! {
1460        Self = usize,
1461        ActualT = u64,
1462        SignedT = isize,
1463        BITS = 64,
1464        BITS_MINUS_ONE = 63,
1465        MAX = 18446744073709551615,
1466        rot = 12,
1467        rot_op = "0xaa00000000006e1",
1468        rot_result = "0x6e10aa",
1469        fsh_op = "0x2fe78e45983acd98",
1470        fshl_result = "0x6e12fe",
1471        fshr_result = "0x6e12fe78e45983ac",
1472        clmul_lhs = "0x7890123456789012",
1473        clmul_rhs = "0xdd358416f52ecd34",
1474        clmul_result = "0xa6299579b980928",
1475        swap_op = "0x1234567890123456",
1476        swapped = "0x5634129078563412",
1477        reversed = "0x6a2c48091e6a2c48",
1478        le_bytes = "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
1479        be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
1480        to_xe_bytes_doc = usize_isize_to_xe_bytes_doc!(),
1481        from_xe_bytes_doc = usize_isize_from_xe_bytes_doc!(),
1482        bound_condition = " on 64-bit targets",
1483    }
1484    midpoint_impl! { usize, u128, unsigned }
1485    carrying_carryless_mul_impl! { usize, u128 }
1486}
1487
1488impl usize {
1489    /// Returns an `usize` where every byte is equal to `x`.
1490    #[inline]
1491    #[ferrocene::prevalidated]
1492    pub(crate) const fn repeat_u8(x: u8) -> usize {
1493        usize::from_ne_bytes([x; size_of::<usize>()])
1494    }
1495
1496    /// Returns an `usize` where every byte pair is equal to `x`.
1497    #[inline]
1498    #[ferrocene::annotation("This function is only being used in constants and cannot be covered")]
1499    #[ferrocene::prevalidated]
1500    pub(crate) const fn repeat_u16(x: u16) -> usize {
1501        let mut r = 0usize;
1502        let mut i = 0;
1503        while i < size_of::<usize>() {
1504            // Use `wrapping_shl` to make it work on targets with 16-bit `usize`
1505            r = r.wrapping_shl(16) | (x as usize);
1506            i += 2;
1507        }
1508        r
1509    }
1510}
1511
1512/// A classification of floating point numbers.
1513///
1514/// This `enum` is used as the return type for [`f32::classify`] and [`f64::classify`]. See
1515/// their documentation for more.
1516///
1517/// # Examples
1518///
1519/// ```
1520/// use std::num::FpCategory;
1521///
1522/// let num = 12.4_f32;
1523/// let inf = f32::INFINITY;
1524/// let zero = 0f32;
1525/// let sub: f32 = 1.1754942e-38;
1526/// let nan = f32::NAN;
1527///
1528/// assert_eq!(num.classify(), FpCategory::Normal);
1529/// assert_eq!(inf.classify(), FpCategory::Infinite);
1530/// assert_eq!(zero.classify(), FpCategory::Zero);
1531/// assert_eq!(sub.classify(), FpCategory::Subnormal);
1532/// assert_eq!(nan.classify(), FpCategory::Nan);
1533/// ```
1534#[derive(Copy, Clone, PartialEq, Eq, Debug)]
1535#[stable(feature = "rust1", since = "1.0.0")]
1536#[ferrocene::prevalidated]
1537pub enum FpCategory {
1538    /// NaN (not a number): this value results from calculations like `(-1.0).sqrt()`.
1539    ///
1540    /// See [the documentation for `f32`](f32) for more information on the unusual properties
1541    /// of NaN.
1542    #[stable(feature = "rust1", since = "1.0.0")]
1543    Nan,
1544
1545    /// Positive or negative infinity, which often results from dividing a nonzero number
1546    /// by zero.
1547    #[stable(feature = "rust1", since = "1.0.0")]
1548    Infinite,
1549
1550    /// Positive or negative zero.
1551    ///
1552    /// See [the documentation for `f32`](f32) for more information on the signedness of zeroes.
1553    #[stable(feature = "rust1", since = "1.0.0")]
1554    Zero,
1555
1556    /// “Subnormal” or “denormal” floating point representation (less precise, relative to
1557    /// their magnitude, than [`Normal`]).
1558    ///
1559    /// Subnormal numbers are larger in magnitude than [`Zero`] but smaller in magnitude than all
1560    /// [`Normal`] numbers.
1561    ///
1562    /// [`Normal`]: Self::Normal
1563    /// [`Zero`]: Self::Zero
1564    #[stable(feature = "rust1", since = "1.0.0")]
1565    Subnormal,
1566
1567    /// A regular floating point number, not any of the exceptional categories.
1568    ///
1569    /// The smallest positive normal numbers are [`f32::MIN_POSITIVE`] and [`f64::MIN_POSITIVE`],
1570    /// and the largest positive normal numbers are [`f32::MAX`] and [`f64::MAX`]. (Unlike signed
1571    /// integers, floating point numbers are symmetric in their range, so negating any of these
1572    /// constants will produce their negative counterpart.)
1573    #[stable(feature = "rust1", since = "1.0.0")]
1574    Normal,
1575}
1576
1577/// Determines if a string of text of that length of that radix could be guaranteed to be
1578/// stored in the given type T.
1579/// Note that if the radix is known to the compiler, it is just the check of digits.len that
1580/// is done at runtime.
1581#[doc(hidden)]
1582#[inline(always)]
1583#[unstable(issue = "none", feature = "std_internals")]
1584#[ferrocene::prevalidated]
1585pub const fn can_not_overflow<T>(radix: u32, is_signed_ty: bool, digits: &[u8]) -> bool {
1586    radix <= 16 && digits.len() <= size_of::<T>() * 2 - is_signed_ty as usize
1587}
1588
1589#[cfg_attr(not(panic = "immediate-abort"), inline(never))]
1590#[cfg_attr(panic = "immediate-abort", inline)]
1591#[cold]
1592#[track_caller]
1593#[ferrocene::prevalidated]
1594const fn from_ascii_radix_panic(radix: u32) -> ! {
1595    const_panic!(
1596        "from_ascii_radix: radix must lie in the range `[2, 36]`",
1597        "from_ascii_radix: radix must lie in the range `[2, 36]` - found {radix}",
1598        radix: u32 = radix,
1599    )
1600}
1601
1602macro_rules! from_str_int_impl {
1603    ($signedness:ident $($int_ty:ty)+) => {$(
1604        #[stable(feature = "rust1", since = "1.0.0")]
1605        #[rustc_const_unstable(feature = "const_convert", issue = "143773")]
1606        const impl FromStr for $int_ty {
1607            type Err = ParseIntError;
1608
1609            /// Parses an integer from a string slice with decimal digits.
1610            ///
1611            /// The characters are expected to be an optional
1612            #[doc = sign_dependent_expr!{
1613                $signedness ?
1614                if signed {
1615                    " `+` or `-` "
1616                }
1617                if unsigned {
1618                    " `+` "
1619                }
1620            }]
1621            /// sign followed by only digits. Leading and trailing non-digit characters (including
1622            /// whitespace) represent an error. Underscores (which are accepted in Rust literals)
1623            /// also represent an error.
1624            ///
1625            /// # See also
1626            /// For parsing numbers in other bases, such as binary or hexadecimal,
1627            /// see [`from_str_radix`][Self::from_str_radix].
1628            ///
1629            /// # Examples
1630            ///
1631            /// ```
1632            /// use std::str::FromStr;
1633            ///
1634            #[doc = concat!("assert_eq!(", stringify!($int_ty), "::from_str(\"+10\"), Ok(10));")]
1635            /// ```
1636            /// Trailing space returns error:
1637            /// ```
1638            /// # use std::str::FromStr;
1639            /// #
1640            #[doc = concat!("assert!(", stringify!($int_ty), "::from_str(\"1 \").is_err());")]
1641            /// ```
1642            #[inline]
1643            fn from_str(src: &str) -> Result<$int_ty, ParseIntError> {
1644                <$int_ty>::from_str_radix(src, 10)
1645            }
1646        }
1647
1648        impl $int_ty {
1649            /// Parses an integer from a string slice with digits in a given base.
1650            ///
1651            /// The string is expected to be an optional
1652            #[doc = sign_dependent_expr!{
1653                $signedness ?
1654                if signed {
1655                    " `+` or `-` "
1656                }
1657                if unsigned {
1658                    " `+` "
1659                }
1660            }]
1661            /// sign followed by only digits. Leading and trailing non-digit characters (including
1662            /// whitespace) represent an error. Underscores (which are accepted in Rust literals)
1663            /// also represent an error.
1664            ///
1665            /// Digits are a subset of these characters, depending on `radix`:
1666            /// * `0-9`
1667            /// * `a-z`
1668            /// * `A-Z`
1669            ///
1670            /// # Panics
1671            ///
1672            /// This function panics if `radix` is not in the range from 2 to 36.
1673            ///
1674            /// # See also
1675            /// If the string to be parsed is in base 10 (decimal),
1676            /// [`from_str`] or [`str::parse`] can also be used.
1677            ///
1678            // FIXME(#122566): These HTML links work around a rustdoc-json test failure.
1679            /// [`from_str`]: #method.from_str
1680            /// [`str::parse`]: primitive.str.html#method.parse
1681            ///
1682            /// # Examples
1683            ///
1684            /// ```
1685            #[doc = concat!("assert_eq!(", stringify!($int_ty), "::from_str_radix(\"A\", 16), Ok(10));")]
1686            /// ```
1687            /// Trailing space returns error:
1688            /// ```
1689            #[doc = concat!("assert!(", stringify!($int_ty), "::from_str_radix(\"1 \", 10).is_err());")]
1690            /// ```
1691            #[stable(feature = "rust1", since = "1.0.0")]
1692            #[rustc_const_stable(feature = "const_int_from_str", since = "1.82.0")]
1693            #[inline]
1694            #[ferrocene::prevalidated]
1695            pub const fn from_str_radix(src: &str, radix: u32) -> Result<$int_ty, ParseIntError> {
1696                <$int_ty>::from_ascii_radix(src.as_bytes(), radix)
1697            }
1698
1699            /// Parses an integer from an ASCII-byte slice with decimal digits.
1700            ///
1701            /// The characters are expected to be an optional
1702            #[doc = sign_dependent_expr!{
1703                $signedness ?
1704                if signed {
1705                    " `+` or `-` "
1706                }
1707                if unsigned {
1708                    " `+` "
1709                }
1710            }]
1711            /// sign followed by only digits. Leading and trailing non-digit characters (including
1712            /// whitespace) represent an error. Underscores (which are accepted in Rust literals)
1713            /// also represent an error.
1714            ///
1715            /// # Examples
1716            ///
1717            /// ```
1718            /// #![feature(int_from_ascii)]
1719            ///
1720            #[doc = concat!("assert_eq!(", stringify!($int_ty), "::from_ascii(b\"+10\"), Ok(10));")]
1721            /// ```
1722            /// Trailing space returns error:
1723            /// ```
1724            /// # #![feature(int_from_ascii)]
1725            /// #
1726            #[doc = concat!("assert!(", stringify!($int_ty), "::from_ascii(b\"1 \").is_err());")]
1727            /// ```
1728            #[unstable(feature = "int_from_ascii", issue = "134821")]
1729            #[inline]
1730            pub const fn from_ascii(src: &[u8]) -> Result<$int_ty, ParseIntError> {
1731                <$int_ty>::from_ascii_radix(src, 10)
1732            }
1733
1734            /// Parses an integer from an ASCII-byte slice with digits in a given base.
1735            ///
1736            /// The characters are expected to be an optional
1737            #[doc = sign_dependent_expr!{
1738                $signedness ?
1739                if signed {
1740                    " `+` or `-` "
1741                }
1742                if unsigned {
1743                    " `+` "
1744                }
1745            }]
1746            /// sign followed by only digits. Leading and trailing non-digit characters (including
1747            /// whitespace) represent an error. Underscores (which are accepted in Rust literals)
1748            /// also represent an error.
1749            ///
1750            /// Digits are a subset of these characters, depending on `radix`:
1751            /// * `0-9`
1752            /// * `a-z`
1753            /// * `A-Z`
1754            ///
1755            /// # Panics
1756            ///
1757            /// This function panics if `radix` is not in the range from 2 to 36.
1758            ///
1759            /// # Examples
1760            ///
1761            /// ```
1762            /// #![feature(int_from_ascii)]
1763            ///
1764            #[doc = concat!("assert_eq!(", stringify!($int_ty), "::from_ascii_radix(b\"A\", 16), Ok(10));")]
1765            /// ```
1766            /// Trailing space returns error:
1767            /// ```
1768            /// # #![feature(int_from_ascii)]
1769            /// #
1770            #[doc = concat!("assert!(", stringify!($int_ty), "::from_ascii_radix(b\"1 \", 10).is_err());")]
1771            /// ```
1772            #[unstable(feature = "int_from_ascii", issue = "134821")]
1773            #[inline]
1774            #[ferrocene::prevalidated]
1775            pub const fn from_ascii_radix(src: &[u8], radix: u32) -> Result<$int_ty, ParseIntError> {
1776                use self::IntErrorKind::*;
1777                use self::ParseIntError as PIE;
1778
1779                if 2 > radix || radix > 36 {
1780                    from_ascii_radix_panic(radix);
1781                }
1782
1783                if src.is_empty() {
1784                    return Err(PIE { kind: Empty });
1785                }
1786
1787                #[allow(unused_comparisons)]
1788                let is_signed_ty = 0 > <$int_ty>::MIN;
1789
1790                let (is_positive, mut digits) = match src {
1791                    [b'+' | b'-'] => {
1792                        return Err(PIE { kind: InvalidDigit });
1793                    }
1794                    [b'+', rest @ ..] => (true, rest),
1795                    [b'-', rest @ ..] if is_signed_ty => (false, rest),
1796                    _ => (true, src),
1797                };
1798
1799                let mut result = 0;
1800
1801                macro_rules! unwrap_or_PIE {
1802                    ($option:expr, $kind:ident) => {
1803                        match $option {
1804                            Some(value) => value,
1805                            None => return Err(PIE { kind: $kind }),
1806                        }
1807                    };
1808                }
1809
1810                if can_not_overflow::<$int_ty>(radix, is_signed_ty, digits) {
1811                    // If the len of the str is short compared to the range of the type
1812                    // we are parsing into, then we can be certain that an overflow will not occur.
1813                    // This bound is when `radix.pow(digits.len()) - 1 <= T::MAX` but the condition
1814                    // above is a faster (conservative) approximation of this.
1815                    //
1816                    // Consider radix 16 as it has the highest information density per digit and will thus overflow the earliest:
1817                    // `u8::MAX` is `ff` - any str of len 2 is guaranteed to not overflow.
1818                    // `i8::MAX` is `7f` - only a str of len 1 is guaranteed to not overflow.
1819                    macro_rules! run_unchecked_loop {
1820                        ($unchecked_additive_op:tt) => {{
1821                            while let [c, rest @ ..] = digits {
1822                                result = result * (radix as $int_ty);
1823                                let x = unwrap_or_PIE!((*c as char).to_digit(radix), InvalidDigit);
1824                                result = result $unchecked_additive_op (x as $int_ty);
1825                                digits = rest;
1826                            }
1827                        }};
1828                    }
1829                    if is_positive {
1830                        run_unchecked_loop!(+)
1831                    } else {
1832                        run_unchecked_loop!(-)
1833                    };
1834                } else {
1835                    macro_rules! run_checked_loop {
1836                        ($checked_additive_op:ident, $overflow_err:ident) => {{
1837                            while let [c, rest @ ..] = digits {
1838                                // When `radix` is passed in as a literal, rather than doing a slow `imul`
1839                                // the compiler can use shifts if `radix` can be expressed as a
1840                                // sum of powers of 2 (x*10 can be written as x*8 + x*2).
1841                                // When the compiler can't use these optimisations,
1842                                // the latency of the multiplication can be hidden by issuing it
1843                                // before the result is needed to improve performance on
1844                                // modern out-of-order CPU as multiplication here is slower
1845                                // than the other instructions, we can get the end result faster
1846                                // doing multiplication first and let the CPU spends other cycles
1847                                // doing other computation and get multiplication result later.
1848                                let mul = result.checked_mul(radix as $int_ty);
1849                                let x = unwrap_or_PIE!((*c as char).to_digit(radix), InvalidDigit) as $int_ty;
1850                                result = unwrap_or_PIE!(mul, $overflow_err);
1851                                result = unwrap_or_PIE!(<$int_ty>::$checked_additive_op(result, x), $overflow_err);
1852                                digits = rest;
1853                            }
1854                        }};
1855                    }
1856                    if is_positive {
1857                        run_checked_loop!(checked_add, PosOverflow)
1858                    } else {
1859                        run_checked_loop!(checked_sub, NegOverflow)
1860                    };
1861                }
1862                Ok(result)
1863            }
1864        }
1865    )*}
1866}
1867
1868from_str_int_impl! { signed isize i8 i16 i32 i64 i128 }
1869from_str_int_impl! { unsigned usize u8 u16 u32 u64 u128 }