Sha256: 51f19443008e8884b15c7d5d4b1f1cfbd5673a9d1da02f1af39fcf20e315f01f

Contents?: true

Size: 1.53 KB

Versions: 8

Compression:

Stored size: 1.53 KB

Contents

// Adapted from https://github.com/Alexhuszagh/rust-lexical.

//! Utilities to calculate exponents.

/// Convert usize into i32 without overflow.
///
/// This is needed to ensure when adjusting the exponent relative to
/// the mantissa we do not overflow for comically-long exponents.
#[inline]
fn into_i32(value: usize) -> i32 {
    if value > i32::MAX as usize {
        i32::MAX
    } else {
        value as i32
    }
}

// EXPONENT CALCULATION

// Calculate the scientific notation exponent without overflow.
//
// For example, 0.1 would be -1, and 10 would be 1 in base 10.
#[inline]
pub(crate) fn scientific_exponent(
    exponent: i32,
    integer_digits: usize,
    fraction_start: usize,
) -> i32 {
    if integer_digits == 0 {
        let fraction_start = into_i32(fraction_start);
        exponent.saturating_sub(fraction_start).saturating_sub(1)
    } else {
        let integer_shift = into_i32(integer_digits - 1);
        exponent.saturating_add(integer_shift)
    }
}

// Calculate the mantissa exponent without overflow.
//
// Remove the number of digits that contributed to the mantissa past
// the dot, and add the number of truncated digits from the mantissa,
// to calculate the scaling factor for the mantissa from a raw exponent.
#[inline]
pub(crate) fn mantissa_exponent(exponent: i32, fraction_digits: usize, truncated: usize) -> i32 {
    if fraction_digits > truncated {
        exponent.saturating_sub(into_i32(fraction_digits - truncated))
    } else {
        exponent.saturating_add(into_i32(truncated - fraction_digits))
    }
}

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/serde_json-1.0.137/src/lexical/exponent.rs
wasmtime-28.0.0 ./ext/cargo-vendor/serde_json-1.0.135/src/lexical/exponent.rs
wasmtime-27.0.0 ./ext/cargo-vendor/serde_json-1.0.133/src/lexical/exponent.rs
wasmtime-26.0.0 ./ext/cargo-vendor/serde_json-1.0.132/src/lexical/exponent.rs
wasmtime-25.0.2 ./ext/cargo-vendor/serde_json-1.0.125/src/lexical/exponent.rs
wasmtime-25.0.1 ./ext/cargo-vendor/serde_json-1.0.125/src/lexical/exponent.rs
wasmtime-25.0.0 ./ext/cargo-vendor/serde_json-1.0.125/src/lexical/exponent.rs
wasmtime-24.0.0 ./ext/cargo-vendor/serde_json-1.0.125/src/lexical/exponent.rs