Skip to content

Commit

Permalink
fix u128 for older Rust - 2nd try
Browse files Browse the repository at this point in the history
  • Loading branch information
mohrezaei committed Nov 15, 2018
1 parent 7f1db48 commit 403dcd6
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions src/power10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,6 @@ static POWER10_HASH_U64: [u64;32] = [1, 100000000000, 10000000000000000, 0, 1000
1000000000000000000, 100, 100000000, 100000000000000000, 0, 10000000000000000000,
10000, 100000, 0, 1000000000, 0, 10000000000, 0];

#[cfg(has_i128)]
mod pow10_u128 {
static POWER10_HASH_U128: [u128; 64] = [100000000000000000000000000000000000, 0, 100000000000000000000000000000000,
0, 1000, 0, 0, 0, 0,
1000000000000000000000000000000000000, 1000000000, 0, 100000000000000, 100, 0, 0, 0, 100000, 0, 0,
10000000000000, 100000000000, 10000000000000000000, 0, 0, 10000000000000000000000000000000000,
100000000, 0, 1000000000000000000000000000000000, 1000000000000, 0, 100000000000000000000000000000000000000,
10000000000000000, 100000000000000000000000000, 0, 10000000000000000000000000000000000000,
1000000000000000000, 1, 10000000000000000000000000, 1000000000000000000000000, 100000000000000000000000000000,
10000000, 10000000000000000000000000000, 0, 1000000000000000000000000000, 100000000000000000, 10000,
0, 1000000, 1000000000000000000000000000000, 0, 100000000000000000000, 10, 0, 10000000000,
10000000000000000000000, 0, 0, 10000000000000000000000000000000, 1000000000000000000000, 0,
100000000000000000000000, 1000000000000000, 0];

#[inline]
pub fn is_pow10_u128(v: u128) -> bool {
let mut hash: u32 = v as u32 | (((v as u64) >> 32) as u32);
hash = hash.wrapping_mul(1249991743).rotate_right(25);
v == POWER10_HASH_U128[(hash & 63) as usize]
}
}

// implementation note: reverse search is a bit faster than hash lookup for u8
#[inline]
fn is_pow10_u8(v: u8) -> bool {
Expand Down Expand Up @@ -83,12 +61,6 @@ fn is_pow10_u64(v: u64) -> bool {
v == POWER10_HASH_U64[(hash & 31) as usize]
}

#[cfg(has_i128)]
#[inline]
fn is_pow10_u128(v: u128) -> bool {
pow10_u128::is_pow10_u128(v)
}

#[cfg(target_pointer_width = "64")]
#[inline]
fn is_pow10_usize(v: usize) -> bool {
Expand All @@ -101,6 +73,33 @@ fn is_pow10_usize(v: usize) -> bool {
is_pow10_u32(v as u32)
}

macro_rules! hide_u128 {
($T:ty) => {
static POWER10_HASH_U128: [$T; 64] = [100000000000000000000000000000000000, 0, 100000000000000000000000000000000,
0, 1000, 0, 0, 0, 0,
1000000000000000000000000000000000000, 1000000000, 0, 100000000000000, 100, 0, 0, 0, 100000, 0, 0,
10000000000000, 100000000000, 10000000000000000000, 0, 0, 10000000000000000000000000000000000,
100000000, 0, 1000000000000000000000000000000000, 1000000000000, 0, 100000000000000000000000000000000000000,
10000000000000000, 100000000000000000000000000, 0, 10000000000000000000000000000000000000,
1000000000000000000, 1, 10000000000000000000000000, 1000000000000000000000000, 100000000000000000000000000000,
10000000, 10000000000000000000000000000, 0, 1000000000000000000000000000, 100000000000000000, 10000,
0, 1000000, 1000000000000000000000000000000, 0, 100000000000000000000, 10, 0, 10000000000,
10000000000000000000000, 0, 0, 10000000000000000000000000000000, 1000000000000000000000, 0,
100000000000000000000000, 1000000000000000, 0];

#[inline]
pub fn is_pow10_u128(v: $T) -> bool {
let mut hash: u32 = v as u32 | (((v as u64) >> 32) as u32);
hash = hash.wrapping_mul(1249991743).rotate_right(25);
v == POWER10_HASH_U128[(hash & 63) as usize]
}

}
}

#[cfg(has_i128)]
hide_u128!(u128);

macro_rules! unsigned_power10 {
($T:ty, $pow_fn: ident) => {
impl Power10 for $T {
Expand Down

0 comments on commit 403dcd6

Please sign in to comment.