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