annotate fft/fftw/fftw-3.3.4/libbench2/util.c @ 40:223f770b5341 kissfft-double tip

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