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