1 | // [See license/rust-lang/libm] Copyright (c) 2018 Jorge Aparicio |
---|---|
2 | pub fn floor(x: f32) -> f32 { |
3 | let mut ui = x.to_bits(); |
4 | let e = (((ui >> 23) as i32) & 0xff) - 0x7f; |
5 | |
6 | if e >= 23 { |
7 | return x; |
8 | } |
9 | if e >= 0 { |
10 | let m: u32 = 0x007fffff >> e; |
11 | if (ui & m) == 0 { |
12 | return x; |
13 | } |
14 | if ui >> 31 != 0 { |
15 | ui += m; |
16 | } |
17 | ui &= !m; |
18 | } else { |
19 | if ui >> 31 == 0 { |
20 | ui = 0; |
21 | } else if ui << 1 != 0 { |
22 | return -1.0; |
23 | } |
24 | } |
25 | f32::from_bits(ui) |
26 | } |
27 |