Skip to content

Commit bcb7c38

Browse files
authored
[C23] Add INFINITY and NAN macros to <float.h> (llvm#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.
1 parent 7de7f50 commit bcb7c38

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,11 @@ C23 Feature Support
338338
- Properly promote bit-fields of bit-precise integer types to the field's type
339339
rather than to ``int``. #GH87641
340340

341+
- Added the ``INFINITY`` and ``NAN`` macros to Clang's ``<float.h>``
342+
freestanding implementation; these macros were defined in ``<math.h>`` in C99
343+
but C23 added them to ``<float.h>`` in
344+
`WG14 N2848 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2848.pdf>`_.
345+
341346
Non-comprehensive list of changes in this release
342347
-------------------------------------------------
343348

clang/lib/Headers/float.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@
8888
# endif
8989
#endif
9090

91+
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || \
92+
!defined(__STRICT_ANSI__)
93+
# undef INFINITY
94+
# undef NAN
95+
#endif
96+
9197
/* Characteristics of floating point types, C99 5.2.4.2.2 */
9298

9399
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
@@ -155,6 +161,13 @@
155161
# define LDBL_HAS_SUBNORM __LDBL_HAS_DENORM__
156162
#endif
157163

164+
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || \
165+
!defined(__STRICT_ANSI__)
166+
/* C23 5.2.5.3.3p29-30 */
167+
# define INFINITY (__builtin_inf())
168+
# define NAN (__builtin_nan(""))
169+
#endif
170+
158171
#ifdef __STDC_WANT_IEC_60559_TYPES_EXT__
159172
# define FLT16_MANT_DIG __FLT16_MANT_DIG__
160173
# define FLT16_DECIMAL_DIG __FLT16_DECIMAL_DIG__

clang/test/Headers/float.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
// RUN: %clang_cc1 -fsyntax-only -verify -std=c89 -ffreestanding %s
22
// RUN: %clang_cc1 -fsyntax-only -verify -std=c99 -ffreestanding %s
33
// RUN: %clang_cc1 -fsyntax-only -verify -std=c11 -ffreestanding %s
4+
// RUN: %clang_cc1 -fsyntax-only -verify -std=c23 -ffreestanding %s
5+
// RUN: %clang_cc1 -fsyntax-only -verify -std=c23 -ffreestanding -ffinite-math-only %s
46
// RUN: %clang_cc1 -fsyntax-only -verify -xc++ -std=c++11 -ffreestanding %s
57
// RUN: %clang_cc1 -fsyntax-only -verify -xc++ -std=c++14 -ffreestanding %s
68
// RUN: %clang_cc1 -fsyntax-only -verify -xc++ -std=c++17 -ffreestanding %s
9+
// RUN: %clang_cc1 -fsyntax-only -verify -xc++ -std=c++23 -ffreestanding %s
10+
// RUN: %clang_cc1 -fsyntax-only -verify -xc++ -std=c++23 -ffreestanding -ffinite-math-only %s
711
// expected-no-diagnostics
812

913
/* Basic floating point conformance checks against:
@@ -207,6 +211,21 @@
207211
#error "Mandatory macros {FLT,DBL,LDBL}_MAX_10_EXP are invalid."
208212
#endif
209213

214+
#if __STDC_VERSION__ >= 202311L || !defined(__STRICT_ANSI__)
215+
#ifndef INFINITY
216+
#error "Mandatory macro INFINITY is missing."
217+
#endif
218+
#ifndef NAN
219+
#error "Mandatory macro NAN is missing."
220+
#endif
221+
#else
222+
#ifdef INFINITY
223+
#error "Macro INFINITY should not be defined."
224+
#endif
225+
#ifdef NAN
226+
#error "Macro NAN should not be defined."
227+
#endif
228+
#endif
210229

211230
/* Internal consistency checks */
212231
_Static_assert(FLT_RADIX == __FLT_RADIX__, "");
@@ -244,3 +263,9 @@ _Static_assert(LDBL_MAX_EXP == __LDBL_MAX_EXP__, "");
244263
_Static_assert(FLT_MAX_10_EXP == __FLT_MAX_10_EXP__, "");
245264
_Static_assert(DBL_MAX_10_EXP == __DBL_MAX_10_EXP__, "");
246265
_Static_assert(LDBL_MAX_10_EXP == __LDBL_MAX_10_EXP__, "");
266+
267+
#if (__STDC_VERSION__ >= 202311L || !defined(__STRICT_ANSI__)) && __FINITE_MATH_ONLY__ == 0
268+
// Ensure INFINITY and NAN are suitable for use in a constant expression.
269+
float f1 = INFINITY;
270+
float f2 = NAN;
271+
#endif

clang/www/c_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ <h2 id="c2x">C23 implementation status</h2>
995995
<tr>
996996
<td>Contradiction about INFINITY macro</td>
997997
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2848.pdf">N2848</a></td>
998-
<td class="unknown" align="center">Unknown</td>
998+
<td class="unreleased" align="center">Clang 19</td>
999999
</tr>
10001000
<tr>
10011001
<td>Require exact-width integer type interfaces</td>

0 commit comments

Comments
 (0)