Commit d445c865 authored by Diego Biurrun's avatar Diego Biurrun

fft-test: Reduce variable scope where sensibly possible

Also merge variable declaration and initialization.
parent eac77fcd
...@@ -65,16 +65,13 @@ static struct { ...@@ -65,16 +65,13 @@ static struct {
static void fft_ref_init(int nbits, int inverse) static void fft_ref_init(int nbits, int inverse)
{ {
int n, i; int i, n = 1 << nbits;
double c1, s1, alpha;
n = 1 << nbits;
exptab = av_malloc((n / 2) * sizeof(*exptab)); exptab = av_malloc((n / 2) * sizeof(*exptab));
for (i = 0; i < (n/2); i++) { for (i = 0; i < (n/2); i++) {
alpha = 2 * M_PI * (float)i / (float)n; double alpha = 2 * M_PI * (float)i / (float)n;
c1 = cos(alpha); double c1 = cos(alpha), s1 = sin(alpha);
s1 = sin(alpha);
if (!inverse) if (!inverse)
s1 = -s1; s1 = -s1;
exptab[i].re = c1; exptab[i].re = c1;
...@@ -84,18 +81,16 @@ static void fft_ref_init(int nbits, int inverse) ...@@ -84,18 +81,16 @@ static void fft_ref_init(int nbits, int inverse)
static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits) static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
{ {
int n, i, j, k, n2; int i, j;
double tmp_re, tmp_im, s, c; int n = 1 << nbits;
FFTComplex *q; int n2 = n >> 1;
n = 1 << nbits;
n2 = n >> 1;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
tmp_re = 0; double tmp_re = 0, tmp_im = 0;
tmp_im = 0; FFTComplex *q = tab;
q = tab;
for (j = 0; j < n; j++) { for (j = 0; j < n; j++) {
k = (i * j) & (n - 1); double s, c;
int k = (i * j) & (n - 1);
if (k >= n2) { if (k >= n2) {
c = -exptab[k - n2].re; c = -exptab[k - n2].re;
s = -exptab[k - n2].im; s = -exptab[k - n2].im;
...@@ -114,15 +109,13 @@ static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits) ...@@ -114,15 +109,13 @@ static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits)
#if CONFIG_MDCT #if CONFIG_MDCT
static void imdct_ref(FFTSample *out, FFTSample *in, int nbits) static void imdct_ref(FFTSample *out, FFTSample *in, int nbits)
{ {
int n = 1<<nbits; int i, k, n = 1 << nbits;
int k, i, a;
double sum, f;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
sum = 0; double sum = 0;
for (k = 0; k < n/2; k++) { for (k = 0; k < n/2; k++) {
a = (2 * i + 1 + (n / 2)) * (2 * k + 1); int a = (2 * i + 1 + (n / 2)) * (2 * k + 1);
f = cos(M_PI * a / (double)(2 * n)); double f = cos(M_PI * a / (double)(2 * n));
sum += f * in[k]; sum += f * in[k];
} }
out[i] = REF_SCALE(-sum, nbits - 2); out[i] = REF_SCALE(-sum, nbits - 2);
...@@ -132,15 +125,13 @@ static void imdct_ref(FFTSample *out, FFTSample *in, int nbits) ...@@ -132,15 +125,13 @@ static void imdct_ref(FFTSample *out, FFTSample *in, int nbits)
/* NOTE: no normalisation by 1 / N is done */ /* NOTE: no normalisation by 1 / N is done */
static void mdct_ref(FFTSample *output, FFTSample *input, int nbits) static void mdct_ref(FFTSample *output, FFTSample *input, int nbits)
{ {
int n = 1<<nbits; int i, k, n = 1 << nbits;
int k, i;
double a, s;
/* do it by hand */ /* do it by hand */
for (k = 0; k < n/2; k++) { for (k = 0; k < n/2; k++) {
s = 0; double s = 0;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
a = (2*M_PI*(2*i+1+n/2)*(2*k+1) / (4 * n)); double a = (2 * M_PI * (2 * i + 1 + n / 2) * (2 * k + 1) / (4 * n));
s += input[i] * cos(a); s += input[i] * cos(a);
} }
output[k] = REF_SCALE(s, nbits - 1); output[k] = REF_SCALE(s, nbits - 1);
...@@ -152,15 +143,13 @@ static void mdct_ref(FFTSample *output, FFTSample *input, int nbits) ...@@ -152,15 +143,13 @@ static void mdct_ref(FFTSample *output, FFTSample *input, int nbits)
#if CONFIG_DCT #if CONFIG_DCT
static void idct_ref(float *output, float *input, int nbits) static void idct_ref(float *output, float *input, int nbits)
{ {
int n = 1<<nbits; int i, k, n = 1 << nbits;
int k, i;
double a, s;
/* do it by hand */ /* do it by hand */
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
s = 0.5 * input[0]; double s = 0.5 * input[0];
for (k = 1; k < n; k++) { for (k = 1; k < n; k++) {
a = M_PI*k*(i+0.5) / n; double a = M_PI * k * (i + 0.5) / n;
s += input[k] * cos(a); s += input[k] * cos(a);
} }
output[i] = 2 * s / n; output[i] = 2 * s / n;
...@@ -168,15 +157,13 @@ static void idct_ref(float *output, float *input, int nbits) ...@@ -168,15 +157,13 @@ static void idct_ref(float *output, float *input, int nbits)
} }
static void dct_ref(float *output, float *input, int nbits) static void dct_ref(float *output, float *input, int nbits)
{ {
int n = 1<<nbits; int i, k, n = 1 << nbits;
int k, i;
double a, s;
/* do it by hand */ /* do it by hand */
for (k = 0; k < n; k++) { for (k = 0; k < n; k++) {
s = 0; double s = 0;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
a = M_PI*k*(i+0.5) / n; double a = M_PI * k * (i + 0.5) / n;
s += input[i] * cos(a); s += input[i] * cos(a);
} }
output[k] = s; output[k] = s;
...@@ -193,10 +180,8 @@ static FFTSample frandom(AVLFG *prng) ...@@ -193,10 +180,8 @@ static FFTSample frandom(AVLFG *prng)
static int check_diff(FFTSample *tab1, FFTSample *tab2, int n, double scale) static int check_diff(FFTSample *tab1, FFTSample *tab2, int n, double scale)
{ {
int i; int i, err = 0;
double max= 0; double error = 0, max = 0;
double error= 0;
int err = 0;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
double e = fabsf(tab1[i] - (tab2[i] / scale)) / RANGE; double e = fabsf(tab1[i] - (tab2[i] / scale)) / RANGE;
...@@ -242,27 +227,23 @@ int main(int argc, char **argv) ...@@ -242,27 +227,23 @@ int main(int argc, char **argv)
{ {
FFTComplex *tab, *tab1, *tab_ref; FFTComplex *tab, *tab1, *tab_ref;
FFTSample *tab2; FFTSample *tab2;
int it, i, c;
int cpuflags;
int do_speed = 0;
int err = 1;
enum tf_transform transform = TRANSFORM_FFT; enum tf_transform transform = TRANSFORM_FFT;
int do_inverse = 0;
FFTContext s1, *s = &s1; FFTContext s1, *s = &s1;
FFTContext m1, *m = &m1; FFTContext m1, *m = &m1;
#if FFT_FLOAT #if FFT_FLOAT
RDFTContext r1, *r = &r1; RDFTContext r1, *r = &r1;
DCTContext d1, *d = &d1; DCTContext d1, *d = &d1;
int fft_size_2;
#endif /* FFT_FLOAT */ #endif /* FFT_FLOAT */
int fft_nbits, fft_size; int it, i, err = 1;
int do_speed = 0, do_inverse = 0;
int fft_nbits = 9, fft_size;
double scale = 1.0; double scale = 1.0;
AVLFG prng; AVLFG prng;
av_lfg_init(&prng, 1); av_lfg_init(&prng, 1);
fft_nbits = 9;
for(;;) { for(;;) {
c = getopt(argc, argv, "hsimrdn:f:c:"); int c = getopt(argc, argv, "hsimrdn:f:c:");
if (c == -1) if (c == -1)
break; break;
switch(c) { switch(c) {
...@@ -291,12 +272,14 @@ int main(int argc, char **argv) ...@@ -291,12 +272,14 @@ int main(int argc, char **argv)
scale = atof(optarg); scale = atof(optarg);
break; break;
case 'c': case 'c':
cpuflags = av_parse_cpu_flags(optarg); {
int cpuflags = av_parse_cpu_flags(optarg);
if (cpuflags < 0) if (cpuflags < 0)
return 1; return 1;
av_set_cpu_flags_mask(cpuflags); av_set_cpu_flags_mask(cpuflags);
break; break;
} }
}
} }
fft_size = 1 << fft_nbits; fft_size = 1 << fft_nbits;
...@@ -388,7 +371,8 @@ int main(int argc, char **argv) ...@@ -388,7 +371,8 @@ int main(int argc, char **argv)
#if FFT_FLOAT #if FFT_FLOAT
#if CONFIG_RDFT #if CONFIG_RDFT
case TRANSFORM_RDFT: case TRANSFORM_RDFT:
fft_size_2 = fft_size >> 1; {
int fft_size_2 = fft_size >> 1;
if (do_inverse) { if (do_inverse) {
tab1[ 0].im = 0; tab1[ 0].im = 0;
tab1[fft_size_2].im = 0; tab1[fft_size_2].im = 0;
...@@ -418,6 +402,7 @@ int main(int argc, char **argv) ...@@ -418,6 +402,7 @@ int main(int argc, char **argv)
err = check_diff((float *)tab_ref, (float *)tab2, fft_size, 1.0); err = check_diff((float *)tab_ref, (float *)tab2, fft_size, 1.0);
} }
break; break;
}
#endif /* CONFIG_RDFT */ #endif /* CONFIG_RDFT */
#if CONFIG_DCT #if CONFIG_DCT
case TRANSFORM_DCT: case TRANSFORM_DCT:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment