replace common sections with [[!include]]
[libreriscv.git] / openpower / sv / int_fp_mv.mdwn
index b5a6b9a0f0ab3ca9dba5dffbba3128dffdbcffcf..af1262135828845ff9529e2367bb3a1a922b49e0 100644 (file)
@@ -1,5 +1,7 @@
 [[!tag standards]]
 
+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>
+
 # FPR-to-GPR and GPR-to-FPR
 
 TODO special constants instruction (e, tau/N, ln 2, sqrt 2, etc.) -- exclude any constants available through fmvis
@@ -13,13 +15,15 @@ Links:
 * <https://bugs.libre-soc.org/show_bug.cgi?id=230#c74>
 * <https://bugs.libre-soc.org/show_bug.cgi?id=230#c76>
 * <https://bugs.libre-soc.org/show_bug.cgi?id=887> fmvis
+* <https://bugs.libre-soc.org/show_bug.cgi?id=1015> int-fp RFC
 * [[int_fp_mv/appendix]]
 * [[sv/rfc/ls002]] - `fmvis` and `fishmv` External RFC Formal Submission
+* [[sv/rfc/ls006]] - int-fp-mv External RFC Formal Submission
 
 Trademarks:
 
 * Rust is a Trademark of the Rust Foundation
-* Java and Javascript are Trademarks of Oracle
+* Java and JavaScript are Trademarks of Oracle
 * LLVM is a Trademark of the LLVM Foundation
 * SPIR-V is a Trademark of the Khronos Group
 * OpenCL is a Trademark of Apple, Inc.
@@ -74,14 +78,16 @@ If adding new Integer <-> FP conversion instructions,
 the opportunity may be taken to modernise the instructions and make them
 well-suited for common/important conversion sequences:
 
-* **standard IEEE754** - used by most languages and CPUs
-* **standard OpenPOWER** - saturation with NaN
-  converted to minimum valid integer
-* **Java** - saturation with NaN converted to 0
-* **JavaScript** - modulo wrapping with Inf/NaN converted to 0
+* Int -> Float
+    * **standard IEEE754** - used by most languages and CPUs
+* Float -> Int
+    * **standard OpenPOWER** - saturation with NaN
+      converted to minimum valid integer
+    * **Java/Saturating** - saturation with NaN converted to 0
+    * **JavaScript** - modulo wrapping with Inf/NaN converted to 0
 
 The assembly listings in the [[int_fp_mv/appendix]] show how costly
-some of these language-specific conversions are: Javascript, the
+some of these language-specific conversions are: JavaScript, the
 worst case, is 32 scalar instructions including seven branch instructions.
 
 # Proposed New Scalar Instructions
