--- /dev/null
+/* Denormal number definitions.
+ Copyright (C) 2025 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#ifndef _MATH_H
+# error "Never use <bits/fp-builtin-denormal.h> directly; include <math.h> instead."
+#endif
+
+/* __FP_BUILTIN_FPCLASSIFY_DENORMAL is defined to 1 if compiler supports
+ handling pseudo-denormal numbers with fpclassify builtin. Pseudo-denormal
+ is a non-standard denormalized floating-point number only supported by
+ Intel double extended-precision (long double). By default assume 1 to
+ enable the usage of compiler builtin on math.h. */
+#define __FP_BUILTIN_FPCLASSIFY_DENORMAL 1
: FUNC ## l ARGS)
#endif
+
+/* Depending on the type of TG_ARG and extra DEFINE to check, either call the
+ BUILTIN with ARGS_B or an appropriately suffixed version of FUNC with
+ arguments (including parentheses) ARGS_B. The function call is used for
+ long double and/or _Float64x is the builtin can not be safely used on all
+ arguments (defined by DEFINE). */
+
+#include <bits/fp-builtin-denormal.h>
+
+#ifdef __NO_LONG_DOUBLE_MATH
+# define __MATH_TG_BUILTIN_CLASSIFY(TG_ARG, BUILTIN, ARGS_B, FUNC, ARGS_F, \
+ DEFINE) \
+ BUILTIN ARGS_B
+#elif __HAVE_DISTINCT_FLOAT128
+# if __HAVE_GENERIC_SELECTION
+# if __HAVE_FLOATN_NOT_TYPEDEF && __HAVE_FLOAT32
+# define __MATH_TG_BUILTIN_CLASSIFY_F32(BUILTIN, ARGS_B, FUNC, ARGS_F, \
+ DEFINE) \
+ _Float32: BUILTIN ARGS_B,
+# else
+# define __MATH_TG_BUILTIN_CLASSIFY_F32(BUILTIN, ARGS_B, FUNC, ARGS_F, \
+ DEFINE)
+# endif
+# define __MATH_TG_BUILTIN_CLASSIFY_LDOUBLE(BUILTIN, ARGS_B, FUNC, ARGS_F, \
+ DEFINE) \
+ long double: DEFINE ? BUILTIN ARGS_B : __ ## FUNC ## l ARGS_F,
+# if __HAVE_FLOATN_NOT_TYPEDEF && __HAVE_FLOAT64X
+# if __HAVE_FLOAT64X_LONG_DOUBLE
+# define __MATH_TG_BUILTIN_CLASSIFY_F64X(BUILTIN, ARGS_B, FUNC, ARGS_F, \
+ DEFINE) \
+ _Float64x: DEFINE ? BUILTIN ARGS_B : __ ## FUNC ## l ARGS_F,
+# else
+# define __MATH_TG_BUILTIN_CLASSIFY_F64X(BUILTIN, ARGS_B, FUNC, ARGS_F, \
+ DEFINE) \
+ _Float64x: DEFINE ? BUILTIN ARGS_B : __ ## FUNC ## f128 ARGS_F,
+# endif
+# else
+# define __MATH_TG_BUILTIN_CLASSIFY_F64X(BUILTIN, ARGS_B, FUNC, ARGS_F, \
+ DEFINE)
+# endif
+# define __MATH_TG_BUILTIN_CLASSIFY_F128(BUILTIN, ARGS_B, FUNC, ARGS_F, \
+ DEFINE) \
+ _Float128: BUILTIN ARGS_B
+# define __MATH_TG_BUILTIN_CLASSIFY(TG_ARG, BUILTIN, ARGS_B, FUNC, ARGS_F, \
+ DEFINE) \
+ _Generic ((TG_ARG), \
+ float: BUILTIN ARGS_B, \
+ __MATH_TG_BUILTIN_CLASSIFY_F32 (BUILTIN, ARGS_B, FUNC, ARGS_F, \
+ DEFINE) \
+ default: BUILTIN ARGS_B, \
+ __MATH_TG_BUILTIN_CLASSIFY_LDOUBLE (BUILTIN, ARGS_B, FUNC, \
+ ARGS_F, DEFINE) \
+ __MATH_TG_BUILTIN_CLASSIFY_F64X (BUILTIN, ARGS_B, FUNC, ARGS_F,\
+ DEFINE) \
+ __MATH_TG_BUILTIN_CLASSIFY_F128 (BUILTIN, ARGS_B, FUNC, ARGS_F,\
+ DEFINE))
+# else
+# define __MATH_TG_BUILTIN_CLASSIFY(TG_ARG, BUILTIN, ARGS_B, FUNC, ARGS_F, \
+ DEFINE) \
+ __builtin_choose_expr \
+ (__builtin_types_compatible_p (__typeof (TG_ARG), float), \
+ __builtin ## BUILTIN ARGS_B, \
+ __builtin_choose_expr \
+ (__builtin_types_compatible_p (__typeof (TG_ARG), double), \
+ __builtin ## BUILTIN ARGS_B, \
+ __builtin_choose_expr \
+ (__builtin_types_compatible_p (__typeof (TG_ARG), long double), \
+ DEFINE ? BUILTIN ARGS_B : __ ## FUNC ## l ARGS_F, \
+ BUILTIN ARGS_B)))
+# endif
+#else
+# define __MATH_TG_BUILTIN_CLASSIFY(TG_ARG, BUILTIN, ARGS_B, FUNC, ARGS_F, \
+ DEFINE) \
+ (sizeof (TG_ARG) == sizeof (float) \
+ ? BUILTIN ARGS_B \
+ : sizeof (TG_ARG) == sizeof (double) \
+ ? BUILTIN ARGS_B \
+ : DEFINE ? BUILTIN ARGS_B : __ ## FUNC ## l ARGS_F)
+#endif
+
/* ISO C99 defines some generic macros which work on any data type. */
#ifdef __USE_ISOC99
with -Os. No further use of this definition of fpclassify is
expected in C++ mode, since libstdc++ provides its own version
of fpclassify in cmath (which undefines fpclassify). */
-# define fpclassify(x) __builtin_fpclassify (FP_NAN, FP_INFINITE, \
- FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
+# define fpclassify(x) \
+ __MATH_TG_BUILTIN_CLASSIFY ((x), \
+ __builtin_fpclassify, (FP_NAN, FP_INFINITE, \
+ FP_NORMAL, FP_SUBNORMAL, \
+ FP_ZERO, x), \
+ fpclassify, (x), \
+ __FP_BUILTIN_FPCLASSIFY_DENORMAL)
# else
# define fpclassify(x) __MATH_TG ((x), __fpclassify, (x))
# endif
--- /dev/null
+/* Define __FP_BUILTIN_DENORMAL.
+ Copyright (C) 2025 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#ifndef _MATH_H
+# error "Never use <bits/fp-builtin-denormal.h> directly; include <math.h> instead."
+#endif
+
+/* Neither GCC (bug 123161) nor clang (issue 172533) handles pseudo-normal
+ numbers correctly with fpclassify builtin. */
+#define __FP_BUILTIN_FPCLASSIFY_DENORMAL 0
--- /dev/null
+/* Ccheck if math.h optimizations to call compiler builtin
+ does not trigger FE_INVALID on x86 denormal numbers.
+ Copyright (C) 2025 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <array_length.h>
+#include <fenv.h>
+#include <math.h>
+#include <math_ldbl.h>
+#include <support/check.h>
+
+#define pseudo_inf { .parts = { 0x00000000, 0x00000000, 0x7fff }}
+#define pseudo_zero { .parts = { 0x00000000, 0x00000000, 0x0100 }}
+#define pseudo_qnan { .parts = { 0x00000001, 0x00000000, 0x7fff }}
+#define pseudo_snan { .parts = { 0x00000001, 0x40000000, 0x7fff }}
+#define pseudo_unnormal { .parts = { 0x00000001, 0x40000000, 0x0100 }}
+
+static const ieee_long_double_shape_type inputs[] = {
+ pseudo_inf,
+ pseudo_zero,
+ pseudo_qnan,
+ pseudo_snan,
+ pseudo_unnormal
+};
+
+static int
+do_test (void)
+{
+ for (int i = 0; i < array_length (inputs); i++)
+ {
+ TEST_COMPARE (feclearexcept (FE_INVALID), 0);
+ TEST_COMPARE (fpclassify (inputs[i].value), FP_NAN);
+ TEST_COMPARE (fetestexcept (FE_INVALID), 0);
+ }
+
+ return 0;
+}
+
+#include <support/test-driver.c>