This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 06c8c8ac256 include/nuttx/lib/math.h: Give NAN with the sign bit clear.
06c8c8ac256 is described below

commit 06c8c8ac25658baa98a532e9020339a54eaf27ae
Author: Marco Casaroli <[email protected]>
AuthorDate: Sun Nov 8 14:52:48 2026 +0100

    include/nuttx/lib/math.h: Give NAN with the sign bit clear.
    
    NAN, NAN_F and NAN_L were INFINITY * 0.0F.  Where NAN is used in code,
    GCC does not fold it (the multiplication raises a floating point
    exception), so it runs at run time and gives the default NaN of the FPU:
    on x86 that NaN has the sign bit set; on arm64 and RISC-V it is clear.
    C does not specify the sign of NAN, but other C libraries give a
    positive one, and programs and tests compare the text: a printf() that
    prints the sign of a NaN, as C allows, prints "-nan" for NAN on x86 and
    "nan" elsewhere.
    
    Add CONFIG_HAVE_BUILTIN_NAN to the built-in list of compiler.h (GCC 4
    and later, and Clang; not Green Hills), and with it use
    __builtin_nan(""), __builtin_nanf("") and __builtin_nanl(""): a quiet
    NaN with the sign bit clear on every architecture, also a constant
    expression.  The types do not change.  Other compilers keep the old
    expression.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/nuttx/compiler.h |  1 +
 include/nuttx/lib/math.h | 17 ++++++++++++++---
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/include/nuttx/compiler.h b/include/nuttx/compiler.h
index ee8e9e87206..4ae07992a4b 100644
--- a/include/nuttx/compiler.h
+++ b/include/nuttx/compiler.h
@@ -199,6 +199,7 @@
 #    define CONFIG_HAVE_BUILTIN_FFS 1
 #    define CONFIG_HAVE_BUILTIN_FFSL 1
 #    define CONFIG_HAVE_BUILTIN_FFSLL 1
+#    define CONFIG_HAVE_BUILTIN_NAN 1
 #  endif
 
   /* An __asm__ label carries __USER_LABEL_PREFIX__ ("_" on RH850 and RX) */
diff --git a/include/nuttx/lib/math.h b/include/nuttx/lib/math.h
index ad4967987be..e62c7fe949a 100644
--- a/include/nuttx/lib/math.h
+++ b/include/nuttx/lib/math.h
@@ -75,17 +75,28 @@
 #endif
 
 #define INFINITY   ((double)(_HUGE_ENUF * _HUGE_ENUF))
-#define NAN        ((double)(INFINITY * 0.0F))
 #define HUGE_VAL   INFINITY
 
 #define INFINITY_F ((float)INFINITY)
-#define NAN_F      ((float)(INFINITY * 0.0F))
 #define HUGE_VALF  ((float)INFINITY)
 
 #define INFINITY_L ((long double)INFINITY)
-#define NAN_L      ((long double)(INFINITY * 0.0F))
 #define HUGE_VALL  ((long double)INFINITY)
 
+/* A quiet NaN with the sign bit clear.  INFINITY * 0 gives the default NaN
+ * of the FPU, and on x86 that NaN has the sign bit set.
+ */
+
+#ifdef CONFIG_HAVE_BUILTIN_NAN
+#  define NAN      __builtin_nan("")
+#  define NAN_F    __builtin_nanf("")
+#  define NAN_L    __builtin_nanl("")
+#else
+#  define NAN      ((double)(INFINITY * 0.0F))
+#  define NAN_F    ((float)(INFINITY * 0.0F))
+#  define NAN_L    ((long double)(INFINITY * 0.0F))
+#endif
+
 #define isnan(x)   ((x) != (x))
 #define isnanf(x)  ((x) != (x))
 #define isnanl(x)  ((x) != (x))

Reply via email to