Skip to content

Commit

Permalink
Removed some unnecessary unsafe in wasm32.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitshifter committed May 26, 2024
1 parent 1345e05 commit 636add0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
6 changes: 4 additions & 2 deletions codegen/templates/vec.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ use std::simd::StdFloat;
use core::arch::aarch64::*;
{% endif %}

{% if is_sse2 or is_wasm32 or is_neon %}
{% if is_sse2 or is_neon %}
#[repr(C)]
union UnionCast {
a: [f32; 4],
Expand Down Expand Up @@ -402,6 +402,8 @@ impl {{ self_t }} {
{{ c }}: v,
{% endfor %}
}
{% elif is_wasm32 %}
Self(f32x4(v, v, v, v))
{% elif is_coresimd %}
Self(Simd::from_array([v; 4]))
{% else %}
Expand Down Expand Up @@ -1233,7 +1235,7 @@ impl {{ self_t }} {
Self(unsafe { _mm_or_ps(_mm_and_ps(rhs.0, mask.0), _mm_andnot_ps(mask.0, self.0)) })
{% elif is_wasm32 %}
let mask = Self::splat(-0.0);
Self(unsafe { v128_or(v128_and(rhs.0, mask.0), v128_andnot(self.0, mask.0)) })
Self(v128_or(v128_and(rhs.0, mask.0), v128_andnot(self.0, mask.0)))
{% elif is_neon %}
let mask = Self::splat(-0.0);
Self(unsafe {
Expand Down
13 changes: 5 additions & 8 deletions src/f32/wasm32/vec3a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ use core::{f32, ops::*};

use core::arch::wasm32::*;

#[repr(C)]
union UnionCast {
a: [f32; 4],
v: Vec3A,
}

/// Creates a 3-dimensional vector.
#[inline(always)]
#[must_use]
Expand Down Expand Up @@ -92,7 +86,7 @@ impl Vec3A {
#[inline]
#[must_use]
pub const fn splat(v: f32) -> Self {
unsafe { UnionCast { a: [v; 4] }.v }
Self(f32x4(v, v, v, v))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
Expand Down Expand Up @@ -389,7 +383,10 @@ impl Vec3A {
#[must_use]
pub fn copysign(self, rhs: Self) -> Self {
let mask = Self::splat(-0.0);
Self(unsafe { v128_or(v128_and(rhs.0, mask.0), v128_andnot(self.0, mask.0)) })
Self(v128_or(
v128_and(rhs.0, mask.0),
v128_andnot(self.0, mask.0),
))
}

/// Returns a bitmask with the lowest 3 bits set to the sign bits from the elements of `self`.
Expand Down
13 changes: 5 additions & 8 deletions src/f32/wasm32/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ use core::{f32, ops::*};

use core::arch::wasm32::*;

#[repr(C)]
union UnionCast {
a: [f32; 4],
v: Vec4,
}

/// Creates a 4-dimensional vector.
#[inline(always)]
#[must_use]
Expand Down Expand Up @@ -94,7 +88,7 @@ impl Vec4 {
#[inline]
#[must_use]
pub const fn splat(v: f32) -> Self {
unsafe { UnionCast { a: [v; 4] }.v }
Self(f32x4(v, v, v, v))
}

/// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
Expand Down Expand Up @@ -375,7 +369,10 @@ impl Vec4 {
#[must_use]
pub fn copysign(self, rhs: Self) -> Self {
let mask = Self::splat(-0.0);
Self(unsafe { v128_or(v128_and(rhs.0, mask.0), v128_andnot(self.0, mask.0)) })
Self(v128_or(
v128_and(rhs.0, mask.0),
v128_andnot(self.0, mask.0),
))
}

/// Returns a bitmask with the lowest 4 bits set to the sign bits from the elements of `self`.
Expand Down

0 comments on commit 636add0

Please sign in to comment.