rewrite cvt_fp_to_int_overview.mdwn to be more like a specification.
[libreriscv.git] / openpower / sv / int_fp_mv / cvt_fp_to_int_overview.mdwn
1 ## Floating-point to Integer Conversion Overview
2
3 <div id="fpr-to-gpr-conversion-mode"></div>
4
5 IEEE 754 doesn't specify what results are obtained when converting a NaN
6 or out-of-range floating-point value to integer, so different programming
7 languages and ISAs have made different choices. The different conversion
8 modes supported by the `cffpr` instruction are as follows:
9
10 * P-Type:
11 This type of conversion is that used by most other PowerISA instructions,
12 as well as commonly used floating-point to integer conversions on x86.
13
14 * S-Type:
15 This type of conversion is used for WebAssembly's
16 [`trunc_sat_u`](https://webassembly.github.io/spec/core/exec/numerics.html#op-trunc-sat-u)
17 and
18 [trunc_sat_s](https://webassembly.github.io/spec/core/exec/numerics.html#op-trunc-sat-s)
19 instructions, as well as several notable programming languages:
20
21 * Java's conversion from
22 [`float`/`double` to `long`/`int`](https://docs.oracle.com/javase/specs/jls/se16/html/jls-5.html#jls-5.1.3)
23 * Rust's [`as` operator](https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics)
24 * LLVM's [`llvm.fptosi.sat`](https://llvm.org/docs/LangRef.html#llvm-fptosi-sat-intrinsic) and
25 [`llvm.fptoui.sat`](https://llvm.org/docs/LangRef.html#llvm-fptoui-sat-intrinsic) intrinsics
26 * SPIR-V's OpenCL dialect's
27 [`OpConvertFToU`](https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#OpConvertFToU) and
28 [`OpConvertFToS`](https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#OpConvertFToS)
29 instructions when decorated with
30 [the `SaturatedConversion` decorator](https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#_a_id_decoration_a_decoration).
31
32 * E-Type:
33 This type of conversion is used for
34 [ECMAScript's `ToInt32` abstract operation](https://262.ecma-international.org/14.0/#sec-toint32).
35
36 This type of conversion is also implemented in ARMv8.3A as the `FJCVTZS`
37 instruction.
38 <https://developer.arm.com/documentation/dui0801/g/hko1477562192868>
39
40 ### Floating-point to Integer Conversion Semantics Summary
41
42 Let `rounded` be the result of `bfp_ROUND_TO_INTEGER(rmode, input)`.
43 Let `w` be the number of bits in the result's type.
44 The result of Floating-point to Integer conversion is as follows:
45
46 ```
47 +--------+------------+-----------------------------------------------------------------------+
48 | Kind | Result's | Category of rounded |
49 | | Signedness +-----------+-----------+-----------+-----------+-----------+-----------+
50 | | | NaN | +Infinity | -Infinity | > Maximum | < Minimum | Otherwise |
51 | | | | | | Possible | Possible | |
52 | | | | | | Result | Result | |
53 +--------+------------+-----------+-----------+-----------+-----------+-----------+-----------+
54 | P-Type | Unsigned | 0 | 2^w - 1 | 0 | 2^w - 1 | 0 | rounded |
55 | +------------+-----------+-----------+-----------+-----------+-----------+-----------+
56 | | Signed | -2^(w-1) | 2^(w-1)-1 | -2^(w-1) | 2^(w-1)-1 | -2^(w-1) | rounded |
57 +--------+------------+-----------+-----------+-----------+-----------+-----------+-----------+
58 | S-Type | Unsigned | 0 | 2^w - 1 | 0 | 2^w - 1 | 0 | rounded |
59 | +------------+-----------+-----------+-----------+-----------+-----------+-----------+
60 | | Signed | 0 | 2^(w-1)-1 | -2^(w-1) | 2^(w-1)-1 | -2^(w-1) | rounded |
61 +--------+------------+-----------+-----------+-----------+-----------+-----------+-----------+
62 | E-Type | Either | 0 | rounded & (2^w - 1) |
63 +--------+------------+-----------------------------------+-----------------------------------+
64 ```