Skip to content

libs/libm: support 128-bit quad-precision and universal fallback in truncl - #20682

Merged
acassis merged 1 commit into
apache:masterfrom
Swatantra-66:fix/libm-truncl-quad-precision
Oct 16, 2026
Merged

acassis merged 1 commit into
apache:masterfrom
Swatantra-66:fix/libm-truncl-quad-precision

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

Fixes: #18351

Resolves the unresolved PANIC(); /* FIX ME */ in libs/libm/libm/lib_truncl.c.

Previously, truncl() aborted the kernel with PANIC() on any architecture where long double is not identical to double and LDBL_MANT_DIG != 64 (e.g., IEEE-754 binary128 quad-precision on RISC-V 64, AArch64, or SPARC with LDBL_MANT_DIG == 113).

This PR:

  1. Adds IEEE-754 128-bit Quad Precision (LDBL_MANT_DIG == 113):
    • Extends the musl toint = 1 / LDBL_EPSILON branch to support 128-bit floats.
    • Provides endianness-aware union ldshape definitions (matching the pattern in libs/libm/libm/lib_scalbnl.c) for both little-endian and big-endian architectures.
  2. Universal Mathematical Fallback (#else):
    • Replaces PANIC() with an exact, IEEE-754 compliant mathematical fallback:
      if (isnan(x))
        {
          return x;
        }
      
      return copysignl((x >= 0.0) ? floorl(x) : ceill(x), x);
    • Correctly preserves signs, signed zeroes (-0.0 / +0.0), infinities, and NaNs without crashing the kernel.
  3. Cleans Up Includes:
    • Drops unused <assert.h> include.
    • Adds <endian.h> in alphabetical order.

Impact

  • Is new feature added?: No (completes truncl math library support).
  • Impact on user: Applications using truncl() on 128-bit long double platforms (e.g. RISC-V 64) will now execute properly instead of crashing with a kernel panic.
  • Impact on build: None.
  • Impact on hardware: None.
  • Impact on documentation: None.
  • Impact on security: Eliminates an unhandled kernel panic vector in libm.

Testing

  • Verified 0 nxstyle / style errors (line length <= 78 chars, 2-space indentation, no trailing whitespaces).
  • Numerical verification of IEEE-754 semantics across positive/negative values, fractional bounds (-1, 1), signed zeros ±0.0, infinities ±Inf, and NaN.

@github-actions github-actions Bot added Area: OS Components OS Components issues Size: S The size of the change in this PR is small labels Oct 15, 2026
…runcl

Previously, truncl() aborted the kernel with PANIC() on architectures where
long double is not equal to double and LDBL_MANT_DIG != 64 (such as IEEE-754
binary128 quad-precision on RISC-V 64, AArch64, or SPARC with LDBL_MANT_DIG == 113).

This commit:
1. Extends the musl bit-level branch to support LDBL_MANT_DIG == 113 (IEEE-754
   quad-precision binary128) with proper big- and little-endian ldshape layouts.
2. Replaces PANIC() in the #else block with an IEEE-754 compliant mathematical
   fallback using copysignl((x >= 0.0) ? floorl(x) : ceill(x), x), guaranteeing
   that truncl() never crashes on any architecture.
3. Removes the unused assert.h include and adds endian.h in alphabetical order.

Fixes: apache#18351
Signed-off-by: Swatantra Yadav <[email protected]>
@Swatantra-66
Swatantra-66 force-pushed the fix/libm-truncl-quad-precision branch from 2e94bc6 to b319514 Compare October 16, 2026 04:45
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

@acassis
acassis merged commit 3c9b631 into apache:master Oct 16, 2026
50 of 53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] truncl: invalid implementation on ARM for 64bits double

3 participants