Cykooz / fast_image_resize

Rust library for fast image resizing with using of SIMD instructions.
Apache License 2.0
310 stars 29 forks source link

Add wasm32 #11

Closed cdmurph32 closed 1 year ago

cdmurph32 commented 1 year ago

Methodology:

  1. Replace sse4 commands with wasm32 equivalents (https://emscripten.org/docs/porting/simd.html)
  2. Reverse shuffle array order.
  3. Improve efficiency a. Declare shuffle arrays as constants to avoid extra bounds checking instructions. b. Use f32x4_pmin instead of f32x4_min

Issues encountered:

  1. With Wasm, the SIMD instructions are determined at runtime depending on the system used. On x86 sse4 _mm_cvtps_epi32 converts inf to i32::MIN or 2_147_483_648u32, while its wasm32 equivalent (on AVX), u32x4_trunc_sat_f32x4, converts inf to u32::MAX. i32x4_trunc_sat_f32x4 converts inf to i32::MAX and cannot be used. This is corrected by capping the result of u32x4_trunc_sat_f32x4 at 2_147_483_648, but introduces an additional instruction.
  2. glassbench uses chrono instead of chrono-wasi with the wasm32-wasi target and cannot currently be used.

Test command: CARGO_TARGET_WASM32_WASI_RUNNER="wasmtime --dir=." cargo wasi test -- --nocapture

cdmurph32 commented 1 year ago

FYI Glassbench does not work in webassembly for a multitude of reasons. I'm playing around with benchmark-simple, which works with WASM, but I don't know if that would be be something that should be merged.

Cykooz commented 1 year ago

Thank you for your work. I will check it when I have more free time.

cdmurph32 commented 1 year ago

I removed the simple benchmark stuff. Looks good!

Cykooz commented 1 year ago

How run benchmarks? I've got many errors:

[wasm-validator error in function 2164] unexpected false: SIMD operation (SIMD is disabled), on 
(v128.store align=1
 (local.get $1)
 (v128.load offset=1351328 align=1
  (i32.const 0)
 )
)

I use command:

CARGO_TARGET_WASM32_WASI_RUNNER="wasmtime --dir=. --wasm-features all" cargo wasi bench --bench bench_resize
cdmurph32 commented 1 year ago

How run benchmarks? I've got many errors:

[wasm-validator error in function 2164] unexpected false: SIMD operation (SIMD is disabled), on 
(v128.store align=1
 (local.get $1)
 (v128.load offset=1351328 align=1
  (i32.const 0)
 )
)

I use command:

CARGO_TARGET_WASM32_WASI_RUNNER="wasmtime --dir=. --wasm-features all" cargo wasi bench --bench bench_resize

CARGO_TARGET_WASM32_WASI_RUNNER="wasmtime --dir=." cargo bench .

cdmurph32 commented 1 year ago

Thank you so much!