Skip to main content

arbitrary_int/
signed.rs

1use crate::{
2    common::{
3        bytes_operation_impl, from_arbitrary_int_impl, from_native_impl, impl_extract,
4        impl_num_traits, impl_schemars, impl_step, impl_sum_product,
5    },
6    traits::{sealed::Sealed, BuiltinInteger, Integer, SignedInteger},
7    TryNewError,
8};
9use core::fmt::{Binary, Debug, Display, Formatter, LowerHex, Octal, UpperHex};
10use core::ops::{
11    Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
12    Mul, MulAssign, Neg, Not, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
13};
14
15macro_rules! impl_signed_integer_native {
16    ($(($type:ident, $unsigned_type:ident)),+) => {
17        $(
18            impl Sealed for $type {}
19
20            impl SignedInteger for $type {}
21
22            impl BuiltinInteger for $type {}
23
24            impl Integer for $type {
25                type UnderlyingType = $type;
26                type UnsignedInteger = $unsigned_type;
27                type SignedInteger = $type;
28
29                const BITS: usize = Self::BITS as usize;
30                const ZERO: Self = 0;
31                const MIN: Self = Self::MIN;
32                const MAX: Self = Self::MAX;
33
34                #[inline]
35                fn new(value: Self::UnderlyingType) -> Self { value }
36
37                #[inline]
38                fn try_new(value: Self::UnderlyingType) -> Result<Self, TryNewError> { Ok(value) }
39
40                #[inline]
41                fn value(self) -> Self::UnderlyingType { self }
42
43                #[inline]
44                fn from_<T: Integer>(value: T) -> Self {
45                    if T::BITS > Self::BITS as usize {
46                        assert!(value >= T::masked_new(Self::MIN) && value <= T::masked_new(Self::MAX));
47                    }
48                    Self::masked_new(value)
49                }
50
51                #[inline]
52                fn masked_new<T: Integer>(value: T) -> Self {
53                    // Primitive types don't need masking
54                    match Self::BITS {
55                        8 => value.as_i8() as Self,
56                        16 => value.as_i16() as Self,
57                        32 => value.as_i32() as Self,
58                        64 => value.as_i64() as Self,
59                        128 => value.as_i128() as Self,
60                        _ => panic!("Unhandled Integer type")
61                    }
62                }
63
64                #[inline]
65                fn as_u8(self) -> u8 { self as u8 }
66
67                #[inline]
68                fn as_u16(self) -> u16 { self as u16 }
69
70                #[inline]
71                fn as_u32(self) -> u32 { self as u32 }
72
73                #[inline]
74                fn as_u64(self) -> u64 { self as u64 }
75
76                #[inline]
77                fn as_u128(self) -> u128 { self as u128 }
78
79                #[inline]
80                fn as_usize(self) -> usize { self as usize }
81
82                #[inline]
83                fn as_i8(self) -> i8 { self as i8 }
84
85                #[inline]
86                fn as_i16(self) -> i16 { self as i16 }
87
88                #[inline]
89                fn as_i32(self) -> i32 { self as i32 }
90
91                #[inline]
92                fn as_i64(self) -> i64 { self as i64 }
93
94                #[inline]
95                fn as_i128(self) -> i128 { self as i128 }
96
97                #[inline]
98                fn as_isize(self) -> isize { self as isize }
99
100                #[inline]
101                fn to_unsigned(self) -> Self::UnsignedInteger { self as Self::UnsignedInteger }
102
103                #[inline]
104                fn from_unsigned(value: Self::UnsignedInteger) -> Self { value as Self }
105            }
106        )+
107    };
108}
109
110impl_signed_integer_native!((i8, u8), (i16, u16), (i32, u32), (i64, u64), (i128, u128));
111
112/// A signed integer of arbitrary bit length.
113///
114/// # In-Memory Representation
115/// The specific in-memory representation that would be seen by calling [`core::mem::transmute`] is unspecified.
116/// but satisfies the following guarantees:
117/// - An `Int<T, BITS>` has the same size/alignment as `T`
118/// - An `Int` has no uninitialized bytes or padding (satisfies [`bytemuck::NoUninit`]).
119/// - If the value of a `Int<T, BITS>` is non-negative,
120///   then it will the same representation as `UInt<T, BITS>`
121///
122/// When `cfg(feature = "bytemuck")` is enabled, the appropriate traits are implemented
123/// based on the above guarantees.
124/// It is not possible to implement [`bytemuck::Contiguous`] because that would be
125/// incompatible with a zero-extended memory representation.
126///
127/// Since the underlying representation is unspecified, it may change in a patch version
128/// without being considered a breaking change.
129///
130/// [`bytemuck::NoUninit`]: https://docs.rs/bytemuck/1/bytemuck/trait.NoUninit.html
131/// [`bytemuck::Contiguous`]: https://docs.rs/bytemuck/1/bytemuck/trait.Contiguous.html
132#[derive(Copy, Clone, Eq, PartialEq, Default, Ord, PartialOrd, Hash)]
133#[repr(transparent)]
134pub struct Int<T: SignedInteger + BuiltinInteger, const BITS: usize> {
135    value: T,
136}
137
138impl<T: SignedInteger + BuiltinInteger, const BITS: usize> Int<T, BITS> {
139    /// The number of bits in the underlying type that are not present in this type.
140    const UNUSED_BITS: usize = (core::mem::size_of::<T>() << 3) - Self::BITS;
141
142    pub const BITS: usize = BITS;
143
144    /// Returns the type as a fundamental data type.
145    ///
146    /// Note that if negative, the returned value may span more bits than [`BITS`](Self::BITS),
147    /// as it preserves the numeric value instead of the bitwise value:
148    ///
149    /// ```
150    /// # use arbitrary_int::i3;
151    /// let value: i8 = i3::new(-1).value();
152    /// assert_eq!(value, -1);
153    /// assert_eq!(value.count_ones(), 8);
154    /// ```
155    ///
156    /// If you need a value within the specified bit range, use [`Self::to_bits`].
157    #[cfg(not(feature = "hint"))]
158    #[inline]
159    pub const fn value(self) -> T {
160        self.value
161    }
162
163    /// Initializes a new value without checking the bounds
164    ///
165    /// # Safety
166    ///
167    /// Must only be called with a value bigger or equal to [`Self::MIN`] and less than or equal to [`Self::MAX`].
168    #[inline]
169    pub const unsafe fn new_unchecked(value: T) -> Self {
170        Self { value }
171    }
172}
173
174macro_rules! int_impl_num {
175    ($(($type:ident, $unsigned_type:ident)),+) => {
176        $(
177            impl<const BITS: usize> Sealed for Int<$type, BITS> {}
178
179            impl<const BITS: usize> SignedInteger for Int<$type, BITS> {}
180
181            impl<const BITS: usize> Integer for Int<$type, BITS> {
182                type UnderlyingType = $type;
183                type SignedInteger = Self;
184                type UnsignedInteger = crate::UInt<$unsigned_type, BITS>;
185
186                const BITS: usize = BITS;
187
188                const ZERO: Self = Self { value: 0 };
189
190                const MIN: Self = Self { value: -Self::MAX.value - 1 };
191
192                // The existence of MAX also serves as a bounds check: If NUM_BITS is > available bits,
193                // we will get a compiler error right here
194                const MAX: Self = Self {
195                    // MAX is always positive so we don't have to worry about the sign
196                    value: (<$type as Integer>::MAX >> (<$type as Integer>::BITS - Self::BITS)),
197                };
198
199                #[inline]
200                fn try_new(value: Self::UnderlyingType) -> Result<Self, TryNewError> {
201                    if value >= Self::MIN.value && value <= Self::MAX.value {
202                        Ok(Self { value })
203                    } else {
204                        Err(TryNewError{})
205                    }
206                }
207
208                #[inline]
209                fn new(value: $type) -> Self {
210                    assert!(value >= Self::MIN.value && value <= Self::MAX.value);
211
212                    Self { value }
213                }
214
215                #[inline]
216                fn from_<T: Integer>(value: T) -> Self {
217                    if Self::BITS < T::BITS {
218                        assert!(value >= Self::MIN.value.as_() && value <= Self::MAX.value.as_());
219                    }
220                    Self { value: Self::UnderlyingType::masked_new(value) }
221                }
222
223                fn masked_new<T: Integer>(value: T) -> Self {
224                    // If the source type is wider, we need to mask and sign-extend. If the source
225                    // type is the same width but unsigned, we also need to sign-extend!
226                    if Self::BITS < T::BITS || (Self::BITS == T::BITS && T::MIN == T::ZERO) {
227                        let value = (value.as_::<Self::UnderlyingType>() << Self::UNUSED_BITS) >> Self::UNUSED_BITS;
228                        Self { value: Self::UnderlyingType::masked_new(value) }
229                    } else {
230                        Self { value: Self::UnderlyingType::masked_new(value) }
231                    }
232                }
233
234                fn as_u8(self) -> u8 {
235                    self.value() as _
236                }
237
238                fn as_u16(self) -> u16 {
239                    self.value() as _
240                }
241
242                fn as_u32(self) -> u32 {
243                    self.value() as _
244                }
245
246                fn as_u64(self) -> u64 {
247                    self.value() as _
248                }
249
250                fn as_u128(self) -> u128 {
251                    self.value() as _
252                }
253
254                fn as_usize(self) -> usize {
255                    self.value() as _
256                }
257
258                fn as_i8(self) -> i8 {
259                    self.value() as _
260                }
261
262                fn as_i16(self) -> i16 {
263                    self.value() as _
264                }
265
266                fn as_i32(self) -> i32 {
267                    self.value() as _
268                }
269
270                fn as_i64(self) -> i64 {
271                    self.value() as _
272                }
273
274                fn as_i128(self) -> i128 {
275                    self.value() as _
276                }
277
278                fn as_isize(self) -> isize {
279                    self.value() as _
280                }
281
282                #[inline]
283                fn to_unsigned(self) -> Self::UnsignedInteger { Self::UnsignedInteger::masked_new(self.value as $unsigned_type) }
284
285                #[inline]
286                fn from_unsigned(value: Self::UnsignedInteger) -> Self {
287                    Self::masked_new(value.value() as $type)
288                }
289
290
291                #[inline]
292                fn value(self) -> $type {
293                    #[cfg(feature = "hint")]
294                    unsafe {
295                        core::hint::assert_unchecked(self.value >= Self::MIN.value);
296                        core::hint::assert_unchecked(self.value <= Self::MAX.value);
297                    }
298
299                    self.value
300                }
301            }
302        )+
303    };
304}
305
306int_impl_num!((i8, u8), (i16, u16), (i32, u32), (i64, u64), (i128, u128));
307
308macro_rules! int_impl {
309    ($(($type:ident, $unsigned_type:ident, doctest = $doctest_attr:literal)),+) => {
310        $(
311            impl<const BITS: usize> Int<$type, BITS> {
312                pub const MASK: $type = (Self::MAX.value << 1) | 1;
313
314                /// Creates an instance. Panics if the given value is outside of the valid range
315                #[inline]
316                pub const fn new(value: $type) -> Self {
317                    assert!(value >= Self::MIN.value && value <= Self::MAX.value);
318
319                    Self { value }
320                }
321
322                /// Creates an instance. Panics if the given value is outside of the valid range
323                #[inline]
324                pub const fn from_i8(value: i8) -> Self {
325                    if Self::BITS < 8 {
326                        assert!(value >= Self::MIN.value as i8 && value <= Self::MAX.value as i8);
327                    }
328                    Self { value: value as $type }
329                }
330
331                /// Creates an instance. Panics if the given value is outside of the valid range
332                #[inline]
333                pub const fn from_i16(value: i16) -> Self {
334                    if Self::BITS < 16 {
335                        assert!(value >= Self::MIN.value as i16 && value <= Self::MAX.value as i16);
336                    }
337                    Self { value: value as $type }
338                }
339
340                /// Creates an instance. Panics if the given value is outside of the valid range
341                #[inline]
342                pub const fn from_i32(value: i32) -> Self {
343                    if Self::BITS < 32 {
344                        assert!(value >= Self::MIN.value as i32 && value <= Self::MAX.value as i32);
345                    }
346                    Self { value: value as $type }
347                }
348
349                /// Creates an instance. Panics if the given value is outside of the valid range
350                #[inline]
351                pub const fn from_i64(value: i64) -> Self {
352                    if Self::BITS < 64 {
353                        assert!(value >= Self::MIN.value as i64 && value <= Self::MAX.value as i64);
354                    }
355                    Self { value: value as $type }
356                }
357
358                /// Creates an instance. Panics if the given value is outside of the valid range
359                #[inline]
360                pub const fn from_i128(value: i128) -> Self {
361                    if Self::BITS < 128 {
362                        assert!(value >= Self::MIN.value as i128 && value <= Self::MAX.value as i128);
363                    }
364                    Self { value: value as $type }
365                }
366
367                /// Creates an instance or an error if the given value is outside of the valid range
368                #[inline]
369                pub const fn try_new(value: $type) -> Result<Self, TryNewError> {
370                    if value >= Self::MIN.value && value <= Self::MAX.value {
371                        Ok(Self { value })
372                    } else {
373                        Err(TryNewError {})
374                    }
375                }
376
377                /// Returns the bitwise representation of the value.
378                ///
379                /// As the bit width is limited to [`BITS`](Self::BITS) the numeric value may differ from [`value`](Self::value).
380                ///
381                #[doc = concat!(" ```", $doctest_attr)]
382                /// # use arbitrary_int::i3;
383                /// let value = i3::new(-1);
384                /// assert_eq!(value.to_bits(), 0b111); // 7
385                /// assert_eq!(value.value(), -1);
386                /// ```
387                ///
388                /// To convert from the bitwise representation back to an instance, use [`from_bits`](Self::from_bits).
389                #[inline]
390                #[must_use = "this returns the result of the operation, without modifying the original"]
391                pub const fn to_bits(self) -> $unsigned_type {
392                    (self.value() & Self::MASK) as $unsigned_type
393                }
394
395                /// Convert the bitwise representation from [`to_bits`](Self::to_bits) to an instance.
396                ///
397                #[doc = concat!(" ```", $doctest_attr)]
398                /// # use arbitrary_int::i3;
399                /// let value = i3::from_bits(0b111);
400                /// assert_eq!(value.value(), -1);
401                /// assert_eq!(value.to_bits(), 0b111);
402                /// ```
403                ///
404                /// If you want to convert a numeric value to an instance instead, use [`new`](Self::new).
405                ///
406                /// # Panics
407                ///
408                /// Panics if the given value exceeds the bit width specified by [`BITS`](Self::BITS).
409                #[inline]
410                pub const fn from_bits(value: $unsigned_type) -> Self {
411                    assert!(value & (!Self::MASK as $unsigned_type) == 0);
412
413                    // First do a logical left shift to put the sign bit at the underlying type's MSB (copying the sign),
414                    // then an arithmetic right shift to sign-extend the value into its original position.
415                    Self { value: ((value << Self::UNUSED_BITS) as $type) >> Self::UNUSED_BITS }
416                }
417
418                /// Tries to convert the bitwise representation from [`to_bits`](Self::to_bits) to an instance.
419                ///
420                #[doc = concat!(" ```", $doctest_attr)]
421                /// # use arbitrary_int::i3;
422                /// i3::try_from_bits(0b1111).expect_err("value is > 3 bits");
423                /// let value = i3::try_from_bits(0b111).expect("value is <= 3 bits");
424                /// assert_eq!(value.value(), -1);
425                /// assert_eq!(value.to_bits(), 0b111);
426                /// ```
427                ///
428                /// If you want to convert a numeric value to an instance instead, use [`try_new`](Self::try_new).
429                ///
430                /// # Errors
431                ///
432                /// Returns an error if the given value exceeds the bit width specified by [`BITS`](Self::BITS).
433                #[inline]
434                pub const fn try_from_bits(value: $unsigned_type) -> Result<Self, TryNewError> {
435                    if value & (!Self::MASK as $unsigned_type) == 0 {
436                        // First do a logical left shift to put the sign bit at the underlying type's MSB (copying the sign),
437                        // then an arithmetic right shift to sign-extend the value into its original position.
438                        Ok(Self { value: ((value << Self::UNUSED_BITS) as $type) >> Self::UNUSED_BITS })
439                    } else {
440                        Err(TryNewError {})
441                    }
442                }
443
444                /// Converts the bitwise representation from [`to_bits`](Self::to_bits) to an instance,
445                /// without checking the bounds.
446                ///
447                /// # Safety
448                ///
449                /// The given value must not exceed the bit width specified by [`Self::BITS`].
450                #[inline]
451                pub const unsafe fn from_bits_unchecked(value: $unsigned_type) -> Self {
452                    // First do a logical left shift to put the sign bit at the underlying type's MSB (copying the sign),
453                    // then an arithmetic right shift to sign-extend the value into its original position.
454                    Self { value: ((value << Self::UNUSED_BITS) as $type) >> Self::UNUSED_BITS }
455                }
456
457                /// Returns the type as a fundamental data type.
458                ///
459                /// Note that if negative, the returned value may span more bits than [`BITS`](Self::BITS)
460                /// as it preserves the numeric value instead of the bitwise value:
461                ///
462                #[doc = concat!(" ```", $doctest_attr)]
463                /// # use arbitrary_int::i3;
464                /// let value: i8 = i3::new(-1).value();
465                /// assert_eq!(value, -1);
466                /// assert_eq!(value.count_ones(), 8);
467                /// ```
468                ///
469                /// If you need a value within the specified bit range, use [`to_bits`](Self::to_bits).
470                #[cfg(feature = "hint")]
471                #[inline]
472                pub const fn value(self) -> $type {
473                    // The hint feature requires the type to be const-comparable,
474                    // which isn't possible in the generic version above. So we have
475                    // an entirely different function if this feature is enabled.
476                    // It only works for primitive types, which should be ok in practice
477                    // (but is technically an API change)
478                    unsafe {
479                        core::hint::assert_unchecked(self.value >= Self::MIN.value);
480                        core::hint::assert_unchecked(self.value <= Self::MAX.value);
481                    }
482                    self.value
483                }
484
485                // Generate the `extract_{i,u}{8,16,32,64,128}` functions.
486                impl_extract!(
487                    $type,
488                    "from_bits(value >> start_bit)",
489                    |value| (value << Self::UNUSED_BITS) >> Self::UNUSED_BITS,
490
491                    (8, (i8, extract_i8), (u8, extract_u8)),
492                    (16, (i16, extract_i16), (u16, extract_u16)),
493                    (32, (i32, extract_i32), (u32, extract_u32)),
494                    (64, (i64, extract_i64), (u64, extract_u64)),
495                    (128, (i128, extract_i128), (u128, extract_u128))
496                );
497
498                /// Returns an [`Int`] with a wider bit depth but with the same base data type
499                #[inline]
500                #[must_use = "this returns the result of the operation, without modifying the original"]
501                pub const fn widen<const BITS_RESULT: usize>(self) -> Int<$type, BITS_RESULT> {
502                    const { assert!(BITS < BITS_RESULT, "Can not call widen() with the given bit widths") };
503
504                    // Query MAX of the result to ensure we get a compiler error if the current definition is bogus (e.g. <u8, 9>)
505                    let _ = Int::<$type, BITS_RESULT>::MAX;
506                    Int::<$type, BITS_RESULT> { value: self.value }
507                }
508
509                /// Wrapping (modular) addition. Computes `self + rhs`, wrapping around at the
510                /// boundary of the type.
511                ///
512                /// # Examples
513                ///
514                /// Basic usage:
515                ///
516                #[doc = concat!(" ```", $doctest_attr)]
517                /// # use arbitrary_int::prelude::*;
518                /// assert_eq!(i14::new(100).wrapping_add(i14::new(27)), i14::new(127));
519                /// assert_eq!(i14::MAX.wrapping_add(i14::new(2)), i14::MIN + i14::new(1));
520                /// ```
521                #[inline]
522                #[must_use = "this returns the result of the operation, without modifying the original"]
523                pub const fn wrapping_add(self, rhs: Self) -> Self {
524                    let sum = self.value().wrapping_add(rhs.value());
525                    Self {
526                        value: (sum << Self::UNUSED_BITS) >> Self::UNUSED_BITS,
527                    }
528                }
529
530                /// Wrapping (modular) subtraction. Computes `self - rhs`, wrapping around at the
531                /// boundary of the type.
532                ///
533                /// # Examples
534                ///
535                /// Basic usage:
536                ///
537                #[doc = concat!(" ```", $doctest_attr)]
538                /// # use arbitrary_int::prelude::*;
539                /// assert_eq!(i14::new(0).wrapping_sub(i14::new(127)), i14::new(-127));
540                /// assert_eq!(i14::new(-2).wrapping_sub(i14::MAX), i14::MAX);
541                /// ```
542                #[inline]
543                #[must_use = "this returns the result of the operation, without modifying the original"]
544                pub const fn wrapping_sub(self, rhs: Self) -> Self {
545                    let sum = self.value().wrapping_sub(rhs.value());
546                    Self {
547                        value: (sum << Self::UNUSED_BITS) >> Self::UNUSED_BITS,
548                    }
549                }
550
551                /// Wrapping (modular) multiplication. Computes `self * rhs`, wrapping around at the
552                /// boundary of the type.
553                ///
554                /// # Examples
555                ///
556                /// Basic usage:
557                ///
558                #[doc = concat!(" ```", $doctest_attr)]
559                /// # use arbitrary_int::prelude::*;
560                /// assert_eq!(i14::new(10).wrapping_mul(i14::new(12)), i14::new(120));
561                /// assert_eq!(i14::new(12).wrapping_mul(i14::new(1024)), i14::new(-4096));
562                /// ```
563                #[inline]
564                #[must_use = "this returns the result of the operation, without modifying the original"]
565                pub const fn wrapping_mul(self, rhs: Self) -> Self {
566                    let sum = self.value().wrapping_mul(rhs.value());
567                    Self {
568                        value: (sum << Self::UNUSED_BITS) >> Self::UNUSED_BITS,
569                    }
570                }
571
572                /// Wrapping (modular) division. Computes `self / rhs`, wrapping around at the
573                /// boundary of the type.
574                ///
575                /// The only case where such wrapping can occur is when one divides `MIN / -1` on a
576                /// signed type (where `MIN` is the negative minimal value for the type); this is
577                /// equivalent to `-MIN`, a positive value that is too large to represent in the type.
578                /// In such a case, this function returns `MIN` itself.
579                ///
580                /// # Panics
581                ///
582                /// This function will panic if `rhs` is zero.
583                ///
584                /// # Examples
585                ///
586                /// Basic usage:
587                ///
588                #[doc = concat!(" ```", $doctest_attr)]
589                /// # use arbitrary_int::prelude::*;
590                /// assert_eq!(i14::new(100).wrapping_div(i14::new(10)), i14::new(10));
591                /// assert_eq!(i14::MIN.wrapping_div(i14::new(-1)), i14::MIN);
592                /// ```
593                #[inline]
594                #[must_use = "this returns the result of the operation, without modifying the original"]
595                pub const fn wrapping_div(self, rhs: Self) -> Self {
596                    let sum = self.value().wrapping_div(rhs.value());
597                    Self {
598                        // Unlike the unsigned implementation we do need to account for overflow here,
599                        // `Self::MIN / -1` is equal to `Self::MAX + 1`.
600                        value: (sum << Self::UNUSED_BITS) >> Self::UNUSED_BITS,
601                    }
602                }
603
604                /// Wrapping (modular) negation. Computes `-self`, wrapping around at the boundary of the type.
605                ///
606                /// The only case where such wrapping can occur is when one negates `MIN` on a signed type
607                /// (where `MIN` is the negative minimal value for the type); this is a positive value that is
608                /// too large to represent in the type. In such a case, this function returns `MIN` itself.
609                ///
610                /// # Examples
611                ///
612                /// Basic usage:
613                ///
614                #[doc = concat!(" ```", $doctest_attr)]
615                /// # use arbitrary_int::prelude::*;
616                /// assert_eq!(i14::new(100).wrapping_neg(), i14::new(-100));
617                /// assert_eq!(i14::new(-100).wrapping_neg(), i14::new(100));
618                /// assert_eq!(i14::MIN.wrapping_neg(), i14::MIN);
619                /// ```
620                #[inline]
621                #[must_use = "this returns the result of the operation, without modifying the original"]
622                pub const fn wrapping_neg(self) -> Self {
623                    let value = (self.value().wrapping_neg() << Self::UNUSED_BITS) >> Self::UNUSED_BITS;
624                    Self { value }
625                }
626
627                /// Panic-free bitwise shift-left; yields `self << mask(rhs)`, where mask removes any
628                /// high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type.
629                ///
630                /// Note that this is not the same as a rotate-left; the RHS of a wrapping shift-left is
631                /// restricted to the range of the type, rather than the bits shifted out of the LHS being
632                /// returned to the other end.
633                /// A [`rotate_left`](Self::rotate_left) function exists as well, which may be what you
634                /// want instead.
635                ///
636                /// # Examples
637                ///
638                /// Basic usage:
639                ///
640                #[doc = concat!(" ```", $doctest_attr)]
641                /// # use arbitrary_int::prelude::*;
642                /// assert_eq!(i14::new(-1).wrapping_shl(7), i14::new(-128));
643                /// assert_eq!(i14::new(-1).wrapping_shl(128), i14::new(-4));
644                /// ```
645                #[inline]
646                #[must_use = "this returns the result of the operation, without modifying the original"]
647                pub const fn wrapping_shl(self, rhs: u32) -> Self {
648                    // modulo is expensive on some platforms, so only do it when necessary
649                    let shift_amount = Self::UNUSED_BITS as u32 + (if rhs >= BITS as u32 {
650                        rhs % (BITS as u32)
651                    } else {
652                        rhs
653                    });
654
655                    Self {
656                        // We could use wrapping_shl here to make Debug builds slightly smaller;
657                        // the downside would be that on weird CPUs that don't do wrapping_shl by
658                        // default release builds would get slightly worse. Using << should give
659                        // good release performance everywere
660                        value: (self.value() << shift_amount) >> Self::UNUSED_BITS,
661                    }
662                }
663
664                /// Panic-free bitwise shift-right; yields `self >> mask(rhs)`, where mask removes any
665                /// high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type.
666                ///
667                /// Note that this is not the same as a rotate-right; the RHS of a wrapping shift-right is
668                /// restricted to the range of the type, rather than the bits shifted out of the LHS being
669                /// returned to the other end.
670                /// A [`rotate_right`](Self::rotate_right) function exists as well, which may be what you
671                /// want instead.
672                ///
673                /// # Examples
674                ///
675                /// Basic usage:
676                ///
677                #[doc = concat!(" ```", $doctest_attr)]
678                /// # use arbitrary_int::prelude::*;
679                /// assert_eq!(i14::new(-128).wrapping_shr(7), i14::new(-1));
680                /// assert_eq!(i14::new(-128).wrapping_shr(60), i14::new(-8));
681                /// ```
682                #[inline]
683                #[must_use = "this returns the result of the operation, without modifying the original"]
684                pub const fn wrapping_shr(self, rhs: u32) -> Self {
685                    // modulo is expensive on some platforms, so only do it when necessary
686                    let shift_amount = if rhs >= (BITS as u32) {
687                        rhs % (BITS as u32)
688                    } else {
689                        rhs
690                    };
691
692                    Self {
693                        value: (self.value() >> shift_amount),
694                    }
695                }
696
697                /// Saturating integer addition. Computes `self + rhs`, saturating at the numeric
698                /// bounds instead of overflowing.
699                ///
700                /// # Examples
701                ///
702                /// Basic usage:
703                ///
704                #[doc = concat!(" ```", $doctest_attr)]
705                /// # use arbitrary_int::prelude::*;
706                /// assert_eq!(i14::new(100).saturating_add(i14::new(1)), i14::new(101));
707                /// assert_eq!(i14::MAX.saturating_add(i14::new(100)), i14::MAX);
708                /// assert_eq!(i14::MIN.saturating_add(i14::new(-1)), i14::MIN);
709                /// ```
710                #[inline]
711                #[must_use = "this returns the result of the operation, without modifying the original"]
712                pub const fn saturating_add(self, rhs: Self) -> Self {
713                    if Self::UNUSED_BITS == 0 {
714                        // We are something like a Int::<i8; 8>, we can fallback to the base implementation.
715                        // This is very unlikely to happen in practice, but checking allows us to use
716                        // `wrapping_add` instead of `saturating_add` in the common case, which is faster.
717                        let value = self.value().saturating_add(rhs.value());
718                        Self { value }
719                    } else {
720                        // We're dealing with fewer bits than the underlying type (e.g. i7).
721                        // That means the addition can never overflow the underlying type.
722                        let value = self.value().wrapping_add(rhs.value());
723                        if value > Self::MAX.value {
724                            Self::MAX
725                        } else if value < Self::MIN.value {
726                            Self::MIN
727                        } else {
728                            Self { value }
729                        }
730                    }
731                }
732
733                /// Saturating integer subtraction. Computes `self - rhs`, saturating at the numeric
734                /// bounds instead of overflowing.
735                ///
736                /// # Examples
737                ///
738                /// Basic usage:
739                ///
740                #[doc = concat!(" ```", $doctest_attr)]
741                /// # use arbitrary_int::prelude::*;
742                /// assert_eq!(i14::new(100).saturating_sub(i14::new(127)), i14::new(-27));
743                /// assert_eq!(i14::MIN.saturating_sub(i14::new(100)), i14::MIN);
744                /// assert_eq!(i14::MAX.saturating_sub(i14::new(-1)), i14::MAX);
745                /// ```
746                #[inline]
747                #[must_use = "this returns the result of the operation, without modifying the original"]
748                pub const fn saturating_sub(self, rhs: Self) -> Self {
749                    if Self::UNUSED_BITS == 0 {
750                        // We are something like a Int::<i8; 8>, we can fallback to the base implementation.
751                        // This is very unlikely to happen in practice, but checking allows us to use
752                        // `wrapping_sub` instead of `saturating_sub` in the common case, which is faster.
753                        let value = self.value().saturating_sub(rhs.value());
754                        Self { value }
755                    } else {
756                        // We're dealing with fewer bits than the underlying type (e.g. i7).
757                        // That means the subtraction can never overflow the underlying type.
758                        let value = self.value().wrapping_sub(rhs.value());
759                        if value > Self::MAX.value {
760                            Self::MAX
761                        } else if value < Self::MIN.value {
762                            Self::MIN
763                        } else {
764                            Self { value }
765                        }
766                    }
767                }
768
769                /// Saturating integer multiplication. Computes `self * rhs`, saturating at the numeric
770                /// bounds instead of overflowing.
771                ///
772                /// # Examples
773                ///
774                /// Basic usage:
775                ///
776                #[doc = concat!(" ```", $doctest_attr)]
777                /// # use arbitrary_int::prelude::*;
778                /// assert_eq!(i14::new(10).saturating_mul(i14::new(12)), i14::new(120));
779                /// assert_eq!(i14::MAX.saturating_mul(i14::new(10)), i14::MAX);
780                /// assert_eq!(i14::MIN.saturating_mul(i14::new(10)), i14::MIN);
781                /// ```
782                #[inline]
783                #[must_use = "this returns the result of the operation, without modifying the original"]
784                pub const fn saturating_mul(self, rhs: Self) -> Self {
785                    let value = if (BITS << 1) <= (core::mem::size_of::<$type>() << 3) {
786                        // We have half the bits (e.g. i4 * i4) of the base type, so we can't overflow the base type
787                        // `wrapping_mul` likely provides the best performance on all cpus
788                        self.value().wrapping_mul(rhs.value())
789                    } else {
790                        // We have more than half the bits (e.g. i6 * i6)
791                        self.value().saturating_mul(rhs.value())
792                    };
793
794                    if value > Self::MAX.value {
795                        Self::MAX
796                    } else if value < Self::MIN.value {
797                        Self::MIN
798                    } else {
799                        Self { value }
800                    }
801                }
802
803                /// Saturating integer division. Computes `self / rhs`, saturating at the numeric
804                /// bounds instead of overflowing.
805                ///
806                /// # Panics
807                ///
808                /// This function will panic if rhs is zero.
809                ///
810                /// # Examples
811                ///
812                /// Basic usage:
813                ///
814                #[doc = concat!(" ```", $doctest_attr)]
815                /// # use arbitrary_int::prelude::*;
816                /// assert_eq!(i14::new(5).saturating_div(i14::new(2)), i14::new(2));
817                /// assert_eq!(i14::MAX.saturating_div(i14::new(-1)), i14::MIN + i14::new(1));
818                /// assert_eq!(i14::MIN.saturating_div(i14::new(-1)), i14::MAX);
819                /// ```
820                #[inline]
821                #[must_use = "this returns the result of the operation, without modifying the original"]
822                pub const fn saturating_div(self, rhs: Self) -> Self {
823                    // As `Self::MIN / -1` is equal to `Self::MAX + 1` we always need to check for overflow.
824                    let value = self.value().saturating_div(rhs.value());
825
826                    if value > Self::MAX.value {
827                        Self::MAX
828                    } else if value < Self::MIN.value {
829                        Self::MIN
830                    } else {
831                        Self { value }
832                    }
833                }
834
835                /// Saturating integer negation. Computes `-self`, returning `MAX` if `self == MIN`
836                /// instead of overflowing.
837                ///
838                /// # Examples
839                ///
840                /// Basic usage:
841                ///
842                #[doc = concat!(" ```", $doctest_attr)]
843                /// # use arbitrary_int::prelude::*;
844                /// assert_eq!(i14::new(100).saturating_neg(), i14::new(-100));
845                /// assert_eq!(i14::new(-100).saturating_neg(), i14::new(100));
846                /// assert_eq!(i14::MIN.saturating_neg(), i14::MAX);
847                /// assert_eq!(i14::MAX.saturating_neg(), i14::MIN + i14::new(1));
848                /// ```
849                #[inline]
850                #[must_use = "this returns the result of the operation, without modifying the original"]
851                pub const fn saturating_neg(self) -> Self {
852                    if self.value() == Self::MIN.value() {
853                        Self::MAX
854                    } else {
855                        // It is not possible for this to wrap as we've already checked for `MIN`.
856                        let value = self.value().wrapping_neg();
857                        Self { value }
858                    }
859                }
860
861                /// Saturating integer exponentiation. Computes `self.pow(exp)`, saturating at the numeric
862                /// bounds instead of overflowing.
863                ///
864                /// # Examples
865                ///
866                /// Basic usage:
867                ///
868                #[doc = concat!(" ```", $doctest_attr)]
869                /// # use arbitrary_int::prelude::*;
870                /// assert_eq!(i14::new(-4).saturating_pow(3), i14::new(-64));
871                /// assert_eq!(i14::MIN.saturating_pow(2), i14::MAX);
872                /// assert_eq!(i14::MIN.saturating_pow(3), i14::MIN);
873                /// ```
874                #[inline]
875                #[must_use = "this returns the result of the operation, without modifying the original"]
876                pub const fn saturating_pow(self, exp: u32) -> Self {
877                    // It might be possible to handwrite this to be slightly faster as both
878                    // `saturating_pow` has to do a bounds-check and then we do second one.
879                    let value = self.value().saturating_pow(exp);
880
881                    if value > Self::MAX.value {
882                        Self::MAX
883                    } else if value < Self::MIN.value {
884                        Self::MIN
885                    } else {
886                        Self { value }
887                    }
888                }
889
890                /// Checked integer addition. Computes `self + rhs`, returning `None` if overflow occurred.
891                ///
892                /// # Examples
893                ///
894                /// Basic usage:
895                ///
896                #[doc = concat!(" ```", $doctest_attr)]
897                /// # use arbitrary_int::prelude::*;
898                /// assert_eq!((i14::MAX - i14::new(2)).checked_add(i14::new(1)), Some(i14::MAX - i14::new(1)));
899                /// assert_eq!((i14::MAX - i14::new(2)).checked_add(i14::new(3)), None);
900                /// ```
901                #[inline]
902                #[must_use = "this returns the result of the operation, without modifying the original"]
903                pub const fn checked_add(self, rhs: Self) -> Option<Self> {
904                    if Self::UNUSED_BITS == 0 {
905                        // We are something like a Int::<i8; 8>, we can fallback to the base implementation.
906                        // This is very unlikely to happen in practice, but checking allows us to use
907                        // `wrapping_add` instead of `checked_add` in the common case, which is faster.
908                        match self.value().checked_add(rhs.value()) {
909                            Some(value) => Some(Self { value }),
910                            None => None
911                        }
912                    } else {
913                        // We're dealing with fewer bits than the underlying type (e.g. i7).
914                        // That means the addition can never overflow the underlying type
915                        let value = self.value().wrapping_add(rhs.value());
916                        if value < Self::MIN.value() || value > Self::MAX.value() {
917                            None
918                        } else {
919                            Some(Self { value })
920                        }
921                    }
922                }
923
924                /// Checked integer subtraction. Computes `self - rhs`, returning `None` if overflow occurred.
925                ///
926                /// # Examples
927                ///
928                /// Basic usage:
929                ///
930                #[doc = concat!(" ```", $doctest_attr)]
931                /// # use arbitrary_int::prelude::*;
932                /// assert_eq!((i14::MIN + i14::new(2)).checked_sub(i14::new(1)), Some(i14::MIN + i14::new(1)));
933                /// assert_eq!((i14::MIN + i14::new(2)).checked_sub(i14::new(3)), None);
934                /// ```
935                #[inline]
936                #[must_use = "this returns the result of the operation, without modifying the original"]
937                pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
938                    if Self::UNUSED_BITS == 0 {
939                        // We are something like a Int::<i8; 8>, we can fallback to the base implementation.
940                        // This is very unlikely to happen in practice, but checking allows us to use
941                        // `wrapping_sub` instead of `checked_sub` in the common case, which is faster.
942                        match self.value().checked_sub(rhs.value()) {
943                            Some(value) => Some(Self { value }),
944                            None => None
945                        }
946                    } else {
947                        // We're dealing with fewer bits than the underlying type (e.g. i7).
948                        // That means the subtraction can never overflow the underlying type
949                        let value = self.value().wrapping_sub(rhs.value());
950                        if value < Self::MIN.value() || value > Self::MAX.value() {
951                            None
952                        } else {
953                            Some(Self { value })
954                        }
955                    }
956                }
957
958                /// Checked integer multiplication. Computes `self * rhs`, returning `None` if overflow occurred.
959                ///
960                /// # Examples
961                ///
962                /// Basic usage:
963                ///
964                #[doc = concat!(" ```", $doctest_attr)]
965                /// # use arbitrary_int::prelude::*;
966                /// assert_eq!(i14::MAX.checked_mul(i14::new(1)), Some(i14::MAX));
967                /// assert_eq!(i14::MAX.checked_mul(i14::new(2)), None);
968                /// ```
969                #[inline]
970                #[must_use = "this returns the result of the operation, without modifying the original"]
971                pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
972                    let product = if (BITS << 1) <= (core::mem::size_of::<$type>() << 3) {
973                        // We have half the bits (e.g. `i4 * i4`) of the base type, so we can't overflow the base type.
974                        // `wrapping_mul` likely provides the best performance on all CPUs.
975                        Some(self.value().wrapping_mul(rhs.value()))
976                    } else {
977                        // We have more than half the bits (e.g. u6 * u6)
978                        self.value().checked_mul(rhs.value())
979                    };
980
981                    match product {
982                        Some(value) if value >= Self::MIN.value() && value <= Self::MAX.value() => {
983                            Some(Self { value })
984                        }
985                        _ => None
986                    }
987                }
988
989                /// Checked integer division. Computes `self / rhs`, returning `None` if `rhs == 0`
990                /// or the division results in overflow.
991                ///
992                /// # Examples
993                ///
994                /// Basic usage:
995                ///
996                #[doc = concat!(" ```", $doctest_attr)]
997                /// # use arbitrary_int::prelude::*;
998                /// assert_eq!((i14::MIN + i14::new(1)).checked_div(i14::new(-1)), Some(i14::new(8191)));
999                /// assert_eq!(i14::MIN.checked_div(i14::new(-1)), None);
1000                /// assert_eq!((i14::new(1)).checked_div(i14::new(0)), None);
1001                /// ```
1002                #[inline]
1003                #[must_use = "this returns the result of the operation, without modifying the original"]
1004                pub const fn checked_div(self, rhs: Self) -> Option<Self> {
1005                    // `checked_div` from the underlying type already catches division by zero,
1006                    // and the only way this can overflow is with `MIN / -1` (which equals `MAX + 1`).
1007                    // Because of this we only need to check if the value is larger than `MAX`.
1008                    match self.value().checked_div(rhs.value()) {
1009                        Some(value) if value <= Self::MAX.value() => Some(Self { value }),
1010                        _ => None
1011                    }
1012                }
1013
1014                /// Checked negation. Computes `-self`, returning `None` if `self == MIN`.
1015                ///
1016                /// # Examples
1017                ///
1018                /// Basic usage:
1019                ///
1020                #[doc = concat!(" ```", $doctest_attr)]
1021                /// # use arbitrary_int::prelude::*;
1022                /// assert_eq!(i14::new(5).checked_neg(), Some(i14::new(-5)));
1023                /// assert_eq!(i14::MIN.checked_neg(), None);
1024                /// ```
1025                pub const fn checked_neg(self) -> Option<Self> {
1026                    if self.value() == Self::MIN.value() {
1027                        None
1028                    } else {
1029                        // It is not possible for this to wrap as we've already checked for `MIN`.
1030                        let value = self.value().wrapping_neg();
1031                        Some(Self { value })
1032                    }
1033                }
1034
1035                /// Checked shift left. Computes `self << rhs`, returning `None` if `rhs` is larger than or
1036                /// equal to the number of bits in `self`.
1037                ///
1038                /// # Examples
1039                ///
1040                /// Basic usage:
1041                ///
1042                #[doc = concat!(" ```", $doctest_attr)]
1043                /// # use arbitrary_int::i14;
1044                /// assert_eq!(i14::new(0x1).checked_shl(4), Some(i14::new(0x10)));
1045                /// assert_eq!(i14::new(0x1).checked_shl(129), None);
1046                /// assert_eq!(i14::new(0x10).checked_shl(13), Some(i14::new(0)));
1047                /// ```
1048                #[inline]
1049                #[must_use = "this returns the result of the operation, without modifying the original"]
1050                pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
1051                    if rhs >= (BITS as u32) {
1052                        None
1053                    } else {
1054                        let value = ((self.value() << rhs) << Self::UNUSED_BITS) >> Self::UNUSED_BITS;
1055                        Some(Self { value })
1056                    }
1057                }
1058
1059                /// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is larger than
1060                /// or equal to the number of bits in `self`.
1061                ///
1062                /// # Examples
1063                ///
1064                /// Basic usage:
1065                ///
1066                #[doc = concat!(" ```", $doctest_attr)]
1067                /// # use arbitrary_int::i14;
1068                /// assert_eq!(i14::new(0x10).checked_shr(4), Some(i14::new(0x1)));
1069                /// assert_eq!(i14::new(0x10).checked_shr(129), None);
1070                /// ```
1071                #[inline]
1072                #[must_use = "this returns the result of the operation, without modifying the original"]
1073                pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
1074                    if rhs >= (BITS as u32) {
1075                        None
1076                    } else {
1077                        let value = ((self.value() >> rhs) << Self::UNUSED_BITS) >> Self::UNUSED_BITS;
1078                        Some(Self { value })
1079                    }
1080                }
1081
1082                /// Calculates `self + rhs`.
1083                ///
1084                /// Returns a tuple of the addition along with a boolean indicating whether an arithmetic
1085                /// overflow would occur. If an overflow would have occurred then the wrapped value is returned.
1086                ///
1087                /// # Examples
1088                ///
1089                /// Basic usage:
1090                ///
1091                #[doc = concat!(" ```", $doctest_attr)]
1092                /// # use arbitrary_int::prelude::*;
1093                /// assert_eq!(i14::new(5).overflowing_add(i14::new(2)), (i14::new(7), false));
1094                /// assert_eq!(i14::MAX.overflowing_add(i14::new(1)), (i14::MIN, true));
1095                /// ```
1096                #[inline]
1097                #[must_use = "this returns the result of the operation, without modifying the original"]
1098                pub const fn overflowing_add(self, rhs: Self) -> (Self, bool) {
1099                    let (value, overflow) = if Self::UNUSED_BITS == 0 {
1100                        // We are something like a Int::<i8; 8>, we can fallback to the base implementation.
1101                        // This is very unlikely to happen in practice, but checking allows us to use
1102                        // `wrapping_add` instead of `overflowing_add` in the common case, which is faster.
1103                        self.value().overflowing_add(rhs.value())
1104                    } else {
1105                        // We're dealing with fewer bits than the underlying type (e.g. i7).
1106                        // That means the addition can never overflow the underlying type.
1107                        let sum = self.value().wrapping_add(rhs.value());
1108                        let value = (sum << Self::UNUSED_BITS) >> Self::UNUSED_BITS;
1109                        (value, value != sum)
1110                    };
1111
1112                    (Self { value }, overflow)
1113                }
1114
1115                /// Calculates `self - rhs`.
1116                ///
1117                /// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic
1118                /// overflow would occur. If an overflow would have occurred then the wrapped value is returned.
1119                ///
1120                /// # Examples
1121                ///
1122                /// Basic usage:
1123                ///
1124                #[doc = concat!(" ```", $doctest_attr)]
1125                /// # use arbitrary_int::prelude::*;
1126                /// assert_eq!(i14::new(5).overflowing_sub(i14::new(2)), (i14::new(3), false));
1127                /// assert_eq!(i14::MIN.overflowing_sub(i14::new(1)), (i14::MAX, true));
1128                /// ```
1129                #[inline]
1130                #[must_use = "this returns the result of the operation, without modifying the original"]
1131                pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
1132                    let (value, overflow) = if Self::UNUSED_BITS == 0 {
1133                        // We are something like a Int::<i8; 8>, we can fallback to the base implementation.
1134                        // This is very unlikely to happen in practice, but checking allows us to use
1135                        // `wrapping_sub` instead of `overflowing_sub` in the common case, which is faster.
1136                        self.value().overflowing_sub(rhs.value())
1137                    } else {
1138                        // We're dealing with fewer bits than the underlying type (e.g. i7).
1139                        // That means the subtraction can never overflow the underlying type
1140                        let sum = self.value().wrapping_sub(rhs.value());
1141                        let value = (sum << Self::UNUSED_BITS) >> Self::UNUSED_BITS;
1142                        (value, value != sum)
1143                    };
1144
1145                    (Self { value }, overflow)
1146                }
1147
1148                /// Calculates the multiplication of `self` and `rhs`.
1149                ///
1150                /// Returns a tuple of the multiplication along with a boolean indicating whether an arithmetic
1151                /// overflow would occur. If an overflow would have occurred then the wrapped value is returned.
1152                ///
1153                /// # Examples
1154                ///
1155                /// Basic usage:
1156                ///
1157                #[doc = concat!(" ```", $doctest_attr)]
1158                /// # use arbitrary_int::prelude::*;
1159                /// assert_eq!(i14::new(5).overflowing_mul(i14::new(2)), (i14::new(10), false));
1160                /// assert_eq!(i14::new(1_000).overflowing_mul(i14::new(10)), (i14::new(-6384), true));
1161                /// ```
1162                #[inline]
1163                #[must_use = "this returns the result of the operation, without modifying the original"]
1164                pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
1165                    let (wrapping_product, overflow) = if (BITS << 1) <= (core::mem::size_of::<$type>() << 3) {
1166                        // We have half the bits (e.g. i4 * i4) of the base type, so we can't overflow the base type.
1167                        // `wrapping_mul` likely provides the best performance on all CPUs.
1168                        (self.value().wrapping_mul(rhs.value()), false)
1169                    } else {
1170                        // We have more than half the bits (e.g. i6 * i6)
1171                        self.value().overflowing_mul(rhs.value())
1172                    };
1173
1174                    let value = (wrapping_product << Self::UNUSED_BITS) >> Self::UNUSED_BITS;
1175                    let overflow2 = value != wrapping_product;
1176                    (Self { value }, overflow || overflow2)
1177                }
1178
1179                /// Calculates the divisor when `self` is divided by `rhs`.
1180                ///
1181                /// Returns a tuple of the divisor along with a boolean indicating whether an arithmetic
1182                /// overflow would occur. If an overflow would occur then self is returned.
1183                ///
1184                /// # Panics
1185                ///
1186                /// This function will panic if `rhs` is zero.
1187                ///
1188                /// # Examples
1189                ///
1190                /// Basic usage:
1191                ///
1192                #[doc = concat!(" ```", $doctest_attr)]
1193                /// # use arbitrary_int::prelude::*;
1194                /// assert_eq!(i14::new(5).overflowing_div(i14::new(2)), (i14::new(2), false));
1195                /// assert_eq!(i14::MIN.overflowing_div(i14::new(-1)), (i14::MIN, true));
1196                /// ```
1197                #[inline]
1198                #[must_use = "this returns the result of the operation, without modifying the original"]
1199                pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
1200                    let (value, overflow) = if Self::UNUSED_BITS == 0 {
1201                        // We are something like a Int::<i8; 8>, we can fallback to the base implementation.
1202                        // This is very unlikely to happen in practice, but checking allows us to use
1203                        // `wrapping_div` instead of `overflowing_div` in the common case, which is faster.
1204                        self.value().overflowing_div(rhs.value())
1205                    } else {
1206                        // We're dealing with fewer bits than the underlying type (e.g. i7).
1207                        // That means the division can never overflow the underlying type.
1208                        let quotient = self.value().wrapping_div(rhs.value());
1209                        let value = (quotient << Self::UNUSED_BITS) >> Self::UNUSED_BITS;
1210                        (value, value != quotient)
1211                    };
1212
1213                    (Self { value }, overflow)
1214                }
1215
1216                /// Negates `self`, overflowing if this is equal to the minimum value.
1217                ///
1218                /// Returns a tuple of the negated version of self along with a boolean indicating whether an
1219                /// overflow happened. If `self` is the minimum value (e.g., `i14::MIN` for values of type `i14`),
1220                /// then the minimum value will be returned again and `true` will be returned for an overflow happening.
1221                ///
1222                /// # Examples
1223                ///
1224                /// Basic usage:
1225                ///
1226                #[doc = concat!(" ```", $doctest_attr)]
1227                /// # use arbitrary_int::prelude::*;
1228                /// assert_eq!(i14::new(2).overflowing_neg(), (i14::new(-2), false));
1229                /// assert_eq!(i14::MIN.overflowing_neg(), (i14::MIN, true));
1230                /// ```
1231                #[inline]
1232                #[must_use = "this returns the result of the operation, without modifying the original"]
1233                pub const fn overflowing_neg(self) -> (Self, bool) {
1234                    let (value, overflow) = if Self::UNUSED_BITS == 0 {
1235                        // We are something like a Int::<i8; 8>, we can fallback to the base implementation.
1236                        // This is very unlikely to happen in practice, but checking allows us to use
1237                        // `wrapping_neg` instead of `overflowing_neg` in the common case, which is faster.
1238                        self.value().overflowing_neg()
1239                    } else {
1240                        // We're dealing with fewer bits than the underlying type (e.g. i7).
1241                        // That means the negation can never overflow the underlying type.
1242                        let negated = self.value().wrapping_neg();
1243                        let value = (negated << Self::UNUSED_BITS) >> Self::UNUSED_BITS;
1244                        (value, value != negated)
1245                    };
1246
1247                    (Self { value }, overflow)
1248                }
1249
1250                /// Shifts `self` left by `rhs` bits.
1251                ///
1252                /// Returns a tuple of the shifted version of `self` along with a boolean indicating whether
1253                /// the shift value was larger than or equal to the number of bits. If the shift value is too
1254                /// large, then value is masked (`N-1`) where `N` is the number of bits, and this value is then
1255                /// used to perform the shift.
1256                ///
1257                /// # Examples
1258                ///
1259                /// Basic usage:
1260                ///
1261                #[doc = concat!(" ```", $doctest_attr)]
1262                /// # use arbitrary_int::prelude::*;
1263                /// assert_eq!(i14::new(0x1).overflowing_shl(4), (i14::new(0x10), false));
1264                /// assert_eq!(i14::new(0x1).overflowing_shl(15), (i14::new(0x2), true));
1265                /// assert_eq!(i14::new(0x10).overflowing_shl(13), (i14::new(0), false));
1266                /// ```
1267                #[inline]
1268                #[must_use = "this returns the result of the operation, without modifying the original"]
1269                pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
1270                    let (shift, overflow) = if rhs >= (BITS as u32) {
1271                        (rhs % (BITS as u32), true)
1272                    } else {
1273                        (rhs, false)
1274                    };
1275
1276                    // This cannot possibly wrap as we've already limited `shift` to `BITS`.
1277                    let value = (self.value().wrapping_shl(shift) << Self::UNUSED_BITS) >> Self::UNUSED_BITS;
1278                    (Self { value }, overflow)
1279                }
1280
1281                /// Shifts `self` right by `rhs` bits.
1282                ///
1283                /// Returns a tuple of the shifted version of `self` along with a boolean indicating whether
1284                /// the shift value was larger than or equal to the number of bits. If the shift value is too
1285                /// large, then value is masked (`N-1`) where `N` is the number of bits, and this value is then
1286                /// used to perform the shift.
1287                ///
1288                /// # Examples
1289                ///
1290                /// Basic usage:
1291                ///
1292                #[doc = concat!(" ```", $doctest_attr)]
1293                /// # use arbitrary_int::prelude::*;
1294                /// assert_eq!(i14::new(0x10).overflowing_shr(4), (i14::new(0x1), false));
1295                /// assert_eq!(i14::new(0x10).overflowing_shr(15), (i14::new(0x8), true));
1296                /// ```
1297                #[inline]
1298                #[must_use = "this returns the result of the operation, without modifying the original"]
1299                pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
1300                    let (shift, overflow) = if rhs >= (BITS as u32) {
1301                        (rhs % (BITS as u32), true)
1302                    } else {
1303                        (rhs, false)
1304                    };
1305
1306                    // This cannot possibly wrap as we've already limited `shift` to `BITS`.
1307                    let value = (self.value().wrapping_shr(shift) << Self::UNUSED_BITS) >> Self::UNUSED_BITS;
1308                    (Self { value }, overflow)
1309                }
1310
1311                /// Returns `true` if `self` is positive and `false` if the number is zero or negative.
1312                ///
1313                /// # Examples
1314                ///
1315                /// Basic usage:
1316                ///
1317                #[doc = concat!(" ```", $doctest_attr)]
1318                /// # use arbitrary_int::prelude::*;
1319                /// assert!(i14::new(10).is_positive());
1320                /// assert!(!i14::new(-10).is_positive());
1321                /// ```
1322                #[inline]
1323                #[must_use]
1324                pub const fn is_positive(self) -> bool {
1325                    self.value() > 0
1326                }
1327
1328                /// Returns `true` if `self` is negative and `false` if the number is zero or positive.
1329                ///
1330                /// # Examples
1331                ///
1332                /// Basic usage:
1333                ///
1334                #[doc = concat!(" ```", $doctest_attr)]
1335                /// # use arbitrary_int::prelude::*;
1336                /// assert!(i14::new(-10).is_negative());
1337                /// assert!(!i14::new(10).is_negative());
1338                /// ```
1339                #[inline]
1340                #[must_use]
1341                pub const fn is_negative(self) -> bool {
1342                    self.value() < 0
1343                }
1344
1345                /// Reverses the order of bits in the integer. The least significant bit becomes the most
1346                /// significant bit, second least-significant bit becomes second most-significant bit, etc.
1347                ///
1348                /// # Examples
1349                ///
1350                /// Basic usage:
1351                ///
1352                #[doc = concat!(" ```", $doctest_attr)]
1353                /// # use arbitrary_int::prelude::*;
1354                /// assert_eq!(i6::from_bits(0b10_1010).reverse_bits(), i6::from_bits(0b01_0101));
1355                /// assert_eq!(i6::new(0), i6::new(0).reverse_bits());
1356                /// ```
1357                #[inline]
1358                #[must_use = "this returns the result of the operation, without modifying the original"]
1359                pub const fn reverse_bits(self) -> Self {
1360                    let value = self.value().reverse_bits() >> Self::UNUSED_BITS;
1361                    Self { value }
1362                }
1363
1364                /// Returns the number of ones in the binary representation of `self`.
1365                ///
1366                /// # Examples
1367                ///
1368                /// Basic usage:
1369                ///
1370                #[doc = concat!(" ```", $doctest_attr)]
1371                /// # use arbitrary_int::prelude::*;
1372                /// let n = i6::from_bits(0b00_1000);
1373                /// assert_eq!(n.count_ones(), 1);
1374                /// ```
1375                #[inline]
1376                pub const fn count_ones(self) -> u32 {
1377                    // Due to sign-extension the unused bits may be either all ones or zeros, so we need to mask them off.
1378                    (self.value() & Self::MASK).count_ones()
1379                }
1380
1381                /// Returns the number of zeros in the binary representation of `self`.
1382                ///
1383                /// # Examples
1384                ///
1385                /// Basic usage:
1386                ///
1387                #[doc = concat!(" ```", $doctest_attr)]
1388                /// # use arbitrary_int::prelude::*;
1389                /// assert_eq!(i6::MAX.count_zeros(), 1);
1390                /// ```
1391                #[inline]
1392                pub const fn count_zeros(self) -> u32 {
1393                    // Due to sign-extension the unused bits may be either all ones or zeros, so we need to mask them off.
1394                    // Afterwards the unused bits are all zero, so we can subtract them from the result.
1395                    // We can avoid a bounds check in debug builds with `wrapping_sub` since this cannot overflow.
1396                    (self.value() & Self::MASK).count_zeros().wrapping_sub(Self::UNUSED_BITS as u32)
1397                }
1398
1399                /// Returns the number of leading ones in the binary representation of `self`.
1400                ///
1401                /// # Examples
1402                ///
1403                /// Basic usage:
1404                ///
1405                #[doc = concat!(" ```", $doctest_attr)]
1406                /// # use arbitrary_int::prelude::*;
1407                /// let n = i6::new(-1);
1408                /// assert_eq!(n.leading_ones(), 6);
1409                /// ```
1410                #[inline]
1411                pub const fn leading_ones(self) -> u32 {
1412                    (self.value() << Self::UNUSED_BITS).leading_ones()
1413                }
1414
1415                /// Returns the number of leading zeros in the binary representation of `self`.
1416                ///
1417                /// # Examples
1418                ///
1419                /// Basic usage:
1420                ///
1421                #[doc = concat!(" ```", $doctest_attr)]
1422                /// # use arbitrary_int::prelude::*;
1423                /// let n = i6::new(-1);
1424                /// assert_eq!(n.leading_zeros(), 0);
1425                /// ```
1426                #[inline]
1427                pub const fn leading_zeros(self) -> u32 {
1428                    if Self::UNUSED_BITS == 0 {
1429                        self.value().leading_zeros()
1430                    } else {
1431                        // Prevent an all-zero value reporting the underlying type's entire bit width by setting
1432                        // the first unused bit to one, causing `leading_zeros()` to ignore all unused bits.
1433                        let first_unused_bit_set = const { 1 << (Self::UNUSED_BITS - 1) };
1434                        ((self.value() << Self::UNUSED_BITS) | first_unused_bit_set).leading_zeros()
1435                    }
1436                }
1437
1438                /// Returns the number of trailing ones in the binary representation of `self`.
1439                ///
1440                /// # Examples
1441                ///
1442                /// Basic usage:
1443                ///
1444                #[doc = concat!(" ```", $doctest_attr)]
1445                /// # use arbitrary_int::prelude::*;
1446                /// let n = i6::new(3);
1447                /// assert_eq!(n.trailing_ones(), 2);
1448                /// ```
1449                #[inline]
1450                pub const fn trailing_ones(self) -> u32 {
1451                    // Prevent an all-ones value reporting the underlying type's entire bit width by masking
1452                    // off all the unused bits.
1453                    (self.value() & Self::MASK).trailing_ones()
1454                }
1455
1456                /// Returns the number of trailing zeros in the binary representation of `self`.
1457                ///
1458                /// # Examples
1459                ///
1460                /// Basic usage:
1461                ///
1462                #[doc = concat!(" ```", $doctest_attr)]
1463                /// # use arbitrary_int::prelude::*;
1464                /// let n = i6::new(-4);
1465                /// assert_eq!(n.trailing_zeros(), 2);
1466                /// ```
1467                #[inline]
1468                pub const fn trailing_zeros(self) -> u32 {
1469                    // Prevent an all-ones value reporting the underlying type's entire bit width by setting
1470                    // all the unused bits.
1471                    (self.value() | !Self::MASK).trailing_zeros()
1472                }
1473
1474                /// Shifts the bits to the left by a specified amount, `n`, wrapping the truncated bits
1475                /// to the end of the resulting integer.
1476                ///
1477                /// Please note this isn’t the same operation as the `<<` shifting operator!
1478                ///
1479                /// # Examples
1480                ///
1481                /// Basic usage:
1482                ///
1483                #[doc = concat!(" ```", $doctest_attr)]
1484                /// # use arbitrary_int::prelude::*;
1485                /// let n = i6::from_bits(0b10_1010);
1486                /// let m = i6::from_bits(0b01_0101);
1487                ///
1488                /// assert_eq!(n.rotate_left(1), m);
1489                /// ```
1490                #[inline]
1491                #[must_use = "this returns the result of the operation, without modifying the original"]
1492                pub const fn rotate_left(self, n: u32) -> Self {
1493                    let b = BITS as u32;
1494                    let n = if n >= b { n % b } else { n };
1495
1496                    // Temporarily switch to an unsigned type to prevent sign-extension with `>>`.
1497                    let moved_bits = ((self.value() << n) & Self::MASK) as $unsigned_type;
1498                    let truncated_bits = ((self.value() & Self::MASK) as $unsigned_type) >> (b - n);
1499                    let value = (((moved_bits | truncated_bits) << Self::UNUSED_BITS) as $type) >> Self::UNUSED_BITS;
1500                    Self { value }
1501                }
1502
1503                /// Shifts the bits to the right by a specified amount, `n`, wrapping the truncated bits
1504                /// to the beginning of the resulting integer.
1505                ///
1506                /// Please note this isn’t the same operation as the `>>` shifting operator!
1507                ///
1508                /// # Examples
1509                ///
1510                /// Basic usage:
1511                ///
1512                #[doc = concat!(" ```", $doctest_attr)]
1513                /// # use arbitrary_int::prelude::*;
1514                /// let n = i6::from_bits(0b10_1010);
1515                /// let m = i6::from_bits(0b01_0101);
1516                ///
1517                /// assert_eq!(n.rotate_right(1), m);
1518                /// ```
1519                #[inline]
1520                #[must_use = "this returns the result of the operation, without modifying the original"]
1521                pub const fn rotate_right(self, n: u32) -> Self {
1522                    let b = BITS as u32;
1523                    let n = if n >= b { n % b } else { n };
1524
1525                    // Temporarily switch to an unsigned type to prevent sign-extension with `>>`.
1526                    let moved_bits = (self.value() & Self::MASK) as $unsigned_type >> n;
1527                    let truncated_bits = ((self.value() << (b - n)) & Self::MASK) as $unsigned_type;
1528                    let value = (((moved_bits | truncated_bits) << Self::UNUSED_BITS) as $type) >> Self::UNUSED_BITS;
1529                    Self { value }
1530                }
1531            }
1532        )+
1533    };
1534}
1535
1536// Because the methods within this macro are effectively copy-pasted for each underlying integer type,
1537// each documentation test gets executed five times (once for each underlying type), even though the
1538// tests themselves aren't specific to said underlying type. This severely slows down `cargo test`,
1539// so we ignore them for all but one (arbitrary) underlying type.
1540int_impl!(
1541    (i8, u8, doctest = "rust"),
1542    (i16, u16, doctest = "ignore"),
1543    (i32, u32, doctest = "ignore"),
1544    (i64, u64, doctest = "ignore"),
1545    (i128, u128, doctest = "ignore")
1546);
1547
1548// Arithmetic operator implementations
1549impl<T: SignedInteger + BuiltinInteger, const BITS: usize> Add for Int<T, BITS>
1550where
1551    T: Shl<usize, Output = T> + Shr<usize, Output = T>,
1552{
1553    type Output = Self;
1554
1555    fn add(self, rhs: Self) -> Self::Output {
1556        let sum = self.value + rhs.value;
1557        let value = (sum << Self::UNUSED_BITS) >> Self::UNUSED_BITS;
1558        debug_assert!(sum == value, "attempted to add with overflow");
1559        Self { value }
1560    }
1561}
1562
1563impl<T: SignedInteger + BuiltinInteger, const BITS: usize> AddAssign for Int<T, BITS>
1564where
1565    T: Shl<usize, Output = T> + Shr<usize, Output = T>,
1566{
1567    fn add_assign(&mut self, rhs: Self) {
1568        // Delegate to the Add implementation above.
1569        *self = *self + rhs;
1570    }
1571}
1572
1573impl<T: SignedInteger + BuiltinInteger, const BITS: usize> Sub for Int<T, BITS>
1574where
1575    T: Shl<usize, Output = T> + Shr<usize, Output = T>,
1576{
1577    type Output = Self;
1578
1579    fn sub(self, rhs: Self) -> Self::Output {
1580        let difference = self.value - rhs.value;
1581        let value = (difference << Self::UNUSED_BITS) >> Self::UNUSED_BITS;
1582        debug_assert!(difference == value, "attempted to subtract with overflow");
1583        Self { value }
1584    }
1585}
1586
1587impl<T: SignedInteger + BuiltinInteger, const BITS: usize> SubAssign for Int<T, BITS>
1588where
1589    T: Shl<usize, Output = T> + Shr<usize, Output = T>,
1590{
1591    fn sub_assign(&mut self, rhs: Self) {
1592        // Delegate to the Sub implementation above.
1593        *self = *self - rhs;
1594    }
1595}
1596
1597impl<T: SignedInteger + BuiltinInteger, const BITS: usize> Mul for Int<T, BITS>
1598where
1599    T: Shl<usize, Output = T> + Shr<usize, Output = T>,
1600{
1601    type Output = Self;
1602
1603    fn mul(self, rhs: Self) -> Self::Output {
1604        let product = self.value * rhs.value;
1605        let value = (product << Self::UNUSED_BITS) >> Self::UNUSED_BITS;
1606        debug_assert!(product == value, "attempted to multiply with overflow");
1607        Self { value }
1608    }
1609}
1610
1611impl<T: SignedInteger + BuiltinInteger, const BITS: usize> MulAssign for Int<T, BITS>
1612where
1613    Self: Integer,
1614{
1615    fn mul_assign(&mut self, rhs: Self) {
1616        // Delegate to the Mul implementation above.
1617        *self = *self * rhs;
1618    }
1619}
1620
1621impl<T: SignedInteger + BuiltinInteger, const BITS: usize> Div for Int<T, BITS>
1622where
1623    T: Shl<usize, Output = T> + Shr<usize, Output = T>,
1624{
1625    type Output = Self;
1626
1627    fn div(self, rhs: Self) -> Self::Output {
1628        // Unlike the unsigned implementation we do need to account for overflow here,
1629        // `Self::MIN / -1` is equal to `Self::MAX + 1` and should therefore panic.
1630        let quotient = self.value / rhs.value;
1631        let value = (quotient << Self::UNUSED_BITS) >> Self::UNUSED_BITS;
1632        debug_assert!(quotient == value, "attempted to divide with overflow");
1633        Self { value }
1634    }
1635}
1636
1637impl<T: SignedInteger + BuiltinInteger, const BITS: usize> DivAssign for Int<T, BITS>
1638where
1639    T: Shl<usize, Output = T> + Shr<usize, Output = T>,
1640{
1641    fn div_assign(&mut self, rhs: Self) {
1642        // Delegate to the Div implementation above.
1643        *self = *self / rhs;
1644    }
1645}
1646
1647impl<T: SignedInteger + BuiltinInteger, const BITS: usize> Neg for Int<T, BITS>
1648where
1649    Self: Integer<UnderlyingType = T>,
1650    T: Shl<usize, Output = T> + Shr<usize, Output = T>,
1651{
1652    type Output = Self;
1653
1654    #[inline]
1655    fn neg(self) -> Self::Output {
1656        let negated = -self.value();
1657        let value = (negated << Self::UNUSED_BITS) >> Self::UNUSED_BITS;
1658        debug_assert!(negated == value, "attempt to negate with overflow");
1659        Self { value }
1660    }
1661}
1662
1663// Bitwise operator implementations
1664impl<T: SignedInteger + BuiltinInteger, const BITS: usize> BitAnd for Int<T, BITS> {
1665    type Output = Self;
1666
1667    fn bitand(self, rhs: Self) -> Self::Output {
1668        let value = self.value & rhs.value;
1669        Self { value }
1670    }
1671}
1672
1673impl<T: SignedInteger + BuiltinInteger, const BITS: usize> BitAndAssign for Int<T, BITS> {
1674    fn bitand_assign(&mut self, rhs: Self) {
1675        self.value &= rhs.value;
1676    }
1677}
1678
1679impl<T: SignedInteger + BuiltinInteger, const BITS: usize> BitOr for Int<T, BITS> {
1680    type Output = Self;
1681
1682    fn bitor(self, rhs: Self) -> Self::Output {
1683        let value = self.value | rhs.value;
1684        Self { value }
1685    }
1686}
1687
1688impl<T: SignedInteger + BuiltinInteger, const BITS: usize> BitOrAssign for Int<T, BITS> {
1689    fn bitor_assign(&mut self, rhs: Self) {
1690        self.value |= rhs.value;
1691    }
1692}
1693
1694impl<T: SignedInteger + BuiltinInteger, const BITS: usize> BitXor for Int<T, BITS> {
1695    type Output = Self;
1696
1697    fn bitxor(self, rhs: Self) -> Self::Output {
1698        let value = self.value ^ rhs.value;
1699        Self { value }
1700    }
1701}
1702
1703impl<T: SignedInteger + BuiltinInteger, const BITS: usize> BitXorAssign for Int<T, BITS> {
1704    fn bitxor_assign(&mut self, rhs: Self) {
1705        self.value ^= rhs.value;
1706    }
1707}
1708
1709impl<T: SignedInteger + BuiltinInteger, const BITS: usize> Not for Int<T, BITS> {
1710    type Output = Self;
1711
1712    fn not(self) -> Self::Output {
1713        let value = !self.value;
1714        Self { value }
1715    }
1716}
1717
1718impl<T: SignedInteger + BuiltinInteger, TSHIFTBITS, const BITS: usize> Shl<TSHIFTBITS>
1719    for Int<T, BITS>
1720where
1721    T: Shl<TSHIFTBITS, Output = T> + Shl<usize, Output = T> + Shr<usize, Output = T>,
1722    TSHIFTBITS: TryInto<usize> + Copy,
1723{
1724    type Output = Self;
1725
1726    fn shl(self, rhs: TSHIFTBITS) -> Self::Output {
1727        // With debug assertions, the << and >> operators throw an exception if the shift amount
1728        // is larger than the number of bits (in which case the result would always be 0)
1729        debug_assert!(
1730            rhs.try_into().unwrap_or(usize::MAX) < BITS,
1731            "attempted to shift left with overflow"
1732        );
1733
1734        // Shift left twice to avoid needing an unnecessarily strict `TSHIFTBITS: Add<Self::UNUSED_BITS>` bound.
1735        // This should be optimised to a single shift.
1736        let value = ((self.value << rhs) << Self::UNUSED_BITS) >> Self::UNUSED_BITS;
1737        Self { value }
1738    }
1739}
1740
1741impl<T: SignedInteger + BuiltinInteger, TSHIFTBITS, const BITS: usize> ShlAssign<TSHIFTBITS>
1742    for Int<T, BITS>
1743where
1744    Self: Integer,
1745    T: Shl<TSHIFTBITS, Output = T> + Shl<usize, Output = T> + Shr<usize, Output = T>,
1746    TSHIFTBITS: TryInto<usize> + Copy,
1747{
1748    fn shl_assign(&mut self, rhs: TSHIFTBITS) {
1749        // Delegate to the Shl implementation above.
1750        *self = *self << rhs;
1751    }
1752}
1753
1754impl<T: SignedInteger + BuiltinInteger, TSHIFTBITS, const BITS: usize> Shr<TSHIFTBITS>
1755    for Int<T, BITS>
1756where
1757    Self: Integer,
1758    T: Shr<TSHIFTBITS, Output = T> + Shl<usize, Output = T> + Shr<usize, Output = T>,
1759    TSHIFTBITS: TryInto<usize> + Copy,
1760{
1761    type Output = Self;
1762
1763    fn shr(self, rhs: TSHIFTBITS) -> Self::Output {
1764        // With debug assertions, the << and >> operators throw an exception if the shift amount
1765        // is larger than the number of bits (in which case the result would always be 0)
1766        debug_assert!(
1767            rhs.try_into().unwrap_or(usize::MAX) < BITS,
1768            "attempted to shift right with overflow"
1769        );
1770
1771        Self {
1772            // Our unused bits can only ever all be 1 or 0, depending on the sign.
1773            // As right shifts on primitive types perform sign-extension anyways we don't need to do any extra work here.
1774            value: self.value >> rhs,
1775        }
1776    }
1777}
1778
1779impl<T: SignedInteger + BuiltinInteger, TSHIFTBITS, const BITS: usize> ShrAssign<TSHIFTBITS>
1780    for Int<T, BITS>
1781where
1782    Self: Integer,
1783    T: Shr<TSHIFTBITS, Output = T> + Shl<usize, Output = T> + Shr<usize, Output = T>,
1784    TSHIFTBITS: TryInto<usize> + Copy,
1785{
1786    fn shr_assign(&mut self, rhs: TSHIFTBITS) {
1787        // Delegate to the Shr implementation above.
1788        *self = *self >> rhs;
1789    }
1790}
1791
1792// Delegated trait implementations
1793impl<T: SignedInteger + BuiltinInteger, const BITS: usize> Display for Int<T, BITS> {
1794    #[inline]
1795    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
1796        Display::fmt(&self.value, f)
1797    }
1798}
1799
1800impl<T: SignedInteger + BuiltinInteger, const BITS: usize> Debug for Int<T, BITS> {
1801    #[inline]
1802    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
1803        Debug::fmt(&self.value, f)
1804    }
1805}
1806
1807impl<T: SignedInteger + BuiltinInteger, const BITS: usize> LowerHex for Int<T, BITS> {
1808    #[inline]
1809    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
1810        LowerHex::fmt(&self.value, f)
1811    }
1812}
1813
1814impl<T: SignedInteger + BuiltinInteger, const BITS: usize> UpperHex for Int<T, BITS> {
1815    #[inline]
1816    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
1817        UpperHex::fmt(&self.value, f)
1818    }
1819}
1820
1821impl<T: SignedInteger + BuiltinInteger, const BITS: usize> Octal for Int<T, BITS> {
1822    #[inline]
1823    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
1824        Octal::fmt(&self.value, f)
1825    }
1826}
1827
1828impl<T: SignedInteger + BuiltinInteger, const BITS: usize> Binary for Int<T, BITS> {
1829    #[inline]
1830    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
1831        Binary::fmt(&self.value, f)
1832    }
1833}
1834
1835impl_bytemuck_basic!(Int, SignedInteger {
1836    /// A [`Int`] initialized to zero has a zero value.
1837    impl Zeroable for ... {}
1838    /// A [`Int`] has no uninitialized bytes or padding.
1839    impl NoUninit for ... {}
1840    /// It is possible to check whether an in-memory representation of an [`Int`] is valid,
1841    /// although the specific meaning of that representation is not specified.
1842    impl CheckedBitPattern for ... {}
1843});
1844
1845#[cfg(feature = "defmt")]
1846impl<T: SignedInteger + BuiltinInteger, const BITS: usize> defmt::Format for Int<T, BITS>
1847where
1848    T: defmt::Format,
1849{
1850    #[inline]
1851    fn format(&self, f: defmt::Formatter) {
1852        self.value.format(f)
1853    }
1854}
1855
1856impl_borsh!(Int, "i", SignedInteger);
1857
1858// Serde's invalid_value error (https://rust-lang.github.io/hashbrown/serde/de/trait.Error.html#method.invalid_value)
1859// takes an Unexpected (https://rust-lang.github.io/hashbrown/serde/de/enum.Unexpected.html) which only accepts a 64 bit
1860// integer. This is a problem for us because we want to support 128 bit integers. To work around this we define our own
1861// error type using the Int's underlying type which implements Display and then use serde::de::Error::custom to create
1862// an error with our custom type.
1863#[cfg(feature = "serde")]
1864struct InvalidIntValueError<T: SignedInteger> {
1865    value: T::UnderlyingType,
1866}
1867
1868#[cfg(feature = "serde")]
1869impl<T: SignedInteger> Display for InvalidIntValueError<T> {
1870    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
1871        write!(
1872            f,
1873            "invalid value: integer `{}`, expected a value between `{}` and `{}`",
1874            self.value,
1875            T::MIN.value(),
1876            T::MAX.value()
1877        )
1878    }
1879}
1880
1881#[cfg(feature = "serde")]
1882impl<T: SignedInteger + BuiltinInteger, const BITS: usize> serde::Serialize for Int<T, BITS>
1883where
1884    T: serde::Serialize,
1885{
1886    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1887        self.value.serialize(serializer)
1888    }
1889}
1890
1891#[cfg(feature = "serde")]
1892impl<'de, T: SignedInteger + BuiltinInteger, const BITS: usize> serde::Deserialize<'de>
1893    for Int<T, BITS>
1894where
1895    Self: SignedInteger<UnderlyingType = T>,
1896    T: serde::Deserialize<'de>,
1897{
1898    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1899        let value = T::deserialize(deserializer)?;
1900
1901        if value >= Self::MIN.value && value <= Self::MAX.value {
1902            Ok(Self { value })
1903        } else {
1904            let err = InvalidIntValueError::<Self> { value };
1905            Err(serde::de::Error::custom(err))
1906        }
1907    }
1908}
1909
1910// Implement `core::iter::Sum` and `core::iter::Product`.
1911impl_sum_product!(Int, 1_i8, SignedInteger);
1912
1913// Implement `core::iter::Step` (if the `step_trait` feature is enabled).
1914impl_step!(Int, SignedInteger);
1915
1916// Implement support for the `num-traits` crate, if the feature is enabled.
1917impl_num_traits!(Int, SignedInteger, i8, |value| (
1918    (value << Self::UNUSED_BITS) >> Self::UNUSED_BITS,
1919    value.clamp(Self::MIN.value(), Self::MAX.value())
1920));
1921
1922// Support for the `schemars` crate, if the feature is enabled.
1923impl_schemars!(Int, "int", SignedInteger);
1924
1925// Implement byte operations for Int's with a bit width aligned to a byte boundary.
1926bytes_operation_impl!(Int<i32, 24>, i32);
1927bytes_operation_impl!(Int<i64, 24>, i64);
1928bytes_operation_impl!(Int<i128, 24>, i128);
1929bytes_operation_impl!(Int<i64, 40>, i64);
1930bytes_operation_impl!(Int<i128, 40>, i128);
1931bytes_operation_impl!(Int<i64, 48>, i64);
1932bytes_operation_impl!(Int<i128, 48>, i128);
1933bytes_operation_impl!(Int<i64, 56>, i64);
1934bytes_operation_impl!(Int<i128, 56>, i128);
1935bytes_operation_impl!(Int<i128, 72>, i128);
1936bytes_operation_impl!(Int<i128, 80>, i128);
1937bytes_operation_impl!(Int<i128, 88>, i128);
1938bytes_operation_impl!(Int<i128, 96>, i128);
1939bytes_operation_impl!(Int<i128, 104>, i128);
1940bytes_operation_impl!(Int<i128, 112>, i128);
1941bytes_operation_impl!(Int<i128, 120>, i128);
1942
1943// Conversions
1944from_arbitrary_int_impl!(Int(i8), [i16, i32, i64, i128]);
1945from_arbitrary_int_impl!(Int(i16), [i8, i32, i64, i128]);
1946from_arbitrary_int_impl!(Int(i32), [i8, i16, i64, i128]);
1947from_arbitrary_int_impl!(Int(i64), [i8, i16, i32, i128]);
1948from_arbitrary_int_impl!(Int(i128), [i8, i32, i64, i16]);
1949
1950from_native_impl!(Int(i8), [i8, i16, i32, i64, i128]);
1951from_native_impl!(Int(i16), [i8, i16, i32, i64, i128]);
1952from_native_impl!(Int(i32), [i8, i16, i32, i64, i128]);
1953from_native_impl!(Int(i64), [i8, i16, i32, i64, i128]);
1954from_native_impl!(Int(i128), [i8, i16, i32, i64, i128]);
1955
1956use crate::common::{impl_borsh, impl_bytemuck_basic};
1957pub use aliases::*;
1958
1959#[allow(non_camel_case_types)]
1960#[rustfmt::skip]
1961pub(crate) mod aliases {
1962    use crate::common::type_alias;
1963
1964    type_alias!(Int(i8), (i1, 1), (i2, 2), (i3, 3), (i4, 4), (i5, 5), (i6, 6), (i7, 7));
1965    type_alias!(Int(i16), (i9, 9), (i10, 10), (i11, 11), (i12, 12), (i13, 13), (i14, 14), (i15, 15));
1966    type_alias!(Int(i32), (i17, 17), (i18, 18), (i19, 19), (i20, 20), (i21, 21), (i22, 22), (i23, 23), (i24, 24), (i25, 25), (i26, 26), (i27, 27), (i28, 28), (i29, 29), (i30, 30), (i31, 31));
1967    type_alias!(Int(i64), (i33, 33), (i34, 34), (i35, 35), (i36, 36), (i37, 37), (i38, 38), (i39, 39), (i40, 40), (i41, 41), (i42, 42), (i43, 43), (i44, 44), (i45, 45), (i46, 46), (i47, 47), (i48, 48), (i49, 49), (i50, 50), (i51, 51), (i52, 52), (i53, 53), (i54, 54), (i55, 55), (i56, 56), (i57, 57), (i58, 58), (i59, 59), (i60, 60), (i61, 61), (i62, 62), (i63, 63));
1968    type_alias!(Int(i128), (i65, 65), (i66, 66), (i67, 67), (i68, 68), (i69, 69), (i70, 70), (i71, 71), (i72, 72), (i73, 73), (i74, 74), (i75, 75), (i76, 76), (i77, 77), (i78, 78), (i79, 79), (i80, 80), (i81, 81), (i82, 82), (i83, 83), (i84, 84), (i85, 85), (i86, 86), (i87, 87), (i88, 88), (i89, 89), (i90, 90), (i91, 91), (i92, 92), (i93, 93), (i94, 94), (i95, 95), (i96, 96), (i97, 97), (i98, 98), (i99, 99), (i100, 100), (i101, 101), (i102, 102), (i103, 103), (i104, 104), (i105, 105), (i106, 106), (i107, 107), (i108, 108), (i109, 109), (i110, 110), (i111, 111), (i112, 112), (i113, 113), (i114, 114), (i115, 115), (i116, 116), (i117, 117), (i118, 118), (i119, 119), (i120, 120), (i121, 121), (i122, 122), (i123, 123), (i124, 124), (i125, 125), (i126, 126), (i127, 127));
1969}