-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample-Matrix-Multiply.c
109 lines (94 loc) · 2.21 KB
/
Example-Matrix-Multiply.c
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
102
103
104
105
106
107
108
109
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include "fp-soft-variable.h"
#define NLINE 4
#define NCOL 4
typedef float typeelt;
typedef typeelt tMatrix[NLINE][NCOL];
char printfFormat[] = "%f ";
void printMatrix(tMatrix a)
{
int line, col;
for (line = 0; line < NLINE; line++)
{
for (col = 0; col < NCOL; col++)
printf(printfFormat, a[line][col]);
printf("\n");
}
printf("\n");
}
void cleanMatrix(tMatrix a)
{
int line, col;
for (line = 0; line < NLINE; line++)
for (col = 0; col < NCOL; col++)
a[line][col] = 0;
}
void sumMatrix(tMatrix a, tMatrix b, tMatrix res)
{
int line, col;
for (line = 0; line < NLINE; line++)
for (col = 0; col < NCOL; col++)
res[line][col] = a[line][col] + b[line][col];
}
void mulMatrix(tMatrix a, tMatrix b, tMatrix res)
{
int line, col, k;
for (line = 0; line < NLINE; line++)
for (col = 0; col < NCOL; col++)
{
res[line][col] = 0;
for (k = 0; k < NCOL; k++)
res[line][col] += a[line][k] * b[k][col];
}
}
void diagMatrix(tMatrix a, typeelt value)
{
int indice, line;
indice = (NLINE < NCOL)?NLINE:NCOL;
for (line = 0; line < indice; line++)
a[line][line] = value;
}
void firstLineMatrix(tMatrix a, typeelt value)
{
int col;
for (col = 0; col < NCOL; col++)
a[0][col] = value;
}
void firstColMatrix(tMatrix a, typeelt value)
{
int line;
for (line = 0; line < NCOL; line++)
a[line][0] = value;
}
void randMatrix(tMatrix a)
{
int line, col;
for (line = 0; line < NLINE; line++)
for (col = 0; col < NCOL; col++)
a[line][col] = (typeelt) (rand () % 1000)/100.0;
}
int main(int argc, char * argv[])
{
tMatrix a, b, c;
int i;
cleanMatrix(a); cleanMatrix(b); cleanMatrix(c);
randMatrix(a); randMatrix(b);
fpSetPrecision (2);
mulMatrix(a, b, c);
printMatrix (c);
fpSetPrecision (23);
mulMatrix(a, b, c);
printMatrix (c);
printf ("Prec. :");
for (i = 0; i < 24; i++) printf ("%4d ", i);
printf ("\nFmul32 :");
for (i = 0; i < 24; i++) printf ("%4lld ", fpGetmulCount(i));
printf ("\nFadd32 :");
for (i = 0; i < 24; i++) printf ("%4lld ", fpGetaddCount(i));
printf ("\nFsub32 :");
for (i = 0; i < 24; i++) printf ("%4lld ", fpGetsubCount(i));
printf ("\n");
return 0;
}