From c2f367e286e2221f7a1593fdd63260335b8058c5 Mon Sep 17 00:00:00 2001 From: Davide Faconti Date: Wed, 11 Nov 2026 09:43:06 +0100 Subject: [PATCH] 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 Claude-Session: https://claude.ai/code/session_01ME1hnNaoD3ZcMj9H8TSYkZ --- _kiss_fft_guts.h | 7 ++ kiss_fftnd.c | 20 ++--- kiss_fftndr.c | 8 +- test/CMakeLists.txt | 2 + test/Makefile | 12 ++- test/test_nd_alignment.c | 187 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 220 insertions(+), 16 deletions(-) create mode 100644 test/test_nd_alignment.c diff --git a/_kiss_fft_guts.h b/_kiss_fft_guts.h index f4b6832..94c33ae 100644 --- a/_kiss_fft_guts.h +++ b/_kiss_fft_guts.h @@ -31,6 +31,13 @@ struct kiss_fft_state{ kiss_fft_cpx twiddles[1]; }; +/* Rounds up the size of each object that kiss_fftnd_alloc / kiss_fftndr_alloc + * pack into one block, so that the next object starts suitably aligned for + * pointers, ints and kiss_fft_cpx (including SIMD vectors). */ +union kiss_fft_pack_align { void *p; int i; kiss_fft_cpx c; }; +#define KISS_FFT_PACK_SIZE_UP(size) \ + (((size) + sizeof(union kiss_fft_pack_align) - 1) / sizeof(union kiss_fft_pack_align) * sizeof(union kiss_fft_pack_align)) + /* Explanation of macros dealing with complex math: diff --git a/kiss_fftnd.c b/kiss_fftnd.c index a7ff84c..fde0c27 100644 --- a/kiss_fftnd.c +++ b/kiss_fftnd.c @@ -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;istates[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;idims[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! diff --git a/kiss_fftndr.c b/kiss_fftndr.c index 52b7d58..d395c05 100644 --- a/kiss_fftndr.c +++ b/kiss_fftndr.c @@ -57,7 +57,7 @@ kiss_fftndr_cfg kiss_fftndr_alloc(const int *dims,int ndims,int inverse_fft,void MAX( 2*dimOther , dimReal+2) * sizeof(kiss_fft_scalar) // freq buffer for one pass + dimOther*(size_t)(dimReal+2) * sizeof(kiss_fft_scalar); // large enough to hold entire input in case of in-place - memneeded = KISS_FFT_ALIGN_SIZE_UP(sizeof( struct kiss_fftndr_state )) + KISS_FFT_ALIGN_SIZE_UP(nr) + KISS_FFT_ALIGN_SIZE_UP(nd) + KISS_FFT_ALIGN_SIZE_UP(ntmp); + memneeded = KISS_FFT_PACK_SIZE_UP(sizeof( struct kiss_fftndr_state )) + KISS_FFT_PACK_SIZE_UP(nr) + KISS_FFT_PACK_SIZE_UP(nd) + KISS_FFT_PACK_SIZE_UP(ntmp); if (lenmem==NULL) { ptr = (char*) malloc(memneeded); @@ -71,14 +71,14 @@ kiss_fftndr_cfg kiss_fftndr_alloc(const int *dims,int ndims,int inverse_fft,void st = (kiss_fftndr_cfg) ptr; memset( st , 0 , memneeded); - ptr += KISS_FFT_ALIGN_SIZE_UP(sizeof(struct kiss_fftndr_state)); + ptr += KISS_FFT_PACK_SIZE_UP(sizeof(struct kiss_fftndr_state)); st->dimReal = dimReal; st->dimOther = dimOther; st->cfg_r = kiss_fftr_alloc( dimReal,inverse_fft,ptr,&nr); - ptr += KISS_FFT_ALIGN_SIZE_UP(nr); + ptr += KISS_FFT_PACK_SIZE_UP(nr); st->cfg_nd = kiss_fftnd_alloc(dims,ndims-1,inverse_fft, ptr,&nd); - ptr += KISS_FFT_ALIGN_SIZE_UP(nd); + ptr += KISS_FFT_PACK_SIZE_UP(nd); st->tmpbuf = ptr; return st; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f6a81be..abde829 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -50,6 +50,8 @@ add_kissfft_test_executable(ffr twotonetest.c) add_kissfft_test_executable(tr test_real.c) add_kissfft_test_executable(tsizeval test_size_validation.c) add_kissfft_test_executable(tndralloc test_fftndr_alloc.c) +# includes ../kiss_fftr.c, ../kiss_fftnd.c and ../kiss_fftndr.c to reach their private structs +add_kissfft_test_executable(tndalign test_nd_alignment.c) add_kissfft_test_executable(next_fast_size next_fast_size.c) diff --git a/test/Makefile b/test/Makefile index 64d7001..6a14f09 100644 --- a/test/Makefile +++ b/test/Makefile @@ -45,6 +45,7 @@ ifneq ($(KISSFFT_OPENMP),1) TESTREAL = tr-$(KISSFFT_DATATYPE) TESTSIZEVAL = tsizeval-$(KISSFFT_DATATYPE) TESTNDRALLOC = tndralloc-$(KISSFFT_DATATYPE) + TESTNDALIGN = tndalign-$(KISSFFT_DATATYPE) TESTKFC = tkfc-$(KISSFFT_DATATYPE) TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE) TESTCPP = testcpp-$(KISSFFT_DATATYPE) @@ -56,6 +57,7 @@ else TESTREAL = tr-$(KISSFFT_DATATYPE)-openmp TESTSIZEVAL = tsizeval-$(KISSFFT_DATATYPE)-openmp TESTNDRALLOC = tndralloc-$(KISSFFT_DATATYPE)-openmp + TESTNDALIGN = tndalign-$(KISSFFT_DATATYPE)-openmp TESTKFC = tkfc-$(KISSFFT_DATATYPE)-openmp TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE)-openmp TESTCPP = testcpp-$(KISSFFT_DATATYPE)-openmp @@ -107,7 +109,7 @@ endif # Target: "make all" # -all: $(BENCHKISS) $(SELFTEST) $(BENCHFFTW) $(TESTREAL) $(TESTSIZEVAL) $(TESTNDRALLOC) $(TESTKFC) $(TESTFASTFILT) +all: $(BENCHKISS) $(SELFTEST) $(BENCHFFTW) $(TESTREAL) $(TESTSIZEVAL) $(TESTNDRALLOC) $(TESTNDALIGN) $(TESTKFC) $(TESTFASTFILT) # # Individual test make rules @@ -128,6 +130,10 @@ $(TESTSIZEVAL): test_size_validation.c $(TESTNDRALLOC): test_fftndr_alloc.c $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm +# includes ../kiss_fftr.c, ../kiss_fftnd.c and ../kiss_fftndr.c to reach their private structs +$(TESTNDALIGN): test_nd_alignment.c ../kiss_fftr.c ../kiss_fftnd.c ../kiss_fftndr.c + $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm + $(BENCHKISS): benchkiss.c pstats.c $(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $^ -L.. -l$(KISSFFTLIB_SHORTNAME) -lm @@ -189,6 +195,8 @@ endif @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTSIZEVAL) $(warning ======kiss_fftndr allocation regression test (type= $(KISSFFT_DATATYPE) )) @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTNDRALLOC) + $(warning ======multi-dimensional FFT state alignment regression test (type= $(KISSFFT_DATATYPE) )) + @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTNDALIGN) $(warning ======timing test (type=$(KISSFFT_DATATYPE))) @LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(BENCHKISS) -x $(NUMFFTS) -n $(NFFT) @[ -x ./$(BENCHFFTW) ] && LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(BENCHFFTW) -x $(NUMFFTS) -n $(NFFT) || true @@ -200,4 +208,4 @@ endif # clean: - rm -f *~ bm-* st-* tr-* tsizeval-* tndralloc-* kf-* tkfc-* ff-* fastfilt-* *.pyc *.pyo *.dat testcpp-* testsimd testsimd-* _test_library_dummy _test_library_dummy.c + rm -f *~ bm-* st-* tr-* tsizeval-* tndralloc-* tndalign-* kf-* tkfc-* ff-* fastfilt-* *.pyc *.pyo *.dat testcpp-* testsimd testsimd-* _test_library_dummy _test_library_dummy.c diff --git a/test/test_nd_alignment.c b/test/test_nd_alignment.c new file mode 100644 index 0000000..4aaea62 --- /dev/null +++ b/test/test_nd_alignment.c @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved. + * This file is part of KISS FFT - https://github.com/mborgerding/kissfft + * + * SPDX-License-Identifier: BSD-3-Clause + * See COPYING file for more information. + */ + +/* Regression test for the memory layout of kiss_fftnd_alloc / kiss_fftndr_alloc. + * + * Both pack several objects into one block. Every object must start at an + * address suitably aligned for its type, otherwise the transforms perform + * misaligned accesses (undefined behavior; a SIGBUS/UBSan error on strict + * platforms, invisible on x86 without sanitizers): + * 1. kiss_fftnd: with an odd number of dimensions the int dims[] array shifts + * tmpbuf and the per-dimension kiss_fft_state objects by 4 bytes, which + * breaks 8-byte aligned types (double) + * 2. kiss_fftndr: the nd state (which contains pointers) follows the real-FFT + * state, whose size need not be a multiple of the pointer size (int16_t) + * + * The private structs are visible because the library sources are compiled + * into this test; the states are built in a caller-supplied, over-aligned + * buffer, so the result depends only on the library's own layout code. + * No transform is run. + */ +#include +#include +#include +#include +#include +#include "kiss_fft.h" +#include "_kiss_fft_guts.h" + +/* the library itself is not built with -W; keep its sign-compare noise out of this test */ +#if defined(__GNUC__) || defined(__clang__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wsign-compare" +#endif +#include "../kiss_fftr.c" +#include "../kiss_fftnd.c" +#include "../kiss_fftndr.c" +#if defined(__GNUC__) || defined(__clang__) +# pragma GCC diagnostic pop +#endif + +/* C89-portable alignment requirement of a type: padding after a char member */ +#define ALIGN_PROBE(tag, type) struct tag { char c; type x; } +#define ALIGNOF(tag) offsetof(struct tag, x) + +ALIGN_PROBE(probe_cpx, kiss_fft_cpx); +ALIGN_PROBE(probe_fft_state, struct kiss_fft_state); +ALIGN_PROBE(probe_fftr_state, struct kiss_fftr_state); +ALIGN_PROBE(probe_fftnd_state, struct kiss_fftnd_state); +ALIGN_PROBE(probe_ptr, void *); +ALIGN_PROBE(probe_int, int); + +#define BUF_ALIGN 64 /* more than any type here needs, so only the layout can misalign */ + +static int failures = 0; + +static void fmt_dims(char *out, const int *dims, int ndims) +{ + int i; + char *p = out; + *p++ = '{'; + for (i = 0; i < ndims; ++i) + p += sprintf(p, i ? ",%d" : "%d", dims[i]); + *p++ = '}'; + *p = '\0'; +} + +static void check_aligned(const char *owner, const char *dimstr, const char *what, + int index, const void *p, size_t align) +{ + size_t off = (size_t)((uintptr_t)p % align); + if (off != 0) { + if (index >= 0) + printf("FAIL: %s dims %s: %s[%d] at offset %lu is misaligned (needs %lu-byte alignment)\n", + owner, dimstr, what, index, (unsigned long)off, (unsigned long)align); + else + printf("FAIL: %s dims %s: %s at offset %lu is misaligned (needs %lu-byte alignment)\n", + owner, dimstr, what, (unsigned long)off, (unsigned long)align); + ++failures; + } +} + +static void check_nd_state(const char *owner, const char *dimstr, kiss_fftnd_cfg nd) +{ + int k; + check_aligned(owner, dimstr, "states array", -1, nd->states, ALIGNOF(probe_ptr)); + check_aligned(owner, dimstr, "dims array", -1, nd->dims, ALIGNOF(probe_int)); + check_aligned(owner, dimstr, "tmpbuf", -1, nd->tmpbuf, ALIGNOF(probe_cpx)); + for (k = 0; k < nd->ndims; ++k) + check_aligned(owner, dimstr, "kiss_fft_state", k, nd->states[k], ALIGNOF(probe_fft_state)); +} + +/* allocate through the caller-supplied-buffer path, in a BUF_ALIGN-aligned block; + * returns NULL (and *raw == NULL or to be freed) if the size is not accepted */ +static void *alloc_in_aligned_buffer(size_t len, void **raw) +{ + *raw = malloc(len + BUF_ALIGN); + if (*raw == NULL) + return NULL; + return (void *)(((uintptr_t)*raw + BUF_ALIGN - 1) & ~(uintptr_t)(BUF_ALIGN - 1)); +} + +static void test_fftnd(const int *dims, int ndims) +{ + char dimstr[64]; + size_t len = 0; + void *raw, *buf; + kiss_fftnd_cfg st; + + fmt_dims(dimstr, dims, ndims); + (void)kiss_fftnd_alloc(dims, ndims, 0, NULL, &len); + buf = alloc_in_aligned_buffer(len, &raw); + if (buf == NULL) { + printf("FAIL: out of memory for kiss_fftnd dims %s\n", dimstr); + ++failures; + return; + } + st = kiss_fftnd_alloc(dims, ndims, 0, buf, &len); + if (st == NULL) { + printf("FAIL: kiss_fftnd_alloc rejected dims %s\n", dimstr); + ++failures; + } else { + check_nd_state("kiss_fftnd", dimstr, st); + } + free(raw); +} + +static void test_fftndr(const int *dims, int ndims) +{ + char dimstr[64]; + size_t len = 0; + void *raw, *buf; + kiss_fftndr_cfg st; + + fmt_dims(dimstr, dims, ndims); + (void)kiss_fftndr_alloc(dims, ndims, 0, NULL, &len); + if (len == 0 || len == (size_t)-1) { + printf("(note: kiss_fftndr_alloc size query rejected dims %s, skipped)\n", dimstr); + return; + } + buf = alloc_in_aligned_buffer(len, &raw); + if (buf == NULL) { + printf("FAIL: out of memory for kiss_fftndr dims %s\n", dimstr); + ++failures; + return; + } + st = kiss_fftndr_alloc(dims, ndims, 0, buf, &len); + if (st == NULL) { + printf("(note: kiss_fftndr_alloc rejected dims %s, skipped)\n", dimstr); + } else { + check_aligned("kiss_fftndr", dimstr, "real-FFT state", -1, st->cfg_r, ALIGNOF(probe_fftr_state)); + check_aligned("kiss_fftndr", dimstr, "nd state", -1, st->cfg_nd, ALIGNOF(probe_fftnd_state)); + check_aligned("kiss_fftndr", dimstr, "tmpbuf", -1, st->tmpbuf, ALIGNOF(probe_cpx)); + if (ndims > 1) + check_nd_state("kiss_fftndr's nd state", dimstr, st->cfg_nd); + } + free(raw); +} + +int main(void) +{ + static const int d7[] = {7}; + static const int d345[] = {3, 4, 5}; + static const int d46[] = {4, 6}; + static const int d23456[] = {2, 3, 4, 5, 6}; + static const int d6[] = {6}; + static const int d346[] = {3, 4, 6}; + static const int d58[] = {5, 8}; + + test_fftnd(d7, 1); + test_fftnd(d345, 3); + test_fftnd(d46, 2); + test_fftnd(d23456, 5); + + test_fftndr(d46, 2); + test_fftndr(d6, 1); + test_fftndr(d346, 3); + test_fftndr(d58, 2); + test_fftndr(d23456, 5); + + printf("%s\n", failures ? "FAIL" : "PASS"); + return failures ? 1 : 0; +}