Date: Tue, 8 Apr 2025 14:32:10 +0000
You can write a proposal.
Although I don't see why this couldn't easily be done as a library.
________________________________
From: Std-Proposals <std-proposals-bounces_at_[hidden]> on behalf of John Platts via Std-Proposals <std-proposals_at_[hidden]>
Sent: Tuesday, April 8, 2025 1:58:24 PM
To: std-proposals_at_[hidden] <std-proposals_at_[hidden]>
Cc: John Platts <john_platts_at_[hidden]>
Subject: [std-proposals] Adding safe saturated floating-point to integer cast to C++26
Introduction:
There is a need to add a safe saturated floating-point integer cast that is guaranteed to saturate a floating-point value to be within the range of the target integer type instead of invoking undefined behavior. There are programming languages other than C++ that already have support for safe saturated floating-point to integer casts, including Java. Some CPU's also have hardware instructions that can do saturated floating-point to integer casts, including ARM CPU's with hardware floating point, AArch64, x86 CPU's with AVX10.2, and PowerPC.
Motivation and Scope:
This is important as some applications require saturating, well-defined behavior for floating-point to integer conversions that are out of range of the target integer type. In addition, some C++ code is ported to C++ from languages such as Java where floating-point to integer casts have saturating semantics.
Impact on the standard:
It is possible to implement a safe saturated floating-point-to-integer conversion using pure C++11 code (including constexpr support with C++14 or later), but compiler builtins are needed to allow for additional optimizations beyond what a pure C++11 implementation would provide (such as vectorization or saturation of out-of-range values).
Design decisions:
The need to add a safe saturated floating-point to integer operation is needed to provide for portable, optimal implementations that provide for additional optimization opportunities, including vectorization and optimization down to a single saturated floating-point to integer conversion instruction when supported on the target CPU.
The safe saturated floating-point to integer operation is required to have well-defined behavior if no trapping floating-point exceptions are raised, even for NaN input values. The safe saturated floating-point to integer operation is permitted to return an arbitrary, implementation-defined value for NaN input values, but such an arbitrary value must be treated as a valid result and must not invoke any undefined behavior if a trapping floating-point exception is not raised.
Although I don't see why this couldn't easily be done as a library.
________________________________
From: Std-Proposals <std-proposals-bounces_at_[hidden]> on behalf of John Platts via Std-Proposals <std-proposals_at_[hidden]>
Sent: Tuesday, April 8, 2025 1:58:24 PM
To: std-proposals_at_[hidden] <std-proposals_at_[hidden]>
Cc: John Platts <john_platts_at_[hidden]>
Subject: [std-proposals] Adding safe saturated floating-point to integer cast to C++26
Introduction:
There is a need to add a safe saturated floating-point integer cast that is guaranteed to saturate a floating-point value to be within the range of the target integer type instead of invoking undefined behavior. There are programming languages other than C++ that already have support for safe saturated floating-point to integer casts, including Java. Some CPU's also have hardware instructions that can do saturated floating-point to integer casts, including ARM CPU's with hardware floating point, AArch64, x86 CPU's with AVX10.2, and PowerPC.
Motivation and Scope:
This is important as some applications require saturating, well-defined behavior for floating-point to integer conversions that are out of range of the target integer type. In addition, some C++ code is ported to C++ from languages such as Java where floating-point to integer casts have saturating semantics.
Impact on the standard:
It is possible to implement a safe saturated floating-point-to-integer conversion using pure C++11 code (including constexpr support with C++14 or later), but compiler builtins are needed to allow for additional optimizations beyond what a pure C++11 implementation would provide (such as vectorization or saturation of out-of-range values).
Design decisions:
The need to add a safe saturated floating-point to integer operation is needed to provide for portable, optimal implementations that provide for additional optimization opportunities, including vectorization and optimization down to a single saturated floating-point to integer conversion instruction when supported on the target CPU.
The safe saturated floating-point to integer operation is required to have well-defined behavior if no trapping floating-point exceptions are raised, even for NaN input values. The safe saturated floating-point to integer operation is permitted to return an arbitrary, implementation-defined value for NaN input values, but such an arbitrary value must be treated as a valid result and must not invoke any undefined behavior if a trapping floating-point exception is not raised.
Received on 2025-04-08 14:32:13