mirror of
https://github.com/mborgerding/kissfft.git
synced 2026-11-18 20:09:30 -05:00
kiss_fftndr: fix allocation size check, odd sizes and ndims == 1
kiss_fftndr_alloc had three problems: - Its overflow check divided the required size (truncated to an int) by every dimension and failed when fewer than sizeof(kiss_fft_scalar) bytes per element remained. The buffers only need slightly more than that per element, so valid sizes were rejected: 230 of the even NxN sizes from 2 to 512 (starting at 54x54) returned NULL. Replace it with a direct check that dimOther*(dimReal+2) scalars fit in size_t with a margin and that dimOther fits the int that kiss_fftnd counts it in, and make prod() saturate instead of wrapping. - An odd last dimension is not supported by kiss_fftr, but the failure of the kiss_fftr_alloc size query was ignored and a config with a NULL kiss_fftr state was returned. Now kiss_fftndr_alloc returns NULL. - With ndims == 1 the inner kiss_fftnd has zero dimensions, and kiss_fftnd never wrote its output in that case (the even-ndims path leaves the result in tmpbuf and the loop body never runs), so kiss_fftndr/kiss_fftndri returned garbage. Make a zero-dimensional kiss_fftnd the identity, which fixes both directions. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01ME1hnNaoD3ZcMj9H8TSYkZ
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
57a701785b
commit
933c235c1c
@@ -49,6 +49,7 @@ target_compile_definitions(tkfc PRIVATE KFC_TEST)
|
||||
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)
|
||||
|
||||
add_kissfft_test_executable(next_fast_size next_fast_size.c)
|
||||
|
||||
|
||||
+9
-2
@@ -44,6 +44,7 @@ ifneq ($(KISSFFT_OPENMP),1)
|
||||
SELFTEST = st-$(KISSFFT_DATATYPE)
|
||||
TESTREAL = tr-$(KISSFFT_DATATYPE)
|
||||
TESTSIZEVAL = tsizeval-$(KISSFFT_DATATYPE)
|
||||
TESTNDRALLOC = tndralloc-$(KISSFFT_DATATYPE)
|
||||
TESTKFC = tkfc-$(KISSFFT_DATATYPE)
|
||||
TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE)
|
||||
TESTCPP = testcpp-$(KISSFFT_DATATYPE)
|
||||
@@ -54,6 +55,7 @@ else
|
||||
SELFTEST = st-$(KISSFFT_DATATYPE)-openmp
|
||||
TESTREAL = tr-$(KISSFFT_DATATYPE)-openmp
|
||||
TESTSIZEVAL = tsizeval-$(KISSFFT_DATATYPE)-openmp
|
||||
TESTNDRALLOC = tndralloc-$(KISSFFT_DATATYPE)-openmp
|
||||
TESTKFC = tkfc-$(KISSFFT_DATATYPE)-openmp
|
||||
TESTFASTFILT = fastfilt-$(KISSFFT_DATATYPE)-openmp
|
||||
TESTCPP = testcpp-$(KISSFFT_DATATYPE)-openmp
|
||||
@@ -105,7 +107,7 @@ endif
|
||||
# Target: "make all"
|
||||
#
|
||||
|
||||
all: $(BENCHKISS) $(SELFTEST) $(BENCHFFTW) $(TESTREAL) $(TESTSIZEVAL) $(TESTKFC) $(TESTFASTFILT)
|
||||
all: $(BENCHKISS) $(SELFTEST) $(BENCHFFTW) $(TESTREAL) $(TESTSIZEVAL) $(TESTNDRALLOC) $(TESTKFC) $(TESTFASTFILT)
|
||||
|
||||
#
|
||||
# Individual test make rules
|
||||
@@ -123,6 +125,9 @@ $(TESTREAL): test_real.c
|
||||
$(TESTSIZEVAL): test_size_validation.c
|
||||
$(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm
|
||||
|
||||
$(TESTNDRALLOC): test_fftndr_alloc.c
|
||||
$(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $< -L.. -l$(KISSFFTLIB_SHORTNAME) -lm
|
||||
|
||||
$(BENCHKISS): benchkiss.c pstats.c
|
||||
$(CC) -o $@ $(CFLAGS) $(TYPEFLAGS) $^ -L.. -l$(KISSFFTLIB_SHORTNAME) -lm
|
||||
|
||||
@@ -182,6 +187,8 @@ endif
|
||||
@LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTREAL)
|
||||
$(warning ======allocation size validation regression test (type= $(KISSFFT_DATATYPE) ))
|
||||
@LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTSIZEVAL)
|
||||
$(warning ======kiss_fftndr allocation regression test (type= $(KISSFFT_DATATYPE) ))
|
||||
@LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):.." ./$(TESTNDRALLOC)
|
||||
$(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
|
||||
@@ -193,4 +200,4 @@ endif
|
||||
#
|
||||
|
||||
clean:
|
||||
rm -f *~ bm-* st-* tr-* tsizeval-* kf-* tkfc-* ff-* fastfilt-* *.pyc *.pyo *.dat testcpp-* testsimd testsimd-* _test_library_dummy _test_library_dummy.c
|
||||
rm -f *~ bm-* st-* tr-* tsizeval-* tndralloc-* kf-* tkfc-* ff-* fastfilt-* *.pyc *.pyo *.dat testcpp-* testsimd testsimd-* _test_library_dummy _test_library_dummy.c
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* 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 tests for kiss_fftndr_alloc / kiss_fftndr:
|
||||
* 1. valid even NxN sizes must not be rejected by the allocation size check
|
||||
* 2. an odd last dimension (unsupported by kiss_fftr) must make alloc fail
|
||||
* 3. ndims == 1 must produce the same spectrum as kiss_fftr
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "kiss_fftndr.h"
|
||||
#include "kiss_fftr.h"
|
||||
|
||||
static int failures = 0;
|
||||
|
||||
static void fail(const char *msg, int a, int b)
|
||||
{
|
||||
printf("FAIL: %s (%d,%d)\n", msg, a, b);
|
||||
++failures;
|
||||
}
|
||||
|
||||
/* allocate through the caller-supplied-buffer path; returns NULL on failure */
|
||||
static kiss_fftndr_cfg alloc_ndr(const int *dims, int ndims, void **buf)
|
||||
{
|
||||
size_t len = 0;
|
||||
*buf = NULL;
|
||||
kiss_fftndr_alloc(dims, ndims, 0, NULL, &len);
|
||||
if (len == 0 || len == (size_t)-1)
|
||||
return NULL;
|
||||
*buf = malloc(len);
|
||||
if (*buf == NULL)
|
||||
return NULL;
|
||||
return kiss_fftndr_alloc(dims, ndims, 0, *buf, &len);
|
||||
}
|
||||
|
||||
static void test_valid_sizes_accepted(void)
|
||||
{
|
||||
int n, first = 0, rejected = 0;
|
||||
for (n = 2; n <= 512; n += 2) {
|
||||
int dims[2];
|
||||
void *buf;
|
||||
dims[0] = n;
|
||||
dims[1] = n;
|
||||
if (alloc_ndr(dims, 2, &buf) == NULL) {
|
||||
if (!first)
|
||||
first = n;
|
||||
++rejected;
|
||||
}
|
||||
free(buf);
|
||||
}
|
||||
if (rejected) {
|
||||
printf("(%d valid even NxN sizes in 2..512 rejected)\n", rejected);
|
||||
fail("kiss_fftndr_alloc rejected a valid square size, first", first, first);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_odd_last_dim_rejected(void)
|
||||
{
|
||||
int dims[2] = {4, 5};
|
||||
void *buf;
|
||||
if (alloc_ndr(dims, 2, &buf) != NULL)
|
||||
fail("kiss_fftndr_alloc accepted an odd last dimension", dims[0], dims[1]);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
/* the inner kiss_fftnd counts dims[0..ndims-2] in an int: a product above INT_MAX
|
||||
* must be reported as too large by the size query (no allocation is attempted) */
|
||||
static void test_int_overflow_rejected(void)
|
||||
{
|
||||
int dims[3] = {46341, 46341, 2}; /* 46341^2 > INT_MAX */
|
||||
size_t len = 0;
|
||||
kiss_fftndr_alloc(dims, 3, 0, NULL, &len);
|
||||
if (len != (size_t)-1)
|
||||
fail("kiss_fftndr_alloc size query accepted dims whose product overflows int", dims[0], dims[1]);
|
||||
}
|
||||
|
||||
#ifndef USE_SIMD
|
||||
static void test_one_dimension_matches_fftr(void)
|
||||
{
|
||||
enum { N = 8 };
|
||||
int dims[1] = {N};
|
||||
kiss_fft_scalar in[N];
|
||||
kiss_fft_cpx got[N / 2 + 1], want[N / 2 + 1];
|
||||
void *buf;
|
||||
kiss_fftndr_cfg ndr;
|
||||
kiss_fftr_cfg r;
|
||||
int k;
|
||||
|
||||
for (k = 0; k < N; ++k)
|
||||
in[k] = (kiss_fft_scalar)(100 * (k + 1));
|
||||
|
||||
ndr = alloc_ndr(dims, 1, &buf);
|
||||
r = kiss_fftr_alloc(N, 0, NULL, NULL);
|
||||
if (ndr == NULL || r == NULL) {
|
||||
fail("allocation failed for ndims == 1", N, 0);
|
||||
} else {
|
||||
kiss_fftndr(ndr, in, got);
|
||||
kiss_fftr(r, in, want);
|
||||
for (k = 0; k <= N / 2; ++k)
|
||||
if (got[k].r != want[k].r || got[k].i != want[k].i)
|
||||
fail("ndims == 1 output differs from kiss_fftr at bin", k, 0);
|
||||
}
|
||||
free(buf);
|
||||
kiss_fftr_free(r);
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_valid_sizes_accepted();
|
||||
test_odd_last_dim_rejected();
|
||||
test_int_overflow_rejected();
|
||||
#ifndef USE_SIMD
|
||||
test_one_dimension_matches_fftr();
|
||||
#endif
|
||||
printf("%s\n", failures ? "FAIL" : "PASS");
|
||||
return failures ? 1 : 0;
|
||||
}
|
||||
Reference in New Issue
Block a user