(no commit message)
[libreriscv.git] / openpower / sv / int_fp_mv.mdwn
1 [[!tag standards]]
2
3 # FPR-to-GPR and GPR-to-FPR
4
5 **Draft Status** under development, for submission as an RFC
6
7 Links:
8
9 * <https://bugs.libre-soc.org/show_bug.cgi?id=650>
10 * <https://bugs.libre-soc.org/show_bug.cgi?id=230#c71>
11 * <https://bugs.libre-soc.org/show_bug.cgi?id=230#c74>
12 * <https://bugs.libre-soc.org/show_bug.cgi?id=230#c76>
13
14 Introduction:
15
16 High-performance CPU/GPU software needs to often convert between integers
17 and floating-point, therefore fast conversion/data-movement instructions
18 are needed. Also given that initialisation of floats tends to take up
19 considerable space (even to just load 0.0) the inclusion of compact
20 format float immediate is up for consideration using BF16 as a base.
21
22 Libre-SOC will be compliant with the
23 **Scalar Floating-Point Subset** (SFFS) i.e. is not implementing VMX/VSX,
24 and with its focus on modern 3D GPU hybrid workloads represents an
25 important new potential use-case for OpenPOWER.
26
27 Prior to the formation of the Compliancy Levels first introduced
28 in v3.0C and v3.1
29 the progressive historic development of the Scalar parts of the Power ISA assumed
30 that VSX would always be there to complement it. However With VMX/VSX
31 **not available** in the newly-introduced SFFS Compliancy Level, the
32 existing non-VSX conversion/data-movement instructions require load/store
33 instructions (slow and expensive) to transfer data between the FPRs and
34 the GPRs. For a 3D GPU this kills any modern competitive edge.
35 Also, because SimpleV needs efficient scalar instructions in
36 order to generate efficient vector instructions, adding new instructions
37 for data-transfer/conversion between FPRs and GPRs multiplies the savings.
38
39 In addition, the vast majority of GPR <-> FPR data-transfers are as part
40 of a FP <-> Integer conversion sequence, therefore reducing the number
41 of instructions required to the minimum seems necessary.
42
43 Therefore, we are proposing adding:
44
45 * FPR load-immediate using `BF16` as the constant
46 * FPR <-> GPR data-transfer instructions that just copy bits without conversion
47 * FPR <-> GPR combined data-transfer/conversion instructions that do
48 Integer <-> FP conversions
49
50 If we're adding new Integer <-> FP conversion instructions, we may
51 as well take this opportunity to modernise the instructions and make them
52 well suited for common/important conversion sequences:
53
54 * standard Integer -> FP IEEE754 conversion (used by most languages and CPUs)
55 * standard OpenPower FP -> Integer conversion (saturation with NaN
56 converted to minimum valid integer)
57 * Rust FP -> Integer conversion (saturation with NaN converted to 0)
58 * JavaScript FP -> Integer conversion (modular with Inf/NaN converted to 0)
59
60 The assembly listings in the [[int_fp_mv/appendix]] show how costly
61 some of these language-specific conversions are: Javascript is 35
62 scalar instructions, including four branches.
63
64 ## FP -> Integer conversions
65
66 Different programming languages turn out to have completely different
67 semantics for FP to Integer conversion. This section gives an overview
68 of the different variants, listing the languages and hardware that
69 implements each variant.
70
71 ## standard Integer -> FP conversion
72
73 This conversion is outlined in the IEEE754 specification. It is used
74 by nearly all programming languages and CPUs. In the case of OpenPOWER,
75 the rounding mode is read from FPSCR
76
77 ### standard OpenPower FP -> Integer conversion
78
79 This conversion, instead of exact IEEE754 Compliance, performs
80 "saturation with NaN converted to minimum valid integer". This
81 is also exactly the same as the x86 ISA conversion senantics.
82 OpenPOWER however has instructions for both:
83
84 * rounding mode read from FPSCR
85 * rounding mode always set to truncate
86
87 ### Rust FP -> Integer conversion
88
89 For the sake of simplicity, the FP -> Integer conversion semantics generalized from those used by Rust's `as` operator will be referred to as [Rust conversion semantics](#fp-to-int-rust-conversion-semantics).
90
91 Those same semantics are used in some way by all of the following languages (not necessarily for the default conversion method):
92
93 * Rust's FP -> Integer conversion using the
94 [`as` operator](https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics)
95 * Java's
96 [FP -> Integer conversion](https://docs.oracle.com/javase/specs/jls/se16/html/jls-5.html#jls-5.1.3)
97 * LLVM's
98 [`llvm.fptosi.sat`](https://llvm.org/docs/LangRef.html#llvm-fptosi-sat-intrinsic) and
99 [`llvm.fptoui.sat`](https://llvm.org/docs/LangRef.html#llvm-fptoui-sat-intrinsic) intrinsics
100 * SPIR-V's OpenCL dialect's
101 [`OpConvertFToU`](https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#OpConvertFToU) and
102 [`OpConvertFToS`](https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#OpConvertFToS)
103 instructions when decorated with
104 [the `SaturatedConversion` decorator](https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#_a_id_decoration_a_decoration).
105
106 ### JavaScript FP -> Integer conversion
107
108 For the sake of simplicity, the FP -> Integer conversion semantics generalized from those used by JavaScripts's `ToInt32` abstract operation will be referred to as [JavaScript conversion semantics](#fp-to-int-javascript-conversion-semantics).
109
110 This instruction is present in ARM assembler as FJCVTZS
111 <https://developer.arm.com/documentation/dui0801/g/hko1477562192868>
112
113 ### Other languages
114
115 TODO: review and investigate other language semantics
116
117 # Proposed New Scalar Instructions
118
119 All of the following instructions use the standard OpenPower conversion to/from 64-bit float format when reading/writing a 32-bit float from/to a FPR. All integers however are sourced/stored in the *GPR*.
120
121 Integer operands and results being in the GPR is the key differentiator between the proposed instructions
122 (the entire rationale) compated to existing Scalar Power ISA.
123 In all existing Power ISA Scalar conversion instructions, all
124 operands are FPRs, even if the format of the source or destination
125 data is actually a scalar integer.
126
127 Note that source and destination widths can be overridden by SimpleV
128 SVP64, and that SVP64 also has Saturation Modes *in addition*
129 to those independently described here. SVP64 Overrides and Saturation
130 work on *both* Fixed *and* Floating Point operands and results.
131 The interactions with SVP64
132 are explained in the [[int_fp_mv/appendix]]
133
134 ## FPR to GPR moves
135
136 * `fmvtg RT, FRA`
137 * `fmvtg. RT, FRA`
138
139 move a 64-bit float from a FPR to a GPR, just copying bits directly.
140 As a direct bitcopy, no exceptions occur and no status flags are set.
141
142 Rc=1 tests RT and sets CR0, exactly like all other Scalar Fixed-Point
143 operations.
144
145 * `fmvtgs RT, FRA`
146 * `fmvtgs. RT, FRA`
147
148 move a 32-bit float from a FPR to a GPR, just copying bits. Converts the
149 64-bit float in `FRA` to a 32-bit float, then writes the 32-bit float to
150 `RT`. Effectively, `fmvtgs` is a macro-fusion of `frsp fmvtg`
151 and therefore has the exact same exception and flags behaviour of `frsp`
152
153 Unlike `frsp` however, with RT being a GPR, Rc=1 follows
154 standard *integer* behaviour, i.e. tests RT and sets CR0.
155
156 ## GPR to FPR moves
157
158 `fmvfg FRT, RA`
159
160 move a 64-bit float from a GPR to a FPR, just copying bits. No exceptions
161 are raised, no flags are altered of any kind.
162
163 Rc=1 tests FRT and sets CR1
164
165 `fmvfgs FRT, RA`
166
167 move a 32-bit float from a GPR to a FPR, just copying bits. Converts the
168 32-bit float in `RA` to a 64-bit float, then writes the 64-bit float to
169 `FRT`. Effectively, `fmvfgs` is a macro-fusion of `fmvfg frsp` and
170 therefore has the exact same exception and flags behaviour of `frsp`
171
172 Rc=1 tests FRT and sets CR1
173
174 TODO: clear statement on evaluation as to whether exceptions or flags raised as part of the **FP** conversion (not the int bitcopy part, the conversion part. the semantics should really be the same as frsp)
175
176 v3.0C section 4.6.7.1 states:
177
178 FPRF is set to the class and sign of the result, except for Invalid Operation Exceptions when VE=1.
179
180 Special Registers Altered:
181 FPRF FR FI
182 FX OX UX XX VXSNAN
183 CR1 (if Rc=1)
184
185 ### Float load immediate <a name="fmvis"></a>
186
187 This is like a variant of `fmvfg`
188
189 `fmvis FRT, FI`
190
191 Reinterprets `FI << 16` as a 32-bit float, which is then converted to a
192 64-bit float and written to `FRT`. This is equivalent to reinterpreting
193 `FI` as a `BF16` and converting to 64-bit float.
194
195 There is no need for an Rc=1 variant because this is an immediate loading
196 instruction. This frees up one extra bit in the X-Form format for packing
197 a full `BF16`.
198
199 Example:
200
201 ```
202 # clearing a FPR
203 fmvis f4, 0 # writes +0.0 to f4
204 # loading handy constants
205 fmvis f4, 0x8000 # writes -0.0 to f4
206 fmvis f4, 0x3F80 # writes +1.0 to f4
207 fmvis f4, 0xBF80 # writes -1.0 to f4
208 fmvis f4, 0xBFC0 # writes -1.5 to f4
209 fmvis f4, 0x7FC0 # writes +qNaN to f4
210 fmvis f4, 0x7F80 # writes +Infinity to f4
211 fmvis f4, 0xFF80 # writes -Infinity to f4
212 fmvis f4, 0x3FFF # writes +1.9921875 to f4
213
214 # clearing 128 FPRs with 2 SVP64 instructions
215 # by issuing 32 vec4 (subvector length 4) ops
216 setvli VL=MVL=32
217 sv.fmvis/vec4 f0, 0 # writes +0.0 to f0-f127
218 ```
219 Important: If the float load immediate instruction(s) are left out,
220 change all [GPR to FPR conversion instructions](#GPR-to-FPR-conversions)
221 to instead write `+0.0` if `RA` is register `0`, at least
222 allowing clearing FPRs.
223
224 | 0-5 | 6-10 | 11-25 | 26-30 | 31 |
225 |--------|------|-------|-------|-----|
226 | Major | FRT | FI | XO | FI0 |
227
228 The above fits reasonably well with Minor 19 and follows the
229 pattern shown by `addpcis`, which uses an entire column of Minor 19
230 XO. 15 bits of FI fit into bits 11 to 25,
231 the top bit FI0 (MSB0 numbered 0) makes 16.
232
233 bf16 = FI0 || FI
234 fp32 = bf16 || [0]*16
235 FRT = Single_to_Double(fp32)
236
237 Also worth noting, `fmvis` fits well with DX-Form:
238
239 | 0-5 | 6-10 | 11-15 | 16-25 | 26-30 | 31 |
240 |--------|------|-------|-------|-------|-----|
241 | Major | FRT | d0 | d1 | XO | d2 |
242
243 bf16 = d2 || d1 || d0
244 fp32 = bf16 || [0]*16
245 FRT = Single_to_Double(fp32)
246
247 ## FPR to GPR conversions
248
249 <div id="fpr-to-gpr-conversion-mode"></div>
250
251 X-Form:
252
253 | 0-5 | 6-10 | 11-15 | 16-25 | 26-30 | 31 |
254 |--------|------|--------|-------|-------|----|
255 | Major | RT | //Mode | FRA | XO | Rc |
256 | Major | FRT | //Mode | RA | XO | Rc |
257
258 Mode values:
259
260 | Mode | `rounding_mode` | Semantics |
261 |------|-----------------|----------------------------------|
262 | 000 | from `FPSCR` | [OpenPower semantics] |
263 | 001 | Truncate | [OpenPower semantics] |
264 | 010 | from `FPSCR` | [Rust semantics] |
265 | 011 | Truncate | [Rust semantics] |
266 | 100 | from `FPSCR` | [JavaScript semantics] |
267 | 101 | Truncate | [JavaScript semantics] |
268 | rest | -- | illegal instruction trap for now |
269
270 [OpenPower semantics]: #fp-to-int-openpower-conversion-semantics
271 [Rust semantics]: #fp-to-int-rust-conversion-semantics
272 [JavaScript semantics]: #fp-to-int-javascript-conversion-semantics
273
274 `fcvttgw RT, FRA, Mode`
275
276 Convert from 64-bit float to 32-bit signed integer, writing the result
277 to the GPR `RT`. Converts using [mode `Mode`]
278
279 `fcvttguw RT, FRA, Mode`
280
281 Convert from 64-bit float to 32-bit unsigned integer, writing the result
282 to the GPR `RT`. Converts using [mode `Mode`]
283
284 `fcvttgd RT, FRA, Mode`
285
286 Convert from 64-bit float to 64-bit signed integer, writing the result
287 to the GPR `RT`. Converts using [mode `Mode`]
288
289 `fcvttgud RT, FRA, Mode`
290
291 Convert from 64-bit float to 64-bit unsigned integer, writing the result
292 to the GPR `RT`. Converts using [mode `Mode`]
293
294 `fcvtstgw RT, FRA, Mode`
295
296 Convert from 32-bit float to 32-bit signed integer, writing the result
297 to the GPR `RT`. Converts using [mode `Mode`]
298
299 `fcvtstguw RT, FRA, Mode`
300
301 Convert from 32-bit float to 32-bit unsigned integer, writing the result
302 to the GPR `RT`. Converts using [mode `Mode`]
303
304 `fcvtstgd RT, FRA, Mode`
305
306 Convert from 32-bit float to 64-bit signed integer, writing the result
307 to the GPR `RT`. Converts using [mode `Mode`]
308
309 `fcvtstgud RT, FRA, Mode`
310
311 Convert from 32-bit float to 64-bit unsigned integer, writing the result
312 to the GPR `RT`. Converts using [mode `Mode`]
313
314 [mode `Mode`]: #fpr-to-gpr-conversion-mode
315
316 ## GPR to FPR conversions
317
318 All of the following GPR to FPR conversions use the rounding mode from `FPSCR`.
319
320 `fcvtfgw FRT, RA`
321
322 Convert from 32-bit signed integer in the GPR `RA` to 64-bit float in `FRT`.
323
324 `fcvtfgws FRT, RA`
325
326 Convert from 32-bit signed integer in the GPR `RA` to 32-bit float in `FRT`.
327
328 `fcvtfguw FRT, RA`
329
330 Convert from 32-bit unsigned integer in the GPR `RA` to 64-bit float in `FRT`.
331
332 `fcvtfguws FRT, RA`
333
334 Convert from 32-bit unsigned integer in the GPR `RA` to 32-bit float in `FRT`.
335
336 `fcvtfgd FRT, RA`
337
338 Convert from 64-bit signed integer in the GPR `RA` to 64-bit float in `FRT`.
339
340 `fcvtfgds FRT, RA`
341
342 Convert from 64-bit signed integer in the GPR `RA` to 32-bit float in `FRT`.
343
344 `fcvtfgud FRT, RA`
345
346 Convert from 64-bit unsigned integer in the GPR `RA` to 64-bit float in `FRT`.
347
348 `fcvtfguds FRT, RA`
349
350 Convert from 64-bit unsigned integer in the GPR `RA` to 32-bit float in `FRT`.
351
352 # FP to Integer Conversion Pseudo-code
353
354 Key for pseudo-code:
355
356 | term | result type | definition |
357 |---------------------------|-------------|----------------------------------------------------------------------------------------------------|
358 | `fp` | -- | `f32` or `f64` (or other types from SimpleV) |
359 | `int` | -- | `u32`/`u64`/`i32`/`i64` (or other types from SimpleV) |
360 | `uint` | -- | the unsigned integer of the same bit-width as `int` |
361 | `int::BITS` | `int` | the bit-width of `int` |
362 | `int::MIN_VALUE` | `int` | the minimum value `int` can store (`0` if unsigned, `-2^(int::BITS-1)` if signed) |
363 | `int::MAX_VALUE` | `int` | the maximum value `int` can store (`2^int::BITS - 1` if unsigned, `2^(int::BITS-1) - 1` if signed) |
364 | `int::VALUE_COUNT` | Integer | the number of different values `int` can store (`2^int::BITS`). too big to fit in `int`. |
365 | `rint(fp, rounding_mode)` | `fp` | rounds the floating-point value `fp` to an integer according to rounding mode `rounding_mode` |
366
367 <div id="fp-to-int-openpower-conversion-semantics"></div>
368 OpenPower conversion semantics (section A.2 page 999 (page 1023) of OpenPower ISA v3.1):
369
370 ```
371 def fp_to_int_open_power<fp, int>(v: fp) -> int:
372 if v is NaN:
373 return int::MIN_VALUE
374 if v >= int::MAX_VALUE:
375 return int::MAX_VALUE
376 if v <= int::MIN_VALUE:
377 return int::MIN_VALUE
378 return (int)rint(v, rounding_mode)
379 ```
380
381 <div id="fp-to-int-rust-conversion-semantics"></div>
382 Rust [conversion semantics](https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics) (with adjustment to add non-truncate rounding modes):
383
384 ```
385 def fp_to_int_rust<fp, int>(v: fp) -> int:
386 if v is NaN:
387 return 0
388 if v >= int::MAX_VALUE:
389 return int::MAX_VALUE
390 if v <= int::MIN_VALUE:
391 return int::MIN_VALUE
392 return (int)rint(v, rounding_mode)
393 ```
394
395 <div id="fp-to-int-javascript-conversion-semantics"></div>
396 Section 7.1 of the ECMAScript / JavaScript
397 [conversion semantics](https://262.ecma-international.org/11.0/#sec-toint32) (with adjustment to add non-truncate rounding modes):
398
399 ```
400 def fp_to_int_java_script<fp, int>(v: fp) -> int:
401 if v is NaN or infinite:
402 return 0
403 v = rint(v, rounding_mode)
404 v = v mod int::VALUE_COUNT # 2^32 for i32, 2^64 for i64, result is non-negative
405 bits = (uint)v
406 return (int)bits
407 ```
408
409 # Equivalent OpenPower ISA v3.0 Assembly Language for FP -> Integer Conversion Modes
410
411 Moved to [[int_fp_mv/appendix]]