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(Float data
[], int nn
[], int ndim
, int isign
)
170 register int i1
, i2
, i3
;
171 int i2rev
, i3rev
, ip1
, ip2
, ip3
, ifp1
, ifp2
;
172 int ibit
, idim
, k1
, k2
, n
, nprev
, nrem
, ntot
;
174 double theta
, wi
, wpi
, wpr
, wr
, wtemp
;
177 for (idim
= 1; idim
<= ndim
; idim
++)
180 for (idim
= ndim
; idim
>= 1; idim
--) {
182 nrem
= ntot
/ (n
* nprev
);
187 for (i2
= 1; i2
<= ip2
; i2
+= ip1
) {
189 for (i1
= i2
; i1
<= i2
+ ip1
- 2; i1
+= 2) {
190 for (i3
= i1
; i3
<= ip3
; i3
+= ip2
) {
191 i3rev
= i2rev
+ i3
- i2
;
192 SWAP(data
[i3
], data
[i3rev
]);
193 SWAP(data
[i3
+ 1], data
[i3rev
+ 1]);
198 while (ibit
>= ip1
&& i2rev
> ibit
) {
207 theta
= isign
* 6.28318530717959 / (ifp2
/ ip1
);
208 wtemp
= sin(0.5 * theta
);
209 wpr
= -2.0 * wtemp
* wtemp
;
213 for (i3
= 1; i3
<= ifp1
; i3
+= ip1
) {
214 for (i1
= i3
; i1
<= i3
+ ip1
- 2; i1
+= 2) {
215 for (i2
= i1
; i2
<= ip3
; i2
+= ifp2
) {
218 tempr
= wr
* data
[k2
] - wi
* data
[k2
+ 1];
219 tempi
= wr
* data
[k2
+ 1] + wi
* data
[k2
];
220 data
[k2
] = data
[k1
] - tempr
;
221 data
[k2
+ 1] = data
[k1
+ 1] - tempi
;
223 data
[k1
+ 1] += tempi
;
226 wr
= (wtemp
= wr
) * wpr
- wi
* wpi
+ wr
;
227 wi
= wi
* wpr
+ wtemp
* wpi
+ wi
;
238 int i
, j
, k
, l
, m
, npasses
= Passes
, faedge
;
239 Float
*fdata
/* , *fd */ ;
240 static int nsize
[] = {0, 0, 0};
242 double mapbase
, mapscale
, /* x, */ rmin
, rmax
, imin
, imax
;
244 faedge
= Asize
; /* FFT array edge size */
245 fanum
= faedge
* faedge
; /* Elements in FFT array */
246 fasize
= ((fanum
+ 1) * 2 * sizeof(Float
)); /* FFT array size */
247 nsize
[1] = nsize
[2] = faedge
;
249 fdata
= (Float
*) malloc(fasize
);
251 fprintf(stdout
, "Can't allocate data array.\n");
255 /* Generate data array to process. */
257 #define Re(x,y) fdata[1 + (faedge * (x) + (y)) * 2]
258 #define Im(x,y) fdata[2 + (faedge * (x) + (y)) * 2]
260 memset(fdata
, 0, fasize
);
261 for (i
= 0; i
< faedge
; i
++) {
262 for (j
= 0; j
< faedge
; j
++) {
263 if (((i
& 15) == 8) || ((j
& 15) == 8))
268 for (i
= 0; i
< npasses
; i
++) {
269 /*printf("Pass %d\n", i);*/
270 /* Transform image to frequency domain. */
271 fourn(fdata
, nsize
, 2, 1);
273 /* Back-transform to image. */
274 fourn(fdata
, nsize
, 2, -1);
278 double r
, ij
, ar
, ai
;
279 rmin
= 1e10
; rmax
= -1e10
;
280 imin
= 1e10
; imax
= -1e10
;
284 for (i
= 1; i
<= fanum
; i
+= 2) {
291 imin
= min(ij
, imin
);
292 imax
= max(ij
, imax
);
295 printf("Real min %.4g, max %.4g. Imaginary min %.4g, max %.4g.\n",
296 rmin
, rmax
, imin
, imax
);
297 printf("Average real %.4g, imaginary %.4g.\n",
298 ar
/ fanum
, ai
/ fanum
);
301 mapscale
= 255 / (rmax
- rmin
);
304 /* See if we got the right answers. */
307 for (i
= 0; i
< faedge
; i
++) {
308 for (j
= 0; j
< faedge
; j
++) {
309 k
= (Re(i
, j
) - mapbase
) * mapscale
;
310 l
= (((i
& 15) == 8) || ((j
& 15) == 8)) ? 255 : 0;
314 "Wrong answer at (%d,%d)! Expected %d, got %d.\n",
320 fprintf(stdout
, "%d passes. No errors in results.\n", npasses
);
322 fprintf(stdout
, "%d passes. %d errors in results.\n",
328 /* Output the result of the transform as a CA Lab pattern
329 file for debugging. */
334 #define SCRN (SCRX * SCRY)
335 unsigned char patarr
[SCRY
][SCRX
];
338 /* Map user external state numbers to internal state index */
340 #define UtoI(x) (((((x) >> 1) & 0x7F) | ((x) << 7)) & 0xFF)
342 /* Copy data from FFT buffer to map. */
344 memset(patarr
, 0, sizeof patarr
);
345 l
= (SCRX
- faedge
) / 2;
346 m
= (faedge
> SCRY
) ? 0 : ((SCRY
- faedge
) / 2);
347 for (i
= 1; i
< faedge
; i
++) {
348 for (j
= 0; j
< min(SCRY
, faedge
); j
++) {
349 k
= (Re(i
, j
) - mapbase
) * mapscale
;
350 patarr
[j
+ m
][i
+ l
] = UtoI(k
);
354 /* Dump pattern map to file. */
356 fp
= fopen("fft.cap", "w");
358 fprintf(stdout
, "Cannot open output file.\n");
363 fwrite(patarr
, SCRN
, 1, fp
);