[Runtime] Fix subnormal rounding and FP4/FP6 saturation (#25249)

Partially addresses #25111.

The runtime conversion helpers return zero for some f32 values just
above half the smallest subnormal, and for FP4/FP6 values whose rounding
carries past the largest exponent.

Allow the implied f32 significand bit in the subnormal rounding shift
and route rounded overflow through the existing infinity/saturation
handler. Tests cover both signs, neighboring f32 values around the
boundaries, and unsigned-zero formats.

The E8M0 rounding change mention in the issue is deferred. This PR takes
no position on the disputed E8M0 midpoint rule and will not close the
broader issue.

Fresh validation of `f39e31941` on macOS arm64 / Apple Clang 21:
- All 41 math tests pass in the normal C++17 build.
- All 29 float-conversion tests pass with AddressSanitizer and
UndefinedBehaviorSanitizer and `-fno-sanitize-recover=all`.
- The same narrowed conversion tests against the original base header
produce 11 failing tests and 18 passing tests, confirming coverage of
the retained fixes.
- The complete strict-sanitizer run stops in the unrelated
`BitwiseScansTest.CLZ64` test because it calls `__builtin_clz(0)`. The
exact same report reproduces with the original base header; a clean
full-suite sanitizer result is not claimed.
- clang-format --dry-run --Werror and git diff --check pass.

Tests were compiled directly from the complete math_test.cc.

Assisted-by: Codex

---------

Signed-off-by: Rayeed Zaman <rayeedzaman@gmail.com>
diff --git a/runtime/src/iree/base/internal/math.h b/runtime/src/iree/base/internal/math.h
index 7afffed..600eadd 100644
--- a/runtime/src/iree/base/internal/math.h
+++ b/runtime/src/iree/base/internal/math.h
@@ -430,7 +430,7 @@
       // by right-shifting the subnormal mantissa.
       int shift_amount = f32_mantissa_bits - dst_mantissa_bits -
                          arithmetic_exp + dst_arithmetic_exp;
-      if (shift_amount < 0 || shift_amount > f32_mantissa_bits) {
+      if (shift_amount < 0 || shift_amount > f32_mantissa_bits + 1) {
         dst_mantissa = 0;
       } else {
         // Source f32 value is normal so has an implied 1... leading bit.
@@ -467,12 +467,12 @@
       // case: the above if() branch already set biased_f32_mantissa=0, so we
       // will be generating a 0 mantissa, as needed for infinite values. The one
       // case where special handling is needed is when the destination type has
-      // no infinities and we need to generate NaN.
+      // no infinities and needs to saturate or generate NaN.
       dst_exp = (arithmetic_exp + dst_exp_bias) << dst_exp_shift;
       dst_mantissa =
           biased_f32_mantissa >> (f32_mantissa_bits - dst_mantissa_bits);
       if (!have_infinity && dst_exp > dst_exp_mask) {
-        generate_nan = true;
+        generate_inf = true;
       }
     }
   }
diff --git a/runtime/src/iree/base/internal/math_test.cc b/runtime/src/iree/base/internal/math_test.cc
index 34118dd..33c30bf 100644
--- a/runtime/src/iree/base/internal/math_test.cc
+++ b/runtime/src/iree/base/internal/math_test.cc
@@ -210,6 +210,17 @@
     std::function<UintType(float)> convert_f32_to_small,
     std::function<float(UintType)> convert_small_to_f32) {
   IREE_MATH_FP_FORMAT_CONSTANTS(small_, exp_bits, mantissa_bits, bias_tweak)
+  const float min_subnormal = convert_small_to_f32(1);
+  const float midpoint = min_subnormal / 2;
+  for (float value :
+       {std::nextafter(midpoint, 0.f), midpoint,
+        std::nextafter(midpoint, INFINITY), min_subnormal * 0.75f}) {
+    const UintType expected = value > midpoint ? 1 : 0;
+    EXPECT_EQ(expected, convert_f32_to_small(value)) << value;
+    EXPECT_EQ(expected || have_neg_zero ? expected | small_sign_mask : 0,
+              convert_f32_to_small(-value))
+        << -value;
+  }
   for (UintType m = 0; m <= small_mantissa_mask; ++m) {
     float value = std::ldexp(m, 1 - small_exp_bias - small_mantissa_bits);
     EXPECT_EQ(value, convert_small_to_f32(m));
@@ -826,6 +837,11 @@
   // Extra large finite values thanks to not having infinities.
   EXPECT_EQ(0x1F, iree_math_f32_to_f6e3m2fn(kF6E3M2FNMax));
   EXPECT_EQ(0x3F, iree_math_f32_to_f6e3m2fn(-kF6E3M2FNMax));
+  for (float value :
+       {std::nextafter(30.f, 0.f), 30.f, std::nextafter(30.f, INFINITY)}) {
+    EXPECT_EQ(0x1F, iree_math_f32_to_f6e3m2fn(value));
+    EXPECT_EQ(0x3F, iree_math_f32_to_f6e3m2fn(-value));
+  }
   // Min normal values.
   EXPECT_EQ(0x04, iree_math_f32_to_f6e3m2fn(kF6E3M2FNMin));
   EXPECT_EQ(0x24, iree_math_f32_to_f6e3m2fn(-kF6E3M2FNMin));
@@ -914,6 +930,11 @@
   // Extra large finite values thanks to not having infinities.
   EXPECT_EQ(0x1F, iree_math_f32_to_f6e2m3fn(kF6E2M3FNMax));
   EXPECT_EQ(0x3F, iree_math_f32_to_f6e2m3fn(-kF6E2M3FNMax));
+  for (float value :
+       {std::nextafter(7.75f, 0.f), 7.75f, std::nextafter(7.75f, INFINITY)}) {
+    EXPECT_EQ(0x1F, iree_math_f32_to_f6e2m3fn(value));
+    EXPECT_EQ(0x3F, iree_math_f32_to_f6e2m3fn(-value));
+  }
   // Min normal values.
   EXPECT_EQ(0x08, iree_math_f32_to_f6e2m3fn(kF6E2M3FNMin));
   EXPECT_EQ(0x28, iree_math_f32_to_f6e2m3fn(-kF6E2M3FNMin));
@@ -1002,6 +1023,11 @@
   // Extra large finite values thanks to not having infinities.
   EXPECT_EQ(0x07, iree_math_f32_to_f4e2m1fn(kF4E2M1FNMax));
   EXPECT_EQ(0x0F, iree_math_f32_to_f4e2m1fn(-kF4E2M1FNMax));
+  for (float value :
+       {std::nextafter(7.f, 0.f), 7.f, std::nextafter(7.f, INFINITY)}) {
+    EXPECT_EQ(0x07, iree_math_f32_to_f4e2m1fn(value));
+    EXPECT_EQ(0x0F, iree_math_f32_to_f4e2m1fn(-value));
+  }
   // Min normal values.
   EXPECT_EQ(0x02, iree_math_f32_to_f4e2m1fn(kF4E2M1FNMin));
   EXPECT_EQ(0x0A, iree_math_f32_to_f4e2m1fn(-kF4E2M1FNMin));