Skip to content

Commit 7268483

Browse files
committed
adding ceilf and floorf for issues rust-lang#56 and rust-lang#54
1 parent 7275814 commit 7268483

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ pub fn _eq(a: u64, b: u64) -> bool {
3434
///
3535
/// This trait is sealed and cannot be implemented outside of `libm`.
3636
pub trait F32Ext: private::Sealed {
37-
#[cfg(todo)]
3837
fn floor(self) -> Self;
3938

40-
#[cfg(todo)]
4139
fn ceil(self) -> Self;
4240

4341
#[cfg(todo)]
@@ -142,13 +140,11 @@ pub trait F32Ext: private::Sealed {
142140
}
143141

144142
impl F32Ext for f32 {
145-
#[cfg(todo)]
146143
#[inline]
147144
fn floor(self) -> Self {
148145
floorf(self)
149146
}
150147

151-
#[cfg(todo)]
152148
#[inline]
153149
fn ceil(self) -> Self {
154150
ceilf(self)

src/math/ceilf.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use core::f32;
2+
3+
pub fn ceilf(x: f32) -> f32 {
4+
let mut ui = x.to_bits();
5+
let e = (((ui >> 23) & 0xff) - 0x7f) as i32;
6+
7+
if e >= 23 {
8+
return x;
9+
}
10+
if e >= 0 {
11+
let m = 0x007fffff >> e;
12+
if (ui & m) == 0 {
13+
return x;
14+
}
15+
force_eval!(x + f32::from_bits(0x7b800000));
16+
if ui >> 31 == 0 {
17+
ui += m;
18+
}
19+
ui &= !m;
20+
} else {
21+
force_eval!(x + f32::from_bits(0x7b800000));
22+
if ui >> 31 != 0 {
23+
return -0.0;
24+
} else if ui << 1 != 0 {
25+
return 1.0;
26+
}
27+
}
28+
return f32::from_bits(ui);
29+
}

src/math/floorf.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use core::f32;
2+
3+
#[inline]
4+
pub fn floorf(x: f32) -> f32 {
5+
let mut ui = x.to_bits();
6+
let e = (((ui >> 23) & 0xff) - 0x7f) as i32;
7+
8+
if e >= 23 {
9+
return x;
10+
}
11+
if e >= 0 {
12+
let m: u32 = 0x007fffff >> e;
13+
if (ui & m) == 0 {
14+
return x;
15+
}
16+
force_eval!(x + f32::from_bits(0x7b800000));
17+
if ui >> 31 != 0 {
18+
ui += m;
19+
}
20+
ui &= !m;
21+
} else {
22+
force_eval!(x + f32::from_bits(0x7b800000));
23+
if ui >> 31 == 0 {
24+
ui = 0;
25+
} else if ui << 1 != 0 {
26+
return -1.0;
27+
}
28+
}
29+
return f32::from_bits(ui);
30+
}

src/math/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ macro_rules! force_eval {
44
}
55
}
66

7+
mod ceilf;
78
mod fabs;
89
mod fabsf;
10+
mod floorf;
911
mod fmodf;
1012
mod powf;
1113
mod round;
@@ -24,8 +26,10 @@ mod hypotf;
2426
//mod service;
2527

2628
pub use self::{
29+
ceilf::ceilf,
2730
fabs::fabs,
2831
fabsf::fabsf,
32+
floorf::floorf,
2933
fmodf::fmodf,
3034
powf::powf,
3135
round::round,

test-generator/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,12 +652,12 @@ fn main() -> Result<(), Box<Error>> {
652652
// With signature `fn(f32) -> f32`
653653
f32_f32! {
654654
// acosf,
655-
// floorf,
655+
floorf,
656656
truncf,
657657
// asinf,
658658
// atanf,
659659
// cbrtf,
660-
// ceilf,
660+
ceilf,
661661
// cosf,
662662
// coshf,
663663
// exp2f,

0 commit comments

Comments
 (0)