File size: 2,633 Bytes
54c34c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include"matrix.h"


matrix mat_create(int num_of_rows, int num_of_cols){
    matrix A;
    A.num_of_rows = num_of_rows;
    A.num_of_cols = num_of_cols;
    DATA_TYPE * ddrAddr = (DATA_TYPE*) malloc( num_of_rows * num_of_cols * sizeof(DATA_TYPE));
    A.ddrAddr = ddrAddr;
    return A;
}

void mat_setValue(matrix A, int row, int col, DATA_TYPE value){
    if((row > A.num_of_rows-1) || (col > A.num_of_cols -1)){
        printf("mat_setValue: index out of bound, row = %d, col = %d\n",row,col);
        return;
    }
    if(row <0  || col < 0){
        printf("mat_setValue: negative index\n");
        return;
    }
    *(A.ddrAddr + row * A.num_of_cols + col) = value;
}

DATA_TYPE mat_getValue(matrix A, int row, int col){
    if((row > A.num_of_rows-1) || (col > A.num_of_cols -1)){
        printf("mat_getValue: index out of bound, row = %d, col = %d\n",row,col);
        return 0;
    }
        if(row <0  || col < 0){
        printf("mat_getValue: negative index\n");
        return 0;
    }
    return *(A.ddrAddr + row * A.num_of_cols + col);
}

void mat_set(matrix_Ptr A_p, void * addr, int num_of_rows, int num_of_cols){ 
    int size = num_of_cols * num_of_rows * sizeof(DATA_TYPE);
    free(A_p->ddrAddr);
    A_p->ddrAddr = (DATA_TYPE *) malloc(size);
    memcpy(A_p->ddrAddr,addr,size);
    A_p->num_of_cols = num_of_cols;
    A_p->num_of_rows = num_of_rows;
    return;
}

void mat_mul(matrix A, matrix B, matrix C, int R_shift){ //A * B = C
    if(A.num_of_cols != B.num_of_rows){
        printf("size mismatch\n");
        return;
    }
    if(A.num_of_rows != C.num_of_rows || B.num_of_cols != C.num_of_cols){
        printf("size mismatch\n");
        return;
    }
    int temp;
    int i;
    int j;
    int k;

    for (i=0;i<C.num_of_rows;i++){
//    	printf("\n");
        for(j=0;j<C.num_of_cols;j++){
            temp=0;
            for(k=0;k<A.num_of_cols;k++){
                temp = temp + mat_getValue(A,i,k)*mat_getValue(B,k,j);
            }
//            printf("temp: %d\n",temp);
            if(R_shift > 0){
            	temp = (temp+(1<<(R_shift-1))) >> R_shift; 
            }

            if (temp > MAX_LIMIT){
                temp = MAX_LIMIT;
            }
            if (temp < MIN_LIMIT){
                temp = MIN_LIMIT;
            }
//            printf("%d",temp);
            mat_setValue(C,i,j,temp);
        }
    }
};

void mat_print(matrix A){
	int i,j;
	for(i=0;i<A.num_of_rows;i++){
		printf("row %d: ",i);
		for(j=0;j<A.num_of_cols;j++){
			printf("%7d",(int)mat_getValue(A,i,j));
		}
		printf("\n");
	}
	printf("\n");
}

void mat_free(matrix A){
    free(A.ddrAddr);
}