Skip to content

Commit b7cf89f

Browse files
committed
Add mul_add and next_after methods to Float
1 parent 6cc7107 commit b7cf89f

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

src/libcore/num/f32.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,22 @@ impl Float for f32 {
543543
fn is_finite(&self) -> bool {
544544
!(self.is_NaN() || self.is_infinite())
545545
}
546+
547+
///
548+
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding error. This
549+
/// produces a more accurate result with better performance than a separate multiplication
550+
/// operation followed by an add.
551+
///
552+
#[inline(always)]
553+
fn mul_add(&self, a: f32, b: f32) -> f32 {
554+
mul_add(*self, a, b)
555+
}
556+
557+
/// Returns the next representable floating-point value in the direction of `other`
558+
#[inline(always)]
559+
fn next_after(&self, other: f32) -> f32 {
560+
nextafter(*self, other)
561+
}
546562
}
547563

548564
//

src/libcore/num/f64.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,22 @@ impl Float for f64 {
583583
fn is_finite(&self) -> bool {
584584
!(self.is_NaN() || self.is_infinite())
585585
}
586+
587+
///
588+
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding error. This
589+
/// produces a more accurate result with better performance than a separate multiplication
590+
/// operation followed by an add.
591+
///
592+
#[inline(always)]
593+
fn mul_add(&self, a: f64, b: f64) -> f64 {
594+
mul_add(*self, a, b)
595+
}
596+
597+
/// Returns the next representable floating-point value in the direction of `other`
598+
#[inline(always)]
599+
fn next_after(&self, other: f64) -> f64 {
600+
nextafter(*self, other)
601+
}
586602
}
587603

588604
//

src/libcore/num/float.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,22 @@ impl Float for float {
735735
fn is_finite(&self) -> bool {
736736
!(self.is_NaN() || self.is_infinite())
737737
}
738+
739+
///
740+
/// Fused multiply-add. Computes `(self * a) + b` with only one rounding error. This
741+
/// produces a more accurate result with better performance than a separate multiplication
742+
/// operation followed by an add.
743+
///
744+
#[inline(always)]
745+
fn mul_add(&self, a: float, b: float) -> float {
746+
mul_add(*self as f64, a as f64, b as f64) as float
747+
}
748+
749+
/// Returns the next representable floating-point value in the direction of `other`
750+
#[inline(always)]
751+
fn next_after(&self, other: float) -> float {
752+
nextafter(*self as f64, other as f64) as float
753+
}
738754
}
739755
740756
#[cfg(test)]

src/libcore/num/num.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ pub trait Fractional: Num
105105
fn recip(&self) -> Self;
106106
}
107107

108+
///
109+
/// Defines constants and methods common to real numbers
110+
///
108111
pub trait Real: Signed
109112
+ Fractional {
110113
// FIXME (#5527): usages of `int` should be replaced with an associated
@@ -237,8 +240,7 @@ pub trait Int: Integer
237240
+ BitCount {}
238241

239242
///
240-
/// Primitive floating point numbers. This trait should only be implemented
241-
/// for the `f32`, `f64`, and `float` types.
243+
/// Primitive floating point numbers
242244
///
243245
pub trait Float: Real
244246
+ Signed
@@ -252,6 +254,9 @@ pub trait Float: Real
252254
fn is_NaN(&self) -> bool;
253255
fn is_infinite(&self) -> bool;
254256
fn is_finite(&self) -> bool;
257+
258+
fn mul_add(&self, a: Self, b: Self) -> Self;
259+
fn next_after(&self, other: Self) -> Self;
255260
}
256261

257262
///

0 commit comments

Comments
 (0)