Skip to content

Commit d053642

Browse files
committed
Introduce hty and os_ty macros and use them
1 parent ae69eaa commit d053642

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

src/int/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
macro_rules! hty {
2+
($ty:ty) => {
3+
<$ty as LargeInt>::HighHalf
4+
}
5+
}
6+
7+
macro_rules! os_ty {
8+
($ty:ty) => {
9+
<$ty as Int>::OtherSign
10+
}
11+
}
112

213
pub mod mul;
314
pub mod sdiv;
@@ -6,18 +17,22 @@ pub mod udiv;
617

718
/// Trait for some basic operations on integers
819
pub trait Int {
20+
/// Type with the same width but other signedness
21+
type OtherSign;
922
/// Returns the bitwidth of the int type
1023
fn bits() -> u32;
1124
}
1225

1326
macro_rules! int_impl {
1427
($ity:ty, $sty:ty, $bits:expr) => {
1528
impl Int for $ity {
29+
type OtherSign = $sty;
1630
fn bits() -> u32 {
1731
$bits
1832
}
1933
}
2034
impl Int for $sty {
35+
type OtherSign = $ity;
2136
fn bits() -> u32 {
2237
$bits
2338
}

src/int/mul.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use int::LargeInt;
22
use int::Int;
33

44
macro_rules! mul {
5-
($intrinsic:ident: $ty:ty, $tyh:ty) => {
5+
($intrinsic:ident: $ty:ty) => {
66
/// Returns `a * b`
77
#[cfg_attr(not(test), no_mangle)]
88
pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $ty {
@@ -13,15 +13,15 @@ macro_rules! mul {
1313
low &= lower_mask;
1414
t += (a.low() >> half_bits).wrapping_mul(b.low() & lower_mask);
1515
low += (t & lower_mask) << half_bits;
16-
let mut high = (t >> half_bits) as $tyh;
16+
let mut high = (t >> half_bits) as hty!($ty);
1717
t = low >> half_bits;
1818
low &= lower_mask;
1919
t += (b.low() >> half_bits).wrapping_mul(a.low() & lower_mask);
2020
low += (t & lower_mask) << half_bits;
21-
high += (t >> half_bits) as $tyh;
22-
high += (a.low() >> half_bits).wrapping_mul(b.low() >> half_bits) as $tyh;
23-
high = high.wrapping_add(a.high().wrapping_mul(b.low() as $tyh))
24-
.wrapping_add((a.low() as $tyh).wrapping_mul(b.high()));
21+
high += (t >> half_bits) as hty!($ty);
22+
high += (a.low() >> half_bits).wrapping_mul(b.low() >> half_bits) as hty!($ty);
23+
high = high.wrapping_add(a.high().wrapping_mul(b.low() as hty!($ty)))
24+
.wrapping_add((a.low() as hty!($ty)).wrapping_mul(b.high()));
2525
<$ty>::from_parts(low, high)
2626
}
2727
}
@@ -73,12 +73,12 @@ macro_rules! mulo {
7373
}
7474

7575
#[cfg(not(all(feature = "c", target_arch = "x86")))]
76-
mul!(__muldi3: u64, u32);
76+
mul!(__muldi3: u64);
7777

7878
#[cfg(stage0)]
79-
mul!(__multi3: u64, u32);
79+
mul!(__multi3: u64);
8080
#[cfg(not(stage0))]
81-
mul!(__multi3: i128, i64);
81+
mul!(__multi3: i128);
8282

8383
mulo!(__mulosi4: i32);
8484
mulo!(__mulodi4: i64);

src/int/udiv.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub extern "C" fn __umoddi3(a: u64, b: u64) -> u64 {
115115
}
116116

117117
macro_rules! udivmod_inner {
118-
($n:expr, $d:expr, $rem:expr, $ty:ty, $tys:ty, $tyh:ty) => {{
118+
($n:expr, $d:expr, $rem:expr, $ty:ty) => {{
119119
let (n, d, rem) = ($n, $d, $rem);
120120
// NOTE X is unknown, K != 0
121121
if n.high() == 0 {
@@ -179,7 +179,7 @@ macro_rules! udivmod_inner {
179179
sr = d.high().leading_zeros().wrapping_sub(n.high().leading_zeros());
180180

181181
// D > N
182-
if sr > <$tyh>::bits() - 2 {
182+
if sr > <hty!($ty)>::bits() - 2 {
183183
if let Some(rem) = rem {
184184
*rem = n;
185185
}
@@ -188,7 +188,7 @@ macro_rules! udivmod_inner {
188188

189189
sr += 1;
190190

191-
// 1 <= sr <= <$tyh>::bits() - 1
191+
// 1 <= sr <= <hty!($ty)>::bits() - 1
192192
q = n << (<$ty>::bits() - sr);
193193
r = n >> sr;
194194
} else if d.high() == 0 {
@@ -208,7 +208,7 @@ macro_rules! udivmod_inner {
208208
};
209209
}
210210

211-
sr = 1 + <$tyh>::bits() + d.low().leading_zeros() - n.high().leading_zeros();
211+
sr = 1 + <hty!($ty)>::bits() + d.low().leading_zeros() - n.high().leading_zeros();
212212

213213
// 2 <= sr <= u64::bits() - 1
214214
q = n << (<$ty>::bits() - sr);
@@ -220,7 +220,7 @@ macro_rules! udivmod_inner {
220220
sr = d.high().leading_zeros().wrapping_sub(n.high().leading_zeros());
221221

222222
// D > N
223-
if sr > <$tyh>::bits() - 1 {
223+
if sr > <hty!($ty)>::bits() - 1 {
224224
if let Some(rem) = rem {
225225
*rem = n;
226226
}
@@ -229,7 +229,7 @@ macro_rules! udivmod_inner {
229229

230230
sr += 1;
231231

232-
// 1 <= sr <= <$tyh>::bits()
232+
// 1 <= sr <= <hty!($ty)>::bits()
233233
q = n << (<$ty>::bits() - sr);
234234
r = n >> sr;
235235
}
@@ -251,8 +251,8 @@ macro_rules! udivmod_inner {
251251
// r -= d;
252252
// carry = 1;
253253
// }
254-
let s = (d.wrapping_sub(r).wrapping_sub(1)) as $tys >> (<$ty>::bits() - 1);
255-
carry = (s & 1) as $tyh;
254+
let s = (d.wrapping_sub(r).wrapping_sub(1)) as os_ty!($ty) >> (<$ty>::bits() - 1);
255+
carry = (s & 1) as hty!($ty);
256256
r -= d & s as $ty;
257257
}
258258

@@ -266,7 +266,7 @@ macro_rules! udivmod_inner {
266266
/// Returns `n / d` and sets `*rem = n % d`
267267
#[cfg_attr(not(test), no_mangle)]
268268
pub extern "C" fn __udivmoddi4(n: u64, d: u64, rem: Option<&mut u64>) -> u64 {
269-
udivmod_inner!(n, d, rem, u64, i64, u32)
269+
udivmod_inner!(n, d, rem, u64)
270270
}
271271

272272
#[cfg(test)]

0 commit comments

Comments
 (0)