kiss_fftnd, kiss_fftndr: align every object packed into the state block

kiss_fftnd_alloc and kiss_fftndr_alloc carve several objects out of one
allocation, advancing by KISS_FFT_ALIGN_SIZE_UP(size), which is the
identity outside SIMD builds. So objects started at whatever offset the
previous one ended:

- kiss_fftnd: after an odd number of int dims, tmpbuf and the
  per-dimension kiss_fft_state objects were 4 bytes off, misaligned for
  double.
- kiss_fftndr: the nd state (which holds pointers) followed the real-FFT
  state, whose size is only a multiple of 4 for int16_t.

These are misaligned accesses (undefined behavior; SIGBUS on strict
platforms, silent on x86). Add KISS_FFT_PACK_SIZE_UP to
_kiss_fft_guts.h, which rounds a size up to a multiple of the size of a
union of pointer, int and kiss_fft_cpx (a multiple of each of their
alignments, and of the SIMD vector alignment), and use it for every
object in both layouts. The reported sizes grow by a few bytes of padding.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01ME1hnNaoD3ZcMj9H8TSYkZ
This commit is contained in:
Davide Faconti
2026-11-15 20:03:51 +01:00
co-authored by Claude Opus 5.5
parent 933c235c1c
commit c2f367e286
6 changed files with 220 additions and 16 deletions
+10 -10
View File
@@ -24,7 +24,7 @@ kiss_fftnd_cfg kiss_fftnd_alloc(const int *dims,int ndims,int inverse_fft,void*m
kiss_fftnd_cfg st = NULL;
int i;
int dimprod=1;
size_t memneeded = KISS_FFT_ALIGN_SIZE_UP(sizeof(struct kiss_fftnd_state));
size_t memneeded = KISS_FFT_PACK_SIZE_UP(sizeof(struct kiss_fftnd_state));
char * ptr = NULL;
for (i=0;i<ndims;++i) {
@@ -37,12 +37,12 @@ kiss_fftnd_cfg kiss_fftnd_alloc(const int *dims,int ndims,int inverse_fft,void*m
return NULL;
}
kiss_fft_alloc (dims[i], inverse_fft, NULL, &sublen);
memneeded += sublen; /* st->states[i] */
memneeded += KISS_FFT_PACK_SIZE_UP(sublen); /* st->states[i] */
dimprod *= dims[i];
}
memneeded += KISS_FFT_ALIGN_SIZE_UP(sizeof(int) * ndims);/* st->dims */
memneeded += KISS_FFT_ALIGN_SIZE_UP(sizeof(void*) * ndims);/* st->states */
memneeded += KISS_FFT_ALIGN_SIZE_UP(sizeof(kiss_fft_cpx) * dimprod); /* st->tmpbuf */
memneeded += KISS_FFT_PACK_SIZE_UP(sizeof(int) * ndims);/* st->dims */
memneeded += KISS_FFT_PACK_SIZE_UP(sizeof(void*) * ndims);/* st->states */
memneeded += KISS_FFT_PACK_SIZE_UP(sizeof(kiss_fft_cpx) * dimprod); /* st->tmpbuf */
if (lenmem == NULL) {/* allocate for the caller*/
ptr = (char *) malloc (memneeded);
@@ -57,23 +57,23 @@ kiss_fftnd_cfg kiss_fftnd_alloc(const int *dims,int ndims,int inverse_fft,void*m
st = (kiss_fftnd_cfg) ptr;
st->dimprod = dimprod;
st->ndims = ndims;
ptr += KISS_FFT_ALIGN_SIZE_UP(sizeof(struct kiss_fftnd_state));
ptr += KISS_FFT_PACK_SIZE_UP(sizeof(struct kiss_fftnd_state));
st->states = (kiss_fft_cfg *)ptr;
ptr += KISS_FFT_ALIGN_SIZE_UP(sizeof(void*) * ndims);
ptr += KISS_FFT_PACK_SIZE_UP(sizeof(void*) * ndims);
st->dims = (int*)ptr;
ptr += KISS_FFT_ALIGN_SIZE_UP(sizeof(int) * ndims);
ptr += KISS_FFT_PACK_SIZE_UP(sizeof(int) * ndims);
st->tmpbuf = (kiss_fft_cpx*)ptr;
ptr += KISS_FFT_ALIGN_SIZE_UP(sizeof(kiss_fft_cpx) * dimprod);
ptr += KISS_FFT_PACK_SIZE_UP(sizeof(kiss_fft_cpx) * dimprod);
for (i=0;i<ndims;++i) {
size_t len;
st->dims[i] = dims[i];
kiss_fft_alloc (st->dims[i], inverse_fft, NULL, &len);
st->states[i] = kiss_fft_alloc (st->dims[i], inverse_fft, ptr,&len);
ptr += len;
ptr += KISS_FFT_PACK_SIZE_UP(len);
}
/*
Hi there!