Skip to content

Commit 4b1048d

Browse files
authored
Merge pull request #1183 from serde-rs/arithmetic
Unify chunk size choice between float and string parsing
2 parents fec0376 + f268173 commit 4b1048d

File tree

7 files changed

+44
-44
lines changed

7 files changed

+44
-44
lines changed

build.rs

+22-12
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,28 @@ use std::env;
33
fn main() {
44
println!("cargo:rerun-if-changed=build.rs");
55

6-
println!("cargo:rustc-check-cfg=cfg(limb_width_32)");
7-
println!("cargo:rustc-check-cfg=cfg(limb_width_64)");
6+
println!("cargo:rustc-check-cfg=cfg(arithmetic32)");
7+
println!("cargo:rustc-check-cfg=cfg(arithmetic64)");
88

9-
// Decide ideal limb width for arithmetic in the float parser. Refer to
10-
// src/lexical/math.rs for where this has an effect.
11-
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
12-
match target_arch.as_str() {
13-
"aarch64" | "mips64" | "powerpc64" | "x86_64" | "loongarch64" => {
14-
println!("cargo:rustc-cfg=limb_width_64");
15-
}
16-
_ => {
17-
println!("cargo:rustc-cfg=limb_width_32");
18-
}
9+
// Decide ideal limb width for arithmetic in the float parser and string
10+
// parser.
11+
let target_arch = env::var_os("CARGO_CFG_TARGET_ARCH").unwrap();
12+
let target_pointer_width = env::var_os("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap();
13+
if target_arch == "aarch64"
14+
|| target_arch == "loongarch64"
15+
|| target_arch == "mips64"
16+
|| target_arch == "powerpc64"
17+
|| target_arch == "wasm32"
18+
|| target_arch == "x86_64"
19+
|| target_pointer_width == "64"
20+
{
21+
// The above list of architectures are ones that have native support for
22+
// 64-bit arithmetic, but which have some targets using a smaller
23+
// pointer width. Examples include aarch64-unknown-linux-gnu_ilp32 and
24+
// x86_64-unknown-linux-gnux32. So our choice of limb width is not
25+
// equivalent to using usize everywhere.
26+
println!("cargo:rustc-cfg=arithmetic64");
27+
} else {
28+
println!("cargo:rustc-cfg=arithmetic32");
1929
}
2030
}

src/lexical/large_powers.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
//! Precalculated large powers for limbs.
44
5-
#[cfg(limb_width_32)]
5+
#[cfg(arithmetic32)]
66
pub(crate) use super::large_powers32::*;
77

8-
#[cfg(limb_width_64)]
8+
#[cfg(arithmetic64)]
99
pub(crate) use super::large_powers64::*;

src/lexical/math.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,29 @@ use core::{cmp, iter, mem};
3737
// sparc64 (`UMUL` only supported double-word arguments).
3838

3939
// 32-BIT LIMB
40-
#[cfg(limb_width_32)]
40+
#[cfg(arithmetic32)]
4141
pub type Limb = u32;
4242

43-
#[cfg(limb_width_32)]
43+
#[cfg(arithmetic32)]
4444
pub const POW5_LIMB: &[Limb] = &POW5_32;
4545

46-
#[cfg(limb_width_32)]
46+
#[cfg(arithmetic32)]
4747
pub const POW10_LIMB: &[Limb] = &POW10_32;
4848

49-
#[cfg(limb_width_32)]
49+
#[cfg(arithmetic32)]
5050
type Wide = u64;
5151

5252
// 64-BIT LIMB
53-
#[cfg(limb_width_64)]
53+
#[cfg(arithmetic64)]
5454
pub type Limb = u64;
5555

56-
#[cfg(limb_width_64)]
56+
#[cfg(arithmetic64)]
5757
pub const POW5_LIMB: &[Limb] = &POW5_64;
5858

59-
#[cfg(limb_width_64)]
59+
#[cfg(arithmetic64)]
6060
pub const POW10_LIMB: &[Limb] = &POW10_64;
6161

62-
#[cfg(limb_width_64)]
62+
#[cfg(arithmetic64)]
6363
type Wide = u128;
6464

6565
/// Cast to limb type.
@@ -79,14 +79,14 @@ fn as_wide<T: Integer>(t: T) -> Wide {
7979

8080
/// Split u64 into limbs, in little-endian order.
8181
#[inline]
82-
#[cfg(limb_width_32)]
82+
#[cfg(arithmetic32)]
8383
fn split_u64(x: u64) -> [Limb; 2] {
8484
[as_limb(x), as_limb(x >> 32)]
8585
}
8686

8787
/// Split u64 into limbs, in little-endian order.
8888
#[inline]
89-
#[cfg(limb_width_64)]
89+
#[cfg(arithmetic64)]
9090
fn split_u64(x: u64) -> [Limb; 1] {
9191
[as_limb(x)]
9292
}

src/lexical/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ pub(crate) mod rounding;
2828
mod shift;
2929
mod small_powers;
3030

31-
#[cfg(limb_width_32)]
31+
#[cfg(arithmetic32)]
3232
mod large_powers32;
3333

34-
#[cfg(limb_width_64)]
34+
#[cfg(arithmetic64)]
3535
mod large_powers64;
3636

3737
// API

src/lexical/small_powers.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
//! Pre-computed small powers.
44
55
// 32 BIT
6-
#[cfg(limb_width_32)]
6+
#[cfg(arithmetic32)]
77
pub(crate) const POW5_32: [u32; 14] = [
88
1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, 48828125, 244140625,
99
1220703125,
1010
];
1111

12-
#[cfg(limb_width_32)]
12+
#[cfg(arithmetic32)]
1313
pub(crate) const POW10_32: [u32; 10] = [
1414
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000,
1515
];
1616

1717
// 64 BIT
18-
#[cfg(limb_width_64)]
18+
#[cfg(arithmetic64)]
1919
pub(crate) const POW5_64: [u64; 28] = [
2020
1,
2121
5,

src/read.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -446,20 +446,10 @@ impl<'a> SliceRead<'a> {
446446
// benchmarks and it's cross-platform, so probably the right fit.
447447
// [1]: https://groups.google.com/forum/#!original/comp.lang.c/2HtQXvg7iKc/xOJeipH6KLMJ
448448

449-
// The following architectures have native support for 64-bit integers,
450-
// but have targets where usize is not 64-bit.
451-
#[cfg(any(
452-
target_arch = "aarch64",
453-
target_arch = "x86_64",
454-
target_arch = "wasm32",
455-
))]
449+
#[cfg(arithmetic64)]
456450
type Chunk = u64;
457-
#[cfg(not(any(
458-
target_arch = "aarch64",
459-
target_arch = "x86_64",
460-
target_arch = "wasm32",
461-
)))]
462-
type Chunk = usize;
451+
#[cfg(arithmetic32)]
452+
type Chunk = u32;
463453

464454
const STEP: usize = mem::size_of::<Chunk>();
465455
const ONE_BYTES: Chunk = Chunk::MAX / 255; // 0x0101...01

tests/lexical/math.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ impl Math for Bigint {
1818
}
1919
}
2020

21-
#[cfg(limb_width_32)]
21+
#[cfg(arithmetic32)]
2222
pub(crate) fn from_u32(x: &[u32]) -> Vec<Limb> {
2323
x.iter().cloned().collect()
2424
}
2525

26-
#[cfg(limb_width_64)]
26+
#[cfg(arithmetic64)]
2727
pub(crate) fn from_u32(x: &[u32]) -> Vec<Limb> {
2828
let mut v = Vec::<Limb>::default();
2929
for xi in x.chunks(2) {

0 commit comments

Comments
 (0)