From 23999ce6670a10aa63f5fffc3eabb5ab63197209 Mon Sep 17 00:00:00 2001 From: Eric Veilleux Date: Wed, 28 Sep 2022 21:53:32 -0400 Subject: [PATCH 1/5] Add more const fn's --- codegen/templates/mat.rs.tera | 88 ++++++++++++++++++++--------------- codegen/templates/vec.rs.tera | 5 +- src/f32/coresimd/mat2.rs | 9 +++- src/f32/coresimd/mat3a.rs | 17 +++---- src/f32/coresimd/mat4.rs | 25 ++++------ src/f32/coresimd/vec3a.rs | 5 +- src/f32/mat3.rs | 2 +- src/f32/scalar/mat2.rs | 2 +- src/f32/scalar/mat3a.rs | 2 +- src/f32/scalar/mat4.rs | 7 ++- src/f32/scalar/vec3a.rs | 5 +- src/f32/sse2/mat2.rs | 4 +- src/f32/sse2/mat3a.rs | 17 +++---- src/f32/sse2/mat4.rs | 25 ++++------ src/f32/sse2/vec3a.rs | 5 +- src/f32/vec3.rs | 5 +- src/f32/wasm32/mat2.rs | 9 +++- src/f32/wasm32/mat3a.rs | 17 +++---- src/f32/wasm32/mat4.rs | 25 ++++------ src/f32/wasm32/vec3a.rs | 5 +- src/f64/dmat2.rs | 2 +- src/f64/dmat3.rs | 2 +- src/f64/dmat4.rs | 2 +- src/f64/dvec3.rs | 5 +- src/i32/ivec3.rs | 5 +- src/u32/uvec3.rs | 5 +- 26 files changed, 147 insertions(+), 153 deletions(-) diff --git a/codegen/templates/mat.rs.tera b/codegen/templates/mat.rs.tera index 4b366970..bff3cb04 100644 --- a/codegen/templates/mat.rs.tera +++ b/codegen/templates/mat.rs.tera @@ -112,7 +112,7 @@ use core::simd::{Which::*, *}; #[allow(unused_imports)] use num_traits::Float; -{% if self_t == "Mat2" and is_sse2 %} +{% if self_t == "Mat2" and not is_scalar %} union UnionCast { a: [f32; 4], v: {{ self_t }} @@ -302,14 +302,30 @@ impl {{ self_t }} { /// Creates a `[{{ scalar_t }}; {{ size }}]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array(&self) -> [{{ scalar_t }}; {{ size }}] { - [ - {% for axis in axes %} - {% for c in components %} - self.{{ axis }}.{{ c }}, + pub const fn to_cols_array(&self) -> [{{ scalar_t }}; {{ size }}] { + {% if self_t == "Mat2" and not is_scalar %} + unsafe { + UnionCast { v: *self }.a + } + {% else %} + {% if dim >= 2 and not is_scalar %} + {% for axis in axes %} + let [{% for c in components %} {{ axis }}_{{ c }}, {% endfor %}] = self.{{ axis }}.to_array(); {%- endfor %} - {%- endfor %} - ] + {% endif %} + + [ + {% for axis in axes %} + {% for c in components %} + {% if dim >= 2 and not is_scalar %} + {{ axis }}_{{ c }}, + {% else %} + self.{{ axis }}.{{ c }}, + {% endif %} + {%- endfor %} + {%- endfor %} + ] + {% endif %} } /// Creates a {{ nxn }} matrix from a `[[{{ scalar_t }}; {{ dim }}]; {{ dim }}]` {{ dim }}D array stored in column major order. @@ -334,43 +350,39 @@ impl {{ self_t }} { {%- endfor %} ] } -{% if dim == 4 and vecn_t != "DVec4" %} + /// Creates a {{ nxn }} matrix with its diagonal set to `diagonal` and all other entries set to 0. #[doc(alias = "scale")] #[inline] pub const fn from_diagonal(diagonal: {{ vecn_t }}) -> Self { - // diagonal.x, diagonal.y etc can't be done in a const-context - let [x, y, z, w] = diagonal.to_array(); - Self::new( - {% for i in range(end = dim) %} - {% for j in range(end = dim) %} - {% if i == j %} - {{ components[i] }}, - {% else %} - 0.0, - {% endif %} + {% if self_t == "Mat4" and not is_scalar %} + // diagonal.x, diagonal.y etc can't be done in a const-context + let [x, y, z, w] = diagonal.to_array(); + Self::new( + {% for i in range(end = dim) %} + {% for j in range(end = dim) %} + {% if i == j %} + {{ components[i] }}, + {% else %} + 0.0, + {% endif %} + {%- endfor %} {%- endfor %} - {%- endfor %} - ) - } -{% else %} - /// Creates a {{ nxn }} matrix with its diagonal set to `diagonal` and all other entries set to 0. - #[doc(alias = "scale")] - #[inline] - pub const fn from_diagonal(diagonal: {{ vecn_t }}) -> Self { - Self::new( - {% for i in range(end = dim) %} - {% for j in range(end = dim) %} - {% if i == j %} - diagonal.{{ components[i] }}, - {% else %} - 0.0, - {% endif %} + ) + {% else %} + Self::new( + {% for i in range(end = dim) %} + {% for j in range(end = dim) %} + {% if i == j %} + diagonal.{{ components[i] }}, + {% else %} + 0.0, + {% endif %} + {%- endfor %} {%- endfor %} - {%- endfor %} - ) + ) + {% endif %} } -{% endif %} {% if dim == 2 %} /// Creates a {{ nxn }} matrix containing the combining non-uniform `scale` and rotation of diff --git a/codegen/templates/vec.rs.tera b/codegen/templates/vec.rs.tera index a8ca85be..a96cd0e2 100644 --- a/codegen/templates/vec.rs.tera +++ b/codegen/templates/vec.rs.tera @@ -389,8 +389,9 @@ impl {{ self_t }} { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub fn extend(self, w: {{ scalar_t }}) -> {{ vec4_t }} { - {{ vec4_t }}::new(self.x, self.y, self.z, w) + pub const fn extend(self, w: {{ scalar_t }}) -> {{ vec4_t }} { + let [x, y, z] = self.to_array(); + {{ vec4_t }}::new(x, y, z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. diff --git a/src/f32/coresimd/mat2.rs b/src/f32/coresimd/mat2.rs index 2e63446a..7ab76e42 100644 --- a/src/f32/coresimd/mat2.rs +++ b/src/f32/coresimd/mat2.rs @@ -12,6 +12,11 @@ use core::simd::{Which::*, *}; #[allow(unused_imports)] use num_traits::Float; +union UnionCast { + a: [f32; 4], + v: Mat2, +} + /// Creates a 2x2 matrix from column vectors. #[inline(always)] pub const fn mat2(x_axis: Vec2, y_axis: Vec2) -> Mat2 { @@ -56,8 +61,8 @@ impl Mat2 { /// Creates a `[f32; 4]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array(&self) -> [f32; 4] { - [self.x_axis.x, self.x_axis.y, self.y_axis.x, self.y_axis.y] + pub const fn to_cols_array(&self) -> [f32; 4] { + unsafe { UnionCast { v: *self }.a } } /// Creates a 2x2 matrix from a `[[f32; 2]; 2]` 2D array stored in column major order. diff --git a/src/f32/coresimd/mat3a.rs b/src/f32/coresimd/mat3a.rs index 8c14461d..cfbeb04c 100644 --- a/src/f32/coresimd/mat3a.rs +++ b/src/f32/coresimd/mat3a.rs @@ -101,17 +101,14 @@ impl Mat3A { /// Creates a `[f32; 9]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array(&self) -> [f32; 9] { + pub const fn to_cols_array(&self) -> [f32; 9] { + let [x_axis_x, x_axis_y, x_axis_z] = self.x_axis.to_array(); + let [y_axis_x, y_axis_y, y_axis_z] = self.y_axis.to_array(); + let [z_axis_x, z_axis_y, z_axis_z] = self.z_axis.to_array(); + [ - self.x_axis.x, - self.x_axis.y, - self.x_axis.z, - self.y_axis.x, - self.y_axis.y, - self.y_axis.z, - self.z_axis.x, - self.z_axis.y, - self.z_axis.z, + x_axis_x, x_axis_y, x_axis_z, y_axis_x, y_axis_y, y_axis_z, z_axis_x, z_axis_y, + z_axis_z, ] } diff --git a/src/f32/coresimd/mat4.rs b/src/f32/coresimd/mat4.rs index 99639727..ab52eb0d 100644 --- a/src/f32/coresimd/mat4.rs +++ b/src/f32/coresimd/mat4.rs @@ -119,24 +119,15 @@ impl Mat4 { /// Creates a `[f32; 16]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array(&self) -> [f32; 16] { + pub const fn to_cols_array(&self) -> [f32; 16] { + let [x_axis_x, x_axis_y, x_axis_z, x_axis_w] = self.x_axis.to_array(); + let [y_axis_x, y_axis_y, y_axis_z, y_axis_w] = self.y_axis.to_array(); + let [z_axis_x, z_axis_y, z_axis_z, z_axis_w] = self.z_axis.to_array(); + let [w_axis_x, w_axis_y, w_axis_z, w_axis_w] = self.w_axis.to_array(); + [ - self.x_axis.x, - self.x_axis.y, - self.x_axis.z, - self.x_axis.w, - self.y_axis.x, - self.y_axis.y, - self.y_axis.z, - self.y_axis.w, - self.z_axis.x, - self.z_axis.y, - self.z_axis.z, - self.z_axis.w, - self.w_axis.x, - self.w_axis.y, - self.w_axis.z, - self.w_axis.w, + x_axis_x, x_axis_y, x_axis_z, x_axis_w, y_axis_x, y_axis_y, y_axis_z, y_axis_w, + z_axis_x, z_axis_y, z_axis_z, z_axis_w, w_axis_x, w_axis_y, w_axis_z, w_axis_w, ] } diff --git a/src/f32/coresimd/vec3a.rs b/src/f32/coresimd/vec3a.rs index 5215abb0..f4888b30 100644 --- a/src/f32/coresimd/vec3a.rs +++ b/src/f32/coresimd/vec3a.rs @@ -129,8 +129,9 @@ impl Vec3A { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub fn extend(self, w: f32) -> Vec4 { - Vec4::new(self.x, self.y, self.z, w) + pub const fn extend(self, w: f32) -> Vec4 { + let [x, y, z] = self.to_array(); + Vec4::new(x, y, z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. diff --git a/src/f32/mat3.rs b/src/f32/mat3.rs index 1839e297..18804c41 100644 --- a/src/f32/mat3.rs +++ b/src/f32/mat3.rs @@ -99,7 +99,7 @@ impl Mat3 { /// Creates a `[f32; 9]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array(&self) -> [f32; 9] { + pub const fn to_cols_array(&self) -> [f32; 9] { [ self.x_axis.x, self.x_axis.y, diff --git a/src/f32/scalar/mat2.rs b/src/f32/scalar/mat2.rs index da98c688..70ca7569 100644 --- a/src/f32/scalar/mat2.rs +++ b/src/f32/scalar/mat2.rs @@ -65,7 +65,7 @@ impl Mat2 { /// Creates a `[f32; 4]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array(&self) -> [f32; 4] { + pub const fn to_cols_array(&self) -> [f32; 4] { [self.x_axis.x, self.x_axis.y, self.y_axis.x, self.y_axis.y] } diff --git a/src/f32/scalar/mat3a.rs b/src/f32/scalar/mat3a.rs index e72c8f02..f8f97b9e 100644 --- a/src/f32/scalar/mat3a.rs +++ b/src/f32/scalar/mat3a.rs @@ -99,7 +99,7 @@ impl Mat3A { /// Creates a `[f32; 9]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array(&self) -> [f32; 9] { + pub const fn to_cols_array(&self) -> [f32; 9] { [ self.x_axis.x, self.x_axis.y, diff --git a/src/f32/scalar/mat4.rs b/src/f32/scalar/mat4.rs index f2de316e..bfeb0c32 100644 --- a/src/f32/scalar/mat4.rs +++ b/src/f32/scalar/mat4.rs @@ -124,7 +124,7 @@ impl Mat4 { /// Creates a `[f32; 16]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array(&self) -> [f32; 16] { + pub const fn to_cols_array(&self) -> [f32; 16] { [ self.x_axis.x, self.x_axis.y, @@ -174,10 +174,9 @@ impl Mat4 { #[doc(alias = "scale")] #[inline] pub const fn from_diagonal(diagonal: Vec4) -> Self { - // diagonal.x, diagonal.y etc can't be done in a const-context - let [x, y, z, w] = diagonal.to_array(); Self::new( - x, 0.0, 0.0, 0.0, 0.0, y, 0.0, 0.0, 0.0, 0.0, z, 0.0, 0.0, 0.0, 0.0, w, + diagonal.x, 0.0, 0.0, 0.0, 0.0, diagonal.y, 0.0, 0.0, 0.0, 0.0, diagonal.z, 0.0, 0.0, + 0.0, 0.0, diagonal.w, ) } diff --git a/src/f32/scalar/vec3a.rs b/src/f32/scalar/vec3a.rs index 71b9ebd9..287d24f1 100644 --- a/src/f32/scalar/vec3a.rs +++ b/src/f32/scalar/vec3a.rs @@ -140,8 +140,9 @@ impl Vec3A { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub fn extend(self, w: f32) -> Vec4 { - Vec4::new(self.x, self.y, self.z, w) + pub const fn extend(self, w: f32) -> Vec4 { + let [x, y, z] = self.to_array(); + Vec4::new(x, y, z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. diff --git a/src/f32/sse2/mat2.rs b/src/f32/sse2/mat2.rs index fa7b6079..8b26830e 100644 --- a/src/f32/sse2/mat2.rs +++ b/src/f32/sse2/mat2.rs @@ -74,8 +74,8 @@ impl Mat2 { /// Creates a `[f32; 4]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array(&self) -> [f32; 4] { - [self.x_axis.x, self.x_axis.y, self.y_axis.x, self.y_axis.y] + pub const fn to_cols_array(&self) -> [f32; 4] { + unsafe { UnionCast { v: *self }.a } } /// Creates a 2x2 matrix from a `[[f32; 2]; 2]` 2D array stored in column major order. diff --git a/src/f32/sse2/mat3a.rs b/src/f32/sse2/mat3a.rs index 25fde995..5ef00467 100644 --- a/src/f32/sse2/mat3a.rs +++ b/src/f32/sse2/mat3a.rs @@ -104,17 +104,14 @@ impl Mat3A { /// Creates a `[f32; 9]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array(&self) -> [f32; 9] { + pub const fn to_cols_array(&self) -> [f32; 9] { + let [x_axis_x, x_axis_y, x_axis_z] = self.x_axis.to_array(); + let [y_axis_x, y_axis_y, y_axis_z] = self.y_axis.to_array(); + let [z_axis_x, z_axis_y, z_axis_z] = self.z_axis.to_array(); + [ - self.x_axis.x, - self.x_axis.y, - self.x_axis.z, - self.y_axis.x, - self.y_axis.y, - self.y_axis.z, - self.z_axis.x, - self.z_axis.y, - self.z_axis.z, + x_axis_x, x_axis_y, x_axis_z, y_axis_x, y_axis_y, y_axis_z, z_axis_x, z_axis_y, + z_axis_z, ] } diff --git a/src/f32/sse2/mat4.rs b/src/f32/sse2/mat4.rs index b6a0c622..2549b226 100644 --- a/src/f32/sse2/mat4.rs +++ b/src/f32/sse2/mat4.rs @@ -122,24 +122,15 @@ impl Mat4 { /// Creates a `[f32; 16]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array(&self) -> [f32; 16] { + pub const fn to_cols_array(&self) -> [f32; 16] { + let [x_axis_x, x_axis_y, x_axis_z, x_axis_w] = self.x_axis.to_array(); + let [y_axis_x, y_axis_y, y_axis_z, y_axis_w] = self.y_axis.to_array(); + let [z_axis_x, z_axis_y, z_axis_z, z_axis_w] = self.z_axis.to_array(); + let [w_axis_x, w_axis_y, w_axis_z, w_axis_w] = self.w_axis.to_array(); + [ - self.x_axis.x, - self.x_axis.y, - self.x_axis.z, - self.x_axis.w, - self.y_axis.x, - self.y_axis.y, - self.y_axis.z, - self.y_axis.w, - self.z_axis.x, - self.z_axis.y, - self.z_axis.z, - self.z_axis.w, - self.w_axis.x, - self.w_axis.y, - self.w_axis.z, - self.w_axis.w, + x_axis_x, x_axis_y, x_axis_z, x_axis_w, y_axis_x, y_axis_y, y_axis_z, y_axis_w, + z_axis_x, z_axis_y, z_axis_z, z_axis_w, w_axis_x, w_axis_y, w_axis_z, w_axis_w, ] } diff --git a/src/f32/sse2/vec3a.rs b/src/f32/sse2/vec3a.rs index 1ead0df4..db40915d 100644 --- a/src/f32/sse2/vec3a.rs +++ b/src/f32/sse2/vec3a.rs @@ -141,8 +141,9 @@ impl Vec3A { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub fn extend(self, w: f32) -> Vec4 { - Vec4::new(self.x, self.y, self.z, w) + pub const fn extend(self, w: f32) -> Vec4 { + let [x, y, z] = self.to_array(); + Vec4::new(x, y, z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. diff --git a/src/f32/vec3.rs b/src/f32/vec3.rs index df0ae09a..5828bbc5 100644 --- a/src/f32/vec3.rs +++ b/src/f32/vec3.rs @@ -134,8 +134,9 @@ impl Vec3 { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub fn extend(self, w: f32) -> Vec4 { - Vec4::new(self.x, self.y, self.z, w) + pub const fn extend(self, w: f32) -> Vec4 { + let [x, y, z] = self.to_array(); + Vec4::new(x, y, z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. diff --git a/src/f32/wasm32/mat2.rs b/src/f32/wasm32/mat2.rs index ef721b3f..8e3b1e41 100644 --- a/src/f32/wasm32/mat2.rs +++ b/src/f32/wasm32/mat2.rs @@ -12,6 +12,11 @@ use core::arch::wasm32::*; #[allow(unused_imports)] use num_traits::Float; +union UnionCast { + a: [f32; 4], + v: Mat2, +} + /// Creates a 2x2 matrix from column vectors. #[inline(always)] pub const fn mat2(x_axis: Vec2, y_axis: Vec2) -> Mat2 { @@ -56,8 +61,8 @@ impl Mat2 { /// Creates a `[f32; 4]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array(&self) -> [f32; 4] { - [self.x_axis.x, self.x_axis.y, self.y_axis.x, self.y_axis.y] + pub const fn to_cols_array(&self) -> [f32; 4] { + unsafe { UnionCast { v: *self }.a } } /// Creates a 2x2 matrix from a `[[f32; 2]; 2]` 2D array stored in column major order. diff --git a/src/f32/wasm32/mat3a.rs b/src/f32/wasm32/mat3a.rs index f5d974a2..6bc8fc6b 100644 --- a/src/f32/wasm32/mat3a.rs +++ b/src/f32/wasm32/mat3a.rs @@ -101,17 +101,14 @@ impl Mat3A { /// Creates a `[f32; 9]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array(&self) -> [f32; 9] { + pub const fn to_cols_array(&self) -> [f32; 9] { + let [x_axis_x, x_axis_y, x_axis_z] = self.x_axis.to_array(); + let [y_axis_x, y_axis_y, y_axis_z] = self.y_axis.to_array(); + let [z_axis_x, z_axis_y, z_axis_z] = self.z_axis.to_array(); + [ - self.x_axis.x, - self.x_axis.y, - self.x_axis.z, - self.y_axis.x, - self.y_axis.y, - self.y_axis.z, - self.z_axis.x, - self.z_axis.y, - self.z_axis.z, + x_axis_x, x_axis_y, x_axis_z, y_axis_x, y_axis_y, y_axis_z, z_axis_x, z_axis_y, + z_axis_z, ] } diff --git a/src/f32/wasm32/mat4.rs b/src/f32/wasm32/mat4.rs index 9ce0c23f..2ae7455a 100644 --- a/src/f32/wasm32/mat4.rs +++ b/src/f32/wasm32/mat4.rs @@ -119,24 +119,15 @@ impl Mat4 { /// Creates a `[f32; 16]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array(&self) -> [f32; 16] { + pub const fn to_cols_array(&self) -> [f32; 16] { + let [x_axis_x, x_axis_y, x_axis_z, x_axis_w] = self.x_axis.to_array(); + let [y_axis_x, y_axis_y, y_axis_z, y_axis_w] = self.y_axis.to_array(); + let [z_axis_x, z_axis_y, z_axis_z, z_axis_w] = self.z_axis.to_array(); + let [w_axis_x, w_axis_y, w_axis_z, w_axis_w] = self.w_axis.to_array(); + [ - self.x_axis.x, - self.x_axis.y, - self.x_axis.z, - self.x_axis.w, - self.y_axis.x, - self.y_axis.y, - self.y_axis.z, - self.y_axis.w, - self.z_axis.x, - self.z_axis.y, - self.z_axis.z, - self.z_axis.w, - self.w_axis.x, - self.w_axis.y, - self.w_axis.z, - self.w_axis.w, + x_axis_x, x_axis_y, x_axis_z, x_axis_w, y_axis_x, y_axis_y, y_axis_z, y_axis_w, + z_axis_x, z_axis_y, z_axis_z, z_axis_w, w_axis_x, w_axis_y, w_axis_z, w_axis_w, ] } diff --git a/src/f32/wasm32/vec3a.rs b/src/f32/wasm32/vec3a.rs index cec3c790..14294855 100644 --- a/src/f32/wasm32/vec3a.rs +++ b/src/f32/wasm32/vec3a.rs @@ -133,8 +133,9 @@ impl Vec3A { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub fn extend(self, w: f32) -> Vec4 { - Vec4::new(self.x, self.y, self.z, w) + pub const fn extend(self, w: f32) -> Vec4 { + let [x, y, z] = self.to_array(); + Vec4::new(x, y, z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. diff --git a/src/f64/dmat2.rs b/src/f64/dmat2.rs index 3f79adf6..d13edbdf 100644 --- a/src/f64/dmat2.rs +++ b/src/f64/dmat2.rs @@ -61,7 +61,7 @@ impl DMat2 { /// Creates a `[f64; 4]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array(&self) -> [f64; 4] { + pub const fn to_cols_array(&self) -> [f64; 4] { [self.x_axis.x, self.x_axis.y, self.y_axis.x, self.y_axis.y] } diff --git a/src/f64/dmat3.rs b/src/f64/dmat3.rs index adf6cbf6..fa7d6f6d 100644 --- a/src/f64/dmat3.rs +++ b/src/f64/dmat3.rs @@ -99,7 +99,7 @@ impl DMat3 { /// Creates a `[f64; 9]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array(&self) -> [f64; 9] { + pub const fn to_cols_array(&self) -> [f64; 9] { [ self.x_axis.x, self.x_axis.y, diff --git a/src/f64/dmat4.rs b/src/f64/dmat4.rs index cac5d550..13d11272 100644 --- a/src/f64/dmat4.rs +++ b/src/f64/dmat4.rs @@ -118,7 +118,7 @@ impl DMat4 { /// Creates a `[f64; 16]` array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array(&self) -> [f64; 16] { + pub const fn to_cols_array(&self) -> [f64; 16] { [ self.x_axis.x, self.x_axis.y, diff --git a/src/f64/dvec3.rs b/src/f64/dvec3.rs index 2251a28c..d1ae77fe 100644 --- a/src/f64/dvec3.rs +++ b/src/f64/dvec3.rs @@ -134,8 +134,9 @@ impl DVec3 { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub fn extend(self, w: f64) -> DVec4 { - DVec4::new(self.x, self.y, self.z, w) + pub const fn extend(self, w: f64) -> DVec4 { + let [x, y, z] = self.to_array(); + DVec4::new(x, y, z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. diff --git a/src/i32/ivec3.rs b/src/i32/ivec3.rs index 0019b515..cbc7f5b8 100644 --- a/src/i32/ivec3.rs +++ b/src/i32/ivec3.rs @@ -128,8 +128,9 @@ impl IVec3 { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub fn extend(self, w: i32) -> IVec4 { - IVec4::new(self.x, self.y, self.z, w) + pub const fn extend(self, w: i32) -> IVec4 { + let [x, y, z] = self.to_array(); + IVec4::new(x, y, z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. diff --git a/src/u32/uvec3.rs b/src/u32/uvec3.rs index 18a5bbd1..42b63493 100644 --- a/src/u32/uvec3.rs +++ b/src/u32/uvec3.rs @@ -116,8 +116,9 @@ impl UVec3 { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub fn extend(self, w: u32) -> UVec4 { - UVec4::new(self.x, self.y, self.z, w) + pub const fn extend(self, w: u32) -> UVec4 { + let [x, y, z] = self.to_array(); + UVec4::new(x, y, z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. From 5825057b8174c1e74edec3004296c2d6be0ed27e Mon Sep 17 00:00:00 2001 From: Virx Date: Thu, 29 Sep 2022 08:32:48 -0400 Subject: [PATCH 2/5] Revert const extend --- codegen/templates/vec.rs.tera | 7 +++---- src/f32/coresimd/vec3a.rs | 5 ++--- src/f32/scalar/vec3a.rs | 5 ++--- src/f32/sse2/vec3a.rs | 5 ++--- src/f32/vec2.rs | 2 +- src/f32/vec3.rs | 5 ++--- src/f32/wasm32/vec3a.rs | 5 ++--- src/f64/dvec2.rs | 2 +- src/f64/dvec3.rs | 5 ++--- src/i32/ivec2.rs | 2 +- src/i32/ivec3.rs | 5 ++--- src/u32/uvec2.rs | 2 +- src/u32/uvec3.rs | 5 ++--- 13 files changed, 23 insertions(+), 32 deletions(-) diff --git a/codegen/templates/vec.rs.tera b/codegen/templates/vec.rs.tera index a96cd0e2..e8402204 100644 --- a/codegen/templates/vec.rs.tera +++ b/codegen/templates/vec.rs.tera @@ -372,7 +372,7 @@ impl {{ self_t }} { {% if dim == 2 %} /// Creates a 3D vector from `self` and the given `z` value. #[inline] - pub const fn extend(self, z: {{ scalar_t }}) -> {{ vec3_t }} { + pub fn extend(self, z: {{ scalar_t }}) -> {{ vec3_t }} { {{ vec3_t }}::new(self.x, self.y, z) } {% elif dim == 3 %} @@ -389,9 +389,8 @@ impl {{ self_t }} { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub const fn extend(self, w: {{ scalar_t }}) -> {{ vec4_t }} { - let [x, y, z] = self.to_array(); - {{ vec4_t }}::new(x, y, z, w) + pub fn extend(self, w: {{ scalar_t }}) -> {{ vec4_t }} { + {{ vec4_t }}::new(self.x, self.y, self.z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. diff --git a/src/f32/coresimd/vec3a.rs b/src/f32/coresimd/vec3a.rs index f4888b30..5215abb0 100644 --- a/src/f32/coresimd/vec3a.rs +++ b/src/f32/coresimd/vec3a.rs @@ -129,9 +129,8 @@ impl Vec3A { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub const fn extend(self, w: f32) -> Vec4 { - let [x, y, z] = self.to_array(); - Vec4::new(x, y, z, w) + pub fn extend(self, w: f32) -> Vec4 { + Vec4::new(self.x, self.y, self.z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. diff --git a/src/f32/scalar/vec3a.rs b/src/f32/scalar/vec3a.rs index 287d24f1..71b9ebd9 100644 --- a/src/f32/scalar/vec3a.rs +++ b/src/f32/scalar/vec3a.rs @@ -140,9 +140,8 @@ impl Vec3A { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub const fn extend(self, w: f32) -> Vec4 { - let [x, y, z] = self.to_array(); - Vec4::new(x, y, z, w) + pub fn extend(self, w: f32) -> Vec4 { + Vec4::new(self.x, self.y, self.z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. diff --git a/src/f32/sse2/vec3a.rs b/src/f32/sse2/vec3a.rs index db40915d..1ead0df4 100644 --- a/src/f32/sse2/vec3a.rs +++ b/src/f32/sse2/vec3a.rs @@ -141,9 +141,8 @@ impl Vec3A { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub const fn extend(self, w: f32) -> Vec4 { - let [x, y, z] = self.to_array(); - Vec4::new(x, y, z, w) + pub fn extend(self, w: f32) -> Vec4 { + Vec4::new(self.x, self.y, self.z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. diff --git a/src/f32/vec2.rs b/src/f32/vec2.rs index 7c275ebb..45e61e7c 100644 --- a/src/f32/vec2.rs +++ b/src/f32/vec2.rs @@ -115,7 +115,7 @@ impl Vec2 { /// Creates a 3D vector from `self` and the given `z` value. #[inline] - pub const fn extend(self, z: f32) -> Vec3 { + pub fn extend(self, z: f32) -> Vec3 { Vec3::new(self.x, self.y, z) } diff --git a/src/f32/vec3.rs b/src/f32/vec3.rs index 5828bbc5..df0ae09a 100644 --- a/src/f32/vec3.rs +++ b/src/f32/vec3.rs @@ -134,9 +134,8 @@ impl Vec3 { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub const fn extend(self, w: f32) -> Vec4 { - let [x, y, z] = self.to_array(); - Vec4::new(x, y, z, w) + pub fn extend(self, w: f32) -> Vec4 { + Vec4::new(self.x, self.y, self.z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. diff --git a/src/f32/wasm32/vec3a.rs b/src/f32/wasm32/vec3a.rs index 14294855..cec3c790 100644 --- a/src/f32/wasm32/vec3a.rs +++ b/src/f32/wasm32/vec3a.rs @@ -133,9 +133,8 @@ impl Vec3A { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub const fn extend(self, w: f32) -> Vec4 { - let [x, y, z] = self.to_array(); - Vec4::new(x, y, z, w) + pub fn extend(self, w: f32) -> Vec4 { + Vec4::new(self.x, self.y, self.z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. diff --git a/src/f64/dvec2.rs b/src/f64/dvec2.rs index 205dca2e..29d370c8 100644 --- a/src/f64/dvec2.rs +++ b/src/f64/dvec2.rs @@ -115,7 +115,7 @@ impl DVec2 { /// Creates a 3D vector from `self` and the given `z` value. #[inline] - pub const fn extend(self, z: f64) -> DVec3 { + pub fn extend(self, z: f64) -> DVec3 { DVec3::new(self.x, self.y, z) } diff --git a/src/f64/dvec3.rs b/src/f64/dvec3.rs index d1ae77fe..2251a28c 100644 --- a/src/f64/dvec3.rs +++ b/src/f64/dvec3.rs @@ -134,9 +134,8 @@ impl DVec3 { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub const fn extend(self, w: f64) -> DVec4 { - let [x, y, z] = self.to_array(); - DVec4::new(x, y, z, w) + pub fn extend(self, w: f64) -> DVec4 { + DVec4::new(self.x, self.y, self.z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. diff --git a/src/i32/ivec2.rs b/src/i32/ivec2.rs index 4ad537b5..e4acba56 100644 --- a/src/i32/ivec2.rs +++ b/src/i32/ivec2.rs @@ -109,7 +109,7 @@ impl IVec2 { /// Creates a 3D vector from `self` and the given `z` value. #[inline] - pub const fn extend(self, z: i32) -> IVec3 { + pub fn extend(self, z: i32) -> IVec3 { IVec3::new(self.x, self.y, z) } diff --git a/src/i32/ivec3.rs b/src/i32/ivec3.rs index cbc7f5b8..0019b515 100644 --- a/src/i32/ivec3.rs +++ b/src/i32/ivec3.rs @@ -128,9 +128,8 @@ impl IVec3 { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub const fn extend(self, w: i32) -> IVec4 { - let [x, y, z] = self.to_array(); - IVec4::new(x, y, z, w) + pub fn extend(self, w: i32) -> IVec4 { + IVec4::new(self.x, self.y, self.z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. diff --git a/src/u32/uvec2.rs b/src/u32/uvec2.rs index b4226001..40bc0041 100644 --- a/src/u32/uvec2.rs +++ b/src/u32/uvec2.rs @@ -100,7 +100,7 @@ impl UVec2 { /// Creates a 3D vector from `self` and the given `z` value. #[inline] - pub const fn extend(self, z: u32) -> UVec3 { + pub fn extend(self, z: u32) -> UVec3 { UVec3::new(self.x, self.y, z) } diff --git a/src/u32/uvec3.rs b/src/u32/uvec3.rs index 42b63493..18a5bbd1 100644 --- a/src/u32/uvec3.rs +++ b/src/u32/uvec3.rs @@ -116,9 +116,8 @@ impl UVec3 { /// Creates a 4D vector from `self` and the given `w` value. #[inline] - pub const fn extend(self, w: u32) -> UVec4 { - let [x, y, z] = self.to_array(); - UVec4::new(x, y, z, w) + pub fn extend(self, w: u32) -> UVec4 { + UVec4::new(self.x, self.y, self.z, w) } /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`. From 6447028f02a37d27126e552930dee55406031502 Mon Sep 17 00:00:00 2001 From: Virx Date: Thu, 29 Sep 2022 08:38:45 -0400 Subject: [PATCH 3/5] let dim 2 extend still be const --- codegen/templates/vec.rs.tera | 2 +- src/f32/vec2.rs | 2 +- src/f64/dvec2.rs | 2 +- src/i32/ivec2.rs | 2 +- src/u32/uvec2.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/codegen/templates/vec.rs.tera b/codegen/templates/vec.rs.tera index e8402204..a8ca85be 100644 --- a/codegen/templates/vec.rs.tera +++ b/codegen/templates/vec.rs.tera @@ -372,7 +372,7 @@ impl {{ self_t }} { {% if dim == 2 %} /// Creates a 3D vector from `self` and the given `z` value. #[inline] - pub fn extend(self, z: {{ scalar_t }}) -> {{ vec3_t }} { + pub const fn extend(self, z: {{ scalar_t }}) -> {{ vec3_t }} { {{ vec3_t }}::new(self.x, self.y, z) } {% elif dim == 3 %} diff --git a/src/f32/vec2.rs b/src/f32/vec2.rs index 45e61e7c..7c275ebb 100644 --- a/src/f32/vec2.rs +++ b/src/f32/vec2.rs @@ -115,7 +115,7 @@ impl Vec2 { /// Creates a 3D vector from `self` and the given `z` value. #[inline] - pub fn extend(self, z: f32) -> Vec3 { + pub const fn extend(self, z: f32) -> Vec3 { Vec3::new(self.x, self.y, z) } diff --git a/src/f64/dvec2.rs b/src/f64/dvec2.rs index 29d370c8..205dca2e 100644 --- a/src/f64/dvec2.rs +++ b/src/f64/dvec2.rs @@ -115,7 +115,7 @@ impl DVec2 { /// Creates a 3D vector from `self` and the given `z` value. #[inline] - pub fn extend(self, z: f64) -> DVec3 { + pub const fn extend(self, z: f64) -> DVec3 { DVec3::new(self.x, self.y, z) } diff --git a/src/i32/ivec2.rs b/src/i32/ivec2.rs index e4acba56..4ad537b5 100644 --- a/src/i32/ivec2.rs +++ b/src/i32/ivec2.rs @@ -109,7 +109,7 @@ impl IVec2 { /// Creates a 3D vector from `self` and the given `z` value. #[inline] - pub fn extend(self, z: i32) -> IVec3 { + pub const fn extend(self, z: i32) -> IVec3 { IVec3::new(self.x, self.y, z) } diff --git a/src/u32/uvec2.rs b/src/u32/uvec2.rs index 40bc0041..b4226001 100644 --- a/src/u32/uvec2.rs +++ b/src/u32/uvec2.rs @@ -100,7 +100,7 @@ impl UVec2 { /// Creates a 3D vector from `self` and the given `z` value. #[inline] - pub fn extend(self, z: u32) -> UVec3 { + pub const fn extend(self, z: u32) -> UVec3 { UVec3::new(self.x, self.y, z) } From 8819a79f983af753592d96046718681c083f0c3c Mon Sep 17 00:00:00 2001 From: Virx Date: Thu, 29 Sep 2022 08:54:08 -0400 Subject: [PATCH 4/5] Make to_cols_array_2d const --- codegen/templates/mat.rs.tera | 18 +++++++++++++----- src/f32/coresimd/mat2.rs | 5 +++-- src/f32/coresimd/mat3a.rs | 2 +- src/f32/coresimd/mat4.rs | 2 +- src/f32/mat3.rs | 2 +- src/f32/scalar/mat2.rs | 2 +- src/f32/scalar/mat3a.rs | 2 +- src/f32/scalar/mat4.rs | 2 +- src/f32/sse2/mat2.rs | 5 +++-- src/f32/sse2/mat3a.rs | 2 +- src/f32/sse2/mat4.rs | 2 +- src/f32/wasm32/mat2.rs | 5 +++-- src/f32/wasm32/mat3a.rs | 2 +- src/f32/wasm32/mat4.rs | 2 +- src/f64/dmat2.rs | 2 +- src/f64/dmat3.rs | 2 +- src/f64/dmat4.rs | 2 +- 17 files changed, 35 insertions(+), 24 deletions(-) diff --git a/codegen/templates/mat.rs.tera b/codegen/templates/mat.rs.tera index bff3cb04..f4ca2bb4 100644 --- a/codegen/templates/mat.rs.tera +++ b/codegen/templates/mat.rs.tera @@ -343,12 +343,20 @@ impl {{ self_t }} { /// Creates a `[[{{ scalar_t }}; {{ dim }}]; {{ dim }}]` {{ dim }}D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array_2d(&self) -> [[{{ scalar_t }}; {{ dim }}]; {{ dim }}] { - [ - {% for axis in axes %} + pub const fn to_cols_array_2d(&self) -> [[{{ scalar_t }}; {{ dim }}]; {{ dim }}] { + {% if self_t == "Mat2" and not is_scalar %} + let self_ = unsafe { &*(self as *const Self as *const crate::deref::Cols2) }; + [ + self_.x_axis.to_array(), + self_.y_axis.to_array(), + ] + {% else %} + [ + {% for axis in axes %} self.{{ axis }}.to_array(), - {%- endfor %} - ] + {%- endfor %} + ] + {% endif %} } /// Creates a {{ nxn }} matrix with its diagonal set to `diagonal` and all other entries set to 0. diff --git a/src/f32/coresimd/mat2.rs b/src/f32/coresimd/mat2.rs index 7ab76e42..780abe18 100644 --- a/src/f32/coresimd/mat2.rs +++ b/src/f32/coresimd/mat2.rs @@ -76,8 +76,9 @@ impl Mat2 { /// Creates a `[[f32; 2]; 2]` 2D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array_2d(&self) -> [[f32; 2]; 2] { - [self.x_axis.to_array(), self.y_axis.to_array()] + pub const fn to_cols_array_2d(&self) -> [[f32; 2]; 2] { + let self_ = unsafe { &*(self as *const Self as *const crate::deref::Cols2) }; + [self_.x_axis.to_array(), self_.y_axis.to_array()] } /// Creates a 2x2 matrix with its diagonal set to `diagonal` and all other entries set to 0. diff --git a/src/f32/coresimd/mat3a.rs b/src/f32/coresimd/mat3a.rs index cfbeb04c..bda6d275 100644 --- a/src/f32/coresimd/mat3a.rs +++ b/src/f32/coresimd/mat3a.rs @@ -127,7 +127,7 @@ impl Mat3A { /// Creates a `[[f32; 3]; 3]` 3D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array_2d(&self) -> [[f32; 3]; 3] { + pub const fn to_cols_array_2d(&self) -> [[f32; 3]; 3] { [ self.x_axis.to_array(), self.y_axis.to_array(), diff --git a/src/f32/coresimd/mat4.rs b/src/f32/coresimd/mat4.rs index ab52eb0d..d8a86f47 100644 --- a/src/f32/coresimd/mat4.rs +++ b/src/f32/coresimd/mat4.rs @@ -147,7 +147,7 @@ impl Mat4 { /// Creates a `[[f32; 4]; 4]` 4D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array_2d(&self) -> [[f32; 4]; 4] { + pub const fn to_cols_array_2d(&self) -> [[f32; 4]; 4] { [ self.x_axis.to_array(), self.y_axis.to_array(), diff --git a/src/f32/mat3.rs b/src/f32/mat3.rs index 18804c41..6c2ed4da 100644 --- a/src/f32/mat3.rs +++ b/src/f32/mat3.rs @@ -128,7 +128,7 @@ impl Mat3 { /// Creates a `[[f32; 3]; 3]` 3D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array_2d(&self) -> [[f32; 3]; 3] { + pub const fn to_cols_array_2d(&self) -> [[f32; 3]; 3] { [ self.x_axis.to_array(), self.y_axis.to_array(), diff --git a/src/f32/scalar/mat2.rs b/src/f32/scalar/mat2.rs index 70ca7569..03d082ef 100644 --- a/src/f32/scalar/mat2.rs +++ b/src/f32/scalar/mat2.rs @@ -80,7 +80,7 @@ impl Mat2 { /// Creates a `[[f32; 2]; 2]` 2D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array_2d(&self) -> [[f32; 2]; 2] { + pub const fn to_cols_array_2d(&self) -> [[f32; 2]; 2] { [self.x_axis.to_array(), self.y_axis.to_array()] } diff --git a/src/f32/scalar/mat3a.rs b/src/f32/scalar/mat3a.rs index f8f97b9e..ea569812 100644 --- a/src/f32/scalar/mat3a.rs +++ b/src/f32/scalar/mat3a.rs @@ -128,7 +128,7 @@ impl Mat3A { /// Creates a `[[f32; 3]; 3]` 3D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array_2d(&self) -> [[f32; 3]; 3] { + pub const fn to_cols_array_2d(&self) -> [[f32; 3]; 3] { [ self.x_axis.to_array(), self.y_axis.to_array(), diff --git a/src/f32/scalar/mat4.rs b/src/f32/scalar/mat4.rs index bfeb0c32..580eb279 100644 --- a/src/f32/scalar/mat4.rs +++ b/src/f32/scalar/mat4.rs @@ -161,7 +161,7 @@ impl Mat4 { /// Creates a `[[f32; 4]; 4]` 4D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array_2d(&self) -> [[f32; 4]; 4] { + pub const fn to_cols_array_2d(&self) -> [[f32; 4]; 4] { [ self.x_axis.to_array(), self.y_axis.to_array(), diff --git a/src/f32/sse2/mat2.rs b/src/f32/sse2/mat2.rs index 8b26830e..22c16f02 100644 --- a/src/f32/sse2/mat2.rs +++ b/src/f32/sse2/mat2.rs @@ -89,8 +89,9 @@ impl Mat2 { /// Creates a `[[f32; 2]; 2]` 2D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array_2d(&self) -> [[f32; 2]; 2] { - [self.x_axis.to_array(), self.y_axis.to_array()] + pub const fn to_cols_array_2d(&self) -> [[f32; 2]; 2] { + let self_ = unsafe { &*(self as *const Self as *const crate::deref::Cols2) }; + [self_.x_axis.to_array(), self_.y_axis.to_array()] } /// Creates a 2x2 matrix with its diagonal set to `diagonal` and all other entries set to 0. diff --git a/src/f32/sse2/mat3a.rs b/src/f32/sse2/mat3a.rs index 5ef00467..14912dd1 100644 --- a/src/f32/sse2/mat3a.rs +++ b/src/f32/sse2/mat3a.rs @@ -130,7 +130,7 @@ impl Mat3A { /// Creates a `[[f32; 3]; 3]` 3D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array_2d(&self) -> [[f32; 3]; 3] { + pub const fn to_cols_array_2d(&self) -> [[f32; 3]; 3] { [ self.x_axis.to_array(), self.y_axis.to_array(), diff --git a/src/f32/sse2/mat4.rs b/src/f32/sse2/mat4.rs index 2549b226..24e16adc 100644 --- a/src/f32/sse2/mat4.rs +++ b/src/f32/sse2/mat4.rs @@ -150,7 +150,7 @@ impl Mat4 { /// Creates a `[[f32; 4]; 4]` 4D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array_2d(&self) -> [[f32; 4]; 4] { + pub const fn to_cols_array_2d(&self) -> [[f32; 4]; 4] { [ self.x_axis.to_array(), self.y_axis.to_array(), diff --git a/src/f32/wasm32/mat2.rs b/src/f32/wasm32/mat2.rs index 8e3b1e41..3fe18a9e 100644 --- a/src/f32/wasm32/mat2.rs +++ b/src/f32/wasm32/mat2.rs @@ -76,8 +76,9 @@ impl Mat2 { /// Creates a `[[f32; 2]; 2]` 2D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array_2d(&self) -> [[f32; 2]; 2] { - [self.x_axis.to_array(), self.y_axis.to_array()] + pub const fn to_cols_array_2d(&self) -> [[f32; 2]; 2] { + let self_ = unsafe { &*(self as *const Self as *const crate::deref::Cols2) }; + [self_.x_axis.to_array(), self_.y_axis.to_array()] } /// Creates a 2x2 matrix with its diagonal set to `diagonal` and all other entries set to 0. diff --git a/src/f32/wasm32/mat3a.rs b/src/f32/wasm32/mat3a.rs index 6bc8fc6b..573ea1f1 100644 --- a/src/f32/wasm32/mat3a.rs +++ b/src/f32/wasm32/mat3a.rs @@ -127,7 +127,7 @@ impl Mat3A { /// Creates a `[[f32; 3]; 3]` 3D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array_2d(&self) -> [[f32; 3]; 3] { + pub const fn to_cols_array_2d(&self) -> [[f32; 3]; 3] { [ self.x_axis.to_array(), self.y_axis.to_array(), diff --git a/src/f32/wasm32/mat4.rs b/src/f32/wasm32/mat4.rs index 2ae7455a..433e893d 100644 --- a/src/f32/wasm32/mat4.rs +++ b/src/f32/wasm32/mat4.rs @@ -147,7 +147,7 @@ impl Mat4 { /// Creates a `[[f32; 4]; 4]` 4D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array_2d(&self) -> [[f32; 4]; 4] { + pub const fn to_cols_array_2d(&self) -> [[f32; 4]; 4] { [ self.x_axis.to_array(), self.y_axis.to_array(), diff --git a/src/f64/dmat2.rs b/src/f64/dmat2.rs index d13edbdf..bd52c8c3 100644 --- a/src/f64/dmat2.rs +++ b/src/f64/dmat2.rs @@ -76,7 +76,7 @@ impl DMat2 { /// Creates a `[[f64; 2]; 2]` 2D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array_2d(&self) -> [[f64; 2]; 2] { + pub const fn to_cols_array_2d(&self) -> [[f64; 2]; 2] { [self.x_axis.to_array(), self.y_axis.to_array()] } diff --git a/src/f64/dmat3.rs b/src/f64/dmat3.rs index fa7d6f6d..600223cb 100644 --- a/src/f64/dmat3.rs +++ b/src/f64/dmat3.rs @@ -128,7 +128,7 @@ impl DMat3 { /// Creates a `[[f64; 3]; 3]` 3D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array_2d(&self) -> [[f64; 3]; 3] { + pub const fn to_cols_array_2d(&self) -> [[f64; 3]; 3] { [ self.x_axis.to_array(), self.y_axis.to_array(), diff --git a/src/f64/dmat4.rs b/src/f64/dmat4.rs index 13d11272..cc4283c2 100644 --- a/src/f64/dmat4.rs +++ b/src/f64/dmat4.rs @@ -155,7 +155,7 @@ impl DMat4 { /// Creates a `[[f64; 4]; 4]` 4D array storing data in column major order. /// If you require data in row major order `transpose` the matrix first. #[inline] - pub fn to_cols_array_2d(&self) -> [[f64; 4]; 4] { + pub const fn to_cols_array_2d(&self) -> [[f64; 4]; 4] { [ self.x_axis.to_array(), self.y_axis.to_array(), From 7b1ddd0b8768daf93aab4ac66bf1853f5f7d8d6e Mon Sep 17 00:00:00 2001 From: Virx Date: Mon, 3 Oct 2022 09:10:09 -0400 Subject: [PATCH 5/5] Directly cast --- codegen/templates/mat.rs.tera | 12 +++--------- src/f32/coresimd/mat2.rs | 10 ++-------- src/f32/sse2/mat2.rs | 5 ++--- src/f32/wasm32/mat2.rs | 10 ++-------- 4 files changed, 9 insertions(+), 28 deletions(-) diff --git a/codegen/templates/mat.rs.tera b/codegen/templates/mat.rs.tera index f4ca2bb4..2fb7856d 100644 --- a/codegen/templates/mat.rs.tera +++ b/codegen/templates/mat.rs.tera @@ -112,7 +112,7 @@ use core::simd::{Which::*, *}; #[allow(unused_imports)] use num_traits::Float; -{% if self_t == "Mat2" and not is_scalar %} +{% if self_t == "Mat2" and is_sse2 %} union UnionCast { a: [f32; 4], v: {{ self_t }} @@ -304,9 +304,7 @@ impl {{ self_t }} { #[inline] pub const fn to_cols_array(&self) -> [{{ scalar_t }}; {{ size }}] { {% if self_t == "Mat2" and not is_scalar %} - unsafe { - UnionCast { v: *self }.a - } + unsafe { *(self as *const Self as *const [f32; 4]) } {% else %} {% if dim >= 2 and not is_scalar %} {% for axis in axes %} @@ -345,11 +343,7 @@ impl {{ self_t }} { #[inline] pub const fn to_cols_array_2d(&self) -> [[{{ scalar_t }}; {{ dim }}]; {{ dim }}] { {% if self_t == "Mat2" and not is_scalar %} - let self_ = unsafe { &*(self as *const Self as *const crate::deref::Cols2) }; - [ - self_.x_axis.to_array(), - self_.y_axis.to_array(), - ] + unsafe { *(self as *const Self as *const [[f32; 2]; 2]) } {% else %} [ {% for axis in axes %} diff --git a/src/f32/coresimd/mat2.rs b/src/f32/coresimd/mat2.rs index 780abe18..276816fa 100644 --- a/src/f32/coresimd/mat2.rs +++ b/src/f32/coresimd/mat2.rs @@ -12,11 +12,6 @@ use core::simd::{Which::*, *}; #[allow(unused_imports)] use num_traits::Float; -union UnionCast { - a: [f32; 4], - v: Mat2, -} - /// Creates a 2x2 matrix from column vectors. #[inline(always)] pub const fn mat2(x_axis: Vec2, y_axis: Vec2) -> Mat2 { @@ -62,7 +57,7 @@ impl Mat2 { /// If you require data in row major order `transpose` the matrix first. #[inline] pub const fn to_cols_array(&self) -> [f32; 4] { - unsafe { UnionCast { v: *self }.a } + unsafe { *(self as *const Self as *const [f32; 4]) } } /// Creates a 2x2 matrix from a `[[f32; 2]; 2]` 2D array stored in column major order. @@ -77,8 +72,7 @@ impl Mat2 { /// If you require data in row major order `transpose` the matrix first. #[inline] pub const fn to_cols_array_2d(&self) -> [[f32; 2]; 2] { - let self_ = unsafe { &*(self as *const Self as *const crate::deref::Cols2) }; - [self_.x_axis.to_array(), self_.y_axis.to_array()] + unsafe { *(self as *const Self as *const [[f32; 2]; 2]) } } /// Creates a 2x2 matrix with its diagonal set to `diagonal` and all other entries set to 0. diff --git a/src/f32/sse2/mat2.rs b/src/f32/sse2/mat2.rs index 22c16f02..311448bf 100644 --- a/src/f32/sse2/mat2.rs +++ b/src/f32/sse2/mat2.rs @@ -75,7 +75,7 @@ impl Mat2 { /// If you require data in row major order `transpose` the matrix first. #[inline] pub const fn to_cols_array(&self) -> [f32; 4] { - unsafe { UnionCast { v: *self }.a } + unsafe { *(self as *const Self as *const [f32; 4]) } } /// Creates a 2x2 matrix from a `[[f32; 2]; 2]` 2D array stored in column major order. @@ -90,8 +90,7 @@ impl Mat2 { /// If you require data in row major order `transpose` the matrix first. #[inline] pub const fn to_cols_array_2d(&self) -> [[f32; 2]; 2] { - let self_ = unsafe { &*(self as *const Self as *const crate::deref::Cols2) }; - [self_.x_axis.to_array(), self_.y_axis.to_array()] + unsafe { *(self as *const Self as *const [[f32; 2]; 2]) } } /// Creates a 2x2 matrix with its diagonal set to `diagonal` and all other entries set to 0. diff --git a/src/f32/wasm32/mat2.rs b/src/f32/wasm32/mat2.rs index 3fe18a9e..7869ff52 100644 --- a/src/f32/wasm32/mat2.rs +++ b/src/f32/wasm32/mat2.rs @@ -12,11 +12,6 @@ use core::arch::wasm32::*; #[allow(unused_imports)] use num_traits::Float; -union UnionCast { - a: [f32; 4], - v: Mat2, -} - /// Creates a 2x2 matrix from column vectors. #[inline(always)] pub const fn mat2(x_axis: Vec2, y_axis: Vec2) -> Mat2 { @@ -62,7 +57,7 @@ impl Mat2 { /// If you require data in row major order `transpose` the matrix first. #[inline] pub const fn to_cols_array(&self) -> [f32; 4] { - unsafe { UnionCast { v: *self }.a } + unsafe { *(self as *const Self as *const [f32; 4]) } } /// Creates a 2x2 matrix from a `[[f32; 2]; 2]` 2D array stored in column major order. @@ -77,8 +72,7 @@ impl Mat2 { /// If you require data in row major order `transpose` the matrix first. #[inline] pub const fn to_cols_array_2d(&self) -> [[f32; 2]; 2] { - let self_ = unsafe { &*(self as *const Self as *const crate::deref::Cols2) }; - [self_.x_axis.to_array(), self_.y_axis.to_array()] + unsafe { *(self as *const Self as *const [[f32; 2]; 2]) } } /// Creates a 2x2 matrix with its diagonal set to `diagonal` and all other entries set to 0.