1 # New instructions for CR/INT predication
6 * main bugreport for crweirds
7 <https://bugs.libre-soc.org/show_bug.cgi?id=533>
8 * <https://bugs.libre-soc.org/show_bug.cgi?id=527>
9 * <https://bugs.libre-soc.org/show_bug.cgi?id=569>
10 * <https://bugs.libre-soc.org/show_bug.cgi?id=558#c47>
19 |0 |6 |9 |11|12 |16 |19 |22 |26 |31|
20 | PO | RT |M |fmsk |BFA |XO |fmap | XO |Rc|
24 * crrweird RT,BFA,M,fmsk,fmap (Rc=0)
25 * crrweird. RT,BFA,M,fmsk,fmap (Rc=1)
28 creg <- CR[4*BFA+32:4*BFA+35]
29 n <- (¬fmap ^ creg) & fmsk
30 result <- (n != 0) if M else (n == fmsk)
31 RT <- [0] * 63 || result
36 When used with SVP64 Prefixing this is a [[sv/normal]]
37 SVP64 type operation and as such can use Rc=1 and RC1 Data-dependent
40 Also as noted below, element-width override bits normally used
41 on the source is instead used to allow multiple results to be packed
42 sequentially into the destination. *Destination elwidth overrides still apply*.
44 Special registers altered:
55 |0 |6 |9 |11|12 |16 |19 |22 |26 |31|
56 | PO | RT |M |fmsk |BFA |XO |fmap | XO |Rc|
60 * mfcrrweird RT,BFA,fmsk,fmap (Rc=0)
61 * mfcrrweird. RT,BFA,fmsk,fmap (Rc=1)
64 creg = CR[4*BFA+32:4*BFA+35]
65 result = (¬fmap ^ creg) & fmsk
66 RT = [0] * 60 || result
71 When used with SVP64 Prefixing this is a [[sv/normal]]
72 SVP64 type operation and as such can use Rc=1 and RC1 Data-dependent
75 Also as noted below, element-width override bits normally used
76 on the source is instead used to allow multiple results to be packed
77 into the destination. *Destination elwidth overrides still apply*
84 |0 |6 |9 |11|12 |16 |19 |22 |26 |31|
85 | PO | RA |M |fmsk |BF |XO |fmap | XO |
86 | PO | BT |M |fmsk |BF |XO |fmap | XO |
87 | PO | BF | |M |fmsk |BF |XO |fmap | XO |
90 * mtcrrweird BF,RA,M,fmsk,fmap
95 result = (¬fmap ^ creg) & fmsk
97 result |= CR[4*BF+32:4*BF+35] & ~fmsk
98 CR[4*BF+32:4*BF+35] = result
101 When used with SVP64 Prefixing this is a [[sv/normal]]
102 SVP64 type operation and as such can use RC1 Data-dependent
105 Hardware Architectural Note: when M=1 this instruction is a Read-Modify-Write
106 on the `BF` CR Field. When M=0 it is a more normal Write.
108 Special Registers Altered:
119 |0 |6 |9 |11|12 |16 |19 |22 |26 |31|
120 | PO | RA |M |fmsk |BF |XO |fmap | XO |
121 | PO | BT |M |fmsk |BF |XO |fmap | XO |
122 | PO | BF | |M |fmsk |BF |XO |fmap | XO |
125 * mtcrweird BF,RA,M,fmsk,fmap
129 creg = reg[63] || reg[63] || reg[63] || reg[63]
130 result = (¬fmap ^ creg) & fmsk
132 result |= CR[4*BF+32:4*BF+35] & ~fmsk
133 CR[4*BF+32:4*BF+35] = result
136 Note that when M=1 this operation is a Read-Modify-Write on the CR Field
137 BF. Masked-out bits of the 4-bit CR Field BF will not be changed when
138 M=1. Correspondingly when M=0 this operation is an overwrite: no read
139 of BF is required because the masked-out bits of the BF CR Field are
142 When used with SVP64 Prefixing this is a [[sv/cr_ops]] SVP64
143 type operation that has 3-bit Data-dependent and 3-bit Predicate-result
144 capability (BF is 3 bits)
146 Special Registers Altered:
152 ## mcrfm - Move CR Field, masked.
157 |0 |6 |9 |11|12 |16 |19 |22 |26 |31|
158 | PO | BF | |M |fmsk |BF |XO |fmap | XO |
161 * mcrfm: BF,BFA,M,fmsk,fmap
164 result = fmsk & CR[4*BFA+32:4*BFA+35]
166 result |= CR[4*BF+32:4*BF+35] & ~fmsk
168 CR[4*BF+32:4*BF+35] = result
171 This instruction copies, sets, or inverts parts of a CR Field
172 into another CR Field. `mcrf` copies only one bit of the CR
173 from any arbitrary bit to any other arbitrary bit, whereas
174 `mcrfm` copies an entire 4-bit CR Field (or masked parts thereof).
175 Unlike `mcrf` the bits of the CR Field may not change position:
176 the EQ bit from the source may only go into the EQ bit of the
177 destination (optionally inverted, set, or cleared).
179 When M=1 this operation is a Read-Modify-Write on the CR Field
180 BF. Masked-out bits of the 4-bit CR Field BF will not be changed when
181 M=1. Correspondingly when M=0 this operation is an overwrite: no read
182 of BF is required because the masked-out bits of the BF CR Field are
185 When used with SVP64 Prefixing this is a [[sv/cr_ops]] SVP64
186 type operation that has 3-bit Data-dependent and 3-bit Predicate-result
187 capability (BF is 3 bits)
189 *Programmer's note: `fmap` being XORed onto the result provides
190 considerable flexibility. individual bits of BFA may be copied inverted
191 to BF by ensuring that `fmsk` and `fmap` have the same bit set. Also,
192 individual bits in BF may be set to 1 by ensuring that the required bit of
193 `fmsk` is set to zero and the same bit in `fmap` is set to 1*
195 Special Registers Altered:
204 |0 |6 |9 |11|12 |16 |19 |22 |26 |31|
205 | PO | BT |M |fmsk |BF |XO |fmap | XO |
208 * crweirder: BT,BFA,fmsk,fmap
211 creg = CR[4*BFA+32:4*BFA+35]
212 n = (¬fmap ^ creg) & fmsk
213 result = (n != 0) if M else (n == fmsk)
217 Special Registers Altered:
223 When used with SVP64 Prefixing this is a [[sv/cr_ops]] SVP64
224 type operation that has 5-bit Data-dependent
225 capability (BT is 5 bits)
227 Hardware Architectural Note: this instruction is always a Read-Modify-Write
228 on the CR Field containing `BT`.
230 **Example Pseudo-ops:**
233 mtcri BF, fmap mtcrweird BF, r0, 0, 0b1111,~fmap
234 mtcrset BF, fmsk mtcrweird BF, r0, 1, fmsk,0b0000
235 mtcrclr BF, fmsk mtcrweird BF, r0, 1, fmsk,0b1111
242 # Vectorized versions involving GPRs
244 The name "weird" refers to a minor violation of SV rules when it comes
245 to deriving the Vectorized versions of these instructions.
247 Normally the progression of the SV for-loop would move on to the
248 next register. Instead however in the scalar case these instructions
249 **remain in the same register** and insert or transfer between **bits**
250 of the scalar integer source or destination. The reason is that when
251 using CR Fields as predicate masks and there is a need to transfer
252 into a GPR, again for use as a predicate mask, the CR Field bits
253 need to be efficiently packed into that one GPR (r3, r10 or r31).
255 Further useful violation of the normal SV Elwidth override rules allows
256 for packing (or unpacking) of multiple CR test results into (or out of)
257 an Integer Element. Note that the CR (source operand) elwidth field is
258 utilised to determine the bit- packing size (1/2/4/8 with remaining
259 bits within the Integer element set to zero) whilst the INT (dest
260 operand) elwidth field still sets the Integer element size as usual
263 **sv.crrweird: RT, BB, fmsk, fmap**
267 if BB.isvec: # Vector CR Field source?
271 n = (¬fmap ^ creg) & fmsk
272 result = (n != 0) if M else (n == fmsk)
274 # TODO: RT.elwidth override to be also added here
275 # note, yes, really, the CR's elwidth field determines
276 # the bit-packing into the INT!
277 if BB.elwidth == 0b00:
278 # pack 1 result into 64-bit registers
279 iregs[RT+i][0..62] = 0
280 iregs[RT+i][63] = result # sets LSB to result
281 if BB.elwidth == 0b01:
282 # pack 2 results sequentially into INT registers
283 iregs[RT+i//2][0..61] = 0
284 iregs[RT+i//2][63-(i%2)] = result
285 if BB.elwidth == 0b10:
286 # pack 4 results sequentially into INT registers
287 iregs[RT+i//4][0..59] = 0
288 iregs[RT+i//4][63-(i%4)] = result
289 if BB.elwidth == 0b11:
290 # pack 8 results sequentially into INT registers
291 iregs[RT+i//8][0..55] = 0
292 iregs[RT+i//8][63-(i%8)] = result
294 # scalar RT destination: exceeding VL=64 is UNDEFINED
295 iregs[RT][63-i] = result # results also in scalar INT
296 # only mapreduce mode (/mr) allows continuation here
297 if not SVRM.mapreduce: break
302 * in the scalar case the CR-Vector assessment
303 is stored bit-wise starting at the LSB of the
304 destination scalar INT
305 * in the INT-vector case the results are packed into LSBs
306 of the INT Elements, the packing arrangement depending on both
307 elwidth override settings.
309 **mfcrrweird: RT, BFA, fmsk.fmap**
311 Unlike `crrweird` the results are 4-bit wide, so the packing
312 will begin to spill over to other destination elements. 8 results per
313 destination at 4-bits each still fits into destination elwidth at 32-bit,
314 but for 16-bit and 8-bit obviously this does not fit, and must split
315 across to the next element
317 When for example destination elwidth is 16-bit (0b10) the following packing
320 - SVRM bits 6:7 equal to 0b00 - one 4-bit result element packed into the
321 first 4-bits of the 16-bit destination element (in the first 4 LSBs)
322 - SVRM bits 6:7 equal to 0b01 - two 4-bit result elements packed into the
323 first 8-bits of the 16-bit destination element (in the first 8 LSBs)
324 - SVRM bits 6:7 equal to 0b10 - four 4-bit result elements packed into each
325 16-bit destination element
326 - SVRM bits 6:7 equal to 0b11 - eight 4-bit result elements, the first four
327 of which are packed into the first 16-bit destination element, the
328 second four of which are packed into the second 16-bit destination element.
330 Pseudocode example: note that dest elwidth overrides affect the
331 packing of results. BB.elwidth in effect requests how many 4-bit
332 result elements would like to be packed, but RT.elwidth determines
333 the limit. Any parts of the destination elements not containing
334 results are set to zero.
342 result = (¬fmap ^ creg) & fmsk # 4-bit result
344 # RT.elwidth override can affect the packing
345 bwid = {0b00:64, 0b01:8, 0b10:16, 0b11:32}[RT.elwidth]
346 t4, t8 = min(4, bwid//2), min(8, bwid//2)
347 # yes, really, the CR's elwidth field determines
348 # the bit-packing into the INT!
349 if BB.elwidth == 0b00:
350 # pack 1 result into 64-bit registers
352 if BB.elwidth == 0b01:
353 # pack 2 results sequentially into INT registers
354 idx, boff = i//2, i%2
355 if BB.elwidth == 0b10:
356 # pack 4 results sequentially into INT registers
357 idx, boff = i//t4, i%t4
358 if BB.elwidth == 0b11:
359 # pack 8 results sequentially into INT registers
360 idx, boff = i//t8, i%t8
362 # scalar RT destination: exceeding VL=16 is UNDEFINED
364 # store 4-bit result in Vector starting from RT
365 iregs[RT+idx][60-boff*4:63-boff*4] = result
367 # only mapreduce mode (/mr) allows continuation here
368 if not SVRM.mapreduce: break
371 # Predication Examples
373 Take the following example:
377 sv.mtcrweird/dm=r10/dz cr8.v, 0, 0b0011.0000
380 Here, RA is zero, so the source input is zero. The destination is CR Field
381 8, and the destination predicate mask indicates to target the first two
382 elements. Destination predicate zeroing is enabled, and the destination
383 predicate is only set in the 2nd bit. fmsk is 0b0011, fmap is all zeros.
385 Let us first consider what should go into element 0 (CR Field 8):
387 * The destination predicate bit is zero, and zeroing is enabled.
388 * Therefore, what is in the source is irrelevant: the result must
390 * Therefore all four bits of CR Field 8 are therefore set to zero.
392 Now the second element, CR Field 9 (CR9):
394 * Bit 2 of the destination predicate, r10, is 1. Therefore the computation
395 of the result is relevant.
396 * RA is zero therefore bit 2 is zero. fmsk is 0b0011 and fmap is 0b0000
397 * When calculating n0 thru n3 we get n0=1, n1=2, n2=0, n3=0
398 * Therefore, CR9 is set (using LSB0 ordering) to 0b0011, i.e. to fmsk.
400 It should be clear that this instruction uses bits of the integer
401 predicate to decide whether to set CR Fields to `(fmsk & ~fmap)` or
402 to zero. Thus, in effect, it is the integer predicate that has been
403 copied into the CR Fields.
405 By using twin predication, zeroing, and inversion (sm=~r3, dm=r10) for
406 example, it becomes possible to combine two Integers together in order
407 to set bits in CR Fields. Likewise there are dozens of ways that CR
408 Predicates can be used, on the same sv.mtcrweird instruction.