correct title length
[libreriscv.git] / simple_v_extension / specification / mv.x.rst
1 MV.X and MV.swizzle
2 ===================
3
4 swizzle needs a MV. see below for a potential way to use the funct7 to do a swizzle in rs2.
5
6 +---------------+-------------+-------+----------+----------+--------+----------+--------+--------+
7 | Encoding | 31:27 | 26:25 | 24:20 | 19:15 | 14:12 | 11:7 | 6:2 | 1:0 |
8 +---------------+-------------+-------+----------+----------+--------+----------+--------+--------+
9 | RV32-I-type + imm[11:0] + rs1[4:0] + funct3 | rd[4:0] + opcode + 0b11 |
10 +---------------+-------------+-------+----------+----------+--------+----------+--------+--------+
11 | RV32-I-type + fn4[3:0] + swizzle[7:0] + rs1[4:0] + 0b000 | rd[4:0] + OP-V + 0b11 |
12 +---------------+-------------+-------+----------+----------+--------+----------+--------+--------+
13
14 * funct3 = MV: 0b000 for FP, 0b001 for INT
15 * OP-V = 0b1010111
16 * fn4 = 4 bit function.
17 * fn4 = 0b0000 - MV-SWIZZLE
18 * fn4 = 0bNN01 - MV-X, NN=elwidth (default/8/16/32)
19 * fn4 = 0bNN11 - MV-X.SUBVL NN=elwidth (default/8/16/32)
20
21 swizzle (only active on SV or P48/P64 when SUBVL!=0):
22
23 +-----+-----+-----+-----+
24 | 7:6 | 5:4 | 3:2 | 1:0 |
25 +-----+-----+-----+-----+
26 | w | z | y | x |
27 +-----+-----+-----+-----+
28
29 MV.X has two modes: SUBVL mode applies the element offsets only within a SUBVL inner loop. This can be used for transposition.
30
31 ::
32
33 for i in range(VL):
34 for j in range(SUBVL):
35 regs[rd] = regs[rd+regs[rs+j]]
36
37 Normal mode will apply the element offsets incrementally:
38
39 ::
40
41 for i in range(VL):
42 for j in range(SUBVL):
43 regs[rd] = regs[rd+regs[rs+k]]
44 k++
45
46
47 Pseudocode for element width part of MV.X:
48
49 ::
50
51 def mv_x(rd, rs1, funct4):
52 elwidth = (funct4>>2) & 0x3
53 bitwidth = {0:XLEN, 1:8, 2:16, 3:32}[elwidth] # get bits per el
54 bytewidth = bitwidth / 8 # get bytes per el
55 for i in range(VL):
56 addr = (unsigned char *)&regs[rs1]
57 offset = addr + bytewidth # get offset within regfile as SRAM
58 # TODO, actually, needs to respect rd and rs1 element width,
59 # here, as well. this pseudocode just illustrates that the
60 # MV.X operation contains a way to compact the indices into
61 # less space.
62 regs[rd] = (unsigned char*)(regs)[offset]
63
64 The idea here is to allow 8-bit indices to be stored inside XLEN-sized
65 registers, such that rather than doing this:
66
67 .. parsed-literal::
68 ldimm x8, 1
69 ldimm x9, 3
70 ldimm x10, 2
71 ldimm x11, 0
72 {SVP.VL=4} MV.X x3, x8, elwidth=default
73
74 The alternative is this:
75
76 .. parsed-literal::
77 ldimm x8, 0x00020301
78 {SVP.VL=4} MV.X x3, x8, elwidth=8
79
80 Thus compacting four indices into the one register. x3 and x8's element
81 width are *independent* of the MV.X elwidth, thus allowing both source
82 and element element widths of the *elements* to be moved to be over-ridden,
83 whilst *at the same time* allowing the *indices* to be compacted, as well.
84
85 ----
86
87 potential MV.X? register-version of MV-swizzle?
88
89 +-------------+-------+-------+----------+----------+--------+----------+--------+--------+
90 | Encoding | 31:27 | 26:25 | 24:20 | 19:15 | 14:12 | 11:7 | 6:2 | 1:0 |
91 +-------------+-------+-------+----------+----------+--------+----------+--------+--------+
92 | RV32-R-type + funct7 + rs2[4:0] + rs1[4:0] + funct3 | rd[4:0] + opcode + 0b11 |
93 +-------------+-------+-------+----------+----------+--------+----------+--------+--------+
94 | RV32-R-type + 0b0000000 + rs2[4:0] + rs1[4:0] + 0b001 | rd[4:0] + OP-V + 0b11 |
95 +-------------+-------+-------+----------+----------+--------+----------+--------+--------+
96
97 * funct3 = MV.X
98 * OP-V = 0b1010111
99 * funct7 = 0b000NN00 - INT MV.X, elwidth=NN (default/8/16/32)
100 * funct7 = 0b000NN10 - FP MV.X, elwidth=NN (default/8/16/32)
101 * funct7 = 0b0000001 - INT MV.swizzle to say that rs2 is a swizzle argument?
102 * funct7 = 0b0000011 - FP MV.swizzle to say that rs2 is a swizzle argument?
103
104 question: do we need a swizzle MV.X as well?
105
106 macro-op fusion
107 ===============
108
109 there is the potential for macro-op fusion of mv-swizzle with the following instruction and/or preceding instruction.
110 <http://lists.libre-riscv.org/pipermail/libre-riscv-dev/2019-August/002486.html>
111
112 VBLOCK context?
113 ===============
114
115 additional idea: a VBLOCK context that says that if a given register is used, it indicates that the
116 register is to be "swizzled", and the VBLOCK swizzle context contains the swizzling to be carried out.
117
118 mm_shuffle_ps?
119 ==============
120
121 __m128 _mm_shuffle_ps(__m128 lo,__m128 hi,
122 _MM_SHUFFLE(hi3,hi2,lo1,lo0))
123 Interleave inputs into low 2 floats and high 2 floats of output. Basically
124 out[0]=lo[lo0];
125 out[1]=lo[lo1];
126 out[2]=hi[hi2];
127 out[3]=hi[hi3];
128
129 For example, _mm_shuffle_ps(a,a,_MM_SHUFFLE(i,i,i,i)) copies the float
130 a[i] into all 4 output floats.
131
132 Transpose
133 =========
134
135 assuming a vector of 4x4 matrixes is stored as 4 separate vectors with subvl=4 in struct-of-array-of-struct form (the form I've been planning on using):
136 using standard (4+4) -> 4 swizzle instructions with 2 input vectors with subvl=4 and 1 output vector with subvl, a vectorized matrix transpose operation can be done in 2 steps with 4 instructions per step to give 8 instructions in total:
137
138 input:
139 | m00 m10 m20 m30 |
140 | m01 m11 m21 m31 |
141 | m02 m12 m22 m32 |
142 | m03 m13 m23 m33 |
143
144 transpose 4 corner 2x2 matrices
145
146 intermediate:
147 | m00 m01 m20 m21 |
148 | m10 m11 m30 m31 |
149 | m02 m03 m22 m23 |
150 | m12 m13 m32 m33 |
151
152 finish transpose
153
154 output:
155 | m00 m01 m02 m03 |
156 | m10 m11 m12 m13 |
157 | m20 m21 m22 m23 |
158 | m30 m31 m32 m33 |
159
160 <http://web.archive.org/web/20100111104515/http://www.randombit.net:80/bitbashing/programming/integer_matrix_transpose_in_sse2.html>
161
162
163 ::
164
165 __m128i T0 = _mm_unpacklo_epi32(I0, I1);
166 __m128i T1 = _mm_unpacklo_epi32(I2, I3);
167 __m128i T2 = _mm_unpackhi_epi32(I0, I1);
168 __m128i T3 = _mm_unpackhi_epi32(I2, I3);
169
170 /* Assigning transposed values back into I[0-3] */
171 I0 = _mm_unpacklo_epi64(T0, T1);
172 I1 = _mm_unpackhi_epi64(T0, T1);
173 I2 = _mm_unpacklo_epi64(T2, T3);
174 I3 = _mm_unpackhi_epi64(T2, T3);
175
176 Transforms for DCT
177 ==================
178
179 <https://opencores.org/websvn/filedetails?repname=mpeg2fpga&path=%2Fmpeg2fpga%2Ftrunk%2Frtl%2Fmpeg2%2Fidct.v>
180