annotate src/fftw-3.3.3/libbench2/util.c @ 95:89f5e221ed7b

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