edit notes
[libreriscv.git] / openpower / sv / int_fp_mv_reduced_insn_count.mdwn
1 [[!tag standards]]
2
3 # Reduced Instruction List Attempt
4
5 Attempt at [reducing the number of different instructions](#reduced-instruction-list) used for int/fp moves.
6
7 **IMPORTANT**: don't merge until everyone's had a chance to review it.
8
9 ## tables for reduced instruction list
10
11 | S_AND_RC | PowerISA naming scheme |
12 |----------|------------------------|
13 | 0 | `fmvtg` |
14 | 1 | `fmvtg.` |
15 | 2 | `fmvtgs` |
16 | 3 | `fmvtgs.` |
17
18 | INT_MODE | PowerISA naming scheme | Integer Type |
19 |----------|------------------------|-----------------|
20 | 0 | `fcvtfgw[s][.]` | signed 32-bit |
21 | 1 | `fcvtfguw[s][.]` | unsigned 32-bit |
22 | 2 | `fcvtfgd[s][.]` | signed 64-bit |
23 | 3 | `fcvtfgud[s][.]` | unsigned 64-bit |
24
25 ## reduced instruction list <a name="reduced-instruction-list"></a>
26
27 | old | new |
28 |--------------------------------------|--------------------------------------------|
29 | `fmvis FRS, D` | `fmvis FRS, D` |
30 | `fishmv FRS, D` | `fishmv FRS, D` |
31 | `fmvtg[s][.] RT, FRA` | `fmvtg RT, FRA, S_AND_RC` |
32 | `fmvfg[s][.] FRT, RA` | `fmvfg FRT, RA, S_AND_RC` |
33 | `fcvtfg[u][w/d][s][.] FRT, RA` | `fcvtfg FRT, RA, S_AND_RC, INT_MODE` |
34 | `fcvt[s]tg[u][w/d][.] RT, FRA, Mode` | `fcvttg RT, FRA, Mode, S_AND_RC, INT_MODE` |
35
36 Notes:
37
38 * `fmvis` and `fishmv` have already been submitted to the ISA WG, so don't modify them!
39 * PowerISA uses `s` and `.` suffixes instead of an immediate for Single and Rc=1 modes respectively.
40 * PowerISA uses `w`, `uw`, `d`, and `ud` suffixes instead of an immediate for selecting between unsigned/signed 32-bit/64-bit.
41 * Jacob: About the only operations I can see that we can realistically remove are Rc=1 versions, however imho that isn't necessary.
42 * Realistically we can't remove any of the Rc=0 instructions because it would make the instruction set non-orthogonal and it would penalize the code using those operations, almost all of which are quite common.
43 * Attempting to condense them into 6 instructions by undoing the PowerISA naming scheme works, but all that changed is the assembler mnemonics (in a bad way by being inconsistent with how PowerISA names things), the instruction encodings don't change at all, unless we want to use an expanded opcode.
44 * Jacob: I think we will want to keep the Single versions of moves/conversions from FPRs to GPRs, since it seems likely to run faster than the Double versions on some CPUs.
45
46 # Rest of document not yet modified:
47
48 # FPR-to-GPR and GPR-to-FPR
49
50 TODO special constants instruction (e, tau/N, ln 2, sqrt 2, etc.) -- exclude any constants available through fmvis
51
52 **Draft Status** under development, for submission as an RFC
53
54 Links:
55
56 * <https://bugs.libre-soc.org/show_bug.cgi?id=650>
57 * <https://bugs.libre-soc.org/show_bug.cgi?id=230#c71>
58 * <https://bugs.libre-soc.org/show_bug.cgi?id=230#c74>
59 * <https://bugs.libre-soc.org/show_bug.cgi?id=230#c76>
60 * <https://bugs.libre-soc.org/show_bug.cgi?id=887> fmvis
61 * [[int_fp_mv/appendix]]
62 * [[sv/rfc/ls002]] - `fmvis` and `fishmv` External RFC Formal Submission
63
64 Trademarks:
65
66 * Rust is a Trademark of the Rust Foundation
67 * Java and Javascript are Trademarks of Oracle
68 * LLVM is a Trademark of the LLVM Foundation
69 * SPIR-V is a Trademark of the Khronos Group
70 * OpenCL is a Trademark of Apple, Inc.
71
72 Referring to these Trademarks within this document
73 is by necessity, in order to put the semantics of each language
74 into context, and is considered "fair use" under Trademark
75 Law.
76
77 Introduction:
78
79 High-performance CPU/GPU software needs to often convert between integers
80 and floating-point, therefore fast conversion/data-movement instructions
81 are needed. Also given that initialisation of floats tends to take up
82 considerable space (even to just load 0.0) the inclusion of two compact
83 format float immediate instructions is up for consideration using 16-bit
84 immediates. BF16 is one of the formats: a second instruction allows a full
85 accuracy FP32 to be constructed.
86
87 Libre-SOC will be compliant with the
88 **Scalar Floating-Point Subset** (SFFS) i.e. is not implementing VMX/VSX,
89 and with its focus on modern 3D GPU hybrid workloads represents an
90 important new potential use-case for OpenPOWER.
91
92 Prior to the formation of the Compliancy Levels first introduced
93 in v3.0C and v3.1
94 the progressive historic development of the Scalar parts of the Power ISA assumed
95 that VSX would always be there to complement it. However With VMX/VSX
96 **not available** in the newly-introduced SFFS Compliancy Level, the
97 existing non-VSX conversion/data-movement instructions require
98 a Vector of load/store
99 instructions (slow and expensive) to transfer data between the FPRs and
100 the GPRs. For a modern 3D GPU this kills any possibility of a
101 competitive edge.
102 Also, because SimpleV needs efficient scalar instructions in
103 order to generate efficient vector instructions, adding new instructions
104 for data-transfer/conversion between FPRs and GPRs multiplies the savings.
105
106 In addition, the vast majority of GPR <-> FPR data-transfers are as part
107 of a FP <-> Integer conversion sequence, therefore reducing the number
108 of instructions required is a priority.
109
110 Therefore, we are proposing adding:
111
112 * FPR load-immediate instructions, one equivalent to `BF16`, the
113 other increasing accuracy to `FP32`
114 * FPR <-> GPR data-transfer instructions that just copy bits without conversion
115 * FPR <-> GPR combined data-transfer/conversion instructions that do
116 Integer <-> FP conversions
117
118 If adding new Integer <-> FP conversion instructions,
119 the opportunity may be taken to modernise the instructions and make them
120 well-suited for common/important conversion sequences:
121
122 * **standard IEEE754** - used by most languages and CPUs
123 * **standard OpenPOWER** - saturation with NaN
124 converted to minimum valid integer
125 * **Java** - saturation with NaN converted to 0
126 * **JavaScript** - modulo wrapping with Inf/NaN converted to 0
127
128 The assembly listings in the [[int_fp_mv/appendix]] show how costly
129 some of these language-specific conversions are: Javascript, the
130 worst case, is 32 scalar instructions including seven branch instructions.
131
132 # Proposed New Scalar Instructions
133
134 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*.
135
136 Integer operands and results being in the GPR is the key differentiator between the proposed instructions
137 (the entire rationale) compared to existing Scalar Power ISA.
138 In all existing Power ISA Scalar conversion instructions, all
139 operands are FPRs, even if the format of the source or destination
140 data is actually a scalar integer.
141
142 *(The existing Scalar instructions being FP-FP only is based on an assumption
143 that VSX will be implemented, and VSX is not part of the SFFS Compliancy
144 Level. An earlier version of the Power ISA used to have similar
145 FPR<->GPR instructions to these:
146 they were deprecated due to this incorrect assumption that VSX would
147 always be present).*
148
149 Note that source and destination widths can be overridden by SimpleV
150 SVP64, and that SVP64 also has Saturation Modes *in addition*
151 to those independently described here. SVP64 Overrides and Saturation
152 work on *both* Fixed *and* Floating Point operands and results.
153 The interactions with SVP64
154 are explained in the [[int_fp_mv/appendix]]
155
156 # Float load immediate <a name="fmvis"></a>
157
158 These are like a variant of `fmvfg` and `oris`, combined.
159 Power ISA currently requires a large
160 number of instructions to get Floating Point constants into registers.
161 `fmvis` on its own is equivalent to BF16 to FP32/64 conversion,
162 but if followed up by `fishmv` an additional 16 bits of accuracy in the
163 mantissa may be achieved.
164
165 These instructions **always** save
166 resources compared to FP-load for exactly the same reason
167 that `li` saves resources: an L1-Data-Cache and memory read
168 is avoided.
169
170 *IBM may consider it worthwhile to extend these two instructions to
171 v3.1 Prefixed (`pfmvis` and `pfishmv`: 8RR, imm0 extended).
172 If so it is recommended that
173 `pfmvis` load a full FP32 immediate and `pfishmv` supplies the three high
174 missing exponent bits (numbered 8 to 10) and the lower additional
175 29 mantissa bits (23 to 51) needed to construct a full FP64 immediate.
176 Strictly speaking the sequence `fmvis fishmv pfishmv` achieves the
177 same effect in the same number of bytes as `pfmvis pfishmv`,
178 making `pfmvis` redundant.*
179
180 Just as Floating-point Load does not set FP Flags neither does fmvis or fishmv.
181 As fishmv is specifically intended to work in conjunction with fmvis
182 to provide additional accuracy, all bits other than those which
183 would have been set by a prior fmvis instruction are deliberately ignored.
184 (If these instructions involved reading from registers rather than immediates
185 it would be a different story).
186
187 ## Load BF16 Immediate
188
189 `fmvis FRS, D`
190
191 Reinterprets `D << 16` as a 32-bit float, which is then converted to a
192 64-bit float and written to `FRS`. This is equivalent to reinterpreting
193 `D` as a `BF16` and converting to 64-bit float.
194 There is no need for an Rc=1 variant because this is an immediate loading
195 instruction.
196
197 Example:
198
199 ```
200 # clearing a FPR
201 fmvis f4, 0 # writes +0.0 to f4
202 # loading handy constants
203 fmvis f4, 0x8000 # writes -0.0 to f4
204 fmvis f4, 0x3F80 # writes +1.0 to f4
205 fmvis f4, 0xBF80 # writes -1.0 to f4
206 fmvis f4, 0xBFC0 # writes -1.5 to f4
207 fmvis f4, 0x7FC0 # writes +qNaN to f4
208 fmvis f4, 0x7F80 # writes +Infinity to f4
209 fmvis f4, 0xFF80 # writes -Infinity to f4
210 fmvis f4, 0x3FFF # writes +1.9921875 to f4
211
212 # clearing 128 FPRs with 2 SVP64 instructions
213 # by issuing 32 vec4 (subvector length 4) ops
214 setvli VL=MVL=32
215 sv.fmvis/vec4 f0, 0 # writes +0.0 to f0-f127
216 ```
217 Important: If the float load immediate instruction(s) are left out,
218 change all [GPR to FPR conversion instructions](#GPR-to-FPR-conversions)
219 to instead write `+0.0` if `RA` is register `0`, at least
220 allowing clearing FPRs.
221
222 `fmvis` fits with DX-Form:
223
224 | 0-5 | 6-10 | 11-15 | 16-25 | 26-30 | 31 | Form |
225 |--------|------|-------|-------|-------|-----|---------|
226 | Major | FRS | d1 | d0 | XO | d2 | DX-Form |
227
228 Pseudocode:
229
230 bf16 = d0 || d1 || d2 # create BF16 immediate
231 fp32 = bf16 || [0]*16 # convert BF16 to FP32
232 FRS = DOUBLE(fp32) # convert FP32 to FP64
233
234 Special registers altered:
235
236 None
237
238 ## Float Immediate Second-Half MV <a name="fishmv"></a>
239
240 `fishmv FRS, D`
241
242 DX-Form:
243
244 | 0-5 | 6-10 | 11-15 | 16-25 | 26-30 | 31 | Form |
245 |--------|------|-------|-------|-------|-----|---------|
246 | Major | FRS | d1 | d0 | XO | d2 | DX-Form |
247
248 Strategically similar to how `oris` is used to construct
249 32-bit Integers, an additional 16-bits of immediate is
250 inserted into `FRS` to extend its accuracy to
251 a full FP32 (stored as usual in FP64 Format within the FPR).
252 If a prior `fmvis` instruction had been used to
253 set the upper 16-bits of an FP32 value, `fishmv` contains the
254 lower 16-bits.
255
256 The key difference between using `li` and `oris` to construct 32-bit
257 GPR Immediates and `fishmv` is that the `fmvis` will have converted
258 the `BF16` immediate to FP64 (Double) format.
259 This is taken into consideration
260 as can be seen in the pseudocode below.
261
262 Pseudocode:
263
264 fp32 <- SINGLE((FRS)) # convert to FP32
265 fp32[16:31] <- d0 || d1 || d2 # replace LSB half
266 FRS <- DOUBLE(fp32) # convert back to FP64
267
268 Special registers altered:
269
270 None
271
272 **This instruction performs a Read-Modify-Write.** *FRS is read, the additional
273 16 bit immediate inserted, and the result also written to FRS*
274
275 Example:
276
277 ```
278 # these two combined instructions write 0x3f808000
279 # into f4 as an FP32 to be converted to an FP64.
280 # actual contents in f4 after conversion: 0x3ff0_1000_0000_0000
281 # first the upper bits, happens to be +1.0
282 fmvis f4, 0x3F80 # writes +1.0 to f4
283 # now write the lower 16 bits of an FP32
284 fishmv f4, 0x8000 # writes +1.00390625 to f4
285 ```
286
287 # Moves
288
289 These instructions perform a straight unaltered bit-level copy from one Register
290 File to another.
291
292 # FPR to GPR moves
293
294 * `fmvtg RT, FRA`
295 * `fmvtg. RT, FRA`
296
297 move a 64-bit float from a FPR to a GPR, just copying bits directly.
298 As a direct bitcopy, no exceptions occur and no status flags are set.
299
300 Rc=1 tests RT and sets CR0, exactly like all other Scalar Fixed-Point
301 operations.
302
303 * `fmvtgs RT, FRA`
304 * `fmvtgs. RT, FRA`
305
306 move a 32-bit float from a FPR to a GPR, just copying bits. Converts the
307 64-bit float in `FRA` to a 32-bit float, using the same method as `stfs`,
308 then writes the 32-bit float to `RT`, setting the high 32-bits to zeros.
309 Effectively, `fmvtgs` is a macro-fusion of `stfs` and `lwz` and therefore
310 does not behave like `frsp` and does not set any fp exception flags.
311
312 Since RT is a GPR, Rc=1 follows standard *integer* behaviour, i.e.
313 tests RT and sets CR0.
314
315 # GPR to FPR moves
316
317 `fmvfg FRT, RA`
318
319 move a 64-bit float from a GPR to a FPR, just copying bits. No exceptions
320 are raised, no flags are altered of any kind.
321
322 Rc=1 tests FRT and sets CR1
323
324 `fmvfgs FRT, RA`
325
326 move a 32-bit float from a GPR to a FPR, just copying bits. Converts the
327 32-bit float in `RA` to a 64-bit float, using the same method as `lfs`,
328 then writes the 64-bit float to `FRT`. Effectively, `fmvfgs` is a
329 macro-fusion of `stw` and `lfs` and therefore no fp exception flags are set.
330
331 Rc=1 tests FRT and sets CR1, following usual fp Rc=1 semantics.
332
333 # Conversions
334
335 Unlike the move instructions
336 these instructions perform conversions between Integer and
337 Floating Point. Truncation can therefore occur, as well
338 as exceptions.
339
340 Mode values:
341
342 | Mode | `rounding_mode` | Semantics |
343 |------|-----------------|----------------------------------|
344 | 000 | from `FPSCR` | [OpenPower semantics] |
345 | 001 | Truncate | [OpenPower semantics] |
346 | 010 | from `FPSCR` | [Java semantics] |
347 | 011 | Truncate | [Java semantics] |
348 | 100 | from `FPSCR` | [JavaScript semantics] |
349 | 101 | Truncate | [JavaScript semantics] |
350 | rest | -- | illegal instruction trap for now |
351
352 [OpenPower semantics]: #fp-to-int-openpower-conversion-semantics
353 [Java semantics]: #fp-to-int-java-conversion-semantics
354 [JavaScript semantics]: #fp-to-int-javascript-conversion-semantics
355
356 ## GPR to FPR conversions
357
358 **Format**
359
360 | 0-5 | 6-10 | 11-15 | 16-25 | 26-30 | 31 | Form |
361 |--------|------|--------|-------|-------|----|------|
362 | Major | FRT | //Mode | RA | XO | Rc |X-Form|
363
364 All of the following GPR to FPR conversions use the rounding mode from `FPSCR`.
365
366 * `fcvtfgw FRT, RA`
367 Convert from 32-bit signed integer in the GPR `RA` to 64-bit float in
368 `FRT`.
369 * `fcvtfgws FRT, RA`
370 Convert from 32-bit signed integer in the GPR `RA` to 32-bit float in
371 `FRT`.
372 * `fcvtfguw FRT, RA`
373 Convert from 32-bit unsigned integer in the GPR `RA` to 64-bit float in
374 `FRT`.
375 * `fcvtfguws FRT, RA`
376 Convert from 32-bit unsigned integer in the GPR `RA` to 32-bit float in
377 `FRT`.
378 * `fcvtfgd FRT, RA`
379 Convert from 64-bit signed integer in the GPR `RA` to 64-bit float in
380 `FRT`.
381 * `fcvtfgds FRT, RA`
382 Convert from 64-bit signed integer in the GPR `RA` to 32-bit float in
383 `FRT`.
384 * `fcvtfgud FRT, RA`
385 Convert from 64-bit unsigned integer in the GPR `RA` to 64-bit float in
386 `FRT`.
387 * `fcvtfguds FRT, RA`
388 Convert from 64-bit unsigned integer in the GPR `RA` to 32-bit float in
389 `FRT`.
390
391 ## FPR to GPR (Integer) conversions
392
393 <div id="fpr-to-gpr-conversion-mode"></div>
394
395 Different programming languages turn out to have completely different
396 semantics for FP to Integer conversion. Below is an overview
397 of the different variants, listing the languages and hardware that
398 implements each variant.
399
400 **Standard IEEE754 conversion**
401
402 This conversion is outlined in the IEEE754 specification. It is used
403 by nearly all programming languages and CPUs. In the case of OpenPOWER,
404 the rounding mode is read from FPSCR
405
406 **Standard OpenPower conversion**
407
408 This conversion, instead of exact IEEE754 Compliance, performs
409 "saturation with NaN converted to minimum valid integer". This
410 is also exactly the same as the x86 ISA conversion semantics.
411 OpenPOWER however has instructions for both:
412
413 * rounding mode read from FPSCR
414 * rounding mode always set to truncate
415
416 **Java conversion**
417
418 For the sake of simplicity, the FP -> Integer conversion semantics generalized from those used by Java's semantics (and Rust's `as` operator) will be referred to as
419 [Java conversion semantics](#fp-to-int-java-conversion-semantics).
420
421 Those same semantics are used in some way by all of the following languages (not necessarily for the default conversion method):
422
423 * Java's
424 [FP -> Integer conversion](https://docs.oracle.com/javase/specs/jls/se16/html/jls-5.html#jls-5.1.3)
425 * Rust's FP -> Integer conversion using the
426 [`as` operator](https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics)
427 * LLVM's
428 [`llvm.fptosi.sat`](https://llvm.org/docs/LangRef.html#llvm-fptosi-sat-intrinsic) and
429 [`llvm.fptoui.sat`](https://llvm.org/docs/LangRef.html#llvm-fptoui-sat-intrinsic) intrinsics
430 * SPIR-V's OpenCL dialect's
431 [`OpConvertFToU`](https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#OpConvertFToU) and
432 [`OpConvertFToS`](https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#OpConvertFToS)
433 instructions when decorated with
434 [the `SaturatedConversion` decorator](https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#_a_id_decoration_a_decoration).
435 * WebAssembly has also introduced
436 [trunc_sat_u](ttps://webassembly.github.io/spec/core/exec/numerics.html#op-trunc-sat-u) and
437 [trunc_sat_s](https://webassembly.github.io/spec/core/exec/numerics.html#op-trunc-sat-s)
438
439 **JavaScript conversion**
440
441 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).
442
443 This instruction is present in ARM assembler as FJCVTZS
444 <https://developer.arm.com/documentation/dui0801/g/hko1477562192868>
445
446 **Format**
447
448 | 0-5 | 6-10 | 11-15 | 16-25 | 26-30 | 31 | Form |
449 |--------|------|--------|-------|-------|----|------|
450 | Major | RT | //Mode | FRA | XO | Rc |X-Form|
451
452 **Rc=1 and OE=1**
453
454 All of these insructions have an Rc=1 mode which sets CR0
455 in the normal way for any instructions producing a GPR result.
456 Additionally, when OE=1, if the numerical value of the FP number
457 is not 100% accurately preserved (due to truncation or saturation
458 and including when the FP number was NaN) then this is considered
459 to be an integer Overflow condition, and CR0.SO, XER.SO and XER.OV
460 are all set as normal for any GPR instructions that overflow.
461
462 **Instructions**
463
464 * `fcvttgw RT, FRA, Mode`
465 Convert from 64-bit float to 32-bit signed integer, writing the result
466 to the GPR `RT`. Converts using [mode `Mode`]. Similar to `fctiw` or `fctiwz`
467 * `fcvttguw RT, FRA, Mode`
468 Convert from 64-bit float to 32-bit unsigned integer, writing the result
469 to the GPR `RT`. Converts using [mode `Mode`]. Similar to `fctiwu` or `fctiwuz`
470 * `fcvttgd RT, FRA, Mode`
471 Convert from 64-bit float to 64-bit signed integer, writing the result
472 to the GPR `RT`. Converts using [mode `Mode`]. Similar to `fctid` or `fctidz`
473 * `fcvttgud RT, FRA, Mode`
474 Convert from 64-bit float to 64-bit unsigned integer, writing the result
475 to the GPR `RT`. Converts using [mode `Mode`]. Similar to `fctidu` or `fctiduz`
476 * `fcvtstgw RT, FRA, Mode`
477 Convert from 32-bit float to 32-bit signed integer, writing the result
478 to the GPR `RT`. Converts using [mode `Mode`]
479 * `fcvtstguw RT, FRA, Mode`
480 Convert from 32-bit float to 32-bit unsigned integer, writing the result
481 to the GPR `RT`. Converts using [mode `Mode`]
482 * `fcvtstgd RT, FRA, Mode`
483 Convert from 32-bit float to 64-bit signed integer, writing the result
484 to the GPR `RT`. Converts using [mode `Mode`]
485 * `fcvtstgud RT, FRA, Mode`
486 Convert from 32-bit float to 64-bit unsigned integer, writing the result
487 to the GPR `RT`. Converts using [mode `Mode`]
488
489 [mode `Mode`]: #fpr-to-gpr-conversion-mode
490
491 ## FP to Integer Conversion Pseudo-code
492
493 Key for pseudo-code:
494
495 | term | result type | definition |
496 |---------------------------|-------------|----------------------------------------------------------------------------------------------------|
497 | `fp` | -- | `f32` or `f64` (or other types from SimpleV) |
498 | `int` | -- | `u32`/`u64`/`i32`/`i64` (or other types from SimpleV) |
499 | `uint` | -- | the unsigned integer of the same bit-width as `int` |
500 | `int::BITS` | `int` | the bit-width of `int` |
501 | `uint::MIN_VALUE` | `uint` | the minimum value `uint` can store: `0` |
502 | `uint::MAX_VALUE` | `uint` | the maximum value `uint` can store: `2^int::BITS - 1` |
503 | `int::MIN_VALUE` | `int` | the minimum value `int` can store : `-2^(int::BITS-1)` |
504 | `int::MAX_VALUE` | `int` | the maximum value `int` can store : `2^(int::BITS-1) - 1` |
505 | `int::VALUE_COUNT` | Integer | the number of different values `int` can store (`2^int::BITS`). too big to fit in `int`. |
506 | `rint(fp, rounding_mode)` | `fp` | rounds the floating-point value `fp` to an integer according to rounding mode `rounding_mode` |
507
508 <div id="fp-to-int-openpower-conversion-semantics"></div>
509 OpenPower conversion semantics (section A.2 page 999 (page 1023) of OpenPower ISA v3.1):
510
511 ```
512 def fp_to_int_open_power<fp, int>(v: fp) -> int:
513 if v is NaN:
514 return int::MIN_VALUE
515 if v >= int::MAX_VALUE:
516 return int::MAX_VALUE
517 if v <= int::MIN_VALUE:
518 return int::MIN_VALUE
519 return (int)rint(v, rounding_mode)
520 ```
521
522 <div id="fp-to-int-java-conversion-semantics"></div>
523 [Java conversion semantics](https://docs.oracle.com/javase/specs/jls/se16/html/jls-5.html#jls-5.1.3)
524 /
525 [Rust semantics](https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics)
526 (with adjustment to add non-truncate rounding modes):
527
528 ```
529 def fp_to_int_java<fp, int>(v: fp) -> int:
530 if v is NaN:
531 return 0
532 if v >= int::MAX_VALUE:
533 return int::MAX_VALUE
534 if v <= int::MIN_VALUE:
535 return int::MIN_VALUE
536 return (int)rint(v, rounding_mode)
537 ```
538
539 <div id="fp-to-int-javascript-conversion-semantics"></div>
540 Section 7.1 of the ECMAScript / JavaScript
541 [conversion semantics](https://262.ecma-international.org/11.0/#sec-toint32) (with adjustment to add non-truncate rounding modes):
542
543 ```
544 def fp_to_int_java_script<fp, int>(v: fp) -> int:
545 if v is NaN or infinite:
546 return 0
547 v = rint(v, rounding_mode) # assume no loss of precision in result
548 v = v mod int::VALUE_COUNT # 2^32 for i32, 2^64 for i64, result is non-negative
549 bits = (uint)v
550 return (int)bits
551 ```
552