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