1 // This small program computes a Fast Fourier Transform. It tests
2 // Valgrind's handling of FP operations. It is representative of all
3 // programs that do a lot of FP operations.
5 // Licensing: This program is closely based on the one of the same name from
6 // http://www.fourmilab.ch/. The front page of that site says:
8 // "Except for a few clearly-marked exceptions, all the material on this
9 // site is in the public domain and may be used in any manner without
10 // permission, restriction, attribution, or compensation."
14 Two-dimensional FFT benchmark
16 Designed and implemented by John Walker in April of 1989.
18 This benchmark executes a specified number of passes (default
19 20) through a loop in which each iteration performs a fast
20 Fourier transform of a square matrix (default size 256x256) of
21 complex numbers (default precision double), followed by the
22 inverse transform. After all loop iterations are performed
23 the results are checked against known correct values.
25 This benchmark is intended for use on C implementations which
26 define "int" as 32 bits or longer and permit allocation and
27 direct addressing of arrays larger than one megabyte.
29 If CAPOUT is defined, the result after all iterations is
30 written as a CA Lab pattern file. This is intended for
31 debugging in case horribly wrong results are obtained on a
34 Archival timings are run with the definitions below set as
35 follows: Float = double, Asize = 256, Passes = 20, CAPOUT not
40 2393.93 Sun 3/260, SunOS 3.4, C, "-f68881 -O".
43 1928 Macintosh IIx, MPW C 3.0, "-mc68020
44 -mc68881 -elems881 -m". (Hugh Hoover).
46 1636.1 Sun 4/110, "cc -O3 -lm". (Michael McClary).
47 The suspicion is that this is software
50 1556.7 Macintosh II, A/UX, "cc -O -lm"
53 1388.8 Sun 386i/250, SunOS 4.0.1 C
54 "-O /usr/lib/trig.il". (James Carrington).
56 1331.93 Sun 3/60, SunOS 4.0.1, C,
57 "-O4 -f68881 /usr/lib/libm.il"
60 1204.0 Apollo Domain DN4000, C, "-cpu 3000 -opt 4".
63 1174.66 Compaq 386/25, SCO Xenix 386 C.
66 1068 Compaq 386/25, SCO Xenix 386,
67 Metaware High C. (Robert Wenig).
69 1064.0 Sun 3/80, SunOS 4.0.3 Beta C
70 "-O3 -f68881 /usr/lib/libm.il". (James Carrington).
72 1061.4 Compaq 386/25, SCO Xenix, High C 1.4.
75 1059.79 Compaq 386/25, 387/25, High C 1.4,
76 DOS|Extender 2.2, 387 inline code
77 generation. (Nathan Bender).
79 777.14 Compaq 386/25, IIT 3C87-25 (387 Compatible),
80 High C 1.5, DOS|Extender 2.2, 387 inline
81 code generation. (Nathan Bender).
83 751 Compaq DeskPro 386/33, High C 1.5 + DOS|Extender,
84 387 code generation. (James Carrington).
86 431.44 Compaq 386/25, Weitek 3167-25, DOS 3.31,
87 High C 1.4, DOS|Extender, Weitek code generation.
90 344.9 Compaq 486/25, Metaware High C 1.6, Phar Lap
91 DOS|Extender, in-line floating point. (Nathan
94 324.2 Data General Motorola 88000, 16 Mhz, Gnu C.
96 323.1 Sun 4/280, C, "-O4". (Eric Hill).
98 254 Compaq SystemPro 486/33, High C 1.5 + DOS|Extender,
99 387 code generation. (James Carrington).
101 242.8 Silicon Graphics Personal IRIS, MIPS R2000A,
102 12.5 Mhz, "-O3" (highest level optimisation).
105 233.0 Sun SPARCStation 1, C, "-O4", SunOS 4.0.3.
108 187.30 DEC PMAX 3100, MIPS 2000 chip.
111 120.46 Sun SparcStation 2, C, "-O4", SunOS 4.1.1.
114 120.21 DEC 3MAX, MIPS 3000, "-O4".
116 98.0 Intel i860 experimental environment,
117 OS/2, data caching disabled. (Kern
120 34.9 Silicon Graphics Indigo², MIPS R4400,
121 175 Mhz, IRIX 5.2, "-O".
123 32.4 Pentium 133, Windows NT, Microsoft Visual
126 17.25 Silicon Graphics Indigo², MIPS R4400,
127 175 Mhz, IRIX 6.5, "-O3".
129 14.10 Dell Dimension XPS R100, Pentium II 400 MHz,
130 Windows 98, Microsoft Visual C 5.0.
132 10.7 Hewlett-Packard Kayak XU 450Mhz Pentium II,
133 Microsoft Visual C++ 6.0, Windows NT 4.0sp3. (Nathan Bender).
135 5.09 Sun Ultra 2, UltraSPARC V9, 300 MHz, gcc -O3.
137 0.846 Dell Inspiron 9100, Pentium 4, 3.4 GHz, gcc -O3.
146 /* The program may be run with Float defined as either float or
147 double. With IEEE arithmetic, the same answers are generated for
148 either floating point mode. */
150 #define Float double /* Floating point type used in FFT */
152 #define Asize 256 /* Array edge size */
153 #define Passes 20 /* Number of FFT/Inverse passes */
155 #define max(a,b) ((a)>(b)?(a):(b))
156 #define min(a,b) ((a)<=(b)?(a):(b))
160 Multi-dimensional fast Fourier transform
162 Adapted from Press et al., "Numerical Recipes in C".
166 #define SWAP(a,b) tempr=(a); (a)=(b); (b)=tempr
168 static void fourn(data
, nn
, ndim
, isign
)
170 int nn
[], ndim
, isign
;
172 register int i1
, i2
, i3
;
173 int i2rev
, i3rev
, ip1
, ip2
, ip3
, ifp1
, ifp2
;
174 int ibit
, idim
, k1
, k2
, n
, nprev
, nrem
, ntot
;
176 double theta
, wi
, wpi
, wpr
, wr
, wtemp
;
179 for (idim
= 1; idim
<= ndim
; idim
++)
182 for (idim
= ndim
; idim
>= 1; idim
--) {
184 nrem
= ntot
/ (n
* nprev
);
189 for (i2
= 1; i2
<= ip2
; i2
+= ip1
) {
191 for (i1
= i2
; i1
<= i2
+ ip1
- 2; i1
+= 2) {
192 for (i3
= i1
; i3
<= ip3
; i3
+= ip2
) {
193 i3rev
= i2rev
+ i3
- i2
;
194 SWAP(data
[i3
], data
[i3rev
]);
195 SWAP(data
[i3
+ 1], data
[i3rev
+ 1]);
200 while (ibit
>= ip1
&& i2rev
> ibit
) {
209 theta
= isign
* 6.28318530717959 / (ifp2
/ ip1
);
210 wtemp
= sin(0.5 * theta
);
211 wpr
= -2.0 * wtemp
* wtemp
;
215 for (i3
= 1; i3
<= ifp1
; i3
+= ip1
) {
216 for (i1
= i3
; i1
<= i3
+ ip1
- 2; i1
+= 2) {
217 for (i2
= i1
; i2
<= ip3
; i2
+= ifp2
) {
220 tempr
= wr
* data
[k2
] - wi
* data
[k2
+ 1];
221 tempi
= wr
* data
[k2
+ 1] + wi
* data
[k2
];
222 data
[k2
] = data
[k1
] - tempr
;
223 data
[k2
+ 1] = data
[k1
+ 1] - tempi
;
225 data
[k1
+ 1] += tempi
;
228 wr
= (wtemp
= wr
) * wpr
- wi
* wpi
+ wr
;
229 wi
= wi
* wpr
+ wtemp
* wpi
+ wi
;
240 int i
, j
, k
, l
, m
, npasses
= Passes
, faedge
;
241 Float
*fdata
/* , *fd */ ;
242 static int nsize
[] = {0, 0, 0};
244 double mapbase
, mapscale
, /* x, */ rmin
, rmax
, imin
, imax
;
246 faedge
= Asize
; /* FFT array edge size */
247 fanum
= faedge
* faedge
; /* Elements in FFT array */
248 fasize
= ((fanum
+ 1) * 2 * sizeof(Float
)); /* FFT array size */
249 nsize
[1] = nsize
[2] = faedge
;
251 fdata
= (Float
*) malloc(fasize
);
253 fprintf(stdout
, "Can't allocate data array.\n");
257 /* Generate data array to process. */
259 #define Re(x,y) fdata[1 + (faedge * (x) + (y)) * 2]
260 #define Im(x,y) fdata[2 + (faedge * (x) + (y)) * 2]
262 memset(fdata
, 0, fasize
);
263 for (i
= 0; i
< faedge
; i
++) {
264 for (j
= 0; j
< faedge
; j
++) {
265 if (((i
& 15) == 8) || ((j
& 15) == 8))
270 for (i
= 0; i
< npasses
; i
++) {
271 /*printf("Pass %d\n", i);*/
272 /* Transform image to frequency domain. */
273 fourn(fdata
, nsize
, 2, 1);
275 /* Back-transform to image. */
276 fourn(fdata
, nsize
, 2, -1);
280 double r
, ij
, ar
, ai
;
281 rmin
= 1e10
; rmax
= -1e10
;
282 imin
= 1e10
; imax
= -1e10
;
286 for (i
= 1; i
<= fanum
; i
+= 2) {
293 imin
= min(ij
, imin
);
294 imax
= max(ij
, imax
);
297 printf("Real min %.4g, max %.4g. Imaginary min %.4g, max %.4g.\n",
298 rmin
, rmax
, imin
, imax
);
299 printf("Average real %.4g, imaginary %.4g.\n",
300 ar
/ fanum
, ai
/ fanum
);
303 mapscale
= 255 / (rmax
- rmin
);
306 /* See if we got the right answers. */
309 for (i
= 0; i
< faedge
; i
++) {
310 for (j
= 0; j
< faedge
; j
++) {
311 k
= (Re(i
, j
) - mapbase
) * mapscale
;
312 l
= (((i
& 15) == 8) || ((j
& 15) == 8)) ? 255 : 0;
316 "Wrong answer at (%d,%d)! Expected %d, got %d.\n",
322 fprintf(stdout
, "%d passes. No errors in results.\n", npasses
);
324 fprintf(stdout
, "%d passes. %d errors in results.\n",
330 /* Output the result of the transform as a CA Lab pattern
331 file for debugging. */
336 #define SCRN (SCRX * SCRY)
337 unsigned char patarr
[SCRY
][SCRX
];
340 /* Map user external state numbers to internal state index */
342 #define UtoI(x) (((((x) >> 1) & 0x7F) | ((x) << 7)) & 0xFF)
344 /* Copy data from FFT buffer to map. */
346 memset(patarr
, 0, sizeof patarr
);
347 l
= (SCRX
- faedge
) / 2;
348 m
= (faedge
> SCRY
) ? 0 : ((SCRY
- faedge
) / 2);
349 for (i
= 1; i
< faedge
; i
++) {
350 for (j
= 0; j
< min(SCRY
, faedge
); j
++) {
351 k
= (Re(i
, j
) - mapbase
) * mapscale
;
352 patarr
[j
+ m
][i
+ l
] = UtoI(k
);
356 /* Dump pattern map to file. */
358 fp
= fopen("fft.cap", "w");
360 fprintf(stdout
, "Cannot open output file.\n");
365 fwrite(patarr
, SCRN
, 1, fp
);