split off code into quaternion_simd.rs

This commit is contained in:
Norbert Nemec 2019-09-01 20:44:24 +02:00
parent f21232a123
commit 153754eb38
3 changed files with 146 additions and 142 deletions

View file

@ -99,6 +99,10 @@ mod structure;
mod matrix; mod matrix;
mod quaternion; mod quaternion;
#[cfg(feature = "simd")]
mod quaternion_simd;
mod vector; mod vector;
#[cfg(feature = "simd")] #[cfg(feature = "simd")]

View file

@ -35,9 +35,6 @@ use point::Point3;
use rotation::{Basis3, Rotation, Rotation3}; use rotation::{Basis3, Rotation, Rotation3};
use vector::Vector3; use vector::Vector3;
#[cfg(feature = "simd")]
use simd::f32x4 as Simdf32x4;
#[cfg(feature = "mint")] #[cfg(feature = "mint")]
use mint; use mint;
@ -55,30 +52,6 @@ pub struct Quaternion<S> {
pub v: Vector3<S>, pub v: Vector3<S>,
} }
#[cfg(feature = "simd")]
impl From<Simdf32x4> for Quaternion<f32> {
#[inline]
fn from(f: Simdf32x4) -> Self {
unsafe {
let mut ret: Self = mem::uninitialized();
{
let ret_mut: &mut [f32; 4] = ret.as_mut();
f.store(ret_mut.as_mut(), 0 as usize);
}
ret
}
}
}
#[cfg(feature = "simd")]
impl Into<Simdf32x4> for Quaternion<f32> {
#[inline]
fn into(self) -> Simdf32x4 {
let self_ref: &[f32; 4] = self.as_ref();
Simdf32x4::load(self_ref.as_ref(), 0 as usize)
}
}
impl<S> Quaternion<S> { impl<S> Quaternion<S> {
/// Construct a new quaternion from one scalar component and three /// Construct a new quaternion from one scalar component and three
/// imaginary components. /// imaginary components.
@ -267,17 +240,6 @@ impl<S: BaseFloat> InnerSpace for Quaternion<S> {
} ); } );
} }
#[cfg(feature = "simd")]
impl InnerSpace for Quaternion<f32> {
#[inline]
fn dot(self, other: Quaternion<f32>) -> f32 {
let lhs: Simdf32x4 = self.into();
let rhs: Simdf32x4 = other.into();
let r = lhs * rhs;
r.extract(0) + r.extract(1) + r.extract(2) + r.extract(3)
}
}
impl<A> From<Euler<A>> for Quaternion<A::Unitless> impl<A> From<Euler<A>> for Quaternion<A::Unitless>
where where
A: Angle + Into<Rad<<A as Angle>::Unitless>>, A: Angle + Into<Rad<<A as Angle>::Unitless>>,
@ -308,71 +270,26 @@ impl_operator!(<S: BaseFloat> Neg for Quaternion<S> {
} }
}); });
#[cfg(feature = "simd")]
impl_operator_simd!{
[Simdf32x4]; Neg for Quaternion<f32> {
fn neg(lhs) -> Quaternion<f32> {
(-lhs).into()
}
}
}
impl_operator!(<S: BaseFloat> Mul<S> for Quaternion<S> { impl_operator!(<S: BaseFloat> Mul<S> for Quaternion<S> {
fn mul(lhs, rhs) -> Quaternion<S> { fn mul(lhs, rhs) -> Quaternion<S> {
Quaternion::from_sv(lhs.s * rhs, lhs.v * rhs) Quaternion::from_sv(lhs.s * rhs, lhs.v * rhs)
} }
}); });
#[cfg(feature = "simd")]
impl_operator_simd!{@rs
[Simdf32x4]; Mul<f32> for Quaternion<f32> {
fn mul(lhs, rhs) -> Quaternion<f32> {
(lhs * rhs).into()
}
}
}
impl_assignment_operator!(<S: BaseFloat> MulAssign<S> for Quaternion<S> { impl_assignment_operator!(<S: BaseFloat> MulAssign<S> for Quaternion<S> {
fn mul_assign(&mut self, scalar) { self.s *= scalar; self.v *= scalar; } fn mul_assign(&mut self, scalar) { self.s *= scalar; self.v *= scalar; }
}); });
#[cfg(feature = "simd")]
impl MulAssign<f32> for Quaternion<f32> {
fn mul_assign(&mut self, other: f32) {
let s: Simdf32x4 = (*self).into();
let other = Simdf32x4::splat(other);
*self = (s * other).into();
}
}
impl_operator!(<S: BaseFloat> Div<S> for Quaternion<S> { impl_operator!(<S: BaseFloat> Div<S> for Quaternion<S> {
fn div(lhs, rhs) -> Quaternion<S> { fn div(lhs, rhs) -> Quaternion<S> {
Quaternion::from_sv(lhs.s / rhs, lhs.v / rhs) Quaternion::from_sv(lhs.s / rhs, lhs.v / rhs)
} }
}); });
#[cfg(feature = "simd")]
impl_operator_simd!{@rs
[Simdf32x4]; Div<f32> for Quaternion<f32> {
fn div(lhs, rhs) -> Quaternion<f32> {
(lhs / rhs).into()
}
}
}
impl_assignment_operator!(<S: BaseFloat> DivAssign<S> for Quaternion<S> { impl_assignment_operator!(<S: BaseFloat> DivAssign<S> for Quaternion<S> {
fn div_assign(&mut self, scalar) { self.s /= scalar; self.v /= scalar; } fn div_assign(&mut self, scalar) { self.s /= scalar; self.v /= scalar; }
}); });
#[cfg(feature = "simd")]
impl DivAssign<f32> for Quaternion<f32> {
fn div_assign(&mut self, other: f32) {
let s: Simdf32x4 = (*self).into();
let other = Simdf32x4::splat(other);
*self = (s / other).into();
}
}
impl_operator!(<S: BaseFloat> Rem<S> for Quaternion<S> { impl_operator!(<S: BaseFloat> Rem<S> for Quaternion<S> {
fn rem(lhs, rhs) -> Quaternion<S> { fn rem(lhs, rhs) -> Quaternion<S> {
Quaternion::from_sv(lhs.s % rhs, lhs.v % rhs) Quaternion::from_sv(lhs.s % rhs, lhs.v % rhs)
@ -398,58 +315,20 @@ impl_operator!(<S: BaseFloat> Add<Quaternion<S> > for Quaternion<S> {
} }
}); });
#[cfg(feature = "simd")]
impl_operator_simd!{
[Simdf32x4]; Add<Quaternion<f32>> for Quaternion<f32> {
fn add(lhs, rhs) -> Quaternion<f32> {
(lhs + rhs).into()
}
}
}
impl_assignment_operator!(<S: BaseFloat> AddAssign<Quaternion<S> > for Quaternion<S> { impl_assignment_operator!(<S: BaseFloat> AddAssign<Quaternion<S> > for Quaternion<S> {
fn add_assign(&mut self, other) { self.s += other.s; self.v += other.v; } fn add_assign(&mut self, other) { self.s += other.s; self.v += other.v; }
}); });
#[cfg(feature = "simd")]
impl AddAssign for Quaternion<f32> {
#[inline]
fn add_assign(&mut self, rhs: Self) {
let s: Simdf32x4 = (*self).into();
let rhs: Simdf32x4 = rhs.into();
*self = (s + rhs).into();
}
}
impl_operator!(<S: BaseFloat> Sub<Quaternion<S> > for Quaternion<S> { impl_operator!(<S: BaseFloat> Sub<Quaternion<S> > for Quaternion<S> {
fn sub(lhs, rhs) -> Quaternion<S> { fn sub(lhs, rhs) -> Quaternion<S> {
Quaternion::from_sv(lhs.s - rhs.s, lhs.v - rhs.v) Quaternion::from_sv(lhs.s - rhs.s, lhs.v - rhs.v)
} }
}); });
#[cfg(feature = "simd")]
impl_operator_simd!{
[Simdf32x4]; Sub<Quaternion<f32>> for Quaternion<f32> {
fn sub(lhs, rhs) -> Quaternion<f32> {
(lhs - rhs).into()
}
}
}
impl_assignment_operator!(<S: BaseFloat> SubAssign<Quaternion<S> > for Quaternion<S> { impl_assignment_operator!(<S: BaseFloat> SubAssign<Quaternion<S> > for Quaternion<S> {
fn sub_assign(&mut self, other) { self.s -= other.s; self.v -= other.v; } fn sub_assign(&mut self, other) { self.s -= other.s; self.v -= other.v; }
}); });
#[cfg(feature = "simd")]
impl SubAssign for Quaternion<f32> {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
let s: Simdf32x4 = (*self).into();
let rhs: Simdf32x4 = rhs.into();
*self = (s - rhs).into();
}
}
impl_operator!(<S: BaseFloat> Mul<Quaternion<S> > for Quaternion<S> { impl_operator!(<S: BaseFloat> Mul<Quaternion<S> > for Quaternion<S> {
fn mul(lhs, rhs) -> Quaternion<S> { fn mul(lhs, rhs) -> Quaternion<S> {
Quaternion::new( Quaternion::new(
@ -461,27 +340,6 @@ impl_operator!(<S: BaseFloat> Mul<Quaternion<S> > for Quaternion<S> {
} }
}); });
#[cfg(feature = "simd")]
impl_operator_simd!{
[Simdf32x4]; Mul<Quaternion<f32>> for Quaternion<f32> {
fn mul(lhs, rhs) -> Quaternion<f32> {
{
let p0 = Simdf32x4::splat(lhs.extract(0)) * rhs;
let p1 = Simdf32x4::splat(lhs.extract(1)) * Simdf32x4::new(
-rhs.extract(1), rhs.extract(0), -rhs.extract(3), rhs.extract(2)
);
let p2 = Simdf32x4::splat(lhs.extract(2)) * Simdf32x4::new(
-rhs.extract(2), rhs.extract(3), rhs.extract(0), -rhs.extract(1)
);
let p3 = Simdf32x4::splat(lhs.extract(3)) * Simdf32x4::new(
-rhs.extract(3), -rhs.extract(2), rhs.extract(1), rhs.extract(0)
);
(p0 + p1 + p2 + p3).into()
}
}
}
}
macro_rules! impl_scalar_mul { macro_rules! impl_scalar_mul {
($S:ident) => { ($S:ident) => {
impl_operator!(Mul<Quaternion<$S>> for $S { impl_operator!(Mul<Quaternion<$S>> for $S {

142
src/quaternion_simd.rs Normal file
View file

@ -0,0 +1,142 @@
// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors,
// refer to the Cargo.toml file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use simd::f32x4 as Simdf32x4;
impl From<Simdf32x4> for Quaternion<f32> {
#[inline]
fn from(f: Simdf32x4) -> Self {
unsafe {
let mut ret: Self = mem::uninitialized();
{
let ret_mut: &mut [f32; 4] = ret.as_mut();
f.store(ret_mut.as_mut(), 0 as usize);
}
ret
}
}
}
impl Into<Simdf32x4> for Quaternion<f32> {
#[inline]
fn into(self) -> Simdf32x4 {
let self_ref: &[f32; 4] = self.as_ref();
Simdf32x4::load(self_ref.as_ref(), 0 as usize)
}
}
impl InnerSpace for Quaternion<f32> {
#[inline]
fn dot(self, other: Quaternion<f32>) -> f32 {
let lhs: Simdf32x4 = self.into();
let rhs: Simdf32x4 = other.into();
let r = lhs * rhs;
r.extract(0) + r.extract(1) + r.extract(2) + r.extract(3)
}
}
impl_operator_simd!{
[Simdf32x4]; Neg for Quaternion<f32> {
fn neg(lhs) -> Quaternion<f32> {
(-lhs).into()
}
}
}
impl_operator_simd!{@rs
[Simdf32x4]; Mul<f32> for Quaternion<f32> {
fn mul(lhs, rhs) -> Quaternion<f32> {
(lhs * rhs).into()
}
}
}
impl MulAssign<f32> for Quaternion<f32> {
fn mul_assign(&mut self, other: f32) {
let s: Simdf32x4 = (*self).into();
let other = Simdf32x4::splat(other);
*self = (s * other).into();
}
}
impl_operator_simd!{@rs
[Simdf32x4]; Div<f32> for Quaternion<f32> {
fn div(lhs, rhs) -> Quaternion<f32> {
(lhs / rhs).into()
}
}
}
impl DivAssign<f32> for Quaternion<f32> {
fn div_assign(&mut self, other: f32) {
let s: Simdf32x4 = (*self).into();
let other = Simdf32x4::splat(other);
*self = (s / other).into();
}
}
impl_operator_simd!{
[Simdf32x4]; Add<Quaternion<f32>> for Quaternion<f32> {
fn add(lhs, rhs) -> Quaternion<f32> {
(lhs + rhs).into()
}
}
}
impl AddAssign for Quaternion<f32> {
#[inline]
fn add_assign(&mut self, rhs: Self) {
let s: Simdf32x4 = (*self).into();
let rhs: Simdf32x4 = rhs.into();
*self = (s + rhs).into();
}
}
impl_operator_simd!{
[Simdf32x4]; Sub<Quaternion<f32>> for Quaternion<f32> {
fn sub(lhs, rhs) -> Quaternion<f32> {
(lhs - rhs).into()
}
}
}
impl SubAssign for Quaternion<f32> {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
let s: Simdf32x4 = (*self).into();
let rhs: Simdf32x4 = rhs.into();
*self = (s - rhs).into();
}
}
impl_operator_simd!{
[Simdf32x4]; Mul<Quaternion<f32>> for Quaternion<f32> {
fn mul(lhs, rhs) -> Quaternion<f32> {
{
let p0 = Simdf32x4::splat(lhs.extract(0)) * rhs;
let p1 = Simdf32x4::splat(lhs.extract(1)) * Simdf32x4::new(
-rhs.extract(1), rhs.extract(0), -rhs.extract(3), rhs.extract(2)
);
let p2 = Simdf32x4::splat(lhs.extract(2)) * Simdf32x4::new(
-rhs.extract(2), rhs.extract(3), rhs.extract(0), -rhs.extract(1)
);
let p3 = Simdf32x4::splat(lhs.extract(3)) * Simdf32x4::new(
-rhs.extract(3), -rhs.extract(2), rhs.extract(1), rhs.extract(0)
);
(p0 + p1 + p2 + p3).into()
}
}
}
}