Skip to content

Commit 235a7de

Browse files
69: implement hypotf r=japaric a=erikdesjardins closes rust-lang#48 Co-authored-by: Erik <[email protected]>
2 parents 41b9e65 + 4aad1fa commit 235a7de

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ pub trait F32Ext {
8888
#[cfg(todo)]
8989
fn cbrt(self) -> Self;
9090

91-
#[cfg(todo)]
9291
fn hypot(self, other: Self) -> Self;
9392

9493
#[cfg(todo)]
@@ -254,7 +253,6 @@ impl F32Ext for f32 {
254253
cbrtf(self)
255254
}
256255

257-
#[cfg(todo)]
258256
#[inline]
259257
fn hypot(self, other: Self) -> Self {
260258
hypotf(self, other)

src/math/hypotf.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use core::f32;
2+
3+
use super::sqrtf;
4+
5+
#[inline]
6+
pub fn hypotf(mut x: f32, mut y: f32) -> f32 {
7+
let x1p90 = f32::from_bits(0x6c800000); // 0x1p90f === 2 ^ 90
8+
let x1p_90 = f32::from_bits(0x12800000); // 0x1p-90f === 2 ^ -90
9+
10+
let mut uxi = x.to_bits();
11+
let mut uyi = y.to_bits();
12+
let uti;
13+
let mut z: f32;
14+
15+
uxi &= -1i32 as u32 >> 1;
16+
uyi &= -1i32 as u32 >> 1;
17+
if uxi < uyi {
18+
uti = uxi;
19+
uxi = uyi;
20+
uyi = uti;
21+
}
22+
23+
x = f32::from_bits(uxi);
24+
y = f32::from_bits(uyi);
25+
if uyi == 0xff<<23 {
26+
return y;
27+
}
28+
if uxi >= 0xff<<23 || uyi == 0 || uxi - uyi >= 25<<23 {
29+
return x + y;
30+
}
31+
32+
z = 1.;
33+
if uxi >= (0x7f+60)<<23 {
34+
z = x1p90;
35+
x *= x1p_90;
36+
y *= x1p_90;
37+
} else if uyi < (0x7f-60)<<23 {
38+
z = x1p_90;
39+
x *= x1p90;
40+
y *= x1p90;
41+
}
42+
z*sqrtf((x as f64 * x as f64 + y as f64 * y as f64) as f32)
43+
}

src/math/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod expf;
1717
mod floor;
1818
mod trunc;
1919
mod truncf;
20+
mod hypotf;
2021

2122
//mod service;
2223

@@ -34,6 +35,7 @@ pub use self::{
3435
floor::floor,
3536
trunc::trunc,
3637
truncf::truncf,
38+
hypotf::hypotf,
3739
};
3840

3941
fn isnanf(x: f32) -> bool {

test-generator/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ f32_f32! {
678678
// With signature `fn(f32, f32) -> f32`
679679
f32f32_f32! {
680680
// atan2f,
681-
// hypotf,
681+
hypotf,
682682
fmodf,
683683
powf,
684684
}

0 commit comments

Comments
 (0)