Hi CUDA Team and Community,
I observed an inconsistency in CUDA 13.2 when passing subnormal float inputs (specifically 1.401298464e-45f, FLT_TRUE_MIN, Hex: 0x00000001) into fast math intrinsic functions (__sincosf, __sinf, and __tanf).
The output values and IEEE 754 bit patterns differ depending on whether the input is evaluated as a compile-time constant (literal) or a dynamic variable (runtime argument).
Reproducer & Details
1. Dynamic Variable Input (arg) at Runtime
When passed as a runtime variable, execution happens on device hardware and follows Flush-To-Zero (FTZ) behavior for fast math intrinsics:
-
__sinf(arg) $\rightarrow$ Flushed to 0.0f (0x00000000)
-
__tanf(arg) $\rightarrow$ Flushed to 0.0f (0x00000000)
-
__sincosf(arg) $\rightarrow$ sin flushed to 0.0f (0x00000000), cos evaluates to 1.0f (0x3f800000)
2. Compile-Time Constant Input (const) via Constant Folding
When passed as a literal, NVCC performs constant folding on the host side during compilation, applying host IEEE 754 subnormal rules instead of GPU FTZ semantics:
-
Host-side constant folding preserves subnormal values or evaluates them differently from GPU FTZ hardware rules.
-
Furthermore, __sinf(const), __sincosf(const), and __tanf(const) exhibit mutually inconsistent handling during constant folding.
Questions for NVIDIA
-
Is this a compiler bug? Should NVCC host-side constant folding emulate the target GPU device FTZ rules when optimizing CUDA fast math intrinsics (__*f)?
-
Normative Behavior: Which result (const host evaluation vs. arg GPU runtime evaluation) is considered the standard normative output for subnormal inputs in CUDA fast math APIs?
Thanks for the report. Yes, these are known compiler defects. Compiler currently tries to fold the correctly evaluated mathematical values instead of matching the runtime behavior for low level fast math primitives.
Flushing behavior is documented for these fast math intrinsics, please see here: 7. Single Precision Intrinsics — CUDA Math API Reference Manual 13.4 documentation
Not sure what may be referred to as mutually inconsistent handling, an example might help. But let me offer a speculation here: fast __tanf is implemented as fast division of fast sine over fast cosine (it is documented as such too), so discrepancies may come from the way the compiler does the const-folding on each of the building block (i.e. fast sine, fast cosine, fast reciprocal + multiply)
Regarding the normative behavior, subnormal flushing is documented for these three functions, but there’s no general rule. More information is available here: 5.5. Floating-Point Computation — CUDA Programming Guide , please notice the __tanhf fast hyperbolic tangent is documented as not flushing subnormals even in the presence of -ftz=true.
The concrete code examples are attached below, you may also refer to: Compiler Explorer
#include <iostream>
#include <iomanip>
#include <cuda_runtime.h>
#include <cstdint>
// Convert float bits to uint32_t to inspect raw IEEE 754 bit pattern
__device__ inline uint32_t float_to_bits(float f) {
return __float_as_int(f);
}
__global__ void test_fast_math_kernel(float dynamic_val) {
// -------------------------------------------------------------
// 1. Dynamic argument mode (arg): Passed at runtime to prevent
// compile-time constant folding by NVCC.
// -------------------------------------------------------------
volatile float arg = dynamic_val; // volatile forces memory read
float arg_sincos_s, arg_sincos_c;
__sincosf(arg, &arg_sincos_s, &arg_sincos_c);
float arg_sin = __sinf(arg);
float arg_tan = __tanf(arg);
// -------------------------------------------------------------
// 2. Compile-time constant mode (const): Direct literal triggers
// compile-time constant folding by NVCC compiler frontend.
// -------------------------------------------------------------
constexpr float const_val = 1.401298464e-45f;
float const_sincos_s, const_sincos_c;
__sincosf(const_val, &const_sincos_s, &const_sincos_c);
float const_sin = __sinf(const_val);
float const_tan = __tanf(const_val);
// -------------------------------------------------------------
// 3. Output results (displays floating-point values and Hex bits)
// -------------------------------------------------------------
printf("================ CUDA 13.2 Math API Test ================\n");
printf("Target Input Value : 1.401298464e-45f (Hex: 0x%08x)\n\n", float_to_bits(dynamic_val));
printf("--- [1. Dynamic Argument Input (arg)] ---\n");
printf("__sincosf(arg) -> sin: %.10e (0x%08x)\n", arg_sincos_s, float_to_bits(arg_sincos_s));
printf(" -> cos: %.10e (0x%08x)\n", arg_sincos_c, float_to_bits(arg_sincos_c));
printf("__sinf(arg) -> %.10e (0x%08x)\n", arg_sin, float_to_bits(arg_sin));
printf("__tanf(arg) -> %.10e (0x%08x)\n\n", arg_tan, float_to_bits(arg_tan));
printf("--- [2. Compile-time Constant Input (const)] ---\n");
printf("__sincosf(const)-> sin: %.10e (0x%08x)\n", const_sincos_s, float_to_bits(const_sincos_s));
printf(" -> cos: %.10e (0x%08x)\n", const_sincos_c, float_to_bits(const_sincos_c));
printf("__sinf(const) -> %.10e (0x%08x)\n", const_sin, float_to_bits(const_sin));
printf("__tanf(const) -> %.10e (0x%08x)\n", const_tan, float_to_bits(const_tan));
printf("=========================================================\n");
}
int main() {
// Smallest denormalized single-precision float (FLT_TRUE_MIN, 0x00000001)
float input_val = 1.401298464e-45f;
test_fast_math_kernel<<<1, 1>>>(input_val);
cudaError_t err = cudaDeviceSynchronize();
if (err != cudaSuccess) {
std::cerr << "CUDA Error: " << cudaGetErrorString(err) << std::endl;
return -1;
}
return 0;
}
result:
================ CUDA 13.2 Math API Test ================
Target Input Value : 1.401298464e-45f (Hex: 0x00000001)
--- [1. Dynamic Argument Input (arg)] ---
__sincosf(arg) -> sin: 0.0000000000e+00 (0x00000000)
-> cos: 1.0000000000e+00 (0x3f800000)
__sinf(arg) -> 0.0000000000e+00 (0x00000000)
__tanf(arg) -> 0.0000000000e+00 (0x00000000)
--- [2. Compile-time Constant Input (const)] ---
__sincosf(const)-> sin: 1.4012984643e-45 (0x00000001)
-> cos: 1.0000000000e+00 (0x3f800000)
__sinf(const) -> 1.4012984643e-45 (0x00000001)
__tanf(const) -> 1.4012984643e-45 (0x00000001)
=========================================================
Normative Behavior: Which result (const host evaluation vs. arg GPU runtime evaluation) is considered the standard normative output for subnormal inputs in CUDA fast math APIs?
The API documentation for __sinf() says: subnormal outputs are flushed to sign preserving zero, accuracy is guaranteed only as absolute error tolerance in a specific input interval. From that you may conclude that the correct behavior for __sinf(+min_denorm) = +0.0f
Cosine function doesn’t care about denormals, they are too close to zero to make any difference, and so result is correctly rounded to 1.0 anyway.
Thus, for the __tanf() - it shall be zero because it is implemented and documented as __sinf(+min_denorm) / __cosf(+min_denorm) = 0.0 / 1.0 = 0.0
Therefore, const-folded results are wrong because they diverge from the specified behavior.
When we refer to the behavior specified by the documentation, does this mean the result obtained at GPU runtime when passing the value as a variable argument?
Documentation doesn’t distinguish the two cases. So as a user you may expect consistency. Inconsistent behavior of the compile-time const-folding optimization is a bug, I already acknowledged that. Runtime behavior (value passed as unknown to the compiler) is consistent with the documentation, so shall be considered correct. However, I will not go so far as to declare that the GPU runtime result is the specification here. Currently we guarantee runtime results to be within certain tolerance from the mathematical result. This is intended to be a more durable guarantee than a specific implementation that may change in the future.
Just a reminder, these are intended to be fast math operations, so their specification is intentionally relaxed to allow for greater flexibility and more performance potential.
Thank you for your reply.