From c5aefbc138733d1d0022784f2ae27579826b8fb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Mei=C3=9Fner?= Date: Sun, 4 Apr 2021 16:31:23 +0200 Subject: [PATCH] Moves simd crate in root crate --- Cargo.toml | 9 ++-- codegen/Cargo.toml | 1 + codegen/src/rust.rs | 2 +- simd/Cargo.toml | 5 -- src/lib.rs | 85 +++++++++++++--------------------- simd/src/lib.rs => src/simd.rs | 21 ++++----- 6 files changed, 48 insertions(+), 75 deletions(-) delete mode 100644 simd/Cargo.toml rename simd/src/lib.rs => src/simd.rs (92%) diff --git a/Cargo.toml b/Cargo.toml index 6d86527..8f3a65c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,9 +2,8 @@ name = "geometric_algebra" version = "0.1.0" authors = ["Alexander Meißner "] +description = "Generate(d) custom libraries for geometric algebras" +repository = "https://github.com/Lichtso/geometric_algebra/" +keywords = ["math", "simd", "vector", "geometric-algebra", "geometry"] +license = "MIT" edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -simd = { path = "simd" } diff --git a/codegen/Cargo.toml b/codegen/Cargo.toml index 5b83ee0..cd6471f 100644 --- a/codegen/Cargo.toml +++ b/codegen/Cargo.toml @@ -3,3 +3,4 @@ name = "codegen" version = "0.1.0" authors = ["Alexander Meißner "] edition = "2018" +publish = false \ No newline at end of file diff --git a/codegen/src/rust.rs b/codegen/src/rust.rs index e97f8ed..6ee6b74 100644 --- a/codegen/src/rust.rs +++ b/codegen/src/rust.rs @@ -170,7 +170,7 @@ pub fn emit_code(collector: &mut W, ast_node: &AstNode, inden AstNode::None => {} AstNode::Preamble => { collector.write_all(b"#![allow(clippy::assign_op_pattern)]\n")?; - collector.write_all(b"use crate::*;\nuse std::ops::{Add, Neg, Sub, Mul, Div};\n\n")?; + collector.write_all(b"use crate::{*, simd::*};\nuse std::ops::{Add, Neg, Sub, Mul, Div};\n\n")?; } AstNode::ClassDefinition { class } => { collector.write_fmt(format_args!("#[derive(Clone, Copy)]\npub struct {} {{\n", class.class_name))?; diff --git a/simd/Cargo.toml b/simd/Cargo.toml deleted file mode 100644 index db81fcf..0000000 --- a/simd/Cargo.toml +++ /dev/null @@ -1,5 +0,0 @@ -[package] -name = "simd" -version = "0.1.0" -authors = ["Alexander Meißner "] -edition = "2018" diff --git a/src/lib.rs b/src/lib.rs index 15ffd16..e5e613c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ #![cfg_attr(all(target_arch = "wasm32", target_feature = "simd128"), feature(wasm_simd))] #![cfg_attr(all(any(target_arch = "arm", target_arch = "aarch64"), target_feature = "neon"), feature(stdsimd))] -pub use simd::*; +pub mod simd; pub mod complex; pub mod ppga2d; pub mod ppga3d; @@ -54,49 +54,6 @@ impl complex::MultiVector { } } -impl ppga2d::Rotor { - pub fn from_angle(mut angle: f32) -> Self { - angle *= 0.5; - Self { - g0: simd::Simd32x2::from([angle.cos(), angle.sin()]), - } - } - - pub fn angle(self) -> f32 { - self.g0.get_f(1).atan2(self.g0.get_f(0)) * 2.0 - } -} - -impl ppga2d::Point { - pub fn from_coordinates(coordinates: [f32; 2]) -> Self { - Self { - g0: simd::Simd32x3::from([1.0, coordinates[0], coordinates[1]]), - } - } - - pub fn from_direction(coordinates: [f32; 2]) -> Self { - Self { - g0: simd::Simd32x3::from([0.0, coordinates[0], coordinates[1]]), - } - } -} - -impl ppga2d::Plane { - pub fn from_normal_and_distance(normal: [f32; 2], distance: f32) -> Self { - Self { - g0: simd::Simd32x3::from([distance, normal[1], -normal[0]]), - } - } -} - -impl ppga2d::Translator { - pub fn from_coordinates(coordinates: [f32; 2]) -> Self { - Self { - g0: simd::Simd32x3::from([1.0, coordinates[1] * 0.5, coordinates[0] * -0.5]), - } - } -} - /// All elements set to `0.0` pub trait Zero { fn zero() -> Self; @@ -113,66 +70,86 @@ pub trait Dual { fn dual(self) -> Self::Output; } +/// Negates elements with `grade % 2 == 1` +/// +/// Also called main involution +pub trait Automorph { + type Output; + fn automorph(self) -> Self::Output; +} + +/// Negates elements with `grade % 4 >= 2` +/// /// Also called reversion pub trait Transpose { type Output; fn transpose(self) -> Self::Output; } -/// Also called involution -pub trait Automorph { - type Output; - fn automorph(self) -> Self::Output; -} - +/// Negates elements with `(grade + 3) % 4 < 2` pub trait Conjugate { type Output; fn conjugate(self) -> Self::Output; } +/// General multi vector multiplication pub trait GeometricProduct { type Output; fn geometric_product(self, other: T) -> Self::Output; } +/// Dual of the geometric product grade filtered by `t == r + s` +/// /// Also called join pub trait RegressiveProduct { type Output; fn regressive_product(self, other: T) -> Self::Output; } +/// Geometric product grade filtered by `t == r + s` +/// /// Also called meet or exterior product pub trait OuterProduct { type Output; fn outer_product(self, other: T) -> Self::Output; } +/// Geometric product grade filtered by `t == (r - s).abs()` +/// /// Also called fat dot product pub trait InnerProduct { type Output; fn inner_product(self, other: T) -> Self::Output; } +/// Geometric product grade filtered by `t == s - r` pub trait LeftContraction { type Output; fn left_contraction(self, other: T) -> Self::Output; } +/// Geometric product grade filtered by `t == r - s` pub trait RightContraction { type Output; fn right_contraction(self, other: T) -> Self::Output; } +/// Geometric product grade filtered by `t == 0` pub trait ScalarProduct { type Output; fn scalar_product(self, other: T) -> Self::Output; } +/// `self * other * self` +/// +/// Basically a sandwich product without an involution pub trait Reflection { type Output; fn reflection(self, other: T) -> Self::Output; } +/// `self * other * self.transpose()` +/// /// Also called sandwich product pub trait Transformation { type Output; @@ -185,19 +162,23 @@ pub trait SquaredMagnitude { fn squared_magnitude(self) -> Self::Output; } +/// Length as scalar +/// /// Also called amplitude, absolute value or norm pub trait Magnitude { type Output; fn magnitude(self) -> Self::Output; } -/// Also called normalize +/// Direction without magnitude (set to scalar `1.0`) +/// +/// Also called sign or normalize pub trait Signum { type Output; fn signum(self) -> Self::Output; } -/// Exponentiation by scalar negative one +/// Exponentiation by scalar `-1.0` pub trait Inverse { type Output; fn inverse(self) -> Self::Output; diff --git a/simd/src/lib.rs b/src/simd.rs similarity index 92% rename from simd/src/lib.rs rename to src/simd.rs index 8018e46..b27d445 100755 --- a/simd/src/lib.rs +++ b/src/simd.rs @@ -1,6 +1,3 @@ -#![cfg_attr(all(target_arch = "wasm32", target_feature = "simd128"), feature(wasm_simd))] -#![cfg_attr(all(any(target_arch = "arm", target_arch = "aarch64"), target_feature = "neon"), feature(stdsimd))] - #[cfg(target_arch = "aarch64")] pub use std::arch::aarch64::*; #[cfg(target_arch = "arm")] @@ -65,7 +62,7 @@ pub union Simd32x2 { macro_rules! match_architecture { ($Simd:ident, $native:tt, $fallback:tt,) => {{ #[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "arm", target_arch = "aarch64", target_arch = "wasm32"))] - unsafe { $Simd $native } + { $Simd $native } #[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "arm", target_arch = "aarch64", target_arch = "wasm32")))] unsafe { $Simd $fallback } }}; @@ -86,14 +83,14 @@ macro_rules! swizzle { ($self:expr, $x:literal, $y:literal, $z:literal, $w:literal) => { $crate::match_architecture!( Simd32x4, - { f128: $crate::_mm_permute_ps($self.f128, ($x as i32) | (($y as i32) << 2) | (($z as i32) << 4) | (($w as i32) << 6)) }, + { f128: $crate::simd::_mm_permute_ps($self.f128, ($x as i32) | (($y as i32) << 2) | (($z as i32) << 4) | (($w as i32) << 6)) }, { f32x4: [ $self.f32x4[$x], $self.f32x4[$y], $self.f32x4[$z], $self.f32x4[$w], ] }, - { v128: $crate::v32x4_shuffle::<$x, $y, $z, $w>($self.v128, $self.v128) }, + { v128: $crate::simd::v32x4_shuffle::<$x, $y, $z, $w>($self.v128, $self.v128) }, { f32x4: [ $self.f32x4[$x], $self.f32x4[$y], @@ -252,7 +249,7 @@ impl std::ops::Add for Simd32x3 { fn add(self, other: Self) -> Self { match_architecture!( Self, - { v32x4: self.v32x4 + other.v32x4 }, + { v32x4: unsafe { self.v32x4 + other.v32x4 } }, { f32x3: [ self.f32x3[0] + other.f32x3[0], self.f32x3[1] + other.f32x3[1], @@ -268,7 +265,7 @@ impl std::ops::Add for Simd32x2 { fn add(self, other: Self) -> Self { match_architecture!( Self, - { v32x4: self.v32x4 + other.v32x4 }, + { v32x4: unsafe { self.v32x4 + other.v32x4 } }, { f32x2: [ self.f32x2[0] + other.f32x2[0], self.f32x2[1] + other.f32x2[1], @@ -302,7 +299,7 @@ impl std::ops::Sub for Simd32x3 { fn sub(self, other: Self) -> Self { match_architecture!( Self, - { v32x4: self.v32x4 - other.v32x4 }, + { v32x4: unsafe { self.v32x4 - other.v32x4 } }, { f32x3: [ self.f32x3[0] - other.f32x3[0], self.f32x3[1] - other.f32x3[1], @@ -318,7 +315,7 @@ impl std::ops::Sub for Simd32x2 { fn sub(self, other: Self) -> Self { match_architecture!( Self, - { v32x4: self.v32x4 - other.v32x4 }, + { v32x4: unsafe { self.v32x4 - other.v32x4 } }, { f32x2: [ self.f32x2[0] - other.f32x2[0], self.f32x2[1] - other.f32x2[1], @@ -352,7 +349,7 @@ impl std::ops::Mul for Simd32x3 { fn mul(self, other: Self) -> Self { match_architecture!( Self, - { v32x4: self.v32x4 * other.v32x4 }, + { v32x4: unsafe { self.v32x4 * other.v32x4 } }, { f32x3: [ self.f32x3[0] * other.f32x3[0], self.f32x3[1] * other.f32x3[1], @@ -368,7 +365,7 @@ impl std::ops::Mul for Simd32x2 { fn mul(self, other: Self) -> Self { match_architecture!( Self, - { v32x4: self.v32x4 * other.v32x4 }, + { v32x4: unsafe { self.v32x4 * other.v32x4 } }, { f32x2: [ self.f32x2[0] * other.f32x2[0], self.f32x2[1] * other.f32x2[1],