mat_mul_simd.c 1.46 KB
Newer Older
Marc's avatar
Marc committed
1
2
3
4
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
5
#include "variables.h"
Marc's avatar
Marc committed
6

7
8
9
10
#ifndef N
#define N 8
#endif

Marc's avatar
Marc committed
11
12
13
14
void print(char c, unsigned char src[N][N]) {
    printf("%c:\n", c);
    for (int i = 0; i<N; i++){
        for (int j = 0; j<N; j++){
Marc's avatar
Marc committed
15
16
            if (c=='B') printf("%d ", src[j][i]);
            else printf("%d ", src[i][j]);
Marc's avatar
Marc committed
17
18
19
20
        }
        printf("\n");
    }
}
Marc's avatar
Marc committed
21

Marc's avatar
Marc committed
22
void product(unsigned char A[N][N], unsigned char B[N][N], unsigned char C[N][N]){
Marc's avatar
Marc committed
23
    int sum = 0;
Marc's avatar
Marc committed
24
    int aux;
Marc's avatar
Marc committed
25
    int matA, matB;
26
27
28
    asm("nop");
    asm("srl %i0, %o1, %g2");
    asm("nop");
Marc's avatar
Marc committed
29
    for(int i=0; i<N; i++)
Marc's avatar
Marc committed
30
        for(int j=0; j<N; j++){
31
            for(int k=0; k<N/4; k++){
Marc's avatar
Marc committed
32
33
34
35
                matA = *((int *) &A[i][k*4]);
                matB = *((int *) &B[j][k*4]);
                asm("usdot %1, %2, %0":"=r"(matA):"r"(matA), "r"(matB));
                asm("usadd_ %1, %2, %0" :"=r"(sum):"r"(sum), "r"(matA));
36
            }
Marc's avatar
Marc committed
37
            C[i][j] = sum;
Marc's avatar
Marc committed
38
            sum = 0;
Marc's avatar
Marc committed
39
        }
40
41
42
    asm("nop");
    asm("srl %i0, %o1, %g2");
    asm("nop");
Marc's avatar
Marc committed
43
}
Marc's avatar
Marc committed
44

Marc's avatar
Marc committed
45
46
47
48
49
50
51
void init(unsigned char A[N][N], unsigned char B[N][N]){
	for(int i=0; i<N; i++)
	    for(int j=0; j<N; j++) {
	        A[i][j] = rand()%10;
	        B[j][i] = rand()%10;
        }
}
Marc's avatar
Marc committed
52

Marc's avatar
Marc committed
53
54
55
56
57
int main()
{
	unsigned char A[N][N], B[N][N], C[N][N];
	srand(N);
	init(A,B);
58
//	puts("TEST BEGIN");
Marc's avatar
Marc committed
59
	product(A,B,C);
60
//	puts("TEST END");
Marc's avatar
Marc committed
61
62
63
64
65
66
67
    #ifdef P_INPUT
	print('A',A);
	print('B',B);
    #endif
    #ifdef P_OUTPUT
	print('C',C);
    #endif
Marc's avatar
Marc committed
68
}