>From ab6dbc99ccd3723ac03f018e8cdffae8fe8eecc1 Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy Date: Tue, 2 Jun 2026 17:50:26 +0000 Subject: [PATCH 4/4] math.h: use builtin copysign bump the gcc version check for builtin copysign. copysignl affects fmal and floatscan, otherwise it's complex math. libc.so .text x86_64: -130 666206 riscv64: -266 612327 aarch64: -32 679499 arm: +192 658581 --- src/include/math.h | 5 ++++- src/math/copysign.c | 1 + src/math/copysignf.c | 1 + src/math/copysignl.c | 2 ++ src/math/riscv32/copysign.c | 1 + src/math/riscv32/copysignf.c | 1 + src/math/riscv64/copysign.c | 1 + src/math/riscv64/copysignf.c | 1 + 8 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/include/math.h b/src/include/math.h index 47f4ad9d..9884f02e 100644 --- a/src/include/math.h +++ b/src/include/math.h @@ -4,7 +4,7 @@ #include "../../include/math.h" #include -#if __GNUC__ >= 3 +#if __GNUC__*100+__GNUC_MINOR__ >= 304 #define fabs(x) __builtin_fabs(x) #define fabsf(x) __builtin_fabsf(x) #define fabsl(x) __builtin_fabsl(x) @@ -13,6 +13,9 @@ #define sqrtf(x) __builtin_sqrtf(x) #endif #define sqrtl(x) __builtin_sqrtl(x) +#define copysign(x,y) __builtin_copysign(x,y) +#define copysignf(x,y) __builtin_copysignf(x,y) +#define copysignl(x,y) __builtin_copysignl(x,y) #endif #endif diff --git a/src/math/copysign.c b/src/math/copysign.c index b09331b6..7ab9f7eb 100644 --- a/src/math/copysign.c +++ b/src/math/copysign.c @@ -1,5 +1,6 @@ #include "libm.h" +#undef copysign double copysign(double x, double y) { union {double f; uint64_t i;} ux={x}, uy={y}; ux.i &= -1ULL/2; diff --git a/src/math/copysignf.c b/src/math/copysignf.c index 0af6ae9b..b193e5b9 100644 --- a/src/math/copysignf.c +++ b/src/math/copysignf.c @@ -1,6 +1,7 @@ #include #include +#undef copysignf float copysignf(float x, float y) { union {float f; uint32_t i;} ux={x}, uy={y}; diff --git a/src/math/copysignl.c b/src/math/copysignl.c index 9dd933cf..ab4e9d72 100644 --- a/src/math/copysignl.c +++ b/src/math/copysignl.c @@ -1,11 +1,13 @@ #include "libm.h" #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 +#undef copysignl long double copysignl(long double x, long double y) { return copysign(x, y); } #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 +#undef copysignl long double copysignl(long double x, long double y) { union ldshape ux = {x}, uy = {y}; diff --git a/src/math/riscv32/copysign.c b/src/math/riscv32/copysign.c index c7854178..82e2197e 100644 --- a/src/math/riscv32/copysign.c +++ b/src/math/riscv32/copysign.c @@ -2,6 +2,7 @@ #if __riscv_flen >= 64 +#undef copysign double copysign(double x, double y) { __asm__ ("fsgnj.d %0, %1, %2" : "=f"(x) : "f"(x), "f"(y)); diff --git a/src/math/riscv32/copysignf.c b/src/math/riscv32/copysignf.c index a125611a..65f0e081 100644 --- a/src/math/riscv32/copysignf.c +++ b/src/math/riscv32/copysignf.c @@ -2,6 +2,7 @@ #if __riscv_flen >= 32 +#undef copysignf float copysignf(float x, float y) { __asm__ ("fsgnj.s %0, %1, %2" : "=f"(x) : "f"(x), "f"(y)); diff --git a/src/math/riscv64/copysign.c b/src/math/riscv64/copysign.c index c7854178..82e2197e 100644 --- a/src/math/riscv64/copysign.c +++ b/src/math/riscv64/copysign.c @@ -2,6 +2,7 @@ #if __riscv_flen >= 64 +#undef copysign double copysign(double x, double y) { __asm__ ("fsgnj.d %0, %1, %2" : "=f"(x) : "f"(x), "f"(y)); diff --git a/src/math/riscv64/copysignf.c b/src/math/riscv64/copysignf.c index a125611a..65f0e081 100644 --- a/src/math/riscv64/copysignf.c +++ b/src/math/riscv64/copysignf.c @@ -2,6 +2,7 @@ #if __riscv_flen >= 32 +#undef copysignf float copysignf(float x, float y) { __asm__ ("fsgnj.s %0, %1, %2" : "=f"(x) : "f"(x), "f"(y)); -- 2.52.0