-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
Pow
trait and implementations for base numeric types and `usi…
…ze`.
- Loading branch information
Showing
5 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/// A trait for calculating a base to the power of an exponent. | ||
pub trait Pow<Base, Exp> { | ||
type Output; | ||
|
||
/// Returns `self` to the power `exp`. | ||
fn pow(self: Base, exp: Exp) -> Self::Output; | ||
} | ||
|
||
mod mul_based { | ||
/// Square and multiply implementation for `Pow`. | ||
pub impl PowByMul< | ||
Base, +Mul<Base>, +Copy<Base>, +Drop<Base>, +core::num::traits::One<Base>, | ||
> of super::Pow<Base, usize> { | ||
type Output = Base; | ||
|
||
fn pow(mut self: Base, mut exp: usize) -> Base { | ||
let (tail_exp, head_exp) = DivRem::div_rem(exp, 2); | ||
let tail_result = if tail_exp == 0 { | ||
core::num::traits::One::one() | ||
} else { | ||
Self::pow(self * self, tail_exp) | ||
}; | ||
if head_exp == 0 { | ||
tail_result | ||
} else { | ||
tail_result * self | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl PowFelt252 = mul_based::PowByMul<felt252>; | ||
impl PowI8 = mul_based::PowByMul<i8>; | ||
impl PowU8 = mul_based::PowByMul<u8>; | ||
impl PowI16 = mul_based::PowByMul<i16>; | ||
impl PowU16 = mul_based::PowByMul<u16>; | ||
impl PowI32 = mul_based::PowByMul<i32>; | ||
impl PowU32 = mul_based::PowByMul<u32>; | ||
impl PowI64 = mul_based::PowByMul<i64>; | ||
impl PowU64 = mul_based::PowByMul<u64>; | ||
impl PowI128 = mul_based::PowByMul<i128>; | ||
impl PowU128 = mul_based::PowByMul<u128>; | ||
impl PowU256 = mul_based::PowByMul<u256>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters