Alignment check in WebAssembly when emulating XMM intrinsics?

Viewed 163

I'm trying to compile the PFFFT library (https://bitbucket.org/jpommier/) to WebAssembly using the -msse option to emulate the SSE intrinsics. Compilation works fine, but there's an alignment check that fails at runtime.

The alignment check macro that fails is:

# define VALIGNED(ptr) ((((long)(ptr)) & 0xF) == 0)

I'm a bit lost here ... does anyone have a hint on how to perform the alignment check with WASM SIMD, or whether it's even necessary ?

Best, n

1 Answers

I assume you're writing C and compiling to WASM - it's not totally clear to me if you're also calling the PFFFT library from C.

Reading the PFFFT-code at lines 1622 + 1707, only two places could fail the macro you're describing (VALIGNED(ptr)):

  1608  void pffft_transform_internal(PFFFT_Setup *setup, const float *finput, float *foutput, v4sf *scratch,
  1609                               pffft_direction_t direction, int ordered) {
   ...
  1622    assert(VALIGNED(finput) && VALIGNED(foutput));
                 ^^^^^^^^^^^^^^^^    ^^^^^^^^^^^^^^^^^
  1623  
   ...

and

  1677  void pffft_zconvolve_accumulate(PFFFT_Setup *s, const float *a, const float *b, float *ab, float scaling) {
   ...
  1707    assert(VALIGNED(a) && VALIGNED(b) && VALIGNED(ab));
                 ^^^^^^^^^^^    ^^^^^^^^^^^    ^^^^^^^^^^^^
   ...

So the "fault" can be placed at the caller(s) of pffft_transform_internal(...) and pffft_zconvolve_accumulate(...) :) The input pointers aren't correctly aligned.

If you are allocating the float-arrays yourself, are you using aligned_alloc() or emulating it yourself? (it's quick and easy! :))

According to @Peter Cordes you could also consider testing with the macros removed/disabled.

Related