Skip to content

Commit

Permalink
Merge pull request #193 from Schultzer/add-signum
Browse files Browse the repository at this point in the history
Add signum
  • Loading branch information
gnzlbg committed Jul 2, 2019
2 parents 885afa3 + 311e4c8 commit b03bda3
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 b03bda3

Please sign in to comment.