Skip to content

Commit

Permalink
Try splitting part of 'Int' into 'MinInt' so we don't need to impleme…
Browse files Browse the repository at this point in the history
…nt everything on u256/i256
  • Loading branch information
tgross35 committed Apr 13, 2024
1 parent 484e773 commit dee4c01
Show file tree
Hide file tree
Showing 13 changed files with 363 additions and 360 deletions.
22 changes: 11 additions & 11 deletions src/float/add.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::float::Float;
use crate::int::{CastInto, Int};
use crate::int::{CastInto, Int, MinInt};

/// Returns `a + b`
fn add<F: Float>(a: F, b: F) -> F
Expand Down Expand Up @@ -57,17 +57,17 @@ where
}

// zero + anything = anything
if a_abs == Int::ZERO {
if a_abs == MinInt::ZERO {
// but we need to get the sign right for zero + zero
if b_abs == Int::ZERO {
if b_abs == MinInt::ZERO {
return F::from_repr(a.repr() & b.repr());
} else {
return b;
}
}

// anything + zero = anything
if b_abs == Int::ZERO {
if b_abs == MinInt::ZERO {
return a;
}
}
Expand Down Expand Up @@ -113,10 +113,10 @@ where
// Shift the significand of b by the difference in exponents, with a sticky
// bottom bit to get rounding correct.
let align = a_exponent.wrapping_sub(b_exponent).cast();
if align != Int::ZERO {
if align != MinInt::ZERO {
if align < bits {
let sticky =
F::Int::from_bool(b_significand << bits.wrapping_sub(align).cast() != Int::ZERO);
F::Int::from_bool(b_significand << bits.wrapping_sub(align).cast() != MinInt::ZERO);
b_significand = (b_significand >> align.cast()) | sticky;
} else {
b_significand = one; // sticky; b is known to be non-zero.
Expand All @@ -125,8 +125,8 @@ where
if subtraction {
a_significand = a_significand.wrapping_sub(b_significand);
// If a == -b, return +zero.
if a_significand == Int::ZERO {
return F::from_repr(Int::ZERO);
if a_significand == MinInt::ZERO {
return F::from_repr(MinInt::ZERO);
}

// If partial cancellation occured, we need to left-shift the result
Expand All @@ -143,8 +143,8 @@ where

// If the addition carried up, we need to right-shift the result and
// adjust the exponent:
if a_significand & implicit_bit << 4 != Int::ZERO {
let sticky = F::Int::from_bool(a_significand & one != Int::ZERO);
if a_significand & implicit_bit << 4 != MinInt::ZERO {
let sticky = F::Int::from_bool(a_significand & one != MinInt::ZERO);
a_significand = a_significand >> 1 | sticky;
a_exponent += 1;
}
Expand All @@ -160,7 +160,7 @@ where
// need to shift the significand.
let shift = (1 - a_exponent).cast();
let sticky =
F::Int::from_bool((a_significand << bits.wrapping_sub(shift).cast()) != Int::ZERO);
F::Int::from_bool((a_significand << bits.wrapping_sub(shift).cast()) != MinInt::ZERO);
a_significand = a_significand >> shift.cast() | sticky;
a_exponent = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/float/cmp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(unreachable_code)]

use crate::float::Float;
use crate::int::Int;
use crate::int::{Int, MinInt};

#[derive(Clone, Copy)]
enum Result {
Expand Down
2 changes: 1 addition & 1 deletion src/float/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![allow(clippy::needless_return)]

use crate::float::Float;
use crate::int::{CastInto, DInt, HInt, Int};
use crate::int::{CastInto, DInt, HInt, Int, MinInt};

fn div32<F: Float>(a: F, b: F) -> F
where
Expand Down
2 changes: 1 addition & 1 deletion src/float/extend.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::float::Float;
use crate::int::{CastInto, Int};
use crate::int::{CastInto, Int, MinInt};

/// Generic conversion from a narrower to a wider IEEE-754 floating-point type
fn extend<F: Float, R: Float>(a: F) -> R
Expand Down
2 changes: 1 addition & 1 deletion src/float/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::ops;

use super::int::Int;
use super::int::{Int, MinInt};

pub mod add;
pub mod cmp;
Expand Down
2 changes: 1 addition & 1 deletion src/float/mul.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::float::Float;
use crate::int::{CastInto, DInt, HInt, Int};
use crate::int::{CastInto, DInt, HInt, Int, MinInt};

fn mul<F: Float>(a: F, b: F) -> F
where
Expand Down
2 changes: 1 addition & 1 deletion src/float/trunc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::float::Float;
use crate::int::{CastInto, Int};
use crate::int::{CastInto, Int, MinInt};

fn trunc<F: Float, R: Float>(a: F) -> R
where
Expand Down
10 changes: 5 additions & 5 deletions src/int/addsub.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::int::{DInt, Int};
use crate::int::{DInt, Int, MinInt};

trait UAddSub: DInt {
trait UAddSub: DInt + Int {
fn uadd(self, other: Self) -> Self {
let (lo, carry) = self.lo().overflowing_add(other.lo());
let hi = self.hi().wrapping_add(other.hi());
Expand All @@ -22,7 +22,7 @@ impl UAddSub for u128 {}

trait AddSub: Int
where
<Self as Int>::UnsignedInt: UAddSub,
<Self as MinInt>::UnsignedInt: UAddSub,
{
fn add(self, other: Self) -> Self {
Self::from_unsigned(self.unsigned().uadd(other.unsigned()))
Expand All @@ -37,7 +37,7 @@ impl AddSub for i128 {}

trait Addo: AddSub
where
<Self as Int>::UnsignedInt: UAddSub,
<Self as MinInt>::UnsignedInt: UAddSub,
{
fn addo(self, other: Self) -> (Self, bool) {
let sum = AddSub::add(self, other);
Expand All @@ -50,7 +50,7 @@ impl Addo for u128 {}

trait Subo: AddSub
where
<Self as Int>::UnsignedInt: UAddSub,
<Self as MinInt>::UnsignedInt: UAddSub,
{
fn subo(self, other: Self) -> (Self, bool) {
let sum = AddSub::sub(self, other);
Expand Down
Loading

0 comments on commit dee4c01

Please sign in to comment.