Sha256: dee3607baf1af0f01deae46e429e097234c50b268eaefebbe716f19f38597900

Contents?: true

Size: 1.05 KB

Versions: 11

Compression:

Stored size: 1.05 KB

Contents

use core::f32;

#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn truncf(x: f32) -> f32 {
    // On wasm32 we know that LLVM's intrinsic will compile to an optimized
    // `f32.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::truncf32(x) }
        }
    }
    let x1p120 = f32::from_bits(0x7b800000); // 0x1p120f === 2 ^ 120

    let mut i: u32 = x.to_bits();
    let mut e: i32 = (i >> 23 & 0xff) as i32 - 0x7f + 9;
    let m: u32;

    if e >= 23 + 9 {
        return x;
    }
    if e < 9 {
        e = 1;
    }
    m = -1i32 as u32 >> e;
    if (i & m) == 0 {
        return x;
    }
    force_eval!(x + x1p120);
    i &= !m;
    f32::from_bits(i)
}

// PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
#[cfg(not(target_arch = "powerpc64"))]
#[cfg(test)]
mod tests {
    #[test]
    fn sanity_check() {
        assert_eq!(super::truncf(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/truncf.rs
wasmtime-28.0.0 ./ext/cargo-vendor/libm-0.2.11/src/math/truncf.rs
wasmtime-27.0.0 ./ext/cargo-vendor/libm-0.2.11/src/math/truncf.rs
wasmtime-26.0.0 ./ext/cargo-vendor/libm-0.2.8/src/math/truncf.rs
wasmtime-25.0.2 ./ext/cargo-vendor/libm-0.2.8/src/math/truncf.rs
wasmtime-25.0.1 ./ext/cargo-vendor/libm-0.2.8/src/math/truncf.rs
wasmtime-25.0.0 ./ext/cargo-vendor/libm-0.2.8/src/math/truncf.rs
wasmtime-24.0.0 ./ext/cargo-vendor/libm-0.2.8/src/math/truncf.rs
wasmtime-23.0.2 ./ext/cargo-vendor/libm-0.2.8/src/math/truncf.rs
wasmtime-22.0.0 ./ext/cargo-vendor/libm-0.2.8/src/math/truncf.rs
wasmtime-21.0.1 ./ext/cargo-vendor/libm-0.2.8/src/math/truncf.rs