[clang] 0e7590a - [C23] Add *_NORM_MAX macros to <float.h> (#96643)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 10 06:49:25 PDT 2024
Author: Aaron Ballman
Date: 2024-07-10T09:49:21-04:00
New Revision: 0e7590a25cd2e49c5849cc7d2facd5810f8900ce
URL: https://github.com/llvm/llvm-project/commit/0e7590a25cd2e49c5849cc7d2facd5810f8900ce
DIFF: https://github.com/llvm/llvm-project/commit/0e7590a25cd2e49c5849cc7d2facd5810f8900ce.diff
LOG: [C23] Add *_NORM_MAX macros to <float.h> (#96643)
This adds the *_NORM_MAX macros to <float.h> for freestanding mode in
Clang; the values were chosen based on the behavior seen coming from GCC
and the values already produced for the *_MAX macros by Clang.
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/Frontend/InitPreprocessor.cpp
clang/lib/Headers/float.h
clang/test/Headers/float.c
clang/test/Preprocessor/init-aarch64.c
clang/test/Preprocessor/init.c
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ebaa8773a262f..1a0023580147e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -355,6 +355,9 @@ C23 Feature Support
- Clang now supports `N3017 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3017.htm>`_
``#embed`` - a scannable, tooling-friendly binary resource inclusion mechanism.
+- Added the ``FLT_NORM_MAX``, ``DBL_NORM_MAX``, and ``LDBL_NORM_MAX`` to the
+ freestanding implementation of ``<float.h>`` that ships with Clang.
+
Non-comprehensive list of changes in this release
-------------------------------------------------
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index 5e52555c6fee9..d40d78a38540b 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -113,7 +113,11 @@ static T PickFP(const llvm::fltSemantics *Sem, T IEEEHalfVal, T IEEESingleVal,
static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix,
const llvm::fltSemantics *Sem, StringRef Ext) {
- const char *DenormMin, *Epsilon, *Max, *Min;
+ const char *DenormMin, *NormMax, *Epsilon, *Max, *Min;
+ NormMax = PickFP(Sem, "6.5504e+4", "3.40282347e+38",
+ "1.7976931348623157e+308", "1.18973149535723176502e+4932",
+ "8.98846567431157953864652595394501e+307",
+ "1.18973149535723176508575932662800702e+4932");
DenormMin = PickFP(Sem, "5.9604644775390625e-8", "1.40129846e-45",
"4.9406564584124654e-324", "3.64519953188247460253e-4951",
"4.94065645841246544176568792868221e-324",
@@ -144,6 +148,7 @@ static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix,
DefPrefix += "_";
Builder.defineMacro(DefPrefix + "DENORM_MIN__", Twine(DenormMin)+Ext);
+ Builder.defineMacro(DefPrefix + "NORM_MAX__", Twine(NormMax)+Ext);
Builder.defineMacro(DefPrefix + "HAS_DENORM__");
Builder.defineMacro(DefPrefix + "DIG__", Twine(Digits));
Builder.defineMacro(DefPrefix + "DECIMAL_DIG__", Twine(DecimalDigits));
diff --git a/clang/lib/Headers/float.h b/clang/lib/Headers/float.h
index 49d4212414d64..e5c439a9d47ae 100644
--- a/clang/lib/Headers/float.h
+++ b/clang/lib/Headers/float.h
@@ -86,6 +86,12 @@
# undef DBL_HAS_SUBNORM
# undef LDBL_HAS_SUBNORM
# endif
+#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || \
+ !defined(__STRICT_ANSI__)
+# undef FLT_NORM_MAX
+# undef DBL_NORM_MAX
+# undef LDBL_NORM_MAX
+#endif
#endif
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || \
@@ -166,6 +172,10 @@
/* C23 5.2.5.3.3p29-30 */
# define INFINITY (__builtin_inff())
# define NAN (__builtin_nanf(""))
+ /* C23 5.2.5.3.3p32 */
+# define FLT_NORM_MAX __FLT_NORM_MAX__
+# define DBL_NORM_MAX __DBL_NORM_MAX__
+# define LDBL_NORM_MAX __LDBL_NORM_MAX__
#endif
#ifdef __STDC_WANT_IEC_60559_TYPES_EXT__
diff --git a/clang/test/Headers/float.c b/clang/test/Headers/float.c
index 218ab58ba62ef..d524d0e53f3fd 100644
--- a/clang/test/Headers/float.c
+++ b/clang/test/Headers/float.c
@@ -14,11 +14,13 @@
// expected-no-diagnostics
/* Basic floating point conformance checks against:
+ - C23 Final Std.
- N1570 draft of C11 Std.
- N1256 draft of C99 Std.
- http://port70.net/~nsz/c/c89/c89-draft.html draft of C89/C90 Std.
*/
/*
+ C23, 5.2.5.3.3p21, pp. 25
C11, 5.2.4.2.2p11, pp. 30
C99, 5.2.4.2.2p9, pp. 25
C89, 2.2.4.2
@@ -225,6 +227,22 @@
_Static_assert(_Generic(INFINITY, float : 1, default : 0), ""); // finite-warning {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}
_Static_assert(_Generic(NAN, float : 1, default : 0), ""); // finite-warning {{use of NaN is undefined behavior due to the currently enabled floating-point options}} \
finite-warning {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}}
+
+#ifndef FLT_NORM_MAX
+ #error "Mandatory macro FLT_NORM_MAX is missing."
+#else
+ _Static_assert(FLT_NORM_MAX >= 1.0E+37F, "Mandatory macro FLT_NORM_MAX is invalid.");
+#endif
+#ifndef DBL_NORM_MAX
+ #error "Mandatory macro DBL_NORM_MAX is missing."
+#else
+ _Static_assert(DBL_NORM_MAX >= 1.0E+37, "Mandatory macro DBL_NORM_MAX is invalid.");
+#endif
+#ifndef LDBL_NORM_MAX
+ #error "Mandatory macro LDBL_NORM_MAX is missing."
+#else
+ _Static_assert(LDBL_NORM_MAX >= 1.0E+37L, "Mandatory macro LDBL_NORM_MAX is invalid.");
+#endif
#else
#ifdef INFINITY
#error "Macro INFINITY should not be defined."
@@ -271,8 +289,14 @@ _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
+#if __STDC_VERSION__ >= 202311L || !defined(__STRICT_ANSI__)
+_Static_assert(FLT_NORM_MAX == __FLT_NORM_MAX__, "");
+_Static_assert(DBL_NORM_MAX == __DBL_NORM_MAX__, "");
+_Static_assert(LDBL_NORM_MAX == __LDBL_NORM_MAX__, "");
+
+#if __FINITE_MATH_ONLY__ == 0
// Ensure INFINITY and NAN are suitable for use in a constant expression.
float f1 = INFINITY;
float f2 = NAN;
#endif
+#endif
diff --git a/clang/test/Preprocessor/init-aarch64.c b/clang/test/Preprocessor/init-aarch64.c
index 9e425ac1c5ce2..81c9a66f13751 100644
--- a/clang/test/Preprocessor/init-aarch64.c
+++ b/clang/test/Preprocessor/init-aarch64.c
@@ -74,6 +74,7 @@
// AARCH64-NEXT: #define __DBL_MIN_10_EXP__ (-307)
// AARCH64-NEXT: #define __DBL_MIN_EXP__ (-1021)
// AARCH64-NEXT: #define __DBL_MIN__ 2.2250738585072014e-308
+// AARCH64-NEXT: #define __DBL_NORM_MAX__ 1.7976931348623157e+308
// AARCH64-NEXT: #define __DECIMAL_DIG__ __LDBL_DECIMAL_DIG__
// AARCH64-NEXT: #define __ELF__ 1
// AARCH64-NEXT: #define __FINITE_MATH_ONLY__ 0
@@ -91,6 +92,7 @@
// AARCH64-NEXT: #define __FLT16_MIN_10_EXP__ (-4)
// AARCH64-NEXT: #define __FLT16_MIN_EXP__ (-13)
// AARCH64-NEXT: #define __FLT16_MIN__ 6.103515625e-5F16
+// AARCH64-NEXT: #define __FLT16_NORM_MAX__ 6.5504e+4F16
// AARCH64-NEXT: #define __FLT_DECIMAL_DIG__ 9
// AARCH64-NEXT: #define __FLT_DENORM_MIN__ 1.40129846e-45F
// AARCH64-NEXT: #define __FLT_DIG__ 6
@@ -105,6 +107,7 @@
// AARCH64-NEXT: #define __FLT_MIN_10_EXP__ (-37)
// AARCH64-NEXT: #define __FLT_MIN_EXP__ (-125)
// AARCH64-NEXT: #define __FLT_MIN__ 1.17549435e-38F
+// AARCH64-NEXT: #define __FLT_NORM_MAX__ 3.40282347e+38F
// AARCH64-NEXT: #define __FLT_RADIX__ 2
// AARCH64-NEXT: #define __FPCLASS_NEGINF 0x0004
// AARCH64-NEXT: #define __FPCLASS_NEGNORMAL 0x0008
@@ -216,6 +219,7 @@
// AARCH64-NEXT: #define __LDBL_MIN_10_EXP__ (-4931)
// AARCH64-NEXT: #define __LDBL_MIN_EXP__ (-16381)
// AARCH64-NEXT: #define __LDBL_MIN__ 3.36210314311209350626267781732175260e-4932L
+// AARCH64-NEXT: #define __LDBL_NORM_MAX__ 1.18973149535723176508575932662800702e+4932L
// AARCH64_LE-NEXT: #define __LITTLE_ENDIAN__ 1
// AARCH64-NEXT: #define __LLONG_WIDTH__ 64
// AARCH64-NEXT: #define __LONG_LONG_MAX__ 9223372036854775807LL
diff --git a/clang/test/Preprocessor/init.c b/clang/test/Preprocessor/init.c
index 12ebaeaedaffa..c177975114332 100644
--- a/clang/test/Preprocessor/init.c
+++ b/clang/test/Preprocessor/init.c
@@ -1648,6 +1648,7 @@
// WEBASSEMBLY-NEXT:#define __DBL_MIN_10_EXP__ (-307)
// WEBASSEMBLY-NEXT:#define __DBL_MIN_EXP__ (-1021)
// WEBASSEMBLY-NEXT:#define __DBL_MIN__ 2.2250738585072014e-308
+// WEBASSEMBLY-NEXT:#define __DBL_NORM_MAX__ 1.7976931348623157e+308
// WEBASSEMBLY-NEXT:#define __DECIMAL_DIG__ __LDBL_DECIMAL_DIG__
// WEBASSEMBLY-NOT:#define __ELF__
// EMSCRIPTEN-THREADS-NEXT:#define __EMSCRIPTEN_PTHREADS__ 1
@@ -1668,6 +1669,7 @@
// WEBASSEMBLY-NOT:#define __FLT16_MIN_10_EXP__
// WEBASSEMBLY-NOT:#define __FLT16_MIN_EXP__
// WEBASSEMBLY-NOT:#define __FLT16_MIN__
+// WEBASSEMBLY-NOT:#define __FLT16_NORM_MAX__
// WEBASSEMBLY-NEXT:#define __FLT_DECIMAL_DIG__ 9
// WEBASSEMBLY-NEXT:#define __FLT_DENORM_MIN__ 1.40129846e-45F
// WEBASSEMBLY-NEXT:#define __FLT_DIG__ 6
@@ -1682,6 +1684,7 @@
// WEBASSEMBLY-NEXT:#define __FLT_MIN_10_EXP__ (-37)
// WEBASSEMBLY-NEXT:#define __FLT_MIN_EXP__ (-125)
// WEBASSEMBLY-NEXT:#define __FLT_MIN__ 1.17549435e-38F
+// WEBASSEMBLY-NEXT:#define __FLT_NORM_MAX__ 3.40282347e+38F
// WEBASSEMBLY-NEXT:#define __FLT_RADIX__ 2
// WEBASSEMBLY-NEXT:#define __FPCLASS_NEGINF 0x0004
// WEBASSEMBLY-NEXT:#define __FPCLASS_NEGNORMAL 0x0008
@@ -1806,6 +1809,7 @@
// WEBASSEMBLY-NEXT:#define __LDBL_MIN_10_EXP__ (-4931)
// WEBASSEMBLY-NEXT:#define __LDBL_MIN_EXP__ (-16381)
// WEBASSEMBLY-NEXT:#define __LDBL_MIN__ 3.36210314311209350626267781732175260e-4932L
+// WEBASSEMBLY-NEXT:#define __LDBL_NORM_MAX__ 1.18973149535723176508575932662800702e+4932L
// WEBASSEMBLY-NEXT:#define __LITTLE_ENDIAN__ 1
// WEBASSEMBLY-NEXT:#define __LLONG_WIDTH__ 64
// WEBASSEMBLY-NEXT:#define __LONG_LONG_MAX__ 9223372036854775807LL
More information about the cfe-commits
mailing list