(no commit message)
[libreriscv.git] / openpower / sv / normal.mdwn
1 [[!tag standards]]
2
3 # Normal SVP64 Modes, for Arithmetic and Logical Operations
4
5 * <https://bugs.libre-soc.org/show_bug.cgi?id=574>
6 * <https://bugs.libre-soc.org/show_bug.cgi?id=558#c47>
7 * [[svp64]]
8
9 Normal SVP64 Mode covers Arithmetic and Logical operations
10 to provide suitable additional behaviour. The Mode
11 field is bits 19-23 of the [[svp64]] RM Field.
12
13 Table of contents:
14
15 [[!toc]]
16
17 # Mode
18
19 Mode is an augmentation of SV behaviour, providing additional
20 functionality. Some of these alterations are element-based (saturation), others involve post-analysis (predicate result) and others are Vector-based (mapreduce, fail-on-first).
21
22 [[sv/ldst]],
23 [[sv/cr_ops]] and [[sv/branches]] are covered separately: the following
24 Modes apply to Arithmetic and Logical SVP64 operations:
25
26 * **simple** mode is straight vectorisation. no augmentations: the vector comprises an array of independently created results.
27 * **ffirst** or data-dependent fail-on-first: see separate section. the vector may be truncated depending on certain criteria.
28 *VL is altered as a result*.
29 * **sat mode** or saturation: clamps each element result to a min/max rather than overflows / wraps. allows signed and unsigned clamping for both INT
30 and FP.
31 * **reduce mode**. if used correctly, a mapreduce (or a prefix sum)
32 is performed. see [[svp64/appendix]].
33 note that there are comprehensive caveats when using this mode.
34 * **pred-result** will test the result (CR testing selects a bit of CR and inverts it, just like branch conditional testing) and if the test fails it
35 is as if the
36 *destination* predicate bit was zero even before starting the operation.
37 When Rc=1 the CR element however is still stored in the CR regfile, even if the test failed. See appendix for details.
38 * **Pack/Unpack** mode, only available when SUBVL is vec2/3/4, performs
39 basic structure packing on sub-elements. Bits 4-5 (normally elwidth) are
40 taken up as Pack/Unpack bits. Saturation may be simultaneously enabled.
41
42 Note that ffirst and reduce modes are not anticipated to be high-performance in some implementations. ffirst due to interactions with VL, and reduce due to it requiring additional operations to produce a result. simple, saturate and pred-result are however inter-element independent and may easily be parallelised to give high performance, regardless of the value of VL.
43
44 The Mode table for Arithmetic and Logical operations
45 is laid out as follows:
46
47 | 0-1 | 2 | 3 4 | description |
48 | --- | --- |---------|-------------------------- |
49 | 00 | 0 | dz sz | simple mode |
50 | 00 | 1 | 0 RG | scalar reduce mode (mapreduce) |
51 | 00 | 1 | 1 / | reserved |
52 | 01 | inv | CR-bit | Rc=1: ffirst CR sel |
53 | 01 | inv | VLi RC1 | Rc=0: ffirst z/nonz |
54 | 10 | N | dz sz | sat mode: N=0/1 u/s |
55 | 11 | inv | CR-bit | Rc=1: pred-result CR sel |
56 | 11 | inv | zz RC1 | Rc=0: pred-result z/nonz |
57
58 Fields:
59
60 * **sz / dz** if predication is enabled will put zeros into the dest (or as src in the case of twin pred) when the predicate bit is zero. otherwise the element is ignored or skipped, depending on context.
61 * **zz**: both sz and dz are set equal to this flag
62 * **inv CR bit** just as in branches (BO) these bits allow testing of a CR bit and whether it is set (inv=0) or unset (inv=1)
63 * **RG** inverts the Vector Loop order (VL-1 downto 0) rather
64 than the normal 0..VL-1
65 * **N** sets signed/unsigned saturation.
66 * **RC1** as if Rc=1, stores CRs *but not the result*
67 * **VLi** VL inclusive: in fail-first mode, the truncation of
68 VL *includes* the current element at the failure point rather
69 than excludes it from the count.
70
71 For LD/ST Modes, see [[sv/ldst]]. For Condition Registers
72 see [[sv/cr_ops]].
73 For Branch modes, see [[sv/branches]].
74
75 # Rounding, clamp and saturate
76
77 See [[av_opcodes]] for relevant opcodes and use-cases.
78
79 To help ensure that audio quality is not compromised by overflow,
80 "saturation" is provided, as well as a way to detect when saturation
81 occurred if desired (Rc=1). When Rc=1 there will be a *vector* of CRs,
82 one CR per element in the result (Note: this is different from VSX which
83 has a single CR per block).
84
85 When N=0 the result is saturated to within the maximum range of an
86 unsigned value. For integer ops this will be 0 to 2^elwidth-1. Similar
87 logic applies to FP operations, with the result being saturated to
88 maximum rather than returning INF, and the minimum to +0.0
89
90 When N=1 the same occurs except that the result is saturated to the min
91 or max of a signed result, and for FP to the min and max value rather
92 than returning +/- INF.
93
94 When Rc=1, the CR "overflow" bit is set on the CR associated with the
95 element, to indicate whether saturation occurred. Note that due to
96 the hugely detrimental effect it has on parallel processing, XER.SO is
97 **ignored** completely and is **not** brought into play here. The CR
98 overflow bit is therefore simply set to zero if saturation did not occur,
99 and to one if it did.
100
101 Note also that saturate on operations that set OE=1 must raise an
102 Illegal Instruction due to the conflicting use of the CR.so bit for
103 storing if
104 saturation occurred. Integer Operations that produce a Carry-Out (CA, CA32):
105 these two bits will be `UNDEFINED` if saturation is also requested.
106
107 Note that the operation takes place at the maximum bitwidth (max of
108 src and dest elwidth) and that truncation occurs to the range of the
109 dest elwidth.
110
111 *Programmer's Note: Post-analysis of the Vector of CRs to find out if any given element hit
112 saturation may be done using a mapreduced CR op (cror), or by using the
113 new crrweird instruction with Rc=1, which will transfer the required
114 CR bits to a scalar integer and update CR0, which will allow testing
115 the scalar integer for nonzero. see [[sv/cr_int_predication]]*
116
117 # Reduce mode
118
119 Reduction in SVP64 is similar in essence to other Vector Processing
120 ISAs, but leverages the underlying scalar Base v3.0B operations.
121 Thus it is more a convention that the programmer may utilise to give
122 the appearance and effect of a Horizontal Vector Reduction. Due
123 to the unusual decoupling it is also possible to perform
124 prefix-sum (Fibonacci Series) in certain circumstances. Details are in the [[svp64/appendix]]
125
126 Reduce Mode should not be confused with Parallel Reduction [[sv/remap]].
127 As explained in the [[sv/appendix]] Reduce Mode switches off the check
128 which would normally stop looping if the result register is scalar.
129 Thus, the result scalar register, if also used as a source scalar,
130 may be used to perform sequential accumulation. This *deliberately*
131 sets up a chain
132 of Register Hazard Dependencies, whereas Parallel Reduce [[sv/remap]]
133 deliberately issues a Tree-Schedule of operations that may be parallelised.
134
135 # Fail-on-first
136
137 Data-dependent fail-on-first has two distinct variants: one for LD/ST,
138 the other for arithmetic operations (actually, CR-driven). Note in each
139 case the assumption is that vector elements are required to appear to be
140 executed in sequential Program Order. When REMAP is not active,
141 element 0 would be the first.
142
143 Data-driven (CR-driven) fail-on-first activates when Rc=1 or other
144 CR-creating operation produces a result (including cmp). Similar to
145 branch, an analysis of the CR is performed and if the test fails, the
146 vector operation terminates and discards all element operations at and
147 above the current one, and VL is truncated to either
148 the *previous* element or the current one, depending on whether
149 VLi (VL "inclusive") is set.
150
151 Thus the new VL comprises a contiguous vector of results,
152 all of which pass the testing criteria (equal to zero, less than zero etc
153 as defined by the CR-bit test).
154
155 The CR-based data-driven fail-on-first is "new" and not found in ARM
156 SVE or RVV. At the same time it is "old" because it is almost
157 identical to a generalised form of Z80's `CPIR` instruction.
158 It is extremely useful for reducing instruction count,
159 however requires speculative execution involving modifications of VL
160 to get high performance implementations. An additional mode (RC1=1)
161 effectively turns what would otherwise be an arithmetic operation
162 into a type of `cmp`. The CR is stored (and the CR.eq bit tested
163 against the `inv` field).
164 If the CR.eq bit is equal to `inv` then the Vector is truncated and
165 the loop ends.
166
167 VLi is only available as an option when `Rc=0` (or for instructions
168 which do not have Rc). When set, the current element is always
169 also included in the count (the new length that VL will be set to).
170 This may be useful in combination with "inv" to truncate the Vector
171 to *exclude* elements that fail a test, or, in the case of implementations
172 of strncpy, to include the terminating zero.
173
174 In CR-based data-driven fail-on-first there is only the option to select
175 and test one bit of each CR (just as with branch BO). For more complex
176 tests this may be insufficient. If that is the case, a vectorised crop
177 such as crand, cror or [[sv/cr_int_predication]] crweirder may be used,
178 and ffirst applied to the crop instead of to
179 the arithmetic vector. Note that crops are covered by
180 the [[sv/cr_ops]] Mode format.
181
182 Two extremely important aspects of ffirst are:
183
184 * LDST ffirst may never set VL equal to zero. This because on the first
185 element an exception must be raised "as normal".
186 * CR-based data-dependent ffirst on the other hand **can** set VL equal
187 to zero. This is the only means in the entirety of SV that VL may be set
188 to zero (with the exception of via the SV.STATE SPR). When VL is set
189 zero due to the first element failing the CR bit-test, all subsequent
190 vectorised operations are effectively `nops` which is
191 *precisely the desired and intended behaviour*.
192
193 The second crucial aspect, compared to LDST Ffirst:
194
195 * LD/ST Failfirst may (beyond the initial first element
196 conditions) truncate VL for any architecturally
197 suitable reason. Beyond the first element LD/ST Failfirst is
198 arbitrarily speculative and 100% non-deterministic.
199 * CR-based data-dependent first on the other hand MUST NOT truncate VL
200 arbitrarily to a length decided by the hardware: VL MUST only be
201 truncated based explicitly on whether a test fails.
202 This because it is a precise Deterministic test on which algorithms
203 can and will will rely.
204
205 **Floating-point Exceptions**
206
207 When Floating-point exceptions are enabled VL must be truncated at
208 the point where the Exception appears not to have occurred. If `VLi`
209 is set then VL must include the faulting element. Although very strongly
210 discouraged the Exception Mode that permits Floating Point Exception
211 notification to arrive too late to unwind is permitted
212 (under protest, due it violating
213 the otherwise 100% Deterministic nature of Data-dependent Fail-first).the
214
215 **Use of lax FP Exception Notification Mode could result in parallel
216 computations proceeding with invalid results that have to be explicitly
217 detected, whereas with the strict FP Execption Mode enabled, FFirst
218 truncates VL, allows subsequent parallel computation to avoid
219 the exceptions entirely**
220
221 ## Data-dependent fail-first on CR operations (crand etc)
222
223 Operations that actually produce or alter CR Field as a result
224 have their own SVP64 Mode, described
225 in [[sv/cr_ops]].
226
227 # pred-result mode
228
229 This mode merges common CR testing with predication, saving on instruction
230 count. Below is the pseudocode excluding predicate zeroing and elwidth
231 overrides. Note that the pseudocode for [[sv/cr_ops]] is slightly different.
232
233 for i in range(VL):
234 # predication test, skip all masked out elements.
235 if predicate_masked_out(i):
236 continue
237 result = op(iregs[RA+i], iregs[RB+i])
238 CRnew = analyse(result) # calculates eq/lt/gt
239 # Rc=1 always stores the CR
240 if Rc=1 or RC1:
241 crregs[offs+i] = CRnew
242 # now test CR, similar to branch
243 if RC1 or CRnew[BO[0:1]] != BO[2]:
244 continue # test failed: cancel store
245 # result optionally stored but CR always is
246 iregs[RT+i] = result
247
248 The reason for allowing the CR element to be stored is so that
249 post-analysis of the CR Vector may be carried out. For example:
250 Saturation may have occurred (and been prevented from updating, by the
251 test) but it is desirable to know *which* elements fail saturation.
252
253 Note that RC1 Mode basically turns all operations into `cmp`. The
254 calculation is performed but it is only the CR that is written. The
255 element result is *always* discarded, never written (just like `cmp`).
256
257 Note that predication is still respected: predicate zeroing is slightly
258 different: elements that fail the CR test *or* are masked out are zero'd.
259
260 # Pack/Unpack Mode
261
262 TODO - move to `sv.setvl` [[sv/setvl]]
263
264 Structured Pack/Unpack is similar to VSX `vpack` and `vunpack` except
265 generalised not only to a Schedule to be applied to any operation but
266 also extended to vec2/3/4.
267
268 Setting this mode changes the meaning of bits 4-5 in `RM` from being
269 `ELWIDTH` to a pair of Pack/Unpack bits. Thus it is not possible
270 to separately override source and destination elwidths at the same
271 time as use Pack/Unpack: the `SRC_ELWIDTH` bits (6-7) must be used as
272 both source and destination elwidth.
273
274 This was considered to be an acceptable
275 compromise. If separate elwidths are required and Pack/Unpack
276 needed without needing to perform a widening or narrowing register
277 move then [[sv/remap]] may be used: Matrix Mode may perform the
278 same operation (but is more costly to set up).
279 See [[sv/svp64/appendix]] for details on how Pack/Unpack
280 is implemented.