From fb177631c51607a85c6fdfb1e6888d39c8eb24f6 Mon Sep 17 00:00:00 2001
From: Yuao Ma <c8ef@outlook.com>
Date: Thu, 15 May 2025 01:31:27 +0800
Subject: [PATCH] gcc: add trigonometric pi-based functions as gcc builtins

Add trigonometric pi-based functions as GCC builtins: acospi, asinpi, atan2pi,
atanpi, cospi, sinpi, and tanpi. Latest glibc already provides support for
these functions, which we plan to leverage in future gfortran implementations.

The patch includes two test cases to verify both correct code generation and
function definition.

If approved, I suggest committing this foundational change first. Constant
folding for these builtins will be addressed in subsequent patches.

This patch partially handled PR118592.

gcc/ChangeLog:

	* builtins.def (TRIG_TYPE): New.
	(BUILT_IN_ACOSPI): New.
	(BUILT_IN_ACOSPIF): New.
	(BUILT_IN_ACOSPIL): New.
	(BUILT_IN_ASINPI): New.
	(BUILT_IN_ASINPIF): New.
	(BUILT_IN_ASINPIL): New.
	(BUILT_IN_ATANPI): New.
	(BUILT_IN_ATANPIF): New.
	(BUILT_IN_ATANPIL): New.
	(BUILT_IN_COSPI): New.
	(BUILT_IN_COSPIF): New.
	(BUILT_IN_COSPIL): New.
	(BUILT_IN_SINPI): New.
	(BUILT_IN_SINPIF): New.
	(BUILT_IN_SINPIL): New.
	(BUILT_IN_TANPI): New.
	(BUILT_IN_TANPIF): New.
	(BUILT_IN_TANPIL): New.
	(TRIG2_TYPE): New.
	(BUILT_IN_ATAN2PI): New.
	(BUILT_IN_ATAN2PIF): New.
	(BUILT_IN_ATAN2PIL): New.
	* doc/extend.texi: Mention new builtins.

gcc/testsuite/ChangeLog:

	* gcc.dg/builtins-1.c: Builtin codegen test.
	* gcc.dg/c23-builtins-1.c: Builtin signature test.

Signed-off-by: Yuao Ma <c8ef@outlook.com>
---
 gcc/builtins.def                      | 32 +++++++++++++++++++++++
 gcc/doc/extend.texi                   | 37 ++++++++++++++++++++++-----
 gcc/testsuite/gcc.dg/builtins-1.c     |  7 +++++
 gcc/testsuite/gcc.dg/c23-builtins-1.c | 22 ++++++++++++++++
 4 files changed, 92 insertions(+), 6 deletions(-)

