Skip to content

Commit 85a83d1

Browse files
committed
Try splitting part of 'Int' into 'MinInt' so we don't need to implement everything on u256/i256
1 parent 484e773 commit 85a83d1

File tree

12 files changed

+365
-374
lines changed

12 files changed

+365
-374
lines changed

src/float/add.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::float::Float;
2-
use crate::int::{CastInto, Int};
2+
use crate::int::{CastInto, Int, MinInt};
33

44
/// Returns `a + b`
55
fn add<F: Float>(a: F, b: F) -> F
@@ -57,17 +57,17 @@ where
5757
}
5858

5959
// zero + anything = anything
60-
if a_abs == Int::ZERO {
60+
if a_abs == MinInt::ZERO {
6161
// but we need to get the sign right for zero + zero
62-
if b_abs == Int::ZERO {
62+
if b_abs == MinInt::ZERO {
6363
return F::from_repr(a.repr() & b.repr());
6464
} else {
6565
return b;
6666
}
6767
}
6868

6969
// anything + zero = anything
70-
if b_abs == Int::ZERO {
70+
if b_abs == MinInt::ZERO {
7171
return a;
7272
}
7373
}
@@ -113,10 +113,10 @@ where
113113
// Shift the significand of b by the difference in exponents, with a sticky
114114
// bottom bit to get rounding correct.
115115
let align = a_exponent.wrapping_sub(b_exponent).cast();
116-
if align != Int::ZERO {
116+
if align != MinInt::ZERO {
117117
if align < bits {
118118
let sticky =
119-
F::Int::from_bool(b_significand << bits.wrapping_sub(align).cast() != Int::ZERO);
119+
F::Int::from_bool(b_significand << bits.wrapping_sub(align).cast() != MinInt::ZERO);
120120
b_significand = (b_significand >> align.cast()) | sticky;
121121
} else {
122122
b_significand = one; // sticky; b is known to be non-zero.
@@ -125,8 +125,8 @@ where
125125
if subtraction {
126126
a_significand = a_significand.wrapping_sub(b_significand);
127127
// If a == -b, return +zero.
128-
if a_significand == Int::ZERO {
129-
return F::from_repr(Int::ZERO);
128+
if a_significand == MinInt::ZERO {
129+
return F::from_repr(MinInt::ZERO);
130130
}
131131

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

144144
// If the addition carried up, we need to right-shift the result and
145145
// adjust the exponent:
146-
if a_significand & implicit_bit << 4 != Int::ZERO {
147-
let sticky = F::Int::from_bool(a_significand & one != Int::ZERO);
146+
if a_significand & implicit_bit << 4 != MinInt::ZERO {
147+
let sticky = F::Int::from_bool(a_significand & one != MinInt::ZERO);
148148
a_significand = a_significand >> 1 | sticky;
149149
a_exponent += 1;
150150
}
@@ -160,7 +160,7 @@ where
160160
// need to shift the significand.
161161
let shift = (1 - a_exponent).cast();
162162
let sticky =
163-
F::Int::from_bool((a_significand << bits.wrapping_sub(shift).cast()) != Int::ZERO);
163+
F::Int::from_bool((a_significand << bits.wrapping_sub(shift).cast()) != MinInt::ZERO);
164164
a_significand = a_significand >> shift.cast() | sticky;
165165
a_exponent = 0;
166166
}

src/float/cmp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(unreachable_code)]
22

33
use crate::float::Float;
4-
use crate::int::Int;
4+
use crate::int::{Int, MinInt};
55

66
#[derive(Clone, Copy)]
77
enum Result {

src/float/div.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![allow(clippy::needless_return)]
44

55
use crate::float::Float;
6-
use crate::int::{CastInto, DInt, HInt, Int};
6+
use crate::int::{CastInto, DInt, HInt, Int, MinInt};
77

88
fn div32<F: Float>(a: F, b: F) -> F
99
where

src/float/extend.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::float::Float;
2-
use crate::int::{CastInto, Int};
2+
use crate::int::{CastInto, Int, MinInt};
33

44
/// Generic conversion from a narrower to a wider IEEE-754 floating-point type
55
fn extend<F: Float, R: Float>(a: F) -> R

src/float/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::ops;
22

3-
use super::int::Int;
3+
use super::int::{Int, MinInt};
44

55
pub mod add;
66
pub mod cmp;

src/float/mul.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::float::Float;
2-
use crate::int::{CastInto, DInt, HInt, Int};
2+
use crate::int::{CastInto, DInt, HInt, Int, MinInt};
33

44
fn mul<F: Float>(a: F, b: F) -> F
55
where

src/float/trunc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::float::Float;
2-
use crate::int::{CastInto, Int};
2+
use crate::int::{CastInto, Int, MinInt};
33

44
fn trunc<F: Float, R: Float>(a: F) -> R
55
where

src/int/addsub.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::int::{DInt, Int};
1+
use crate::int::{DInt, Int, MinInt};
22

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

2323
trait AddSub: Int
2424
where
25-
<Self as Int>::UnsignedInt: UAddSub,
25+
<Self as MinInt>::UnsignedInt: UAddSub,
2626
{
2727
fn add(self, other: Self) -> Self {
2828
Self::from_unsigned(self.unsigned().uadd(other.unsigned()))
@@ -37,7 +37,7 @@ impl AddSub for i128 {}
3737

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

5151
trait Subo: AddSub
5252
where
53-
<Self as Int>::UnsignedInt: UAddSub,
53+
<Self as MinInt>::UnsignedInt: UAddSub,
5454
{
5555
fn subo(self, other: Self) -> (Self, bool) {
5656
let sum = AddSub::sub(self, other);

0 commit comments

Comments
 (0)