Skip to content

Commit b03bda3

Browse files
authored
Merge pull request rust-lang#193 from Schultzer/add-signum
Add signum
2 parents 885afa3 + 311e4c8 commit b03bda3

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/lib.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ pub trait F32Ext: private::Sealed + Sized {
7373

7474
fn abs(self) -> Self;
7575

76-
// NOTE depends on unstable intrinsics::copysignf32
77-
// fn signum(self) -> Self;
76+
fn signum(self) -> Self;
7877

7978
fn mul_add(self, a: Self, b: Self) -> Self;
8079

@@ -178,6 +177,15 @@ impl F32Ext for f32 {
178177
fabsf(self)
179178
}
180179

180+
#[inline]
181+
fn signum(self) -> Self {
182+
if self.is_nan() {
183+
f32::NAN
184+
} else {
185+
copysignf(1., self)
186+
}
187+
}
188+
181189
#[inline]
182190
fn mul_add(self, a: Self, b: Self) -> Self {
183191
fmaf(self, a, b)
@@ -361,8 +369,7 @@ pub trait F64Ext: private::Sealed + Sized {
361369

362370
fn abs(self) -> Self;
363371

364-
// NOTE depends on unstable intrinsics::copysignf64
365-
// fn signum(self) -> Self;
372+
fn signum(self) -> Self;
366373

367374
fn mul_add(self, a: Self, b: Self) -> Self;
368375

@@ -466,6 +473,15 @@ impl F64Ext for f64 {
466473
fabs(self)
467474
}
468475

476+
#[inline]
477+
fn signum(self) -> Self {
478+
if self.is_nan() {
479+
f64::NAN
480+
} else {
481+
copysign(1., self)
482+
}
483+
}
484+
469485
#[inline]
470486
fn mul_add(self, a: Self, b: Self) -> Self {
471487
fma(self, a, b)

0 commit comments

Comments
 (0)