@@ -239,269 +245,4 @@ fmvis f4, 0x3F80 # writes +1.0 to f4
 fishmv f4, 0x8000 # writes +1.00390625 to f4
 ```
 
-# Moves
-
-These instructions perform a straight unaltered bit-level copy from one Register
-File to another.
-
-# FPR to GPR moves
-
-* `fmvtg RT, FRA`
-* `fmvtg. RT, FRA`
-
-move a 64-bit float from a FPR to a GPR, just copying bits directly.
-As a direct bitcopy, no exceptions occur and no status flags are set.
-
-Rc=1 tests RT and sets CR0, exactly like all other Scalar Fixed-Point
-operations.
-
-* `fmvtgs RT, FRA`
-* `fmvtgs. RT, FRA`
-
-move a 32-bit float from a FPR to a GPR, just copying bits. Converts the
-64-bit float in `FRA` to a 32-bit float, using the same method as `stfs`,
-then writes the 32-bit float to `RT`, setting the high 32-bits to zeros.
-Effectively, `fmvtgs` is a macro-fusion of `stfs` and `lwz` and therefore
-does not behave like `frsp` and does not set any fp exception flags.
-
-Since RT is a GPR, Rc=1 follows standard *integer* behaviour, i.e.
-tests RT and sets CR0.
-
-# GPR to FPR moves
-
-`fmvfg FRT, RA`
-
-move a 64-bit float from a GPR to a FPR, just copying bits. No exceptions
-are raised, no flags are altered of any kind.
-
-Rc=1 tests FRT and sets CR1
-
-`fmvfgs FRT, RA`
-
-move a 32-bit float from a GPR to a FPR, just copying bits. Converts the
-32-bit float in `RA` to a 64-bit float, using the same method as `lfs`,
-then writes the 64-bit float to `FRT`. Effectively, `fmvfgs` is a
-macro-fusion of `stw` and `lfs` and therefore no fp exception flags are set.
-
-Rc=1 tests FRT and sets CR1, following usual fp Rc=1 semantics.
-
-# Conversions
-
-Unlike the move instructions
-these instructions perform conversions between Integer and
-Floating Point. Truncation can therefore occur, as well
-as exceptions.
-
-Mode values:
-
-| Mode | `rounding_mode` | Semantics                        |
-|------|-----------------|----------------------------------|
-| 000  | from `FPSCR`    | [OpenPower semantics]            |
-| 001  | Truncate        | [OpenPower semantics]            |
-| 010  | from `FPSCR`    | [Java semantics]                 |
-| 011  | Truncate        | [Java semantics]                 |
-| 100  | from `FPSCR`    | [JavaScript semantics]           |
-| 101  | Truncate        | [JavaScript semantics]           |
-| rest | --              | illegal instruction trap for now |
-
-[OpenPower semantics]: #fp-to-int-openpower-conversion-semantics
-[Java semantics]: #fp-to-int-java-conversion-semantics
-[JavaScript semantics]: #fp-to-int-javascript-conversion-semantics
-
-## GPR to FPR conversions
-
-**Format**
-
-|  0-5   | 6-10 | 11-15  | 16-25 | 26-30 | 31 | Form |
-|--------|------|--------|-------|-------|----|------|
-|  Major | FRT  | //Mode | RA    | XO    | Rc |X-Form|
-
-All of the following GPR to FPR conversions use the rounding mode from `FPSCR`.
-
-* `fcvtfgw FRT, RA`
-  Convert from 32-bit signed integer in the GPR `RA` to 64-bit float in 
-  `FRT`.
-* `fcvtfgws FRT, RA`
-  Convert from 32-bit signed integer in the GPR `RA` to 32-bit float in 
-  `FRT`.
-* `fcvtfguw FRT, RA`
-  Convert from 32-bit unsigned integer in the GPR `RA` to 64-bit float in 
-  `FRT`.
-* `fcvtfguws FRT, RA`
-  Convert from 32-bit unsigned integer in the GPR `RA` to 32-bit float in 
-  `FRT`.
-* `fcvtfgd FRT, RA`
-  Convert from 64-bit signed integer in the GPR `RA` to 64-bit float in 
-  `FRT`.
-* `fcvtfgds FRT, RA`
-  Convert from 64-bit signed integer in the GPR `RA` to 32-bit float in 
-  `FRT`.
-* `fcvtfgud FRT, RA`
-  Convert from 64-bit unsigned integer in the GPR `RA` to 64-bit float in 
-  `FRT`.
-* `fcvtfguds FRT, RA`
-  Convert from 64-bit unsigned integer in the GPR `RA` to 32-bit float in 
-  `FRT`.
-
-## FPR to GPR (Integer) conversions
-
-<div id="fpr-to-gpr-conversion-mode"></div>
-
-Different programming languages turn out to have completely different
-semantics for FP to Integer conversion.  Below is an overview
-of the different variants, listing the languages and hardware that
-implements each variant.
-
-**Standard IEEE754 conversion**
-
-This conversion is outlined in the IEEE754 specification.  It is used
-by nearly all programming languages and CPUs.  In the case of OpenPOWER,
-the rounding mode is read from FPSCR
-
-**Standard OpenPower conversion**
-
-This conversion, instead of exact IEEE754 Compliance, performs
-"saturation with NaN converted to minimum valid integer". This
-is also exactly the same as the x86 ISA conversion semantics.
-OpenPOWER however has instructions for both:
-
-* rounding mode read from FPSCR
-* rounding mode always set to truncate
-
-**Java conversion**
-
-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
-[Java conversion semantics](#fp-to-int-java-conversion-semantics).
-
-Those same semantics are used in some way by all of the following languages (not necessarily for the default conversion method):
-
-* Java's
-  [FP -> Integer conversion](https://docs.oracle.com/javase/specs/jls/se16/html/jls-5.html#jls-5.1.3)
-* Rust's FP -> Integer conversion using the
-  [`as` operator](https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics)
-* LLVM's
-  [`llvm.fptosi.sat`](https://llvm.org/docs/LangRef.html#llvm-fptosi-sat-intrinsic) and
-  [`llvm.fptoui.sat`](https://llvm.org/docs/LangRef.html#llvm-fptoui-sat-intrinsic) intrinsics
-* SPIR-V's OpenCL dialect's
-  [`OpConvertFToU`](https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#OpConvertFToU) and
-  [`OpConvertFToS`](https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#OpConvertFToS)
-  instructions when decorated with
-  [the `SaturatedConversion` decorator](https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#_a_id_decoration_a_decoration).
-* WebAssembly has also introduced
- [trunc_sat_u](ttps://webassembly.github.io/spec/core/exec/numerics.html#op-trunc-sat-u) and
- [trunc_sat_s](https://webassembly.github.io/spec/core/exec/numerics.html#op-trunc-sat-s)
-
-**JavaScript conversion**
-
-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).
-
-This instruction is present in ARM assembler as FJCVTZS
-<https://developer.arm.com/documentation/dui0801/g/hko1477562192868>
-
-**Format**
-
-|  0-5   | 6-10 | 11-15  | 16-25 | 26-30 | 31 | Form |
-|--------|------|--------|-------|-------|----|------|
-|  Major | RT   | //Mode | FRA   | XO    | Rc |X-Form|
-
-**Rc=1 and OE=1**
-
-All of these insructions have an Rc=1 mode which sets CR0
-in the normal way for any instructions producing a GPR result.
-Additionally, when OE=1, if the numerical value of the FP number
-is not 100% accurately preserved (due to truncation or saturation
-and including when the FP number was NaN) then this is considered
-to be an integer Overflow condition, and CR0.SO, XER.SO and XER.OV
-are all set as normal for any GPR instructions that overflow.
-
-**Instructions**
-
-* `fcvttgw RT, FRA, Mode`
-  Convert from 64-bit float to 32-bit signed integer, writing the result
-  to the GPR `RT`. Converts using [mode `Mode`]. Similar to `fctiw` or `fctiwz`
-* `fcvttguw RT, FRA, Mode`
-  Convert from 64-bit float to 32-bit unsigned integer, writing the result
-  to the GPR `RT`. Converts using [mode `Mode`]. Similar to `fctiwu` or `fctiwuz`
-* `fcvttgd RT, FRA, Mode`
-  Convert from 64-bit float to 64-bit signed integer, writing the result
-  to the GPR `RT`. Converts using [mode `Mode`]. Similar to `fctid` or `fctidz`
-* `fcvttgud RT, FRA, Mode`
-  Convert from 64-bit float to 64-bit unsigned integer, writing the result
-  to the GPR `RT`. Converts using [mode `Mode`]. Similar to `fctidu` or `fctiduz`
-* `fcvtstgw RT, FRA, Mode`
-  Convert from 32-bit float to 32-bit signed integer, writing the result
-  to the GPR `RT`. Converts using [mode `Mode`]
-* `fcvtstguw RT, FRA, Mode`
-  Convert from 32-bit float to 32-bit unsigned integer, writing the result
-  to the GPR `RT`. Converts using [mode `Mode`]
-* `fcvtstgd RT, FRA, Mode`
-  Convert from 32-bit float to 64-bit signed integer, writing the result
-  to the GPR `RT`. Converts using [mode `Mode`]
-* `fcvtstgud RT, FRA, Mode`
-  Convert from 32-bit float to 64-bit unsigned integer, writing the result
-  to the GPR `RT`. Converts using [mode `Mode`]
-
-[mode `Mode`]: #fpr-to-gpr-conversion-mode
-
-## FP to Integer Conversion Pseudo-code
-
-Key for pseudo-code:
-
-| term                      | result type | definition                                                                                         |
-|---------------------------|-------------|----------------------------------------------------------------------------------------------------|
-| `fp`                      | --          | `f32` or `f64` (or other types from SimpleV)                                                       |
-| `int`                     | --          | `u32`/`u64`/`i32`/`i64` (or other types from SimpleV)                                              |
-| `uint`                    | --          | the unsigned integer of the same bit-width as `int`                                                |
-| `int::BITS`               | `int`       | the bit-width of `int`                                                                             |
-| `uint::MIN_VALUE`         | `uint`      | the minimum value `uint` can store: `0`                   |
-| `uint::MAX_VALUE`          | `uint`       | the maximum value `uint` can store: `2^int::BITS - 1`  |
-| `int::MIN_VALUE`          | `int`       | the minimum value `int` can store : `-2^(int::BITS-1)`              |
-| `int::MAX_VALUE`          | `int`       | the maximum value `int` can store :  `2^(int::BITS-1) - 1`  |
-| `int::VALUE_COUNT`        | Integer     | the number of different values `int` can store (`2^int::BITS`). too big to fit in `int`.           |
-| `rint(fp, rounding_mode)` | `fp`        | rounds the floating-point value `fp` to an integer according to rounding mode `rounding_mode`      |
-
-<div id="fp-to-int-openpower-conversion-semantics"></div>
-OpenPower conversion semantics (section A.2 page 999 (page 1023) of OpenPower ISA v3.1):
-
-```
-def fp_to_int_open_power<fp, int>(v: fp) -> int:
-    if v is NaN:
-        return int::MIN_VALUE
-    if v >= int::MAX_VALUE:
-        return int::MAX_VALUE
-    if v <= int::MIN_VALUE:
-        return int::MIN_VALUE
-    return (int)rint(v, rounding_mode)
-```
-
-<div id="fp-to-int-java-conversion-semantics"></div>
-[Java conversion semantics](https://docs.oracle.com/javase/specs/jls/se16/html/jls-5.html#jls-5.1.3)
-/
-[Rust semantics](https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics)
-(with adjustment to add non-truncate rounding modes):
-
-```
-def fp_to_int_java<fp, int>(v: fp) -> int:
-    if v is NaN:
-        return 0
-    if v >= int::MAX_VALUE:
-        return int::MAX_VALUE
-    if v <= int::MIN_VALUE:
-        return int::MIN_VALUE
-    return (int)rint(v, rounding_mode)
-```
-
-<div id="fp-to-int-javascript-conversion-semantics"></div>
-Section 7.1 of the ECMAScript / JavaScript
-[conversion semantics](https://262.ecma-international.org/11.0/#sec-toint32) (with adjustment to add non-truncate rounding modes):
-
-```
-def fp_to_int_java_script<fp, int>(v: fp) -> int:
-    if v is NaN or infinite:
-        return 0
-    v = rint(v, rounding_mode)  # assume no loss of precision in result
-    v = v mod int::VALUE_COUNT  # 2^32 for i32, 2^64 for i64, result is non-negative
-    bits = (uint)v
-    return (int)bits
-```
-
+[[!inline pages="openpower/sv/int_fp_mv/moves_and_conversions" raw=yes ]]