anatol opened a new pull request, #20553:
URL: https://github.com/apache/nuttx/pull/20553

   The public math header declares fmaf(), but the NuttX libm does not provide 
it. An unoptimized K230 Sigmoid build therefore fails to link when GCC leaves 
the fused operations as calls to the standard function.
   
   Add a portable, correctly rounded fmaf() adapted from musl (MIT). The 
license header and the pinned upstream revision are kept. It adds in binary64, 
where the binary32 product is exact, and rounds that sum to odd if it is a 
binary32 halfway or subnormal value, so that the second rounding to binary32 
neither picks the wrong neighbour nor misses the underflow and inexact flags. 
It needs a binary64 double: it is not built where DBL_MANT_DIG is not 53, 
instead of rounding wrongly.
   
   On RISC-V targets with an FPU (new LIBM_ARCH_FMAF, set by ARCH_FPU) use the 
fmadd.s instruction instead, in both the Make and CMake builds.
   
   Add tools/test_fmaf.c, a host regression that compares the portable fmaf() 
with the host libm: result bits and the invalid, overflow, underflow and 
inexact flags, in all four rounding modes. Besides boundary, tie and random 
inputs it has the double rounding traps, whose exact sum is just below or above 
a binary32 halfway value but rounds to it in binary64. Random inputs hit those 
with a probability near 2^-29.
   
   Testing
   
   Host, Linux x86_64 with glibc as the reference: tools/test_fmaf.c built with 
gcc 16.2.1 (-O0, -O2, -O3) and clang 23.1.1 (-O0, -O3), and with both at -O2 
with ASan and UBSan. All 4,072,672 cases match in the four rounding modes 
(boundary, tie and double rounding trap cases, and 1,000,000 random cases per 
mode). It fails for each of four broken fmaf() variants: the plain 
(float)((double)x * y + z), no correction at all, no halfway correction, and no 
subnormal correction.
   
   RISC-V, bare metal on qemu-system-riscv64 11.1.2 (virt, rv64imafdc, lp64d), 
with a throwaway harness that is not part of this commit: the fmadd.s version 
and the portable version return the same bits and exception flags in 1,072,672 
cases, in the four rounding modes. The comparison also fails with the portable 
version without the halfway correction.
   
   Build, Make with riscv64-elf-gcc 16.2.0 and arm-none-eabi-gcc 16.2.0, the 
libm.a of canmv230:nsh (K230), rv-virt:nsh64, rv-virt:nsh and bl602evb:elf (F 
extension only): fmaf is lib_fmaf_riscv.o, one fmadd.s and no undefined fmaf in 
the archive, and the same at -O0. For rv-virt:nsh64 and rv-virt:nsh with 
ARCH_FPU disabled, esp32c3-legacy-devkit:elf and linum-stm32h753bi:buzzer, fmaf 
is lib_fmaf.o and there are no warnings. nxstyle passes on the new C files.
   
   Assisted-by: Claude Code:claude-sonnet-5-5
   
   *Note: Please adhere to [Contributing 
Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).*
   
   ## Summary
   
   *Update this section with information on why change is necessary,
    what it exactly does and how, if new feature shows up, provide
    references (dependencies, similar problems and solutions), etc.*
   
   ## Impact
   
   *Update this section, where applicable, on how change affects users,
    build process, hardware, documentation, security, compatibility, etc.*
   
   ## Testing
   
   *This section should provide a detailed description of what you did
   to verify your changes work and do not break existing code.*
   
   *Please provide information about your host machine, the board(s) you
   tested your changes on, and how you tested. Logs should be included.*
   
   *For example, when changing something in the core OS functions, you
   may want to run the OSTest application to verify that there are no
   regressions. Changes to ADC code may warrant running the `adc`
   example. Adding a new uORB driver may require that you run
   `uorb_listener` to verify correct operation.*
   
   *Pure documentation changes can just be tested with `make html`
   (see docs) and verification of the correct format in your
   browser.*
   
   **_PRs without testing information will not be accepted. We will
   request test logs._**
   


-- 
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]

Reply via email to