[PATCH 10/10] Don't use copysign in initializer

H.J. Lu hjl.tools@gmail.com
Fri Dec 13 07:13:10 GMT 2024


GCC 4.9 issues an error for copysign in initializer:

In file included from tst-printf-format-p-double.c:20:0:
tst-printf-format-skeleton-double.c:29:3: error: initializer element is not a constant expression [-Werror]
   { -HUGE_VAL, -DBL_MAX, -DBL_MIN, copysign (0, -1), -NAN, NAN, 0, DBL_MIN,
   ^

since it can't fold "copysign (0, -1)".  Use a constructor to call
copysign to initialize the array element at run-time instead.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 stdio-common/tst-printf-format-skeleton-double.c  | 10 ++++++++--
 stdio-common/tst-printf-format-skeleton-ldouble.c | 10 ++++++++--
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/stdio-common/tst-printf-format-skeleton-double.c b/stdio-common/tst-printf-format-skeleton-double.c
index 03ac594736..c803b7026d 100644
--- a/stdio-common/tst-printf-format-skeleton-double.c
+++ b/stdio-common/tst-printf-format-skeleton-double.c
@@ -25,9 +25,15 @@
 #define REF_VAL(v) (v)
 #define PREC DBL_MANT_DIG
 typedef double type_t;
-static const type_t vals[] =
-  { -HUGE_VAL, -DBL_MAX, -DBL_MIN, copysign (0, -1), -NAN, NAN, 0, DBL_MIN,
+static type_t vals[] =
+  { -HUGE_VAL, -DBL_MAX, -DBL_MIN, 0, -NAN, NAN, 0, DBL_MIN,
     DBL_MAX, HUGE_VAL };
 static const char length[] = "";
 
+static void __attribute__((constructor))
+init_copysign (void)
+{
+  vals[3] = copysign (0, -1);
+}
+
 #include "tst-printf-format-skeleton.c"
diff --git a/stdio-common/tst-printf-format-skeleton-ldouble.c b/stdio-common/tst-printf-format-skeleton-ldouble.c
index ed47e77963..50a8caf6ef 100644
--- a/stdio-common/tst-printf-format-skeleton-ldouble.c
+++ b/stdio-common/tst-printf-format-skeleton-ldouble.c
@@ -26,11 +26,17 @@
 #define REF_VAL(v) (v)
 #define PREC LDBL_MANT_DIG
 typedef long double type_t;
-static const type_t vals[] =
-  { -HUGE_VAL, -LDBL_MAX, -LDBL_MIN, copysign (0, -1), -NAN, NAN, 0, LDBL_MIN,
+static type_t vals[] =
+  { -HUGE_VAL, -LDBL_MAX, -LDBL_MIN, 0, -NAN, NAN, 0, LDBL_MIN,
     LDBL_MAX, HUGE_VAL };
 static const char length[] = "L";
 
+static void __attribute__((constructor))
+init_copysign (void)
+{
+  vals[3] = copysign (0, -1);
+}
+
 #ifndef TIMEOUT
 # define TIMEOUT (DEFAULT_TIMEOUT * 64)
 #endif
-- 
2.47.1



More information about the Libc-alpha mailing list