429638e7 |
1 | /* Copyright (c) 2014, Ian Sutton <ian@kremlin.cc> |
2 | * |
3 | * Permission to use, copy, modify, and/or distribute this software for |
4 | * any purpose with or without fee is hereby granted, provided that the |
5 | * above copyright notice and this permission notice appear in all |
6 | * copies. |
7 | * |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL |
9 | * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED |
10 | * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE |
11 | * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
12 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
13 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
14 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
15 | * PERFORMANCE OF THIS SOFTWARE. |
16 | * |
17 | * This code implements a simple matrix operation in C, to be |
18 | * cross-compiled into Loongson MIPS 32-bit instructions. |
19 | */ |
20 | |
21 | #ifdef DEBUG |
22 | #include <stdio.h> |
23 | #endif |
24 | |
25 | #include "proto.h" |
26 | |
27 | int main(int argc, char *argv[]) { |
28 | |
29 | #ifdef HARDCODED_OPERANDS |
30 | |
31 | /* volatile, otherwise GCC optimizations muddy things up. 'a' and |
32 | * 'b' are our starting matricies */ |
33 | volatile int a[MS][MS] = { |
34 | { 1, 2, 3}, |
35 | { 4, 5, 6}, |
36 | { 7, 8, 9} |
37 | }; |
38 | |
39 | volatile int b[MS][MS] = { |
40 | { 1, 4, 7}, |
41 | { 2, 5, 8}, |
42 | { 3, 6, 9} |
43 | }; |
44 | #endif |
45 | |
46 | /* 'd' and 'c' are matricies that store the result of the |
47 | * combinations of 'a' and 'b' prior to multiplication */ |
48 | volatile int c[MS][MS], d[MS][MS]; |
49 | |
50 | /* where our resultant matrix is stored */ |
51 | volatile int result[MS][MS]; |
52 | |
53 | /* zero-out our result buffers */ |
54 | matrix_init0(c); |
55 | matrix_init0(d); |
56 | matrix_init0(result); |
57 | |
58 | /* first we combine A + B and store it in C */ |
59 | matrix_add(a, b, c); |
60 | |
61 | /* then we subtract A - B and store it in D */ |
62 | matrix_sub(a, b, d); |
63 | |
64 | /* finally, we multiply A * B and store it in result */ |
65 | matrix_mult(a, b, result); |
66 | |
67 | #ifdef DEBUG |
68 | |
69 | printf("Matrix A:\n"); |
70 | matrix_print(a); |
71 | |
72 | printf("\nMatrix B:\n"); |
73 | matrix_print(b); |
74 | |
75 | printf("\nAdded Matrix (A + B) -> C:\n"); |
76 | matrix_print(c); |
77 | |
78 | printf("\nSubtracted Matrix (A - B) -> D:\n"); |
79 | matrix_print(d); |
80 | |
81 | printf("\nMultiplied Matrix (C * D) -> Result:\n"); |
82 | matrix_print(result); |
83 | |
84 | #endif |
85 | |
86 | return 0; |
87 | } |
88 | |
89 | static void matrix_add(volatile int op1[MS][MS], volatile int op2[MS][MS], volatile int result_buf[MS][MS]) { |
90 | |
91 | int i, j; |
92 | |
93 | i = 0; |
94 | j = 0; |
95 | |
96 | for(; i < MS; i++) { |
97 | |
98 | j = 0; |
99 | |
100 | for(; j < MS; j++) |
101 | result_buf[i][j] = op1[i][j] + op2[i][j]; |
102 | } |
103 | } |
104 | |
105 | static void matrix_sub(volatile int op1[MS][MS], volatile int op2[MS][MS], volatile int result_buf[MS][MS]) { |
106 | |
107 | int i, j; |
108 | |
109 | i = 0; |
110 | j = 0; |
111 | |
112 | for(; i < MS; i++) { |
113 | |
114 | j = 0; |
115 | |
116 | for(; j < MS; j++) |
117 | result_buf[i][j] = op1[i][j] - op2[i][j]; |
118 | } |
119 | } |
120 | |
121 | static void matrix_mult(volatile int op1[MS][MS], volatile int op2[MS][MS], volatile int result_buf[MS][MS]) { |
122 | |
123 | int i, j, k; |
124 | |
125 | i = 0; |
126 | j = 0; |
127 | k = 0; |
128 | |
129 | for(; i < MS; i++) { |
130 | |
131 | j = 0; |
132 | |
133 | for(; j < MS; j++) { |
134 | |
135 | k = 0; |
136 | |
137 | for(; k < MS; k++) |
138 | result_buf[i][j] += op1[i][k] * op2[k][j]; |
139 | } |
140 | } |
141 | } |
142 | |
143 | static void matrix_init0(volatile int matrix[MS][MS]) { |
144 | |
145 | int i, j; |
146 | |
147 | i = 0; |
148 | j = 0; |
149 | |
150 | for(; i < MS; i++) { |
151 | |
152 | j = 0; |
153 | |
154 | for(; j < MS; j++) |
155 | matrix[i][j] = 0; |
156 | } |
157 | } |
158 | |
159 | #ifdef DEBUG |
160 | static void matrix_print(volatile int matrix[MS][MS]) { |
161 | |
162 | int i,j; |
163 | |
164 | i = 0; |
165 | j = 0; |
166 | |
167 | printf("\t┌────────────┐\n"); |
168 | |
169 | for(; i < MS; i++) { |
170 | |
171 | printf("\t│"); |
172 | j = 0; |
173 | |
174 | for(; j < MS; j++) { |
175 | |
176 | printf("%3d", matrix[i][j]); |
177 | |
178 | if(j + 1 != MS) |
179 | printf(" "); |
180 | } |
181 | |
182 | printf(" │\n"); |
183 | } |
184 | |
185 | printf("\t└────────────┘\n"); |
186 | } |
187 | #endif |