1 /* Copyright (c) 2014, Ian Sutton <ian@kremlin.cc>
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
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.
17 * This code implements a simple matrix operation in C, to be
18 * cross-compiled into Loongson MIPS 32-bit instructions.
27 int main(int argc
, char *argv
[]) {
29 #ifdef HARDCODED_OPERANDS
31 /* volatile, otherwise GCC optimizations muddy things up. 'a' and
32 * 'b' are our starting matricies */
33 volatile int a
[MS
][MS
] = {
39 volatile int b
[MS
][MS
] = {
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
];
50 /* where our resultant matrix is stored */
51 volatile int result
[MS
][MS
];
53 /* zero-out our result buffers */
58 /* first we combine A + B and store it in C */
61 /* then we subtract A - B and store it in D */
64 /* finally, we multiply A * B and store it in result */
65 matrix_mult(a
, b
, result
);
69 printf("Matrix A:\n");
72 printf("\nMatrix B:\n");
75 printf("\nAdded Matrix (A + B) -> C:\n");
78 printf("\nSubtracted Matrix (A - B) -> D:\n");
81 printf("\nMultiplied Matrix (C * D) -> Result:\n");
89 static void matrix_add(volatile int op1
[MS
][MS
], volatile int op2
[MS
][MS
], volatile int result_buf
[MS
][MS
]) {
101 result_buf
[i
][j
] = op1
[i
][j
] + op2
[i
][j
];
105 static void matrix_sub(volatile int op1
[MS
][MS
], volatile int op2
[MS
][MS
], volatile int result_buf
[MS
][MS
]) {
117 result_buf
[i
][j
] = op1
[i
][j
] - op2
[i
][j
];
121 static void matrix_mult(volatile int op1
[MS
][MS
], volatile int op2
[MS
][MS
], volatile int result_buf
[MS
][MS
]) {
138 result_buf
[i
][j
] += op1
[i
][k
] * op2
[k
][j
];
143 static void matrix_init0(volatile int matrix
[MS
][MS
]) {
160 static void matrix_print(volatile int matrix
[MS
][MS
]) {
167 printf("\t┌────────────┐\n");
176 printf("%3d", matrix
[i
][j
]);
185 printf("\t└────────────┘\n");