Skip to content

Commit dd15e90

Browse files
committed
Reduce the x86-specificity of the crate
Currently a lot of things are gated on `cfg(target_arch = "x86")`, which is used to indicate systems with 32-bit words. Apply the following changes to make this less specific: * Replace x86-32 configuration with configuration based on `target_pointer_width`. * Where possible, replace `#[cfg]` with `cfg!` or eliminate the check, to increase the percentage of code that gets validated on any platform. * Remove some configuration that was unneeded, for the same reason. This includes things like `#[cfg(target_arch = "x86")]` on comparisons to `EXPONENT_MIN` or `EXPONENT_MAX`. The checks are only useful on 32-bit platforms, but this is a trivial compiler optimization so not much is gained by keeping the config. I have verified that the crate builds on armv7 targets (but have not tested). Fixes: stencillogic#26
1 parent 6f76df1 commit dd15e90

File tree

13 files changed

+239
-388
lines changed

13 files changed

+239
-388
lines changed

astro-float-num/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ serde = { version = "1.0.147", optional = true }
2323
rand = { version = "0.8.5", optional = true }
2424
lazy_static = { version = "1.4.0", default-features = false, features = [] }
2525
itertools = { version = "0.10.3", default-features = false, features = [] }
26+
cfg-if = "1.0.0"
2627

2728
[features]
2829
default = ["std", "random", "serde"]

astro-float-num/src/common/util.rs

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Auxiliary functions.
22
33
use crate::{
4-
defs::{Word, WORD_BIT_SIZE, WORD_MAX, WORD_SIGNIFICANT_BIT},
4+
defs::{DoubleWord, Word, WORD_BASE, WORD_BIT_SIZE, WORD_MAX, WORD_SIGNIFICANT_BIT},
55
RoundingMode,
66
};
77

@@ -117,32 +117,28 @@ pub fn calc_sqrt_cost(p: usize, cost_mul: usize, cost_add: usize) -> usize {
117117

118118
#[inline(always)]
119119
pub fn add_carry(a: Word, b: Word, c: Word, r: &mut Word) -> Word {
120+
// Using
120121
#[cfg(target_arch = "x86_64")]
121122
{
122123
// platform-specific operation
123-
unsafe { core::arch::x86_64::_addcarry_u64(c as u8, a, b, r) as Word }
124+
return unsafe { core::arch::x86_64::_addcarry_u64(c as u8, a, b, r) as Word };
124125
}
125126

126127
#[cfg(target_arch = "x86")]
127128
{
128129
// platform-specific operation
129-
unsafe { core::arch::x86::_addcarry_u32(c as u8, a, b, r) as Word }
130+
return unsafe { core::arch::x86::_addcarry_u32(c as u8, a, b, r) as Word };
130131
}
131132

132-
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))]
133-
{
134-
use crate::defs::DoubleWord;
135-
use crate::defs::WORD_BASE;
136-
137-
let mut s = c as DoubleWord + a as DoubleWord + b as DoubleWord;
138-
if s >= WORD_BASE {
139-
s -= WORD_BASE;
140-
*r = s as Word;
141-
1
142-
} else {
143-
*r = s as Word;
144-
0
145-
}
133+
#[allow(unreachable_code)] // not used on x86
134+
let mut s = c as DoubleWord + a as DoubleWord + b as DoubleWord;
135+
if s >= WORD_BASE {
136+
s -= WORD_BASE;
137+
*r = s as Word;
138+
1
139+
} else {
140+
*r = s as Word;
141+
0
146142
}
147143
}
148144

