util.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // See LICENSE for license details.
  2. #ifndef __UTIL_H
  3. #define __UTIL_H
  4. //--------------------------------------------------------------------------
  5. // Macros
  6. // Set HOST_DEBUG to 1 if you are going to compile this for a host
  7. // machine (ie Athena/Linux) for debug purposes and set HOST_DEBUG
  8. // to 0 if you are compiling with the smips-gcc toolchain.
  9. #ifndef HOST_DEBUG
  10. #define HOST_DEBUG 0
  11. #endif
  12. // Set PREALLOCATE to 1 if you want to preallocate the benchmark
  13. // function before starting stats. If you have instruction/data
  14. // caches and you don't want to count the overhead of misses, then
  15. // you will need to use preallocation.
  16. #ifndef PREALLOCATE
  17. #define PREALLOCATE 0
  18. #endif
  19. // Set SET_STATS to 1 if you want to carve out the piece that actually
  20. // does the computation.
  21. #if HOST_DEBUG
  22. #include <stdio.h>
  23. static void setStats(int enable) {}
  24. #else
  25. extern void setStats(int enable);
  26. #endif
  27. #include <stdint.h>
  28. #define static_assert(cond) switch(0) { case 0: case !!(long)(cond): ; }
  29. static void printArray(const char name[], int n, const int arr[])
  30. {
  31. #if HOST_DEBUG
  32. int i;
  33. printf( " %10s :", name );
  34. for ( i = 0; i < n; i++ )
  35. printf( " %3d ", arr[i] );
  36. printf( "\n" );
  37. #endif
  38. }
  39. static void printDoubleArray(const char name[], int n, const double arr[])
  40. {
  41. #if HOST_DEBUG
  42. int i;
  43. printf( " %10s :", name );
  44. for ( i = 0; i < n; i++ )
  45. printf( " %g ", arr[i] );
  46. printf( "\n" );
  47. #endif
  48. }
  49. static int verify(int n, const volatile int* test, const int* verify)
  50. {
  51. int i;
  52. // Unrolled for faster verification
  53. for (i = 0; i < n/2*2; i+=2)
  54. {
  55. int t0 = test[i], t1 = test[i+1];
  56. int v0 = verify[i], v1 = verify[i+1];
  57. if (t0 != v0) return i+1;
  58. if (t1 != v1) return i+2;
  59. }
  60. if (n % 2 != 0 && test[n-1] != verify[n-1])
  61. return n;
  62. return 0;
  63. }
  64. static int verifyDouble(int n, const volatile double* test, const double* verify)
  65. {
  66. int i;
  67. // Unrolled for faster verification
  68. for (i = 0; i < n/2*2; i+=2)
  69. {
  70. double t0 = test[i], t1 = test[i+1];
  71. double v0 = verify[i], v1 = verify[i+1];
  72. int eq1 = t0 == v0, eq2 = t1 == v1;
  73. if (!(eq1 & eq2)) return i+1+eq1;
  74. }
  75. if (n % 2 != 0 && test[n-1] != verify[n-1])
  76. return n;
  77. return 0;
  78. }
  79. static void __attribute__((noinline)) barrier(int ncores)
  80. {
  81. static volatile int sense;
  82. static volatile int count;
  83. static __thread int threadsense;
  84. __sync_synchronize();
  85. threadsense = !threadsense;
  86. if (__sync_fetch_and_add(&count, 1) == ncores-1)
  87. {
  88. count = 0;
  89. sense = threadsense;
  90. }
  91. else while(sense != threadsense)
  92. ;
  93. __sync_synchronize();
  94. }
  95. static uint64_t lfsr(uint64_t x)
  96. {
  97. uint64_t bit = (x ^ (x >> 1)) & 1;
  98. return (x >> 1) | (bit << 62);
  99. }
  100. #ifdef __riscv
  101. #include "encoding.h"
  102. #endif
  103. #define stringify_1(s) #s
  104. #define stringify(s) stringify_1(s)
  105. #define stats(code, iter) do { \
  106. unsigned long _c = -read_csr(mcycle), _i = -read_csr(minstret); \
  107. code; \
  108. _c += read_csr(mcycle), _i += read_csr(minstret); \
  109. if (cid == 0) \
  110. printf("\n%s: %ld cycles, %ld.%ld cycles/iter, %ld.%ld CPI\n", \
  111. stringify(code), _c, _c/iter, 10*_c/iter%10, _c/_i, 10*_c/_i%10); \
  112. } while(0)
  113. #endif //__UTIL_H