Skip to content

Commit

Permalink
Expose names fields for non SIMD vectors and use Deref to emulate nam…
Browse files Browse the repository at this point in the history
…ed fields for SIMD vectors (#94)

* Change `Vec2` and `Vec3` to use named public fields (e.g. `.x`, `.y`)
* Implement `Deref` and `DerefMut` for `Vec4`, `Vec3A` and `Quat` to emulate named fields

For now the old methods are still there, but they will be deprecated.

Fixes #89.
  • Loading branch information
bitshifter committed Nov 13, 2020
1 parent 5f1f2d8 commit 44d3208
Show file tree
Hide file tree
Showing 29 changed files with 3,113 additions and 1,052 deletions.
13 changes: 13 additions & 0 deletions benches/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ fn vec3a_to_rgb_op(v: Vec3A) -> u32 {
((red as u32) << 16 | (green as u32) << 8 | (blue as u32)).into()
}

#[inline]
fn vec3a_deref(v: Vec3A) -> [f32; 3] {
[v.x, v.y, v.z]
}

#[inline]
fn vec3a_accessors(v: Vec3A) -> [f32; 3] {
[v.x(), v.y(), v.z()]
Expand Down Expand Up @@ -64,6 +69,13 @@ op => vec3a_to_rgb_op,
from => random_vec3a
);

bench_func!(
vec3a_to_array_deref,
"vec3a into array deref",
op => vec3a_deref,
from => random_vec3a
);

bench_func!(
vec3a_to_array_accessors,
"vec3a into array slow",
Expand Down Expand Up @@ -103,6 +115,7 @@ criterion_group!(
vec3a_euler,
vec3a_to_rgb,
vec3a_to_array_accessors,
vec3a_to_array_deref,
vec3a_to_array_into,
vec3a_to_tuple_into,
vec3a_to_vec3,
Expand Down
5 changes: 5 additions & 0 deletions build_all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

cargo test --features "bytemuck mint rand serde debug-glam-assert transform-types" && \
cargo test --features "scalar-math bytemuck mint rand serde debug-glam-assert transform-types" && \
cargo test --no-default-features --features "libm scalar-math bytemuck mint rand serde debug-glam-assert transform-types"
8 changes: 4 additions & 4 deletions src/f32/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,10 @@ fn test_sse2_m128_sin() {
let v = unsafe { Vec4(sse2::m128_sin(v.0)) };
let a_sin = a.sin();
// dbg!((a, a_sin, v));
assert_approx_eq!(v.x(), a_sin, 1e-6);
assert_approx_eq!(v.z(), a_sin, 1e-6);
assert_approx_eq!(v.y(), a_sin, 1e-6);
assert_approx_eq!(v.w(), a_sin, 1e-6);
assert_approx_eq!(v.x, a_sin, 1e-6);
assert_approx_eq!(v.z, a_sin, 1e-6);
assert_approx_eq!(v.y, a_sin, 1e-6);
assert_approx_eq!(v.w, a_sin, 1e-6);
}

let mut a = -PI;
Expand Down
2 changes: 1 addition & 1 deletion src/f32/mat2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,4 @@ impl<'a> Product<&'a Self> for Mat2 {
{
iter.fold(IDENTITY, |a, &b| Self::mul(a, b))
}
}
}
20 changes: 16 additions & 4 deletions src/f32/mat3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,21 @@ impl Mat3 {
// #[cfg(vec3a_f32)]
{
Self {
x_axis: Vec3::new(self.x_axis.0, self.y_axis.0, self.z_axis.0),
y_axis: Vec3::new(self.x_axis.1, self.y_axis.1, self.z_axis.1),
z_axis: Vec3::new(self.x_axis.2, self.y_axis.2, self.z_axis.2),
x_axis: Vec3 {
x: self.x_axis.x,
y: self.y_axis.x,
z: self.z_axis.x,
},
y_axis: Vec3 {
x: self.x_axis.y,
y: self.y_axis.y,
z: self.z_axis.y,
},
z_axis: Vec3 {
x: self.x_axis.z,
y: self.y_axis.z,
z: self.z_axis.z,
},
}
}
}
Expand Down Expand Up @@ -542,4 +554,4 @@ impl<'a> Product<&'a Self> for Mat3 {
{
iter.fold(IDENTITY, |a, &b| Self::mul(a, b))
}
}
}
12 changes: 6 additions & 6 deletions src/f32/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,14 +715,14 @@ impl Mat4 {
};

let col0 = Vec4::new(
inverse.x_axis.x(),
inverse.y_axis.x(),
inverse.z_axis.x(),
inverse.w_axis.x(),
inverse.x_axis.x,
inverse.y_axis.x,
inverse.z_axis.x,
inverse.w_axis.x,
);

let dot0 = self.x_axis * col0;
let dot1 = dot0.x() + dot0.y() + dot0.z() + dot0.w();
let dot1 = dot0.x + dot0.y + dot0.z + dot0.w;

glam_assert!(dot1 != 0.0);

Expand Down Expand Up @@ -1154,4 +1154,4 @@ impl<'a> Product<&'a Self> for Mat4 {
{
iter.fold(IDENTITY, |a, &b| Self::mul(a, b))
}
}
}
17 changes: 17 additions & 0 deletions src/f32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ pub use vec4::*;
pub use vec4_mask::*;
pub use vec4_swizzle::*;

