malachite_base/num/basic/
integers.rs

1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::comparison::traits::{Max, Min};
10use crate::named::Named;
11use crate::num::arithmetic::traits::{
12    AbsDiff, AddMul, AddMulAssign, ArithmeticCheckedShl, ArithmeticCheckedShr, BinomialCoefficient,
13    CeilingRoot, CeilingRootAssign, CeilingSqrt, CeilingSqrtAssign, CheckedAdd, CheckedAddMul,
14    CheckedBinomialCoefficient, CheckedDiv, CheckedMul, CheckedNeg, CheckedPow, CheckedRoot,
15    CheckedSqrt, CheckedSquare, CheckedSub, CheckedSubMul, DivAssignMod, DivAssignRem, DivExact,
16    DivExactAssign, DivMod, DivRem, DivRound, DivRoundAssign, DivisibleBy, DivisibleByPowerOf2,
17    EqMod, EqModPowerOf2, ExtendedGcd, FloorRoot, FloorRootAssign, FloorSqrt, FloorSqrtAssign,
18    JacobiSymbol, KroneckerSymbol, LegendreSymbol, Mod, ModAssign, ModPowerOf2, ModPowerOf2Assign,
19    OverflowingAdd, OverflowingAddAssign, OverflowingAddMul, OverflowingAddMulAssign,
20    OverflowingDiv, OverflowingDivAssign, OverflowingMul, OverflowingMulAssign, OverflowingNeg,
21    OverflowingNegAssign, OverflowingPow, OverflowingPowAssign, OverflowingSquare,
22    OverflowingSquareAssign, OverflowingSub, OverflowingSubAssign, OverflowingSubMul,
23    OverflowingSubMulAssign, Parity, Pow, PowAssign, PowerOf2, RemPowerOf2, RemPowerOf2Assign,
24    RotateLeft, RotateLeftAssign, RotateRight, RotateRightAssign, RoundToMultiple,
25    RoundToMultipleAssign, RoundToMultipleOfPowerOf2, RoundToMultipleOfPowerOf2Assign,
26    SaturatingAdd, SaturatingAddAssign, SaturatingAddMul, SaturatingAddMulAssign, SaturatingMul,
27    SaturatingMulAssign, SaturatingPow, SaturatingPowAssign, SaturatingSquare,
28    SaturatingSquareAssign, SaturatingSub, SaturatingSubAssign, SaturatingSubMul,
29    SaturatingSubMulAssign, ShlRound, ShlRoundAssign, ShrRound, ShrRoundAssign, Sign, Square,
30    SquareAssign, SubMul, SubMulAssign, WrappingAdd, WrappingAddAssign, WrappingAddMul,
31    WrappingAddMulAssign, WrappingDiv, WrappingDivAssign, WrappingMul, WrappingMulAssign,
32    WrappingNeg, WrappingNegAssign, WrappingPow, WrappingPowAssign, WrappingSquare,
33    WrappingSquareAssign, WrappingSub, WrappingSubAssign, WrappingSubMul, WrappingSubMulAssign,
34};
35use crate::num::basic::traits::{One, Two, Zero};
36use crate::num::comparison::traits::{EqAbs, OrdAbs, PartialOrdAbs};
37use crate::num::conversion::traits::{
38    ConvertibleFrom, ExactFrom, ExactInto, FromSciString, FromStringBase, IsInteger,
39    OverflowingFrom, OverflowingInto, RoundingFrom, RoundingInto, SaturatingFrom, SaturatingInto,
40    ToSci, ToStringBase, WrappingFrom, WrappingInto,
41};
42#[cfg(feature = "random")]
43use crate::num::factorization::traits::IsSquare;
44use crate::num::float::NiceFloat;
45use crate::num::logic::traits::{
46    BitAccess, BitBlockAccess, BitConvertible, BitIterable, BitScan, CountOnes, CountZeros,
47    LeadingZeros, LowMask, NotAssign, SignificantBits, TrailingZeros,
48};
49#[cfg(feature = "random")]
50use crate::num::random::HasRandomPrimitiveInts;
51use core::fmt::{Binary, Debug, Display, LowerHex, Octal, UpperHex};
52use core::hash::Hash;
53use core::iter::{Product, Sum};
54use core::ops::{
55    Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
56    Mul, MulAssign, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
57};
58use core::panic::{RefUnwindSafe, UnwindSafe};
59use core::str::FromStr;
60
61pub const USIZE_IS_U32: bool = usize::WIDTH == u32::WIDTH;
62pub const USIZE_IS_U64: bool = usize::WIDTH == u64::WIDTH;
63
64// Checks that usize is equivalent to u32 or u64, at compile time. The rest of Malachite can assume
65// that this condition is true.
66const _USIZE_ASSERTION: () = assert!(USIZE_IS_U32 || USIZE_IS_U64);
67
68// When the `random` feature is enabled, the HasRandomPrimitiveInts bound is included.
69
70#[cfg(feature = "random")]
71/// Defines functions on primitive integer types: uxx, ixx, usize, and isize.
72///
73/// The different types are distinguished by whether they are signed or unsigned, and by their
74/// widths. The width $W$ is the number of bits in the type. For example, the width of [`u32`] or
75/// [`i32`] is 32. Each type has $2^W$ distinct values.
76///
77/// Let $n$ be a value of type `Self`. If `Self` is unsigned, $0 \leq n < 2^W$. If `Self` is signed,
78/// $2^{W-1} \leq n < 2^{W-1}$.
79pub trait PrimitiveInt:
80    'static
81    + AbsDiff<Self>
82    + Add<Self, Output = Self>
83    + AddAssign<Self>
84    + AddMul<Self, Self, Output = Self>
85    + AddMulAssign<Self, Self>
86    + ArithmeticCheckedShl<i128, Output = Self>
87    + ArithmeticCheckedShl<i16, Output = Self>
88    + ArithmeticCheckedShl<i32, Output = Self>
89    + ArithmeticCheckedShl<i64, Output = Self>
90    + ArithmeticCheckedShl<i8, Output = Self>
91    + ArithmeticCheckedShl<isize, Output = Self>
92    + ArithmeticCheckedShl<u128, Output = Self>
93    + ArithmeticCheckedShl<u16, Output = Self>
94    + ArithmeticCheckedShl<u32, Output = Self>
95    + ArithmeticCheckedShl<u64, Output = Self>
96    + ArithmeticCheckedShl<u8, Output = Self>
97    + ArithmeticCheckedShl<usize, Output = Self>
98    + ArithmeticCheckedShr<i128, Output = Self>
99    + ArithmeticCheckedShr<i16, Output = Self>
100    + ArithmeticCheckedShr<i32, Output = Self>
101    + ArithmeticCheckedShr<i64, Output = Self>
102    + ArithmeticCheckedShr<i8, Output = Self>
103    + ArithmeticCheckedShr<isize, Output = Self>
104    + Binary
105    + BinomialCoefficient<Self>
106    + BitAccess
107    + BitAnd<Self, Output = Self>
108    + BitAndAssign<Self>
109    + BitBlockAccess
110    + BitConvertible
111    + BitIterable
112    + BitOr<Self, Output = Self>
113    + BitOrAssign<Self>
114    + BitScan
115    + BitXor<Self, Output = Self>
116    + BitXorAssign<Self>
117    + CeilingRoot<u64, Output = Self>
118    + CeilingRootAssign<u64>
119    + CeilingSqrt<Output = Self>
120    + CeilingSqrtAssign
121    + CheckedAdd<Self, Output = Self>
122    + CheckedAddMul<Self, Self, Output = Self>
123    + CheckedBinomialCoefficient<Self>
124    + CheckedDiv<Self, Output = Self>
125    + CheckedMul<Self, Output = Self>
126    + CheckedNeg<Output = Self>
127    + CheckedPow<u64, Output = Self>
128    + CheckedRoot<u64, Output = Self>
129    + CheckedSqrt<Output = Self>
130    + CheckedSquare<Output = Self>
131    + CheckedSub<Self, Output = Self>
132    + CheckedSubMul<Self, Self, Output = Self>
133    + Clone
134    + ConvertibleFrom<f32>
135    + ConvertibleFrom<f64>
136    + ConvertibleFrom<i128>
137    + ConvertibleFrom<i16>
138    + ConvertibleFrom<i32>
139    + ConvertibleFrom<i64>
140    + ConvertibleFrom<i8>
141    + ConvertibleFrom<isize>
142    + ConvertibleFrom<u128>
143    + ConvertibleFrom<u16>
144    + ConvertibleFrom<u32>
145    + ConvertibleFrom<u64>
146    + ConvertibleFrom<u8>
147    + ConvertibleFrom<usize>
148    + Copy
149    + CountOnes
150    + CountZeros
151    + Debug
152    + Default
153    + Display
154    + Div<Self, Output = Self>
155    + DivAssign<Self>
156    + DivAssignMod<Self, ModOutput = Self>
157    + DivAssignRem<Self, RemOutput = Self>
158    + DivExact<Self, Output = Self>
159    + DivExactAssign<Self>
160    + DivMod<Self, DivOutput = Self, ModOutput = Self>
161    + DivRem<Self, DivOutput = Self, RemOutput = Self>
162    + DivRound<Self, Output = Self>
163    + DivRoundAssign<Self>
164    + DivisibleBy<Self>
165    + DivisibleByPowerOf2
166    + Eq
167    + EqAbs<Self>
168    + EqMod<Self, Self>
169    + EqModPowerOf2<Self>
170    + ExactFrom<i128>
171    + ExactFrom<i16>
172    + ExactFrom<i32>
173    + ExactFrom<i64>
174    + ExactFrom<i8>
175    + ExactFrom<isize>
176    + ExactFrom<u128>
177    + ExactFrom<u16>
178    + ExactFrom<u32>
179    + ExactFrom<u64>
180    + ExactFrom<u8>
181    + ExactFrom<usize>
182    + ExactInto<i128>
183    + ExactInto<i16>
184    + ExactInto<i32>
185    + ExactInto<i64>
186    + ExactInto<i8>
187    + ExactInto<isize>
188    + ExactInto<u128>
189    + ExactInto<u16>
190    + ExactInto<u32>
191    + ExactInto<u64>
192    + ExactInto<u8>
193    + ExactInto<usize>
194    + ExtendedGcd<Self>
195    + FloorRoot<u64, Output = Self>
196    + FloorRootAssign<u64>
197    + FloorSqrt<Output = Self>
198    + FloorSqrtAssign
199    + From<bool>
200    + FromSciString
201    + FromStr
202    + FromStringBase
203    + HasRandomPrimitiveInts
204    + Hash
205    + IsInteger
206    + IsSquare
207    + JacobiSymbol<Self>
208    + KroneckerSymbol<Self>
209    + LeadingZeros
210    + LegendreSymbol<Self>
211    + LowMask
212    + LowerHex
213    + Max
214    + Min
215    + Mod<Self, Output = Self>
216    + ModAssign<Self>
217    + ModPowerOf2
218    + ModPowerOf2Assign
219    + Mul<Self, Output = Self>
220    + MulAssign<Self>
221    + Named
222    + Not<Output = Self>
223    + NotAssign
224    + Octal
225    + One
226    + Ord
227    + OrdAbs
228    + OverflowingAdd<Self, Output = Self>
229    + OverflowingAddAssign<Self>
230    + OverflowingAddMul<Self, Self, Output = Self>
231    + OverflowingAddMulAssign<Self, Self>
232    + OverflowingDiv<Self, Output = Self>
233    + OverflowingDivAssign<Self>
234    + OverflowingFrom<i128>
235    + OverflowingFrom<i16>
236    + OverflowingFrom<i32>
237    + OverflowingFrom<i64>
238    + OverflowingFrom<i8>
239    + OverflowingFrom<isize>
240    + OverflowingFrom<u128>
241    + OverflowingFrom<u16>
242    + OverflowingFrom<u32>
243    + OverflowingFrom<u64>
244    + OverflowingFrom<u8>
245    + OverflowingFrom<usize>
246    + OverflowingInto<i128>
247    + OverflowingInto<i16>
248    + OverflowingInto<i32>
249    + OverflowingInto<i64>
250    + OverflowingInto<i8>
251    + OverflowingInto<isize>
252    + OverflowingInto<u128>
253    + OverflowingInto<u16>
254    + OverflowingInto<u32>
255    + OverflowingInto<u64>
256    + OverflowingInto<u8>
257    + OverflowingInto<usize>
258    + OverflowingMul<Self, Output = Self>
259    + OverflowingMulAssign<Self>
260    + OverflowingNeg<Output = Self>
261    + OverflowingNegAssign
262    + OverflowingPow<u64, Output = Self>
263    + OverflowingPowAssign<u64>
264    + OverflowingSquare<Output = Self>
265    + OverflowingSquareAssign
266    + OverflowingSub<Self, Output = Self>
267    + OverflowingSubAssign<Self>
268    + OverflowingSubMul<Self, Self, Output = Self>
269    + OverflowingSubMulAssign<Self, Self>
270    + Parity
271    + PartialEq<Self>
272    + PartialOrd<Self>
273    + PartialOrdAbs<Self>
274    + Pow<u64, Output = Self>
275    + PowAssign<u64>
276    + PowerOf2<u64>
277    + Product
278    + RefUnwindSafe
279    + Rem<Self, Output = Self>
280    + RemAssign<Self>
281    + RemPowerOf2<Output = Self>
282    + RemPowerOf2Assign
283    + RotateLeft<Output = Self>
284    + RotateLeftAssign
285    + RotateRight<Output = Self>
286    + RotateRightAssign
287    + RoundToMultiple<Self, Output = Self>
288    + RoundToMultipleAssign<Self>
289    + RoundToMultipleOfPowerOf2<u64, Output = Self>
290    + RoundToMultipleOfPowerOf2Assign<u64>
291    + RoundingFrom<f32>
292    + RoundingFrom<f64>
293    + RoundingInto<f32>
294    + RoundingInto<f64>
295    + SaturatingAdd<Self, Output = Self>
296    + SaturatingAddAssign<Self>
297    + SaturatingAddMul<Self, Self, Output = Self>
298    + SaturatingAddMulAssign<Self, Self>
299    + SaturatingFrom<i128>
300    + SaturatingFrom<i16>
301    + SaturatingFrom<i32>
302    + SaturatingFrom<i64>
303    + SaturatingFrom<i8>
304    + SaturatingFrom<isize>
305    + SaturatingFrom<u128>
306    + SaturatingFrom<u16>
307    + SaturatingFrom<u32>
308    + SaturatingFrom<u64>
309    + SaturatingFrom<u8>
310    + SaturatingFrom<usize>
311    + SaturatingInto<i128>
312    + SaturatingInto<i16>
313    + SaturatingInto<i32>
314    + SaturatingInto<i64>
315    + SaturatingInto<i8>
316    + SaturatingInto<isize>
317    + SaturatingInto<u128>
318    + SaturatingInto<u16>
319    + SaturatingInto<u32>
320    + SaturatingInto<u64>
321    + SaturatingInto<u8>
322    + SaturatingInto<usize>
323    + SaturatingMul<Self, Output = Self>
324    + SaturatingMulAssign<Self>
325    + SaturatingPow<u64, Output = Self>
326    + SaturatingPowAssign<u64>
327    + SaturatingSquare<Output = Self>
328    + SaturatingSquareAssign
329    + SaturatingSub<Self, Output = Self>
330    + SaturatingSubAssign<Self>
331    + SaturatingSubMul<Self, Self, Output = Self>
332    + SaturatingSubMulAssign<Self, Self>
333    + Shl<i128, Output = Self>
334    + Shl<i16, Output = Self>
335    + Shl<i32, Output = Self>
336    + Shl<i64, Output = Self>
337    + Shl<i8, Output = Self>
338    + Shl<u128, Output = Self>
339    + Shl<u16, Output = Self>
340    + Shl<u32, Output = Self>
341    + Shl<u64, Output = Self>
342    + Shl<u8, Output = Self>
343    + ShlAssign<i128>
344    + ShlAssign<i16>
345    + ShlAssign<i32>
346    + ShlAssign<i64>
347    + ShlAssign<i8>
348    + ShlAssign<isize>
349    + ShlAssign<u128>
350    + ShlAssign<u16>
351    + ShlAssign<u32>
352    + ShlAssign<u64>
353    + ShlAssign<u8>
354    + ShlAssign<usize>
355    + ShlRound<i128, Output = Self>
356    + ShlRound<i16, Output = Self>
357    + ShlRound<i32, Output = Self>
358    + ShlRound<i64, Output = Self>
359    + ShlRound<i8, Output = Self>
360    + ShlRound<isize, Output = Self>
361    + ShlRoundAssign<i128>
362    + ShlRoundAssign<i16>
363    + ShlRoundAssign<i32>
364    + ShlRoundAssign<i64>
365    + ShlRoundAssign<i8>
366    + ShlRoundAssign<isize>
367    + Shr<i128, Output = Self>
368    + Shr<i16, Output = Self>
369    + Shr<i32, Output = Self>
370    + Shr<i64, Output = Self>
371    + Shr<i8, Output = Self>
372    + Shr<isize, Output = Self>
373    + Shr<u128, Output = Self>
374    + Shr<u16, Output = Self>
375    + Shr<u32, Output = Self>
376    + Shr<u64, Output = Self>
377    + Shr<u8, Output = Self>
378    + Shr<usize, Output = Self>
379    + ShrAssign<i128>
380    + ShrAssign<i16>
381    + ShrAssign<i32>
382    + ShrAssign<i64>
383    + ShrAssign<i8>
384    + ShrAssign<isize>
385    + ShrAssign<u128>
386    + ShrAssign<u16>
387    + ShrAssign<u32>
388    + ShrAssign<u64>
389    + ShrAssign<u8>
390    + ShrAssign<usize>
391    + ShrRound<i128, Output = Self>
392    + ShrRound<i16, Output = Self>
393    + ShrRound<i32, Output = Self>
394    + ShrRound<i64, Output = Self>
395    + ShrRound<i8, Output = Self>
396    + ShrRound<isize, Output = Self>
397    + ShrRound<u128, Output = Self>
398    + ShrRound<u16, Output = Self>
399    + ShrRound<u32, Output = Self>
400    + ShrRound<u64, Output = Self>
401    + ShrRound<u8, Output = Self>
402    + ShrRound<usize, Output = Self>
403    + ShrRoundAssign<i128>
404    + ShrRoundAssign<i16>
405    + ShrRoundAssign<i32>
406    + ShrRoundAssign<i64>
407    + ShrRoundAssign<i8>
408    + ShrRoundAssign<isize>
409    + ShrRoundAssign<u128>
410    + ShrRoundAssign<u16>
411    + ShrRoundAssign<u32>
412    + ShrRoundAssign<u64>
413    + ShrRoundAssign<u8>
414    + ShrRoundAssign<usize>
415    + Sign
416    + SignificantBits
417    + Sized
418    + Square<Output = Self>
419    + SquareAssign
420    + Sub<Self, Output = Self>
421    + SubAssign<Self>
422    + SubMul<Self, Self, Output = Self>
423    + SubMulAssign<Self, Self>
424    + Sum<Self>
425    + ToSci
426    + ToStringBase
427    + TrailingZeros
428    + TryFrom<NiceFloat<f32>>
429    + TryFrom<i128>
430    + TryFrom<i16>
431    + TryFrom<i32>
432    + TryFrom<i64>
433    + TryFrom<i8>
434    + TryFrom<isize>
435    + TryFrom<u128>
436    + TryFrom<u16>
437    + TryFrom<u32>
438    + TryFrom<u64>
439    + TryFrom<u8>
440    + TryFrom<usize>
441    + TryInto<NiceFloat<f32>>
442    + TryInto<i128>
443    + TryInto<i16>
444    + TryInto<i32>
445    + TryInto<i64>
446    + TryInto<i8>
447    + TryInto<isize>
448    + TryInto<u128>
449    + TryInto<u16>
450    + TryInto<u32>
451    + TryInto<u64>
452    + TryInto<u8>
453    + TryInto<usize>
454    + Two
455    + UnwindSafe
456    + UpperHex
457    + WrappingAdd<Self, Output = Self>
458    + WrappingAddAssign<Self>
459    + WrappingAddMul<Self, Self, Output = Self>
460    + WrappingAddMulAssign<Self, Self>
461    + WrappingDiv<Self, Output = Self>
462    + WrappingDivAssign<Self>
463    + WrappingFrom<i128>
464    + WrappingFrom<i16>
465    + WrappingFrom<i32>
466    + WrappingFrom<i64>
467    + WrappingFrom<i8>
468    + WrappingFrom<isize>
469    + WrappingFrom<u128>
470    + WrappingFrom<u16>
471    + WrappingFrom<u32>
472    + WrappingFrom<u64>
473    + WrappingFrom<u8>
474    + WrappingFrom<usize>
475    + WrappingInto<i128>
476    + WrappingInto<i16>
477    + WrappingInto<i32>
478    + WrappingInto<i64>
479    + WrappingInto<i8>
480    + WrappingInto<isize>
481    + WrappingInto<u128>
482    + WrappingInto<u16>
483    + WrappingInto<u32>
484    + WrappingInto<u64>
485    + WrappingInto<u8>
486    + WrappingInto<usize>
487    + WrappingMul<Self, Output = Self>
488    + WrappingMulAssign<Self>
489    + WrappingNeg<Output = Self>
490    + WrappingNegAssign
491    + WrappingPow<u64, Output = Self>
492    + WrappingPowAssign<u64>
493    + WrappingSquare<Output = Self>
494    + WrappingSquareAssign
495    + WrappingSub<Self, Output = Self>
496    + WrappingSubAssign<Self>
497    + WrappingSubMul<Self, Self, Output = Self>
498    + WrappingSubMulAssign<Self, Self>
499    + Zero
500{
501    /// The number of bits of `Self`.
502    const WIDTH: u64;
503
504    /// The base-2 logarithm of the number of bits of `Self`.
505    ///
506    /// Whenever you need to use `n / WIDTH`, you can use `n >> LOG_WIDTH` instead.
507    ///
508    /// This is $\log_2 W$.
509    ///
510    /// Note that this value is correct for all of the built-in primitive integer types, but it will
511    /// not be correct for custom types whose $W$ is not a power of 2. For such implementations,
512    /// `LOG_WIDTH` should not be used.
513    const LOG_WIDTH: u64 = Self::WIDTH.trailing_zeros() as u64;
514
515    /// A mask that consists of `LOG_WIDTH` bits.
516    ///
517    /// Whenever you need to use `n % WIDTH`, you can use `n & WIDTH_MASK` instead.
518    ///
519    /// This is $W - 1$.
520    ///
521    /// Note that this value is correct for all of the built-in primitive integer types, but it will
522    /// not be correct for custom types whose $W$ is not a power of 2. For such implementations,
523    /// `WIDTH_MASK` should not be used.
524    const WIDTH_MASK: u64 = Self::WIDTH - 1;
525
526    /// Gets the most-significant bit of `Self`. For signed integers, this is the sign bit.
527    ///
528    /// If `Self` is unsigned, $f(n) = (n \geq 2^{W-1})$. If `Self` is unsigned, $f(n) = (n < 0)$.
529    ///
530    /// # Worst-case complexity
531    /// Constant time and additional memory.
532    ///
533    /// # Examples
534    /// ```
535    /// use malachite_base::num::basic::integers::PrimitiveInt;
536    ///
537    /// assert_eq!(123u32.get_highest_bit(), false);
538    /// assert_eq!(4000000000u32.get_highest_bit(), true);
539    /// assert_eq!(2000000000i32.get_highest_bit(), false);
540    /// assert_eq!((-2000000000i32).get_highest_bit(), true);
541    /// ```
542    #[inline]
543    fn get_highest_bit(&self) -> bool {
544        self.get_bit(Self::WIDTH - 1)
545    }
546}
547
548#[cfg(not(feature = "random"))]
549/// Defines functions on primitive integer types: uxx, ixx, usize, and isize.
550///
551/// The different types are distinguished by whether they are signed or unsigned, and by their
552/// widths. The width $W$ is the number of bits in the type. For example, the width of [`u32`] or
553/// [`i32`] is 32. Each type has $2^W$ distinct values.
554///
555/// Let $n$ be a value of type `Self`. If `Self` is unsigned, $0 \leq n < 2^W$. If `Self` is signed,
556/// $2^{W-1} \leq n < 2^{W-1}$.
557pub trait PrimitiveInt:
558    'static
559    + AbsDiff<Self>
560    + Add<Self, Output = Self>
561    + AddAssign<Self>
562    + AddMul<Self, Self, Output = Self>
563    + AddMulAssign<Self, Self>
564    + ArithmeticCheckedShl<i128, Output = Self>
565    + ArithmeticCheckedShl<i16, Output = Self>
566    + ArithmeticCheckedShl<i32, Output = Self>
567    + ArithmeticCheckedShl<i64, Output = Self>
568    + ArithmeticCheckedShl<i8, Output = Self>
569    + ArithmeticCheckedShl<isize, Output = Self>
570    + ArithmeticCheckedShl<u128, Output = Self>
571    + ArithmeticCheckedShl<u16, Output = Self>
572    + ArithmeticCheckedShl<u32, Output = Self>
573    + ArithmeticCheckedShl<u64, Output = Self>
574    + ArithmeticCheckedShl<u8, Output = Self>
575    + ArithmeticCheckedShl<usize, Output = Self>
576    + ArithmeticCheckedShr<i128, Output = Self>
577    + ArithmeticCheckedShr<i16, Output = Self>
578    + ArithmeticCheckedShr<i32, Output = Self>
579    + ArithmeticCheckedShr<i64, Output = Self>
580    + ArithmeticCheckedShr<i8, Output = Self>
581    + ArithmeticCheckedShr<isize, Output = Self>
582    + Binary
583    + BinomialCoefficient<Self>
584    + BitAccess
585    + BitAnd<Self, Output = Self>
586    + BitAndAssign<Self>
587    + BitBlockAccess
588    + BitConvertible
589    + BitIterable
590    + BitOr<Self, Output = Self>
591    + BitOrAssign<Self>
592    + BitScan
593    + BitXor<Self, Output = Self>
594    + BitXorAssign<Self>
595    + CeilingRoot<u64, Output = Self>
596    + CeilingRootAssign<u64>
597    + CeilingSqrt<Output = Self>
598    + CeilingSqrtAssign
599    + CheckedAdd<Self, Output = Self>
600    + CheckedAddMul<Self, Self, Output = Self>
601    + CheckedBinomialCoefficient<Self>
602    + CheckedDiv<Self, Output = Self>
603    + CheckedMul<Self, Output = Self>
604    + CheckedNeg<Output = Self>
605    + CheckedPow<u64, Output = Self>
606    + CheckedRoot<u64, Output = Self>
607    + CheckedSqrt<Output = Self>
608    + CheckedSquare<Output = Self>
609    + CheckedSub<Self, Output = Self>
610    + CheckedSubMul<Self, Self, Output = Self>
611    + Clone
612    + ConvertibleFrom<f32>
613    + ConvertibleFrom<f64>
614    + ConvertibleFrom<i128>
615    + ConvertibleFrom<i16>
616    + ConvertibleFrom<i32>
617    + ConvertibleFrom<i64>
618    + ConvertibleFrom<i8>
619    + ConvertibleFrom<isize>
620    + ConvertibleFrom<u128>
621    + ConvertibleFrom<u16>
622    + ConvertibleFrom<u32>
623    + ConvertibleFrom<u64>
624    + ConvertibleFrom<u8>
625    + ConvertibleFrom<usize>
626    + Copy
627    + CountOnes
628    + CountZeros
629    + Debug
630    + Default
631    + Display
632    + Div<Self, Output = Self>
633    + DivAssign<Self>
634    + DivAssignMod<Self, ModOutput = Self>
635    + DivAssignRem<Self, RemOutput = Self>
636    + DivExact<Self, Output = Self>
637    + DivExactAssign<Self>
638    + DivMod<Self, DivOutput = Self, ModOutput = Self>
639    + DivRem<Self, DivOutput = Self, RemOutput = Self>
640    + DivRound<Self, Output = Self>
641    + DivRoundAssign<Self>
642    + DivisibleBy<Self>
643    + DivisibleByPowerOf2
644    + Eq
645    + EqAbs<Self>
646    + EqMod<Self, Self>
647    + EqModPowerOf2<Self>
648    + ExactFrom<i128>
649    + ExactFrom<i16>
650    + ExactFrom<i32>
651    + ExactFrom<i64>
652    + ExactFrom<i8>
653    + ExactFrom<isize>
654    + ExactFrom<u128>
655    + ExactFrom<u16>
656    + ExactFrom<u32>
657    + ExactFrom<u64>
658    + ExactFrom<u8>
659    + ExactFrom<usize>
660    + ExactInto<i128>
661    + ExactInto<i16>
662    + ExactInto<i32>
663    + ExactInto<i64>
664    + ExactInto<i8>
665    + ExactInto<isize>
666    + ExactInto<u128>
667    + ExactInto<u16>
668    + ExactInto<u32>
669    + ExactInto<u64>
670    + ExactInto<u8>
671    + ExactInto<usize>
672    + ExtendedGcd<Self>
673    + FloorRoot<u64, Output = Self>
674    + FloorRootAssign<u64>
675    + FloorSqrt<Output = Self>
676    + FloorSqrtAssign
677    + From<bool>
678    + FromSciString
679    + FromStr
680    + FromStringBase
681    + Hash
682    + IsInteger
683    + JacobiSymbol<Self>
684    + KroneckerSymbol<Self>
685    + LeadingZeros
686    + LegendreSymbol<Self>
687    + LowMask
688    + LowerHex
689    + Max
690    + Min
691    + Mod<Self, Output = Self>
692    + ModAssign<Self>
693    + ModPowerOf2
694    + ModPowerOf2Assign
695    + Mul<Self, Output = Self>
696    + MulAssign<Self>
697    + Named
698    + Not<Output = Self>
699    + NotAssign
700    + Octal
701    + One
702    + Ord
703    + OrdAbs
704    + OverflowingAdd<Self, Output = Self>
705    + OverflowingAddAssign<Self>
706    + OverflowingAddMul<Self, Self, Output = Self>
707    + OverflowingAddMulAssign<Self, Self>
708    + OverflowingDiv<Self, Output = Self>
709    + OverflowingDivAssign<Self>
710    + OverflowingFrom<i128>
711    + OverflowingFrom<i16>
712    + OverflowingFrom<i32>
713    + OverflowingFrom<i64>
714    + OverflowingFrom<i8>
715    + OverflowingFrom<isize>
716    + OverflowingFrom<u128>
717    + OverflowingFrom<u16>
718    + OverflowingFrom<u32>
719    + OverflowingFrom<u64>
720    + OverflowingFrom<u8>
721    + OverflowingFrom<usize>
722    + OverflowingInto<i128>
723    + OverflowingInto<i16>
724    + OverflowingInto<i32>
725    + OverflowingInto<i64>
726    + OverflowingInto<i8>
727    + OverflowingInto<isize>
728    + OverflowingInto<u128>
729    + OverflowingInto<u16>
730    + OverflowingInto<u32>
731    + OverflowingInto<u64>
732    + OverflowingInto<u8>
733    + OverflowingInto<usize>
734    + OverflowingMul<Self, Output = Self>
735    + OverflowingMulAssign<Self>
736    + OverflowingNeg<Output = Self>
737    + OverflowingNegAssign
738    + OverflowingPow<u64, Output = Self>
739    + OverflowingPowAssign<u64>
740    + OverflowingSquare<Output = Self>
741    + OverflowingSquareAssign
742    + OverflowingSub<Self, Output = Self>
743    + OverflowingSubAssign<Self>
744    + OverflowingSubMul<Self, Self, Output = Self>
745    + OverflowingSubMulAssign<Self, Self>
746    + Parity
747    + PartialEq<Self>
748    + PartialOrd<Self>
749    + PartialOrdAbs<Self>
750    + Pow<u64, Output = Self>
751    + PowAssign<u64>
752    + PowerOf2<u64>
753    + Product
754    + RefUnwindSafe
755    + Rem<Self, Output = Self>
756    + RemAssign<Self>
757    + RemPowerOf2<Output = Self>
758    + RemPowerOf2Assign
759    + RotateLeft<Output = Self>
760    + RotateLeftAssign
761    + RotateRight<Output = Self>
762    + RotateRightAssign
763    + RoundToMultiple<Self, Output = Self>
764    + RoundToMultipleAssign<Self>
765    + RoundToMultipleOfPowerOf2<u64, Output = Self>
766    + RoundToMultipleOfPowerOf2Assign<u64>
767    + RoundingFrom<f32>
768    + RoundingFrom<f64>
769    + RoundingInto<f32>
770    + RoundingInto<f64>
771    + SaturatingAdd<Self, Output = Self>
772    + SaturatingAddAssign<Self>
773    + SaturatingAddMul<Self, Self, Output = Self>
774    + SaturatingAddMulAssign<Self, Self>
775    + SaturatingFrom<i128>
776    + SaturatingFrom<i16>
777    + SaturatingFrom<i32>
778    + SaturatingFrom<i64>
779    + SaturatingFrom<i8>
780    + SaturatingFrom<isize>
781    + SaturatingFrom<u128>
782    + SaturatingFrom<u16>
783    + SaturatingFrom<u32>
784    + SaturatingFrom<u64>
785    + SaturatingFrom<u8>
786    + SaturatingFrom<usize>
787    + SaturatingInto<i128>
788    + SaturatingInto<i16>
789    + SaturatingInto<i32>
790    + SaturatingInto<i64>
791    + SaturatingInto<i8>
792    + SaturatingInto<isize>
793    + SaturatingInto<u128>
794    + SaturatingInto<u16>
795    + SaturatingInto<u32>
796    + SaturatingInto<u64>
797    + SaturatingInto<u8>
798    + SaturatingInto<usize>
799    + SaturatingMul<Self, Output = Self>
800    + SaturatingMulAssign<Self>
801    + SaturatingPow<u64, Output = Self>
802    + SaturatingPowAssign<u64>
803    + SaturatingSquare<Output = Self>
804    + SaturatingSquareAssign
805    + SaturatingSub<Self, Output = Self>
806    + SaturatingSubAssign<Self>
807    + SaturatingSubMul<Self, Self, Output = Self>
808    + SaturatingSubMulAssign<Self, Self>
809    + Shl<i128, Output = Self>
810    + Shl<i16, Output = Self>
811    + Shl<i32, Output = Self>
812    + Shl<i64, Output = Self>
813    + Shl<i8, Output = Self>
814    + Shl<u128, Output = Self>
815    + Shl<u16, Output = Self>
816    + Shl<u32, Output = Self>
817    + Shl<u64, Output = Self>
818    + Shl<u8, Output = Self>
819    + ShlAssign<i128>
820    + ShlAssign<i16>
821    + ShlAssign<i32>
822    + ShlAssign<i64>
823    + ShlAssign<i8>
824    + ShlAssign<isize>
825    + ShlAssign<u128>
826    + ShlAssign<u16>
827    + ShlAssign<u32>
828    + ShlAssign<u64>
829    + ShlAssign<u8>
830    + ShlAssign<usize>
831    + ShlRound<i128, Output = Self>
832    + ShlRound<i16, Output = Self>
833    + ShlRound<i32, Output = Self>
834    + ShlRound<i64, Output = Self>
835    + ShlRound<i8, Output = Self>
836    + ShlRound<isize, Output = Self>
837    + ShlRoundAssign<i128>
838    + ShlRoundAssign<i16>
839    + ShlRoundAssign<i32>
840    + ShlRoundAssign<i64>
841    + ShlRoundAssign<i8>
842    + ShlRoundAssign<isize>
843    + Shr<i128, Output = Self>
844    + Shr<i16, Output = Self>
845    + Shr<i32, Output = Self>
846    + Shr<i64, Output = Self>
847    + Shr<i8, Output = Self>
848    + Shr<isize, Output = Self>
849    + Shr<u128, Output = Self>
850    + Shr<u16, Output = Self>
851    + Shr<u32, Output = Self>
852    + Shr<u64, Output = Self>
853    + Shr<u8, Output = Self>
854    + Shr<usize, Output = Self>
855    + ShrAssign<i128>
856    + ShrAssign<i16>
857    + ShrAssign<i32>
858    + ShrAssign<i64>
859    + ShrAssign<i8>
860    + ShrAssign<isize>
861    + ShrAssign<u128>
862    + ShrAssign<u16>
863    + ShrAssign<u32>
864    + ShrAssign<u64>
865    + ShrAssign<u8>
866    + ShrAssign<usize>
867    + ShrRound<i128, Output = Self>
868    + ShrRound<i16, Output = Self>
869    + ShrRound<i32, Output = Self>
870    + ShrRound<i64, Output = Self>
871    + ShrRound<i8, Output = Self>
872    + ShrRound<isize, Output = Self>
873    + ShrRound<u128, Output = Self>
874    + ShrRound<u16, Output = Self>
875    + ShrRound<u32, Output = Self>
876    + ShrRound<u64, Output = Self>
877    + ShrRound<u8, Output = Self>
878    + ShrRound<usize, Output = Self>
879    + ShrRoundAssign<i128>
880    + ShrRoundAssign<i16>
881    + ShrRoundAssign<i32>
882    + ShrRoundAssign<i64>
883    + ShrRoundAssign<i8>
884    + ShrRoundAssign<isize>
885    + ShrRoundAssign<u128>
886    + ShrRoundAssign<u16>
887    + ShrRoundAssign<u32>
888    + ShrRoundAssign<u64>
889    + ShrRoundAssign<u8>
890    + ShrRoundAssign<usize>
891    + Sign
892    + SignificantBits
893    + Sized
894    + Square<Output = Self>
895    + SquareAssign
896    + Sub<Self, Output = Self>
897    + SubAssign<Self>
898    + SubMul<Self, Self, Output = Self>
899    + SubMulAssign<Self, Self>
900    + Sum<Self>
901    + ToSci
902    + ToStringBase
903    + TrailingZeros
904    + TryFrom<NiceFloat<f32>>
905    + TryFrom<i128>
906    + TryFrom<i16>
907    + TryFrom<i32>
908    + TryFrom<i64>
909    + TryFrom<i8>
910    + TryFrom<isize>
911    + TryFrom<u128>
912    + TryFrom<u16>
913    + TryFrom<u32>
914    + TryFrom<u64>
915    + TryFrom<u8>
916    + TryFrom<usize>
917    + TryInto<NiceFloat<f32>>
918    + TryInto<i128>
919    + TryInto<i16>
920    + TryInto<i32>
921    + TryInto<i64>
922    + TryInto<i8>
923    + TryInto<isize>
924    + TryInto<u128>
925    + TryInto<u16>
926    + TryInto<u32>
927    + TryInto<u64>
928    + TryInto<u8>
929    + TryInto<usize>
930    + Two
931    + UnwindSafe
932    + UpperHex
933    + WrappingAdd<Self, Output = Self>
934    + WrappingAddAssign<Self>
935    + WrappingAddMul<Self, Self, Output = Self>
936    + WrappingAddMulAssign<Self, Self>
937    + WrappingDiv<Self, Output = Self>
938    + WrappingDivAssign<Self>
939    + WrappingFrom<i128>
940    + WrappingFrom<i16>
941    + WrappingFrom<i32>
942    + WrappingFrom<i64>
943    + WrappingFrom<i8>
944    + WrappingFrom<isize>
945    + WrappingFrom<u128>
946    + WrappingFrom<u16>
947    + WrappingFrom<u32>
948    + WrappingFrom<u64>
949    + WrappingFrom<u8>
950    + WrappingFrom<usize>
951    + WrappingInto<i128>
952    + WrappingInto<i16>
953    + WrappingInto<i32>
954    + WrappingInto<i64>
955    + WrappingInto<i8>
956    + WrappingInto<isize>
957    + WrappingInto<u128>
958    + WrappingInto<u16>
959    + WrappingInto<u32>
960    + WrappingInto<u64>
961    + WrappingInto<u8>
962    + WrappingInto<usize>
963    + WrappingMul<Self, Output = Self>
964    + WrappingMulAssign<Self>
965    + WrappingNeg<Output = Self>
966    + WrappingNegAssign
967    + WrappingPow<u64, Output = Self>
968    + WrappingPowAssign<u64>
969    + WrappingSquare<Output = Self>
970    + WrappingSquareAssign
971    + WrappingSub<Self, Output = Self>
972    + WrappingSubAssign<Self>
973    + WrappingSubMul<Self, Self, Output = Self>
974    + WrappingSubMulAssign<Self, Self>
975    + Zero
976{
977    /// The number of bits of `Self`.
978    const WIDTH: u64;
979
980    /// The base-2 logarithm of the number of bits of `Self`.
981    ///
982    /// Whenever you need to use `n / WIDTH`, you can use `n >> LOG_WIDTH` instead.
983    ///
984    /// This is $\log_2 W$.
985    ///
986    /// Note that this value is correct for all of the built-in primitive integer types, but it will
987    /// not be correct for custom types whose $W$ is not a power of 2. For such implementations,
988    /// `LOG_WIDTH` should not be used.
989    const LOG_WIDTH: u64 = Self::WIDTH.trailing_zeros() as u64;
990
991    /// A mask that consists of `LOG_WIDTH` bits.
992    ///
993    /// Whenever you need to use `n % WIDTH`, you can use `n & WIDTH_MASK` instead.
994    ///
995    /// This is $W - 1$.
996    ///
997    /// Note that this value is correct for all of the built-in primitive integer types, but it will
998    /// not be correct for custom types whose $W$ is not a power of 2. For such implementations,
999    /// `WIDTH_MASK` should not be used.
1000    const WIDTH_MASK: u64 = Self::WIDTH - 1;
1001
1002    /// Gets the most-significant bit of `Self`. For signed integers, this is the sign bit.
1003    ///
1004    /// If `Self` is unsigned, $f(n) = (n \geq 2^{W-1})$. If `Self` is unsigned, $f(n) = (n < 0)$.
1005    ///
1006    /// # Worst-case complexity
1007    /// Constant time and additional memory.
1008    ///
1009    /// # Examples
1010    /// ```
1011    /// use malachite_base::num::basic::integers::PrimitiveInt;
1012    ///
1013    /// assert_eq!(123u32.get_highest_bit(), false);
1014    /// assert_eq!(4000000000u32.get_highest_bit(), true);
1015    /// assert_eq!(2000000000i32.get_highest_bit(), false);
1016    /// assert_eq!((-2000000000i32).get_highest_bit(), true);
1017    /// ```
1018    #[inline]
1019    fn get_highest_bit(&self) -> bool {
1020        self.get_bit(Self::WIDTH - 1)
1021    }
1022}
1023
1024/// Defines basic trait implementations that are the same for unsigned and signed types.
1025macro_rules! impl_basic_traits_primitive_int {
1026    ($t:ident, $width:expr) => {
1027        /// # Examples
1028        ///
1029        /// See [here](self).
1030        impl PrimitiveInt for $t {
1031            const WIDTH: u64 = $width;
1032        }
1033
1034        impl_named!($t);
1035
1036        /// The constant 0.
1037        ///
1038        /// # Examples
1039        /// See [here](self).
1040        impl Zero for $t {
1041            const ZERO: $t = 0;
1042        }
1043
1044        /// The constant 1.
1045        ///
1046        /// # Examples
1047        /// See [here](self).
1048        impl One for $t {
1049            const ONE: $t = 1;
1050        }
1051
1052        /// The constant 2.
1053        ///
1054        /// # Examples
1055        /// See [here](self).
1056        impl Two for $t {
1057            const TWO: $t = 2;
1058        }
1059
1060        /// The lowest value representable by this type.
1061        ///
1062        /// If `Self` is unsigned, `MIN` is 0. If `Self` is signed, `MIN` is $-2^{W-1}$.
1063        ///
1064        /// # Examples
1065        /// See [here](self).
1066        impl Min for $t {
1067            const MIN: $t = $t::MIN;
1068        }
1069
1070        /// The highest value representable by this type.
1071        ///
1072        /// If `Self` is unsigned, `MAX` is $2^W-1$. If `Self` is signed, `MAX` is $2^{W-1}-1$.
1073        ///
1074        /// # Examples
1075        /// See [here](self).
1076        impl Max for $t {
1077            const MAX: $t = $t::MAX;
1078        }
1079    };
1080}
1081impl_basic_traits_primitive_int!(u8, 8);
1082impl_basic_traits_primitive_int!(u16, 16);
1083impl_basic_traits_primitive_int!(u32, 32);
1084impl_basic_traits_primitive_int!(u64, 64);
1085impl_basic_traits_primitive_int!(u128, 128);
1086impl_basic_traits_primitive_int!(usize, 0usize.trailing_zeros() as u64);
1087impl_basic_traits_primitive_int!(i8, 8);
1088impl_basic_traits_primitive_int!(i16, 16);
1089impl_basic_traits_primitive_int!(i32, 32);
1090impl_basic_traits_primitive_int!(i64, 64);
1091impl_basic_traits_primitive_int!(i128, 128);
1092impl_basic_traits_primitive_int!(isize, 0usize.trailing_zeros() as u64);