Skip to content

Commit cd78955

Browse files
bors[bot]jackmott
andcommitted
77: adding ceilf and floorf r=japaric a=jackmott Finishes issues rust-lang#56 and rust-lang#54 Co-authored-by: Jack Mott <[email protected]>
2 parents 7379ef8 + 7268483 commit cd78955

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

src/lib.rs

-4
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
fn round(self) -> Self;
@@ -141,13 +139,11 @@ pub trait F32Ext: private::Sealed {
141139
}
142140

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

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

src/math/ceilf.rs

+29
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

+30
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

+4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ macro_rules! force_eval {
66
};
77
}
88

9+
mod ceilf;
910
mod fabs;
1011
mod fabsf;
12+
mod floorf;
1113
mod fmodf;
1214
mod powf;
1315
mod round;
@@ -27,8 +29,10 @@ mod hypotf;
2729
//mod service;
2830

2931
pub use self::{
32+
ceilf::ceilf,
3033
fabs::fabs,
3134
fabsf::fabsf,
35+
floorf::floorf,
3236
fmodf::fmodf,
3337
powf::powf,
3438
round::round,

test-generator/src/main.rs

+2-2
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)