fix links to ls006.fpintmv.mdwn
[libreriscv.git] / openpower / sv / int_fp_mv.mdwn
1 [[!tag standards]]
2
3 Note on considered alternative naming schemes: we decided to switch to using the reduced mnemonic naming scheme (over some people's objections) since it would be 5 instructions instead of dozens, though we did consider trying to match PowerISA's existing naming scheme for the instructions rather than only for the instruction aliases. <https://bugs.libre-soc.org/show_bug.cgi?id=1015#c7>
4
5 # FPR-to-GPR and GPR-to-FPR
6
7 TODO special constants instruction (e, tau/N, ln 2, sqrt 2, etc.) -- exclude any constants available through fmvis
8
9 **Draft Status** under development, for submission as an RFC
10
11 Links:
12
13 * <https://bugs.libre-soc.org/show_bug.cgi?id=650>
14 * <https://bugs.libre-soc.org/show_bug.cgi?id=230#c71>
15 * <https://bugs.libre-soc.org/show_bug.cgi?id=230#c74>
16 * <https://bugs.libre-soc.org/show_bug.cgi?id=230#c76>
17 * <https://bugs.libre-soc.org/show_bug.cgi?id=887> fmvis
18 * <https://bugs.libre-soc.org/show_bug.cgi?id=1015> int-fp RFC
19 * [[int_fp_mv/appendix]]
20 * [[sv/rfc/ls002.fmi]] - `fmvis` and `fishmv` External RFC Formal Submission
21 * [[sv/rfc/ls006.fpintmv]] - int-fp-mv External RFC Formal Submission
22
23 Trademarks:
24
25 * Rust is a Trademark of the Rust Foundation
26 * Java and JavaScript are Trademarks of Oracle
27 * LLVM is a Trademark of the LLVM Foundation
28 * SPIR-V is a Trademark of the Khronos Group
29 * OpenCL is a Trademark of Apple, Inc.
30
31 Referring to these Trademarks within this document
32 is by necessity, in order to put the semantics of each language
33 into context, and is considered "fair use" under Trademark
34 Law.
35
36 Introduction:
37
38 High-performance CPU/GPU software needs to often convert between integers
39 and floating-point, therefore fast conversion/data-movement instructions
40 are needed. Also given that initialisation of floats tends to take up
41 considerable space (even to just load 0.0) the inclusion of two compact
42 format float immediate instructions is up for consideration using 16-bit
43 immediates. BF16 is one of the formats: a second instruction allows a full
44 accuracy FP32 to be constructed.
45
46 Libre-SOC will be compliant with the
47 **Scalar Floating-Point Subset** (SFFS) i.e. is not implementing VMX/VSX,
48 and with its focus on modern 3D GPU hybrid workloads represents an
49 important new potential use-case for OpenPOWER.
50
51 Prior to the formation of the Compliancy Levels first introduced
52 in v3.0C and v3.1
53 the progressive historic development of the Scalar parts of the Power ISA assumed
54 that VSX would always be there to complement it. However With VMX/VSX
55 **not available** in the newly-introduced SFFS Compliancy Level, the
56 existing non-VSX conversion/data-movement instructions require
57 a Vector of load/store
58 instructions (slow and expensive) to transfer data between the FPRs and
59 the GPRs. For a modern 3D GPU this kills any possibility of a
60 competitive edge.
61 Also, because SimpleV needs efficient scalar instructions in
62 order to generate efficient vector instructions, adding new instructions
63 for data-transfer/conversion between FPRs and GPRs multiplies the savings.
64
65 In addition, the vast majority of GPR <-> FPR data-transfers are as part
66 of a FP <-> Integer conversion sequence, therefore reducing the number
67 of instructions required is a priority.
68
69 Therefore, we are proposing adding:
70
71 * FPR load-immediate instructions, one equivalent to `BF16`, the
72 other increasing accuracy to `FP32`
73 * FPR <-> GPR data-transfer instructions that just copy bits without conversion
74 * FPR <-> GPR combined data-transfer/conversion instructions that do
75 Integer <-> FP conversions
76
77 If adding new Integer <-> FP conversion instructions,
78 the opportunity may be taken to modernise the instructions and make them
79 well-suited for common/important conversion sequences:
80
81 * Int -> Float
82 * **standard IEEE754** - used by most languages and CPUs
83 * Float -> Int
84 * **standard OpenPOWER** - saturation with NaN
85 converted to minimum valid integer
86 * **Java/Saturating** - saturation with NaN converted to 0
87 * **JavaScript** - modulo wrapping with Inf/NaN converted to 0
88
89 The assembly listings in the [[int_fp_mv/appendix]] show how costly
90 some of these language-specific conversions are: JavaScript, the
91 worst case, is 32 scalar instructions including seven branch instructions.
92
93 # Proposed New Scalar Instructions
94
95 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*.
96
97 Integer operands and results being in the GPR is the key differentiator between the proposed instructions
98 (the entire rationale) compared to existing Scalar Power ISA.
99 In all existing Power ISA Scalar conversion instructions, all
100 operands are FPRs, even if the format of the source or destination
101 data is actually a scalar integer.
102
103 *(The existing Scalar instructions being FP-FP only is based on an assumption
104 that VSX will be implemented, and VSX is not part of the SFFS Compliancy
105 Level. An earlier version of the Power ISA used to have similar
106 FPR<->GPR instructions to these:
107 they were deprecated due to this incorrect assumption that VSX would
108 always be present).*
109
110 Note that source and destination widths can be overridden by SimpleV
111 SVP64, and that SVP64 also has Saturation Modes *in addition*
112 to those independently described here. SVP64 Overrides and Saturation
113 work on *both* Fixed *and* Floating Point operands and results.
114 The interactions with SVP64
115 are explained in the [[int_fp_mv/appendix]]
116
117 # Float load immediate <a name="fmvis"></a>
118
119 These are like a variant of `mtfpr` and `oris`, combined.
120 Power ISA currently requires a large
121 number of instructions to get Floating Point constants into registers.
122 `fmvis` on its own is equivalent to BF16 to FP32/64 conversion,
123 but if followed up by `fishmv` an additional 16 bits of accuracy in the
124 mantissa may be achieved.
125
126 These instructions **always** save
127 resources compared to FP-load for exactly the same reason
128 that `li` saves resources: an L1-Data-Cache and memory read
129 is avoided.
130
131 *IBM may consider it worthwhile to extend these two instructions to
132 v3.1 Prefixed (`pfmvis` and `pfishmv`: 8RR, imm0 extended).
133 If so it is recommended that
134 `pfmvis` load a full FP32 immediate and `pfishmv` supplies the three high
135 missing exponent bits (numbered 8 to 10) and the lower additional
136 29 mantissa bits (23 to 51) needed to construct a full FP64 immediate.
137 Strictly speaking the sequence `fmvis fishmv pfishmv` achieves the
138 same effect in the same number of bytes as `pfmvis pfishmv`,
139 making `pfmvis` redundant.*
140
141 Just as Floating-point Load does not set FP Flags neither does fmvis or fishmv.
142 As fishmv is specifically intended to work in conjunction with fmvis
143 to provide additional accuracy, all bits other than those which
144 would have been set by a prior fmvis instruction are deliberately ignored.
145 (If these instructions involved reading from registers rather than immediates
146 it would be a different story).
147
148 ## Load BF16 Immediate
149
150 `fmvis FRS, D`
151
152 Reinterprets `D << 16` as a 32-bit float, which is then converted to a
153 64-bit float and written to `FRS`. This is equivalent to reinterpreting
154 `D` as a `BF16` and converting to 64-bit float.
155 There is no need for an Rc=1 variant because this is an immediate loading
156 instruction.
157
158 Example:
159
160 ```
161 # clearing a FPR
162 fmvis f4, 0 # writes +0.0 to f4
163 # loading handy constants
164 fmvis f4, 0x8000 # writes -0.0 to f4
165 fmvis f4, 0x3F80 # writes +1.0 to f4
166 fmvis f4, 0xBF80 # writes -1.0 to f4
167 fmvis f4, 0xBFC0 # writes -1.5 to f4
168 fmvis f4, 0x7FC0 # writes +qNaN to f4
169 fmvis f4, 0x7F80 # writes +Infinity to f4
170 fmvis f4, 0xFF80 # writes -Infinity to f4
171 fmvis f4, 0x3FFF # writes +1.9921875 to f4
172
173 # clearing 128 FPRs with 2 SVP64 instructions
174 # by issuing 32 vec4 (subvector length 4) ops
175 setvli VL=MVL=32
176 sv.fmvis/vec4 f0, 0 # writes +0.0 to f0-f127
177 ```
178 Important: If the float load immediate instruction(s) are left out,
179 change all [GPR to FPR conversion instructions](#GPR-to-FPR-conversions)
180 to instead write `+0.0` if `RA` is register `0`, at least
181 allowing clearing FPRs.
182
183 `fmvis` fits with DX-Form:
184
185 | 0-5 | 6-10 | 11-15 | 16-25 | 26-30 | 31 | Form |
186 |--------|------|-------|-------|-------|-----|---------|
187 | Major | FRS | d1 | d0 | XO | d2 | DX-Form |
188
189 Pseudocode:
190
191 bf16 = d0 || d1 || d2 # create BF16 immediate
192 fp32 = bf16 || [0]*16 # convert BF16 to FP32
193 FRS = DOUBLE(fp32) # convert FP32 to FP64
194
195 Special registers altered:
196
197 None
198
199 ## Float Immediate Second-Half MV <a name="fishmv"></a>
200
201 `fishmv FRS, D`
202
203 DX-Form:
204
205 | 0-5 | 6-10 | 11-15 | 16-25 | 26-30 | 31 | Form |
206 |--------|------|-------|-------|-------|-----|---------|
207 | Major | FRS | d1 | d0 | XO | d2 | DX-Form |
208
209 Strategically similar to how `oris` is used to construct
210 32-bit Integers, an additional 16-bits of immediate is
211 inserted into `FRS` to extend its accuracy to
212 a full FP32 (stored as usual in FP64 Format within the FPR).
213 If a prior `fmvis` instruction had been used to
214 set the upper 16-bits of an FP32 value, `fishmv` contains the
215 lower 16-bits.
216
217 The key difference between using `li` and `oris` to construct 32-bit
218 GPR Immediates and `fishmv` is that the `fmvis` will have converted
219 the `BF16` immediate to FP64 (Double) format.
220 This is taken into consideration
221 as can be seen in the pseudocode below.
222
223 Pseudocode:
224
225 fp32 <- SINGLE((FRS)) # convert to FP32
226 fp32[16:31] <- d0 || d1 || d2 # replace LSB half
227 FRS <- DOUBLE(fp32) # convert back to FP64
228
229 Special registers altered:
230
231 None
232
233 **This instruction performs a Read-Modify-Write.** *FRS is read, the additional
234 16 bit immediate inserted, and the result also written to FRS*
235
236 Example:
237
238 ```
239 # these two combined instructions write 0x3f808000
240 # into f4 as an FP32 to be converted to an FP64.
241 # actual contents in f4 after conversion: 0x3ff0_1000_0000_0000
242 # first the upper bits, happens to be +1.0
243 fmvis f4, 0x3F80 # writes +1.0 to f4
244 # now write the lower 16 bits of an FP32
245 fishmv f4, 0x8000 # writes +1.00390625 to f4
246 ```
247
248 [[!inline pages="openpower/sv/int_fp_mv/moves_and_conversions" raw=yes ]]