From 56bde37add6f54339bd916d729b5d690f0b5f5a5 Mon Sep 17 00:00:00 2001 From: lit Date: Thu, 13 Mar 2025 00:30:08 +0800 Subject: [PATCH] fixes #24772: system.NaN was negative when C (#24774) fixes #24772 The old implementation was said to copied from Windows SDK, but you can find the newer SDK's definition is updated and the sign is reversed compared to the old. Also, `__builtin_nanf("")` is used if available, which is more efficient than previous (In x86_64 gcc, latter produces 32B code but former just 8B). (cherry picked from commit 4f326246413a8894703b9af9f9bfd26ddcd0883d) --- lib/nimbase.h | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/nimbase.h b/lib/nimbase.h index 0e037cd311..ebc919b0b3 100644 --- a/lib/nimbase.h +++ b/lib/nimbase.h @@ -491,13 +491,22 @@ typedef char* NCSTRING; #define paramCount() cmdCount -// NAN definition copied from math.h included in the Windows SDK version 10.0.14393.0 -#ifndef NAN +#ifndef NAN /* use __builtin_nanf which is faster, if available */ +# if defined(__GNUC__) +# define NAN (__builtin_nanf("")) +# elif defined(__clang__) /* XXX: writing __has_builtin this line cause MSVC complains. */ +# if __has_builtin (__builtin_nanf) +# define NAN (__builtin_nanf("")) +# endif +# endif +#endif + +#ifndef NAN /* modified from math.h included in the Windows SDK version 10.0.26100.0 */ # ifndef _HUGE_ENUF -# define _HUGE_ENUF 1e+300 // _HUGE_ENUF*_HUGE_ENUF must overflow +# define _HUGE_ENUF 1e+300 /* _HUGE_ENUF*_HUGE_ENUF must overflow */ # endif # define NAN_INFINITY ((float)(_HUGE_ENUF * _HUGE_ENUF)) -# define NAN ((float)(NAN_INFINITY * 0.0F)) +# define NAN (-(float)(NAN_INFINITY * 0.0F)) #endif #ifndef INF