From 2ee6d9af3d3a83ccdeb4f3147b640b12cf2d35b0 Mon Sep 17 00:00:00 2001 From: Mahmut Bulut Date: Sat, 11 Apr 2020 03:08:23 +0200 Subject: [PATCH 1/2] Linear interpolation (lerp) implementation Enable feature flag for the use of it --- src/libstd/f32.rs | 24 ++++++++++++++++++++++++ src/libstd/f64.rs | 24 ++++++++++++++++++++++++ src/libstd/lib.rs | 1 + 3 files changed, 49 insertions(+) diff --git a/src/libstd/f32.rs b/src/libstd/f32.rs index 65273275a4006..874276d93e297 100644 --- a/src/libstd/f32.rs +++ b/src/libstd/f32.rs @@ -912,6 +912,30 @@ impl f32 { } x } + + /// Computes `self + step(upper - pol)` the linear interpolation between + /// `self` and `upper` for the parameter `pol` (or extrapolation, + /// when `pol` is outside the range [0,1]). + /// + /// # Examples + /// + /// ``` + /// #![feature(lerp)] + /// let x = 10.0_f32; + /// let y = 20.0_f32; + /// let pol = 0.5_f32; + /// + /// // lerp(x, y, pol) + /// let lerp = x.lerp(y, pol); + /// + /// assert!(lerp == 15.0_f32); + /// ``` + #[must_use = "method returns a new number and does not mutate the original value"] + #[unstable(feature = "lerp", issue = "71015")] + #[inline] + pub fn lerp(self, upper: f32, pol: f32) -> f32 { + self * (1_f32 - pol) + upper * pol + } } #[cfg(test)] diff --git a/src/libstd/f64.rs b/src/libstd/f64.rs index ff222fc8539d0..bf058a57bdab4 100644 --- a/src/libstd/f64.rs +++ b/src/libstd/f64.rs @@ -915,6 +915,30 @@ impl f64 { x } + /// Computes `self + step(upper - pol)` the linear interpolation between + /// `self` and `upper` for the parameter `pol` (or extrapolation, + /// when `pol` is outside the range [0,1]). + /// + /// # Examples + /// + /// ``` + /// #![feature(lerp)] + /// let x = 10.0_f64; + /// let y = 20.0_f64; + /// let pol = 0.5_f64; + /// + /// // lerp(x, y, pol) + /// let lerp = x.lerp(y, pol); + /// + /// assert!(lerp == 15.0_f64); + /// ``` + #[must_use = "method returns a new number and does not mutate the original value"] + #[unstable(feature = "lerp", issue = "71015")] + #[inline] + pub fn lerp(self, upper: f64, pol: f64) -> f64 { + self * (1_f64 - pol) + upper * pol + } + // Solaris/Illumos requires a wrapper around log, log2, and log10 functions // because of their non-standard behavior (e.g., log(-n) returns -Inf instead // of expected NaN). diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index a9a519f0a3a71..5d7a91335cbe7 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -247,6 +247,7 @@ #![feature(cfg_target_thread_local)] #![feature(char_error_internals)] #![feature(clamp)] +#![feature(lerp)] #![feature(concat_idents)] #![feature(const_cstr_unchecked)] #![feature(const_raw_ptr_deref)] From d255996c3d1b124af313f5ab25412e9654ad0016 Mon Sep 17 00:00:00 2001 From: Mahmut Bulut Date: Sun, 12 Apr 2020 15:48:57 +0200 Subject: [PATCH 2/2] Try using FMA ops --- src/libstd/f32.rs | 2 +- src/libstd/f64.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/f32.rs b/src/libstd/f32.rs index 874276d93e297..0e81f723245d0 100644 --- a/src/libstd/f32.rs +++ b/src/libstd/f32.rs @@ -934,7 +934,7 @@ impl f32 { #[unstable(feature = "lerp", issue = "71015")] #[inline] pub fn lerp(self, upper: f32, pol: f32) -> f32 { - self * (1_f32 - pol) + upper * pol + unsafe { intrinsics::fmaf32(self, 1_f32 - pol, upper * pol) } } } diff --git a/src/libstd/f64.rs b/src/libstd/f64.rs index bf058a57bdab4..3da01f7192c02 100644 --- a/src/libstd/f64.rs +++ b/src/libstd/f64.rs @@ -936,7 +936,7 @@ impl f64 { #[unstable(feature = "lerp", issue = "71015")] #[inline] pub fn lerp(self, upper: f64, pol: f64) -> f64 { - self * (1_f64 - pol) + upper * pol + unsafe { intrinsics::fmaf64(self, 1_f64 - pol, upper * pol) } } // Solaris/Illumos requires a wrapper around log, log2, and log10 functions