C23 added trigonometric math functions that act on half-revolutions. Implement them in C.
Reduce the input argument of `sinpi?` and `tanpi?` to the interval [-1, +1] before passing the actual calculation on to `sin?` or `tan?`, respectively. Reduce the input argument of `cospi?` to the interval [0, 2] before passing the calculation on to `cos?`. For `acospi?`, `asinpi?`, `atanpi?`, and `atan2pi?`, pass the actual calculation on to `acos?`, `asin?`, `atan?`, or `atan2?`, respectively, without any domain reduction. Compared to the last patch, the new functions are now defined in separate files and added to libmingwex. Also, the new functions are now declared in `math.h` when building the CRT independent of the C standard version.
From 63fce9ab4d96b9515938f986b45b22c1da7b1eee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=BCtzel?= <[email protected]> Date: Thu, 8 Jan 2026 15:28:02 +0100 Subject: [PATCH] crt: Implement trigonometric functions on half-revolutions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit C23 added trigonometric math functions that act on half-revolutions. Implement them in C. Reduce the input argument of `sinpi?` and `tanpi?` to the interval [-1, +1] before passing the actual calculation on to `sin?` or `tan?`, respectively. Reduce the input argument of `cospi?` to the interval [0, 2] before passing the calculation on to `cos?`. For `acospi?`, `asinpi?`, `atanpi?`, and `atan2pi?`, pass the actual calculation on to `acos?`, `asin?`, `atan?`, or `atan2?`, respectively, without any domain reduction. Signed-off-by: Markus Mützel <[email protected]> --- mingw-w64-crt/Makefile.am | 10 ++++++++- mingw-w64-crt/math/acospi.c | 16 +++++++++++++++ mingw-w64-crt/math/acospif.c | 16 +++++++++++++++ mingw-w64-crt/math/acospil.c | 16 +++++++++++++++ mingw-w64-crt/math/asinpi.c | 16 +++++++++++++++ mingw-w64-crt/math/asinpif.c | 16 +++++++++++++++ mingw-w64-crt/math/asinpil.c | 16 +++++++++++++++ mingw-w64-crt/math/atan2pi.c | 16 +++++++++++++++ mingw-w64-crt/math/atan2pif.c | 16 +++++++++++++++ mingw-w64-crt/math/atan2pil.c | 16 +++++++++++++++ mingw-w64-crt/math/atanpi.c | 16 +++++++++++++++ mingw-w64-crt/math/atanpif.c | 16 +++++++++++++++ mingw-w64-crt/math/atanpil.c | 16 +++++++++++++++ mingw-w64-crt/math/cospi.c | 17 ++++++++++++++++ mingw-w64-crt/math/cospif.c | 17 ++++++++++++++++ mingw-w64-crt/math/cospil.c | 17 ++++++++++++++++ mingw-w64-crt/math/sinpi.c | 17 ++++++++++++++++ mingw-w64-crt/math/sinpif.c | 17 ++++++++++++++++ mingw-w64-crt/math/sinpil.c | 17 ++++++++++++++++ mingw-w64-crt/math/tanpi.c | 17 ++++++++++++++++ mingw-w64-crt/math/tanpif.c | 17 ++++++++++++++++ mingw-w64-crt/math/tanpil.c | 17 ++++++++++++++++ mingw-w64-headers/crt/math.h | 38 +++++++++++++++++++++++++++++++++++ 23 files changed, 392 insertions(+), 1 deletion(-) create mode 100644 mingw-w64-crt/math/acospi.c create mode 100644 mingw-w64-crt/math/acospif.c create mode 100644 mingw-w64-crt/math/acospil.c create mode 100644 mingw-w64-crt/math/asinpi.c create mode 100644 mingw-w64-crt/math/asinpif.c create mode 100644 mingw-w64-crt/math/asinpil.c create mode 100644 mingw-w64-crt/math/atan2pi.c create mode 100644 mingw-w64-crt/math/atan2pif.c create mode 100644 mingw-w64-crt/math/atan2pil.c create mode 100644 mingw-w64-crt/math/atanpi.c create mode 100644 mingw-w64-crt/math/atanpif.c create mode 100644 mingw-w64-crt/math/atanpil.c create mode 100644 mingw-w64-crt/math/cospi.c create mode 100644 mingw-w64-crt/math/cospif.c create mode 100644 mingw-w64-crt/math/cospil.c create mode 100644 mingw-w64-crt/math/sinpi.c create mode 100644 mingw-w64-crt/math/sinpif.c create mode 100644 mingw-w64-crt/math/sinpil.c create mode 100644 mingw-w64-crt/math/tanpi.c create mode 100644 mingw-w64-crt/math/tanpif.c create mode 100644 mingw-w64-crt/math/tanpil.c diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am index ff032f953..c8bce98e4 100644 --- a/mingw-w64-crt/Makefile.am +++ b/mingw-w64-crt/Makefile.am @@ -1214,7 +1214,12 @@ src_libmingwex=\ gdtoa/misc.c gdtoa/qnan.c gdtoa/smisc.c gdtoa/strtodg.c gdtoa/strtodnrp.c gdtoa/strtof.c \ gdtoa/strtopx.c gdtoa/sum.c gdtoa/ulp.c \ \ + math/acospi.c math/acospif.c math/acospil.c \ + math/asinpi.c math/asinpif.c math/asinpil.c \ + math/atan2pi.c math/atan2pif.c math/atan2pil.c \ + math/atanpi.c math/atanpif.c math/atanpil.c \ math/coshl.c \ + math/cospi.c math/cospif.c math/cospil.c \ math/fabsl.c math/fp_consts.c math/fp_constsf.c \ math/fp_constsl.c math/fpclassify.c math/fpclassifyf.c math/fpclassifyl.c math/frexpf.c math/frexpl.c \ math/hypotf.c math/hypotl.c math/isnan.c math/isnanf.c math/isnanl.c \ @@ -1223,7 +1228,10 @@ src_libmingwex=\ math/powi.c math/powif.c math/powil.c \ math/signbit.c math/signbitf.c math/signbitl.c \ math/signgam.c \ - math/sinhl.c math/sqrtl.c math/tanhl.c \ + math/sinhl.c \ + math/sinpi.c math/sinpif.c math/sinpil.c \ + math/sqrtl.c math/tanhl.c \ + math/tanpi.c math/tanpif.c math/tanpil.c \ math/powi.def.h math/sqrt.def.h \ math/cephes_mconf.h math/fp_consts.h \ \ diff --git a/mingw-w64-crt/math/acospi.c b/mingw-w64-crt/math/acospi.c new file mode 100644 index 000000000..9125bf96e --- /dev/null +++ b/mingw-w64-crt/math/acospi.c @@ -0,0 +1,16 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +double __cdecl acospi(double x) +{ + return acos(x)/M_PI; +} diff --git a/mingw-w64-crt/math/acospif.c b/mingw-w64-crt/math/acospif.c new file mode 100644 index 000000000..790415617 --- /dev/null +++ b/mingw-w64-crt/math/acospif.c @@ -0,0 +1,16 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +float __cdecl acospif(float x) +{ + return acosf(x)/M_PI; +} diff --git a/mingw-w64-crt/math/acospil.c b/mingw-w64-crt/math/acospil.c new file mode 100644 index 000000000..c8a6dceb5 --- /dev/null +++ b/mingw-w64-crt/math/acospil.c @@ -0,0 +1,16 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +long double __cdecl acospil(long double x) +{ + return acosl(x)/M_PI; +} diff --git a/mingw-w64-crt/math/asinpi.c b/mingw-w64-crt/math/asinpi.c new file mode 100644 index 000000000..efc0804cd --- /dev/null +++ b/mingw-w64-crt/math/asinpi.c @@ -0,0 +1,16 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +double __cdecl asinpi(double x) +{ + return asin(x)/M_PI; +} diff --git a/mingw-w64-crt/math/asinpif.c b/mingw-w64-crt/math/asinpif.c new file mode 100644 index 000000000..0ac86c520 --- /dev/null +++ b/mingw-w64-crt/math/asinpif.c @@ -0,0 +1,16 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +float __cdecl asinpif(float x) +{ + return asinf(x)/M_PI; +} diff --git a/mingw-w64-crt/math/asinpil.c b/mingw-w64-crt/math/asinpil.c new file mode 100644 index 000000000..9af922a0e --- /dev/null +++ b/mingw-w64-crt/math/asinpil.c @@ -0,0 +1,16 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +long double __cdecl asinpil(long double x) +{ + return asinl(x)/M_PI; +} diff --git a/mingw-w64-crt/math/atan2pi.c b/mingw-w64-crt/math/atan2pi.c new file mode 100644 index 000000000..52c199c71 --- /dev/null +++ b/mingw-w64-crt/math/atan2pi.c @@ -0,0 +1,16 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +double __cdecl atan2pi(double x, double y) +{ + return atan2(x, y)/M_PI; +} diff --git a/mingw-w64-crt/math/atan2pif.c b/mingw-w64-crt/math/atan2pif.c new file mode 100644 index 000000000..a5c75cda5 --- /dev/null +++ b/mingw-w64-crt/math/atan2pif.c @@ -0,0 +1,16 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +float __cdecl atan2pif(float x, float y) +{ + return atan2f(x, y)/M_PI; +} diff --git a/mingw-w64-crt/math/atan2pil.c b/mingw-w64-crt/math/atan2pil.c new file mode 100644 index 000000000..e345119c6 --- /dev/null +++ b/mingw-w64-crt/math/atan2pil.c @@ -0,0 +1,16 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +long double __cdecl atan2pil(long double x, long double y) +{ + return atan2l(x, y)/M_PI; +} diff --git a/mingw-w64-crt/math/atanpi.c b/mingw-w64-crt/math/atanpi.c new file mode 100644 index 000000000..ff393c773 --- /dev/null +++ b/mingw-w64-crt/math/atanpi.c @@ -0,0 +1,16 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +double __cdecl atanpi(double x) +{ + return atan(x)/M_PI; +} diff --git a/mingw-w64-crt/math/atanpif.c b/mingw-w64-crt/math/atanpif.c new file mode 100644 index 000000000..4c3e598ab --- /dev/null +++ b/mingw-w64-crt/math/atanpif.c @@ -0,0 +1,16 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +float __cdecl atanpif(float x) +{ + return atanf(x)/M_PI; +} diff --git a/mingw-w64-crt/math/atanpil.c b/mingw-w64-crt/math/atanpil.c new file mode 100644 index 000000000..28024ea9b --- /dev/null +++ b/mingw-w64-crt/math/atanpil.c @@ -0,0 +1,16 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +long double __cdecl atanpil(long double x) +{ + return atanl(x)/M_PI; +} diff --git a/mingw-w64-crt/math/cospi.c b/mingw-w64-crt/math/cospi.c new file mode 100644 index 000000000..28b11e3bc --- /dev/null +++ b/mingw-w64-crt/math/cospi.c @@ -0,0 +1,17 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +double __cdecl cospi(double x) +{ + x = fmod(x, 2.0); + return cos(x*M_PI); +} diff --git a/mingw-w64-crt/math/cospif.c b/mingw-w64-crt/math/cospif.c new file mode 100644 index 000000000..2591ca963 --- /dev/null +++ b/mingw-w64-crt/math/cospif.c @@ -0,0 +1,17 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +float __cdecl cospif(float x) +{ + x = fmodf(x, 2.0F); + return cosf(x*M_PI); +} diff --git a/mingw-w64-crt/math/cospil.c b/mingw-w64-crt/math/cospil.c new file mode 100644 index 000000000..546e3e972 --- /dev/null +++ b/mingw-w64-crt/math/cospil.c @@ -0,0 +1,17 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +long double __cdecl cospil(long double x) +{ + x = fmodl(x, 2.0L); + return cosl(x*M_PI); +} diff --git a/mingw-w64-crt/math/sinpi.c b/mingw-w64-crt/math/sinpi.c new file mode 100644 index 000000000..85ece3b40 --- /dev/null +++ b/mingw-w64-crt/math/sinpi.c @@ -0,0 +1,17 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +double __cdecl sinpi(double x) +{ + x = remainder(x, 2.0); + return sin(x*M_PI); +} diff --git a/mingw-w64-crt/math/sinpif.c b/mingw-w64-crt/math/sinpif.c new file mode 100644 index 000000000..176331caf --- /dev/null +++ b/mingw-w64-crt/math/sinpif.c @@ -0,0 +1,17 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +float __cdecl sinpif(float x) +{ + x = remainderf(x, 2.0F); + return sinf(x*M_PI); +} diff --git a/mingw-w64-crt/math/sinpil.c b/mingw-w64-crt/math/sinpil.c new file mode 100644 index 000000000..4c81408a6 --- /dev/null +++ b/mingw-w64-crt/math/sinpil.c @@ -0,0 +1,17 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +long double __cdecl sinpil(long double x) +{ + x = remainderl(x, 2.0L); + return sinl(x*M_PI); +} diff --git a/mingw-w64-crt/math/tanpi.c b/mingw-w64-crt/math/tanpi.c new file mode 100644 index 000000000..64ee67907 --- /dev/null +++ b/mingw-w64-crt/math/tanpi.c @@ -0,0 +1,17 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +double __cdecl tanpi(double x) +{ + x = remainder(x, 2.0); + return tan(x*M_PI); +} diff --git a/mingw-w64-crt/math/tanpif.c b/mingw-w64-crt/math/tanpif.c new file mode 100644 index 000000000..24345e919 --- /dev/null +++ b/mingw-w64-crt/math/tanpif.c @@ -0,0 +1,17 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +float __cdecl tanpif(float x) +{ + x = remainderf(x, 2.0F); + return tanf(x*M_PI); +} diff --git a/mingw-w64-crt/math/tanpil.c b/mingw-w64-crt/math/tanpil.c new file mode 100644 index 000000000..fece82cdd --- /dev/null +++ b/mingw-w64-crt/math/tanpil.c @@ -0,0 +1,17 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +long double __cdecl tanpil(long double x) +{ + x = remainderl(x, 2.0L); + return tanl(x*M_PI); +} diff --git a/mingw-w64-headers/crt/math.h b/mingw-w64-headers/crt/math.h index b5ddf940b..a597e57a5 100644 --- a/mingw-w64-headers/crt/math.h +++ b/mingw-w64-headers/crt/math.h @@ -698,6 +698,44 @@ __mingw_choose_expr ( \ #endif extern long double __cdecl tanhl(long double); +#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) \ + || defined(_CRTBLD) +/* 7.12.4.8 */ + double __cdecl acospi(double _X); + float __cdecl acospif(float _X); + long double __cdecl acospil(long double _X); + +/* 7.12.4.9 */ + double __cdecl asinpi(double _X); + float __cdecl asinpif(float _X); + long double __cdecl asinpil(long double _X); + +/* 7.12.4.10 */ + double __cdecl atanpi(double _X); + float __cdecl atanpif(float _X); + long double __cdecl atanpil(long double _X); + +/* 7.12.4.11 */ + double __cdecl atan2pi(double _X, double _Y); + float __cdecl atan2pif(float _X, float _Y); + long double __cdecl atan2pil(long double _X, long double _Y); + +/* 7.12.4.12 */ + double __cdecl cospi(double _X); + float __cdecl cospif(float _X); + long double __cdecl cospil(long double _X); + +/* 7.12.4.13 */ + double __cdecl sinpi(double _X); + float __cdecl sinpif(float _X); + long double __cdecl sinpil(long double _X); + +/* 7.12.4.14 */ + double __cdecl tanpi(double _X); + float __cdecl tanpif(float _X); + long double __cdecl tanpil(long double _X); +#endif + /* Inverse hyperbolic trig functions */ /* 7.12.5.1 */ extern double __cdecl acosh (double); -- 2.51.0.windows.2
_______________________________________________ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