#[derive(Clone, Copy, Default, PartialEq, PartialOrd)]
#[repr(C)]
pub struct XYZW {
pub x: f32,
pub y: f32,
pub z: f32,
pub w: f32,
}

#[derive(Clone, Copy, Default, PartialEq, PartialOrd)]
#[repr(C)]
pub struct XYZ {
pub x: f32,
pub y: f32,
pub z: f32,
}

#[cfg(feature = "bytemuck")]
mod glam_bytemuck;
#[cfg(feature = "bytemuck")]
Expand Down
26 changes: 21 additions & 5 deletions src/f32/quat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use core::arch::x86_64::*;
use core::{
cmp::Ordering,
fmt,
ops::{Add, Div, Mul, MulAssign, Neg, Sub},
ops::{Add, Deref, DerefMut, Div, Mul, MulAssign, Neg, Sub},
};

#[cfg(feature = "std")]
use std::iter::{Product, Sum};

#[cfg(feature = "std")]
const ZERO: Quat = const_quat!([0.0, 0.0, 0.0, 0.0]);
const IDENTITY: Quat = const_quat!([0.0, 0.0, 0.0, 1.0]);

Expand Down Expand Up @@ -229,7 +230,7 @@ impl Quat {

#[cfg(vec4_f32)]
{
Self::from_xyzw(-(self.0).0, -(self.0).1, -(self.0).2, (self.0).3)
Self::from_xyzw(-self.0.x, -self.0.y, -self.0.z, self.0.w)
}
}

Expand Down Expand Up @@ -303,7 +304,7 @@ impl Quat {
// If the quat.w is close to -1.0, the angle will be near 2*PI which is close to
// a negative 0 rotation. By forcing quat.w to be positive, we'll end up with
// the shortest path.
let positive_w_angle = scalar_acos(self.0.w().abs()) * 2.0;
let positive_w_angle = scalar_acos(self.0.w.abs()) * 2.0;
positive_w_angle < THRESHOLD_ANGLE
}

Expand Down Expand Up @@ -438,7 +439,7 @@ impl Quat {
#[cfg(vec4_f32)]
{
let other = Vec3::from(other);
let w = self.0.w();
let w = self.0.w;
let b = Vec3::from(self.0);
let b2 = b.dot(b);
Vec3A::from(
Expand Down Expand Up @@ -730,6 +731,21 @@ impl From<__m128> for Quat {
}
}

impl Deref for Quat {
type Target = super::XYZW;
#[inline(always)]
fn deref(&self) -> &Self::Target {
unsafe { &*(self as *const Self as *const Self::Target) }
}
}

impl DerefMut for Quat {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(self as *mut Self as *mut Self::Target) }
}
}

#[cfg(feature = "std")]
impl<'a> Sum<&'a Self> for Quat {
fn sum<I>(iter: I) -> Self
Expand All @@ -748,4 +764,4 @@ impl<'a> Product<&'a Self> for Quat {
{
iter.fold(IDENTITY, |a, &b| Self::mul(a, b))
}
}
}

0 comments on commit 44d3208

Please sign in to comment.