retab for gitweb
[cse-arch-proj1.git] / src / proto.c
CommitLineData
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
27int main(int argc, char *argv[]) {
28
bf3227c4 29 #ifdef HARDCODED_OPERANDS
429638e7 30
bf3227c4 31 /* volatile, otherwise GCC optimizations muddy things up. 'a' and
429638e7 32 * 'b' are our starting matricies */
bf3227c4 33 volatile int a[MS][MS] = {
34 { 1, 2, 3},
35 { 4, 5, 6},
36 { 7, 8, 9}
37 };
429638e7 38
bf3227c4 39 volatile int b[MS][MS] = {
40 { 1, 4, 7},
41 { 2, 5, 8},
42 { 3, 6, 9}
43 };
44 #endif
429638e7 45
bf3227c4 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];
429638e7 49
bf3227c4 50 /* where our resultant matrix is stored */
51 volatile int result[MS][MS];
429638e7 52
bf3227c4 53 /* zero-out our result buffers */
54 matrix_init0(c);
55 matrix_init0(d);
56 matrix_init0(result);
429638e7 57
bf3227c4 58 /* first we combine A + B and store it in C */
59 matrix_add(a, b, c);
429638e7 60
bf3227c4 61 /* then we subtract A - B and store it in D */
62 matrix_sub(a, b, d);
429638e7 63
bf3227c4 64 /* finally, we multiply A * B and store it in result */
65 matrix_mult(a, b, result);
429638e7 66
bf3227c4 67 #ifdef DEBUG
429638e7 68
bf3227c4 69 printf("Matrix A:\n");
70 matrix_print(a);
429638e7 71
bf3227c4 72 printf("\nMatrix B:\n");
73 matrix_print(b);
429638e7 74
bf3227c4 75 printf("\nAdded Matrix (A + B) -> C:\n");
76 matrix_print(c);
429638e7 77
bf3227c4 78 printf("\nSubtracted Matrix (A - B) -> D:\n");
79 matrix_print(d);
429638e7 80
bf3227c4 81 printf("\nMultiplied Matrix (C * D) -> Result:\n");
82 matrix_print(result);
429638e7 83
bf3227c4 84 #endif
429638e7 85
bf3227c4 86 return 0;
429638e7 87}
88
89static void matrix_add(volatile int op1[MS][MS], volatile int op2[MS][MS], volatile int result_buf[MS][MS]) {
90
bf3227c4 91 int i, j;
429638e7 92
bf3227c4 93 i = 0;
94 j = 0;
429638e7 95
bf3227c4 96 for(; i < MS; i++) {
429638e7 97
bf3227c4 98 j = 0;
429638e7 99
bf3227c4 100 for(; j < MS; j++)
101 result_buf[i][j] = op1[i][j] + op2[i][j];
102 }
429638e7 103}
104
105static void matrix_sub(volatile int op1[MS][MS], volatile int op2[MS][MS], volatile int result_buf[MS][MS]) {
106
bf3227c4 107 int i, j;
429638e7 108
bf3227c4 109 i = 0;
110 j = 0;
429638e7 111
bf3227c4 112 for(; i < MS; i++) {
429638e7 113
bf3227c4 114 j = 0;
429638e7 115
bf3227c4 116 for(; j < MS; j++)
117 result_buf[i][j] = op1[i][j] - op2[i][j];
118 }
429638e7 119}
120
121static void matrix_mult(volatile int op1[MS][MS], volatile int op2[MS][MS], volatile int result_buf[MS][MS]) {
bf3227c4 122
123 int i, j, k;
429638e7 124
bf3227c4 125 i = 0;
126 j = 0;
127 k = 0;
429638e7 128
bf3227c4 129 for(; i < MS; i++) {
429638e7 130
bf3227c4 131 j = 0;
429638e7 132
bf3227c4 133 for(; j < MS; j++) {
429638e7 134
bf3227c4 135 k = 0;
429638e7 136
bf3227c4 137 for(; k < MS; k++)
138 result_buf[i][j] += op1[i][k] * op2[k][j];
139 }
140 }
429638e7 141}
142
143static void matrix_init0(volatile int matrix[MS][MS]) {
144
bf3227c4 145 int i, j;
429638e7 146
bf3227c4 147 i = 0;
148 j = 0;
429638e7 149
bf3227c4 150 for(; i < MS; i++) {
429638e7 151
bf3227c4 152 j = 0;
429638e7 153
bf3227c4 154 for(; j < MS; j++)
155 matrix[i][j] = 0;
156 }
429638e7 157}
158
159#ifdef DEBUG
160static void matrix_print(volatile int matrix[MS][MS]) {
161
bf3227c4 162 int i,j;
429638e7 163
bf3227c4 164 i = 0;
165 j = 0;
429638e7 166
bf3227c4 167 printf("\t┌────────────┐\n");
429638e7 168
bf3227c4 169 for(; i < MS; i++) {
429638e7 170
bf3227c4 171 printf("\t│");
172 j = 0;
429638e7 173
bf3227c4 174 for(; j < MS; j++) {
429638e7 175
bf3227c4 176 printf("%3d", matrix[i][j]);
429638e7 177
bf3227c4 178 if(j + 1 != MS)
179 printf(" ");
180 }
429638e7 181
bf3227c4 182 printf(" │\n");
183 }
429638e7 184
bf3227c4 185 printf("\t└────────────┘\n");
429638e7 186}
187#endif