Skip to content

Commit

Permalink
Use BVec4A as the Vec4 mask type even when SIMD is unavailable. (#442)
Browse files Browse the repository at this point in the history
This is a breaking change.

When SIMD is not available performance will be worse than using BVec4 however the API will be consistent between different architectures.

When the scalar-math feature is enabled BVec4 is still used.
  • Loading branch information
bitshifter authored Dec 3, 2023
1 parent a9c6af9 commit bfaa2ac
Show file tree
Hide file tree
Showing 30 changed files with 116 additions and 112 deletions.
2 changes: 1 addition & 1 deletion codegen/src/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl ContextBuilder {
}

pub fn new_vec4() -> Self {
Self::new_vecn(4).with_scalar_t("f32")
Self::new_vecn(4).with_scalar_t("f32").with_is_align(true)
}

pub fn new_dvec2() -> Self {
Expand Down
26 changes: 16 additions & 10 deletions codegen/templates/vec.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Generated from {{template_path}} template. Edit the template, not the generated file.

{% if is_scalar %}
{% if is_scalar and not is_align %}
{% set mask_t = "BVec" ~ dim %}
{% else %}
{% set is_simd = true %}
Expand All @@ -12,6 +12,8 @@
{% set simd_t = "v128" %}
{% elif is_coresimd %}
{% set simd_t = "f32x4" %}
{% else %}
{% set is_simd = false %}
{% endif %}
{% set mask_t = "BVec" ~ dim ~ "A" %}
{% endif %}
Expand All @@ -20,7 +22,7 @@
{% set is_signed = true %}
{% set is_float = true %}
{% if scalar_t == "f32" %}
{% if dim == 3 and is_simd or is_align %}
{% if dim == 3 and is_align %}
{% set self_t = "Vec3A" %}
{% set mask_t = "BVec3A" %}
{% else %}
Expand Down Expand Up @@ -120,8 +122,16 @@
{% set zero = "0" %}
{% endif %}

use crate::{
{{ mask_t }},
{% if mask_t == "BVec4A" and scalar_t == "f32" and is_scalar %}
#[cfg(feature = "scalar-math")]
use crate::BVec4 as BVec4A;
#[cfg(not(feature = "scalar-math"))]
use crate::BVec4A;
use crate::{
{% else %}
use crate::{
{{ mask_t }},
{% endif %}
{% if self_t != vec2_t %}
{{ vec2_t }},
{% endif %}
Expand Down Expand Up @@ -373,11 +383,7 @@ impl {{ self_t }} {
{% if is_scalar %}
Self {
{% for c in components %}
{%- if is_align %}
{{ c }}: if mask.{{ c }} != 0 { if_true.{{ c }} } else { if_false.{{ c }} },
{%- else %}
{{ c }}: if mask.{{ c }} { if_true.{{ c }} } else { if_false.{{ c }} },
{%- endif %}
{{ c }}: if mask.test({{ loop.index0 }}) { if_true.{{ c }} } else { if_false.{{ c }} },
{%- endfor %}
}
{% elif is_sse2 %}
Expand Down Expand Up @@ -513,7 +519,7 @@ impl {{ self_t }} {
{% if is_sse2 %}
Self(unsafe { dot{{ dim }}_into_m128(self.0, rhs.0) })
{% elif is_wasm32 %}
Self(unsafe { dot{{ dim }}_into_v128(self.0, rhs.0) })
Self(dot{{ dim }}_into_v128(self.0, rhs.0))
{% elif is_coresimd %}
Self(dot{{ dim }}_into_f32x4(self.0, rhs.0))
{% else %}
Expand Down
6 changes: 3 additions & 3 deletions src/f32/scalar/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ impl Vec3A {
#[inline]
pub fn select(mask: BVec3A, if_true: Self, if_false: Self) -> Self {
Self {
x: if mask.x != 0 { if_true.x } else { if_false.x },
y: if mask.y != 0 { if_true.y } else { if_false.y },
z: if mask.z != 0 { if_true.z } else { if_false.z },
x: if mask.test(0) { if_true.x } else { if_false.x },
y: if mask.test(1) { if_true.y } else { if_false.y },
z: if mask.test(2) { if_true.z } else { if_false.z },
}
}

Expand Down
44 changes: 24 additions & 20 deletions src/f32/scalar/vec4.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Generated from vec.rs.tera template. Edit the template, not the generated file.

use crate::{f32::math, BVec4, Vec2, Vec3, Vec3A};
#[cfg(feature = "scalar-math")]
use crate::BVec4 as BVec4A;
#[cfg(not(feature = "scalar-math"))]
use crate::BVec4A;
use crate::{f32::math, Vec2, Vec3, Vec3A};

#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down Expand Up @@ -109,12 +113,12 @@ impl Vec4 {
/// A true element in the mask uses the corresponding element from `if_true`, and false
/// uses the element from `if_false`.
#[inline]
pub fn select(mask: BVec4, if_true: Self, if_false: Self) -> Self {
pub fn select(mask: BVec4A, if_true: Self, if_false: Self) -> Self {
Self {
x: if mask.x { if_true.x } else { if_false.x },
y: if mask.y { if_true.y } else { if_false.y },
z: if mask.z { if_true.z } else { if_false.z },
w: if mask.w { if_true.w } else { if_false.w },
x: if mask.test(0) { if_true.x } else { if_false.x },
y: if mask.test(1) { if_true.y } else { if_false.y },
z: if mask.test(2) { if_true.z } else { if_false.z },
w: if mask.test(3) { if_true.w } else { if_false.w },
}
}

Expand Down Expand Up @@ -237,8 +241,8 @@ impl Vec4 {
/// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all
/// elements.
#[inline]
pub fn cmpeq(self, rhs: Self) -> BVec4 {
BVec4::new(
pub fn cmpeq(self, rhs: Self) -> BVec4A {
BVec4A::new(
self.x.eq(&rhs.x),
self.y.eq(&rhs.y),
self.z.eq(&rhs.z),
Expand All @@ -252,8 +256,8 @@ impl Vec4 {
/// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all
/// elements.
#[inline]
pub fn cmpne(self, rhs: Self) -> BVec4 {
BVec4::new(
pub fn cmpne(self, rhs: Self) -> BVec4A {
BVec4A::new(
self.x.ne(&rhs.x),
self.y.ne(&rhs.y),
self.z.ne(&rhs.z),
Expand All @@ -267,8 +271,8 @@ impl Vec4 {
/// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all
/// elements.
#[inline]
pub fn cmpge(self, rhs: Self) -> BVec4 {
BVec4::new(
pub fn cmpge(self, rhs: Self) -> BVec4A {
BVec4A::new(
self.x.ge(&rhs.x),
self.y.ge(&rhs.y),
self.z.ge(&rhs.z),
Expand All @@ -282,8 +286,8 @@ impl Vec4 {
/// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all
/// elements.
#[inline]
pub fn cmpgt(self, rhs: Self) -> BVec4 {
BVec4::new(
pub fn cmpgt(self, rhs: Self) -> BVec4A {
BVec4A::new(
self.x.gt(&rhs.x),
self.y.gt(&rhs.y),
self.z.gt(&rhs.z),
Expand All @@ -297,8 +301,8 @@ impl Vec4 {
/// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all
/// elements.
#[inline]
pub fn cmple(self, rhs: Self) -> BVec4 {
BVec4::new(
pub fn cmple(self, rhs: Self) -> BVec4A {
BVec4A::new(
self.x.le(&rhs.x),
self.y.le(&rhs.y),
self.z.le(&rhs.z),
Expand All @@ -312,8 +316,8 @@ impl Vec4 {
/// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all
/// elements.
#[inline]
pub fn cmplt(self, rhs: Self) -> BVec4 {
BVec4::new(
pub fn cmplt(self, rhs: Self) -> BVec4A {
BVec4A::new(
self.x.lt(&rhs.x),
self.y.lt(&rhs.y),
self.z.lt(&rhs.z),
Expand Down Expand Up @@ -387,8 +391,8 @@ impl Vec4 {
///
/// In other words, this computes `[x.is_nan(), y.is_nan(), z.is_nan(), w.is_nan()]`.
#[inline]
pub fn is_nan_mask(self) -> BVec4 {
BVec4::new(
pub fn is_nan_mask(self) -> BVec4A {
BVec4A::new(
self.x.is_nan(),
self.y.is_nan(),
self.z.is_nan(),
Expand Down
4 changes: 2 additions & 2 deletions src/f32/vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ impl Vec2 {
#[inline]
pub fn select(mask: BVec2, if_true: Self, if_false: Self) -> Self {
Self {
x: if mask.x { if_true.x } else { if_false.x },
y: if mask.y { if_true.y } else { if_false.y },
x: if mask.test(0) { if_true.x } else { if_false.x },
y: if mask.test(1) { if_true.y } else { if_false.y },
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/f32/vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ impl Vec3 {
#[inline]
pub fn select(mask: BVec3, if_true: Self, if_false: Self) -> Self {
Self {
x: if mask.x { if_true.x } else { if_false.x },
y: if mask.y { if_true.y } else { if_false.y },
z: if mask.z { if_true.z } else { if_false.z },
x: if mask.test(0) { if_true.x } else { if_false.x },
y: if mask.test(1) { if_true.y } else { if_false.y },
z: if mask.test(2) { if_true.z } else { if_false.z },
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/f32/wasm32/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl Vec3A {
/// Returns a vector where every component is the dot product of `self` and `rhs`.
#[inline]
pub fn dot_into_vec(self, rhs: Self) -> Self {
Self(unsafe { dot3_into_v128(self.0, rhs.0) })
Self(dot3_into_v128(self.0, rhs.0))
}

/// Computes the cross product of `self` and `rhs`.
Expand Down
2 changes: 1 addition & 1 deletion src/f32/wasm32/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl Vec4 {
/// Returns a vector where every component is the dot product of `self` and `rhs`.
#[inline]
pub fn dot_into_vec(self, rhs: Self) -> Self {
Self(unsafe { dot4_into_v128(self.0, rhs.0) })
Self(dot4_into_v128(self.0, rhs.0))
}

/// Returns a vector containing the minimum values for each element of `self` and `rhs`.
Expand Down
4 changes: 2 additions & 2 deletions src/f64/dvec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ impl DVec2 {
#[inline]
pub fn select(mask: BVec2, if_true: Self, if_false: Self) -> Self {
Self {
x: if mask.x { if_true.x } else { if_false.x },
y: if mask.y { if_true.y } else { if_false.y },
x: if mask.test(0) { if_true.x } else { if_false.x },
y: if mask.test(1) { if_true.y } else { if_false.y },
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/f64/dvec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ impl DVec3 {
#[inline]
pub fn select(mask: BVec3, if_true: Self, if_false: Self) -> Self {
Self {
x: if mask.x { if_true.x } else { if_false.x },
y: if mask.y { if_true.y } else { if_false.y },
z: if mask.z { if_true.z } else { if_false.z },
x: if mask.test(0) { if_true.x } else { if_false.x },
y: if mask.test(1) { if_true.y } else { if_false.y },
z: if mask.test(2) { if_true.z } else { if_false.z },
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/f64/dvec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ impl DVec4 {
#[inline]
pub fn select(mask: BVec4, if_true: Self, if_false: Self) -> Self {
Self {
x: if mask.x { if_true.x } else { if_false.x },
y: if mask.y { if_true.y } else { if_false.y },
z: if mask.z { if_true.z } else { if_false.z },
w: if mask.w { if_true.w } else { if_false.w },
x: if mask.test(0) { if_true.x } else { if_false.x },
y: if mask.test(1) { if_true.y } else { if_false.y },
z: if mask.test(2) { if_true.z } else { if_false.z },
w: if mask.test(3) { if_true.w } else { if_false.w },
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/i16/i16vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ impl I16Vec2 {
#[inline]
pub fn select(mask: BVec2, if_true: Self, if_false: Self) -> Self {
Self {
x: if mask.x { if_true.x } else { if_false.x },
y: if mask.y { if_true.y } else { if_false.y },
x: if mask.test(0) { if_true.x } else { if_false.x },
y: if mask.test(1) { if_true.y } else { if_false.y },
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/i16/i16vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ impl I16Vec3 {
#[inline]
pub fn select(mask: BVec3, if_true: Self, if_false: Self) -> Self {
Self {
x: if mask.x { if_true.x } else { if_false.x },
y: if mask.y { if_true.y } else { if_false.y },
z: if mask.z { if_true.z } else { if_false.z },
x: if mask.test(0) { if_true.x } else { if_false.x },
y: if mask.test(1) { if_true.y } else { if_false.y },
z: if mask.test(2) { if_true.z } else { if_false.z },
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/i16/i16vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ impl I16Vec4 {
#[inline]
pub fn select(mask: BVec4, if_true: Self, if_false: Self) -> Self {
Self {
x: if mask.x { if_true.x } else { if_false.x },
y: if mask.y { if_true.y } else { if_false.y },
z: if mask.z { if_true.z } else { if_false.z },
w: if mask.w { if_true.w } else { if_false.w },
x: if mask.test(0) { if_true.x } else { if_false.x },
y: if mask.test(1) { if_true.y } else { if_false.y },
z: if mask.test(2) { if_true.z } else { if_false.z },
w: if mask.test(3) { if_true.w } else { if_false.w },
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/i32/ivec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ impl IVec2 {
#[inline]
pub fn select(mask: BVec2, if_true: Self, if_false: Self) -> Self {
Self {
x: if mask.x { if_true.x } else { if_false.x },
y: if mask.y { if_true.y } else { if_false.y },
x: if mask.test(0) { if_true.x } else { if_false.x },
y: if mask.test(1) { if_true.y } else { if_false.y },
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/i32/ivec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ impl IVec3 {
#[inline]
pub fn select(mask: BVec3, if_true: Self, if_false: Self) -> Self {
Self {
x: if mask.x { if_true.x } else { if_false.x },
y: if mask.y { if_true.y } else { if_false.y },
z: if mask.z { if_true.z } else { if_false.z },
x: if mask.test(0) { if_true.x } else { if_false.x },
y: if mask.test(1) { if_true.y } else { if_false.y },
z: if mask.test(2) { if_true.z } else { if_false.z },
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/i32/ivec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ impl IVec4 {
#[inline]
pub fn select(mask: BVec4, if_true: Self, if_false: Self) -> Self {
Self {
x: if mask.x { if_true.x } else { if_false.x },
y: if mask.y { if_true.y } else { if_false.y },
z: if mask.z { if_true.z } else { if_false.z },
w: if mask.w { if_true.w } else { if_false.w },
x: if mask.test(0) { if_true.x } else { if_false.x },
y: if mask.test(1) { if_true.y } else { if_false.y },
z: if mask.test(2) { if_true.z } else { if_false.z },
w: if mask.test(3) { if_true.w } else { if_false.w },
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/i64/i64vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ impl I64Vec2 {
#[inline]
pub fn select(mask: BVec2, if_true: Self, if_false: Self) -> Self {
Self {
x: if mask.x { if_true.x } else { if_false.x },
y: if mask.y { if_true.y } else { if_false.y },
x: if mask.test(0) { if_true.x } else { if_false.x },
y: if mask.test(1) { if_true.y } else { if_false.y },
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/i64/i64vec3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ impl I64Vec3 {
#[inline]
pub fn select(mask: BVec3, if_true: Self, if_false: Self) -> Self {
Self {
x: if mask.x { if_true.x } else { if_false.x },
y: if mask.y { if_true.y } else { if_false.y },
z: if mask.z { if_true.z } else { if_false.z },
x: if mask.test(0) { if_true.x } else { if_false.x },
y: if mask.test(1) { if_true.y } else { if_false.y },
z: if mask.test(2) { if_true.z } else { if_false.z },
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/i64/i64vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ impl I64Vec4 {
#[inline]
pub fn select(mask: BVec4, if_true: Self, if_false: Self) -> Self {
Self {
x: if mask.x { if_true.x } else { if_false.x },
y: if mask.y { if_true.y } else { if_false.y },
z: if mask.z { if_true.z } else { if_false.z },
w: if mask.w { if_true.w } else { if_false.w },
x: if mask.test(0) { if_true.x } else { if_false.x },
y: if mask.test(1) { if_true.y } else { if_false.y },
z: if mask.test(2) { if_true.z } else { if_false.z },
w: if mask.test(3) { if_true.w } else { if_false.w },
}
}

Expand Down
Loading

0 comments on commit bfaa2ac

Please sign in to comment.