Skip to content

Commit

Permalink
Add more const fn's (#343)
Browse files Browse the repository at this point in the history
* Add more const fn's

* Revert const extend

* let dim 2 extend still be const

* Make to_cols_array_2d const

* Directly cast
  • Loading branch information
VirxEC committed Oct 4, 2022
1 parent 5c46189 commit 34c57c6
Show file tree
Hide file tree
Showing 17 changed files with 135 additions and 158 deletions.
98 changes: 56 additions & 42 deletions codegen/templates/mat.rs.tera
Expand Up @@ -302,14 +302,28 @@ 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 { *(self as *const Self as *const [f32; 4]) }
{% 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 @@ -327,50 +341,50 @@ 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 %}
unsafe { *(self as *const Self as *const [[f32; 2]; 2]) }
{% else %}
[
{% for axis in axes %}
self.{{ axis }}.to_array(),
{%- 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 %}
{%- endfor %}
{%- endfor %}
)
]
{% endif %}
}
{% 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 %}
{% 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 %}
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 %}
)
{% endif %}
}
{% endif %}

{% if dim == 2 %}
/// Creates a {{ nxn }} matrix containing the combining non-uniform `scale` and rotation of
Expand Down
8 changes: 4 additions & 4 deletions src/f32/coresimd/mat2.rs
Expand Up @@ -56,8 +56,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 { *(self as *const Self as *const [f32; 4]) }
}

/// Creates a 2x2 matrix from a `[[f32; 2]; 2]` 2D array stored in column major order.
Expand All @@ -71,8 +71,8 @@ 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] {
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.
Expand Down
19 changes: 8 additions & 11 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 All @@ -130,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(),
Expand Down
27 changes: 9 additions & 18 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 All @@ -156,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(),
Expand Down
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions 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 All @@ -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()]
}

Expand Down
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -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(),
Expand Down
9 changes: 4 additions & 5 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 @@ -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(),
Expand All @@ -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
8 changes: 4 additions & 4 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 { *(self as *const Self as *const [f32; 4]) }
}

/// Creates a 2x2 matrix from a `[[f32; 2]; 2]` 2D array stored in column major order.
Expand All @@ -89,8 +89,8 @@ 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] {
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.
Expand Down
19 changes: 8 additions & 11 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 All @@ -133,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(),
Expand Down

0 comments on commit 34c57c6

Please sign in to comment.