[clang] bcb7c38 - [C23] Add INFINITY and NAN macros to <float.h> (#96659)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 3 04:23:47 PDT 2024
Author: Aaron Ballman
Date: 2024-07-03T07:23:44-04:00
New Revision: bcb7c38af7de59f3b2201734ee11987839cd7bbe
URL: https://github.com/llvm/llvm-project/commit/bcb7c38af7de59f3b2201734ee11987839cd7bbe
DIFF: https://github.com/llvm/llvm-project/commit/bcb7c38af7de59f3b2201734ee11987839cd7bbe.diff
LOG: [C23] Add INFINITY and NAN macros to <float.h> (#96659)
This is in support of WG14 N2848 which only define the macros if
an infinity or nan are supported. However, because we support builtins
that can produce an infinity or a NAN, and because we have pragmas
that control infinity or NAN behavior, we always define the macros even
if the user passed -ffinite-math-only on the command line.
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/Headers/float.h
clang/test/Headers/float.c
clang/www/c_status.html
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 1537eaaba0c66..f40fd1cd145bb 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -338,6 +338,11 @@ C23 Feature Support
- Properly promote bit-fields of bit-precise integer types to the field's type
rather than to ``int``. #GH87641
+- Added the ``INFINITY`` and ``NAN`` macros to Clang's ``<float.h>``
+ freestanding implementation; these macros were defined in ``<math.h>`` in C99
+ but C23 added them to ``<float.h>`` in
+ `WG14 N2848 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2848.pdf>`_.
+
Non-comprehensive list of changes in this release
-------------------------------------------------
diff --git a/clang/lib/Headers/float.h b/clang/lib/Headers/float.h
index 642c8f06cc938..a565a33243df1 100644
--- a/clang/lib/Headers/float.h
+++ b/clang/lib/Headers/float.h
@@ -88,6 +88,12 @@
# endif
#endif
+#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || \
+ !defined(__STRICT_ANSI__)
+# undef INFINITY
+# undef NAN
+#endif
+
/* Characteristics of floating point types, C99 5.2.4.2.2 */
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
@@ -155,6 +161,13 @@
# define LDBL_HAS_SUBNORM __LDBL_HAS_DENORM__
#endif
+#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || \
+ !defined(__STRICT_ANSI__)
+ /* C23 5.2.5.3.3p29-30 */
+# define INFINITY (__builtin_inf())
+# define NAN (__builtin_nan(""))
+#endif
+
#ifdef __STDC_WANT_IEC_60559_TYPES_EXT__
# define FLT16_MANT_DIG __FLT16_MANT_DIG__
# define FLT16_DECIMAL_DIG __FLT16_DECIMAL_DIG__
diff --git a/clang/test/Headers/float.c b/clang/test/Headers/float.c
index 70c11b0537537..b9e6e971545e5 100644
--- a/clang/test/Headers/float.c
+++ b/clang/test/Headers/float.c
@@ -1,9 +1,13 @@
// RUN: %clang_cc1 -fsyntax-only -verify -std=c89 -ffreestanding %s
// RUN: %clang_cc1 -fsyntax-only -verify -std=c99 -ffreestanding %s
// RUN: %clang_cc1 -fsyntax-only -verify -std=c11 -ffreestanding %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c23 -ffreestanding %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c23 -ffreestanding -ffinite-math-only %s
// RUN: %clang_cc1 -fsyntax-only -verify -xc++ -std=c++11 -ffreestanding %s
// RUN: %clang_cc1 -fsyntax-only -verify -xc++ -std=c++14 -ffreestanding %s
// RUN: %clang_cc1 -fsyntax-only -verify -xc++ -std=c++17 -ffreestanding %s
+// RUN: %clang_cc1 -fsyntax-only -verify -xc++ -std=c++23 -ffreestanding %s
+// RUN: %clang_cc1 -fsyntax-only -verify -xc++ -std=c++23 -ffreestanding -ffinite-math-only %s
// expected-no-diagnostics
/* Basic floating point conformance checks against:
@@ -207,6 +211,21 @@
#error "Mandatory macros {FLT,DBL,LDBL}_MAX_10_EXP are invalid."
#endif
+#if __STDC_VERSION__ >= 202311L || !defined(__STRICT_ANSI__)
+ #ifndef INFINITY
+ #error "Mandatory macro INFINITY is missing."
+ #endif
+ #ifndef NAN
+ #error "Mandatory macro NAN is missing."
+ #endif
+#else
+ #ifdef INFINITY
+ #error "Macro INFINITY should not be defined."
+ #endif
+ #ifdef NAN
+ #error "Macro NAN should not be defined."
+ #endif
+#endif
/* Internal consistency checks */
_Static_assert(FLT_RADIX == __FLT_RADIX__, "");
@@ -244,3 +263,9 @@ _Static_assert(LDBL_MAX_EXP == __LDBL_MAX_EXP__, "");
_Static_assert(FLT_MAX_10_EXP == __FLT_MAX_10_EXP__, "");
_Static_assert(DBL_MAX_10_EXP == __DBL_MAX_10_EXP__, "");
_Static_assert(LDBL_MAX_10_EXP == __LDBL_MAX_10_EXP__, "");
+
+#if (__STDC_VERSION__ >= 202311L || !defined(__STRICT_ANSI__)) && __FINITE_MATH_ONLY__ == 0
+// Ensure INFINITY and NAN are suitable for use in a constant expression.
+float f1 = INFINITY;
+float f2 = NAN;
+#endif
diff --git a/clang/www/c_status.html b/clang/www/c_status.html
index ccb39a15b25aa..3fb1efc1989e8 100644
--- a/clang/www/c_status.html
+++ b/clang/www/c_status.html
@@ -995,7 +995,7 @@ <h2 id="c2x">C23 implementation status</h2>
<tr>
<td>Contradiction about INFINITY macro</td>
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2848.pdf">N2848</a></td>
- <td class="unknown" align="center">Unknown</td>
+ <td class="unreleased" align="center">Clang 19</td>
</tr>
<tr>
<td>Require exact-width integer type interfaces</td>
More information about the cfe-commits
mailing list