Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
rust source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
mrustc (master)
rustc 1.0.0
rustc 1.1.0
rustc 1.10.0
rustc 1.11.0
rustc 1.12.0
rustc 1.13.0
rustc 1.14.0
rustc 1.15.1
rustc 1.16.0
rustc 1.17.0
rustc 1.18.0
rustc 1.19.0
rustc 1.2.0
rustc 1.20.0
rustc 1.21.0
rustc 1.22.0
rustc 1.23.0
rustc 1.24.0
rustc 1.25.0
rustc 1.26.0
rustc 1.27.0
rustc 1.27.1
rustc 1.28.0
rustc 1.29.0
rustc 1.3.0
rustc 1.30.0
rustc 1.31.0
rustc 1.32.0
rustc 1.33.0
rustc 1.34.0
rustc 1.35.0
rustc 1.36.0
rustc 1.37.0
rustc 1.38.0
rustc 1.39.0
rustc 1.4.0
rustc 1.40.0
rustc 1.41.0
rustc 1.42.0
rustc 1.43.0
rustc 1.44.0
rustc 1.45.0
rustc 1.45.2
rustc 1.46.0
rustc 1.47.0
rustc 1.48.0
rustc 1.49.0
rustc 1.5.0
rustc 1.50.0
rustc 1.51.0
rustc 1.52.0
rustc 1.53.0
rustc 1.54.0
rustc 1.55.0
rustc 1.56.0
rustc 1.57.0
rustc 1.58.0
rustc 1.59.0
rustc 1.6.0
rustc 1.60.0
rustc 1.61.0
rustc 1.62.0
rustc 1.63.0
rustc 1.64.0
rustc 1.65.0
rustc 1.66.0
rustc 1.67.0
rustc 1.68.0
rustc 1.69.0
rustc 1.7.0
rustc 1.70.0
rustc 1.71.0
rustc 1.72.0
rustc 1.73.0
rustc 1.74.0
rustc 1.75.0
rustc 1.76.0
rustc 1.77.0
rustc 1.78.0
rustc 1.79.0
rustc 1.8.0
rustc 1.80.0
rustc 1.81.0
rustc 1.82.0
rustc 1.83.0
rustc 1.84.0
rustc 1.85.0
rustc 1.86.0
rustc 1.87.0
rustc 1.88.0
rustc 1.89.0
rustc 1.9.0
rustc beta
rustc nightly
rustc-cg-gcc (master)
x86-64 GCCRS (GCC master)
x86-64 GCCRS (GCCRS master)
x86-64 GCCRS 14.1 (GCC assertions)
x86-64 GCCRS 14.1 (GCC)
x86-64 GCCRS 14.2 (GCC assertions)
x86-64 GCCRS 14.2 (GCC)
x86-64 GCCRS 14.3 (GCC assertions)
x86-64 GCCRS 14.3 (GCC)
x86-64 GCCRS 15.1 (GCC assertions)
x86-64 GCCRS 15.1 (GCC)
x86-64 GCCRS 15.2 (GCC assertions)
x86-64 GCCRS 15.2 (GCC)
Options
Source code
#![feature(adt_const_params, generic_const_exprs, core_intrinsics)] use std::marker::ConstParamTy; use std::ops::{Mul, BitAnd, BitXor}; #[derive(Copy, Clone)] pub struct MVec<const BASIS: &'static [Basis]> where [(); BASIS.len()]: { components: [f32; BASIS.len()], } #[derive(ConstParamTy, PartialEq, Eq, Copy, Clone)] pub struct Basis { p: u32, n: u32, z: u32, } enum Sign { Zero, Pos, Neg, } const S : Basis = Basis{p: 0, n: 0, z: 0}; pub type Scalar = MVec<{&[S]}>; const E0 : Basis = Basis{p: 0, n: 0, z: 1}; const E1 : Basis = Basis{p: 0b0010, n: 0, z: 0}; const E2 : Basis = Basis{p: 0b0100, n: 0, z: 0}; const E3 : Basis = Basis{p: 0b1000, n: 0, z: 0}; const E12 : Basis = Basis{p: 0b0110, n: 0, z: 0}; const E13 : Basis = Basis{p: 0b1010, n: 0, z: 0}; const E23 : Basis = Basis{p: 0b1100, n: 0, z: 0}; const E01 : Basis = Basis{p: 0b0010, n: 0, z: 1}; const E02 : Basis = Basis{p: 0b0100, n: 0, z: 1}; const E03 : Basis = Basis{p: 0b1000, n: 0, z: 1}; const E012 : Basis = Basis{p: 0b0110, n: 0, z: 1}; const E013 : Basis = Basis{p: 0b1010, n: 0, z: 1}; const E023 : Basis = Basis{p: 0b1100, n: 0, z: 1}; const E123 : Basis = Basis{p: 0b1110, n: 0, z: 0}; const E0123 : Basis = Basis{p: 0b1110, n: 0, z: 1}; const PGA3_BASIS: &'static [Basis] = &[ S, E0, E1, E2, E3, E12, E13, E23, E01, E02, E03, E012, E013, E023, E123, E0123 ]; impl Basis { #[inline(always)] const fn flips(self: Basis, other: Basis) -> Sign { let a = self.as_u128(); let b = other.as_u128(); #[inline(always)] const fn compute_swaps(a: u128, b: u128) -> usize { if a == 0 { 0 } else { (a & b).count_ones() as usize + compute_swaps(a >> 1, b) } } let swaps = compute_swaps(a >> 1, b); if (swaps + (self.n & other.n).count_ones() as usize) % 2 == 0 { Sign::Pos } else { Sign::Neg } } #[inline(always)] const fn flips_mul(self: Basis, other: Basis) -> Sign { if self.z & other.z != 0 { return Sign::Zero; } else { self.flips(other) } } #[inline(always)] const fn flips_outer(self: Basis, other: Basis) -> Sign { if self.p & other.p != 0 || self.n & other.n != 0 || self.z & other.z != 0 { return Sign::Zero; } else { self.flips(other) } } #[inline(always)] fn flips_inner(self: Basis, other: Basis) -> Sign { let pss = self.as_u128() | other.as_u128(); if (pss == other.as_u128() || pss == self.as_u128()) && (self.z & other.z == 0) { self.flips(other) } else { Sign::Zero } } #[inline(always)] const fn as_u128(self) -> u128 { (self.p as u128) | (self.n as u128) << 32 | (self.z as u128) << 64 } #[inline(always)] const fn mul(self, other: Self) -> Self { Self { p: self.p ^ other.p, n: self.n ^ other.n, z: self.z ^ other.z, } } #[inline(always)] const fn or(self, other: Self) -> Self { Self { p: self.p | other.p, n: self.n | other.n, z: self.z | other.z, } } } impl<const BASIS: &'static [Basis]> MVec<BASIS> where [(); BASIS.len()]: { pub fn reverse(mut self) -> Self { for (b, x) in BASIS.iter().zip(self.components.iter_mut()) { if let Some(total) = b.as_u128().count_ones().checked_sub(1) { // Triangular number formula if ((total + 1) * total) / 2 % 2 != 0 { *x = -*x } } } self } const fn pss<const A_BASIS: &'static [Basis]>() -> Basis { let mut acc = S; let mut i = 0; while i < BASIS.len() { acc = acc.or(BASIS[i]); i += 1; } let mut i = 0; while i < A_BASIS.len() { acc = acc.or(A_BASIS[i]); i += 1; } acc } pub fn dual<const OUT_BASIS: &'static [Basis]>(self) -> MVec<OUT_BASIS> where [(); OUT_BASIS.len()]: { // Workaround for inability to generate the OUT_BASIS at compile-time assert_eq!(BASIS.len(), OUT_BASIS.len()); let pss = Self::pss::<OUT_BASIS>(); let mut out = [0.0; OUT_BASIS.len()]; for (&x_b, x) in BASIS.iter().zip(self.components) { let out_b = x_b.mul(pss); let (_, cell) = OUT_BASIS.iter().zip(out.iter_mut()).find(|&(&b, _)| b == out_b).unwrap(); *cell = match x_b.flips(pss) { Sign::Neg => -x, Sign::Pos => x, Sign::Zero => unreachable!(), }; } MVec { components: out } } pub fn undual<const PSS: Basis, const OUT_BASIS: &'static [Basis]>(self) -> MVec<OUT_BASIS> where [(); OUT_BASIS.len()]: { // Workaround for inability to generate the OUT_BASIS at compile-time assert_eq!(BASIS.len(), OUT_BASIS.len()); let pss = Self::pss::<OUT_BASIS>(); let mut out = [0.0; OUT_BASIS.len()]; for (&x_b, x) in BASIS.iter().zip(self.components) { let out_b = x_b.mul(pss); let (_, cell) = OUT_BASIS.iter().zip(out.iter_mut()).find(|&(&b, _)| b == out_b).unwrap(); *cell = match pss.flips(x_b) { Sign::Neg => -x, Sign::Pos => x, Sign::Zero => unreachable!(), }; } MVec { components: out } } //#[inline(always)] pub fn from_prod<const X_BASIS: &'static [Basis], const Y_BASIS: &'static [Basis]>(x: MVec<X_BASIS>, y: MVec<Y_BASIS>) -> Self where [(); X_BASIS.len()]:, [(); Y_BASIS.len()]:, { use std::intrinsics::{fadd_fast, fsub_fast, fmul_fast}; let mut out = [0.0; BASIS.len()]; for (&x_b, x) in X_BASIS.iter().zip(x.components) { for (&y_b, y) in Y_BASIS.iter().zip(y.components) { let out_b = x_b.mul(y_b); if let Some((_, cell)) = BASIS.iter().zip(out.iter_mut()).find(|&(&b, _)| b == out_b) { match x_b.flips_mul(y_b) { Sign::Pos => *cell = unsafe { fadd_fast(*cell, fmul_fast(x, y)) }, Sign::Neg => *cell = unsafe { fsub_fast(*cell, fmul_fast(x, y)) }, Sign::Zero => (), } } } } Self { components: out } } pub fn from_outer_prod<const X_BASIS: &'static [Basis], const Y_BASIS: &'static [Basis]>(x: MVec<X_BASIS>, y: MVec<Y_BASIS>) -> Self where [(); X_BASIS.len()]:, [(); Y_BASIS.len()]:, { use std::intrinsics::{fadd_fast, fsub_fast, fmul_fast}; let mut out = [0.0; BASIS.len()]; for (&x_b, x) in X_BASIS.iter().zip(x.components) { for (&y_b, y) in Y_BASIS.iter().zip(y.components) { let out_b = x_b.mul(y_b); if let Some((_, cell)) = BASIS.iter().zip(out.iter_mut()).find(|&(&b, _)| b == out_b) { match x_b.flips_outer(y_b) { Sign::Pos => *cell = unsafe { fadd_fast(*cell, fmul_fast(x, y)) }, Sign::Neg => *cell = unsafe { fsub_fast(*cell, fmul_fast(x, y)) }, Sign::Zero => (), } } } } Self { components: out } } pub fn from_inner_prod<const X_BASIS: &'static [Basis], const Y_BASIS: &'static [Basis]>(x: MVec<X_BASIS>, y: MVec<Y_BASIS>) -> Self where [(); X_BASIS.len()]:, [(); Y_BASIS.len()]:, { use std::intrinsics::{fadd_fast, fsub_fast, fmul_fast}; let mut out = [0.0; BASIS.len()]; for (&x_b, x) in X_BASIS.iter().zip(x.components) { for (&y_b, y) in Y_BASIS.iter().zip(y.components) { let out_b = x_b.mul(y_b); if let Some((_, cell)) = BASIS.iter().zip(out.iter_mut()).find(|&(&b, _)| b == out_b) { match x_b.flips_inner(y_b) { Sign::Pos => *cell = unsafe { fadd_fast(*cell, fmul_fast(x, y)) }, Sign::Neg => *cell = unsafe { fsub_fast(*cell, fmul_fast(x, y)) }, Sign::Zero => (), } } } } Self { components: out } } } macro_rules! impl_mul{ ($x:ty, $y:ty = $out:ty) => { impl Mul<$y> for $x { type Output = $out; #[inline] fn mul(self, rhs: $y) -> Self::Output { MVec::from_prod(self, rhs) } } } } macro_rules! impl_outer{ ($x:ty, $y:ty = $out:ty) => { impl BitAnd<$y> for $x { type Output = $out; fn bitand(self, rhs: $y) -> Self::Output { MVec::from_outer_prod(self, rhs) } } } } macro_rules! impl_inner{ ($x:ty, $y:ty = $out:ty) => { impl BitXor<$y> for $x { type Output = $out; fn bitxor(self, rhs: $y) -> Self::Output { MVec::from_inner_prod(self, rhs) } } } } impl_mul!(MVec<{ &[E1, E2] }>, MVec<{ &[E1, E2] }> = MVec<{ &[S, E12] }>); impl_mul!(MVec<{ &[E1, E2, E3] }>, MVec<{ &[E1, E2, E3] }> = MVec<{ &[S, E12, E13, E23] }>); impl_outer!(MVec<{ &[E1, E2] }>, MVec<{ &[E1, E2] }> = MVec<{ &[E12] }>); impl_outer!(MVec<{ &[E1, E2, E3] }>, MVec<{ &[E1, E2, E3] }> = MVec<{ &[E12, E13, E23] }>); impl_inner!(MVec<{ &[E1, E2, E3] }>, MVec<{ &[E12, E13, E23] }> = MVec<{ &[E1, E2, E3] }>); impl_inner!(MVec<{ &[E1, E2, E3] }>, MVec<{ &[E1, E2, E3] }> = MVec<{ &[S] }>); pub type Motor = MVec<{ &[S, E12, E13, E23, E01, E02, E03, E0123] }>; pub type Point = MVec<{ &[E123, E012, E013, E023] }>; pub type Flector = MVec<{ &[E1, E2, E3, E0, E123, E012, E013, E023] }>; impl_mul!(Motor, Point = Flector); impl_mul!(Flector, Motor = Flector); pub extern "Rust" fn mulc(m: Motor, p: Point) -> Point { Point::from_prod(m * p * m.reverse(), Scalar{ components: [1.0]}) }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree