Skip to content

libm: fix truncl when long double has same size as double#18480

Draft
Cynerd wants to merge 1 commit intoapache:masterfrom
Cynerd:truncl-arm
Draft

libm: fix truncl when long double has same size as double#18480
Cynerd wants to merge 1 commit intoapache:masterfrom
Cynerd:truncl-arm

Conversation

@Cynerd
Copy link
Contributor

@Cynerd Cynerd commented Mar 2, 2026

Summary

The truncl implementation expects 80 bit little endian representation of the floating point number. That is valid for x86 but not for example arm. On arm long double is the same as double.

This was discovered with newer version of GCC (15.2.0) that started emitting warning:

  libm/lib_truncl.c: In function 'truncl':
  libm/lib_truncl.c:68:14: error: 'u.i.se' is used uninitialized [-Werror=uninitialized]
     68 |   int e = u.i.se & 0x7fff;
        |           ~~~^~~
  libm/lib_truncl.c:63:17: note: 'u' declared here
     63 |   union ldshape u =
        |                 ^
  libm/lib_truncl.c:69:14: error: 'u.i.se' is used uninitialized [-Werror=uninitialized]
     69 |   int s = u.i.se >> 15;
        |           ~~~^~~
  libm/lib_truncl.c:63:17: note: 'u' declared here
     63 |   union ldshape u =
        |                 ^
  libm/lib_truncl.c:63:17: error: 'u.i.se' is used uninitialized [-Werror=uninitialized]

Impact

This is technically a fix for truncl on platforms that have long double of the same size as double, as the logic was correct only for 80 bit float.

Testing

The testing was performed only by compilation, as the implementation just redirects to the trunc instead. The warning disappeared with the changes made in this PR.

The truncl implementation expects 80 bit little endian representation of
the floating point number. That is valid for x86 but not for example
arm. On arm long double is the same as double.

This was discovered with newer version of GCC (15.2.0) that started
emitting warning:

  libm/lib_truncl.c: In function 'truncl':
  libm/lib_truncl.c:68:14: error: 'u.i.se' is used uninitialized [-Werror=uninitialized]
     68 |   int e = u.i.se & 0x7fff;
        |           ~~~^~~
  libm/lib_truncl.c:63:17: note: 'u' declared here
     63 |   union ldshape u =
        |                 ^
  libm/lib_truncl.c:69:14: error: 'u.i.se' is used uninitialized [-Werror=uninitialized]
     69 |   int s = u.i.se >> 15;
        |           ~~~^~~
  libm/lib_truncl.c:63:17: note: 'u' declared here
     63 |   union ldshape u =
        |                 ^
  libm/lib_truncl.c:63:17: error: 'u.i.se' is used uninitialized [-Werror=uninitialized]

Signed-off-by: Karel Kočí <cynerd@email.cz>
@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 Mar 2, 2026
@Cynerd Cynerd marked this pull request as draft March 2, 2026 13:36
@Cynerd
Copy link
Contributor Author

Cynerd commented Mar 2, 2026

I just tested it with x86 and the condition is wrong because https://github.com/apache/nuttx/blob/master/include/nuttx/lib/float.h#L67 ... ups

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.

1 participant