Skip to content

Commit

Permalink
Replace impl From<Vec4> for Vec3A with Vec3A::from_vec4 method. (#522)
Browse files Browse the repository at this point in the history
The From impl violated the trait contract that the conversion should be
lossless.
  • Loading branch information
bitshifter authored Jun 4, 2024
1 parent c95f107 commit c6f4da4
Show file tree
Hide file tree
Showing 20 changed files with 91 additions and 122 deletions.
22 changes: 7 additions & 15 deletions codegen/templates/mat.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -452,19 +452,11 @@ impl {{ self_t }} {
#[inline]
#[must_use]
pub fn from_mat4(m: {{ mat4_t }}) -> Self {
{% if self_t == "Mat3A" %}
Self::from_cols(
m.x_axis.into(),
m.y_axis.into(),
m.z_axis.into(),
)
{% else %}
Self::from_cols(
m.x_axis.xyz(),
m.y_axis.xyz(),
m.z_axis.xyz(),
)
{% endif %}
Self::from_cols(
{{ col_t }}::from_vec4(m.x_axis),
{{ col_t }}::from_vec4(m.y_axis),
{{ col_t }}::from_vec4(m.z_axis),
)
}

/// Creates a 3D rotation matrix from the given quaternion.
Expand Down Expand Up @@ -1991,7 +1983,7 @@ impl {{ self_t }} {
res = self.y_axis.mul(rhs.yyyy()).add(res);
res = self.z_axis.mul(rhs.zzzz()).add(res);
res = self.w_axis.add(res);
res.into()
Vec3A::from_vec4(res)
{% endif %}
}

Expand All @@ -2008,7 +2000,7 @@ impl {{ self_t }} {
let mut res = self.x_axis.mul(rhs.xxxx());
res = self.y_axis.mul(rhs.yyyy()).add(res);
res = self.z_axis.mul(rhs.zzzz()).add(res);
res.into()
Vec3A::from_vec4(res)
{% endif %}
}
{% endif %}
Expand Down
33 changes: 15 additions & 18 deletions codegen/templates/vec.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,20 @@ impl {{ self_t }} {
{{ vec3_t }}::new(self.x, self.y, z)
}
{% elif dim == 3 %}
{% if self_t == "Vec3A" %}
/// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
///
/// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop.
#[inline]
#[must_use]
pub fn from_vec4(v: Vec4) -> Self {
{% if is_scalar %}
Self { x: v.x, y: v.y, z: v.z }
{% else %}
Self(v.0)
{% endif %}
}
{% else %}
/// Internal method for creating a 3D vector from a 4D vector, discarding `w`.
#[allow(dead_code)]
#[inline]
Expand All @@ -516,6 +530,7 @@ impl {{ self_t }} {
Self(v.0)
{% endif %}
}
{% endif %}

/// Creates a 4D vector from `self` and the given `w` value.
#[inline]
Expand Down Expand Up @@ -3481,24 +3496,6 @@ impl From<Vec3> for Vec3A {
}
}

impl From<Vec4> for Vec3A {
/// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
///
/// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop.
#[inline]
fn from(v: Vec4) -> Self {
{% if is_scalar %}
Self {
x: v.x,
y: v.y,
z: v.z,
}
{% else %}
Self(v.0)
{% endif %}
}
}

impl From<Vec3A> for Vec3 {
#[inline]
fn from(v: Vec3A) -> Self {
Expand Down
6 changes: 5 additions & 1 deletion src/f32/coresimd/mat3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ impl Mat3A {
#[inline]
#[must_use]
pub fn from_mat4(m: Mat4) -> Self {
Self::from_cols(m.x_axis.into(), m.y_axis.into(), m.z_axis.into())
Self::from_cols(
Vec3A::from_vec4(m.x_axis),
Vec3A::from_vec4(m.y_axis),
Vec3A::from_vec4(m.z_axis),
)
}

/// Creates a 3D rotation matrix from the given quaternion.
Expand Down
4 changes: 2 additions & 2 deletions src/f32/coresimd/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ impl Mat4 {
res = self.y_axis.mul(rhs.yyyy()).add(res);
res = self.z_axis.mul(rhs.zzzz()).add(res);
res = self.w_axis.add(res);
res.into()
Vec3A::from_vec4(res)
}

/// Transforms the give [`Vec3A`] as 3D vector.
Expand All @@ -1139,7 +1139,7 @@ impl Mat4 {
let mut res = self.x_axis.mul(rhs.xxxx());
res = self.y_axis.mul(rhs.yyyy()).add(res);
res = self.z_axis.mul(rhs.zzzz()).add(res);
res.into()
Vec3A::from_vec4(res)
}

/// Transforms a 4D vector.
Expand Down
17 changes: 4 additions & 13 deletions src/f32/coresimd/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ impl Vec3A {
slice[2] = self.z;
}

/// Internal method for creating a 3D vector from a 4D vector, discarding `w`.
#[allow(dead_code)]
/// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
///
/// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop.
#[inline]
#[must_use]
pub(crate) fn from_vec4(v: Vec4) -> Self {
pub fn from_vec4(v: Vec4) -> Self {
Self(v.0)
}

Expand Down Expand Up @@ -1289,16 +1290,6 @@ impl From<Vec3> for Vec3A {
}
}

impl From<Vec4> for Vec3A {
/// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
///
/// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop.
#[inline]
fn from(v: Vec4) -> Self {
Self(v.0)
}
}

impl From<Vec3A> for Vec3 {
#[inline]
fn from(v: Vec3A) -> Self {
Expand Down
6 changes: 5 additions & 1 deletion src/f32/mat3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ impl Mat3 {
#[inline]
#[must_use]
pub fn from_mat4(m: Mat4) -> Self {
Self::from_cols(m.x_axis.xyz(), m.y_axis.xyz(), m.z_axis.xyz())
Self::from_cols(
Vec3::from_vec4(m.x_axis),
Vec3::from_vec4(m.y_axis),
Vec3::from_vec4(m.z_axis),
)
}

/// Creates a 3D rotation matrix from the given quaternion.
Expand Down
6 changes: 5 additions & 1 deletion src/f32/neon/mat3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ impl Mat3A {
#[inline]
#[must_use]
pub fn from_mat4(m: Mat4) -> Self {
Self::from_cols(m.x_axis.into(), m.y_axis.into(), m.z_axis.into())
Self::from_cols(
Vec3A::from_vec4(m.x_axis),
Vec3A::from_vec4(m.y_axis),
Vec3A::from_vec4(m.z_axis),
)
}

/// Creates a 3D rotation matrix from the given quaternion.
Expand Down
4 changes: 2 additions & 2 deletions src/f32/neon/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ impl Mat4 {
res = self.y_axis.mul(rhs.yyyy()).add(res);
res = self.z_axis.mul(rhs.zzzz()).add(res);
res = self.w_axis.add(res);
res.into()
Vec3A::from_vec4(res)
}

/// Transforms the give [`Vec3A`] as 3D vector.
Expand All @@ -1144,7 +1144,7 @@ impl Mat4 {
let mut res = self.x_axis.mul(rhs.xxxx());
res = self.y_axis.mul(rhs.yyyy()).add(res);
res = self.z_axis.mul(rhs.zzzz()).add(res);
res.into()
Vec3A::from_vec4(res)
}

/// Transforms a 4D vector.
Expand Down
17 changes: 4 additions & 13 deletions src/f32/neon/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,12 @@ impl Vec3A {
slice[2] = self.z;
}

/// Internal method for creating a 3D vector from a 4D vector, discarding `w`.
#[allow(dead_code)]
/// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
///
/// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop.
#[inline]
#[must_use]
pub(crate) fn from_vec4(v: Vec4) -> Self {
pub fn from_vec4(v: Vec4) -> Self {
Self(v.0)
}

Expand Down Expand Up @@ -1356,16 +1357,6 @@ impl From<Vec3> for Vec3A {
}
}

impl From<Vec4> for Vec3A {
/// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
///
/// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop.
#[inline]
fn from(v: Vec4) -> Self {
Self(v.0)
}
}

impl From<Vec3A> for Vec3 {
#[inline]
fn from(v: Vec3A) -> Self {
Expand Down
6 changes: 5 additions & 1 deletion src/f32/scalar/mat3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ impl Mat3A {
#[inline]
#[must_use]
pub fn from_mat4(m: Mat4) -> Self {
Self::from_cols(m.x_axis.into(), m.y_axis.into(), m.z_axis.into())
Self::from_cols(
Vec3A::from_vec4(m.x_axis),
Vec3A::from_vec4(m.y_axis),
Vec3A::from_vec4(m.z_axis),
)
}

/// Creates a 3D rotation matrix from the given quaternion.
Expand Down
21 changes: 4 additions & 17 deletions src/f32/scalar/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,12 @@ impl Vec3A {
slice[2] = self.z;
}

/// Internal method for creating a 3D vector from a 4D vector, discarding `w`.
#[allow(dead_code)]
/// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
///
/// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop.
#[inline]
#[must_use]
pub(crate) fn from_vec4(v: Vec4) -> Self {
pub fn from_vec4(v: Vec4) -> Self {
Self {
x: v.x,
y: v.y,
Expand Down Expand Up @@ -1407,20 +1408,6 @@ impl From<Vec3> for Vec3A {
}
}

impl From<Vec4> for Vec3A {
/// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
///
/// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop.
#[inline]
fn from(v: Vec4) -> Self {
Self {
x: v.x,
y: v.y,
z: v.z,
}
}
}

impl From<Vec3A> for Vec3 {
#[inline]
fn from(v: Vec3A) -> Self {
Expand Down
6 changes: 5 additions & 1 deletion src/f32/sse2/mat3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ impl Mat3A {
#[inline]
#[must_use]
pub fn from_mat4(m: Mat4) -> Self {
Self::from_cols(m.x_axis.into(), m.y_axis.into(), m.z_axis.into())
Self::from_cols(
Vec3A::from_vec4(m.x_axis),
Vec3A::from_vec4(m.y_axis),
Vec3A::from_vec4(m.z_axis),
)
}

/// Creates a 3D rotation matrix from the given quaternion.
Expand Down
4 changes: 2 additions & 2 deletions src/f32/sse2/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,7 @@ impl Mat4 {
res = self.y_axis.mul(rhs.yyyy()).add(res);
res = self.z_axis.mul(rhs.zzzz()).add(res);
res = self.w_axis.add(res);
res.into()
Vec3A::from_vec4(res)
}

/// Transforms the give [`Vec3A`] as 3D vector.
Expand All @@ -1148,7 +1148,7 @@ impl Mat4 {
let mut res = self.x_axis.mul(rhs.xxxx());
res = self.y_axis.mul(rhs.yyyy()).add(res);
res = self.z_axis.mul(rhs.zzzz()).add(res);
res.into()
Vec3A::from_vec4(res)
}

/// Transforms a 4D vector.
Expand Down
17 changes: 4 additions & 13 deletions src/f32/sse2/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,12 @@ impl Vec3A {
slice[2] = self.z;
}

/// Internal method for creating a 3D vector from a 4D vector, discarding `w`.
#[allow(dead_code)]
/// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
///
/// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop.
#[inline]
#[must_use]
pub(crate) fn from_vec4(v: Vec4) -> Self {
pub fn from_vec4(v: Vec4) -> Self {
Self(v.0)
}

Expand Down Expand Up @@ -1364,16 +1365,6 @@ impl From<Vec3> for Vec3A {
}
}

impl From<Vec4> for Vec3A {
/// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
///
/// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop.
#[inline]
fn from(v: Vec4) -> Self {
Self(v.0)
}
}

impl From<Vec3A> for Vec3 {
#[inline]
fn from(v: Vec3A) -> Self {
Expand Down
6 changes: 5 additions & 1 deletion src/f32/wasm32/mat3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ impl Mat3A {
#[inline]
#[must_use]
pub fn from_mat4(m: Mat4) -> Self {
Self::from_cols(m.x_axis.into(), m.y_axis.into(), m.z_axis.into())
Self::from_cols(
Vec3A::from_vec4(m.x_axis),
Vec3A::from_vec4(m.y_axis),
Vec3A::from_vec4(m.z_axis),
)
}

/// Creates a 3D rotation matrix from the given quaternion.
Expand Down
4 changes: 2 additions & 2 deletions src/f32/wasm32/mat4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ impl Mat4 {
res = self.y_axis.mul(rhs.yyyy()).add(res);
res = self.z_axis.mul(rhs.zzzz()).add(res);
res = self.w_axis.add(res);
res.into()
Vec3A::from_vec4(res)
}

/// Transforms the give [`Vec3A`] as 3D vector.
Expand All @@ -1139,7 +1139,7 @@ impl Mat4 {
let mut res = self.x_axis.mul(rhs.xxxx());
res = self.y_axis.mul(rhs.yyyy()).add(res);
res = self.z_axis.mul(rhs.zzzz()).add(res);
res.into()
Vec3A::from_vec4(res)
}

/// Transforms a 4D vector.
Expand Down
Loading

0 comments on commit c6f4da4

Please sign in to comment.