1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
// This code is part of the Problem Based Benchmark Suite (PBBS)
// Copyright (c) 2010 Guy Blelloch and the PBBS team
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights (to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifndef _BENCH_UTILS_INCLUDED
#define _BENCH_UTILS_INCLUDED
#include <iostream>
#include <algorithm>
#include <limits.h>
#if defined(__APPLE__)
#define PTCMPXCH " cmpxchgl %2,%1\n"
#else
#define PTCMPXCH " cmpxchgq %2,%1\n"
// Needed to make frequent large allocations efficient with standard
// malloc implementation. Otherwise they are allocated directly from
// vm.
#include <malloc.h>
static int __ii = mallopt(M_MMAP_MAX,0);
static int __jj = mallopt(M_TRIM_THRESHOLD,-1);
#endif
#define newA(__E,__n) (__E*) malloc((__n)*sizeof(__E))
namespace utils {
// ADAM: added inline to remove "defined but not used" warning
static inline void myAssert(int cond, std::string s) {
if (!cond) {
std::cout << s << std::endl;
abort();
}
}
// returns the log base 2 rounded up (works on ints or longs or unsigned versions)
template <class T>
static int log2Up(T i) {
int a=0;
T b=i-1;
while (b > 0) {b = b >> 1; a++;}
return a;
}
// ADAM: added inline to remove "defined but not used" warning
static inline int logUp(unsigned int i) {
int a=0;
int b=i-1;
while (b > 0) {b = b >> 1; a++;}
return a;
}
// ADAM: added inline to remove "defined but not used" warning
static inline int logUpLong(unsigned long i) {
int a=0;
long b=i-1;
while (b > 0) {b = b >> 1; a++;}
return a;
}
inline unsigned int hash(unsigned int a)
{
a = (a+0x7ed55d16) + (a<<12);
a = (a^0xc761c23c) ^ (a>>19);
a = (a+0x165667b1) + (a<<5);
a = (a+0xd3a2646c) ^ (a<<9);
a = (a+0xfd7046c5) + (a<<3);
a = (a^0xb55a4f09) ^ (a>>16);
return a;
}
inline int hashInt(unsigned int a) {
return hash(a) & (((unsigned) 1 << 31) - 1);
}
inline unsigned int hash2(unsigned int a)
{
return (((unsigned int) 1103515245 * a) + (unsigned int) 12345) %
(unsigned int) 0xFFFFFFFF;
}
// compare and swap on 8 byte quantities
inline bool LCAS(long *ptr, long oldv, long newv) {
unsigned char ret;
/* Note that sete sets a 'byte' not the word */
__asm__ __volatile__ (
" lock\n"
" cmpxchgq %2,%1\n"
" sete %0\n"
: "=q" (ret), "=m" (*ptr)
: "r" (newv), "m" (*ptr), "a" (oldv)
: "memory");
return ret;
}
// compare and swap on 4 byte quantity
inline bool SCAS(int *ptr, int oldv, int newv) {
unsigned char ret;
/* Note that sete sets a 'byte' not the word */
__asm__ __volatile__ (
" lock\n"
" cmpxchgl %2,%1\n"
" sete %0\n"
: "=q" (ret), "=m" (*ptr)
: "r" (newv), "m" (*ptr), "a" (oldv)
: "memory");
return ret;
}
// The conditional should be removed by the compiler
// this should work with pointer types, or pairs of integers
template <class ET>
inline bool CAS(ET *ptr, ET oldv, ET newv) {
if (sizeof(ET) == 8) {
return utils::LCAS((long*) ptr, *((long*) &oldv), *((long*) &newv));
} else if (sizeof(ET) == 4) {
return utils::SCAS((int *) ptr, *((int *) &oldv), *((int *) &newv));
} else {
std::cout << "CAS bad length" << std::endl;
abort();
}
}
template <class ET>
inline bool CAS_GCC(ET *ptr, ET oldv, ET newv) {
return __sync_bool_compare_and_swap(ptr, oldv, newv);
}
template <class ET>
inline ET fetchAndAdd(ET *a, ET b) {
volatile ET newV, oldV;
abort();
do {oldV = *a; newV = oldV + b;}
while (!CAS(a, oldV, newV));
return oldV;
}
template <class ET>
inline void writeAdd(ET *a, ET b) {
volatile ET newV, oldV;
do {oldV = *a; newV = oldV + b;}
while (!CAS(a, oldV, newV));
}
template <class ET>
inline bool writeMax(ET *a, ET b) {
ET c; bool r=0;
do c = *a;
while (c < b && !(r=CAS(a,c,b)));
return r;
}
template <class ET>
inline bool writeMin(ET *a, ET b) {
ET c; bool r=0;
do c = *a;
while (c > b && !(r=CAS(a,c,b)));
// while (c > b && !(r=__sync_bool_compare_and_swap(a, c, b)));
return r;
}
template <class ET>
inline bool writeMin(ET **a, ET *b) {
ET* c; bool r = 0;
do c = *a;
while (c > b && !(r=CAS(a,c,b)));
return r;
}
template <class E>
struct identityF { E operator() (const E& x) {return x;}};
template <class E>
struct addF { E operator() (const E& a, const E& b) const {return a+b;}};
template <class E>
struct absF { E operator() (const E& a) const {return std::abs(a);}};
template <class E>
struct zeroF { E operator() (const E& a) const {return 0;}};
template <class E>
struct maxF { E operator() (const E& a, const E& b) const {return (a>b) ? a : b;}};
template <class E>
struct minF { E operator() (const E& a, const E& b) const {return (a<b) ? a : b;}};
template <class E1, class E2>
struct firstF {E1 operator() (std::pair<E1,E2> a) {return a.first;} };
template <class E1, class E2>
struct secondF {E2 operator() (std::pair<E1,E2> a) {return a.second;} };
}
#endif // _BENCH_UTILS_INCLUDED
|