mborgerding / kissfft

a Fast Fourier Transform (FFT) library that tries to Keep it Simple, Stupid
Other
1.46k stars 283 forks source link

mis-aligned sub-allocation in kiss_fftnd_alloc #40

Closed bsupnik closed 4 years ago

bsupnik commented 4 years ago

I'm not sure if this is a known issue, but if I call kiss_fftnd_alloc with a 256x256 iFFT and USE_SIMD set to 1, I end up with misaligned memory.

I've traced the bug to kiss_fftnd_alloc packing several things into a single buffer and losing the 16-byte alignment of the parent block.

If this is not a known issue, I can attempt to fix this and submit a pull request. My plan was to add padding to the hand-built sub-allocation layout to put the actual numeric data back into the alignment of the parent block.

gvanem commented 3 years ago

Hey I was hit by this crash on Windows (with MSVC and clang-cl). My fix was simply:

--- a/kiss_fftnd.c 2021-03-02 19:54:51
+++ b/kiss_fftnd.c 2021-03-03 14:14:53
@@ -38,7 +38,7 @@
     memneeded += KISS_FFT_ALIGN_SIZE_UP(sizeof(kiss_fft_cpx) * dimprod); /* st->tmpbuf */

     if (lenmem == NULL) {/* allocate for the caller*/
-        ptr = (char *) malloc (memneeded);
+        ptr = (char *) KISS_FFT_MALLOC (memneeded);
     } else { /* initialize supplied buffer if big enough */
         if (*lenmem >= memneeded)
             ptr = (char *) mem;

With this change, Windows' _aligned_free(ptr) is happy; no more runtime aborts.