whoops re-added accidentally-deleted CSV file
[openpower-isa.git] / src / openpower / decoder / isa / test_caller_svp64_matrix.py
1 from nmigen import Module, Signal
2 from nmigen.back.pysim import Simulator, Delay, Settle
3 from nmutil.formaltest import FHDLTestCase
4 import unittest
5 from openpower.decoder.isa.caller import ISACaller
6 from openpower.decoder.power_decoder import (create_pdecode)
7 from openpower.decoder.power_decoder2 import (PowerDecode2)
8 from openpower.simulator.program import Program
9 from openpower.decoder.isa.caller import ISACaller, SVP64State
10 from openpower.decoder.selectable_int import SelectableInt
11 from openpower.decoder.orderedset import OrderedSet
12 from openpower.decoder.isa.all import ISA
13 from openpower.decoder.isa.test_caller import Register, run_tst
14 from openpower.sv.trans.svp64 import SVP64Asm
15 from openpower.consts import SVP64CROffs
16 from copy import deepcopy
17 from openpower.decoder.helpers import fp64toselectable
18 from functools import reduce
19 import operator
20
21
22 class DecoderTestCase(FHDLTestCase):
23
24 def _check_regs(self, sim, expected):
25 for i in range(32):
26 self.assertEqual(sim.gpr(i), SelectableInt(expected[i], 64))
27
28 def test_sv_remap1(self):
29 """>>> lst = ["svshape 2, 2, 3, 0, 0",
30 "svremap 31, 1, 2, 3, 0, 0, 0",
31 "sv.fmadds *0, *8, *16, *0"
32 ]
33 REMAP fmadds FRT, FRA, FRC, FRB
34 """
35 lst = SVP64Asm(["svshape 2, 2, 3, 0, 0",
36 "svremap 31, 1, 2, 3, 0, 0, 0",
37 "sv.fmadds *0, *16, *32, *0"
38 ])
39 lst = list(lst)
40
41 fprs = [0] * 64
42 # 3x2 matrix
43 X1 = [[1, 2, 3],
44 [3, 4, 5],
45 ]
46 # 2x3 matrix
47 Y1 = [[6, 7],
48 [8, 9],
49 [10, 11],
50 ]
51
52 X = X1
53 Y = Y1
54
55 xf = reduce(operator.add, X)
56 yf = reduce(operator.add, Y)
57 print ("flattened X,Y")
58 print ("\t", xf)
59 print ("\t", yf)
60
61 # and create a linear result2, same scheme
62 #result1 = [0] * (ydim1*xdim2)
63
64
65 res = []
66 # store FPs
67 for i, x in enumerate(xf):
68 fprs[i+16] = fp64toselectable(float(x)) # X matrix
69 for i, y in enumerate(yf):
70 fprs[i+32] = fp64toselectable(float(y)) # Y matrix
71 continue
72 #t = DOUBLE2SINGLE(fp64toselectable(t)) # convert to Power single
73 #u = DOUBLE2SINGLE(fp64toselectable(u)) # from double
74 #res.append((t, u))
75 #print ("FFT", i, "in", a, b, "coeff", c, "mul",
76 # mul, "res", t, u)
77
78 with Program(lst, bigendian=False) as program:
79 sim = self.run_tst_program(program, initial_fprs=fprs)
80 print ("spr svshape0", sim.spr['SVSHAPE0'])
81 print (" xdimsz", sim.spr['SVSHAPE0'].xdimsz)
82 print (" ydimsz", sim.spr['SVSHAPE0'].ydimsz)
83 print (" zdimsz", sim.spr['SVSHAPE0'].zdimsz)
84 print ("spr svshape1", sim.spr['SVSHAPE1'])
85 print ("spr svshape2", sim.spr['SVSHAPE2'])
86 print ("spr svshape3", sim.spr['SVSHAPE3'])
87 for i in range(4):
88 print ("i", i, float(sim.fpr(i)))
89 # confirm that the results are as expected
90 #for i, (t, u) in enumerate(res):
91 # self.assertEqual(sim.fpr(i+2), t)
92 # self.assertEqual(sim.fpr(i+6), u)
93
94 def test_sv_remap2(self):
95 """>>> lst = ["svshape 5, 4, 3, 0, 0",
96 "svremap 31, 1, 2, 3, 0, 0, 0",
97 "sv.fmadds *0, *8, *16, *0"
98 ]
99 REMAP fmadds FRT, FRA, FRC, FRB
100 """
101 lst = SVP64Asm(["svshape 4, 3, 3, 0, 0",
102 "svremap 31, 1, 2, 3, 0, 0, 0",
103 "sv.fmadds *0, *16, *32, *0"
104 ])
105 lst = list(lst)
106
107 # 3x2 matrix
108 X1 = [[1, 2, 3],
109 [3, 4, 5],
110 ]
111 # 2x3 matrix
112 Y1 = [[6, 7],
113 [8, 9],
114 [10, 11],
115 ]
116
117 #### test matrices 2
118 # 3x3 matrix
119 X2 = [[12,7,3],
120 [4 ,5,6],
121 [7 ,8,9],
122 ]
123 # 3x4 matrix
124 Y2 = [[5,8,1,2],
125 [6,7,3,0],
126 [4,5,9,1]]
127
128 #### test matrices 3
129 # 3x4 matrix
130 X3 = [[12,7,3],
131 [4 ,5,6],
132 [7 ,8,9],
133 [2 ,0,1]]
134 # 3x5 matrix
135 Y3 = [[5,8,1,2,3],
136 [6,7,3,0,9],
137 [4,5,9,1,2]]
138
139 X = X2
140 Y = Y2
141
142 # get the dimensions of the 2 matrices
143 xdim1 = len(X[0])
144 ydim1 = len(X)
145 xdim2 = len(Y[0])
146 ydim2 = len(Y)
147
148 print ("xdim2 ydim1 ydim2", xdim2, ydim1, ydim2)
149
150 xf = reduce(operator.add, X)
151 yf = reduce(operator.add, Y)
152 print ("flattened X,Y")
153 print ("\t", xf)
154 print ("\t", yf)
155
156 # and create a linear result2, same scheme
157 #result1 = [0] * (ydim1*xdim2)
158
159
160 res = []
161 # store FPs
162 fprs = [0] * 64
163 for i, x in enumerate(xf):
164 fprs[i+16] = fp64toselectable(float(x)) # X matrix
165 for i, y in enumerate(yf):
166 fprs[i+32] = fp64toselectable(float(y)) # Y matrix
167 continue
168 #t = DOUBLE2SINGLE(fp64toselectable(t)) # convert to Power single
169 #u = DOUBLE2SINGLE(fp64toselectable(u)) # from double
170 #res.append((t, u))
171 #print ("FFT", i, "in", a, b, "coeff", c, "mul",
172 # mul, "res", t, u)
173
174 with Program(lst, bigendian=False) as program:
175 sim = self.run_tst_program(program, initial_fprs=fprs)
176 print ("spr svshape0", sim.spr['SVSHAPE0'])
177 print (" xdimsz", sim.spr['SVSHAPE0'].xdimsz)
178 print (" ydimsz", sim.spr['SVSHAPE0'].ydimsz)
179 print (" zdimsz", sim.spr['SVSHAPE0'].zdimsz)
180 print ("spr svshape1", sim.spr['SVSHAPE1'])
181 print ("spr svshape2", sim.spr['SVSHAPE2'])
182 print ("spr svshape3", sim.spr['SVSHAPE3'])
183 for i in range(16):
184 print ("i", i, float(sim.fpr(i)))
185 # confirm that the results are as expected
186 #for i, (t, u) in enumerate(res):
187 # self.assertEqual(sim.fpr(i+2), t)
188 # self.assertEqual(sim.fpr(i+6), u)
189
190 def run_tst_program(self, prog, initial_regs=None,
191 svstate=None,
192 initial_mem=None,
193 initial_fprs=None):
194 if initial_regs is None:
195 initial_regs = [0] * 32
196 simulator = run_tst(prog, initial_regs, mem=initial_mem,
197 initial_fprs=initial_fprs,
198 svstate=svstate)
199
200 print ("GPRs")
201 simulator.gpr.dump()
202 print ("FPRs")
203 simulator.fpr.dump()
204
205 return simulator
206
207
208 if __name__ == "__main__":
209 unittest.main()