-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatmul.s
More file actions
109 lines (90 loc) · 2.04 KB
/
matmul.s
File metadata and controls
109 lines (90 loc) · 2.04 KB
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
.globl matmul
.text
# =======================================================
# FUNCTION: Matrix Multiplication of 2 integer matrices
# d = matmul(m0, m1)
# If the dimensions don't match, exit with exit code 2
# Arguments:
# a0 is the pointer to the start of m0
# a1 is the # of rows (height) of m0
# a2 is the # of columns (width) of m0
# a3 is the pointer to the start of m1
# a4 is the # of rows (height) of m1
# a5 is the # of columns (width) of m1
# a6 is the pointer to the the start of d
# Returns:
# None, sets d = matmul(m0, m1)
# =======================================================
matmul:
# Error if mismatched dimensions
bne a2, a4, mismatched_dimensions
# Prologue save all s's and ra
addi sp, sp, -52
sw ra, 0(sp)
sw s0, 4(sp)
sw s1, 8(sp)
sw s2, 12(sp)
sw s3, 16(sp)
sw s4, 20(sp)
sw s5, 24(sp)
sw s6, 28(sp)
sw s7, 32(sp)
sw s8, 36(sp)
sw s9, 40(sp)
sw s10, 44(sp)
sw s11, 48(sp)
mv s0, a0
mv s1, a1 #ht of d
mv s2, a2
mv s3, a3
mv s4, a4
mv s5, a5 #width of d (dimensions of d -> s1*s5) (stride for m1 as well)
mv s6, a6
add s7, x0, x0 #index of d
add s8, x0, x0 #outer loop index: i
outer_loop_start:
bge s8, s1, outer_loop_end
add s9, x0, x0 #inner loop index: j
mul t0, s8, s2
slli t0, t0, 2
add s10, s0, t0 #ptr0
inner_loop_start:
bge s9, s5, inner_loop_end
slli t1, s9, 2
add s11, t1, s3 #ptr1
#dot Call prep
mv a0, s10
mv a1, s11
mv a2, s2
li a3, 1
mv a4, s5
jal ra, dot #dot call
slli t2, s7, 2
add t2, t2, s6
sw a0, 0(t2)
addi s7, s7, 1 #index of d++
addi s9, s9, 1 #j++
j inner_loop_start
inner_loop_end:
addi s8, s8, 1 #i++
j outer_loop_start
outer_loop_end:
# Epilogue
lw ra, 0(sp)
lw s0, 4(sp)
lw s1, 8(sp)
lw s2, 12(sp)
lw s3, 16(sp)
lw s4, 20(sp)
lw s5, 24(sp)
lw s6, 28(sp)
lw s7, 32(sp)
lw s8, 36(sp)
lw s9, 40(sp)
lw s10, 44(sp)
lw s11, 48(sp)
addi sp, sp, 52
ret
mismatched_dimensions:
li a1 2
jal exit2