Skip to content

Commit

Permalink
Make truncate and extend use matching aligned types.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitshifter committed Mar 19, 2024
1 parent 49b3aaf commit 11b8a6f
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 58 deletions.
40 changes: 36 additions & 4 deletions codegen/templates/affine.rs.tera
Expand Up @@ -39,7 +39,7 @@ use crate::{
{% if self_t == "Affine2" %}
Mat3A, Vec3A,
{% elif self_t == "Affine3A" %}
Vec3, Mat3,
Vec3, Mat3, Mat4A,
{% endif %}
{% if dim == 2 %}
{{ mat_t }}, {{ col_t }}, {{ mat3_t }},
Expand Down Expand Up @@ -499,6 +499,23 @@ impl {{ self_t }} {
}
}

{% if scalar_t == "f32" %}
/// The given `Mat4A` must be an affine transform,
/// i.e. contain no perspective transform.
#[inline]
#[must_use]
pub fn from_mat4a(m: Mat4A) -> Self {
Self {
matrix3: {{ mat_t }}::from_cols(
{{ col_t }}::from(m.x_axis),
{{ col_t }}::from(m.y_axis),
{{ col_t }}::from(m.z_axis),
),
translation: {{ col_t }}::from(m.w_axis),
}
}
{% endif %}

/// Extracts `scale`, `rotation` and `translation` from `self`.
///
/// The transform is expected to be non-degenerate and without shearing, or the output
Expand Down Expand Up @@ -816,17 +833,32 @@ impl Mul<{{ self_t }}> for {{ mat3_t }} {
}
}
{% elif dim == 3 %}
impl From<{{ self_t }}> for {{ mat4_t }} {

{% if scalar_t == "f32" %}
impl From<{{ self_t }}> for Mat4A {
#[inline]
fn from(m: {{ self_t }}) -> {{ mat4_t }} {
{{ mat4_t }}::from_cols(
fn from(m: {{ self_t }}) -> Self {
Self::from_cols(
m.matrix3.x_axis.extend(0.0),
m.matrix3.y_axis.extend(0.0),
m.matrix3.z_axis.extend(0.0),
m.translation.extend(1.0),
)
}
}
{% endif %}

impl From<{{ self_t }}> for {{ mat4_t }} {
#[inline]
fn from(m: {{ self_t }}) -> Self {
Self::from_cols(
m.matrix3.x_axis.extend(0.0).into(),
m.matrix3.y_axis.extend(0.0).into(),
m.matrix3.z_axis.extend(0.0).into(),
m.translation.extend(1.0).into(),
)
}
}

impl Mul<{{ mat4_t }}> for {{ self_t }} {
type Output = {{ mat4_t }};
Expand Down
8 changes: 4 additions & 4 deletions codegen/templates/mat.rs.tera
Expand Up @@ -741,7 +741,7 @@ impl {{ self_t }} {
self.z_axis.mul(inv_scale.z).xyz(),
);

let translation = self.w_axis.truncate();
let translation = self.w_axis.truncate().into();

(scale, rotation, translation)
}
Expand Down Expand Up @@ -1803,7 +1803,7 @@ impl {{ self_t }} {
res = self.z_axis.mul(rhs.z).add(res);
res = self.w_axis.add(res);
res = res.mul(res.wwww().recip());
res.truncate()
res.truncate().into()
}

/// Transforms the given 3D vector as a point.
Expand All @@ -1826,7 +1826,7 @@ impl {{ self_t }} {
res = self.y_axis.mul(rhs.y).add(res);
res = self.z_axis.mul(rhs.z).add(res);
res = self.w_axis.add(res);
res.truncate()
res.truncate().into()
}

/// Transforms the give 3D vector as a direction.
Expand All @@ -1846,7 +1846,7 @@ impl {{ self_t }} {
let mut res = self.x_axis.mul(rhs.x);
res = self.y_axis.mul(rhs.y).add(res);
res = self.z_axis.mul(rhs.z).add(res);
res.truncate()
res.truncate().into()
}

{% endif %}
Expand Down
14 changes: 14 additions & 0 deletions codegen/templates/vec.rs.tera
Expand Up @@ -25,6 +25,8 @@
{% if is_align %}
{% set self_t = "Vec" ~ dim ~ "A" %}
{% set mask_t = "BVec" ~ dim ~ "A" %}
{% set vec3a_t = "Vec3A" %}
{% set vec4a_t = "Vec4A" %}
{% else %}
{% set self_t = "Vec" ~ dim %}
{% endif %}
Expand Down Expand Up @@ -478,9 +480,15 @@ impl {{ self_t }} {
/// Creates a 4D vector from `self` and the given `w` value.
#[inline]
#[must_use]
{%- if vec4a_t %}
pub fn extend(self, w: {{ scalar_t }}) -> {{ vec4a_t }} {
{{ vec4a_t }}::from((self, w))
}
{% else %}
pub fn extend(self, w: {{ scalar_t }}) -> {{ vec4_t }} {
{{ vec4_t }}::new(self.x, self.y, self.z, w)
}
{% endif %}

/// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`.
///
Expand All @@ -501,10 +509,16 @@ impl {{ self_t }} {
{%- endif %}
#[inline]
#[must_use]
{%- if vec3a_t %}
pub fn truncate(self) -> {{ vec3a_t }} {
{{ vec3a_t }}::from(self)
}
{%- else %}
pub fn truncate(self) -> {{ vec3_t }} {
{{ vec3_t }}::new(self.x, self.y, self.z)
}
{% endif %}
{% endif %}


{% for c in components %}
Expand Down
35 changes: 31 additions & 4 deletions src/f32/affine3a.rs
@@ -1,6 +1,6 @@
// Generated from affine.rs.tera template. Edit the template, not the generated file.

use crate::{Mat3, Mat3A, Mat4, Quat, Vec3, Vec3A};
use crate::{Mat3, Mat3A, Mat4, Mat4A, Quat, Vec3, Vec3A};
use core::ops::{Deref, DerefMut, Mul, MulAssign};

/// A 3D affine transform, which can represent translation, rotation, scaling and shear.
Expand Down Expand Up @@ -269,6 +269,21 @@ impl Affine3A {
}
}

/// The given `Mat4A` must be an affine transform,
/// i.e. contain no perspective transform.
#[inline]
#[must_use]
pub fn from_mat4a(m: Mat4A) -> Self {
Self {
matrix3: Mat3A::from_cols(
Vec3A::from(m.x_axis),
Vec3A::from(m.y_axis),
Vec3A::from(m.z_axis),
),
translation: Vec3A::from(m.w_axis),
}
}

/// Extracts `scale`, `rotation` and `translation` from `self`.
///
/// The transform is expected to be non-degenerate and without shearing, or the output
Expand Down Expand Up @@ -550,10 +565,10 @@ impl MulAssign for Affine3A {
}
}

impl From<Affine3A> for Mat4 {
impl From<Affine3A> for Mat4A {
#[inline]
fn from(m: Affine3A) -> Mat4 {
Mat4::from_cols(
fn from(m: Affine3A) -> Self {
Self::from_cols(
m.matrix3.x_axis.extend(0.0),
m.matrix3.y_axis.extend(0.0),
m.matrix3.z_axis.extend(0.0),
Expand All @@ -562,6 +577,18 @@ impl From<Affine3A> for Mat4 {
}
}

impl From<Affine3A> for Mat4 {
#[inline]
fn from(m: Affine3A) -> Self {
Self::from_cols(
m.matrix3.x_axis.extend(0.0).into(),
m.matrix3.y_axis.extend(0.0).into(),
m.matrix3.z_axis.extend(0.0).into(),
m.translation.extend(1.0).into(),
)
}
}

impl Mul<Mat4> for Affine3A {
type Output = Mat4;

Expand Down
8 changes: 4 additions & 4 deletions src/f32/coresimd/mat4a.rs
Expand Up @@ -264,7 +264,7 @@ impl Mat4A {
self.z_axis.mul(inv_scale.z).xyz(),
);

let translation = self.w_axis.truncate();
let translation = self.w_axis.truncate().into();

(scale, rotation, translation)
}
Expand Down Expand Up @@ -1070,7 +1070,7 @@ impl Mat4A {
res = self.z_axis.mul(rhs.z).add(res);
res = self.w_axis.add(res);
res = res.mul(res.wwww().recip());
res.truncate()
res.truncate().into()
}

/// Transforms the given 3D vector as a point.
Expand All @@ -1093,7 +1093,7 @@ impl Mat4A {
res = self.y_axis.mul(rhs.y).add(res);
res = self.z_axis.mul(rhs.z).add(res);
res = self.w_axis.add(res);
res.truncate()
res.truncate().into()
}

/// Transforms the give 3D vector as a direction.
Expand All @@ -1113,7 +1113,7 @@ impl Mat4A {
let mut res = self.x_axis.mul(rhs.x);
res = self.y_axis.mul(rhs.y).add(res);
res = self.z_axis.mul(rhs.z).add(res);
res.truncate()
res.truncate().into()
}

/// Transforms the given [`Vec3A`] as 3D point.
Expand Down
4 changes: 2 additions & 2 deletions src/f32/coresimd/vec3a.rs
Expand Up @@ -149,8 +149,8 @@ impl Vec3A {
/// Creates a 4D vector from `self` and the given `w` value.
#[inline]
#[must_use]
pub fn extend(self, w: f32) -> Vec4 {
Vec4::new(self.x, self.y, self.z, w)
pub fn extend(self, w: f32) -> Vec4A {
Vec4A::from((self, w))
}

/// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`.
Expand Down
4 changes: 2 additions & 2 deletions src/f32/coresimd/vec4a.rs
Expand Up @@ -152,8 +152,8 @@ impl Vec4A {
/// To truncate to [`Vec3A`] use [`Vec3A::from()`].
#[inline]
#[must_use]
pub fn truncate(self) -> Vec3 {
Vec3::new(self.x, self.y, self.z)
pub fn truncate(self) -> Vec3A {
Vec3A::from(self)
}

/// Creates a 4D vector from `self` with the given value of `x`.
Expand Down
8 changes: 4 additions & 4 deletions src/f32/mat4.rs
Expand Up @@ -269,7 +269,7 @@ impl Mat4 {
self.z_axis.mul(inv_scale.z).xyz(),
);

let translation = self.w_axis.truncate();
let translation = self.w_axis.truncate().into();

(scale, rotation, translation)
}
Expand Down Expand Up @@ -985,7 +985,7 @@ impl Mat4 {
res = self.z_axis.mul(rhs.z).add(res);
res = self.w_axis.add(res);
res = res.mul(res.wwww().recip());
res.truncate()
res.truncate().into()
}

/// Transforms the given 3D vector as a point.
Expand All @@ -1008,7 +1008,7 @@ impl Mat4 {
res = self.y_axis.mul(rhs.y).add(res);
res = self.z_axis.mul(rhs.z).add(res);
res = self.w_axis.add(res);
res.truncate()
res.truncate().into()
}

/// Transforms the give 3D vector as a direction.
Expand All @@ -1028,7 +1028,7 @@ impl Mat4 {
let mut res = self.x_axis.mul(rhs.x);
res = self.y_axis.mul(rhs.y).add(res);
res = self.z_axis.mul(rhs.z).add(res);
res.truncate()
res.truncate().into()
}

/// Transforms the given [`Vec3A`] as 3D point.
Expand Down
8 changes: 4 additions & 4 deletions src/f32/scalar/mat4a.rs
Expand Up @@ -270,7 +270,7 @@ impl Mat4A {
self.z_axis.mul(inv_scale.z).xyz(),
);

let translation = self.w_axis.truncate();
let translation = self.w_axis.truncate().into();

(scale, rotation, translation)
}
Expand Down Expand Up @@ -986,7 +986,7 @@ impl Mat4A {
res = self.z_axis.mul(rhs.z).add(res);
res = self.w_axis.add(res);
res = res.mul(res.wwww().recip());
res.truncate()
res.truncate().into()
}

/// Transforms the given 3D vector as a point.
Expand All @@ -1009,7 +1009,7 @@ impl Mat4A {
res = self.y_axis.mul(rhs.y).add(res);
res = self.z_axis.mul(rhs.z).add(res);
res = self.w_axis.add(res);
res.truncate()
res.truncate().into()
}

/// Transforms the give 3D vector as a direction.
Expand All @@ -1029,7 +1029,7 @@ impl Mat4A {
let mut res = self.x_axis.mul(rhs.x);
res = self.y_axis.mul(rhs.y).add(res);
res = self.z_axis.mul(rhs.z).add(res);
res.truncate()
res.truncate().into()
}

/// Transforms the given [`Vec3A`] as 3D point.
Expand Down
4 changes: 2 additions & 2 deletions src/f32/scalar/vec3a.rs
Expand Up @@ -156,8 +156,8 @@ impl Vec3A {
/// Creates a 4D vector from `self` and the given `w` value.
#[inline]
#[must_use]
pub fn extend(self, w: f32) -> Vec4 {
Vec4::new(self.x, self.y, self.z, w)
pub fn extend(self, w: f32) -> Vec4A {
Vec4A::from((self, w))
}

/// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`.
Expand Down
4 changes: 2 additions & 2 deletions src/f32/scalar/vec4a.rs
Expand Up @@ -169,8 +169,8 @@ impl Vec4A {
/// To truncate to [`Vec3A`] use [`Vec3A::from()`].
#[inline]
#[must_use]
pub fn truncate(self) -> Vec3 {
Vec3::new(self.x, self.y, self.z)
pub fn truncate(self) -> Vec3A {
Vec3A::from(self)
}

/// Creates a 4D vector from `self` with the given value of `x`.
Expand Down
8 changes: 4 additions & 4 deletions src/f32/sse2/mat4a.rs
Expand Up @@ -267,7 +267,7 @@ impl Mat4A {
self.z_axis.mul(inv_scale.z).xyz(),
);

let translation = self.w_axis.truncate();
let translation = self.w_axis.truncate().into();

(scale, rotation, translation)
}
Expand Down Expand Up @@ -1079,7 +1079,7 @@ impl Mat4A {
res = self.z_axis.mul(rhs.z).add(res);
res = self.w_axis.add(res);
res = res.mul(res.wwww().recip());
res.truncate()
res.truncate().into()
}

/// Transforms the given 3D vector as a point.
Expand All @@ -1102,7 +1102,7 @@ impl Mat4A {
res = self.y_axis.mul(rhs.y).add(res);
res = self.z_axis.mul(rhs.z).add(res);
res = self.w_axis.add(res);
res.truncate()
res.truncate().into()
}

/// Transforms the give 3D vector as a direction.
Expand All @@ -1122,7 +1122,7 @@ impl Mat4A {
let mut res = self.x_axis.mul(rhs.x);
res = self.y_axis.mul(rhs.y).add(res);
res = self.z_axis.mul(rhs.z).add(res);
res.truncate()
res.truncate().into()
}

/// Transforms the given [`Vec3A`] as 3D point.
Expand Down

0 comments on commit 11b8a6f

Please sign in to comment.