Skip to content

Commit

Permalink
switch to num-bigint over ibig
Browse files Browse the repository at this point in the history
  • Loading branch information
EclecticGriffin committed Sep 9, 2024
1 parent 1b3d20b commit cd452cc
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 192 deletions.
25 changes: 3 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions interp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ pest_consume.workspace = true
argh.workspace = true
owo-colors = "^3.5"
bitvec = "1.0"
base64 = "0.13.0"
serde_json = "1.0"
rustyline = "=10.0.0"
fraction = { version = "0.11.0", features = ["with-serde-support"] }
thiserror = "1.0.26"
ibig = { version = "0.3.4", features = ["serde"] }
slog = "2.7.0"
slog-term = "2.8.0"
slog-async = "2.7.0"
ahash = "0.8.3"
num-bigint = "0.4.6"
num-traits = "0.2.19"

once_cell = "1.9.0"
petgraph = "0.6.3"
Expand Down
18 changes: 6 additions & 12 deletions interp/src/flatten/primitives/stateful/math.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
use ibig::ops::RemEuclid;

use crate::{
flatten::{
flat_ir::prelude::*,
primitives::{
declare_ports,
declare_ports, ports,
prim_trait::*,
utils::{floored_division, int_sqrt, ShiftBuffer},
},
},
values::InputNumber,
};
use crate::{
flatten::{
primitives::{ports, prim_trait::*},
structures::environment::PortMap,
},
values::Value,
values::{InputNumber, Value},
};
use num_traits::Euclid;

pub struct StdMultPipe<const DEPTH: usize> {
base_port: GlobalPortIdx,
Expand Down Expand Up @@ -199,7 +193,7 @@ impl<const DEPTH: usize, const SIGNED: bool> Primitive
(left
.val()
.as_unsigned()
.rem_euclid(right.val().as_unsigned()))
.rem_euclid(&right.val().as_unsigned()))
.into()
} else {
(left.val().as_signed()
Expand Down Expand Up @@ -530,7 +524,7 @@ impl<const DEPTH: usize, const SIGNED: bool> Primitive
(left
.val()
.as_unsigned()
.rem_euclid(right.val().as_unsigned()))
.rem_euclid(&right.val().as_unsigned()))
.into()
} else {
(left.val().as_signed()
Expand Down
28 changes: 15 additions & 13 deletions interp/src/flatten/primitives/utils.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use num_bigint::{BigInt, BigUint, Sign};
use std::collections::VecDeque;

use ibig::{ibig, UBig};
use ibig::{ubig, IBig};

pub(crate) fn floored_division(left: &IBig, right: &IBig) -> IBig {
/// There's probably a better way to do this but I'm not fixing it right now
pub(crate) fn floored_division(left: &BigInt, right: &BigInt) -> BigInt {
let div = left / right;

if left.signum() != ibig!(-1) && right.signum() != ibig!(-1) {
if left.sign() != Sign::Minus && right.sign() != Sign::Minus {
div
} else if (div.signum() == (-1).into() || div.signum() == 0.into())
} else if (div.sign() == Sign::Minus || div.sign() == Sign::NoSign)
&& (left != &(&div * right))
{
div - 1_i32
Expand All @@ -18,14 +17,17 @@ pub(crate) fn floored_division(left: &IBig, right: &IBig) -> IBig {
}

/// Implementation of integer square root via a basic binary search algorithm
/// based on wikipedia psuedocode
pub(crate) fn int_sqrt(i: &UBig) -> UBig {
let mut lower: UBig = ubig!(0);
let mut upper: UBig = i + ubig!(1);
let mut temp: UBig;
/// based on wikipedia pseudocode.
///
/// TODO griffin: See if this can be replaced with a `sqrt` function in the
/// `num-bigint` crate
pub(crate) fn int_sqrt(i: &BigUint) -> BigUint {
let mut lower: BigUint = BigUint::from(0_u32);
let mut upper: BigUint = i + BigUint::from(1_u32);
let mut temp: BigUint;

while lower != (&upper - ubig!(1)) {
temp = (&lower + &upper) / ubig!(2);
while lower != (&upper - BigUint::from(1_u32)) {
temp = (&lower + &upper) / BigUint::from(2_u32);
if &(&temp * &temp) <= i {
lower = temp
} else {
Expand Down
Loading

0 comments on commit cd452cc

Please sign in to comment.