comparison src/fftw-3.3.3/libbench2/util.c @ 10:37bf6b4a2645

Add FFTW3
author Chris Cannam
date Wed, 20 Mar 2013 15:35:50 +0000
parents
children
comparison
equal deleted inserted replaced
9:c0fb53affa76 10:37bf6b4a2645
1 /*
2 * Copyright (c) 2000 Matteo Frigo
3 * Copyright (c) 2000 Massachusetts Institute of Technology
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21 #include "bench.h"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stddef.h>
25 #include <math.h>
26
27 #if defined(HAVE_DECL_MEMALIGN) && !HAVE_DECL_MEMALIGN
28 # if defined(HAVE_MALLOC_H)
29 # include <malloc.h>
30 # else
31 extern void *memalign(size_t, size_t);
32 # endif
33 #endif
34
35 #if defined(HAVE_DECL_POSIX_MEMALIGN) && !HAVE_DECL_POSIX_MEMALIGN
36 extern int posix_memalign(void **, size_t, size_t);
37 #endif
38
39 void bench_assertion_failed(const char *s, int line, const char *file)
40 {
41 ovtpvt_err("bench: %s:%d: assertion failed: %s\n", file, line, s);
42 bench_exit(EXIT_FAILURE);
43 }
44
45 #ifdef HAVE_DRAND48
46 # if defined(HAVE_DECL_DRAND48) && !HAVE_DECL_DRAND48
47 extern double drand48(void);
48 # endif
49 double bench_drand(void)
50 {
51 return drand48() - 0.5;
52 }
53 # if defined(HAVE_DECL_SRAND48) && !HAVE_DECL_SRAND48
54 extern void srand48(long);
55 # endif
56 void bench_srand(int seed)
57 {
58 srand48(seed);
59 }
60 #else
61 double bench_drand(void)
62 {
63 double d = rand();
64 return (d / (double) RAND_MAX) - 0.5;
65 }
66 void bench_srand(int seed)
67 {
68 srand(seed);
69 }
70 #endif
71
72 /**********************************************************
73 * DEBUGGING CODE
74 **********************************************************/
75 #ifdef BENCH_DEBUG
76 static int bench_malloc_cnt = 0;
77
78 /*
79 * debugging malloc/free. Initialize every malloced and freed area to
80 * random values, just to make sure we are not using uninitialized
81 * pointers. Also check for writes past the ends of allocated blocks,
82 * and a couple of other things.
83 *
84 * This code is a quick and dirty hack -- use at your own risk.
85 */
86
87 static int bench_malloc_total = 0, bench_malloc_max = 0, bench_malloc_cnt_max = 0;
88
89 #define MAGIC ((size_t)0xABadCafe)
90 #define PAD_FACTOR 2
91 #define TWO_SIZE_T (2 * sizeof(size_t))
92
93 #define VERBOSE_ALLOCATION 0
94
95 #if VERBOSE_ALLOCATION
96 #define WHEN_VERBOSE(a) a
97 #else
98 #define WHEN_VERBOSE(a)
99 #endif
100
101 void *bench_malloc(size_t n)
102 {
103 char *p;
104 size_t i;
105
106 bench_malloc_total += n;
107
108 if (bench_malloc_total > bench_malloc_max)
109 bench_malloc_max = bench_malloc_total;
110
111 p = (char *) malloc(PAD_FACTOR * n + TWO_SIZE_T);
112 BENCH_ASSERT(p);
113
114 /* store the size in a known position */
115 ((size_t *) p)[0] = n;
116 ((size_t *) p)[1] = MAGIC;
117 for (i = 0; i < PAD_FACTOR * n; i++)
118 p[i + TWO_SIZE_T] = (char) (i ^ 0xDEADBEEF);
119
120 ++bench_malloc_cnt;
121
122 if (bench_malloc_cnt > bench_malloc_cnt_max)
123 bench_malloc_cnt_max = bench_malloc_cnt;
124
125 /* skip the size we stored previously */
126 return (void *) (p + TWO_SIZE_T);
127 }
128
129 void bench_free(void *p)
130 {
131 char *q;
132
133 BENCH_ASSERT(p);
134
135 q = ((char *) p) - TWO_SIZE_T;
136 BENCH_ASSERT(q);
137
138 {
139 size_t n = ((size_t *) q)[0];
140 size_t magic = ((size_t *) q)[1];
141 size_t i;
142
143 ((size_t *) q)[0] = 0; /* set to zero to detect duplicate free's */
144
145 BENCH_ASSERT(magic == MAGIC);
146 ((size_t *) q)[1] = ~MAGIC;
147
148 bench_malloc_total -= n;
149 BENCH_ASSERT(bench_malloc_total >= 0);
150
151 /* check for writing past end of array: */
152 for (i = n; i < PAD_FACTOR * n; ++i)
153 if (q[i + TWO_SIZE_T] != (char) (i ^ 0xDEADBEEF)) {
154 BENCH_ASSERT(0 /* array bounds overwritten */);
155 }
156 for (i = 0; i < PAD_FACTOR * n; ++i)
157 q[i + TWO_SIZE_T] = (char) (i ^ 0xBEEFDEAD);
158
159 --bench_malloc_cnt;
160
161 BENCH_ASSERT(bench_malloc_cnt >= 0);
162
163 BENCH_ASSERT(
164 (bench_malloc_cnt == 0 && bench_malloc_total == 0) ||
165 (bench_malloc_cnt > 0 && bench_malloc_total > 0));
166
167 free(q);
168 }
169 }
170
171 #else
172 /**********************************************************
173 * NON DEBUGGING CODE
174 **********************************************************/
175 /* production version, no hacks */
176
177 #define MIN_ALIGNMENT 128 /* must be power of two */
178
179 #define real_free free /* memalign and malloc use ordinary free */
180
181 void *bench_malloc(size_t n)
182 {
183 void *p;
184 if (n == 0) n = 1;
185
186 #if defined(WITH_OUR_MALLOC)
187 /* Our own aligned malloc/free. Assumes sizeof(void*) is
188 a power of two <= 8 and that malloc is at least
189 sizeof(void*)-aligned. Assumes size_t = uintptr_t. */
190 {
191 void *p0;
192 if ((p0 = malloc(n + MIN_ALIGNMENT))) {
193 p = (void *) (((size_t) p0 + MIN_ALIGNMENT) & (~((size_t) (MIN_ALIGNMENT - 1))));
194 *((void **) p - 1) = p0;
195 }
196 else
197 p = (void *) 0;
198 }
199 #elif defined(HAVE_MEMALIGN)
200 p = memalign(MIN_ALIGNMENT, n);
201 #elif defined(HAVE_POSIX_MEMALIGN)
202 /* note: posix_memalign is broken in glibc 2.2.5: it constrains
203 the size, not the alignment, to be (power of two) * sizeof(void*).
204 The bug seems to have been fixed as of glibc 2.3.1. */
205 if (posix_memalign(&p, MIN_ALIGNMENT, n))
206 p = (void*) 0;
207 #elif defined(__ICC) || defined(__INTEL_COMPILER) || defined(HAVE__MM_MALLOC)
208 /* Intel's C compiler defines _mm_malloc and _mm_free intrinsics */
209 p = (void *) _mm_malloc(n, MIN_ALIGNMENT);
210 # undef real_free
211 # define real_free _mm_free
212 #else
213 p = malloc(n);
214 #endif
215
216 BENCH_ASSERT(p);
217 return p;
218 }
219
220 void bench_free(void *p)
221 {
222 #ifdef WITH_OUR_MALLOC
223 if (p) free(*((void **) p - 1));
224 #else
225 real_free(p);
226 #endif
227 }
228
229 #endif
230
231 void bench_free0(void *p)
232 {
233 if (p) bench_free(p);
234 }