Skip to content

Commit 0b8c3e0

Browse files
committed
Small refactor to use associated consts
1 parent 23f14d3 commit 0b8c3e0

File tree

6 files changed

+52
-66
lines changed

6 files changed

+52
-66
lines changed

src/float/conv.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ macro_rules! int_to_float {
1111
let mant_dig = <$fty>::significand_bits() + 1;
1212
let exponent_bias = <$fty>::exponent_bias();
1313

14-
let n = <$ity>::bits();
14+
let n = <$ity>::BITS;
1515
let (s, a) = i.extract_sign();
1616
let mut a = a;
1717

@@ -21,7 +21,7 @@ macro_rules! int_to_float {
2121
// exponent
2222
let mut e = sd - 1;
2323

24-
if <$ity>::bits() < mant_dig {
24+
if <$ity>::BITS < mant_dig {
2525
return <$fty>::from_parts(s,
2626
(e + exponent_bias) as <$fty as Float>::Int,
2727
(a as <$fty as Float>::Int) << (mant_dig - e - 1))
@@ -142,7 +142,7 @@ macro_rules! float_to_int {
142142
let f = $f;
143143
let fixint_min = <$ity>::min_value();
144144
let fixint_max = <$ity>::max_value();
145-
let fixint_bits = <$ity>::bits() as usize;
145+
let fixint_bits = <$ity>::BITS as usize;
146146
let fixint_unsigned = fixint_min == 0;
147147

148148
let sign_bit = <$fty>::sign_mask();

src/int/mod.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ pub trait Int:
3939
/// Unsigned version of Self
4040
type UnsignedInt: Int;
4141

42-
/// Returns the bitwidth of the int type
43-
fn bits() -> u32;
42+
/// The bitwidth of the int type
43+
const BITS: u32;
4444

45-
fn zero() -> Self;
46-
fn one() -> Self;
45+
const ZERO: Self;
46+
const ONE: Self;
4747

4848
/// Extracts the sign from self and returns a tuple.
4949
///
@@ -83,17 +83,10 @@ macro_rules! int_impl {
8383
type OtherSign = $ity;
8484
type UnsignedInt = $uty;
8585

86-
fn zero() -> Self {
87-
0
88-
}
89-
90-
fn one() -> Self {
91-
1
92-
}
86+
const BITS: u32 = $bits;
9387

94-
fn bits() -> u32 {
95-
$bits
96-
}
88+
const ZERO: Self = 0;
89+
const ONE: Self = 1;
9790

9891
fn extract_sign(self) -> (bool, $uty) {
9992
(false, self)
@@ -140,17 +133,10 @@ macro_rules! int_impl {
140133
type OtherSign = $uty;
141134
type UnsignedInt = $uty;
142135

143-
fn bits() -> u32 {
144-
$bits
145-
}
146-
147-
fn zero() -> Self {
148-
0
149-
}
136+
const BITS: u32 = $bits;
150137

151-
fn one() -> Self {
152-
1
153-
}
138+
const ZERO: Self = 0;
139+
const ONE: Self = 1;
154140

155141
fn extract_sign(self) -> (bool, $uty) {
156142
if self < 0 {

src/int/mul.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use int::Int;
55

66
trait Mul: LargeInt {
77
fn mul(self, other: Self) -> Self {
8-
let half_bits = Self::bits() / 4;
9-
let lower_mask = !<<Self as LargeInt>::LowHalf>::zero() >> half_bits;
8+
let half_bits = Self::BITS / 4;
9+
let lower_mask = !<<Self as LargeInt>::LowHalf>::ZERO >> half_bits;
1010
let mut low = (self.low() & lower_mask).wrapping_mul(other.low() & lower_mask);
1111
let mut t = low >> half_bits;
1212
low &= lower_mask;
@@ -33,23 +33,23 @@ trait Mulo: Int + ops::Neg<Output = Self> {
3333
*overflow = 0;
3434
let result = self.wrapping_mul(other);
3535
if self == Self::min_value() {
36-
if other != Self::zero() && other != Self::one() {
36+
if other != Self::ZERO && other != Self::ONE {
3737
*overflow = 1;
3838
}
3939
return result;
4040
}
4141
if other == Self::min_value() {
42-
if self != Self::zero() && self != Self::one() {
42+
if self != Self::ZERO && self != Self::ONE {
4343
*overflow = 1;
4444
}
4545
return result;
4646
}
4747

48-
let sa = self >> (Self::bits() - 1);
48+
let sa = self >> (Self::BITS - 1);
4949
let abs_a = (self ^ sa) - sa;
50-
let sb = other >> (Self::bits() - 1);
50+
let sb = other >> (Self::BITS - 1);
5151
let abs_b = (other ^ sb) - sb;
52-
let two = Self::one() + Self::one();
52+
let two = Self::ONE + Self::ONE;
5353
if abs_a < two || abs_b < two {
5454
return result;
5555
}

src/int/sdiv.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use int::Int;
33
trait Div: Int {
44
/// Returns `a / b`
55
fn div(self, other: Self) -> Self {
6-
let s_a = self >> (Self::bits() - 1);
7-
let s_b = other >> (Self::bits() - 1);
8-
// NOTE it's OK to overflow here because of the `as $uty` cast below
6+
let s_a = self >> (Self::BITS - 1);
7+
let s_b = other >> (Self::BITS - 1);
8+
// NOTE it's OK to overflow here because of the `.unsigned()` below.
99
// This whole operation is computing the absolute value of the inputs
1010
// So some overflow will happen when dealing with e.g. `i64::MIN`
1111
// where the absolute value is `(-i64::MIN) as u64`
@@ -25,10 +25,10 @@ impl Div for i128 {}
2525
trait Mod: Int {
2626
/// Returns `a % b`
2727
fn mod_(self, other: Self) -> Self {
28-
let s = other >> (Self::bits() - 1);
28+
let s = other >> (Self::BITS - 1);
2929
// NOTE(wrapping_sub) see comment in the `div`
3030
let b = (other ^ s).wrapping_sub(s);
31-
let s = self >> (Self::bits() - 1);
31+
let s = self >> (Self::BITS - 1);
3232
let a = (self ^ s).wrapping_sub(s);
3333

3434
let r = a.unsigned().aborting_rem(b.unsigned());

src/int/shift.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use int::{Int, LargeInt};
22

33
trait Ashl: Int + LargeInt {
4-
/// Returns `a << b`, requires `b < $ty::bits()`
4+
/// Returns `a << b`, requires `b < Self::BITS`
55
fn ashl(self, offset: u32) -> Self
66
where Self: LargeInt<HighHalf = <Self as LargeInt>::LowHalf>,
77
{
8-
let half_bits = Self::bits() / 2;
8+
let half_bits = Self::BITS / 2;
99
if offset & half_bits != 0 {
10-
Self::from_parts(Int::zero(), self.low() << (offset - half_bits))
10+
Self::from_parts(Int::ZERO, self.low() << (offset - half_bits))
1111
} else if offset == 0 {
1212
self
1313
} else {
@@ -22,11 +22,11 @@ impl Ashl for u64 {}
2222
impl Ashl for u128 {}
2323

2424
trait Ashr: Int + LargeInt {
25-
/// Returns arithmetic `a >> b`, requires `b < $ty::bits()`
25+
/// Returns arithmetic `a >> b`, requires `b < Self::BITS`
2626
fn ashr(self, offset: u32) -> Self
2727
where Self: LargeInt<LowHalf = <<Self as LargeInt>::HighHalf as Int>::UnsignedInt>,
2828
{
29-
let half_bits = Self::bits() / 2;
29+
let half_bits = Self::BITS / 2;
3030
if offset & half_bits != 0 {
3131
Self::from_parts((self.high() >> (offset - half_bits)).unsigned(),
3232
self.high() >> (half_bits - 1))
@@ -44,13 +44,13 @@ impl Ashr for i64 {}
4444
impl Ashr for i128 {}
4545

4646
trait Lshr: Int + LargeInt {
47-
/// Returns logical `a >> b`, requires `b < $ty::bits()`
47+
/// Returns logical `a >> b`, requires `b < Self::BITS`
4848
fn lshr(self, offset: u32) -> Self
4949
where Self: LargeInt<HighHalf = <Self as LargeInt>::LowHalf>,
5050
{
51-
let half_bits = Self::bits() / 2;
51+
let half_bits = Self::BITS / 2;
5252
if offset & half_bits != 0 {
53-
Self::from_parts(self.high() >> (offset - half_bits), Int::zero())
53+
Self::from_parts(self.high() >> (offset - half_bits), Int::ZERO)
5454
} else if offset == 0 {
5555
self
5656
} else {

src/int/udiv.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ macro_rules! udivmod_inner {
6363
sr = d.high().leading_zeros().wrapping_sub(n.high().leading_zeros());
6464

6565
// D > N
66-
if sr > <hty!($ty)>::bits() - 2 {
66+
if sr > <hty!($ty)>::BITS - 2 {
6767
if let Some(rem) = rem {
6868
*rem = n;
6969
}
@@ -72,8 +72,8 @@ macro_rules! udivmod_inner {
7272

7373
sr += 1;
7474

75-
// 1 <= sr <= <hty!($ty)>::bits() - 1
76-
q = n << (<$ty>::bits() - sr);
75+
// 1 <= sr <= <hty!($ty)>::BITS - 1
76+
q = n << (<$ty>::BITS - sr);
7777
r = n >> sr;
7878
} else if d.high() == 0 {
7979
// K X
@@ -92,10 +92,10 @@ macro_rules! udivmod_inner {
9292
};
9393
}
9494

95-
sr = 1 + <hty!($ty)>::bits() + d.low().leading_zeros() - n.high().leading_zeros();
95+
sr = 1 + <hty!($ty)>::BITS + d.low().leading_zeros() - n.high().leading_zeros();
9696

97-
// 2 <= sr <= u64::bits() - 1
98-
q = n << (<$ty>::bits() - sr);
97+
// 2 <= sr <= u64::BITS - 1
98+
q = n << (<$ty>::BITS - sr);
9999
r = n >> sr;
100100
} else {
101101
// K X
@@ -104,7 +104,7 @@ macro_rules! udivmod_inner {
104104
sr = d.high().leading_zeros().wrapping_sub(n.high().leading_zeros());
105105

106106
// D > N
107-
if sr > <hty!($ty)>::bits() - 1 {
107+
if sr > <hty!($ty)>::BITS - 1 {
108108
if let Some(rem) = rem {
109109
*rem = n;
110110
}
@@ -113,16 +113,16 @@ macro_rules! udivmod_inner {
113113

114114
sr += 1;
115115

116-
// 1 <= sr <= <hty!($ty)>::bits()
117-
q = n << (<$ty>::bits() - sr);
116+
// 1 <= sr <= <hty!($ty)>::BITS
117+
q = n << (<$ty>::BITS - sr);
118118
r = n >> sr;
119119
}
120120

121121
// Not a special case
122122
// q and r are initialized with
123-
// q = n << (u64::bits() - sr)
123+
// q = n << (u64::BITS - sr)
124124
// r = n >> sr
125-
// 1 <= sr <= u64::bits() - 1
125+
// 1 <= sr <= u64::BITS - 1
126126
let mut carry = 0;
127127

128128
// Don't use a range because they may generate references to memcpy in unoptimized code
@@ -131,15 +131,15 @@ macro_rules! udivmod_inner {
131131
i += 1;
132132

133133
// r:q = ((r:q) << 1) | carry
134-
r = (r << 1) | (q >> (<$ty>::bits() - 1));
134+
r = (r << 1) | (q >> (<$ty>::BITS - 1));
135135
q = (q << 1) | carry as $ty;
136136

137137
// carry = 0
138138
// if r >= d {
139139
// r -= d;
140140
// carry = 1;
141141
// }
142-
let s = (d.wrapping_sub(r).wrapping_sub(1)) as os_ty!($ty) >> (<$ty>::bits() - 1);
142+
let s = (d.wrapping_sub(r).wrapping_sub(1)) as os_ty!($ty) >> (<$ty>::BITS - 1);
143143
carry = (s & 1) as hty!($ty);
144144
r -= d & s as $ty;
145145
}
@@ -169,19 +169,19 @@ intrinsics! {
169169
let mut sr = d.leading_zeros().wrapping_sub(n.leading_zeros());
170170

171171
// d > n
172-
if sr > u32::bits() - 1 {
172+
if sr > u32::BITS - 1 {
173173
return 0;
174174
}
175175

176176
// d == 1
177-
if sr == u32::bits() - 1 {
177+
if sr == u32::BITS - 1 {
178178
return n;
179179
}
180180

181181
sr += 1;
182182

183-
// 1 <= sr <= u32::bits() - 1
184-
let mut q = n << (u32::bits() - sr);
183+
// 1 <= sr <= u32::BITS - 1
184+
let mut q = n << (u32::BITS - sr);
185185
let mut r = n >> sr;
186186

187187
let mut carry = 0;
@@ -192,7 +192,7 @@ intrinsics! {
192192
i += 1;
193193

194194
// r:q = ((r:q) << 1) | carry
195-
r = (r << 1) | (q >> (u32::bits() - 1));
195+
r = (r << 1) | (q >> (u32::BITS - 1));
196196
q = (q << 1) | carry;
197197

198198
// carry = 0;
@@ -201,7 +201,7 @@ intrinsics! {
201201
// carry = 1;
202202
// }
203203

204-
let s = (d.wrapping_sub(r).wrapping_sub(1)) as i32 >> (u32::bits() - 1);
204+
let s = (d.wrapping_sub(r).wrapping_sub(1)) as i32 >> (u32::BITS - 1);
205205
carry = (s & 1) as u32;
206206
r -= d & s as u32;
207207
}

0 commit comments

Comments
 (0)