1 // See LICENSE for license details.
6 //--------------------------------------------------------------------------
9 // Set HOST_DEBUG to 1 if you are going to compile this for a host
10 // machine (ie Athena/Linux) for debug purposes and set HOST_DEBUG
11 // to 0 if you are compiling with the smips-gcc toolchain.
17 // Set PREALLOCATE to 1 if you want to preallocate the benchmark
18 // function before starting stats. If you have instruction/data
19 // caches and you don't want to count the overhead of misses, then
20 // you will need to use preallocation.
26 // Set SET_STATS to 1 if you want to carve out the piece that actually
27 // does the computation.
31 static void setStats(int enable
) {}
33 extern void setStats(int enable
);
40 #define static_assert(cond) switch(0) { case 0: case !!(long)(cond): ; }
42 static void printArray(const char name
[], int n
, const int arr
[])
46 printf( " %10s :", name
);
47 for ( i
= 0; i
< n
; i
++ )
48 printf( " %3d ", arr
[i
] );
53 static void printDoubleArray(const char name
[], int n
, const double arr
[])
57 printf( " %10s :", name
);
58 for ( i
= 0; i
< n
; i
++ )
59 printf( " %g ", arr
[i
] );
64 static int verify(int n
, const volatile int* test
, const int* verify
)
67 // Unrolled for faster verification
68 for (i
= 0; i
< n
/2*2; i
+=2)
70 int t0
= test
[i
], t1
= test
[i
+1];
71 int v0
= verify
[i
], v1
= verify
[i
+1];
72 if (t0
!= v0
) return i
+1;
73 if (t1
!= v1
) return i
+2;
75 if (n
% 2 != 0 && test
[n
-1] != verify
[n
-1])
80 static int verifyDouble(int n
, const volatile double* test
, const double* verify
)
83 // Unrolled for faster verification
84 for (i
= 0; i
< n
/2*2; i
+=2)
86 double t0
= test
[i
], t1
= test
[i
+1];
87 double v0
= verify
[i
], v1
= verify
[i
+1];
88 int eq1
= t0
== v0
, eq2
= t1
== v1
;
89 if (!(eq1
& eq2
)) return i
+1+eq1
;
91 if (n
% 2 != 0 && test
[n
-1] != verify
[n
-1])
96 static void __attribute__((noinline
)) barrier(int ncores
)
98 static volatile int sense
;
99 static volatile int count
;
100 static __thread
int threadsense
;
102 __sync_synchronize();
104 threadsense
= !threadsense
;
105 if (__sync_fetch_and_add(&count
, 1) == ncores
-1)
110 else while(sense
!= threadsense
)
113 __sync_synchronize();
116 static uint64_t lfsr(uint64_t x
)
118 uint64_t bit
= (x
^ (x
>> 1)) & 1;
119 return (x
>> 1) | (bit
<< 62);
123 #include "encoding.h"
126 #define stringify_1(s) #s
127 #define stringify(s) stringify_1(s)
128 #define stats(code, iter) do { \
129 unsigned long _c = -rdcycle(), _i = -rdinstret(); \
131 _c += rdcycle(), _i += rdinstret(); \
133 printf("\n%s: %ld cycles, %ld.%ld cycles/iter, %ld.%ld CPI\n", \
134 stringify(code), _c, _c/iter, 10*_c/iter%10, _c/_i, 10*_c/_i%10); \