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
);
38 #define static_assert(cond) switch(0) { case 0: case !!(long)(cond): ; }
40 static void printArray(const char name
[], int n
, const int arr
[])
44 printf( " %10s :", name
);
45 for ( i
= 0; i
< n
; i
++ )
46 printf( " %3d ", arr
[i
] );
51 static void printDoubleArray(const char name
[], int n
, const double arr
[])
55 printf( " %10s :", name
);
56 for ( i
= 0; i
< n
; i
++ )
57 printf( " %g ", arr
[i
] );
62 static int verify(int n
, const volatile int* test
, const int* verify
)
65 // Unrolled for faster verification
66 for (i
= 0; i
< n
/2*2; i
+=2)
68 int t0
= test
[i
], t1
= test
[i
+1];
69 int v0
= verify
[i
], v1
= verify
[i
+1];
70 if (t0
!= v0
) return i
+1;
71 if (t1
!= v1
) return i
+2;
73 if (n
% 2 != 0 && test
[n
-1] != verify
[n
-1])
78 static int verifyDouble(int n
, const volatile double* test
, const double* verify
)
81 // Unrolled for faster verification
82 for (i
= 0; i
< n
/2*2; i
+=2)
84 double t0
= test
[i
], t1
= test
[i
+1];
85 double v0
= verify
[i
], v1
= verify
[i
+1];
86 int eq1
= t0
== v0
, eq2
= t1
== v1
;
87 if (!(eq1
& eq2
)) return i
+1+eq1
;
89 if (n
% 2 != 0 && test
[n
-1] != verify
[n
-1])
94 static void __attribute__((noinline
)) barrier(int ncores
)
96 static volatile int sense
;
97 static volatile int count
;
98 static __thread
int threadsense
;
100 __sync_synchronize();
102 threadsense
= !threadsense
;
103 if (__sync_fetch_and_add(&count
, 1) == ncores
-1)
108 else while(sense
!= threadsense
)
111 __sync_synchronize();
114 static uint64_t lfsr(uint64_t x
)
116 uint64_t bit
= (x
^ (x
>> 1)) & 1;
117 return (x
>> 1) | (bit
<< 62);
121 #include "encoding.h"
124 #define stringify_1(s) #s
125 #define stringify(s) stringify_1(s)
126 #define stats(code, iter) do { \
127 unsigned long _c = -read_csr(mcycle), _i = -read_csr(minstret); \
129 _c += read_csr(mcycle), _i += read_csr(minstret); \
131 printf("\n%s: %ld cycles, %ld.%ld cycles/iter, %ld.%ld CPI\n", \
132 stringify(code), _c, _c/iter, 10*_c/iter%10, _c/_i, 10*_c/_i%10); \