Sha256: 642264897cc1505e720c8cf313be81aa9fd53aae866644a2e988d01dbc77fd8a

Contents?: true

Size: 962 Bytes

Versions: 11

Compression:

Stored size: 962 Bytes

Contents

use core::f64;

#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn trunc(x: f64) -> f64 {
    // On wasm32 we know that LLVM's intrinsic will compile to an optimized
    // `f64.trunc` native instruction, so we can leverage this for both code size
    // and speed.
    llvm_intrinsically_optimized! {
        #[cfg(target_arch = "wasm32")] {
            return unsafe { ::core::intrinsics::truncf64(x) }
        }
    }
    let x1p120 = f64::from_bits(0x4770000000000000); // 0x1p120f === 2 ^ 120

    let mut i: u64 = x.to_bits();
    let mut e: i64 = (i >> 52 & 0x7ff) as i64 - 0x3ff + 12;
    let m: u64;

    if e >= 52 + 12 {
        return x;
    }
    if e < 12 {
        e = 1;
    }
    m = -1i64 as u64 >> e;
    if (i & m) == 0 {
        return x;
    }
    force_eval!(x + x1p120);
    i &= !m;
    f64::from_bits(i)
}

#[cfg(test)]
mod tests {
    #[test]
    fn sanity_check() {
        assert_eq!(super::trunc(1.1), 1.0);
    }
}

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/libm-0.2.11/src/math/trunc.rs
wasmtime-28.0.0 ./ext/cargo-vendor/libm-0.2.11/src/math/trunc.rs
wasmtime-27.0.0 ./ext/cargo-vendor/libm-0.2.11/src/math/trunc.rs
wasmtime-26.0.0 ./ext/cargo-vendor/libm-0.2.8/src/math/trunc.rs
wasmtime-25.0.2 ./ext/cargo-vendor/libm-0.2.8/src/math/trunc.rs
wasmtime-25.0.1 ./ext/cargo-vendor/libm-0.2.8/src/math/trunc.rs
wasmtime-25.0.0 ./ext/cargo-vendor/libm-0.2.8/src/math/trunc.rs
wasmtime-24.0.0 ./ext/cargo-vendor/libm-0.2.8/src/math/trunc.rs
wasmtime-23.0.2 ./ext/cargo-vendor/libm-0.2.8/src/math/trunc.rs
wasmtime-22.0.0 ./ext/cargo-vendor/libm-0.2.8/src/math/trunc.rs
wasmtime-21.0.1 ./ext/cargo-vendor/libm-0.2.8/src/math/trunc.rs