casaroli opened a new pull request, #21568:
URL: https://github.com/apache/nuttx/pull/21568
## Summary
`NAN`, `NAN_F` and `NAN_L` in `include/nuttx/lib/math.h` were `INFINITY *
0.0F`. GCC does not fold this multiplication (it raises an invalid operation
exception), so it runs at run time and gives the default NaN of the FPU. On x86
(SSE) that NaN has the sign bit set; on arm64 and RISC-V it is clear. Since
#21505, `printf()` prints the sign of a NaN, as C allows, so `printf("%f",
NAN)` prints `-nan` on x86_64 and `nan` on the other architectures.
This PR adds `CONFIG_HAVE_BUILTIN_NAN` to the built-in list of
`include/nuttx/compiler.h`, next to `CONFIG_HAVE_BUILTIN_BSWAP16` and the
others. With it, `NAN`, `NAN_F` and `NAN_L` are `__builtin_nan("")`,
`__builtin_nanf("")` and `__builtin_nanl("")`: a quiet NaN with the sign bit
clear on every architecture, and a constant expression. The types do not change
(`double`, `float`, `long double`).
## Impact
- Compilers: GCC 4 and later and Clang (which defines `__GNUC__` 4) get the
built-ins. Green Hills (which defines `__GNUC__` but is kept out of that list),
SDCC, ZDS-II, IAR, MSVC and TASKING keep the old expression.
- Only the sign bit of the NaN changes, and only where the FPU default NaN
is negative (x86, x86_64). This includes what `strtod("nan")` and the libm
functions that return `NAN`, `NAN_F` or `NAN_L` give.
- No ABI change, no Kconfig change. No Documentation page describes these
macros, so there is no documentation change.
## Testing
A test program, `nanpos.c`, prints `NAN`, `NAN_F`, `NAN_L`, `nan("")` and
`strtod("nan")` with `%f`/`%Lf` and their `signbit()`, and `-NAN`.
| Configuration | master | this PR |
|---|---|---|
| `qemu-intel64:nsh` (flat, x86_64, x86_64-elf-gcc 16.2, QEMU) | `NAN`,
`NAN_F`, `NAN_L` and `strtod("nan")` print `-nan`; `-NAN` prints `nan` | all
print `nan` with the sign bit clear; `-NAN` prints `-nan` |
| `qemu-armv8a:knsh` (kernel, arm64, QEMU) | all print `nan`; `-NAN` prints
`-nan` | the same |
On `qemu-intel64:nsh` the test image boots with `-kernel`
(`CONFIG_ARCH_MULTIBOOT1`) and uses the HPET timer without X2APIC, because QEMU
without KVM on this host does not boot the default timer setup. ostest on
`qemu-armv8a:knsh` and `qemu-armv8a:nsh` (flat): exit status 0 on master and
with this PR.
Build with the NuttX libm (`CONFIG_LIBM`) and `EXTRAFLAGS="-Wno-cpp
-Werror"`: `qemu-armv8a:nsh`, `rv-virt:nsh64` and `qemu-intel64:nsh` build with
no compiler warning. On `qemu-intel64:nsh` GCC 16 gives `declaration of 'sinpi'
shadows a built-in function` in `lib_gamma.c` also on master, so that build
uses `-Wno-error=shadow`.
`tools/checkpatch.sh -c -u -m -g` passes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]