From 429638e7e3010b5e58890507ed42c394e4e835e4 Mon Sep 17 00:00:00 2001 From: kremlin Date: Fri, 17 Oct 2014 16:57:34 -0400 Subject: [PATCH] initial commit, have C logic down --- project_1.txt | 25 ++++++ src/Makefile | 19 +++++ src/bin/.gitignore | 4 + src/proto.c | 187 +++++++++++++++++++++++++++++++++++++++++++++ src/proto.h | 33 ++++++++ 5 files changed, 268 insertions(+) create mode 100644 project_1.txt create mode 100644 src/Makefile create mode 100644 src/bin/.gitignore create mode 100644 src/proto.c create mode 100644 src/proto.h diff --git a/project_1.txt b/project_1.txt new file mode 100644 index 0000000..b4d0706 --- /dev/null +++ b/project_1.txt @@ -0,0 +1,25 @@ +Ian Sutton +CSE 381 +Fall 2014 + +Project 1 - MIPS Matrix Multiplier + + My approach to this project was to use my existing skills with +relatively higher-architecture operating system/kernel to better +understand the relatively lower-level CPU operations: 'top-down' rather +than 'bottom-up'. I started by writing an equivalent program in C along +with a makefile that specifies a compilation routine that results in a +rather unoptimized binary, important for later reasons. I compiled on an +intel i386 single-core 32-bit pentium chip (on an old thinkpad) in order +to get the least complicated instructions. I stripped the ELF +headers/footers and translated the sequence of opcodes into readable x86 +ASM. + +This is where I started to get a feel for how this program was working +on the instruction level. I then changed the compilation options to +instead cross-compile and target an older Longsoon 32-bit MIPS platform, +suitable for this course. After adjusting the optimization settings, I +was able to come up with an instruction file suitable for this +assignment (specifically, it did not use any MIPS64, multithreaded, or +pseudo-instructions prohibited by the handout). I then ran it through +MARS to guarantee operability on the professor's end. diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..49cb66e --- /dev/null +++ b/src/Makefile @@ -0,0 +1,19 @@ +.PHONY: all + +CC=/bin/cc +CARGS=-Wall -Werror -Wextra -pedantic -std=c99 +SANITY=-Wno-unused-parameter -Wno-unused-variable +CDBG=-g +CIO=proto.c -o bin/out.amd64.multi.bin + +# optimization is likely to produce confusing output as we are targeting readable MIPS ASM +COPT=-O0 + +# HARDCODED_OPERANDS is a boolean, MS (matrix size) is the square size of our matrices +CDEFS=-D HARDCODED_OPERANDS=1 -D MS=3 + +all: + $(CC) $(CARGS) $(SANITY) $(CDEFS) $(CIO) $(COPT) + +debug: + $(CC) $(CARGS) $(SANITY) $(CDEFS) $(CIO) $(COPT) $(CDBG) -D DEBUG=1 diff --git a/src/bin/.gitignore b/src/bin/.gitignore new file mode 100644 index 0000000..5e7d273 --- /dev/null +++ b/src/bin/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore diff --git a/src/proto.c b/src/proto.c new file mode 100644 index 0000000..f4a2544 --- /dev/null +++ b/src/proto.c @@ -0,0 +1,187 @@ +/* Copyright (c) 2014, Ian Sutton + * + * Permission to use, copy, modify, and/or distribute this software for + * any purpose with or without fee is hereby granted, provided that the + * above copyright notice and this permission notice appear in all + * copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE + * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + * + * This code implements a simple matrix operation in C, to be + * cross-compiled into Loongson MIPS 32-bit instructions. + */ + +#ifdef DEBUG +#include +#endif + +#include "proto.h" + +int main(int argc, char *argv[]) { + + #ifdef HARDCODED_OPERANDS + + /* volatile, otherwise GCC optimizations muddy things up. 'a' and + * 'b' are our starting matricies */ + volatile int a[MS][MS] = { + { 1, 2, 3}, + { 4, 5, 6}, + { 7, 8, 9} + }; + + volatile int b[MS][MS] = { + { 1, 4, 7}, + { 2, 5, 8}, + { 3, 6, 9} + }; + #endif + + /* 'd' and 'c' are matricies that store the result of the + * combinations of 'a' and 'b' prior to multiplication */ + volatile int c[MS][MS], d[MS][MS]; + + /* where our resultant matrix is stored */ + volatile int result[MS][MS]; + + /* zero-out our result buffers */ + matrix_init0(c); + matrix_init0(d); + matrix_init0(result); + + /* first we combine A + B and store it in C */ + matrix_add(a, b, c); + + /* then we subtract A - B and store it in D */ + matrix_sub(a, b, d); + + /* finally, we multiply A * B and store it in result */ + matrix_mult(a, b, result); + + #ifdef DEBUG + + printf("Matrix A:\n"); + matrix_print(a); + + printf("\nMatrix B:\n"); + matrix_print(b); + + printf("\nAdded Matrix (A + B) -> C:\n"); + matrix_print(c); + + printf("\nSubtracted Matrix (A - B) -> D:\n"); + matrix_print(d); + + printf("\nMultiplied Matrix (C * D) -> Result:\n"); + matrix_print(result); + + #endif + + return 0; +} + +static void matrix_add(volatile int op1[MS][MS], volatile int op2[MS][MS], volatile int result_buf[MS][MS]) { + + int i, j; + + i = 0; + j = 0; + + for(; i < MS; i++) { + + j = 0; + + for(; j < MS; j++) + result_buf[i][j] = op1[i][j] + op2[i][j]; + } +} + +static void matrix_sub(volatile int op1[MS][MS], volatile int op2[MS][MS], volatile int result_buf[MS][MS]) { + + int i, j; + + i = 0; + j = 0; + + for(; i < MS; i++) { + + j = 0; + + for(; j < MS; j++) + result_buf[i][j] = op1[i][j] - op2[i][j]; + } +} + +static void matrix_mult(volatile int op1[MS][MS], volatile int op2[MS][MS], volatile int result_buf[MS][MS]) { + + int i, j, k; + + i = 0; + j = 0; + k = 0; + + for(; i < MS; i++) { + + j = 0; + + for(; j < MS; j++) { + + k = 0; + + for(; k < MS; k++) + result_buf[i][j] += op1[i][k] * op2[k][j]; + } + } +} + +static void matrix_init0(volatile int matrix[MS][MS]) { + + int i, j; + + i = 0; + j = 0; + + for(; i < MS; i++) { + + j = 0; + + for(; j < MS; j++) + matrix[i][j] = 0; + } +} + +#ifdef DEBUG +static void matrix_print(volatile int matrix[MS][MS]) { + + int i,j; + + i = 0; + j = 0; + + printf("\t┌────────────┐\n"); + + for(; i < MS; i++) { + + printf("\t│"); + j = 0; + + for(; j < MS; j++) { + + printf("%3d", matrix[i][j]); + + if(j + 1 != MS) + printf(" "); + } + + printf(" │\n"); + } + + printf("\t└────────────┘\n"); +} +#endif diff --git a/src/proto.h b/src/proto.h new file mode 100644 index 0000000..2466737 --- /dev/null +++ b/src/proto.h @@ -0,0 +1,33 @@ +/* Copyright (c) 2014, Ian Sutton + * + * Permission to use, copy, modify, and/or distribute this software for + * any purpose with or without fee is hereby granted, provided that the + * above copyright notice and this permission notice appear in all + * copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE + * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + * + * This code implements a simple matrix operation in C, to be + * cross-compiled volatile into Loongson MIPS 32-bit instructions. + * + * (Header file) + */ + +static void matrix_add(volatile int op1[MS][MS], volatile int op2[MS][MS], volatile int result_buf[MS][MS]); + +static void matrix_sub(volatile int op1[MS][MS], volatile int op2[MS][MS], volatile int result_buf[MS][MS]); + +static void matrix_mult(volatile int op1[MS][MS], volatile int op2[MS][MS], volatile int result_buf[MS][MS]); + +#ifdef DEBUG +static void matrix_print(volatile int matrix[MS][MS]); +#endif + +static void matrix_init0(volatile int matrix[MS][MS]); -- 2.41.0