sfmt icon indicating copy to clipboard operation
sfmt copied to clipboard

Support for `wasm32-unknown-unknown` target

Open odanado opened this issue 1 year ago • 0 comments

Background

When compiling a project that depends on this library with the --target wasm32-unknown-unknown option, the following error occurs:

error[E0412]: cannot find type `__m128i` in this scope
 --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/packed.rs:9:25
  |
9 | pub(crate) type i32x4 = __m128i;
  |                         ^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_set_epi32` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/packed.rs:12:14
   |
12 |     unsafe { _mm_set_epi32(e3, e2, e1, e0) }
   |              ^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_setzero_si128` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/packed.rs:16:14
   |
16 |     unsafe { _mm_setzero_si128() }
   |              ^^^^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_extract_epi32` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/packed.rs:22:18
   |
22 |             0 => _mm_extract_epi32(vals, 0) as u32,
   |                  ^^^^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_extract_epi32` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/packed.rs:23:18
   |
23 |             1 => _mm_extract_epi32(vals, 1) as u32,
   |                  ^^^^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_extract_epi32` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/packed.rs:24:18
   |
24 |             2 => _mm_extract_epi32(vals, 2) as u32,
   |                  ^^^^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_extract_epi32` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/packed.rs:25:18
   |
25 |             3 => _mm_extract_epi32(vals, 3) as u32,
   |                  ^^^^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_insert_epi32` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/packed.rs:34:18
   |
34 |             0 => _mm_insert_epi32(*vals, val, 0),
   |                  ^^^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_insert_epi32` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/packed.rs:35:18
   |
35 |             1 => _mm_insert_epi32(*vals, val, 1),
   |                  ^^^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_insert_epi32` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/packed.rs:36:18
   |
36 |             2 => _mm_insert_epi32(*vals, val, 2),
   |                  ^^^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_insert_epi32` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/packed.rs:37:18
   |
37 |             3 => _mm_insert_epi32(*vals, val, 3),
   |                  ^^^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_srli_epi32` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/sfmt.rs:32:17
   |
32 |         let y = _mm_srli_epi32(b, SFMT_SR1);
   |                 ^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_srli_si128` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/sfmt.rs:33:17
   |
33 |         let z = _mm_srli_si128(c, SFMT_SR2);
   |                 ^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_slli_epi32` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/sfmt.rs:34:17
   |
34 |         let v = _mm_slli_epi32(d, SFMT_SL1);
   |                 ^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_xor_si128` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/sfmt.rs:35:17
   |
35 |         let z = _mm_xor_si128(z, a);
   |                 ^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_xor_si128` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/sfmt.rs:36:17
   |
36 |         let z = _mm_xor_si128(z, v);
   |                 ^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_slli_si128` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/sfmt.rs:37:17
   |
37 |         let x = _mm_slli_si128(a, SFMT_SL2);
   |                 ^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_and_si128` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/sfmt.rs:38:17
   |
38 |         let y = _mm_and_si128(y, mask);
   |                 ^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_xor_si128` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/sfmt.rs:39:17
   |
39 |         let z = _mm_xor_si128(z, x);
   |                 ^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `_mm_xor_si128` in this scope
  --> /Users/odan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sfmt-0.7.0/src/sfmt.rs:40:9
   |
40 |         _mm_xor_si128(z, y)
   |         ^^^^^^^^^^^^^ not found in this scope

This error occurs because functions starting with _mm_ are not defined when target_arch is wasm32.

Motivation

By supporting wasm32, this library can be used in edge environments such as Cloudflare Workers.

Solution

To solve this issue, I have an idea to define these types and functions using core::arch::wasm32.

This idea is inspired by the article Authoring a SIMD enhanced Wasm library with Rust.

I am very interested in adding wasm32 support. I plan to use core::arch::wasm32 to define the necessary types and functions to achieve this. Here is a brief outline of the code I intend to implement:

#[cfg(target_arch = "wasm32")]
type i32x4 = v128;

#[cfg(target_arch = "wasm32")]
unsafe fn _mm_setzero_si128() -> v128 {
  return i32x4(0, 0, 0, 0);
}

Would it be possible for me to create a pull request to add this support?

odanado avatar Jun 25 '24 15:06 odanado