| 1 | // [See license/rust-lang/libm] Copyright (c) 2018 Jorge Aparicio |
| 2 | #[cfg (not(all(any(target_arch = "x86" , target_arch = "x86_64" ), feature = "simd" )))] |
| 3 | pub fn trunc(x: f32) -> f32 { |
| 4 | let mut i: u32 = x.to_bits(); |
| 5 | let mut e: i32 = (i >> 23 & 0xff) as i32 - 0x7f + 9; |
| 6 | let m: u32; |
| 7 | if e >= 23 + 9 { |
| 8 | return x; |
| 9 | } |
| 10 | if e < 9 { |
| 11 | e = 1; |
| 12 | } |
| 13 | m = -1i32 as u32 >> e; |
| 14 | if (i & m) == 0 { |
| 15 | return x; |
| 16 | } |
| 17 | i &= !m; |
| 18 | f32::from_bits(i) |
| 19 | } |
| 20 | |
| 21 | #[cfg (all(any(target_arch = "x86" , target_arch = "x86_64" ), feature = "simd" ))] |
| 22 | #[inline (always)] |
| 23 | pub fn trunc(value: f32) -> f32 { |
| 24 | #[cfg (target_arch = "x86" )] |
| 25 | use core::arch::x86::*; |
| 26 | #[cfg (target_arch = "x86_64" )] |
| 27 | use core::arch::x86_64::*; |
| 28 | |
| 29 | unsafe { _mm_cvtss_f32(_mm_cvtepi32_ps(_mm_cvttps_epi32(_mm_set_ss(value)))) } |
| 30 | } |
| 31 | |