core_matrix.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. Author : Shay Gal-On, EEMBC
  3. This file is part of EEMBC(R) and CoreMark(TM), which are Copyright (C) 2009
  4. All rights reserved.
  5. EEMBC CoreMark Software is a product of EEMBC and is provided under the terms of the
  6. CoreMark License that is distributed with the official EEMBC COREMARK Software release.
  7. If you received this EEMBC CoreMark Software without the accompanying CoreMark License,
  8. you must discontinue use and download the official release from www.coremark.org.
  9. Also, if you are publicly displaying scores generated from the EEMBC CoreMark software,
  10. make sure that you are in compliance with Run and Reporting rules specified in the accompanying readme.txt file.
  11. EEMBC
  12. 4354 Town Center Blvd. Suite 114-200
  13. El Dorado Hills, CA, 95762
  14. */
  15. #include "coremark.h"
  16. /*
  17. Topic: Description
  18. Matrix manipulation benchmark
  19. This very simple algorithm forms the basis of many more complex algorithms.
  20. The tight inner loop is the focus of many optimizations (compiler as well as hardware based)
  21. and is thus relevant for embedded processing.
  22. The total available data space will be divided to 3 parts:
  23. NxN Matrix A - initialized with small values (upper 3/4 of the bits all zero).
  24. NxN Matrix B - initialized with medium values (upper half of the bits all zero).
  25. NxN Matrix C - used for the result.
  26. The actual values for A and B must be derived based on input that is not available at compile time.
  27. */
  28. ee_s16 matrix_test(ee_u32 N, MATRES *C, MATDAT *A, MATDAT *B, MATDAT val);
  29. ee_s16 matrix_sum(ee_u32 N, MATRES *C, MATDAT clipval);
  30. void matrix_mul_const(ee_u32 N, MATRES *C, MATDAT *A, MATDAT val);
  31. void matrix_mul_vect(ee_u32 N, MATRES *C, MATDAT *A, MATDAT *B);
  32. void matrix_mul_matrix(ee_u32 N, MATRES *C, MATDAT *A, MATDAT *B);
  33. void matrix_mul_matrix_bitextract(ee_u32 N, MATRES *C, MATDAT *A, MATDAT *B);
  34. void matrix_add_const(ee_u32 N, MATDAT *A, MATDAT val);
  35. #define matrix_test_next(x) (x+1)
  36. #define matrix_clip(x,y) ((y) ? (x) & 0x0ff : (x) & 0x0ffff)
  37. #define matrix_big(x) (0xf000 | (x))
  38. #define bit_extract(x,from,to) (((x)>>(from)) & (~(0xffffffff << (to))))
  39. #if CORE_DEBUG
  40. void printmat(MATDAT *A, ee_u32 N, char *name) {
  41. ee_u32 i,j;
  42. ee_printf("Matrix %s [%dx%d]:\n",name,N,N);
  43. for (i=0; i<N; i++) {
  44. for (j=0; j<N; j++) {
  45. if (j!=0)
  46. ee_printf(",");
  47. ee_printf("%d",A[i*N+j]);
  48. }
  49. ee_printf("\n");
  50. }
  51. }
  52. void printmatC(MATRES *C, ee_u32 N, char *name) {
  53. ee_u32 i,j;
  54. ee_printf("Matrix %s [%dx%d]:\n",name,N,N);
  55. for (i=0; i<N; i++) {
  56. for (j=0; j<N; j++) {
  57. if (j!=0)
  58. ee_printf(",");
  59. ee_printf("%d",C[i*N+j]);
  60. }
  61. ee_printf("\n");
  62. }
  63. }
  64. #endif
  65. /* Function: core_bench_matrix
  66. Benchmark function
  67. Iterate <matrix_test> N times,
  68. changing the matrix values slightly by a constant amount each time.
  69. */
  70. ee_u16 core_bench_matrix(mat_params *p, ee_s16 seed, ee_u16 crc) {
  71. ee_u32 N=p->N;
  72. MATRES *C=p->C;
  73. MATDAT *A=p->A;
  74. MATDAT *B=p->B;
  75. MATDAT val=(MATDAT)seed;
  76. crc=crc16(matrix_test(N,C,A,B,val),crc);
  77. return crc;
  78. }
  79. /* Function: matrix_test
  80. Perform matrix manipulation.
  81. Parameters:
  82. N - Dimensions of the matrix.
  83. C - memory for result matrix.
  84. A - input matrix
  85. B - operator matrix (not changed during operations)
  86. Returns:
  87. A CRC value that captures all results calculated in the function.
  88. In particular, crc of the value calculated on the result matrix
  89. after each step by <matrix_sum>.
  90. Operation:
  91. 1 - Add a constant value to all elements of a matrix.
  92. 2 - Multiply a matrix by a constant.
  93. 3 - Multiply a matrix by a vector.
  94. 4 - Multiply a matrix by a matrix.
  95. 5 - Add a constant value to all elements of a matrix.
  96. After the last step, matrix A is back to original contents.
  97. */
  98. ee_s16 matrix_test(ee_u32 N, MATRES *C, MATDAT *A, MATDAT *B, MATDAT val) {
  99. ee_u16 crc=0;
  100. MATDAT clipval=matrix_big(val);
  101. matrix_add_const(N,A,val); /* make sure data changes */
  102. #if CORE_DEBUG
  103. printmat(A,N,"matrix_add_const");
  104. #endif
  105. matrix_mul_const(N,C,A,val);
  106. crc=crc16(matrix_sum(N,C,clipval),crc);
  107. #if CORE_DEBUG
  108. printmatC(C,N,"matrix_mul_const");
  109. #endif
  110. matrix_mul_vect(N,C,A,B);
  111. crc=crc16(matrix_sum(N,C,clipval),crc);
  112. #if CORE_DEBUG
  113. printmatC(C,N,"matrix_mul_vect");
  114. #endif
  115. matrix_mul_matrix(N,C,A,B);
  116. crc=crc16(matrix_sum(N,C,clipval),crc);
  117. #if CORE_DEBUG
  118. printmatC(C,N,"matrix_mul_matrix");
  119. #endif
  120. matrix_mul_matrix_bitextract(N,C,A,B);
  121. crc=crc16(matrix_sum(N,C,clipval),crc);
  122. #if CORE_DEBUG
  123. printmatC(C,N,"matrix_mul_matrix_bitextract");
  124. #endif
  125. matrix_add_const(N,A,-val); /* return matrix to initial value */
  126. return crc;
  127. }
  128. /* Function : matrix_init
  129. Initialize the memory block for matrix benchmarking.
  130. Parameters:
  131. blksize - Size of memory to be initialized.
  132. memblk - Pointer to memory block.
  133. seed - Actual values chosen depend on the seed parameter.
  134. p - pointers to <mat_params> containing initialized matrixes.
  135. Returns:
  136. Matrix dimensions.
  137. Note:
  138. The seed parameter MUST be supplied from a source that cannot be determined at compile time
  139. */
  140. ee_u32 core_init_matrix(ee_u32 blksize, void *memblk, ee_s32 seed, mat_params *p) {
  141. ee_u32 N=0;
  142. MATDAT *A;
  143. MATDAT *B;
  144. ee_s32 order=1;
  145. MATDAT val;
  146. ee_u32 i=0,j=0;
  147. if (seed==0)
  148. seed=1;
  149. while (j<blksize) {
  150. i++;
  151. j=i*i*2*4;
  152. }
  153. N=i-1;
  154. A=(MATDAT *)align_mem(memblk);
  155. B=A+N*N;
  156. for (i=0; i<N; i++) {
  157. for (j=0; j<N; j++) {
  158. seed = ( ( order * seed ) % 65536 );
  159. val = (seed + order);
  160. val=matrix_clip(val,0);
  161. B[i*N+j] = val;
  162. val = (val + order);
  163. val=matrix_clip(val,1);
  164. A[i*N+j] = val;
  165. order++;
  166. }
  167. }
  168. p->A=A;
  169. p->B=B;
  170. p->C=(MATRES *)align_mem(B+N*N);
  171. p->N=N;
  172. #if CORE_DEBUG
  173. printmat(A,N,"A");
  174. printmat(B,N,"B");
  175. #endif
  176. return N;
  177. }
  178. /* Function: matrix_sum
  179. Calculate a function that depends on the values of elements in the matrix.
  180. For each element, accumulate into a temporary variable.
  181. As long as this value is under the parameter clipval,
  182. add 1 to the result if the element is bigger then the previous.
  183. Otherwise, reset the accumulator and add 10 to the result.
  184. */
  185. ee_s16 matrix_sum(ee_u32 N, MATRES *C, MATDAT clipval) {
  186. MATRES tmp=0,prev=0,cur=0;
  187. ee_s16 ret=0;
  188. ee_u32 i,j;
  189. for (i=0; i<N; i++) {
  190. for (j=0; j<N; j++) {
  191. cur=C[i*N+j];
  192. tmp+=cur;
  193. if (tmp>clipval) {
  194. ret+=10;
  195. tmp=0;
  196. } else {
  197. ret += (cur>prev) ? 1 : 0;
  198. }
  199. prev=cur;
  200. }
  201. }
  202. return ret;
  203. }
  204. /* Function: matrix_mul_const
  205. Multiply a matrix by a constant.
  206. This could be used as a scaler for instance.
  207. */
  208. void matrix_mul_const(ee_u32 N, MATRES *C, MATDAT *A, MATDAT val) {
  209. ee_u32 i,j;
  210. for (i=0; i<N; i++) {
  211. for (j=0; j<N; j++) {
  212. C[i*N+j]=(MATRES)A[i*N+j] * (MATRES)val;
  213. }
  214. }
  215. }
  216. /* Function: matrix_add_const
  217. Add a constant value to all elements of a matrix.
  218. */
  219. void matrix_add_const(ee_u32 N, MATDAT *A, MATDAT val) {
  220. ee_u32 i,j;
  221. for (i=0; i<N; i++) {
  222. for (j=0; j<N; j++) {
  223. A[i*N+j] += val;
  224. }
  225. }
  226. }
  227. /* Function: matrix_mul_vect
  228. Multiply a matrix by a vector.
  229. This is common in many simple filters (e.g. fir where a vector of coefficients is applied to the matrix.)
  230. */
  231. void matrix_mul_vect(ee_u32 N, MATRES *C, MATDAT *A, MATDAT *B) {
  232. ee_u32 i,j;
  233. for (i=0; i<N; i++) {
  234. C[i]=0;
  235. for (j=0; j<N; j++) {
  236. C[i]+=(MATRES)A[i*N+j] * (MATRES)B[j];
  237. }
  238. }
  239. }
  240. /* Function: matrix_mul_matrix
  241. Multiply a matrix by a matrix.
  242. Basic code is used in many algorithms, mostly with minor changes such as scaling.
  243. */
  244. void matrix_mul_matrix(ee_u32 N, MATRES *C, MATDAT *A, MATDAT *B) {
  245. ee_u32 i,j,k;
  246. for (i=0; i<N; i++) {
  247. for (j=0; j<N; j++) {
  248. C[i*N+j]=0;
  249. for(k=0;k<N;k++)
  250. {
  251. C[i*N+j]+=(MATRES)A[i*N+k] * (MATRES)B[k*N+j];
  252. }
  253. }
  254. }
  255. }
  256. /* Function: matrix_mul_matrix_bitextract
  257. Multiply a matrix by a matrix, and extract some bits from the result.
  258. Basic code is used in many algorithms, mostly with minor changes such as scaling.
  259. */
  260. void matrix_mul_matrix_bitextract(ee_u32 N, MATRES *C, MATDAT *A, MATDAT *B) {
  261. ee_u32 i,j,k;
  262. for (i=0; i<N; i++) {
  263. for (j=0; j<N; j++) {
  264. C[i*N+j]=0;
  265. for(k=0;k<N;k++)
  266. {
  267. MATRES tmp=(MATRES)A[i*N+k] * (MATRES)B[k*N+j];
  268. C[i*N+j]+=bit_extract(tmp,2,4)*bit_extract(tmp,5,7);
  269. }
  270. }
  271. }
  272. }