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