From 65196bce3f680e599cf003078b34258b3617cdf6 Mon Sep 17 00:00:00 2001 From: Mike Kruskal <62662355+mkruskal-google@users.noreply.github.com> Date: Mon, 25 Nov 2024 12:11:29 -0800 Subject: [PATCH] Backport windows fixes with INFINITY/NAN (#19390) * Fixed `NAN`/`INFINITY` definitions to work on Windows 11 SDK. * Regenerate stale files --- php/ext/google/protobuf/php-upb.c | 32 ++++++++++++++++++++++++--- ruby/ext/google/protobuf_c/ruby-upb.c | 32 ++++++++++++++++++++++++--- upb/message/message.c | 32 ++++++++++++++++++++++++--- 3 files changed, 87 insertions(+), 9 deletions(-) diff --git a/php/ext/google/protobuf/php-upb.c b/php/ext/google/protobuf/php-upb.c index 0c4bbfb484d6..ed2c11c5bd27 100644 --- a/php/ext/google/protobuf/php-upb.c +++ b/php/ext/google/protobuf/php-upb.c @@ -6256,9 +6256,35 @@ bool _upb_mapsorter_pushexts(_upb_mapsorter* s, // Must be last. -const float kUpb_FltInfinity = INFINITY; -const double kUpb_Infinity = INFINITY; -const double kUpb_NaN = NAN; +// The latest win32 SDKs have an invalid definition of NAN. +// https://developercommunity.visualstudio.com/t/NAN-is-no-longer-compile-time-constant-i/10688907 +// +// Unfortunately, the `0.0 / 0.0` workaround doesn't work in Clang under C23, so +// try __builtin_nan first, if that exists. +#ifdef _WIN32 +#ifdef __has_builtin +#if __has_builtin(__builtin_nan) +#define UPB_NAN __builtin_nan("0") +#endif +#if __has_builtin(__builtin_inf) +#define UPB_INFINITY __builtin_inf() +#endif +#endif +#ifndef UPB_NAN +#define UPB_NAN 0.0 / 0.0 +#endif +#ifndef UPB_INFINITY +#define UPB_INFINITY 1.0 / 0.0 +#endif +#else +// For !_WIN32, assume math.h works. +#define UPB_NAN NAN +#define UPB_INFINITY INFINITY +#endif + +const float kUpb_FltInfinity = UPB_INFINITY; +const double kUpb_Infinity = UPB_INFINITY; +const double kUpb_NaN = UPB_NAN; static const size_t overhead = sizeof(upb_Message_InternalData); diff --git a/ruby/ext/google/protobuf_c/ruby-upb.c b/ruby/ext/google/protobuf_c/ruby-upb.c index 902c9c71a61a..b399b7066a3f 100644 --- a/ruby/ext/google/protobuf_c/ruby-upb.c +++ b/ruby/ext/google/protobuf_c/ruby-upb.c @@ -5771,9 +5771,35 @@ bool _upb_mapsorter_pushexts(_upb_mapsorter* s, // Must be last. -const float kUpb_FltInfinity = INFINITY; -const double kUpb_Infinity = INFINITY; -const double kUpb_NaN = NAN; +// The latest win32 SDKs have an invalid definition of NAN. +// https://developercommunity.visualstudio.com/t/NAN-is-no-longer-compile-time-constant-i/10688907 +// +// Unfortunately, the `0.0 / 0.0` workaround doesn't work in Clang under C23, so +// try __builtin_nan first, if that exists. +#ifdef _WIN32 +#ifdef __has_builtin +#if __has_builtin(__builtin_nan) +#define UPB_NAN __builtin_nan("0") +#endif +#if __has_builtin(__builtin_inf) +#define UPB_INFINITY __builtin_inf() +#endif +#endif +#ifndef UPB_NAN +#define UPB_NAN 0.0 / 0.0 +#endif +#ifndef UPB_INFINITY +#define UPB_INFINITY 1.0 / 0.0 +#endif +#else +// For !_WIN32, assume math.h works. +#define UPB_NAN NAN +#define UPB_INFINITY INFINITY +#endif + +const float kUpb_FltInfinity = UPB_INFINITY; +const double kUpb_Infinity = UPB_INFINITY; +const double kUpb_NaN = UPB_NAN; static const size_t overhead = sizeof(upb_Message_InternalData); diff --git a/upb/message/message.c b/upb/message/message.c index 265d30df13c5..d1a84125ba56 100644 --- a/upb/message/message.c +++ b/upb/message/message.c @@ -15,9 +15,35 @@ // Must be last. #include "upb/port/def.inc" -const float kUpb_FltInfinity = INFINITY; -const double kUpb_Infinity = INFINITY; -const double kUpb_NaN = NAN; +// The latest win32 SDKs have an invalid definition of NAN. +// https://developercommunity.visualstudio.com/t/NAN-is-no-longer-compile-time-constant-i/10688907 +// +// Unfortunately, the `0.0 / 0.0` workaround doesn't work in Clang under C23, so +// try __builtin_nan first, if that exists. +#ifdef _WIN32 +#ifdef __has_builtin +#if __has_builtin(__builtin_nan) +#define UPB_NAN __builtin_nan("0") +#endif +#if __has_builtin(__builtin_inf) +#define UPB_INFINITY __builtin_inf() +#endif +#endif +#ifndef UPB_NAN +#define UPB_NAN 0.0 / 0.0 +#endif +#ifndef UPB_INFINITY +#define UPB_INFINITY 1.0 / 0.0 +#endif +#else +// For !_WIN32, assume math.h works. +#define UPB_NAN NAN +#define UPB_INFINITY INFINITY +#endif + +const float kUpb_FltInfinity = UPB_INFINITY; +const double kUpb_Infinity = UPB_INFINITY; +const double kUpb_NaN = UPB_NAN; static const size_t overhead = sizeof(upb_Message_InternalData);