Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more const fn's #343

Merged
merged 5 commits into from Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 50 additions & 38 deletions codegen/templates/mat.rs.tera
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions codegen/templates/vec.rs.tera
Expand Up @@ -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 }} {
VirxEC marked this conversation as resolved.
Show resolved Hide resolved
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`.
Expand Down
9 changes: 7 additions & 2 deletions src/f32/coresimd/mat2.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 }
VirxEC marked this conversation as resolved.
Show resolved Hide resolved
}

/// Creates a 2x2 matrix from a `[[f32; 2]; 2]` 2D array stored in column major order.
Expand Down
17 changes: 7 additions & 10 deletions src/f32/coresimd/mat3a.rs
Expand Up @@ -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,
]
}

Expand Down
25 changes: 8 additions & 17 deletions src/f32/coresimd/mat4.rs
Expand Up @@ -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,
]
}

Expand Down
5 changes: 3 additions & 2 deletions src/f32/coresimd/vec3a.rs
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion src/f32/mat3.rs
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/f32/scalar/mat2.rs
Expand Up @@ -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]
}

Expand Down
2 changes: 1 addition & 1 deletion src/f32/scalar/mat3a.rs
Expand Up @@ -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,
Expand Down
7 changes: 3 additions & 4 deletions src/f32/scalar/mat4.rs
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)
}

Expand Down
5 changes: 3 additions & 2 deletions src/f32/scalar/vec3a.rs
Expand Up @@ -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`.
Expand Down
4 changes: 2 additions & 2 deletions src/f32/sse2/mat2.rs
Expand Up @@ -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.
Expand Down
17 changes: 7 additions & 10 deletions src/f32/sse2/mat3a.rs
Expand Up @@ -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,
]
}

Expand Down
25 changes: 8 additions & 17 deletions src/f32/sse2/mat4.rs
Expand Up @@ -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,
]
}

Expand Down
5 changes: 3 additions & 2 deletions src/f32/sse2/vec3a.rs
Expand Up @@ -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`.
Expand Down
5 changes: 3 additions & 2 deletions src/f32/vec3.rs
Expand Up @@ -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`.
Expand Down
9 changes: 7 additions & 2 deletions src/f32/wasm32/mat2.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down