Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pow to i256 #2955

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 82 additions & 0 deletions arrow-buffer/src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,55 @@ impl i256 {
let (val, overflow) = Self::from_bigint_with_overflow(l % r);
(!overflow).then_some(val)
}

/// Performs checked exponentiation
#[inline]
pub fn checked_pow(self, mut exp: u32) -> Option<Self> {
if exp == 0 {
return Some(i256::from_i128(1));
}

let mut base = self;
let mut acc: Self = i256::from_i128(1);

while exp > 1 {
if (exp & 1) == 1 {
acc = acc.checked_mul(base)?;
}
exp /= 2;
base = base.checked_mul(base)?;
}
// since exp!=0, finally the exp must be 1.
// Deal with the final bit of the exponent separately, since
// squaring the base afterwards is not necessary and may cause a
// needless overflow.
acc.checked_mul(base)
}

/// Performs wrapping exponentiation
#[inline]
pub fn wrapping_pow(self, mut exp: u32) -> Self {
if exp == 0 {
return i256::from_i128(1);
}

let mut base = self;
let mut acc: Self = i256::from_i128(1);

while exp > 1 {
if (exp & 1) == 1 {
acc = acc.wrapping_mul(base);
}
exp /= 2;
base = base.wrapping_mul(base);
}

// since exp!=0, finally the exp must be 1.
// Deal with the final bit of the exponent separately, since
// squaring the base afterwards is not necessary and may cause a
// needless overflow.
acc.wrapping_mul(base)
}
}

/// Performs an unsigned multiplication of `a * b` returning a tuple of
Expand Down Expand Up @@ -455,6 +504,39 @@ mod tests {
expected
),
}

// Exponentiation
for exp in vec![0, 1, 3, 8, 100].into_iter() {
let actual = il.wrapping_pow(exp);
let (expected, overflow) =
i256::from_bigint_with_overflow(bl.clone().pow(exp));
assert_eq!(actual.to_string(), expected.to_string());

let checked = il.checked_pow(exp);
match overflow {
true => assert!(
checked.is_none(),
"{} ^ {} = {} vs {} * {} = {}",
il,
exp,
actual,
bl,
exp,
expected
),
false => assert_eq!(
checked.unwrap(),
actual,
"{} ^ {} = {} vs {} * {} = {}",
il,
exp,
actual,
bl,
exp,
expected
),
}
}
}

#[test]
Expand Down