Skip to content

Commit b3ba640

Browse files
authored
Support WebAssembly SIMD instructions
1 parent 48d21df commit b3ba640

File tree

6 files changed

+232
-20
lines changed

6 files changed

+232
-20
lines changed

.cargo/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.wasm32-wasi]
2+
runner = "wasmtime run --wasm-features all --dir ."

.github/workflows/main.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,36 @@ jobs:
5353
env:
5454
RUSTFLAGS: -Ctarget-feature=+avx2
5555
run: cargo test --verbose --all-features
56+
57+
wasm:
58+
runs-on: ubuntu-18.04
59+
strategy:
60+
matrix:
61+
rust:
62+
- stable
63+
steps:
64+
- name: Checkout
65+
uses: actions/checkout@v2
66+
67+
- name: Install toolchain
68+
uses: actions-rs/toolchain@v1
69+
with:
70+
toolchain: ${{ matrix.rust }}
71+
override: true
72+
target: wasm32-wasi
73+
74+
- name: Install wasmtime
75+
run: |
76+
curl https://wasmtime.dev/install.sh -sSf | bash
77+
echo "$HOME/.wasmtime/bin" >> $GITHUB_PATH
78+
79+
- name: Build with minimal features (no_std)
80+
run: cargo build --target wasm32-wasi --verbose --no-default-features --features libm
81+
82+
- name: Run tests without SIMD
83+
run: cargo test --target wasm32-wasi --verbose --no-default-features --features png-format
84+
85+
- name: Run tests with SIMD128
86+
env:
87+
RUSTFLAGS: -Ctarget-feature=+simd128,+bulk-memory,+nontrapping-fptoint,+sign-ext
88+
run: cargo test --target wasm32-wasi --verbose --all-features

src/wide/f32x4_t.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ cfg_if::cfg_if! {
1414
#[derive(Default, Clone, Copy, PartialEq, Debug)]
1515
#[repr(C, align(16))]
1616
pub struct f32x4(m128);
17+
} else if #[cfg(all(feature = "simd", target_feature = "simd128"))] {
18+
use core::arch::wasm32::*;
19+
20+
// repr(transparent) allows for directly passing the v128 on the WASM stack.
21+
#[derive(Clone, Copy, Debug)]
22+
#[repr(transparent)]
23+
pub struct f32x4(v128);
24+
25+
impl Default for f32x4 {
26+
fn default() -> Self {
27+
Self::splat(0.0)
28+
}
29+
}
30+
31+
impl PartialEq for f32x4 {
32+
fn eq(&self, other: &Self) -> bool {
33+
u32x4_all_true(f32x4_eq(self.0, other.0))
34+
}
35+
}
1736
} else {
1837
#[derive(Default, Clone, Copy, PartialEq, Debug)]
1938
#[repr(C, align(16))]
@@ -33,6 +52,8 @@ impl f32x4 {
3352
cfg_if::cfg_if! {
3453
if #[cfg(all(feature = "simd", target_feature = "sse"))] {
3554
Self(max_m128(self.0, rhs.0))
55+
} else if #[cfg(all(feature = "simd", target_feature = "simd128"))] {
56+
Self(f32x4_max(self.0, rhs.0))
3657
} else {
3758
Self([
3859
self.0[0].max(rhs.0[0]),
@@ -48,6 +69,8 @@ impl f32x4 {
4869
cfg_if::cfg_if! {
4970
if #[cfg(all(feature = "simd", target_feature = "sse"))] {
5071
Self(min_m128(self.0, rhs.0))
72+
} else if #[cfg(all(feature = "simd", target_feature = "simd128"))] {
73+
Self(f32x4_min(self.0, rhs.0))
5174
} else {
5275
Self([
5376
self.0[0].min(rhs.0[0]),
@@ -79,6 +102,8 @@ impl core::ops::Add for f32x4 {
79102
cfg_if::cfg_if! {
80103
if #[cfg(all(feature = "simd", target_feature = "sse"))] {
81104
Self(add_m128(self.0, rhs.0))
105+
} else if #[cfg(all(feature = "simd", target_feature = "simd128"))] {
106+
Self(f32x4_add(self.0, rhs.0))
82107
} else {
83108
Self([
84109
self.0[0] + rhs.0[0],
@@ -104,6 +129,8 @@ impl core::ops::Sub for f32x4 {
104129
cfg_if::cfg_if! {
105130
if #[cfg(all(feature = "simd", target_feature = "sse"))] {
106131
Self(sub_m128(self.0, rhs.0))
132+
} else if #[cfg(all(feature = "simd", target_feature = "simd128"))] {
133+
Self(f32x4_sub(self.0, rhs.0))
107134
} else {
108135
Self([
109136
self.0[0] - rhs.0[0],
@@ -123,6 +150,8 @@ impl core::ops::Mul for f32x4 {
123150
cfg_if::cfg_if! {
124151
if #[cfg(all(feature = "simd", target_feature = "sse"))] {
125152
Self(mul_m128(self.0, rhs.0))
153+
} else if #[cfg(all(feature = "simd", target_feature = "simd128"))] {
154+
Self(f32x4_mul(self.0, rhs.0))
126155
} else {
127156
Self([
128157
self.0[0] * rhs.0[0],

0 commit comments

Comments
 (0)