diff --git a/gcc/builtins.def b/gcc/builtins.def
index ff470051e54..fdcad54a5d7 100644
--- a/gcc/builtins.def
+++ b/gcc/builtins.def
@@ -844,6 +844,38 @@ DEF_EXT_LIB_FLOATN_NX_BUILTINS (BUILT_IN_CTANH, "ctanh", CPROJ_TYPE, ATTR_MATHFN
 DEF_C99_COMPL_BUILTIN        (BUILT_IN_CTANL, "ctanl", BT_FN_COMPLEX_LONGDOUBLE_COMPLEX_LONGDOUBLE, ATTR_MATHFN_FPROUNDING)
 DEF_EXT_LIB_FLOATN_NX_BUILTINS (BUILT_IN_CTAN, "ctan", CPROJ_TYPE, ATTR_MATHFN_FPROUNDING)
 #undef CPROJ_TYPE
+#define TRIG_TYPE(F) BT_FN_##F##_##F
+DEF_C23_BUILTIN (BUILT_IN_ACOSPI, "acospi", BT_FN_DOUBLE_DOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_C23_BUILTIN (BUILT_IN_ACOSPIF, "acospif", BT_FN_FLOAT_FLOAT, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_C23_BUILTIN (BUILT_IN_ACOSPIL, "acospil", BT_FN_LONGDOUBLE_LONGDOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_EXT_LIB_FLOATN_NX_BUILTINS (BUILT_IN_ACOSPI, "acospi", TRIG_TYPE, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_C23_BUILTIN (BUILT_IN_ASINPI, "asinpi", BT_FN_DOUBLE_DOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_C23_BUILTIN (BUILT_IN_ASINPIF, "asinpif", BT_FN_FLOAT_FLOAT, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_C23_BUILTIN (BUILT_IN_ASINPIL, "asinpil", BT_FN_LONGDOUBLE_LONGDOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_EXT_LIB_FLOATN_NX_BUILTINS (BUILT_IN_ASINPI, "asinpi", TRIG_TYPE, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_C23_BUILTIN (BUILT_IN_ATANPI, "atanpi", BT_FN_DOUBLE_DOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_C23_BUILTIN (BUILT_IN_ATANPIF, "atanpif", BT_FN_FLOAT_FLOAT, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_C23_BUILTIN (BUILT_IN_ATANPIL, "atanpil", BT_FN_LONGDOUBLE_LONGDOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_EXT_LIB_FLOATN_NX_BUILTINS (BUILT_IN_ATANPI, "atanpi", TRIG_TYPE, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_C23_BUILTIN (BUILT_IN_COSPI, "cospi", BT_FN_DOUBLE_DOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_C23_BUILTIN (BUILT_IN_COSPIF, "cospif", BT_FN_FLOAT_FLOAT, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_C23_BUILTIN (BUILT_IN_COSPIL, "cospil", BT_FN_LONGDOUBLE_LONGDOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_EXT_LIB_FLOATN_NX_BUILTINS (BUILT_IN_COSPI, "cospi", TRIG_TYPE, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_C23_BUILTIN (BUILT_IN_SINPI, "sinpi", BT_FN_DOUBLE_DOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_C23_BUILTIN (BUILT_IN_SINPIF, "sinpif", BT_FN_FLOAT_FLOAT, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_C23_BUILTIN (BUILT_IN_SINPIL, "sinpil", BT_FN_LONGDOUBLE_LONGDOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_EXT_LIB_FLOATN_NX_BUILTINS (BUILT_IN_SINPI, "sinpi", TRIG_TYPE, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_C23_BUILTIN (BUILT_IN_TANPI, "tanpi", BT_FN_DOUBLE_DOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_C23_BUILTIN (BUILT_IN_TANPIF, "tanpif", BT_FN_FLOAT_FLOAT, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_C23_BUILTIN (BUILT_IN_TANPIL, "tanpil", BT_FN_LONGDOUBLE_LONGDOUBLE, ATTR_MATHFN_FPROUNDING_ERRNO)
+DEF_EXT_LIB_FLOATN_NX_BUILTINS (BUILT_IN_TANPI, "tanpi", TRIG_TYPE, ATTR_MATHFN_FPROUNDING_ERRNO)
+#undef TRIG_TYPE
+#define TRIG2_TYPE(F) BT_FN_##F##_##F##_##F
+DEF_C23_BUILTIN (BUILT_IN_ATAN2PI, "atan2pi", BT_FN_DOUBLE_DOUBLE_DOUBLE, ATTR_CONST_NOTHROW_LEAF_LIST)
+DEF_C23_BUILTIN (BUILT_IN_ATAN2PIF, "atan2pif", BT_FN_FLOAT_FLOAT_FLOAT, ATTR_CONST_NOTHROW_LEAF_LIST)
+DEF_C23_BUILTIN (BUILT_IN_ATAN2PIL, "atan2pil", BT_FN_LONGDOUBLE_LONGDOUBLE_LONGDOUBLE, ATTR_CONST_NOTHROW_LEAF_LIST)
+DEF_EXT_LIB_FLOATN_NX_BUILTINS (BUILT_IN_ATAN2PI, "atan2pi", TRIG2_TYPE, ATTR_CONST_NOTHROW_LEAF_LIST)
+#undef TRIG2_TYPE
 
 /* Category: string/memory builtins.  */
 DEF_EXT_LIB_BUILTIN    (BUILT_IN_BCMP, "bcmp", BT_FN_INT_CONST_PTR_CONST_PTR_SIZE, ATTR_PURE_NOTHROW_NONNULL_IF123_LEAF)
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 40ccf22b29f..37dae2552d6 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -14374,6 +14374,9 @@ a function call results in a compile-time error.
 @findex acoshf
 @findex acoshl
 @findex acosl
+@findex acospi
+@findex acospif
+@findex acospil
 @findex alloca
 @findex asin
 @findex asinf
@@ -14381,15 +14384,24 @@ a function call results in a compile-time error.
 @findex asinhf
 @findex asinhl
 @findex asinl
+@findex asinpi
+@findex asinpif
+@findex asinpil
 @findex atan
 @findex atan2
 @findex atan2f
 @findex atan2l
+@findex atan2pi
+@findex atan2pif
+@findex atan2pil
 @findex atanf
 @findex atanh
 @findex atanhf
 @findex atanhl
 @findex atanl
+@findex atanpi
+@findex atanpif
+@findex atanpil
 @findex bcmp
 @findex bzero
 @findex cabs
@@ -14453,6 +14465,9 @@ a function call results in a compile-time error.
 @findex coshf
 @findex coshl
 @findex cosl
+@findex cospi
+@findex cospif
+@findex cospil
 @findex cpow
 @findex cpowf
 @findex cpowl
@@ -14688,6 +14703,9 @@ a function call results in a compile-time error.
 @findex sinhf
 @findex sinhl
 @findex sinl
+@findex sinpi
+@findex sinpif
+@findex sinpil
 @findex snprintf
 @findex sprintf
 @findex sqrt
@@ -14722,6 +14740,9 @@ a function call results in a compile-time error.
 @findex tanhf
 @findex tanhl
 @findex tanl
+@findex tanpi
+@findex tanpif
+@findex tanpil
 @findex tgamma
 @findex tgammaf
 @findex tgammal
@@ -14764,7 +14785,10 @@ a particular case, a call to the library function is emitted.
 @opindex std
 Outside strict ISO C mode (@option{-ansi}, @option{-std=c90},
 @option{-std=c99} or @option{-std=c11}), the functions
-@code{_exit}, @code{alloca}, @code{bcmp}, @code{bzero},
+@code{_exit}, @code{alloca}, @code{acospi}, @code{acospif}, @code{acospil},
+@code{asinpi}, @code{asinpif}, @code{asinpil}, @code{atan2pi}, @code{atan2pif},
+@code{atan2pil}, @code{atanpi}, @code{atanpif}, @code{atanpil}, @code{bcmp},
+@code{bzero}, @code{cospi}, @code{cospif}, @code{cospil},
 @code{dcgettext}, @code{dgettext}, @code{dremf}, @code{dreml},
 @code{drem}, @code{exp10f}, @code{exp10l}, @code{exp10}, @code{ffsll},
 @code{ffsl}, @code{ffs}, @code{fprintf_unlocked},
@@ -14779,11 +14803,12 @@ Outside strict ISO C mode (@option{-ansi}, @option{-std=c90},
 @code{signbit}, @code{signbitf}, @code{signbitl}, @code{signbitd32},
 @code{signbitd64}, @code{signbitd128}, @code{significandf},
 @code{significandl}, @code{significand}, @code{sincosf},
-@code{sincosl}, @code{sincos}, @code{stpcpy}, @code{stpncpy},
-@code{strcasecmp}, @code{strdup}, @code{strfmon}, @code{strncasecmp},
-@code{strndup}, @code{strnlen}, @code{toascii}, @code{y0f}, @code{y0l},
-@code{y0}, @code{y1f}, @code{y1l}, @code{y1}, @code{ynf}, @code{ynl} and
-@code{yn}
+@code{sincosl}, @code{sincos}, @code{sinpif}, @code{sinpil}, @code{sinpi},
+@code{stpcpy}, @code{stpncpy}, @code{strcasecmp}, @code{strdup},
+@code{strfmon}, @code{strncasecmp}, @code{strndup}, @code{strnlen},
+@code{tanpif}, @code{tanpil}, @code{tanpi}, @code{toascii}, @code{y0f},
+@code{y0l}, @code{y0}, @code{y1f}, @code{y1l}, @code{y1}, @code{ynf},
+@code{ynl} and @code{yn}
 may be handled as built-in functions.
 All these functions have corresponding versions
 prefixed with @code{__builtin_}, which may be used even in strict C90
diff --git a/gcc/testsuite/gcc.dg/builtins-1.c b/gcc/testsuite/gcc.dg/builtins-1.c
index 6128642b5b6..8ddf37049cf 100644
--- a/gcc/testsuite/gcc.dg/builtins-1.c
+++ b/gcc/testsuite/gcc.dg/builtins-1.c
@@ -114,16 +114,21 @@ _Complex long double test_##FN##l(_Complex long double x, _Complex long double y
 /* Keep this list sorted alphabetically by function name.  */
 FPTEST1     (acos)
 FPTEST1     (acosh)
+FPTEST1     (acospi)
 FPTEST1     (asin)
 FPTEST1     (asinh)
+FPTEST1     (asinpi)
 FPTEST1     (atan)
 FPTEST2     (atan2)
+FPTEST2     (atan2pi)
 FPTEST1     (atanh)
+FPTEST1     (atanpi)
 FPTEST1     (cbrt)
 FPTEST1     (ceil)
 FPTEST2     (copysign)
 FPTEST1     (cos)
 FPTEST1     (cosh)
+FPTEST1     (cospi)
 FPTEST2     (drem)
 FPTEST1     (erf)
 FPTEST1     (erfc)
@@ -178,9 +183,11 @@ FPTEST1     (significand)
 FPTEST1     (sin)
 FPTEST3FPP23VOID (sincos)
 FPTEST1     (sinh)
+FPTEST1     (sinpi)
 FPTEST1     (sqrt)
 FPTEST1     (tan)
 FPTEST1     (tanh)
+FPTEST1     (tanpi)
 FPTEST1     (tgamma)
 FPTEST1     (trunc)
 FPTEST1     (y0)
diff --git a/gcc/testsuite/gcc.dg/c23-builtins-1.c b/gcc/testsuite/gcc.dg/c23-builtins-1.c
index 39f0ea4bf59..fe679642606 100644
--- a/gcc/testsuite/gcc.dg/c23-builtins-1.c
+++ b/gcc/testsuite/gcc.dg/c23-builtins-1.c
@@ -3,11 +3,33 @@
 /* { dg-do compile } */
 /* { dg-options "-std=c23" } */
 
+/* Keep this list sorted alphabetically by function name.  */
+int acospi (void); /* { dg-warning "conflicting types for built-in function" } */
+int acospif (void); /* { dg-warning "conflicting types for built-in function" } */
+int acospil (void); /* { dg-warning "conflicting types for built-in function" } */
+int asinpi (void); /* { dg-warning "conflicting types for built-in function" } */
+int asinpif (void); /* { dg-warning "conflicting types for built-in function" } */
+int asinpil (void); /* { dg-warning "conflicting types for built-in function" } */
+int atan2pi (void); /* { dg-warning "conflicting types for built-in function" } */
+int atan2pif (void); /* { dg-warning "conflicting types for built-in function" } */
+int atan2pil (void); /* { dg-warning "conflicting types for built-in function" } */
+int atanpi (void); /* { dg-warning "conflicting types for built-in function" } */
+int atanpif (void); /* { dg-warning "conflicting types for built-in function" } */
+int atanpil (void); /* { dg-warning "conflicting types for built-in function" } */
+int cospi (void); /* { dg-warning "conflicting types for built-in function" } */
+int cospif (void); /* { dg-warning "conflicting types for built-in function" } */
+int cospil (void); /* { dg-warning "conflicting types for built-in function" } */
 int exp10 (void); /* { dg-warning "conflicting types for built-in function" } */
 int exp10f (void); /* { dg-warning "conflicting types for built-in function" } */
 int exp10l (void); /* { dg-warning "conflicting types for built-in function" } */
 int roundeven (void); /* { dg-warning "conflicting types for built-in function" } */
 int roundevenf (void); /* { dg-warning "conflicting types for built-in function" } */
 int roundevenl (void); /* { dg-warning "conflicting types for built-in function" } */
+int sinpi (void); /* { dg-warning "conflicting types for built-in function" } */
+int sinpif (void); /* { dg-warning "conflicting types for built-in function" } */
+int sinpil (void); /* { dg-warning "conflicting types for built-in function" } */
 int strdup (void); /* { dg-warning "conflicting types for built-in function" } */
 int strndup (void); /* { dg-warning "conflicting types for built-in function" } */
+int tanpi (void); /* { dg-warning "conflicting types for built-in function" } */
+int tanpif (void); /* { dg-warning "conflicting types for built-in function" } */
+int tanpil (void); /* { dg-warning "conflicting types for built-in function" } */
-- 
2.43.0

