Clean up benchmarks; support uarch-specific counters
[riscv-tests.git] / benchmarks / common / util.h
index 10f3169aa5608a074851eeec7b1436682449b9fb..1f0c73cd44a0c29f24b6f03a241456d57430ba72 100644 (file)
@@ -1,16 +1,99 @@
-// helpful utility and synch functions 
-
-// relies on defining "ncores" before including this file...
-
 #ifndef __UTIL_H
 #define __UTIL_H
 
-#include <machine/syscall.h>
+//--------------------------------------------------------------------------
+// Macros
+
+// Set HOST_DEBUG to 1 if you are going to compile this for a host
+// machine (ie Athena/Linux) for debug purposes and set HOST_DEBUG
+// to 0 if you are compiling with the smips-gcc toolchain.
+
+#ifndef HOST_DEBUG
+#define HOST_DEBUG 0
+#endif
+
+// Set PREALLOCATE to 1 if you want to preallocate the benchmark
+// function before starting stats. If you have instruction/data
+// caches and you don't want to count the overhead of misses, then
+// you will need to use preallocation.
+
+#ifndef PREALLOCATE
+#define PREALLOCATE 0
+#endif
+
+// Set SET_STATS to 1 if you want to carve out the piece that actually
+// does the computation.
+
+#ifndef SET_STATS
+#define SET_STATS 0
+#endif
+
+#if HOST_DEBUG
+#include <stdio.h>
+static void setStats(int enable) {}
+#else
+extern void setStats(int enable);
+#endif
+
+static void printArray(const char name[], int n, const int arr[])
+{
+#if HOST_DEBUG
+  int i;
+  printf( " %10s :", name );
+  for ( i = 0; i < n; i++ )
+    printf( " %3d ", arr[i] );
+  printf( "\n" );
+#endif
+}
+
+static void printDoubleArray(const char name[], int n, const double arr[])
+{
+#if HOST_DEBUG
+  int i;
+  printf( " %10s :", name );
+  for ( i = 0; i < n; i++ )
+    printf( " %g ", arr[i] );
+  printf( "\n" );
+#endif
+}
+
+static int verify(int n, const int test[], const int verify[])
+{
+  int i;
+  // Unrolled for faster verification
+  for (i = 0; i < n/2*2; i+=2)
+  {
+    int t0 = test[i], t1 = test[i+1];
+    int v0 = verify[i], v1 = verify[i+1];
+    if (t0 != v0) return i+1;
+    if (t1 != v1) return i+2;
+  }
+  if (n % 2 != 0 && test[n-1] != verify[n-1])
+    return n;
+  return 0;
+}
 
-#define rdcycle() ({ unsigned long _c; asm volatile ("rdcycle %0" : "=r"(_c) :: "memory"); _c; })
-#define rdinstret() ({ unsigned long _c; asm volatile ("rdinstret %0" : "=r"(_c) :: "memory"); _c; })
-                            
-void __attribute__((noinline)) barrier()
+static int verifyDouble(int n, const double test[], const double verify[])
+{
+  int i;
+  // Unrolled for faster verification
+  for (i = 0; i < n/2*2; i+=2)
+  {
+    double t0 = test[i], t1 = test[i+1];
+    double v0 = verify[i], v1 = verify[i+1];
+    int eq1 = t0 == v0, eq2 = t1 == v1;
+    if (!(eq1 & eq2)) return i+1+eq1;
+  }
+  if (n % 2 != 0 && test[n-1] != verify[n-1])
+    return n;
+  return 0;
+}
+
+#ifndef ncores
+#define ncores 1
+#endif
+
+static void __attribute__((noinline)) barrier()
 {
   static volatile int sense;
   static volatile int count;
@@ -30,31 +113,8 @@ void __attribute__((noinline)) barrier()
   __sync_synchronize();
 }
 
-
-
-
-
-void finishTest(int test_result)
-{
-#if HOST_DEBUG
-  if ( test_result == 1 )
-    printf( "*** PASSED ***\n" );
-  else
-    printf( "*** FAILED *** (tohost = %d)\n", test_result);
-  exit(0);
-#else
-   {
-      // perform exit syscall
-      asm volatile(
-          "move a0,%0 ;"
-          "li a1,0    ;"
-          "li a2,0    ;"
-          "li a3,0    ;"
-          "li v0,%1   ;"
-          "scall" : : "r"(test_result) , "i"(SYS_exit));
-   }
+#ifdef __riscv
+#include "encoding.h"
 #endif
-}
 
 #endif //__UTIL_H
-