Fix FPU initialization code
[riscv-tests.git] / benchmarks / common / util.h
1 // See LICENSE for license details.
2
3 #ifndef __UTIL_H
4 #define __UTIL_H
5
6 extern void setStats(int enable);
7
8 #include <stdint.h>
9
10 #define static_assert(cond) switch(0) { case 0: case !!(long)(cond): ; }
11
12 static int verify(int n, const volatile int* test, const int* verify)
13 {
14 int i;
15 // Unrolled for faster verification
16 for (i = 0; i < n/2*2; i+=2)
17 {
18 int t0 = test[i], t1 = test[i+1];
19 int v0 = verify[i], v1 = verify[i+1];
20 if (t0 != v0) return i+1;
21 if (t1 != v1) return i+2;
22 }
23 if (n % 2 != 0 && test[n-1] != verify[n-1])
24 return n;
25 return 0;
26 }
27
28 static int verifyDouble(int n, const volatile double* test, const double* verify)
29 {
30 int i;
31 // Unrolled for faster verification
32 for (i = 0; i < n/2*2; i+=2)
33 {
34 double t0 = test[i], t1 = test[i+1];
35 double v0 = verify[i], v1 = verify[i+1];
36 int eq1 = t0 == v0, eq2 = t1 == v1;
37 if (!(eq1 & eq2)) return i+1+eq1;
38 }
39 if (n % 2 != 0 && test[n-1] != verify[n-1])
40 return n;
41 return 0;
42 }
43
44 static void __attribute__((noinline)) barrier(int ncores)
45 {
46 static volatile int sense;
47 static volatile int count;
48 static __thread int threadsense;
49
50 __sync_synchronize();
51
52 threadsense = !threadsense;
53 if (__sync_fetch_and_add(&count, 1) == ncores-1)
54 {
55 count = 0;
56 sense = threadsense;
57 }
58 else while(sense != threadsense)
59 ;
60
61 __sync_synchronize();
62 }
63
64 static uint64_t lfsr(uint64_t x)
65 {
66 uint64_t bit = (x ^ (x >> 1)) & 1;
67 return (x >> 1) | (bit << 62);
68 }
69
70 static uintptr_t insn_len(uintptr_t pc)
71 {
72 return (*(unsigned short*)pc & 3) ? 4 : 2;
73 }
74
75 #ifdef __riscv
76 #include "encoding.h"
77 #endif
78
79 #define stringify_1(s) #s
80 #define stringify(s) stringify_1(s)
81 #define stats(code, iter) do { \
82 unsigned long _c = -read_csr(mcycle), _i = -read_csr(minstret); \
83 code; \
84 _c += read_csr(mcycle), _i += read_csr(minstret); \
85 if (cid == 0) \
86 printf("\n%s: %ld cycles, %ld.%ld cycles/iter, %ld.%ld CPI\n", \
87 stringify(code), _c, _c/iter, 10*_c/iter%10, _c/_i, 10*_c/_i%10); \
88 } while(0)
89
90 #endif //__UTIL_H