mirror of
https://github.com/mborgerding/kissfft.git
synced 2026-11-18 20:09:30 -05:00
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
134 lines
4.2 KiB
C
134 lines
4.2 KiB
C
/*
|
|
* Copyright (c) 2003-2004, 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.
|
|
*/
|
|
|
|
#include "kiss_fftndr.h"
|
|
#include "_kiss_fft_guts.h"
|
|
#define MAX(x,y) ( ( (x)<(y) )?(y):(x) )
|
|
|
|
struct kiss_fftndr_state
|
|
{
|
|
int dimReal;
|
|
size_t dimOther;
|
|
kiss_fftr_cfg cfg_r;
|
|
kiss_fftnd_cfg cfg_nd;
|
|
void * tmpbuf;
|
|
};
|
|
|
|
static size_t prod(const int *dims, int ndims)
|
|
{
|
|
size_t x=1;
|
|
while (ndims--) {
|
|
if (*dims > 0 && x > (size_t)-1 / (size_t)*dims)
|
|
return (size_t)-1; /* overflow: saturate so the size check below fails */
|
|
x *= *dims++;
|
|
}
|
|
return x;
|
|
}
|
|
|
|
kiss_fftndr_cfg kiss_fftndr_alloc(const int *dims,int ndims,int inverse_fft,void*mem,size_t*lenmem)
|
|
{
|
|
KISS_FFT_ALIGN_CHECK(mem)
|
|
|
|
kiss_fftndr_cfg st = NULL;
|
|
size_t nr=0 , nd=0,ntmp=0;
|
|
int dimReal = dims[ndims-1];
|
|
size_t dimOther = prod(dims,ndims-1);
|
|
size_t memneeded;
|
|
char * ptr = NULL;
|
|
|
|
(void)kiss_fftr_alloc(dimReal,inverse_fft,NULL,&nr);
|
|
if (nr == 0)
|
|
return NULL; /* dimReal not supported by kiss_fftr (e.g. odd) */
|
|
|
|
/* check for overflow: the buffers below need about 2*dimOther*(dimReal+2) scalars; keep a 2x margin */
|
|
/* kiss_fftnd counts the other dimensions in an int */
|
|
if (dimOther > INT_MAX || dimOther > (size_t)-1 / 4 / sizeof(kiss_fft_scalar) / ((size_t)dimReal + 2)) {
|
|
if (lenmem!=NULL)
|
|
*lenmem = (size_t)(-1);
|
|
return NULL;
|
|
}
|
|
(void)kiss_fftnd_alloc(dims,ndims-1,inverse_fft,NULL,&nd);
|
|
ntmp =
|
|
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_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);
|
|
}else{
|
|
if (*lenmem >= memneeded)
|
|
ptr = (char *)mem;
|
|
*lenmem = memneeded;
|
|
}
|
|
if (ptr==NULL)
|
|
return NULL;
|
|
|
|
st = (kiss_fftndr_cfg) ptr;
|
|
memset( st , 0 , memneeded);
|
|
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_PACK_SIZE_UP(nr);
|
|
st->cfg_nd = kiss_fftnd_alloc(dims,ndims-1,inverse_fft, ptr,&nd);
|
|
ptr += KISS_FFT_PACK_SIZE_UP(nd);
|
|
st->tmpbuf = ptr;
|
|
|
|
return st;
|
|
}
|
|
|
|
void kiss_fftndr(kiss_fftndr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata)
|
|
{
|
|
size_t k1,k2;
|
|
size_t dimReal = (size_t)st->dimReal;
|
|
size_t dimOther = st->dimOther;
|
|
int nrbins = dimReal/2+1;
|
|
|
|
kiss_fft_cpx * tmp1 = (kiss_fft_cpx*)st->tmpbuf;
|
|
kiss_fft_cpx * tmp2 = tmp1 + MAX(nrbins,dimOther);
|
|
|
|
// timedata is N0 x N1 x ... x Nk real
|
|
|
|
// take a real chunk of data, fft it and place the output at correct intervals
|
|
for (k1=0;k1<dimOther;++k1) {
|
|
kiss_fftr( st->cfg_r, timedata + k1*dimReal , tmp1 ); // tmp1 now holds nrbins complex points
|
|
for (k2=0;k2<nrbins;++k2)
|
|
tmp2[ k2*dimOther+k1 ] = tmp1[k2];
|
|
}
|
|
|
|
for (k2=0;k2<nrbins;++k2) {
|
|
kiss_fftnd(st->cfg_nd, tmp2+k2*dimOther, tmp1); // tmp1 now holds dimOther complex points
|
|
for (k1=0;k1<dimOther;++k1)
|
|
freqdata[ k1*(nrbins) + k2] = tmp1[k1];
|
|
}
|
|
}
|
|
|
|
void kiss_fftndri(kiss_fftndr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata)
|
|
{
|
|
int k1,k2;
|
|
int dimReal = st->dimReal;
|
|
int dimOther = st->dimOther;
|
|
int nrbins = dimReal/2+1;
|
|
kiss_fft_cpx * tmp1 = (kiss_fft_cpx*)st->tmpbuf;
|
|
kiss_fft_cpx * tmp2 = tmp1 + MAX(nrbins,dimOther);
|
|
|
|
for (k2=0;k2<nrbins;++k2) {
|
|
for (k1=0;k1<dimOther;++k1)
|
|
tmp1[k1] = freqdata[ k1*(nrbins) + k2 ];
|
|
kiss_fftnd(st->cfg_nd, tmp1, tmp2+k2*dimOther);
|
|
}
|
|
|
|
for (k1=0;k1<dimOther;++k1) {
|
|
for (k2=0;k2<nrbins;++k2)
|
|
tmp1[k2] = tmp2[ k2*dimOther+k1 ];
|
|
kiss_fftri( st->cfg_r,tmp1,timedata + k1*dimReal);
|
|
}
|
|
}
|