Skip to content

Commit

Permalink
[C23] Add *_NORM_MAX macros to <float.h> (#96643)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AaronBallman authored Jul 10, 2024
1 parent 4c03207 commit 0e7590a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------------------------------------

Expand Down
7 changes: 6 additions & 1 deletion clang/lib/Frontend/InitPreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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));
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/Headers/float.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) || \
Expand Down Expand Up @@ -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__
Expand Down
26 changes: 25 additions & 1 deletion clang/test/Headers/float.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions clang/test/Preprocessor/init-aarch64.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions clang/test/Preprocessor/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0e7590a

Please sign in to comment.