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

6
7
8
#ifndef N
#define N 8
#endif
Marc's avatar
Marc committed
9
10
11
12
13
14
15
16
17
18
19
int computeCell(int a, int b){

    int r;
    asm("smul %1, %0, %0" 
            : "=r"(r) 
            : "r"(a), "0"(b));

    //printf("a: %#010x\nb: %#010x\nr: %#010x\n",a,b,r);
    return r;
}

20
21
22
23
24
25
26
27
28
int computeSum(int a, int b) {
    int r;
    //sum sum a b
    asm("add %1, %0, %0" 
            : "=r"(r) 
            : "r"(a), "0"(b));
    return r;
}

Marc's avatar
Marc committed
29
30
31
32
33
34
35
int main()
{
    char string[3*(3+(6*N*N+N))];
    int pos = 0;
	unsigned char A[N][N], B[N][N], C[N][N];
	srand(N);

Marc's avatar
Marc committed
36

Marc's avatar
Marc committed
37
38
	for(int i=0; i<N; i++)
	    for(int j=0; j<N; j++) {
Marc's avatar
Marc committed
39
40
	        A[i][j] = rand()%10;
	        B[j][i] = rand()%10;
Marc's avatar
Marc committed
41
42
        }

Marc's avatar
Marc committed
43
    int sum = 0;
44
45
46
47
48
49
    int aux; 

	puts("TEST BEGIN");
    asm("nop");
    asm("srl %i0, %o1, %g2");
    asm("nop");
Marc's avatar
Marc committed
50
    for(int i=0; i<N; i++)
Marc's avatar
Marc committed
51
        for(int j=0; j<N; j++){
52
53
54
55
            for(int k=0; k<N/4; k++){
                aux = computeCell(*((int *) &A[i][k*4]),*((int *) &B[j][k*4]));
                sum = computeSum(aux, sum);
            }
Marc's avatar
Marc committed
56
            C[i][j] = sum;
Marc's avatar
Marc committed
57
            sum = 0;
Marc's avatar
Marc committed
58
        }
59
60
61
62
    asm("nop");
    asm("srl %i0, %o1, %g2");
    asm("nop");
	puts("TEST END");
Marc's avatar
Marc committed
63
64
65
66
67
68
69
70
71

	pos += sprintf(&string[pos],"A:\n");
	for(int i=0; i<N; i++){
	    for(int j=0; j<N; j++)
            pos+=sprintf(&string[pos],"%d ", A[i][j]);
	    pos+=sprintf(&string[pos],"\n");
    }

	pos += sprintf(&string[pos],"B:\n");
Marc's avatar
Marc committed
72
73
74
    for(int j=0; j<N; j++){
        for(int i=0; i<N; i++)
            pos+=sprintf(&string[pos],"%d ", B[i][j]);
Marc's avatar
Marc committed
75
76
77
78
79
80
81
82
83
84
85
	    pos+=sprintf(&string[pos],"\n");
    }

	pos += sprintf(&string[pos],"C:\n");
	for(int i=0; i<N; i++){
	    for(int j=0; j<N; j++)
            pos+=sprintf(&string[pos],"%d ", C[i][j]);
	    pos+=sprintf(&string[pos],"\n");
    }
    
    puts(string);
86
    puts("END OF SIMULATION");
Marc's avatar
Marc committed
87
}