@@ -151,30 +147,25 @@ pub fn sub_borrow(a: Word, b: Word, c: Word, r: &mut Word) -> Word {
151147
#[cfg(target_arch = "x86_64")]
152148
{
153149
// platform-specific operation
154-
unsafe { core::arch::x86_64::_subborrow_u64(c as u8, a, b, r) as Word }
150+
return unsafe { core::arch::x86_64::_subborrow_u64(c as u8, a, b, r) as Word };
155151
}
156152

157153
#[cfg(target_arch = "x86")]
158154
{
159155
// platform-specific operation
160-
unsafe { core::arch::x86::_subborrow_u32(c as u8, a, b, r) as Word }
156+
return unsafe { core::arch::x86::_subborrow_u32(c as u8, a, b, r) as Word };
161157
}
162158

163-
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))]
164-
{
165-
use crate::defs::DoubleWord;
166-
use crate::defs::WORD_BASE;
159+
#[allow(unreachable_code)] // not used on x86
160+
let v1 = a as DoubleWord;
161+
let v2 = b as DoubleWord + c as DoubleWord;
167162

168-
let v1 = a as DoubleWord;
169-
let v2 = b as DoubleWord + c as DoubleWord;
170-
171-
if v1 < v2 {
172-
*r = (v1 + WORD_BASE - v2) as Word;
173-
1
174-
} else {
175-
*r = (v1 - v2) as Word;
176-
0
177-
}
163+
if v1 < v2 {
164+
*r = (v1 + WORD_BASE - v2) as Word;
165+
1
166+
} else {
167+
*r = (v1 - v2) as Word;
168+
0
178169
}
179170
}
180171

astro-float-num/src/conv.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ impl BigFloatNumber {
5757
}
5858
}
5959

