Moves simd crate in root crate
This commit is contained in:
parent
dda229dd6d
commit
c5aefbc138
6 changed files with 48 additions and 75 deletions
|
|
@ -2,9 +2,8 @@
|
||||||
name = "geometric_algebra"
|
name = "geometric_algebra"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Alexander Meißner <AlexanderMeissner@gmx.net>"]
|
authors = ["Alexander Meißner <AlexanderMeissner@gmx.net>"]
|
||||||
|
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"
|
edition = "2018"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
simd = { path = "simd" }
|
|
||||||
|
|
|
||||||
|
|
@ -3,3 +3,4 @@ name = "codegen"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Alexander Meißner <AlexanderMeissner@gmx.net>"]
|
authors = ["Alexander Meißner <AlexanderMeissner@gmx.net>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
publish = false
|
||||||
|
|
@ -170,7 +170,7 @@ pub fn emit_code<W: std::io::Write>(collector: &mut W, ast_node: &AstNode, inden
|
||||||
AstNode::None => {}
|
AstNode::None => {}
|
||||||
AstNode::Preamble => {
|
AstNode::Preamble => {
|
||||||
collector.write_all(b"#![allow(clippy::assign_op_pattern)]\n")?;
|
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 } => {
|
AstNode::ClassDefinition { class } => {
|
||||||
collector.write_fmt(format_args!("#[derive(Clone, Copy)]\npub struct {} {{\n", class.class_name))?;
|
collector.write_fmt(format_args!("#[derive(Clone, Copy)]\npub struct {} {{\n", class.class_name))?;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "simd"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["Alexander Meißner <AlexanderMeissner@gmx.net>"]
|
|
||||||
edition = "2018"
|
|
||||||
85
src/lib.rs
85
src/lib.rs
|
|
@ -1,7 +1,7 @@
|
||||||
#![cfg_attr(all(target_arch = "wasm32", target_feature = "simd128"), feature(wasm_simd))]
|
#![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_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 complex;
|
||||||
pub mod ppga2d;
|
pub mod ppga2d;
|
||||||
pub mod ppga3d;
|
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`
|
/// All elements set to `0.0`
|
||||||
pub trait Zero {
|
pub trait Zero {
|
||||||
fn zero() -> Self;
|
fn zero() -> Self;
|
||||||
|
|
@ -113,66 +70,86 @@ pub trait Dual {
|
||||||
fn dual(self) -> Self::Output;
|
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
|
/// Also called reversion
|
||||||
pub trait Transpose {
|
pub trait Transpose {
|
||||||
type Output;
|
type Output;
|
||||||
fn transpose(self) -> Self::Output;
|
fn transpose(self) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Also called involution
|
/// Negates elements with `(grade + 3) % 4 < 2`
|
||||||
pub trait Automorph {
|
|
||||||
type Output;
|
|
||||||
fn automorph(self) -> Self::Output;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Conjugate {
|
pub trait Conjugate {
|
||||||
type Output;
|
type Output;
|
||||||
fn conjugate(self) -> Self::Output;
|
fn conjugate(self) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// General multi vector multiplication
|
||||||
pub trait GeometricProduct<T> {
|
pub trait GeometricProduct<T> {
|
||||||
type Output;
|
type Output;
|
||||||
fn geometric_product(self, other: T) -> Self::Output;
|
fn geometric_product(self, other: T) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Dual of the geometric product grade filtered by `t == r + s`
|
||||||
|
///
|
||||||
/// Also called join
|
/// Also called join
|
||||||
pub trait RegressiveProduct<T> {
|
pub trait RegressiveProduct<T> {
|
||||||
type Output;
|
type Output;
|
||||||
fn regressive_product(self, other: T) -> Self::Output;
|
fn regressive_product(self, other: T) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Geometric product grade filtered by `t == r + s`
|
||||||
|
///
|
||||||
/// Also called meet or exterior product
|
/// Also called meet or exterior product
|
||||||
pub trait OuterProduct<T> {
|
pub trait OuterProduct<T> {
|
||||||
type Output;
|
type Output;
|
||||||
fn outer_product(self, other: T) -> Self::Output;
|
fn outer_product(self, other: T) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Geometric product grade filtered by `t == (r - s).abs()`
|
||||||
|
///
|
||||||
/// Also called fat dot product
|
/// Also called fat dot product
|
||||||
pub trait InnerProduct<T> {
|
pub trait InnerProduct<T> {
|
||||||
type Output;
|
type Output;
|
||||||
fn inner_product(self, other: T) -> Self::Output;
|
fn inner_product(self, other: T) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Geometric product grade filtered by `t == s - r`
|
||||||
pub trait LeftContraction<T> {
|
pub trait LeftContraction<T> {
|
||||||
type Output;
|
type Output;
|
||||||
fn left_contraction(self, other: T) -> Self::Output;
|
fn left_contraction(self, other: T) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Geometric product grade filtered by `t == r - s`
|
||||||
pub trait RightContraction<T> {
|
pub trait RightContraction<T> {
|
||||||
type Output;
|
type Output;
|
||||||
fn right_contraction(self, other: T) -> Self::Output;
|
fn right_contraction(self, other: T) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Geometric product grade filtered by `t == 0`
|
||||||
pub trait ScalarProduct<T> {
|
pub trait ScalarProduct<T> {
|
||||||
type Output;
|
type Output;
|
||||||
fn scalar_product(self, other: T) -> Self::Output;
|
fn scalar_product(self, other: T) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `self * other * self`
|
||||||
|
///
|
||||||
|
/// Basically a sandwich product without an involution
|
||||||
pub trait Reflection<T> {
|
pub trait Reflection<T> {
|
||||||
type Output;
|
type Output;
|
||||||
fn reflection(self, other: T) -> Self::Output;
|
fn reflection(self, other: T) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `self * other * self.transpose()`
|
||||||
|
///
|
||||||
/// Also called sandwich product
|
/// Also called sandwich product
|
||||||
pub trait Transformation<T> {
|
pub trait Transformation<T> {
|
||||||
type Output;
|
type Output;
|
||||||
|
|
@ -185,19 +162,23 @@ pub trait SquaredMagnitude {
|
||||||
fn squared_magnitude(self) -> Self::Output;
|
fn squared_magnitude(self) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Length as scalar
|
||||||
|
///
|
||||||
/// Also called amplitude, absolute value or norm
|
/// Also called amplitude, absolute value or norm
|
||||||
pub trait Magnitude {
|
pub trait Magnitude {
|
||||||
type Output;
|
type Output;
|
||||||
fn magnitude(self) -> Self::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 {
|
pub trait Signum {
|
||||||
type Output;
|
type Output;
|
||||||
fn signum(self) -> Self::Output;
|
fn signum(self) -> Self::Output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Exponentiation by scalar negative one
|
/// Exponentiation by scalar `-1.0`
|
||||||
pub trait Inverse {
|
pub trait Inverse {
|
||||||
type Output;
|
type Output;
|
||||||
fn inverse(self) -> Self::Output;
|
fn inverse(self) -> Self::Output;
|
||||||
|
|
|
||||||
|
|
@ -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")]
|
#[cfg(target_arch = "aarch64")]
|
||||||
pub use std::arch::aarch64::*;
|
pub use std::arch::aarch64::*;
|
||||||
#[cfg(target_arch = "arm")]
|
#[cfg(target_arch = "arm")]
|
||||||
|
|
@ -65,7 +62,7 @@ pub union Simd32x2 {
|
||||||
macro_rules! match_architecture {
|
macro_rules! match_architecture {
|
||||||
($Simd:ident, $native:tt, $fallback:tt,) => {{
|
($Simd:ident, $native:tt, $fallback:tt,) => {{
|
||||||
#[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "arm", target_arch = "aarch64", target_arch = "wasm32"))]
|
#[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")))]
|
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "arm", target_arch = "aarch64", target_arch = "wasm32")))]
|
||||||
unsafe { $Simd $fallback }
|
unsafe { $Simd $fallback }
|
||||||
}};
|
}};
|
||||||
|
|
@ -86,14 +83,14 @@ macro_rules! swizzle {
|
||||||
($self:expr, $x:literal, $y:literal, $z:literal, $w:literal) => {
|
($self:expr, $x:literal, $y:literal, $z:literal, $w:literal) => {
|
||||||
$crate::match_architecture!(
|
$crate::match_architecture!(
|
||||||
Simd32x4,
|
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: [
|
{ f32x4: [
|
||||||
$self.f32x4[$x],
|
$self.f32x4[$x],
|
||||||
$self.f32x4[$y],
|
$self.f32x4[$y],
|
||||||
$self.f32x4[$z],
|
$self.f32x4[$z],
|
||||||
$self.f32x4[$w],
|
$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: [
|
{ f32x4: [
|
||||||
$self.f32x4[$x],
|
$self.f32x4[$x],
|
||||||
$self.f32x4[$y],
|
$self.f32x4[$y],
|
||||||
|
|
@ -252,7 +249,7 @@ impl std::ops::Add<Simd32x3> for Simd32x3 {
|
||||||
fn add(self, other: Self) -> Self {
|
fn add(self, other: Self) -> Self {
|
||||||
match_architecture!(
|
match_architecture!(
|
||||||
Self,
|
Self,
|
||||||
{ v32x4: self.v32x4 + other.v32x4 },
|
{ v32x4: unsafe { self.v32x4 + other.v32x4 } },
|
||||||
{ f32x3: [
|
{ f32x3: [
|
||||||
self.f32x3[0] + other.f32x3[0],
|
self.f32x3[0] + other.f32x3[0],
|
||||||
self.f32x3[1] + other.f32x3[1],
|
self.f32x3[1] + other.f32x3[1],
|
||||||
|
|
@ -268,7 +265,7 @@ impl std::ops::Add<Simd32x2> for Simd32x2 {
|
||||||
fn add(self, other: Self) -> Self {
|
fn add(self, other: Self) -> Self {
|
||||||
match_architecture!(
|
match_architecture!(
|
||||||
Self,
|
Self,
|
||||||
{ v32x4: self.v32x4 + other.v32x4 },
|
{ v32x4: unsafe { self.v32x4 + other.v32x4 } },
|
||||||
{ f32x2: [
|
{ f32x2: [
|
||||||
self.f32x2[0] + other.f32x2[0],
|
self.f32x2[0] + other.f32x2[0],
|
||||||
self.f32x2[1] + other.f32x2[1],
|
self.f32x2[1] + other.f32x2[1],
|
||||||
|
|
@ -302,7 +299,7 @@ impl std::ops::Sub<Simd32x3> for Simd32x3 {
|
||||||
fn sub(self, other: Self) -> Self {
|
fn sub(self, other: Self) -> Self {
|
||||||
match_architecture!(
|
match_architecture!(
|
||||||
Self,
|
Self,
|
||||||
{ v32x4: self.v32x4 - other.v32x4 },
|
{ v32x4: unsafe { self.v32x4 - other.v32x4 } },
|
||||||
{ f32x3: [
|
{ f32x3: [
|
||||||
self.f32x3[0] - other.f32x3[0],
|
self.f32x3[0] - other.f32x3[0],
|
||||||
self.f32x3[1] - other.f32x3[1],
|
self.f32x3[1] - other.f32x3[1],
|
||||||
|
|
@ -318,7 +315,7 @@ impl std::ops::Sub<Simd32x2> for Simd32x2 {
|
||||||
fn sub(self, other: Self) -> Self {
|
fn sub(self, other: Self) -> Self {
|
||||||
match_architecture!(
|
match_architecture!(
|
||||||
Self,
|
Self,
|
||||||
{ v32x4: self.v32x4 - other.v32x4 },
|
{ v32x4: unsafe { self.v32x4 - other.v32x4 } },
|
||||||
{ f32x2: [
|
{ f32x2: [
|
||||||
self.f32x2[0] - other.f32x2[0],
|
self.f32x2[0] - other.f32x2[0],
|
||||||
self.f32x2[1] - other.f32x2[1],
|
self.f32x2[1] - other.f32x2[1],
|
||||||
|
|
@ -352,7 +349,7 @@ impl std::ops::Mul<Simd32x3> for Simd32x3 {
|
||||||
fn mul(self, other: Self) -> Self {
|
fn mul(self, other: Self) -> Self {
|
||||||
match_architecture!(
|
match_architecture!(
|
||||||
Self,
|
Self,
|
||||||
{ v32x4: self.v32x4 * other.v32x4 },
|
{ v32x4: unsafe { self.v32x4 * other.v32x4 } },
|
||||||
{ f32x3: [
|
{ f32x3: [
|
||||||
self.f32x3[0] * other.f32x3[0],
|
self.f32x3[0] * other.f32x3[0],
|
||||||
self.f32x3[1] * other.f32x3[1],
|
self.f32x3[1] * other.f32x3[1],
|
||||||
|
|
@ -368,7 +365,7 @@ impl std::ops::Mul<Simd32x2> for Simd32x2 {
|
||||||
fn mul(self, other: Self) -> Self {
|
fn mul(self, other: Self) -> Self {
|
||||||
match_architecture!(
|
match_architecture!(
|
||||||
Self,
|
Self,
|
||||||
{ v32x4: self.v32x4 * other.v32x4 },
|
{ v32x4: unsafe { self.v32x4 * other.v32x4 } },
|
||||||
{ f32x2: [
|
{ f32x2: [
|
||||||
self.f32x2[0] * other.f32x2[0],
|
self.f32x2[0] * other.f32x2[0],
|
||||||
self.f32x2[1] * other.f32x2[1],
|
self.f32x2[1] * other.f32x2[1],
|
||||||
Loading…
Add table
Reference in a new issue