minor priority/wording change on ls006.fpintmv
[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 does not 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: <div id="fpr-to-gpr-conversion-p-type"></div>
11 Used by most other PowerISA instructions,
12 as well as commonly used floating-point to integer conversions on x86.
13
14 * S-Type: <div id="fpr-to-gpr-conversion-s-type"></div>
15 Used for several notable programming languages:
16
17 * Java's conversion from `float`/`double` to `long`/`int`[^java_fp_to_int]
18 * Rust's `as` operator[^rust_as_operator]
19 * LLVM's `llvm.fptosi.sat`[^llvm_fptosi_sat] and
20 `llvm.fptoui.sat`[^llvm_fptoui_sat] intrinsics
21 * SPIR-V's OpenCL dialect's `OpConvertFToU`[^SPIRV_OpConvertFToU] and
22 `OpConvertFToS`[^SPIRV_OpConvertFToS] instructions when decorated with
23 the `SaturatedConversion`[^SPIRV_SaturatedConversion] decorator.
24 * Also WebAssembly's `trunc_sat_u`[^trunc_sat_u] and
25 `trunc_sat_s`[^trunc_sat_s] instructions,
26
27
28 * E-Type: <div id="fpr-to-gpr-conversion-e-type"></div>
29 Used for ECMAScript's `ToInt32` abstract operation[^ECMAScript_ToInt32].
30 Also implemented in ARMv8.3A as the `FJCVTZS` instruction[^ARM_FJCVTZS].
31
32 [^trunc_sat_u]: WASM's `trunc_sat_u`:
33 <https://webassembly.github.io/spec/core/exec/numerics.html#op-trunc-sat-u>
34 [^trunc_sat_s]: WASM's `trunc_sat_s`:
35 <https://webassembly.github.io/spec/core/exec/numerics.html#op-trunc-sat-s>
36 [^java_fp_to_int]: Java `float`/`double` to `long`/`int` conversion:
37 <https://docs.oracle.com/javase/specs/jls/se16/html/jls-5.html#jls-5.1.3>
38 [^rust_as_operator]: Rust's `as` operator:
39 <https://doc.rust-lang.org/1.70.0/reference/expressions/operator-expr.html#numeric-cast>
40 [^llvm_fptosi_sat]: LLVM's `llvm.fptosi.sat` intrinsic:
41 <https://llvm.org/docs/LangRef.html#llvm-fptosi-sat-intrinsic>
42 [^llvm_fptoui_sat]: LLVM's `llvm.fptoui.sat` intrinsic:
43 <https://llvm.org/docs/LangRef.html#llvm-fptoui-sat-intrinsic>
44 [^SPIRV_OpConvertFToU]: SPIR-V's `OpConvertFToU` instruction:
45 <https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#OpConvertFToU>
46 [^SPIRV_OpConvertFToS]: SPIR-V's `OpConvertFToS` instruction:
47 <https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#OpConvertFToS>
48 [^SPIRV_SaturatedConversion]: SPIR-V's `SaturatedConversion` decorator:<br/>
49 <https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#_a_id_decoration_a_decoration>
50 [^ECMAScript_ToInt32]: ECMAScript's `ToInt32` abstract operation:
51 <https://262.ecma-international.org/14.0/#sec-toint32>
52 [^ARM_FJCVTZS]: ARM's `FJCVTZS` instruction:
53 <https://developer.arm.com/documentation/dui0801/g/hko1477562192868>
54
55 ### Floating-point to Integer Conversion Semantics Summary
56
57 Let `round` be the result of `bfp_ROUND_TO_INTEGER(rmode, input)`.
58 Let `w` be the number of bits in the result's type.
59 The result of Floating-point to Integer conversion is as follows:
60
61 ```
62 +------+------+---------------------------------------------------------------+
63 |Type| Result | Category of rounding |
64 | | Sign +----------+-----------+----------+-----------+---------+-------+
65 | | | NaN | +Inf | -Inf | > Max | < Min | Else |
66 | | | | | | Possible | Possible| |
67 | | | | | | Result | Result | |
68 +----+--------+----------+-----------+----------+-----------+---------+-------+
69 | P |Unsigned| 0 | 2^w - 1 | 0 | 2^w - 1 | 0 | round |
70 | +--------+----------+-----------+----------+-----------+---------+-------+
71 | | Signed | -2^(w-1) | 2^(w-1)-1 | -2^(w-1) | 2^(w-1)-1 | -2^(w-1)| round |
72 +----+--------+----------+-----------+----------+-----------+---------+-------+
73 | S |Unsigned| 0 | 2^w - 1 | 0 | 2^w - 1 | 0 | round |
74 | +--------+----------+-----------+----------+-----------+---------+-------+
75 | | Signed | 0 | 2^(w-1)-1 | -2^(w-1) | 2^(w-1)-1 | -2^(w-1)| round |
76 +----+--------+----------+-----------+----------+-----------+---------+-------+
77 | E | Either | 0 | round & (2^w - 1) |
78 +----+--------+---------------------------------+-----------------------------+
79 ```