Floating-point to Integer Conversion Overview

IEEE 754 doesn't specify what results are obtained when converting a NaN or out-of-range floating-point value to integer, so different programming languages and ISAs have made different choices. The different conversion modes supported by the cffpr instruction are as follows:

  • P-Type:

    Used by most other PowerISA instructions, as well as commonly used floating-point to integer conversions on x86.

  • S-Type:

    Used for WebAssembly's trunc_sat_u1 and trunc_sat_s2 instructions, as well as several notable programming languages:

    • Java's conversion from float/double to long/int3
    • Rust's as operator4
    • LLVM's llvm.fptosi.sat5 and llvm.fptoui.sat6 intrinsics
    • SPIR-V's OpenCL dialect's OpConvertFToU7 and OpConvertFToS8 instructions when decorated with the SaturatedConversion9 decorator.
  • E-Type:

    Used for ECMAScript's ToInt32 abstract operation10. Also implemented in ARMv8.3A as the FJCVTZS instruction11.

Floating-point to Integer Conversion Semantics Summary

Let rounded be the result of bfp_ROUND_TO_INTEGER(rmode, input). Let w be the number of bits in the result's type. The result of Floating-point to Integer conversion is as follows:

+--------+------------+-----------------------------------------------------------------------+
| Type   | Result's   | Category of rounded                                                   |
|        | Signedness +-----------+-----------+-----------+-----------+-----------+-----------+
|        |            | NaN       | +Infinity | -Infinity | > Maximum | < Minimum | Otherwise |
|        |            |           |           |           | Possible  | Possible  |           |
|        |            |           |           |           | Result    | Result    |           |
+--------+------------+-----------+-----------+-----------+-----------+-----------+-----------+
| P-Type | Unsigned   | 0         | 2^w - 1   | 0         | 2^w - 1   | 0         | rounded   |
|        +------------+-----------+-----------+-----------+-----------+-----------+-----------+
|        | Signed     | -2^(w-1)  | 2^(w-1)-1 | -2^(w-1)  | 2^(w-1)-1 | -2^(w-1)  | rounded   |
+--------+------------+-----------+-----------+-----------+-----------+-----------+-----------+
| S-Type | Unsigned   | 0         | 2^w - 1   | 0         | 2^w - 1   | 0         | rounded   |
|        +------------+-----------+-----------+-----------+-----------+-----------+-----------+
|        | Signed     | 0         | 2^(w-1)-1 | -2^(w-1)  | 2^(w-1)-1 | -2^(w-1)  | rounded   |
+--------+------------+-----------+-----------+-----------+-----------+-----------+-----------+
| E-Type | Either     | 0                                 | rounded & (2^w - 1)               |
+--------+------------+-----------------------------------+-----------------------------------+