Skip to content

Commit

Permalink
Merge pull request #1184 from serde-rs/fastarithmetic
Browse files Browse the repository at this point in the history
Improve cfg names for fast arithmetic
  • Loading branch information
dtolnay committed Aug 23, 2024
2 parents 4b1048d + ffc4a43 commit e6282b0
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 25 deletions.
7 changes: 3 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use std::env;
fn main() {
println!("cargo:rerun-if-changed=build.rs");

println!("cargo:rustc-check-cfg=cfg(arithmetic32)");
println!("cargo:rustc-check-cfg=cfg(arithmetic64)");
println!("cargo:rustc-check-cfg=cfg(fast_arithmetic, values(\"32\", \"64\"))");

// Decide ideal limb width for arithmetic in the float parser and string
// parser.
Expand All @@ -23,8 +22,8 @@ fn main() {
// pointer width. Examples include aarch64-unknown-linux-gnu_ilp32 and
// x86_64-unknown-linux-gnux32. So our choice of limb width is not
// equivalent to using usize everywhere.
println!("cargo:rustc-cfg=arithmetic64");
println!("cargo:rustc-cfg=fast_arithmetic=\"64\"");
} else {
println!("cargo:rustc-cfg=arithmetic32");
println!("cargo:rustc-cfg=fast_arithmetic=\"32\"");
}
}
4 changes: 2 additions & 2 deletions src/lexical/large_powers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

//! Precalculated large powers for limbs.

#[cfg(arithmetic32)]
#[cfg(fast_arithmetic = "32")]
pub(crate) use super::large_powers32::*;

#[cfg(arithmetic64)]
#[cfg(fast_arithmetic = "64")]
pub(crate) use super::large_powers64::*;
20 changes: 10 additions & 10 deletions src/lexical/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,29 @@ use core::{cmp, iter, mem};
// sparc64 (`UMUL` only supported double-word arguments).

// 32-BIT LIMB
#[cfg(arithmetic32)]
#[cfg(fast_arithmetic = "32")]
pub type Limb = u32;

#[cfg(arithmetic32)]
#[cfg(fast_arithmetic = "32")]
pub const POW5_LIMB: &[Limb] = &POW5_32;

#[cfg(arithmetic32)]
#[cfg(fast_arithmetic = "32")]
pub const POW10_LIMB: &[Limb] = &POW10_32;

#[cfg(arithmetic32)]
#[cfg(fast_arithmetic = "32")]
type Wide = u64;

// 64-BIT LIMB
#[cfg(arithmetic64)]
#[cfg(fast_arithmetic = "64")]
pub type Limb = u64;

#[cfg(arithmetic64)]
#[cfg(fast_arithmetic = "64")]
pub const POW5_LIMB: &[Limb] = &POW5_64;

#[cfg(arithmetic64)]
#[cfg(fast_arithmetic = "64")]
pub const POW10_LIMB: &[Limb] = &POW10_64;

#[cfg(arithmetic64)]
#[cfg(fast_arithmetic = "64")]
type Wide = u128;

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

/// Split u64 into limbs, in little-endian order.
#[inline]
#[cfg(arithmetic32)]
#[cfg(fast_arithmetic = "32")]
fn split_u64(x: u64) -> [Limb; 2] {
[as_limb(x), as_limb(x >> 32)]
}

/// Split u64 into limbs, in little-endian order.
#[inline]
#[cfg(arithmetic64)]
#[cfg(fast_arithmetic = "64")]
fn split_u64(x: u64) -> [Limb; 1] {
[as_limb(x)]
}
Expand Down
4 changes: 2 additions & 2 deletions src/lexical/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ pub(crate) mod rounding;
mod shift;
mod small_powers;

#[cfg(arithmetic32)]
#[cfg(fast_arithmetic = "32")]
mod large_powers32;

#[cfg(arithmetic64)]
#[cfg(fast_arithmetic = "64")]
mod large_powers64;

// API
Expand Down
6 changes: 3 additions & 3 deletions src/lexical/small_powers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
//! Pre-computed small powers.

// 32 BIT
#[cfg(arithmetic32)]
#[cfg(fast_arithmetic = "32")]
pub(crate) const POW5_32: [u32; 14] = [
1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, 48828125, 244140625,
1220703125,
];

#[cfg(arithmetic32)]
#[cfg(fast_arithmetic = "32")]
pub(crate) const POW10_32: [u32; 10] = [
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000,
];

// 64 BIT
#[cfg(arithmetic64)]
#[cfg(fast_arithmetic = "64")]
pub(crate) const POW5_64: [u64; 28] = [
1,
5,
Expand Down
4 changes: 2 additions & 2 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,9 @@ impl<'a> SliceRead<'a> {
// benchmarks and it's cross-platform, so probably the right fit.
// [1]: https://groups.google.com/forum/#!original/comp.lang.c/2HtQXvg7iKc/xOJeipH6KLMJ

#[cfg(arithmetic64)]
#[cfg(fast_arithmetic = "64")]
type Chunk = u64;
#[cfg(arithmetic32)]
#[cfg(fast_arithmetic = "32")]
type Chunk = u32;

const STEP: usize = mem::size_of::<Chunk>();
Expand Down
4 changes: 2 additions & 2 deletions tests/lexical/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ impl Math for Bigint {
}
}

#[cfg(arithmetic32)]
#[cfg(fast_arithmetic = "32")]
pub(crate) fn from_u32(x: &[u32]) -> Vec<Limb> {
x.iter().cloned().collect()
}

#[cfg(arithmetic64)]
#[cfg(fast_arithmetic = "64")]
pub(crate) fn from_u32(x: &[u32]) -> Vec<Limb> {
let mut v = Vec::<Limb>::default();
for xi in x.chunks(2) {
Expand Down

0 comments on commit e6282b0

Please sign in to comment.