60-
#[cfg(target_arch = "x86")]
6160
if e < EXPONENT_MIN || e > EXPONENT_MAX {
6261
return Err(Error::InvalidArgument);
6362
}
@@ -830,7 +829,7 @@ mod tests {
830829
let n = BigFloatNumber::from_f64(64, -83.591552734375).unwrap();
831830
assert_eq!(n.cmp(&g), 0);
832831

833-
#[cfg(target_arch = "x86")]
832+
#[cfg(not(target_pointer_width = "64"))]
834833
{
835834
let n = BigFloatNumber::from_raw_parts(
836835
&[2576980377, 2576980377, 2576980377],
@@ -891,7 +890,7 @@ mod tests {
891890
assert!(g.cmp(&n) == 0);
892891
}
893892

894-
#[cfg(not(target_arch = "x86"))]
893+
#[cfg(target_pointer_width = "64")]
895894
{
896895
let n = BigFloatNumber::from_raw_parts(
897896
&[0x9999999999999999, 0x9999999999999999, 0x9999999999999999],

astro-float-num/src/defs.rs

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,44 @@ use std::collections::TryReserveError;
88
#[cfg(not(feature = "std"))]
99
use alloc::collections::TryReserveError;
1010

11-
/// A word.
12-
#[cfg(not(target_arch = "x86"))]
13-
pub type Word = u64;
11+
cfg_if::cfg_if! {
12+
if #[cfg(target_pointer_width = "64")] {
13+
/// A word.
14+
pub type Word = u64;
1415

15-
/// Doubled word.
16-
#[cfg(not(target_arch = "x86"))]
17-
pub type DoubleWord = u128;
16+
/// Doubled word.
17+
pub type DoubleWord = u128;
1818

19-
/// Word with sign.
20-
#[cfg(not(target_arch = "x86"))]
21-
pub type SignedWord = i128;
19+
/// Word with sign.
20+
pub type SignedWord = i128;
21+
} else {
22+
/// A word.
23+
pub type Word = u32;
2224

23-
/// A word.
24-
#[cfg(target_arch = "x86")]
25-
pub type Word = u32;
25+
/// Doubled word.
26+
pub type DoubleWord = u64;
2627

27-
/// Doubled word.
28-
#[cfg(target_arch = "x86")]
29-
pub type DoubleWord = u64;
30-
31-
/// Word with sign.
32-
#[cfg(target_arch = "x86")]
33-
pub type SignedWord = i64;
28+
/// Word with sign.
29+
pub type SignedWord = i64;
30+
}
31+
}
3432

3533
/// An exponent.
3634
pub type Exponent = i32;
3735

3836
/// Maximum exponent value.
39-
#[cfg(not(target_arch = "x86"))]
40-
pub const EXPONENT_MAX: Exponent = Exponent::MAX;
41-
42-
/// Maximum exponent value.
43-
#[cfg(target_arch = "x86")]
44-
pub const EXPONENT_MAX: Exponent = Exponent::MAX / 4;
45-
46-
/// Minimum exponent value.
47-
#[cfg(not(target_arch = "x86"))]
48-
pub const EXPONENT_MIN: Exponent = Exponent::MIN;
37+
pub const EXPONENT_MAX: Exponent = if cfg!(target_pointer_width = "64") {
38+
Exponent::MAX
39+
} else {
40+
Exponent::MAX / 4
41+
};
4942

5043
/// Minimum exponent value.
51-
#[cfg(target_arch = "x86")]
52-
pub const EXPONENT_MIN: Exponent = Exponent::MIN / 4;
44+
pub const EXPONENT_MIN: Exponent = if cfg!(target_pointer_width = "64") {
45+
Exponent::MIN
46+
} else {
47+
Exponent::MIN / 4
48+
};
5349

5450
/// Maximum value of a word.
5551
pub const WORD_MAX: Word = Word::MAX;

astro-float-num/src/ext.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,16 +2158,10 @@ mod tests {
21582158

21592159
let d1 = ONE.clone();
21602160
assert!(d1.exponent() == Some(1));
2161-
let words: &[Word] = {
2162-
#[cfg(not(target_arch = "x86"))]
2163-
{
2164-
&[0, 0x8000000000000000]
2165-
}
2166-
#[cfg(target_arch = "x86")]
2167-
{
2168-
&[0, 0, 0, 0x80000000]
2169-
}
2170-
};
2161+
#[cfg(target_pointer_width = "64")]
2162+
let words: &[Word] = &[0, 0x8000000000000000];
2163+
#[cfg(not(target_pointer_width = "64"))]
2164+
let words: &[Word] = &[0, 0, 0, 0x80000000];
21712165

21722166
assert!(d1.mantissa_digits() == Some(words));
21732167
assert!(d1.mantissa_max_bit_len() == Some(DEFAULT_P));
@@ -2473,22 +2467,17 @@ mod rand_tests {
24732467

24742468
use super::*;
24752469
use crate::defs::EXPONENT_MAX;
2470+
use crate::defs::EXPONENT_MIN;
24762471

24772472
#[test]
24782473
fn test_rand() {
24792474
for _ in 0..1000 {
24802475
let p = rand::random::<usize>() % 1000 + DEFAULT_P;
2481-
let exp_from;
2482-
#[cfg(not(target_arch = "x86"))]
2483-
{
2484-
exp_from = rand::random::<Exponent>().abs();
2485-
}
2486-
#[cfg(target_arch = "x86")]
2487-
{
2488-
use crate::defs::EXPONENT_MIN;
2489-
exp_from =
2490-
rand::random::<Exponent>().abs() % (EXPONENT_MAX - EXPONENT_MIN) + EXPONENT_MIN;
2491-
}
2476+
let exp_from = if cfg!(target_pointer_width = "64") {
2477+
rand::random::<Exponent>().abs()
2478+
} else {
2479+
rand::random::<Exponent>().abs() % (EXPONENT_MAX - EXPONENT_MIN) + EXPONENT_MIN
2480+
};
24922481
let exp_shift = if EXPONENT_MAX > exp_from {
24932482
rand::random::<Exponent>().abs()
24942483
% (EXPONENT_MAX as isize - exp_from as isize) as Exponent

astro-float-num/src/mantissa/mantissa.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -717,12 +717,11 @@ impl Mantissa {
717717
let nd = m.len() - size_of::<u64>() / size_of::<Word>();
718718
m[..nd].fill(0);
719719

720-
#[cfg(not(target_arch = "x86"))]
720+
#[cfg(target_pointer_width = "64")]
721721
{
722722
m[nd] = u;
723723
}
724-
725-
#[cfg(target_arch = "x86")]
724+
#[cfg(not(target_pointer_width = "64"))]
726725
{
727726
let mut u = u;
728727
for v in &mut m[nd..] {
@@ -764,12 +763,11 @@ impl Mantissa {
764763

765764
#[cfg(test)]
766765
pub fn to_u64(&self) -> u64 {
767-
#[cfg(not(target_arch = "x86"))]
766+
#[cfg(target_pointer_width = "64")]
768767
{
769768
self.m[self.m.len() - 1]
770769
}
771-
772-
#[cfg(target_arch = "x86")]
770+
#[cfg(not(target_pointer_width = "64"))]
773771
{
774772
let mut ret: u64 = 0;
775773
let nd = size_of::<u64>() / size_of::<Word>();

0 commit comments

Comments
 (0)