Skip to main content

malachite_base/num/arithmetic/
traits.rs

1// Copyright © 2026 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::num::basic::traits::Two;
10use crate::rounding_modes::RoundingMode;
11use core::cmp::Ordering;
12
13/// Takes the absolute value of a number. Assumes that the number has a representable absolute
14/// value.
15pub trait Abs {
16    type Output;
17
18    fn abs(self) -> Self::Output;
19}
20
21/// Replaces a number with its absolute value. Assumes that the number has a representable absolute
22/// value.
23pub trait AbsAssign {
24    fn abs_assign(&mut self);
25}
26
27/// Takes the absolute value of a number and converts to the unsigned equivalent.
28pub trait UnsignedAbs {
29    type Output;
30
31    fn unsigned_abs(self) -> Self::Output;
32}
33
34/// Subtracts two numbers and takes the absolute value of the difference.
35pub trait AbsDiff<RHS = Self> {
36    type Output;
37
38    fn abs_diff(self, other: RHS) -> Self::Output;
39}
40
41/// Replaces a number with the absolute value of its difference with another number.
42pub trait AbsDiffAssign<RHS = Self> {
43    fn abs_diff_assign(&mut self, other: RHS);
44}
45
46/// Adds a number and the product of two other numbers.
47pub trait AddMul<Y = Self, Z = Self> {
48    type Output;
49
50    fn add_mul(self, y: Y, z: Z) -> Self::Output;
51}
52
53/// Adds a number and the product of two other numbers, in place.
54pub trait AddMulAssign<Y = Self, Z = Self> {
55    fn add_mul_assign(&mut self, y: Y, z: Z);
56}
57
58/// Calculates the AGM (arithmetic-geometric mean) of two numbers.
59pub trait Agm<RHS = Self> {
60    type Output;
61
62    fn agm(self, other: RHS) -> Self::Output;
63}
64
65/// Replaces a number with the AGM (arithmetic-geometric mean) of it and another number.
66pub trait AgmAssign<RHS = Self> {
67    fn agm_assign(&mut self, other: RHS);
68}
69
70/// Left-shifts a number (multiplies it by a power of 2), returning `None` if the result is not
71/// representable.
72pub trait ArithmeticCheckedShl<RHS> {
73    type Output;
74
75    fn arithmetic_checked_shl(self, other: RHS) -> Option<Self::Output>;
76}
77
78/// Right-shifts a number (divides it by a power of 2), returning `None` if the result is not
79/// representable.
80pub trait ArithmeticCheckedShr<RHS> {
81    type Output;
82
83    fn arithmetic_checked_shr(self, other: RHS) -> Option<Self::Output>;
84}
85
86pub trait BinomialCoefficient<T = Self> {
87    fn binomial_coefficient(n: T, k: T) -> Self;
88}
89
90pub trait CheckedBinomialCoefficient<T = Self>: Sized {
91    fn checked_binomial_coefficient(n: T, k: T) -> Option<Self>;
92}
93
94/// Takes the ceiling of a number.
95pub trait Ceiling {
96    type Output;
97
98    fn ceiling(self) -> Self::Output;
99}
100
101/// Replaces a number with its ceiling.
102pub trait CeilingAssign {
103    fn ceiling_assign(&mut self);
104}
105
106/// Takes the absolute valie of a number, returning `None` if the result is not representable.
107pub trait CheckedAbs {
108    type Output;
109
110    fn checked_abs(self) -> Option<Self::Output>;
111}
112
113/// Adds two numbers, returning `None` if the result is not representable.
114pub trait CheckedAdd<RHS = Self> {
115    type Output;
116
117    fn checked_add(self, other: RHS) -> Option<Self::Output>;
118}
119
120/// Adds a number and the product of two other numbers, returning `None` if the result is not
121/// representable.
122pub trait CheckedAddMul<Y = Self, Z = Self> {
123    type Output;
124
125    fn checked_add_mul(self, y: Y, z: Z) -> Option<Self::Output>;
126}
127
128/// Divides two numbers, returning `None` if the result is not representable.
129pub trait CheckedDiv<RHS = Self> {
130    type Output;
131
132    fn checked_div(self, other: RHS) -> Option<Self::Output>;
133}
134
135/// Multiplies two numbers, returning `None` if the result is not representable.
136pub trait CheckedMul<RHS = Self> {
137    type Output;
138
139    fn checked_mul(self, other: RHS) -> Option<Self::Output>;
140}
141
142/// Negates a number, returning `None` if the result is not representable.
143pub trait CheckedNeg {
144    type Output;
145
146    fn checked_neg(self) -> Option<Self::Output>;
147}
148
149/// Finds the smallest integer power of 2 greater than or equal to a number, returning `None` if the
150/// result is not representable.
151pub trait CheckedNextPowerOf2 {
152    type Output;
153
154    fn checked_next_power_of_2(self) -> Option<Self::Output>;
155}
156
157/// Raises a number to a power, returning `None` if the result is not representable.
158pub trait CheckedPow<RHS> {
159    type Output;
160
161    fn checked_pow(self, exp: RHS) -> Option<Self::Output>;
162}
163
164/// Squares a number, returning `None` if the result is not representable.
165pub trait CheckedSquare {
166    type Output;
167
168    fn checked_square(self) -> Option<Self::Output>;
169}
170
171/// Subtracts two numbers, returning `None` if the result is not representable.
172pub trait CheckedSub<RHS = Self> {
173    type Output;
174
175    fn checked_sub(self, other: RHS) -> Option<Self::Output>;
176}
177
178/// Subtracts a number by the product of two other numbers, returning `None` if the result is not
179/// representable.
180pub trait CheckedSubMul<Y = Self, Z = Self> {
181    type Output;
182
183    fn checked_sub_mul(self, y: Y, z: Z) -> Option<Self::Output>;
184}
185
186/// Determines whether two numbers are coprime.
187pub trait CoprimeWith<RHS = Self> {
188    fn coprime_with(self, other: RHS) -> bool;
189}
190
191/// Divides two numbers, assuming the first exactly divides the second.
192///
193/// If it doesn't, the `div_exact` function may panic or return a meaningless result.
194pub trait DivExact<RHS = Self> {
195    type Output;
196
197    fn div_exact(self, other: RHS) -> Self::Output;
198}
199
200/// Divides a number by another number in place, assuming the first exactly divides the second.
201///
202/// If it doesn't, this function may panic or assign a meaningless number to the first number.
203pub trait DivExactAssign<RHS = Self> {
204    fn div_exact_assign(&mut self, other: RHS);
205}
206
207/// Divides two numbers, returning the quotient and remainder. The quotient is rounded towards
208/// negative infinity, and the remainder has the same sign as the divisor (second input).
209///
210/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
211pub trait DivMod<RHS = Self> {
212    type DivOutput;
213    type ModOutput;
214
215    fn div_mod(self, other: RHS) -> (Self::DivOutput, Self::ModOutput);
216}
217
218/// Divides two numbers, returning the quotient and remainder. The quotient is rounded towards the
219/// quotient that makes the remainder nonnegative, and the remainder is always nonnegative.
220///
221/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq r < |y|$.
222pub trait DivEuclidean<RHS = Self> {
223    type DivOutput;
224    type ModOutput;
225
226    fn div_euclidean(self, other: RHS) -> (Self::DivOutput, Self::ModOutput);
227}
228
229/// Divides a number by another number in place, returning the remainder. The quotient is rounded
230/// towards negative infinity, and the remainder has the same sign as the divisor (second input).
231///
232/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
233pub trait DivAssignMod<RHS = Self> {
234    type ModOutput;
235
236    fn div_assign_mod(&mut self, other: RHS) -> Self::ModOutput;
237}
238
239/// Divides a number by another number in place, returning the remainder. The quotient is rounded
240/// towards the quotient that makes the remainder nonnegative, and the remainder is always
241/// nonnegative.
242///
243/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq r < |y|$.
244pub trait DivAssignEuclidean<RHS = Self> {
245    type ModOutput;
246
247    fn div_assign_euclidean(&mut self, other: RHS) -> Self::ModOutput;
248}
249
250/// Divides two numbers, returning the quotient and remainder. The quotient is rounded towards zero,
251/// and the remainder has the same sign as the dividend (first input).
252///
253/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
254pub trait DivRem<RHS = Self> {
255    type DivOutput;
256    type RemOutput;
257
258    fn div_rem(self, other: RHS) -> (Self::DivOutput, Self::RemOutput);
259}
260
261/// Divides a number by another number in place, returning the remainder. The quotient is rounded
262/// towards zero, and the remainder has the same sign as the dividend (first input).
263///
264/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
265pub trait DivAssignRem<RHS = Self> {
266    type RemOutput;
267
268    fn div_assign_rem(&mut self, other: RHS) -> Self::RemOutput;
269}
270
271/// Divides a number by another number, returning the ceiling of the quotient and the remainder of
272/// the negative of the first number divided by the second.
273///
274/// The quotient and remainder satisfy $x = qy - r$ and $0 \leq r < y$.
275pub trait CeilingDivNegMod<RHS = Self> {
276    type DivOutput;
277    type ModOutput;
278
279    fn ceiling_div_neg_mod(self, other: RHS) -> (Self::DivOutput, Self::ModOutput);
280}
281
282/// Divides a number by another number in place, taking the ceiling of the quotient and returning
283/// the remainder of the negative of the first number divided by the second.
284///
285/// The quotient and remainder satisfy $x = qy - r$ and $0 \leq r < y$.
286pub trait CeilingDivAssignNegMod<RHS = Self> {
287    type ModOutput;
288
289    fn ceiling_div_assign_neg_mod(&mut self, other: RHS) -> Self::ModOutput;
290}
291
292/// Divides a number by another number, returning the quotient and remainder. The quotient is
293/// rounded towards positive infinity and the remainder has the opposite sign as the divisor (second
294/// input).
295///
296/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
297pub trait CeilingDivMod<RHS = Self> {
298    type DivOutput;
299    type ModOutput;
300
301    fn ceiling_div_mod(self, other: RHS) -> (Self::DivOutput, Self::ModOutput);
302}
303
304/// Divides a number by another number in place, taking the quotient and returning the remainder.
305/// The quotient is rounded towards positive infinity and the remainder has the opposite sign of the
306/// divisor (second input).
307///
308/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
309pub trait CeilingDivAssignMod<RHS = Self> {
310    type ModOutput;
311
312    fn ceiling_div_assign_mod(&mut self, other: RHS) -> Self::ModOutput;
313}
314
315/// Divides a number by another number and rounds according to a specified rounding mode. An
316/// [`Ordering`] is also returned, indicating whether the returned value is less than, equal to, or
317/// greater than the exact value.
318pub trait DivRound<RHS = Self> {
319    type Output;
320
321    fn div_round(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
322}
323
324/// Divides a number by another number in place and rounds according to a specified rounding mode.
325/// An [`Ordering`] is returned, indicating whether the assigned value is less than, equal to, or
326/// greater than the exact value.
327pub trait DivRoundAssign<RHS = Self> {
328    fn div_round_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
329}
330
331/// Determines whether a number is divisible by $2^k$.
332pub trait DivisibleByPowerOf2 {
333    fn divisible_by_power_of_2(self, pow: u64) -> bool;
334}
335
336/// Determines whether a number is divisible by another number.
337pub trait DivisibleBy<RHS = Self> {
338    fn divisible_by(self, other: RHS) -> bool;
339}
340
341/// Determines whether a number is equivalent to another number modulo $2^k$.
342pub trait EqModPowerOf2<RHS = Self> {
343    fn eq_mod_power_of_2(self, other: RHS, pow: u64) -> bool;
344}
345
346/// Determines whether a number is equivalent to another number modulo $m$.
347pub trait EqMod<RHS = Self, M = Self> {
348    fn eq_mod(self, other: RHS, m: M) -> bool;
349}
350
351/// Computes the GCD (greatest common divisor) of two numbers $a$ and $b$, and also the coefficients
352/// $x$ and $y$ in Bézout's identity $ax+by=\gcd(a,b)$.
353///
354/// The are infinitely many $x$, $y$ that satisfy the identity, so the full specification is more
355/// detailed:
356///
357/// - $f(0, 0) = (0, 0, 0)$.
358/// - $f(a, ak) = (a, 1, 0)$ if $a > 0$ and $k \neq 1$.
359/// - $f(a, ak) = (-a, -1, 0)$ if $a < 0$ and $k \neq 1$.
360/// - $f(bk, b) = (b, 0, 1)$ if $b > 0$.
361/// - $f(bk, b) = (-b, 0, -1)$ if $b < 0$.
362/// - $f(a, b) = (g, x, y)$ if $a \neq 0$ and $b \neq 0$ and $\gcd(a, b) \neq \min(|a|, |b|)$, where
363///   $g = \gcd(a, b) \geq 0$, $ax + by = g$, $x \leq \lfloor b/g \rfloor$, and $y \leq \lfloor a/g
364///   \rfloor$.
365pub trait ExtendedGcd<RHS = Self> {
366    type Gcd;
367    type Cofactor;
368
369    fn extended_gcd(self, other: RHS) -> (Self::Gcd, Self::Cofactor, Self::Cofactor);
370}
371
372/// Computes the factorial of a `u64`.
373pub trait Factorial {
374    fn factorial(n: u64) -> Self;
375}
376
377/// Computes the factorial of a `u64`, returning `None` if the result is too large to be
378/// represented.
379pub trait CheckedFactorial: Sized {
380    fn checked_factorial(n: u64) -> Option<Self>;
381}
382
383/// Computes the double factorial of a `u64`. The double factorial of a non-negative integer is the
384/// product of all the positive integers that are less than or equal to it and have the same parity
385/// as it.
386pub trait DoubleFactorial {
387    fn double_factorial(n: u64) -> Self;
388}
389
390/// Computes the double factorial of a `u64`, returning `None` if the result is too large to be
391/// represented. The double factorial of a non-negative integer is the product of all the positive
392/// integers that are less than or equal to it and have the same parity as it.
393pub trait CheckedDoubleFactorial: Sized {
394    fn checked_double_factorial(n: u64) -> Option<Self>;
395}
396
397/// Computes the $m$-multifactorial of a `u64`. The $m$-multifactorial of a non-negative integer $n$
398/// is the product of all integers $k$ such that $0<k\leq n$ and $k\equiv n \pmod m$.
399pub trait Multifactorial {
400    fn multifactorial(n: u64, m: u64) -> Self;
401}
402
403/// Computes the $m$-multifactorial of a `u64`, returning `None` if the result is too large to be
404/// represented. The $m$-multifactorial of a non-negative integer $n$ is the product of all integers
405/// $k$ such that $0<k\leq n$ and $k\equiv n \pmod m$.
406pub trait CheckedMultifactorial: Sized {
407    fn checked_multifactorial(n: u64, m: u64) -> Option<Self>;
408}
409
410/// Computes the subfactorial of a `u64`. The subfactorial of a non-negative integer $n$ counts the
411/// number of derangements of $n$ elements, which are the permutations in which no element is fixed.
412pub trait Subfactorial {
413    fn subfactorial(n: u64) -> Self;
414}
415
416/// Computes the subfactorial of a `u64`, returning `None` if the result is too large to be
417/// represented. The subfactorial of a non-negative integer $n$ counts the number of derangements of
418/// $n$ elements, which are the permutations in which no element is fixed.
419pub trait CheckedSubfactorial: Sized {
420    fn checked_subfactorial(n: u64) -> Option<Self>;
421}
422
423/// Takes the floor of a number.
424pub trait Floor {
425    type Output;
426
427    fn floor(self) -> Self::Output;
428}
429
430/// Replaces a number with its floor.
431pub trait FloorAssign {
432    fn floor_assign(&mut self);
433}
434
435/// Calculates the GCD (greatest common divisor) of two numbers.
436pub trait Gcd<RHS = Self> {
437    type Output;
438
439    fn gcd(self, other: RHS) -> Self::Output;
440}
441
442/// Replaces a number with the GCD (greatest common divisor) of it and another number.
443pub trait GcdAssign<RHS = Self> {
444    fn gcd_assign(&mut self, other: RHS);
445}
446
447/// Determines whether a number is an integer power of 2.
448pub trait IsPowerOf2 {
449    fn is_power_of_2(&self) -> bool;
450}
451
452/// Calculates the LCM (least common multiple) of two numbers.
453pub trait Lcm<RHS = Self> {
454    type Output;
455
456    fn lcm(self, other: RHS) -> Self::Output;
457}
458
459/// Replaces a number with the LCM (least common multiple) of it and another number.
460pub trait LcmAssign<RHS = Self> {
461    fn lcm_assign(&mut self, other: RHS);
462}
463
464/// Takes the natural logarithm of a number.
465pub trait Ln {
466    type Output;
467
468    fn ln(self) -> Self::Output;
469}
470
471/// Replaces a number with its natural logarithm.
472pub trait LnAssign {
473    fn ln_assign(&mut self);
474}
475
476/// Computes $\ln(1+x)$.
477pub trait Ln1PlusX {
478    type Output;
479
480    fn ln_1_plus_x(self) -> Self::Output;
481}
482
483/// Replaces a number $x$ by $\ln(1+x)$.
484pub trait Ln1PlusXAssign {
485    fn ln_1_plus_x_assign(&mut self);
486}
487
488/// Calculates the LCM (least common multiple) of two numbers, returning `None` if the result is not
489/// representable.
490pub trait CheckedLcm<RHS = Self> {
491    type Output;
492
493    fn checked_lcm(self, other: RHS) -> Option<Self::Output>;
494}
495
496/// Calculates the Legendre symbol of two numbers. Typically the implementations will be identical
497/// to those of [`JacobiSymbol`].
498pub trait LegendreSymbol<RHS = Self> {
499    fn legendre_symbol(self, other: RHS) -> i8;
500}
501
502/// Calculates the Jacobi symbol of two numbers.
503pub trait JacobiSymbol<RHS = Self> {
504    fn jacobi_symbol(self, other: RHS) -> i8;
505}
506
507/// Calculates the Kronecker symbol of two numbers.
508pub trait KroneckerSymbol<RHS = Self> {
509    fn kronecker_symbol(self, other: RHS) -> i8;
510}
511
512/// Calculates the base-$b$ logarithm of a number, or returns `None` if the number is not a perfect
513/// power of $b$.
514pub trait CheckedLogBase<B = Self> {
515    type Output;
516
517    fn checked_log_base(self, base: B) -> Option<Self::Output>;
518}
519
520/// Calculates the floor of the base-$b$ logarithm of a number.
521pub trait FloorLogBase<B = Self> {
522    type Output;
523
524    fn floor_log_base(self, base: B) -> Self::Output;
525}
526
527/// Calculates the ceiling of the base-$b$ logarithm of a number.
528pub trait CeilingLogBase<B = Self> {
529    type Output;
530
531    fn ceiling_log_base(self, base: B) -> Self::Output;
532}
533
534/// Calculates the base-2 logarithm of a number, or returns `None` if the number is not a perfect
535/// power of 2.
536pub trait CheckedLogBase2 {
537    type Output;
538
539    fn checked_log_base_2(self) -> Option<Self::Output>;
540}
541
542/// Calculates the base-2 logarithm of a number.
543pub trait LogBase2 {
544    type Output;
545
546    fn log_base_2(self) -> Self::Output;
547}
548
549/// Replaces a number with its base-2 logarithm.
550pub trait LogBase2Assign {
551    fn log_base_2_assign(&mut self);
552}
553
554/// Calculates the base-10 logarithm of a number, rounding the (generally irrational) result.
555pub trait LogBase10 {
556    type Output;
557
558    fn log_base_10(self) -> Self::Output;
559}
560
561/// Replaces a number with its base-10 logarithm, rounding the (generally irrational) result.
562pub trait LogBase10Assign {
563    fn log_base_10_assign(&mut self);
564}
565
566/// Computes $\log_2(1+x)$.
567pub trait LogBase2Of1PlusX {
568    type Output;
569
570    fn log_base_2_1_plus_x(self) -> Self::Output;
571}
572
573/// Replaces a number $x$ by $\log_2(1+x)$.
574pub trait LogBase2Of1PlusXAssign {
575    fn log_base_2_1_plus_x_assign(&mut self);
576}
577
578/// Computes $\log_{2^k}(1+x)$.
579pub trait LogBasePowerOf2Of1PlusX<POW> {
580    type Output;
581
582    fn log_base_power_of_2_1_plus_x(self, pow: POW) -> Self::Output;
583}
584
585/// Replaces a number $x$ by $\log_{2^k}(1+x)$.
586pub trait LogBasePowerOf2Of1PlusXAssign<POW> {
587    fn log_base_power_of_2_1_plus_x_assign(&mut self, pow: POW);
588}
589
590/// Computes $\log_b(1+x)$ for an integer base $b$.
591pub trait LogBaseOf1PlusX<B = Self> {
592    type Output;
593
594    fn log_base_1_plus_x(self, base: B) -> Self::Output;
595}
596
597/// Replaces a number $x$ by $\log_b(1+x)$ for an integer base $b$.
598pub trait LogBaseOf1PlusXAssign<B = Self> {
599    fn log_base_1_plus_x_assign(&mut self, base: B);
600}
601
602/// Computes $\log_{10}(1+x)$.
603pub trait LogBase10Of1PlusX {
604    type Output;
605
606    fn log_base_10_1_plus_x(self) -> Self::Output;
607}
608
609/// Replaces a number $x$ by $\log_{10}(1+x)$.
610pub trait LogBase10Of1PlusXAssign {
611    fn log_base_10_1_plus_x_assign(&mut self);
612}
613
614/// Calculates the floor of the base-2 logarithm of a number.
615pub trait FloorLogBase2 {
616    type Output;
617
618    fn floor_log_base_2(self) -> Self::Output;
619}
620
621/// Calculates the ceiling of the base-2 logarithm of a number.
622pub trait CeilingLogBase2 {
623    type Output;
624
625    fn ceiling_log_base_2(self) -> Self::Output;
626}
627
628/// Calculates the base-$2^k$ logarithm of a number, or returns `None` if the number is not a
629/// perfect power of $2^k$.
630pub trait CheckedLogBasePowerOf2<POW> {
631    type Output;
632
633    fn checked_log_base_power_of_2(self, pow: POW) -> Option<Self::Output>;
634}
635
636/// Calculates the floor of the base-$2^k$ logarithm of a number.
637pub trait FloorLogBasePowerOf2<POW> {
638    type Output;
639
640    fn floor_log_base_power_of_2(self, pow: POW) -> Self::Output;
641}
642
643/// Calculates the ceiling of the base-$2^k$ logarithm of a number.
644pub trait CeilingLogBasePowerOf2<POW> {
645    type Output;
646
647    fn ceiling_log_base_power_of_2(self, pow: POW) -> Self::Output;
648}
649
650/// Calculates the base-$2^k$ logarithm of a number.
651pub trait LogBasePowerOf2<POW> {
652    type Output;
653
654    fn log_base_power_of_2(self, pow: POW) -> Self::Output;
655}
656
657/// Replaces a number with its base-$2^k$ logarithm.
658pub trait LogBasePowerOf2Assign<POW> {
659    fn log_base_power_of_2_assign(&mut self, pow: POW);
660}
661
662/// Calculates the base-$b$ logarithm of a number, rounding the (generally irrational) result.
663pub trait LogBase<B = Self> {
664    type Output;
665
666    fn log_base(self, base: B) -> Self::Output;
667}
668
669/// Replaces a number with its base-$b$ logarithm, rounding the (generally irrational) result.
670pub trait LogBaseAssign<B = Self> {
671    fn log_base_assign(&mut self, base: B);
672}
673
674/// Adds two numbers modulo a third number $m$. The inputs must be already reduced modulo $m$.
675pub trait ModAdd<RHS = Self, M = Self> {
676    type Output;
677
678    fn mod_add(self, other: RHS, m: M) -> Self::Output;
679}
680
681/// Adds two numbers modulo a third number $m$, in place. The inputs must be already reduced modulo
682/// $m$.
683pub trait ModAddAssign<RHS = Self, M = Self> {
684    fn mod_add_assign(&mut self, other: RHS, m: M);
685}
686
687/// Finds the multiplicative inverse of a number modulo another number $m$. The input must be
688/// already reduced modulo $m$.
689pub trait ModInverse<M = Self> {
690    type Output;
691
692    fn mod_inverse(self, m: M) -> Option<Self::Output>;
693}
694
695/// Checks whether a number is reduced modulo another number $m$.
696pub trait ModIsReduced<M = Self> {
697    fn mod_is_reduced(&self, m: &M) -> bool;
698}
699
700/// Multiplies two numbers modulo a third number $m$. The inputs must be already reduced modulo $m$.
701pub trait ModMul<RHS = Self, M = Self> {
702    type Output;
703
704    fn mod_mul(self, other: RHS, m: M) -> Self::Output;
705}
706
707/// Multiplies two numbers modulo a third number $m$, in place. The inputs must be already reduced
708/// modulo $m$.
709pub trait ModMulAssign<RHS = Self, M = Self> {
710    fn mod_mul_assign(&mut self, other: RHS, m: M);
711}
712
713/// Multiplies two numbers modulo a third number $m$. The inputs must be already reduced modulo $m$.
714///
715/// If multiple modular multiplications with the same modulus are necessary, it can be quicker to
716/// precompute some piece of data and reuse it in the multiplication calls. This trait provides a
717/// function for precomputing the data and a function for using it during multiplication.
718pub trait ModMulPrecomputed<RHS = Self, M = Self> {
719    type Output;
720    type Data;
721
722    /// Precomputes some data to use for modular multiplication.
723    fn precompute_mod_mul_data(m: &M) -> Self::Data;
724
725    fn mod_mul_precomputed(self, other: RHS, m: M, data: &Self::Data) -> Self::Output;
726}
727
728/// Multiplies two numbers modulo a third number $m$, in place.The inputs must be already reduced
729/// modulo $m$.
730///
731/// If multiple modular multiplications with the same modulus are necessary, it can be quicker to
732/// precompute some piece of data and reuse it in the multiplication calls. This trait provides a
733/// function for using precomputed data during multiplication. For precomputing the data, use the
734/// [`precompute_mod_mul_data`](ModMulPrecomputed::precompute_mod_mul_data) function in
735/// [`ModMulPrecomputed`].
736pub trait ModMulPrecomputedAssign<RHS = Self, M = Self>: ModMulPrecomputed<RHS, M> {
737    fn mod_mul_precomputed_assign(&mut self, other: RHS, m: M, data: &Self::Data);
738}
739
740/// Negates a number modulo another number $m$. The input must be already reduced modulo $m$.
741pub trait ModNeg<M = Self> {
742    type Output;
743
744    fn mod_neg(self, m: M) -> Self::Output;
745}
746
747/// Negates a number modulo another number $m$, in place. The input must be already reduced modulo
748/// $m$.
749pub trait ModNegAssign<M = Self> {
750    fn mod_neg_assign(&mut self, m: M);
751}
752
753/// Divides a number by another number, returning just the remainder. The remainder has the same
754/// sign as the divisor (second number).
755///
756/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
757/// |r| < |y|$.
758pub trait Mod<RHS = Self> {
759    type Output;
760
761    fn mod_op(self, other: RHS) -> Self::Output;
762}
763
764/// Divides a number by another number, replacing the first number by the remainder. The remainder
765/// has the same sign as the divisor (second number).
766///
767/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
768/// |r| < |y|$.
769pub trait ModAssign<RHS = Self> {
770    fn mod_assign(&mut self, other: RHS);
771}
772
773/// Divides the negative of a number by another number, returning the remainder.
774///
775/// If the quotient were computed, the quotient and remainder would satisfy $x = qy - r$ and $0 \leq
776/// r < y$.
777pub trait NegMod<RHS = Self> {
778    type Output;
779
780    fn neg_mod(self, other: RHS) -> Self::Output;
781}
782
783/// Divides the negative of a number by another number, replacing the first number by the remainder.
784///
785/// If the quotient were computed, the quotient and remainder would satisfy $x = qy - r$ and $0 \leq
786/// r < y$.
787pub trait NegModAssign<RHS = Self> {
788    fn neg_mod_assign(&mut self, other: RHS);
789}
790
791/// Divides a number by another number, returning just the remainder. The remainder has the opposite
792/// sign as the divisor (second number).
793///
794/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
795/// |r| < |y|$.
796pub trait CeilingMod<RHS = Self> {
797    type Output;
798
799    fn ceiling_mod(self, other: RHS) -> Self::Output;
800}
801
802/// Divides a number by another number, replacing the first number by the remainder. The remainder
803/// has the same sign as the divisor (second number).
804///
805/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
806/// |r| < |y|$.
807pub trait CeilingModAssign<RHS = Self> {
808    fn ceiling_mod_assign(&mut self, other: RHS);
809}
810
811/// Raises a number to a power modulo another number $m$. The base must be already reduced modulo
812/// $m$.
813pub trait ModPow<RHS = Self, M = Self> {
814    type Output;
815
816    fn mod_pow(self, exp: RHS, m: M) -> Self::Output;
817}
818
819/// Raises a number to a power modulo another number $m$, in place. The base must be already reduced
820/// modulo $m$.
821pub trait ModPowAssign<RHS = Self, M = Self> {
822    fn mod_pow_assign(&mut self, exp: RHS, m: M);
823}
824
825/// Raises a number to a power modulo another number $m$. The base must be already reduced modulo
826/// $m$.
827///
828/// If multiple modular exponentiations with the same modulus are necessary, it can be quicker to
829/// precompute some piece of data and reuse it in the exponentiation calls. This trait provides a
830/// function for precomputing the data and a function for using it during exponentiation.
831pub trait ModPowPrecomputed<RHS = Self, M = Self>
832where
833    Self: Sized,
834{
835    type Output;
836    type Data;
837
838    /// Precomputes some data to use for modular exponentiation.
839    fn precompute_mod_pow_data(m: &M) -> Self::Data;
840
841    fn mod_pow_precomputed(self, exp: RHS, m: M, data: &Self::Data) -> Self::Output;
842}
843
844/// Raises a number to a power modulo another number $m$, in place. The base must be already reduced
845/// modulo $m$.
846///
847/// If multiple modular exponentiations with the same modulus are necessary, it can be quicker to
848/// precompute some piece of data and reuse it in the exponentiation calls. This trait provides a
849/// function for using precomputed data during exponentiation. For precomputing the data, use the
850/// [`precompute_mod_pow_data`](ModPowPrecomputed::precompute_mod_pow_data) function in
851/// [`ModPowPrecomputed`].
852pub trait ModPowPrecomputedAssign<RHS: Two = Self, M = Self>: ModPowPrecomputed<RHS, M> {
853    fn mod_pow_precomputed_assign(&mut self, exp: RHS, m: M, data: &Self::Data);
854}
855
856/// Adds two numbers modulo $2^k$. The inputs must be already reduced modulo $2^k$.
857pub trait ModPowerOf2Add<RHS = Self> {
858    type Output;
859
860    fn mod_power_of_2_add(self, other: RHS, pow: u64) -> Self::Output;
861}
862
863/// Adds two numbers modulo $2^k$, in place. The inputs must be already reduced modulo $2^k$.
864pub trait ModPowerOf2AddAssign<RHS = Self> {
865    fn mod_power_of_2_add_assign(&mut self, other: RHS, pow: u64);
866}
867
868/// Finds the multiplicative inverse of a number modulo $2^k$. The input must be already reduced
869/// modulo $2^k$.
870pub trait ModPowerOf2Inverse {
871    type Output;
872
873    fn mod_power_of_2_inverse(self, pow: u64) -> Option<Self::Output>;
874}
875
876/// Checks whether a number is reduced modulo $2^k$.
877pub trait ModPowerOf2IsReduced {
878    fn mod_power_of_2_is_reduced(&self, pow: u64) -> bool;
879}
880
881/// Multiplies two numbers modulo $2^k$. The inputs must be already reduced modulo $2^k$.
882pub trait ModPowerOf2Mul<RHS = Self> {
883    type Output;
884
885    fn mod_power_of_2_mul(self, other: RHS, pow: u64) -> Self::Output;
886}
887
888/// Multiplies two numbers modulo $2^k$, in place. The inputs must be already reduced modulo $2^k$.
889pub trait ModPowerOf2MulAssign<RHS = Self> {
890    fn mod_power_of_2_mul_assign(&mut self, other: RHS, pow: u64);
891}
892
893/// Negates a number modulo $2^k$. The input must be already reduced modulo $2^k$.
894pub trait ModPowerOf2Neg {
895    type Output;
896
897    fn mod_power_of_2_neg(self, pow: u64) -> Self::Output;
898}
899
900/// Negates a number modulo $2^k$ in place. The input must be already reduced modulo $2^k$.
901pub trait ModPowerOf2NegAssign {
902    fn mod_power_of_2_neg_assign(&mut self, pow: u64);
903}
904
905/// Raises a number to a power modulo $2^k$. The base must be already reduced modulo $2^k$.
906pub trait ModPowerOf2Pow<RHS = Self> {
907    type Output;
908
909    fn mod_power_of_2_pow(self, exp: RHS, pow: u64) -> Self::Output;
910}
911
912/// Raises a number to a power modulo $2^k$, in place. The base must be already reduced modulo
913/// $2^k$.
914pub trait ModPowerOf2PowAssign<RHS = Self> {
915    fn mod_power_of_2_pow_assign(&mut self, exp: RHS, pow: u64);
916}
917
918/// Left-shifts a number (multiplies it by a power of 2) modulo $2^k$. The number must be already
919/// reduced modulo $2^k$.
920pub trait ModPowerOf2Shl<RHS> {
921    type Output;
922
923    fn mod_power_of_2_shl(self, other: RHS, pow: u64) -> Self::Output;
924}
925
926/// Left-shifts a number (multiplies it by a power of 2) modulo $2^k$, in place. The number must be
927/// already reduced modulo $2^k$.
928pub trait ModPowerOf2ShlAssign<RHS> {
929    fn mod_power_of_2_shl_assign(&mut self, other: RHS, pow: u64);
930}
931
932/// Right-shifts a number (divides it by a power of 2) modulo $2^k$. The number must be already
933/// reduced modulo $2^k$.
934pub trait ModPowerOf2Shr<RHS> {
935    type Output;
936
937    fn mod_power_of_2_shr(self, other: RHS, pow: u64) -> Self::Output;
938}
939
940/// Right-shifts a number (divides it by a power of 2) modulo $2^k$, in place. The number must be
941/// already reduced modulo $2^k$.
942pub trait ModPowerOf2ShrAssign<RHS> {
943    fn mod_power_of_2_shr_assign(&mut self, other: RHS, pow: u64);
944}
945
946/// Squares a number modulo $2^k$. The input must be already reduced modulo $2^k$.
947pub trait ModPowerOf2Square {
948    type Output;
949
950    fn mod_power_of_2_square(self, pow: u64) -> Self::Output;
951}
952
953/// Squares a number modulo $2^k$ in place. The input must be already reduced modulo $2^k$.
954pub trait ModPowerOf2SquareAssign {
955    fn mod_power_of_2_square_assign(&mut self, pow: u64);
956}
957
958/// Subtracts two numbers modulo $2^k$. The inputs must be already reduced modulo $2^k$.
959pub trait ModPowerOf2Sub<RHS = Self> {
960    type Output;
961
962    fn mod_power_of_2_sub(self, other: RHS, pow: u64) -> Self::Output;
963}
964
965/// Subtracts two numbers modulo $2^k$, in place. The inputs must be already reduced modulo $2^k$.
966pub trait ModPowerOf2SubAssign<RHS = Self> {
967    fn mod_power_of_2_sub_assign(&mut self, other: RHS, pow: u64);
968}
969
970/// Divides a number by $2^k$, returning just the remainder. The remainder is non-negative.
971///
972/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
973/// \leq r < 2^k$.
974pub trait ModPowerOf2 {
975    type Output;
976
977    fn mod_power_of_2(self, other: u64) -> Self::Output;
978}
979
980/// Divides a number by $2^k$, replacing the number by the remainder. The remainder is non-negative.
981///
982/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
983/// \leq r < 2^k$.
984pub trait ModPowerOf2Assign {
985    fn mod_power_of_2_assign(&mut self, other: u64);
986}
987
988/// Divides a number by $2^k$, returning just the remainder. The remainder has the same sign as the
989/// number.
990///
991/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
992/// \leq |r| < 2^k$.
993pub trait RemPowerOf2 {
994    type Output;
995
996    fn rem_power_of_2(self, other: u64) -> Self::Output;
997}
998
999/// Divides a number by $2^k$, replacing the number by the remainder. The remainder has the same
1000/// sign as the number.
1001///
1002/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
1003/// \leq |r| < 2^k$.
1004pub trait RemPowerOf2Assign {
1005    fn rem_power_of_2_assign(&mut self, other: u64);
1006}
1007
1008/// Divides the negative of a number by $2^k$, returning the remainder.
1009///
1010/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k - r$ and $0
1011/// \leq r < 2^k$.
1012pub trait NegModPowerOf2 {
1013    type Output;
1014
1015    fn neg_mod_power_of_2(self, other: u64) -> Self::Output;
1016}
1017
1018/// Divides the negative of a number by $2^k$, replacing the number by the remainder.
1019///
1020/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k - r$ and $0
1021/// \leq r < 2^k$.
1022pub trait NegModPowerOf2Assign {
1023    fn neg_mod_power_of_2_assign(&mut self, other: u64);
1024}
1025
1026/// Divides a number by $2^k$, returning just the remainder. The remainder is non-positive.
1027///
1028/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
1029/// \leq -r < 2^k$.
1030pub trait CeilingModPowerOf2 {
1031    type Output;
1032
1033    fn ceiling_mod_power_of_2(self, other: u64) -> Self::Output;
1034}
1035
1036/// Divides a number by $2^k$, replacing the number by the remainder. The remainder is non-positive.
1037///
1038/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
1039/// \leq -r < 2^k$.
1040pub trait CeilingModPowerOf2Assign {
1041    fn ceiling_mod_power_of_2_assign(&mut self, other: u64);
1042}
1043
1044/// Left-shifts a number (multiplies it by a power of 2) modulo another number $m$. The number must
1045/// be already reduced modulo $m$.
1046pub trait ModShl<RHS, M = Self> {
1047    type Output;
1048
1049    fn mod_shl(self, other: RHS, m: M) -> Self::Output;
1050}
1051
1052/// Left-shifts a number (multiplies it by a power of 2) modulo another number $m$, in place. The
1053/// number must be already reduced modulo $m$.
1054pub trait ModShlAssign<RHS, M = Self> {
1055    fn mod_shl_assign(&mut self, other: RHS, m: M);
1056}
1057
1058/// Left-shifts a number (divides it by a power of 2) modulo another number $m$. The number must be
1059/// already reduced modulo $m$.
1060pub trait ModShr<RHS, M = Self> {
1061    type Output;
1062
1063    fn mod_shr(self, other: RHS, m: M) -> Self::Output;
1064}
1065
1066/// Left-shifts a number (divides it by a power of 2) modulo another number $m$, in place. The
1067/// number must be already reduced modulo $m$.
1068pub trait ModShrAssign<RHS, M = Self> {
1069    fn mod_shr_assign(&mut self, other: RHS, m: M);
1070}
1071
1072/// Squares a number modulo another number $m$. The input must be already reduced modulo $m$.
1073pub trait ModSquare<M = Self> {
1074    type Output;
1075
1076    fn mod_square(self, m: M) -> Self::Output;
1077}
1078
1079/// Squares a number modulo another number $m$, in place. The input must be already reduced modulo
1080/// $m$.
1081pub trait ModSquareAssign<M = Self> {
1082    fn mod_square_assign(&mut self, m: M);
1083}
1084
1085/// Squares a number modulo another number $m$. The input must be already reduced modulo $m$.
1086///
1087/// If multiple modular squarings with the same modulus are necessary, it can be quicker to
1088/// precompute some piece of data using
1089/// [`precompute_mod_pow_data`](ModPowPrecomputed::precompute_mod_pow_data) function in
1090/// [`ModMulPrecomputed`] and reuse it in the squaring calls.
1091pub trait ModSquarePrecomputed<RHS = Self, M = Self>: ModPowPrecomputed<RHS, M>
1092where
1093    Self: Sized,
1094{
1095    fn mod_square_precomputed(self, m: M, data: &Self::Data) -> Self::Output;
1096}
1097
1098/// Squares a number modulo another number $m$, in place. The input must be already reduced modulo
1099/// $m$.
1100///
1101/// If multiple modular squarings with the same modulus are necessary, it can be quicker to
1102/// precompute some piece of data using
1103/// [`precompute_mod_pow_data`](ModPowPrecomputed::precompute_mod_pow_data) function in
1104/// [`ModMulPrecomputed`] and reuse it in the squaring calls.
1105pub trait ModSquarePrecomputedAssign<RHS = Self, M = Self>: ModPowPrecomputed<RHS, M> {
1106    fn mod_square_precomputed_assign(&mut self, m: M, data: &Self::Data);
1107}
1108
1109/// Adds two numbers modulo a third number $m$. The inputs must be already reduced modulo $m$.
1110pub trait ModSub<RHS = Self, M = Self> {
1111    type Output;
1112
1113    fn mod_sub(self, other: RHS, m: M) -> Self::Output;
1114}
1115
1116/// Adds two numbers modulo a third number $m$, in place. The inputs must be already reduced modulo
1117/// $m$.
1118pub trait ModSubAssign<RHS = Self, M = Self> {
1119    fn mod_sub_assign(&mut self, other: RHS, m: M);
1120}
1121
1122/// Replaces a number with its negative. Assumes the result is representable.
1123pub trait NegAssign {
1124    fn neg_assign(&mut self);
1125}
1126
1127/// Returns the smallest power of 2 greater than or equal to a number. Assumes the result is
1128/// representable.
1129pub trait NextPowerOf2 {
1130    type Output;
1131
1132    fn next_power_of_2(self) -> Self::Output;
1133}
1134
1135/// Replaces a number with the smallest power of 2 greater than or equal it. Assumes the result is
1136/// representable.
1137pub trait NextPowerOf2Assign {
1138    fn next_power_of_2_assign(&mut self);
1139}
1140
1141/// Takes the absolute value of a number.
1142///
1143/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
1144/// occurred. If an overflow occurred, then the wrapped number is returned.
1145pub trait OverflowingAbs {
1146    type Output;
1147
1148    fn overflowing_abs(self) -> (Self::Output, bool);
1149}
1150
1151/// Replaces a number with its absolute value.
1152///
1153/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1154/// then the wrapped number is assigned.
1155pub trait OverflowingAbsAssign {
1156    fn overflowing_abs_assign(&mut self) -> bool;
1157}
1158
1159/// Adds two numbers.
1160///
1161/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1162/// occurred. If an overflow occurred, then the wrapped number is returned.
1163pub trait OverflowingAdd<RHS = Self> {
1164    type Output;
1165
1166    fn overflowing_add(self, other: RHS) -> (Self::Output, bool);
1167}
1168
1169/// Adds a number to another number in place.
1170///
1171/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1172/// then the wrapped number is assigned.
1173pub trait OverflowingAddAssign<RHS = Self> {
1174    fn overflowing_add_assign(&mut self, other: RHS) -> bool;
1175}
1176
1177/// Adds a number and the product of two other numbers.
1178///
1179/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
1180/// occurred. If an overflow occurred, then the wrapped number is returned.
1181pub trait OverflowingAddMul<Y = Self, Z = Self> {
1182    type Output;
1183
1184    fn overflowing_add_mul(self, y: Y, z: Z) -> (Self::Output, bool);
1185}
1186
1187/// Adds a number and the product of two other numbers, in place.
1188///
1189/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
1190/// occurred. If an overflow occurred, then the wrapped number is returned.
1191pub trait OverflowingAddMulAssign<Y = Self, Z = Self> {
1192    fn overflowing_add_mul_assign(&mut self, y: Y, z: Z) -> bool;
1193}
1194
1195/// Divides two numbers.
1196///
1197/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1198/// occurred. If an overflow occurred, then the wrapped number is returned.
1199pub trait OverflowingDiv<RHS = Self> {
1200    type Output;
1201
1202    fn overflowing_div(self, other: RHS) -> (Self::Output, bool);
1203}
1204
1205/// Divides a number by another number in place.
1206///
1207/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1208/// then the wrapped number is assigned.
1209pub trait OverflowingDivAssign<RHS = Self> {
1210    fn overflowing_div_assign(&mut self, other: RHS) -> bool;
1211}
1212
1213/// Multiplies two numbers.
1214///
1215/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1216/// occurred. If an overflow occurred, then the wrapped number is returned.
1217pub trait OverflowingMul<RHS = Self> {
1218    type Output;
1219
1220    fn overflowing_mul(self, other: RHS) -> (Self::Output, bool);
1221}
1222
1223/// Multiplies a number by another number in place.
1224///
1225/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1226/// then the wrapped number is assigned.
1227pub trait OverflowingMulAssign<RHS = Self> {
1228    fn overflowing_mul_assign(&mut self, other: RHS) -> bool;
1229}
1230
1231/// Negates a number.
1232///
1233/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1234/// occurred. If an overflow occurred, then the wrapped number is returned.
1235pub trait OverflowingNeg {
1236    type Output;
1237
1238    fn overflowing_neg(self) -> (Self::Output, bool);
1239}
1240
1241/// Negates a number in place.
1242///
1243/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1244/// then the wrapped number is assigned.
1245pub trait OverflowingNegAssign {
1246    fn overflowing_neg_assign(&mut self) -> bool;
1247}
1248
1249/// Raises a number to a power.
1250///
1251/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1252/// occurred. If an overflow occurred, then the wrapped number is returned.
1253pub trait OverflowingPow<RHS> {
1254    type Output;
1255
1256    fn overflowing_pow(self, exp: RHS) -> (Self::Output, bool);
1257}
1258
1259/// Raises a number to a power in place.
1260///
1261/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1262/// then the wrapped number is assigned.
1263pub trait OverflowingPowAssign<RHS = Self> {
1264    fn overflowing_pow_assign(&mut self, exp: RHS) -> bool;
1265}
1266
1267/// Squares a number.
1268///
1269/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1270/// occurred. If an overflow occurred, then the wrapped number is returned.
1271pub trait OverflowingSquare {
1272    type Output;
1273
1274    fn overflowing_square(self) -> (Self::Output, bool);
1275}
1276
1277/// Squares a number in place.
1278///
1279/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1280/// then the wrapped number is assigned.
1281pub trait OverflowingSquareAssign {
1282    fn overflowing_square_assign(&mut self) -> bool;
1283}
1284
1285/// Subtracts two numbers.
1286///
1287/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1288/// occurred. If an overflow occurred, then the wrapped number is returned.
1289pub trait OverflowingSub<RHS = Self> {
1290    type Output;
1291
1292    fn overflowing_sub(self, other: RHS) -> (Self::Output, bool);
1293}
1294
1295/// Subtracts a number by another number in place.
1296///
1297/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1298/// then the wrapped number is assigned.
1299pub trait OverflowingSubAssign<RHS = Self> {
1300    fn overflowing_sub_assign(&mut self, other: RHS) -> bool;
1301}
1302
1303/// Subtracts a number by the product of two other numbers.
1304///
1305/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
1306/// occurred. If an overflow occurred, then the wrapped number is returned.
1307pub trait OverflowingSubMul<Y = Self, Z = Self> {
1308    type Output;
1309
1310    fn overflowing_sub_mul(self, y: Y, z: Z) -> (Self::Output, bool);
1311}
1312
1313/// Subtracts a number by the product of two other numbers, in place.
1314///
1315/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
1316/// occurred. If an overflow occurred, then the wrapped number is returned.
1317pub trait OverflowingSubMulAssign<Y = Self, Z = Self> {
1318    fn overflowing_sub_mul_assign(&mut self, y: Y, z: Z) -> bool;
1319}
1320
1321/// Determines whether a number is even or odd.
1322pub trait Parity {
1323    /// Determines whether a number is even.
1324    fn even(self) -> bool;
1325
1326    /// Determines whether a number is odd.
1327    fn odd(self) -> bool;
1328}
1329
1330/// Raises a number to a power. Assumes the result is representable.
1331pub trait Pow<RHS> {
1332    type Output;
1333
1334    fn pow(self, exp: RHS) -> Self::Output;
1335}
1336
1337/// Raises a number to a power in place. Assumes the result is representable.
1338pub trait PowAssign<RHS = Self> {
1339    fn pow_assign(&mut self, exp: RHS);
1340}
1341
1342/// Raises 2 to a power.
1343pub trait PowerOf2<POW> {
1344    fn power_of_2(pow: POW) -> Self;
1345}
1346
1347pub trait Primorial {
1348    fn primorial(n: u64) -> Self;
1349
1350    fn product_of_first_n_primes(n: u64) -> Self;
1351}
1352
1353pub trait CheckedPrimorial: Sized {
1354    fn checked_primorial(n: u64) -> Option<Self>;
1355
1356    fn checked_product_of_first_n_primes(n: u64) -> Option<Self>;
1357}
1358
1359/// Finds the reciprocal (multiplicative inverse) of a number.
1360pub trait Reciprocal {
1361    type Output;
1362
1363    fn reciprocal(self) -> Self::Output;
1364}
1365
1366/// Replaces a number with its reciprocal (multiplicative inverse).
1367pub trait ReciprocalAssign {
1368    fn reciprocal_assign(&mut self);
1369}
1370
1371/// Takes the reciprocal of the square root of a number.
1372pub trait ReciprocalSqrt {
1373    type Output;
1374
1375    fn reciprocal_sqrt(self) -> Self::Output;
1376}
1377
1378/// Replaces a number with the reciprocal of its square root.
1379pub trait ReciprocalSqrtAssign {
1380    fn reciprocal_sqrt_assign(&mut self);
1381}
1382
1383/// Finds the floor of the $n$th root of a number.
1384pub trait FloorRoot<POW> {
1385    type Output;
1386
1387    fn floor_root(self, pow: POW) -> Self::Output;
1388}
1389
1390/// Replaces a number with the floor of its $n$th root.
1391pub trait FloorRootAssign<POW> {
1392    fn floor_root_assign(&mut self, pow: POW);
1393}
1394
1395/// Finds the ceiling of the $n$th root of a number.
1396pub trait CeilingRoot<POW> {
1397    type Output;
1398
1399    fn ceiling_root(self, pow: POW) -> Self::Output;
1400}
1401
1402/// Replaces a number with the ceiling of its $n$th root.
1403pub trait CeilingRootAssign<POW> {
1404    fn ceiling_root_assign(&mut self, pow: POW);
1405}
1406
1407/// Finds the $n$th root of a number, returning `None` if it is not a perfect $n$th power.
1408pub trait CheckedRoot<POW> {
1409    type Output;
1410
1411    fn checked_root(self, pow: POW) -> Option<Self::Output>;
1412}
1413
1414/// Finds the floor of the $n$th root of a number, returning both the root and the remainder.
1415pub trait RootRem<POW> {
1416    type RootOutput;
1417    type RemOutput;
1418
1419    fn root_rem(self, exp: POW) -> (Self::RootOutput, Self::RemOutput);
1420}
1421
1422/// Replaces a number with the floor of its $n$th root, returning the remainder.
1423pub trait RootAssignRem<POW> {
1424    type RemOutput;
1425
1426    fn root_assign_rem(&mut self, exp: POW) -> Self::RemOutput;
1427}
1428
1429/// Rotates a number left, inserting the leftmost bits into the right end.
1430pub trait RotateLeft {
1431    type Output;
1432
1433    fn rotate_left(self, n: u64) -> Self::Output;
1434}
1435
1436/// Rotates a number left, inserting the leftmost bits into the right end, in place.
1437pub trait RotateLeftAssign {
1438    fn rotate_left_assign(&mut self, n: u64);
1439}
1440
1441/// Rotates a number right, inserting the leftmost bits into the left end.
1442pub trait RotateRight {
1443    type Output;
1444
1445    fn rotate_right(self, n: u64) -> Self::Output;
1446}
1447
1448/// Rotates a number right, inserting the leftmost bits into the left end, in place.
1449pub trait RotateRightAssign {
1450    fn rotate_right_assign(&mut self, n: u64);
1451}
1452
1453/// Rounds a number to a multiple of another number, according to a specified rounding mode. An
1454/// [`Ordering`] is also returned, indicating whether the returned value is less than, equal to, or
1455/// greater than the original value.
1456pub trait RoundToMultiple<RHS = Self> {
1457    type Output;
1458
1459    fn round_to_multiple(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
1460}
1461
1462/// Rounds a number to a multiple of another number in place, according to a specified rounding
1463/// mode. [`Ordering`] is returned, indicating whether the returned value is less than, equal to, or
1464/// greater than the original value.
1465pub trait RoundToMultipleAssign<RHS = Self> {
1466    fn round_to_multiple_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
1467}
1468
1469/// Rounds a number to a multiple of $2^k$, according to a specified rounding mode. An [`Ordering`]
1470/// is also returned, indicating whether the returned value is less than, equal to, or greater than
1471/// the original value.
1472pub trait RoundToMultipleOfPowerOf2<RHS> {
1473    type Output;
1474
1475    fn round_to_multiple_of_power_of_2(
1476        self,
1477        pow: RHS,
1478        rm: RoundingMode,
1479    ) -> (Self::Output, Ordering);
1480}
1481
1482/// Rounds a number to a multiple of $2^k$ in place, according to a specified rounding mode. An
1483/// [`Ordering`] is returned, indicating whether the returned value is less than, equal to, or
1484/// greater than the original value.
1485pub trait RoundToMultipleOfPowerOf2Assign<RHS> {
1486    fn round_to_multiple_of_power_of_2_assign(&mut self, pow: RHS, rm: RoundingMode) -> Ordering;
1487}
1488
1489/// Takes the absolute value of a number, saturating at the numeric bounds instead of overflowing.
1490pub trait SaturatingAbs {
1491    type Output;
1492
1493    fn saturating_abs(self) -> Self::Output;
1494}
1495
1496/// Replaces a number with its absolute value, saturating at the numeric bounds instead of
1497/// overflowing.
1498pub trait SaturatingAbsAssign {
1499    fn saturating_abs_assign(&mut self);
1500}
1501
1502/// Adds two numbers, saturating at the numeric bounds instead of overflowing.
1503pub trait SaturatingAdd<RHS = Self> {
1504    type Output;
1505
1506    fn saturating_add(self, other: RHS) -> Self::Output;
1507}
1508
1509/// Add a number to another number in place, saturating at the numeric bounds instead of
1510/// overflowing.
1511pub trait SaturatingAddAssign<RHS = Self> {
1512    fn saturating_add_assign(&mut self, other: RHS);
1513}
1514
1515/// Adds a number and the product of two other numbers, saturating at the numeric bounds instead of
1516/// overflowing.
1517pub trait SaturatingAddMul<Y = Self, Z = Self> {
1518    type Output;
1519
1520    fn saturating_add_mul(self, y: Y, z: Z) -> Self::Output;
1521}
1522
1523/// Adds a number and the product of two other numbers in place, saturating at the numeric bounds
1524/// instead of overflowing.
1525pub trait SaturatingAddMulAssign<Y = Self, Z = Self> {
1526    fn saturating_add_mul_assign(&mut self, y: Y, z: Z);
1527}
1528
1529/// Multiplies two numbers, saturating at the numeric bounds instead of overflowing.
1530pub trait SaturatingMul<RHS = Self> {
1531    type Output;
1532
1533    fn saturating_mul(self, other: RHS) -> Self::Output;
1534}
1535
1536/// Multiplies a number by another number in place, saturating at the numeric bounds instead of
1537/// overflowing.
1538pub trait SaturatingMulAssign<RHS = Self> {
1539    fn saturating_mul_assign(&mut self, other: RHS);
1540}
1541
1542/// Negates a number, saturating at the numeric bounds instead of overflowing.
1543pub trait SaturatingNeg {
1544    type Output;
1545
1546    fn saturating_neg(self) -> Self::Output;
1547}
1548
1549/// Negates a number in place, saturating at the numeric bounds instead of overflowing.
1550pub trait SaturatingNegAssign {
1551    fn saturating_neg_assign(&mut self);
1552}
1553
1554/// Raises a number to a power, saturating at the numeric bounds instead of overflowing.
1555pub trait SaturatingPow<RHS> {
1556    type Output;
1557
1558    fn saturating_pow(self, exp: RHS) -> Self::Output;
1559}
1560
1561/// Raises a number to a power in place, saturating at the numeric bounds instead of overflowing.
1562pub trait SaturatingPowAssign<RHS = Self> {
1563    fn saturating_pow_assign(&mut self, exp: RHS);
1564}
1565
1566/// Squares a number, saturating at the numeric bounds instead of overflowing.
1567pub trait SaturatingSquare {
1568    type Output;
1569
1570    fn saturating_square(self) -> Self::Output;
1571}
1572
1573/// Squares a number in place, saturating at the numeric bounds instead of overflowing.
1574pub trait SaturatingSquareAssign {
1575    fn saturating_square_assign(&mut self);
1576}
1577
1578/// Subtracts two numbers, saturating at the numeric bounds instead of overflowing.
1579pub trait SaturatingSub<RHS = Self> {
1580    type Output;
1581
1582    fn saturating_sub(self, other: RHS) -> Self::Output;
1583}
1584
1585/// Subtracts a number by another number in place, saturating at the numeric bounds instead of
1586/// overflowing.
1587pub trait SaturatingSubAssign<RHS = Self> {
1588    fn saturating_sub_assign(&mut self, other: RHS);
1589}
1590
1591/// Subtracts a number by the product of two other numbers, saturating at the numeric bounds instead
1592/// of overflowing.
1593pub trait SaturatingSubMul<Y = Self, Z = Self> {
1594    type Output;
1595
1596    fn saturating_sub_mul(self, y: Y, z: Z) -> Self::Output;
1597}
1598
1599/// Subtracts a number by the product of two other numbers in place, saturating at the numeric
1600/// bounds instead of overflowing.
1601pub trait SaturatingSubMulAssign<Y = Self, Z = Self> {
1602    fn saturating_sub_mul_assign(&mut self, y: Y, z: Z);
1603}
1604
1605/// Left-shifts a number (multiplies it by a power of 2), rounding the result according to a
1606/// specified rounding mode. An [`Ordering`] is also returned, indicating whether the returned value
1607/// is less than, equal to, or greater than the exact value.
1608///
1609/// Rounding might only be necessary if `other` is negative.
1610pub trait ShlRound<RHS> {
1611    type Output;
1612
1613    fn shl_round(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
1614}
1615
1616/// Left-shifts a number (multiplies it by a power of 2) in place, rounding the result according to
1617/// a specified rounding mode. An [`Ordering`] is also returned, indicating whether the assigned
1618/// value is less than, equal to, or greater than the exact value.
1619///
1620/// Rounding might only be necessary if `other` is negative.
1621pub trait ShlRoundAssign<RHS> {
1622    fn shl_round_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
1623}
1624
1625/// Right-shifts a number (divides it by a power of 2), rounding the result according to a specified
1626/// rounding mode. An [`Ordering`] is also returned, indicating whether the returned value is less
1627/// than, equal to, or greater than the exact value.
1628///
1629/// Rounding might only be necessary if `other` is positive.
1630pub trait ShrRound<RHS> {
1631    type Output;
1632
1633    fn shr_round(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
1634}
1635
1636/// Right-shifts a number (divides it by a power of 2) in place, rounding the result according to a
1637/// specified rounding mode. An [`Ordering`] is also returned, indicating whether the assigned value
1638/// is less than, equal to, or greater than the exact value.
1639///
1640/// Rounding might only be necessary if `other` is positive.
1641pub trait ShrRoundAssign<RHS> {
1642    fn shr_round_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
1643}
1644
1645/// Returns `Greater`, `Equal`, or `Less`, depending on whether a number is positive, zero, or
1646/// negative, respectively.
1647pub trait Sign {
1648    fn sign(&self) -> Ordering;
1649}
1650
1651/// Takes the square root of a number.
1652pub trait Sqrt {
1653    type Output;
1654
1655    fn sqrt(self) -> Self::Output;
1656}
1657
1658/// Replaces a number with its square root.
1659pub trait SqrtAssign {
1660    fn sqrt_assign(&mut self);
1661}
1662
1663/// Finds the floor of the square root of a number.
1664pub trait FloorSqrt {
1665    type Output;
1666
1667    fn floor_sqrt(self) -> Self::Output;
1668}
1669
1670/// Replaces a number with the floor of its square root.
1671pub trait FloorSqrtAssign {
1672    fn floor_sqrt_assign(&mut self);
1673}
1674
1675/// Finds the ceiling of the square root of a number.
1676pub trait CeilingSqrt {
1677    type Output;
1678
1679    fn ceiling_sqrt(self) -> Self::Output;
1680}
1681
1682/// Replaces a number with the ceiling of its square root.
1683pub trait CeilingSqrtAssign {
1684    fn ceiling_sqrt_assign(&mut self);
1685}
1686
1687/// Finds the square root of a number, returning `None` if it is not a perfect square.
1688pub trait CheckedSqrt {
1689    type Output;
1690
1691    fn checked_sqrt(self) -> Option<Self::Output>;
1692}
1693
1694/// Finds the floor of the square root of a number, returning both the root and the remainder.
1695pub trait SqrtRem {
1696    type SqrtOutput;
1697    type RemOutput;
1698
1699    fn sqrt_rem(self) -> (Self::SqrtOutput, Self::RemOutput);
1700}
1701
1702/// Replaces a number with the floor of its square root, returning the remainder.
1703pub trait SqrtAssignRem {
1704    type RemOutput;
1705
1706    fn sqrt_assign_rem(&mut self) -> Self::RemOutput;
1707}
1708
1709/// Squares a number.
1710pub trait Square {
1711    type Output;
1712
1713    fn square(self) -> Self::Output;
1714}
1715
1716/// Replaces a number with its square.
1717pub trait SquareAssign {
1718    fn square_assign(&mut self);
1719}
1720
1721/// Subtracts a number by the product of two other numbers.
1722pub trait SubMul<Y = Self, Z = Self> {
1723    type Output;
1724
1725    fn sub_mul(self, y: Y, z: Z) -> Self::Output;
1726}
1727
1728/// Subtracts a number by the product of two other numbers, in place.
1729pub trait SubMulAssign<Y = Self, Z = Self> {
1730    fn sub_mul_assign(&mut self, y: Y, z: Z);
1731}
1732
1733/// Takes the absolute value of a number, wrapping around at the boundary of the type.
1734pub trait WrappingAbs {
1735    type Output;
1736
1737    fn wrapping_abs(self) -> Self::Output;
1738}
1739
1740/// Replaces a number with its absolute value, wrapping around at the boundary of the type.
1741pub trait WrappingAbsAssign {
1742    fn wrapping_abs_assign(&mut self);
1743}
1744
1745/// Adds two numbers, wrapping around at the boundary of the type.
1746pub trait WrappingAdd<RHS = Self> {
1747    type Output;
1748
1749    fn wrapping_add(self, other: RHS) -> Self::Output;
1750}
1751
1752/// Adds a number to another number in place, wrapping around at the boundary of the type.
1753pub trait WrappingAddAssign<RHS = Self> {
1754    fn wrapping_add_assign(&mut self, other: RHS);
1755}
1756
1757/// Adds a number and the product of two other numbers, wrapping around at the boundary of the type.
1758pub trait WrappingAddMul<Y = Self, Z = Self> {
1759    type Output;
1760
1761    fn wrapping_add_mul(self, y: Y, z: Z) -> Self::Output;
1762}
1763
1764/// Adds a number and the product of two other numbers, in place, wrapping around at the boundary of
1765/// the type.
1766pub trait WrappingAddMulAssign<Y = Self, Z = Self> {
1767    fn wrapping_add_mul_assign(&mut self, y: Y, z: Z);
1768}
1769
1770/// Divides a number by another number, wrapping around at the boundary of the type.
1771pub trait WrappingDiv<RHS = Self> {
1772    type Output;
1773
1774    fn wrapping_div(self, other: RHS) -> Self::Output;
1775}
1776
1777/// Divides a number by another number in place, wrapping around at the boundary of the type.
1778pub trait WrappingDivAssign<RHS = Self> {
1779    fn wrapping_div_assign(&mut self, other: RHS);
1780}
1781
1782/// Multiplies two numbers, wrapping around at the boundary of the type.
1783pub trait WrappingMul<RHS = Self> {
1784    type Output;
1785
1786    fn wrapping_mul(self, other: RHS) -> Self::Output;
1787}
1788
1789/// Multiplies a number by another number in place, wrapping around at the boundary of the type.
1790pub trait WrappingMulAssign<RHS = Self> {
1791    fn wrapping_mul_assign(&mut self, other: RHS);
1792}
1793
1794/// Negates a number, wrapping around at the boundary of the type.
1795pub trait WrappingNeg {
1796    type Output;
1797
1798    fn wrapping_neg(self) -> Self::Output;
1799}
1800
1801/// Negates a number in place, wrapping around at the boundary of the type.
1802pub trait WrappingNegAssign {
1803    fn wrapping_neg_assign(&mut self);
1804}
1805
1806/// Raises a number to a power, wrapping around at the boundary of the type.
1807pub trait WrappingPow<RHS> {
1808    type Output;
1809
1810    fn wrapping_pow(self, exp: RHS) -> Self::Output;
1811}
1812
1813/// Raises a number to a power in place, wrapping around at the boundary of the type.
1814pub trait WrappingPowAssign<RHS = Self> {
1815    fn wrapping_pow_assign(&mut self, exp: RHS);
1816}
1817
1818/// Squares a number, wrapping around at the boundary of the type.
1819pub trait WrappingSquare {
1820    type Output;
1821
1822    fn wrapping_square(self) -> Self::Output;
1823}
1824
1825/// Squares a number in place, wrapping around at the boundary of the type.
1826pub trait WrappingSquareAssign {
1827    fn wrapping_square_assign(&mut self);
1828}
1829
1830/// Subtracts two numbers, wrapping around at the boundary of the type.
1831pub trait WrappingSub<RHS = Self> {
1832    type Output;
1833
1834    fn wrapping_sub(self, other: RHS) -> Self::Output;
1835}
1836
1837/// Subtracts a number by another number in place, wrapping around at the boundary of the type.
1838pub trait WrappingSubAssign<RHS = Self> {
1839    fn wrapping_sub_assign(&mut self, other: RHS);
1840}
1841
1842/// Subtracts a number by the product of two other numbers, wrapping around at the boundary of the
1843/// type.
1844pub trait WrappingSubMul<Y = Self, Z = Self> {
1845    type Output;
1846
1847    fn wrapping_sub_mul(self, y: Y, z: Z) -> Self::Output;
1848}
1849
1850/// Subtracts a number by the product of two other numbers, in place, wrapping around at the
1851/// boundary of the type.
1852pub trait WrappingSubMulAssign<Y = Self, Z = Self> {
1853    fn wrapping_sub_mul_assign(&mut self, y: Y, z: Z);
1854}
1855
1856/// Multiplies two numbers, returning the product as a pair of `Self` values.
1857///
1858/// The more significant number always comes first.
1859pub trait XMulYToZZ: Sized {
1860    fn x_mul_y_to_zz(x: Self, y: Self) -> (Self, Self);
1861}
1862
1863/// Adds two numbers, each composed of two `Self` values, returning the sum as a pair of `Self`
1864/// values.
1865///
1866/// The more significant number always comes first. Addition is wrapping, and overflow is not
1867/// indicated.
1868pub trait XXAddYYToZZ: Sized {
1869    fn xx_add_yy_to_zz(x_1: Self, x_0: Self, y_1: Self, y_0: Self) -> (Self, Self);
1870}
1871
1872/// Computes the quotient and remainder of two numbers. The first is composed of two `Self` values,
1873/// and the second of a single one.
1874///
1875/// `x_1` must be less than `y`.
1876pub trait XXDivModYToQR: Sized {
1877    fn xx_div_mod_y_to_qr(x_1: Self, x_0: Self, y: Self) -> (Self, Self);
1878}
1879
1880/// Subtracts two numbers, each composed of two `Self` values, returing the difference as a pair of
1881/// `Self` values.
1882///
1883/// The more significant number always comes first. Subtraction is wrapping, and overflow is not
1884/// indicated.
1885pub trait XXSubYYToZZ: Sized {
1886    fn xx_sub_yy_to_zz(x_1: Self, x_0: Self, y_1: Self, y_0: Self) -> (Self, Self);
1887}
1888
1889/// Adds two numbers, each composed of three `Self` values, returning the sum as a triple of `Self`
1890/// values.
1891///
1892/// The more significant number always comes first. Addition is wrapping, and overflow is not
1893/// indicated.
1894pub trait XXXAddYYYToZZZ: Sized {
1895    fn xxx_add_yyy_to_zzz(
1896        x_2: Self,
1897        x_1: Self,
1898        x_0: Self,
1899        y_2: Self,
1900        y_1: Self,
1901        y_0: Self,
1902    ) -> (Self, Self, Self);
1903}
1904
1905/// Subtracts two numbers, each composed of three `Self` values, returing the difference as a triple
1906/// of `Self` values.
1907///
1908/// The more significant number always comes first. Subtraction is wrapping, and overflow is not
1909/// indicated.
1910pub trait XXXSubYYYToZZZ: Sized {
1911    fn xxx_sub_yyy_to_zzz(
1912        x_2: Self,
1913        x_1: Self,
1914        x_0: Self,
1915        y_2: Self,
1916        y_1: Self,
1917        y_0: Self,
1918    ) -> (Self, Self, Self);
1919}
1920
1921/// Adds two numbers, each composed of four `Self` values, returning the sum as a quadruple of
1922/// `Self` values.
1923///
1924/// The more significant number always comes first. Addition is wrapping, and overflow is not
1925/// indicated.
1926pub trait XXXXAddYYYYToZZZZ: Sized {
1927    #[allow(clippy::too_many_arguments)]
1928    fn xxxx_add_yyyy_to_zzzz(
1929        x_3: Self,
1930        x_2: Self,
1931        x_1: Self,
1932        x_0: Self,
1933        y_3: Self,
1934        y_2: Self,
1935        y_1: Self,
1936        y_0: Self,
1937    ) -> (Self, Self, Self, Self);
1938}