From 06c7293e8391b3d61aabe9a1e438b7f03b474c6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sat, 14 Sep 2013 11:06:41 +0200 Subject: [PATCH] Specialized Mul, RMul, and LMul for Mat2 and Mat3. --- Makefile | 4 +-- src/lib.rs | 1 - src/mat.rs | 12 ++++---- src/mat_spec.rs | 78 +++++++++++++++++++++++++++++++++++++++++++++-- src/vec_macros.rs | 8 +++-- 5 files changed, 90 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 3e961e87..9c3d98cf 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ nalgebra_lib_path=lib nalgebra_doc_path=doc all: mkdir -p $(nalgebra_lib_path) - rust build src/lib.rs --out-dir $(nalgebra_lib_path) --opt-level 2 + rust build src/lib.rs --out-dir $(nalgebra_lib_path) --opt-level 3 test: mkdir -p $(nalgebra_lib_path) @@ -11,7 +11,7 @@ test: rm libtest~ bench: - rustc --test src/lib.rs --opt-level 2 -o bench~ && ./bench~ --bench + rustc --test src/lib.rs --opt-level 3 -o bench~ && ./bench~ --bench rm bench~ doc: diff --git a/src/lib.rs b/src/lib.rs index 8b36d633..ba3db564 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,6 @@ #[deny(non_uppercase_statics)]; #[deny(unnecessary_qualification)]; #[deny(missing_doc)]; -#[deny(warnings)]; extern mod std; extern mod extra; diff --git a/src/mat.rs b/src/mat.rs index 6ce49761..20b7449c 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -115,9 +115,9 @@ iterable_mut_impl!(Mat2, 2) dim_impl!(Mat2, 2) indexable_impl!(Mat2, 2) at_fast_impl!(Mat2, 2) -mul_impl!(Mat2, 2) -rmul_impl!(Mat2, Vec2, 2) -lmul_impl!(Mat2, Vec2, 2) +// (specialized) mul_impl!(Mat2, 2) +// (specialized) rmul_impl!(Mat2, Vec2, 2) +// (specialized) lmul_impl!(Mat2, Vec2, 2) transform_impl!(Mat2, Vec2) // (specialized) inv_impl!(Mat2, 2) transpose_impl!(Mat2, 2) @@ -172,9 +172,9 @@ iterable_mut_impl!(Mat3, 3) dim_impl!(Mat3, 3) indexable_impl!(Mat3, 3) at_fast_impl!(Mat3, 3) -mul_impl!(Mat3, 3) -rmul_impl!(Mat3, Vec3, 3) -lmul_impl!(Mat3, Vec3, 3) +// (specialized) mul_impl!(Mat3, 3) +// (specialized) rmul_impl!(Mat3, Vec3, 3) +// (specialized) lmul_impl!(Mat3, Vec3, 3) transform_impl!(Mat3, Vec3) // (specialized) inv_impl!(Mat3, 3) transpose_impl!(Mat3, 3) diff --git a/src/mat_spec.rs b/src/mat_spec.rs index 4de3f8cb..3887e69c 100644 --- a/src/mat_spec.rs +++ b/src/mat_spec.rs @@ -1,6 +1,6 @@ use std::num::{Zero, One}; -use vec::Vec3; -use mat::{Mat1, Mat2, Mat3, Inv, Row, Col}; +use vec::{Vec2, Vec3}; +use mat::{Mat1, Mat2, Mat3, Inv, Row, Col, RMul, LMul}; use mat; // some specializations: @@ -189,6 +189,80 @@ impl Col> for Mat3 { } } +impl + Add> Mul, Mat3> for Mat3 { + #[inline] + fn mul(&self, other: &Mat3) -> Mat3 { + Mat3::new( + self.m11 * other.m11 + self.m12 * other.m21 + self.m13 * other.m31, + self.m11 * other.m12 + self.m12 * other.m22 + self.m13 * other.m32, + self.m11 * other.m13 + self.m12 * other.m23 + self.m13 * other.m33, + + self.m21 * other.m11 + self.m22 * other.m21 + self.m23 * other.m31, + self.m21 * other.m12 + self.m22 * other.m22 + self.m23 * other.m32, + self.m21 * other.m13 + self.m22 * other.m23 + self.m23 * other.m33, + + self.m31 * other.m11 + self.m32 * other.m21 + self.m33 * other.m31, + self.m31 * other.m12 + self.m32 * other.m22 + self.m33 * other.m32, + self.m31 * other.m13 + self.m32 * other.m23 + self.m33 * other.m33 + ) + } +} + +impl + Add> Mul, Mat2> for Mat2 { + #[inline(always)] + fn mul(&self, other: &Mat2) -> Mat2 { + Mat2::new( + self.m11 * other.m11 + self.m12 * other.m21, + self.m11 * other.m12 + self.m12 * other.m22, + + self.m21 * other.m11 + self.m22 * other.m21, + self.m21 * other.m12 + self.m22 * other.m22 + ) + } +} + +impl + Add> RMul> for Mat3 { + #[inline(always)] + fn rmul(&self, v: &Vec3) -> Vec3 { + Vec3::new( + self.m11 * v.x + self.m12 * v.y + self.m13 * v.z, + self.m21 * v.x + self.m22 * v.y + self.m23 * v.z, + self.m31 * v.x + self.m32 * v.y + self.m33 * v.z + ) + } +} + +impl + Add> LMul> for Mat3 { + #[inline(always)] + fn lmul(&self, v: &Vec3) -> Vec3 { + Vec3::new( + self.m11 * v.x + self.m21 * v.y + self.m31 * v.z, + self.m12 * v.x + self.m22 * v.y + self.m32 * v.z, + self.m13 * v.x + self.m23 * v.y + self.m33 * v.z + ) + } +} + +impl + Add> RMul> for Mat2 { + #[inline(always)] + fn rmul(&self, v: &Vec2) -> Vec2 { + Vec2::new( + self.m11 * v.x + self.m12 * v.y, + self.m21 * v.x + self.m22 * v.y + ) + } +} + +impl + Add> LMul> for Mat2 { + #[inline(always)] + fn lmul(&self, v: &Vec2) -> Vec2 { + Vec2::new( + self.m11 * v.x + self.m21 * v.y, + self.m12 * v.x + self.m22 * v.y + ) + } +} + // FIXME: move this to another file? impl mat::Mat4 { /// Computes a projection matrix given the frustrum near plane width, height, the field of diff --git a/src/vec_macros.rs b/src/vec_macros.rs index 78acbdd0..6f892deb 100644 --- a/src/vec_macros.rs +++ b/src/vec_macros.rs @@ -219,7 +219,9 @@ macro_rules! basis_impl( for i in range(0u, $dim) { let mut basis_element : $t = Zero::zero(); - basis_element.set(i, One::one()); + unsafe { + basis_element.set_fast(i, One::one()); + } if !f(basis_element) { return } } @@ -234,7 +236,9 @@ macro_rules! basis_impl( for i in range(0u, $dim) { let mut basis_element : $t = Zero::zero(); - basis_element.set(i, One::one()); + unsafe { + basis_element.set_fast(i, One::one()); + } if basis.len() == $dim - 1 { break;