Skip to content

Commit 0e3fdc2

Browse files
authored
[C23] Support the SNAN macros in <float.h> (llvm#162858)
These macros were brought in as part of the TS 18661-1 integration in C23, and then renamed in WG14 N2710. Test coverage is being added to WG14 N3364 from C2y because that paper was fixing a bug with the way these macros are handled with unary + and - operators, so all the existing test coverage was there, just needed to use a header file instead of defining the macros manually. Fixes llvm#162830
1 parent 625aa09 commit 0e3fdc2

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ C2y Feature Support
187187

188188
C23 Feature Support
189189
^^^^^^^^^^^^^^^^^^^
190+
- Added ``FLT_SNAN``, ``DBL_SNAN``, and ``LDBL_SNAN`` to Clang's ``<float.h>``
191+
header in C23 and later modes. This implements
192+
`WG14 N2710 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2710.htm>`_.
190193

191194
Non-comprehensive list of changes in this release
192195
-------------------------------------------------

clang/lib/Headers/float.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@
8989
!defined(__STRICT_ANSI__)
9090
# undef INFINITY
9191
# undef NAN
92+
# undef FLT_SNAN
93+
# undef DBL_SNAN
94+
# undef LDBL_SNAN
9295
#endif
9396

9497
/* Characteristics of floating point types, C99 5.2.4.2.2 */
@@ -160,9 +163,15 @@
160163

161164
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || \
162165
!defined(__STRICT_ANSI__)
166+
/* C23 5.2.5.3.2p28 */
167+
# define FLT_SNAN (__builtin_nansf(""))
168+
# define DBL_SNAN (__builtin_nans(""))
169+
# define LDBL_SNAN (__builtin_nansl(""))
170+
163171
/* C23 5.2.5.3.3p29-30 */
164172
# define INFINITY (__builtin_inff())
165173
# define NAN (__builtin_nanf(""))
174+
166175
/* C23 5.2.5.3.3p32 */
167176
# define FLT_NORM_MAX __FLT_NORM_MAX__
168177
# define DBL_NORM_MAX __DBL_NORM_MAX__

clang/test/C/C2y/n3364.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
// RUN: %clang_cc1 -verify -std=c2y -Wall -pedantic -emit-llvm -o - %s
2-
// RUN: %clang_cc1 -verify -Wall -pedantic -emit-llvm -o - %s
1+
// RUN: %clang_cc1 -verify -std=c2y -ffreestanding -Wall -pedantic -emit-llvm -o - %s
2+
// RUN: %clang_cc1 -verify -ffreestanding -Wall -pedantic -emit-llvm -o - %s
33
// expected-no-diagnostics
44

55
/* WG14 N3364: Yes
66
* Give consistent wording for SNAN initialization v3
77
*
88
* Ensure that initializing from a signaling NAN (optionally with a unary + or
99
* -) at translation time behaves correctly at runtime.
10+
*
11+
* This also serves as a test for C23's WG14 N2710 which introduces these
12+
* macros into float.h in Clang 22.
1013
*/
1114

12-
#define FLT_SNAN __builtin_nansf("1")
13-
#define DBL_SNAN __builtin_nans("1")
14-
#define LD_SNAN __builtin_nansl("1")
15+
#if __STDC_VERSION__ >= 202311L
16+
#include <float.h>
17+
#else
18+
#define FLT_SNAN __builtin_nansf("")
19+
#define DBL_SNAN __builtin_nans("")
20+
#define LDBL_SNAN __builtin_nansl("")
21+
#endif
1522

1623
float f1 = FLT_SNAN;
1724
float f2 = +FLT_SNAN;
@@ -27,9 +34,9 @@ double d3 = -DBL_SNAN;
2734
// CHECK: @d2 = {{.*}}global double 0x7FF0000000000001
2835
// CHECK: @d3 = {{.*}}global double 0xFFF0000000000001
2936

30-
long double ld1 = LD_SNAN;
31-
long double ld2 = +LD_SNAN;
32-
long double ld3 = -LD_SNAN;
37+
long double ld1 = LDBL_SNAN;
38+
long double ld2 = +LDBL_SNAN;
39+
long double ld3 = -LDBL_SNAN;
3340
// CHECK: @ld1 = {{.*}}global {{double 0x7FF0000000000001|x86_fp80 0xK7FFF8000000000000001|fp128 0xL00000000000000017FFF000000000000}}
3441
// CHECK: @ld2 = {{.*}}global {{double 0x7FF0000000000001|x86_fp80 0xK7FFF8000000000000001|fp128 0xL00000000000000017FFF000000000000}}
3542
// CHECK: @ld3 = {{.*}}global {{double 0xFFF0000000000001|x86_fp80 0xKFFFF8000000000000001|fp128 0xL0000000000000001FFFF000000000000}}

0 commit comments

Comments
 (0)