Skip to content

Commit

Permalink
Add std::simd::StdFloat methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
n3vu0r committed Mar 4, 2022
1 parent f5f5028 commit cd6ba34
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 6 deletions.
8 changes: 4 additions & 4 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Version 0.5.0 (2022-02-07)
# Version 0.5.0 (2022-03-04)

* Add [`SimdReal::mul_add()`] (incomplete).
* Add [`std::simd::StdFloat`] methods.
* Add SIMD trait/type conversations.
* Add [`FromUnchecked`] and [`IntoUnchecked`] traits.
* Rename [`WrapFrom`] and [`WrapInto`] traits to [`PeelFrom`] and [`PeelInto`] traits.
Expand Down Expand Up @@ -76,5 +76,5 @@ https://docs.rs/lav/latest/lav/trait.SimdReal.html#method.mask_flag
https://docs.rs/lav/latest/lav/trait.SimdReal.html#method.lanes_approx_ne
[`SimdReal::insert()`]:
https://docs.rs/lav/latest/lav/trait.SimdReal.html#method.insert
[`SimdReal::mul_add()`]:
https://docs.rs/lav/latest/lav/trait.SimdReal.html#method.mul_add
[`std::simd::StdFloat`]:
https://doc.rust-lang.org/nightly/std/simd/trait.StdFloat.html
71 changes: 70 additions & 1 deletion src/simd_real/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use super::{ApproxEq, Select, SimdReal};
use core::simd::{LaneCount, Mask, Simd, SupportedLaneCount, Swizzle, Swizzle2};

#[cfg(feature = "libm")]
use super::Real;
#[cfg(not(feature = "libm"))]
use std::simd::StdFloat;

impl<const LANES: usize> SimdReal<f32, LANES> for Simd<f32, LANES>
where
LaneCount<LANES>: SupportedLaneCount,
Expand Down Expand Up @@ -231,11 +236,75 @@ where
self.to_radians()
}

#[cfg(feature = "libm")]
#[inline]
fn mul_add(self, a: Self, b: Self) -> Self {
// TODO self.mul_add(a, b)
self * a + b
}
#[cfg(not(feature = "libm"))]
#[inline]
fn mul_add(self, a: Self, b: Self) -> Self {
StdFloat::mul_add(self, a, b)
}
#[cfg(feature = "libm")]
fn sqrt(self) -> Self {
self.to_array().map(Real::sqrt).into()
}
#[cfg(not(feature = "libm"))]
#[inline]
fn sqrt(self) -> Self {
StdFloat::sqrt(self)
}
#[cfg(feature = "libm")]
#[inline]
fn floor(self) -> Self {
self.to_array().map(Real::floor).into()
}
#[cfg(not(feature = "libm"))]
#[inline]
fn floor(self) -> Self {
StdFloat::floor(self)
}
#[cfg(feature = "libm")]
#[inline]
fn ceil(self) -> Self {
self.to_array().map(Real::ceil).into()
}
#[cfg(not(feature = "libm"))]
#[inline]
fn ceil(self) -> Self {
StdFloat::ceil(self)
}
#[cfg(feature = "libm")]
#[inline]
fn round(self) -> Self {
self.to_array().map(Real::round).into()
}
#[cfg(not(feature = "libm"))]
#[inline]
fn round(self) -> Self {
StdFloat::round(self)
}
#[cfg(feature = "libm")]
#[inline]
fn trunc(self) -> Self {
self.to_array().map(Real::trunc).into()
}
#[cfg(not(feature = "libm"))]
#[inline]
fn trunc(self) -> Self {
StdFloat::trunc(self)
}
#[cfg(feature = "libm")]
#[inline]
fn fract(self) -> Self {
self.to_array().map(Real::fract).into()
}
#[cfg(not(feature = "libm"))]
#[inline]
fn fract(self) -> Self {
StdFloat::fract(self)
}
}

impl<const LANES: usize> Select<Mask<i32, LANES>> for Simd<f32, LANES>
Expand Down
71 changes: 70 additions & 1 deletion src/simd_real/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use super::{ApproxEq, Select, SimdReal};
use core::simd::{LaneCount, Mask, Simd, SupportedLaneCount, Swizzle, Swizzle2};

#[cfg(feature = "libm")]
use super::Real;
#[cfg(not(feature = "libm"))]
use std::simd::StdFloat;

impl<const LANES: usize> SimdReal<f64, LANES> for Simd<f64, LANES>
where
LaneCount<LANES>: SupportedLaneCount,
Expand Down Expand Up @@ -231,11 +236,75 @@ where
self.to_radians()
}

#[cfg(feature = "libm")]
#[inline]
fn mul_add(self, a: Self, b: Self) -> Self {
// TODO self.mul_add(a, b)
self * a + b
}
#[cfg(not(feature = "libm"))]
#[inline]
fn mul_add(self, a: Self, b: Self) -> Self {
StdFloat::mul_add(self, a, b)
}
#[cfg(feature = "libm")]
fn sqrt(self) -> Self {
self.to_array().map(Real::sqrt).into()
}
#[cfg(not(feature = "libm"))]
#[inline]
fn sqrt(self) -> Self {
StdFloat::sqrt(self)
}
#[cfg(feature = "libm")]
#[inline]
fn floor(self) -> Self {
self.to_array().map(Real::floor).into()
}
#[cfg(not(feature = "libm"))]
#[inline]
fn floor(self) -> Self {
StdFloat::floor(self)
}
#[cfg(feature = "libm")]
#[inline]
fn ceil(self) -> Self {
self.to_array().map(Real::ceil).into()
}
#[cfg(not(feature = "libm"))]
#[inline]
fn ceil(self) -> Self {
StdFloat::ceil(self)
}
#[cfg(feature = "libm")]
#[inline]
fn round(self) -> Self {
self.to_array().map(Real::round).into()
}
#[cfg(not(feature = "libm"))]
#[inline]
fn round(self) -> Self {
StdFloat::round(self)
}
#[cfg(feature = "libm")]
#[inline]
fn trunc(self) -> Self {
self.to_array().map(Real::trunc).into()
}
#[cfg(not(feature = "libm"))]
#[inline]
fn trunc(self) -> Self {
StdFloat::trunc(self)
}
#[cfg(feature = "libm")]
#[inline]
fn fract(self) -> Self {
self.to_array().map(Real::fract).into()
}
#[cfg(not(feature = "libm"))]
#[inline]
fn fract(self) -> Self {
StdFloat::fract(self)
}
}

impl<const LANES: usize> Select<Mask<i64, LANES>> for Simd<f64, LANES>
Expand Down
19 changes: 19 additions & 0 deletions src/simd_real/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,25 @@ where
/// will be heavily dependant on designing algorithms with specific target hardware in mind.
#[must_use]
fn mul_add(self, a: Self, b: Self) -> Self;
/// Produces a vector where every lane has the square root value of the equivalently-indexed
/// lane in `self`
#[must_use]
fn sqrt(self) -> Self;
/// Returns the largest integer value less than or equal to each lane.
#[must_use]
fn floor(self) -> Self;
/// Returns the smallest integer greater than or equal to each lane.
#[must_use]
fn ceil(self) -> Self;
/// Rounds to the nearest integer value. Ties round toward zero.
#[must_use]
fn round(self) -> Self;
/// Returns the floating point's integer value, with its fractional part removed.
#[must_use]
fn trunc(self) -> Self;
/// Returns the floating point's fractional value, with its integer part removed.
#[must_use]
fn fract(self) -> Self;

/// Converts an array to a SIMD vector mask.
#[must_use]
Expand Down

0 comments on commit cd6ba34

Please sign in to comment.