1 //! Definitions of integer that is known not to equal zero.
4 use crate::ops::{BitOr, BitOrAssign, Div, Rem};
5 use crate::str::FromStr;
7 use super::from_str_radix;
8 use super::{IntErrorKind, ParseIntError};
11 macro_rules! impl_nonzero_fmt {
12 ( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
15 impl fmt::$Trait for $Ty {
17 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 macro_rules! nonzero_integers {
26 ( $( #[$stability: meta] #[$const_new_unchecked_stability: meta] $Ty: ident($Int: ty); )+ ) => {
28 /// An integer that is known not to equal zero.
30 /// This enables some memory layout optimization.
31 #[doc = concat!("For example, `Option<", stringify!($Ty), ">` is the same size as `", stringify!($Int), "`:")]
34 /// use std::mem::size_of;
35 #[doc = concat!("assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", stringify!($Int), ">());")]
38 #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
40 #[rustc_layout_scalar_valid_range_start(1)]
41 #[rustc_nonnull_optimization_guaranteed]
45 /// Creates a non-zero without checking whether the value is non-zero.
46 /// This results in undefined behaviour if the value is zero.
50 /// The value must not be zero.
52 #[$const_new_unchecked_stability]
54 pub const unsafe fn new_unchecked(n: $Int) -> Self {
55 // SAFETY: this is guaranteed to be safe by the caller.
59 /// Creates a non-zero if the given value is not zero.
61 #[rustc_const_stable(feature = "const_nonzero_int_methods", since = "1.47.0")]
63 pub const fn new(n: $Int) -> Option<Self> {
65 // SAFETY: we just checked that there's no `0`
66 Some(unsafe { Self(n) })
72 /// Returns the value as a primitive type.
75 #[rustc_const_stable(feature = "nonzero", since = "1.34.0")]
76 pub const fn get(self) -> $Int {
82 #[stable(feature = "from_nonzero", since = "1.31.0")]
83 impl From<$Ty> for $Int {
84 #[doc = concat!("Converts a `", stringify!($Ty), "` into an `", stringify!($Int), "`")]
86 fn from(nonzero: $Ty) -> Self {
91 #[stable(feature = "nonzero_bitor", since = "1.45.0")]
95 fn bitor(self, rhs: Self) -> Self::Output {
96 // SAFETY: since `self` and `rhs` are both nonzero, the
97 // result of the bitwise-or will be nonzero.
98 unsafe { $Ty::new_unchecked(self.get() | rhs.get()) }
102 #[stable(feature = "nonzero_bitor", since = "1.45.0")]
103 impl BitOr<$Int> for $Ty {
106 fn bitor(self, rhs: $Int) -> Self::Output {
107 // SAFETY: since `self` is nonzero, the result of the
108 // bitwise-or will be nonzero regardless of the value of
110 unsafe { $Ty::new_unchecked(self.get() | rhs) }
114 #[stable(feature = "nonzero_bitor", since = "1.45.0")]
115 impl BitOr<$Ty> for $Int {
118 fn bitor(self, rhs: $Ty) -> Self::Output {
119 // SAFETY: since `rhs` is nonzero, the result of the
120 // bitwise-or will be nonzero regardless of the value of
122 unsafe { $Ty::new_unchecked(self | rhs.get()) }
126 #[stable(feature = "nonzero_bitor", since = "1.45.0")]
127 impl BitOrAssign for $Ty {
129 fn bitor_assign(&mut self, rhs: Self) {
134 #[stable(feature = "nonzero_bitor", since = "1.45.0")]
135 impl BitOrAssign<$Int> for $Ty {
137 fn bitor_assign(&mut self, rhs: $Int) {
143 #[$stability] (Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
150 #[stable(feature = "nonzero", since = "1.28.0")] #[rustc_const_stable(feature = "nonzero", since = "1.28.0")] NonZeroU8(u8);
151 #[stable(feature = "nonzero", since = "1.28.0")] #[rustc_const_stable(feature = "nonzero", since = "1.28.0")] NonZeroU16(u16);
152 #[stable(feature = "nonzero", since = "1.28.0")] #[rustc_const_stable(feature = "nonzero", since = "1.28.0")] NonZeroU32(u32);
153 #[stable(feature = "nonzero", since = "1.28.0")] #[rustc_const_stable(feature = "nonzero", since = "1.28.0")] NonZeroU64(u64);
154 #[stable(feature = "nonzero", since = "1.28.0")] #[rustc_const_stable(feature = "nonzero", since = "1.28.0")] NonZeroU128(u128);
155 #[stable(feature = "nonzero", since = "1.28.0")] #[rustc_const_stable(feature = "nonzero", since = "1.28.0")] NonZeroUsize(usize);
156 #[stable(feature = "signed_nonzero", since = "1.34.0")] #[rustc_const_stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI8(i8);
157 #[stable(feature = "signed_nonzero", since = "1.34.0")] #[rustc_const_stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI16(i16);
158 #[stable(feature = "signed_nonzero", since = "1.34.0")] #[rustc_const_stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI32(i32);
159 #[stable(feature = "signed_nonzero", since = "1.34.0")] #[rustc_const_stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI64(i64);
160 #[stable(feature = "signed_nonzero", since = "1.34.0")] #[rustc_const_stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI128(i128);
161 #[stable(feature = "signed_nonzero", since = "1.34.0")] #[rustc_const_stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroIsize(isize);
164 macro_rules! from_str_radix_nzint_impl {
166 #[stable(feature = "nonzero_parse", since = "1.35.0")]
167 impl FromStr for $t {
168 type Err = ParseIntError;
169 fn from_str(src: &str) -> Result<Self, Self::Err> {
170 Self::new(from_str_radix(src, 10)?)
171 .ok_or(ParseIntError {
172 kind: IntErrorKind::Zero
179 from_str_radix_nzint_impl! { NonZeroU8 NonZeroU16 NonZeroU32 NonZeroU64 NonZeroU128 NonZeroUsize
180 NonZeroI8 NonZeroI16 NonZeroI32 NonZeroI64 NonZeroI128 NonZeroIsize }
182 macro_rules! nonzero_leading_trailing_zeros {
183 ( $( $Ty: ident($Uint: ty) , $LeadingTestExpr:expr ;)+ ) => {
186 /// Returns the number of leading zeros in the binary representation of `self`.
188 /// On many architectures, this function can perform better than `leading_zeros()` on the underlying integer type, as special handling of zero can be avoided.
195 #[doc = concat!("let n = std::num::", stringify!($Ty), "::new(", stringify!($LeadingTestExpr), ").unwrap();")]
197 /// assert_eq!(n.leading_zeros(), 0);
199 #[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
200 #[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
202 pub const fn leading_zeros(self) -> u32 {
203 // SAFETY: since `self` can not be zero it is safe to call ctlz_nonzero
204 unsafe { intrinsics::ctlz_nonzero(self.0 as $Uint) as u32 }
207 /// Returns the number of trailing zeros in the binary representation
210 /// On many architectures, this function can perform better than `trailing_zeros()` on the underlying integer type, as special handling of zero can be avoided.
217 #[doc = concat!("let n = std::num::", stringify!($Ty), "::new(0b0101000).unwrap();")]
219 /// assert_eq!(n.trailing_zeros(), 3);
221 #[stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
222 #[rustc_const_stable(feature = "nonzero_leading_trailing_zeros", since = "1.53.0")]
224 pub const fn trailing_zeros(self) -> u32 {
225 // SAFETY: since `self` can not be zero it is safe to call cttz_nonzero
226 unsafe { intrinsics::cttz_nonzero(self.0 as $Uint) as u32 }
234 nonzero_leading_trailing_zeros! {
235 NonZeroU8(u8), u8::MAX;
236 NonZeroU16(u16), u16::MAX;
237 NonZeroU32(u32), u32::MAX;
238 NonZeroU64(u64), u64::MAX;
239 NonZeroU128(u128), u128::MAX;
240 NonZeroUsize(usize), usize::MAX;
242 NonZeroI16(u16), -1i16;
243 NonZeroI32(u32), -1i32;
244 NonZeroI64(u64), -1i64;
245 NonZeroI128(u128), -1i128;
246 NonZeroIsize(usize), -1isize;
249 macro_rules! nonzero_integers_div {
250 ( $( $Ty: ident($Int: ty); )+ ) => {
252 #[stable(feature = "nonzero_div", since = "1.51.0")]
253 impl Div<$Ty> for $Int {
255 /// This operation rounds towards zero,
256 /// truncating any fractional part of the exact result, and cannot panic.
258 fn div(self, other: $Ty) -> $Int {
259 // SAFETY: div by zero is checked because `other` is a nonzero,
260 // and MIN/-1 is checked because `self` is an unsigned int.
261 unsafe { crate::intrinsics::unchecked_div(self, other.get()) }
265 #[stable(feature = "nonzero_div", since = "1.51.0")]
266 impl Rem<$Ty> for $Int {
268 /// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic.
270 fn rem(self, other: $Ty) -> $Int {
271 // SAFETY: rem by zero is checked because `other` is a nonzero,
272 // and MIN/-1 is checked because `self` is an unsigned int.
273 unsafe { crate::intrinsics::unchecked_rem(self, other.get()) }
280 nonzero_integers_div! {
289 // A bunch of methods for unsigned nonzero types only.
290 macro_rules! nonzero_unsigned_operations {
291 ( $( $Ty: ident($Int: ty); )+ ) => {
294 /// Add an unsigned integer to a non-zero value.
295 /// Check for overflow and return [`None`] on overflow
296 /// As a consequence, the result cannot wrap to zero.
302 /// #![feature(nonzero_ops)]
303 #[doc = concat!("# use std::num::", stringify!($Ty), ";")]
305 /// # fn main() { test().unwrap(); }
306 /// # fn test() -> Option<()> {
307 #[doc = concat!("let one = ", stringify!($Ty), "::new(1)?;")]
308 #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")]
309 #[doc = concat!("let max = ", stringify!($Ty), "::new(",
310 stringify!($Int), "::MAX)?;")]
312 /// assert_eq!(Some(two), one.checked_add(1));
313 /// assert_eq!(None, max.checked_add(1));
317 #[unstable(feature = "nonzero_ops", issue = "84186")]
319 pub const fn checked_add(self, other: $Int) -> Option<$Ty> {
320 if let Some(result) = self.get().checked_add(other) {
321 // SAFETY: $Int::checked_add returns None on overflow
322 // so the result cannot be zero.
323 Some(unsafe { $Ty::new_unchecked(result) })
329 /// Add an unsigned integer to a non-zero value.
330 #[doc = concat!("Return [`", stringify!($Int), "::MAX`] on overflow.")]
335 /// #![feature(nonzero_ops)]
336 #[doc = concat!("# use std::num::", stringify!($Ty), ";")]
338 /// # fn main() { test().unwrap(); }
339 /// # fn test() -> Option<()> {
340 #[doc = concat!("let one = ", stringify!($Ty), "::new(1)?;")]
341 #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")]
342 #[doc = concat!("let max = ", stringify!($Ty), "::new(",
343 stringify!($Int), "::MAX)?;")]
345 /// assert_eq!(two, one.saturating_add(1));
346 /// assert_eq!(max, max.saturating_add(1));
350 #[unstable(feature = "nonzero_ops", issue = "84186")]
352 pub const fn saturating_add(self, other: $Int) -> $Ty {
353 // SAFETY: $Int::saturating_add returns $Int::MAX on overflow
354 // so the result cannot be zero.
355 unsafe { $Ty::new_unchecked(self.get().saturating_add(other)) }
358 /// Add an unsigned integer to a non-zero value,
359 /// assuming overflow cannot occur.
360 /// Overflow is unchecked, and it is undefined behaviour to overflow
361 /// *even if the result would wrap to a non-zero value*.
362 /// The behaviour is undefined as soon as
363 #[doc = concat!("`self + rhs > ", stringify!($Int), "::MAX`.")]
368 /// #![feature(nonzero_ops)]
369 #[doc = concat!("# use std::num::", stringify!($Ty), ";")]
371 /// # fn main() { test().unwrap(); }
372 /// # fn test() -> Option<()> {
373 #[doc = concat!("let one = ", stringify!($Ty), "::new(1)?;")]
374 #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")]
376 /// assert_eq!(two, unsafe { one.unchecked_add(1) });
380 #[unstable(feature = "nonzero_ops", issue = "84186")]
382 pub unsafe fn unchecked_add(self, other: $Int) -> $Ty {
383 // SAFETY: The caller ensures there is no overflow.
384 unsafe { $Ty::new_unchecked(self.get().unchecked_add(other)) }
387 /// Returns the smallest power of two greater than or equal to n.
388 /// Check for overflow and return [`None`]
389 /// if the next power of two is greater than the type’s maximum value.
390 /// As a consequence, the result cannot wrap to zero.
395 /// #![feature(nonzero_ops)]
396 #[doc = concat!("# use std::num::", stringify!($Ty), ";")]
398 /// # fn main() { test().unwrap(); }
399 /// # fn test() -> Option<()> {
400 #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")]
401 #[doc = concat!("let three = ", stringify!($Ty), "::new(3)?;")]
402 #[doc = concat!("let four = ", stringify!($Ty), "::new(4)?;")]
403 #[doc = concat!("let max = ", stringify!($Ty), "::new(",
404 stringify!($Int), "::MAX)?;")]
406 /// assert_eq!(Some(two), two.checked_next_power_of_two() );
407 /// assert_eq!(Some(four), three.checked_next_power_of_two() );
408 /// assert_eq!(None, max.checked_next_power_of_two() );
412 #[unstable(feature = "nonzero_ops", issue = "84186")]
414 pub const fn checked_next_power_of_two(self) -> Option<$Ty> {
415 if let Some(nz) = self.get().checked_next_power_of_two() {
416 // SAFETY: The next power of two is positive
417 // and overflow is checked.
418 Some(unsafe { $Ty::new_unchecked(nz) })
428 nonzero_unsigned_operations! {
437 // A bunch of methods for signed nonzero types only.
438 macro_rules! nonzero_signed_operations {
439 ( $( $Ty: ident($Int: ty) -> $Uty: ident($Uint: ty); )+ ) => {
442 /// Computes the absolute value of self.
443 #[doc = concat!("See [`", stringify!($Int), "::abs`]")]
444 /// for documentation on overflow behaviour.
449 /// #![feature(nonzero_ops)]
450 #[doc = concat!("# use std::num::", stringify!($Ty), ";")]
452 /// # fn main() { test().unwrap(); }
453 /// # fn test() -> Option<()> {
454 #[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")]
455 #[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")]
457 /// assert_eq!(pos, pos.abs());
458 /// assert_eq!(pos, neg.abs());
462 #[unstable(feature = "nonzero_ops", issue = "84186")]
464 pub const fn abs(self) -> $Ty {
465 // SAFETY: This cannot overflow to zero.
466 unsafe { $Ty::new_unchecked(self.get().abs()) }
469 /// Checked absolute value.
470 /// Check for overflow and returns [`None`] if
471 #[doc = concat!("`self == ", stringify!($Int), "::MIN`.")]
472 /// The result cannot be zero.
477 /// #![feature(nonzero_ops)]
478 #[doc = concat!("# use std::num::", stringify!($Ty), ";")]
480 /// # fn main() { test().unwrap(); }
481 /// # fn test() -> Option<()> {
482 #[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")]
483 #[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")]
484 #[doc = concat!("let min = ", stringify!($Ty), "::new(",
485 stringify!($Int), "::MIN)?;")]
487 /// assert_eq!(Some(pos), neg.checked_abs());
488 /// assert_eq!(None, min.checked_abs());
492 #[unstable(feature = "nonzero_ops", issue = "84186")]
494 pub const fn checked_abs(self) -> Option<$Ty> {
495 if let Some(nz) = self.get().checked_abs() {
496 // SAFETY: absolute value of nonzero cannot yield zero values.
497 Some(unsafe { $Ty::new_unchecked(nz) })
503 /// Computes the absolute value of self,
504 /// with overflow information, see
505 #[doc = concat!("[`", stringify!($Int), "::overflowing_abs`].")]
510 /// #![feature(nonzero_ops)]
511 #[doc = concat!("# use std::num::", stringify!($Ty), ";")]
513 /// # fn main() { test().unwrap(); }
514 /// # fn test() -> Option<()> {
515 #[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")]
516 #[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")]
517 #[doc = concat!("let min = ", stringify!($Ty), "::new(",
518 stringify!($Int), "::MIN)?;")]
520 /// assert_eq!((pos, false), pos.overflowing_abs());
521 /// assert_eq!((pos, false), neg.overflowing_abs());
522 /// assert_eq!((min, true), min.overflowing_abs());
526 #[unstable(feature = "nonzero_ops", issue = "84186")]
528 pub const fn overflowing_abs(self) -> ($Ty, bool) {
529 let (nz, flag) = self.get().overflowing_abs();
531 // SAFETY: absolute value of nonzero cannot yield zero values.
532 unsafe { $Ty::new_unchecked(nz) },
537 /// Saturating absolute value, see
538 #[doc = concat!("[`", stringify!($Int), "::saturating_abs`].")]
543 /// #![feature(nonzero_ops)]
544 #[doc = concat!("# use std::num::", stringify!($Ty), ";")]
546 /// # fn main() { test().unwrap(); }
547 /// # fn test() -> Option<()> {
548 #[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")]
549 #[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")]
550 #[doc = concat!("let min = ", stringify!($Ty), "::new(",
551 stringify!($Int), "::MIN)?;")]
552 #[doc = concat!("let min_plus = ", stringify!($Ty), "::new(",
553 stringify!($Int), "::MIN + 1)?;")]
554 #[doc = concat!("let max = ", stringify!($Ty), "::new(",
555 stringify!($Int), "::MAX)?;")]
557 /// assert_eq!(pos, pos.saturating_abs());
558 /// assert_eq!(pos, neg.saturating_abs());
559 /// assert_eq!(max, min.saturating_abs());
560 /// assert_eq!(max, min_plus.saturating_abs());
564 #[unstable(feature = "nonzero_ops", issue = "84186")]
566 pub const fn saturating_abs(self) -> $Ty {
567 // SAFETY: absolute value of nonzero cannot yield zero values.
568 unsafe { $Ty::new_unchecked(self.get().saturating_abs()) }
571 /// Wrapping absolute value, see
572 #[doc = concat!("[`", stringify!($Int), "::wrapping_abs`].")]
577 /// #![feature(nonzero_ops)]
578 #[doc = concat!("# use std::num::", stringify!($Ty), ";")]
580 /// # fn main() { test().unwrap(); }
581 /// # fn test() -> Option<()> {
582 #[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")]
583 #[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")]
584 #[doc = concat!("let min = ", stringify!($Ty), "::new(",
585 stringify!($Int), "::MIN)?;")]
586 #[doc = concat!("let max = ", stringify!($Ty), "::new(",
587 stringify!($Int), "::MAX)?;")]
589 /// assert_eq!(pos, pos.wrapping_abs());
590 /// assert_eq!(pos, neg.wrapping_abs());
591 /// assert_eq!(min, min.wrapping_abs());
592 /// # // FIXME: add once Neg is implemented?
593 /// # // assert_eq!(max, (-max).wrapping_abs());
597 #[unstable(feature = "nonzero_ops", issue = "84186")]
599 pub const fn wrapping_abs(self) -> $Ty {
600 // SAFETY: absolute value of nonzero cannot yield zero values.
601 unsafe { $Ty::new_unchecked(self.get().wrapping_abs()) }
604 /// Computes the absolute value of self
605 /// without any wrapping or panicking.
610 /// #![feature(nonzero_ops)]
611 #[doc = concat!("# use std::num::", stringify!($Ty), ";")]
612 #[doc = concat!("# use std::num::", stringify!($Uty), ";")]
614 /// # fn main() { test().unwrap(); }
615 /// # fn test() -> Option<()> {
616 #[doc = concat!("let u_pos = ", stringify!($Uty), "::new(1)?;")]
617 #[doc = concat!("let i_pos = ", stringify!($Ty), "::new(1)?;")]
618 #[doc = concat!("let i_neg = ", stringify!($Ty), "::new(-1)?;")]
619 #[doc = concat!("let i_min = ", stringify!($Ty), "::new(",
620 stringify!($Int), "::MIN)?;")]
621 #[doc = concat!("let u_max = ", stringify!($Uty), "::new(",
622 stringify!($Uint), "::MAX / 2 + 1)?;")]
624 /// assert_eq!(u_pos, i_pos.unsigned_abs());
625 /// assert_eq!(u_pos, i_neg.unsigned_abs());
626 /// assert_eq!(u_max, i_min.unsigned_abs());
630 #[unstable(feature = "nonzero_ops", issue = "84186")]
632 pub const fn unsigned_abs(self) -> $Uty {
633 // SAFETY: absolute value of nonzero cannot yield zero values.
634 unsafe { $Uty::new_unchecked(self.get().unsigned_abs()) }
641 nonzero_signed_operations! {
642 NonZeroI8(i8) -> NonZeroU8(u8);
643 NonZeroI16(i16) -> NonZeroU16(u16);
644 NonZeroI32(i32) -> NonZeroU32(u32);
645 NonZeroI64(i64) -> NonZeroU64(u64);
646 NonZeroI128(i128) -> NonZeroU128(u128);
647 NonZeroIsize(isize) -> NonZeroUsize(usize);
650 // A bunch of methods for both signed and unsigned nonzero types.
651 macro_rules! nonzero_unsigned_signed_operations {
652 ( $( $signedness:ident $Ty: ident($Int: ty); )+ ) => {
655 /// Multiply two non-zero integers together.
656 /// Check for overflow and return [`None`] on overflow.
657 /// As a consequence, the result cannot wrap to zero.
662 /// #![feature(nonzero_ops)]
663 #[doc = concat!("# use std::num::", stringify!($Ty), ";")]
665 /// # fn main() { test().unwrap(); }
666 /// # fn test() -> Option<()> {
667 #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")]
668 #[doc = concat!("let four = ", stringify!($Ty), "::new(4)?;")]
669 #[doc = concat!("let max = ", stringify!($Ty), "::new(",
670 stringify!($Int), "::MAX)?;")]
672 /// assert_eq!(Some(four), two.checked_mul(two));
673 /// assert_eq!(None, max.checked_mul(two));
677 #[unstable(feature = "nonzero_ops", issue = "84186")]
679 pub const fn checked_mul(self, other: $Ty) -> Option<$Ty> {
680 if let Some(result) = self.get().checked_mul(other.get()) {
681 // SAFETY: checked_mul returns None on overflow
682 // and `other` is also non-null
683 // so the result cannot be zero.
684 Some(unsafe { $Ty::new_unchecked(result) })
690 /// Multiply two non-zero integers together.
691 #[doc = concat!("Return [`", stringify!($Int), "::MAX`] on overflow.")]
696 /// #![feature(nonzero_ops)]
697 #[doc = concat!("# use std::num::", stringify!($Ty), ";")]
699 /// # fn main() { test().unwrap(); }
700 /// # fn test() -> Option<()> {
701 #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")]
702 #[doc = concat!("let four = ", stringify!($Ty), "::new(4)?;")]
703 #[doc = concat!("let max = ", stringify!($Ty), "::new(",
704 stringify!($Int), "::MAX)?;")]
706 /// assert_eq!(four, two.saturating_mul(two));
707 /// assert_eq!(max, four.saturating_mul(max));
711 #[unstable(feature = "nonzero_ops", issue = "84186")]
713 pub const fn saturating_mul(self, other: $Ty) -> $Ty {
714 // SAFETY: saturating_mul returns u*::MAX on overflow
715 // and `other` is also non-null
716 // so the result cannot be zero.
717 unsafe { $Ty::new_unchecked(self.get().saturating_mul(other.get())) }
720 /// Multiply two non-zero integers together,
721 /// assuming overflow cannot occur.
722 /// Overflow is unchecked, and it is undefined behaviour to overflow
723 /// *even if the result would wrap to a non-zero value*.
724 /// The behaviour is undefined as soon as
725 #[doc = sign_dependent_expr!{
728 concat!("`self * rhs > ", stringify!($Int), "::MAX`, ",
729 "or `self * rhs < ", stringify!($Int), "::MIN`.")
732 concat!("`self * rhs > ", stringify!($Int), "::MAX`.")
739 /// #![feature(nonzero_ops)]
740 #[doc = concat!("# use std::num::", stringify!($Ty), ";")]
742 /// # fn main() { test().unwrap(); }
743 /// # fn test() -> Option<()> {
744 #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")]
745 #[doc = concat!("let four = ", stringify!($Ty), "::new(4)?;")]
747 /// assert_eq!(four, unsafe { two.unchecked_mul(two) });
751 #[unstable(feature = "nonzero_ops", issue = "84186")]
753 pub unsafe fn unchecked_mul(self, other: $Ty) -> $Ty {
754 // SAFETY: The caller ensures there is no overflow.
755 unsafe { $Ty::new_unchecked(self.get().unchecked_mul(other.get())) }
758 /// Raise non-zero value to an integer power.
759 /// Check for overflow and return [`None`] on overflow.
760 /// As a consequence, the result cannot wrap to zero.
765 /// #![feature(nonzero_ops)]
766 #[doc = concat!("# use std::num::", stringify!($Ty), ";")]
768 /// # fn main() { test().unwrap(); }
769 /// # fn test() -> Option<()> {
770 #[doc = concat!("let three = ", stringify!($Ty), "::new(3)?;")]
771 #[doc = concat!("let twenty_seven = ", stringify!($Ty), "::new(27)?;")]
772 #[doc = concat!("let half_max = ", stringify!($Ty), "::new(",
773 stringify!($Int), "::MAX / 2)?;")]
775 /// assert_eq!(Some(twenty_seven), three.checked_pow(3));
776 /// assert_eq!(None, half_max.checked_pow(3));
780 #[unstable(feature = "nonzero_ops", issue = "84186")]
782 pub const fn checked_pow(self, other: u32) -> Option<$Ty> {
783 if let Some(result) = self.get().checked_pow(other) {
784 // SAFETY: checked_pow returns None on overflow
785 // so the result cannot be zero.
786 Some(unsafe { $Ty::new_unchecked(result) })
792 /// Raise non-zero value to an integer power.
793 #[doc = sign_dependent_expr!{
796 concat!("Return [`", stringify!($Int), "::MIN`] ",
797 "or [`", stringify!($Int), "::MAX`] on overflow.")
800 concat!("Return [`", stringify!($Int), "::MAX`] on overflow.")
807 /// #![feature(nonzero_ops)]
808 #[doc = concat!("# use std::num::", stringify!($Ty), ";")]
810 /// # fn main() { test().unwrap(); }
811 /// # fn test() -> Option<()> {
812 #[doc = concat!("let three = ", stringify!($Ty), "::new(3)?;")]
813 #[doc = concat!("let twenty_seven = ", stringify!($Ty), "::new(27)?;")]
814 #[doc = concat!("let max = ", stringify!($Ty), "::new(",
815 stringify!($Int), "::MAX)?;")]
817 /// assert_eq!(twenty_seven, three.saturating_pow(3));
818 /// assert_eq!(max, max.saturating_pow(3));
822 #[unstable(feature = "nonzero_ops", issue = "84186")]
824 pub const fn saturating_pow(self, other: u32) -> $Ty {
825 // SAFETY: saturating_pow returns u*::MAX on overflow
826 // so the result cannot be zero.
827 unsafe { $Ty::new_unchecked(self.get().saturating_pow(other)) }
834 // Use this when the generated code should differ between signed and unsigned types.
835 macro_rules! sign_dependent_expr {
836 (signed ? if signed { $signed_case:expr } if unsigned { $unsigned_case:expr } ) => {
839 (unsigned ? if signed { $signed_case:expr } if unsigned { $unsigned_case:expr } ) => {
844 nonzero_unsigned_signed_operations! {
845 unsigned NonZeroU8(u8);
846 unsigned NonZeroU16(u16);
847 unsigned NonZeroU32(u32);
848 unsigned NonZeroU64(u64);
849 unsigned NonZeroU128(u128);
850 unsigned NonZeroUsize(usize);
851 signed NonZeroI8(i8);
852 signed NonZeroI16(i16);
853 signed NonZeroI32(i32);
854 signed NonZeroI64(i64);
855 signed NonZeroI128(i128);
856 signed NonZeroIsize(isize);
859 macro_rules! nonzero_unsigned_is_power_of_two {
860 ( $( $Ty: ident )+ ) => {
864 /// Returns `true` if and only if `self == (1 << k)` for some `k`.
866 /// On many architectures, this function can perform better than `is_power_of_two()`
867 /// on the underlying integer type, as special handling of zero can be avoided.
874 /// #![feature(nonzero_is_power_of_two)]
876 #[doc = concat!("let eight = std::num::", stringify!($Ty), "::new(8).unwrap();")]
877 /// assert!(eight.is_power_of_two());
878 #[doc = concat!("let ten = std::num::", stringify!($Ty), "::new(10).unwrap();")]
879 /// assert!(!ten.is_power_of_two());
881 #[unstable(feature = "nonzero_is_power_of_two", issue = "81106")]
883 pub const fn is_power_of_two(self) -> bool {
884 // LLVM 11 normalizes `unchecked_sub(x, 1) & x == 0` to the implementation seen here.
885 // On the basic x86-64 target, this saves 3 instructions for the zero check.
886 // On x86_64 with BMI1, being nonzero lets it codegen to `BLSR`, which saves an instruction
887 // compared to the `POPCNT` implementation on the underlying integer type.
889 intrinsics::ctpop(self.get()) < 2
897 nonzero_unsigned_is_power_of_two! { NonZeroU8 NonZeroU16 NonZeroU32 NonZeroU64 NonZeroU128 NonZeroUsize }