Skip to content

Commit

Permalink
Add signum
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Schultzer <benjamin@schultzer.com>
  • Loading branch information
Schultzer committed Jul 2, 2019
1 parent 885afa3 commit 311e4c8
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ pub trait F32Ext: private::Sealed + Sized {

fn abs(self) -> Self;

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

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

Expand Down Expand Up @@ -178,6 +177,15 @@ impl F32Ext for f32 {
fabsf(self)
}

#[inline]
fn signum(self) -> Self {
if self.is_nan() {
f32::NAN
} else {
copysignf(1., self)
}
}

#[inline]
fn mul_add(self, a: Self, b: Self) -> Self {
fmaf(self, a, b)
Expand Down Expand Up @@ -361,8 +369,7 @@ pub trait F64Ext: private::Sealed + Sized {

fn abs(self) -> Self;

// NOTE depends on unstable intrinsics::copysignf64
// fn signum(self) -> Self;
fn signum(self) -> Self;

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

Expand Down Expand Up @@ -466,6 +473,15 @@ impl F64Ext for f64 {
fabs(self)
}

#[inline]
fn signum(self) -> Self {
if self.is_nan() {
f64::NAN
} else {
copysign(1., self)
}
}

#[inline]
fn mul_add(self, a: Self, b: Self) -> Self {
fma(self, a, b)
Expand Down

0 comments on commit 311e4c8

Please sign in to comment.