Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #include "mv-mult.h"
  2. #include <xmmintrin.h>
  3.  
  4. // Matrix-Vector multiplication
  5. // mat is a SIZE by SIZE matrix, that is arranged in row-column, format,
  6. // That is, you first select a particular row, and then a particular column.
  7. // Each row is laid out as a one-dimensional, array, so if you wanted
  8. // to select a particular row, you would use mat[row]. You can
  9. // also select smaller intervals, by using &mat[row][col].
  10. // The vector is also laid out as a one-dimensional arrow, similar to a row.
  11. // M-V multiplication proceeds by taking the dot product of a matrix row
  12. // with the vector, and doing this for each row in the matrix
  13.  
  14. // vectorize the below code using SIMD intrinsics
  15. float *
  16. mv_mult_vector(float mat[SIZE][SIZE], float vec[SIZE]) {
  17. static float ret[SIZE];
  18.  
  19. // for (int i = 0; i < SIZE; i ++) {
  20. // ret[i] = 0;
  21. // for (int j = 0; j < SIZE; j ++) {
  22. // ret[i] += mat[i][j] * vec[j];
  23. // }
  24. // }
  25.  
  26. float inner_product = 0.0, temp[4];
  27. __m128 acc, mat_128, vec_128; // 4x32-bit float registers
  28. acc = _mm_set1_ps(0.0); // set all four words in acc to 0.0
  29.  
  30. int j = 0;
  31. for (; j < SIZE; j++) {
  32. int i = 0;
  33. acc = _mm_set1_ps(0.0);
  34. for (; i < (SIZE - 3); i += 4) {
  35.  
  36. mat_128 = _mm_loadu_ps(&mat[j][i]); // load groups of four floats
  37. vec_128 = _mm_loadu_ps(&vec[j]);
  38. acc = _mm_add_ps(acc, _mm_mul_ps(mat_128, vec_128));
  39. }
  40. _mm_storeu_ps(temp, acc); // add the accumulated values
  41. ret[j] = temp[0] + temp[1] + temp[2] + temp[3];
  42. for (; i < SIZE; i++) { // add up the remaining floats
  43. ret[j] += mat[j][i] * vec[j];
  44. }
  45. }
  46.  
  47.  
  48.  
  49. return ret;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement