Re: PING! Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
On Tue, Oct 25, 2016 at 11:37 AM Cesar Philippidiswrote: > > On 10/10/2016 08:06 AM, Fritz Reese wrote: > > > --- a/gcc/fortran/intrinsic.texi > > +++ b/gcc/fortran/intrinsic.texi > > @@ -23,6 +23,9 @@ Some basic guidelines for editing this document: > > @end ignore > > > > @tex > > +\gdef\acosd{\mathop{\rm acosd}\nolimits} > > +\gdef\asind{\mathop{\rm asind}\nolimits} > > +\gdef\atand{\mathop{\rm atand}\nolimits} > > \gdef\acos{\mathop{\rm acos}\nolimits} > > \gdef\asin{\mathop{\rm asin}\nolimits} > > \gdef\atan{\mathop{\rm atan}\nolimits} > > There should be a new mathop for cosd or else ... > > > +@item @emph{Return value}: > > +The return value is of the same type and kind as @var{X}. The real part > > +of the result is in degrees. If @var{X} is of the type @code{REAL}, > > +the return value lies in the range @math{ -1 \leq \cosd (x) \leq 1}. > > ... this will cause texinfo/pdflatex to complain about an undefined > function when you try to run 'make pdf'. The attached adds a mathop for > cosd. > > Is this patch OK for trunk? > > Cesar Yes, I think that counts as 'obvious'. Thanks! --- Fritz Reese
Re: PING! Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
On 10/10/2016 08:06 AM, Fritz Reese wrote: > --- a/gcc/fortran/intrinsic.texi > +++ b/gcc/fortran/intrinsic.texi > @@ -23,6 +23,9 @@ Some basic guidelines for editing this document: > @end ignore > > @tex > +\gdef\acosd{\mathop{\rm acosd}\nolimits} > +\gdef\asind{\mathop{\rm asind}\nolimits} > +\gdef\atand{\mathop{\rm atand}\nolimits} > \gdef\acos{\mathop{\rm acos}\nolimits} > \gdef\asin{\mathop{\rm asin}\nolimits} > \gdef\atan{\mathop{\rm atan}\nolimits} There should be a new mathop for cosd or else ... > +@item @emph{Return value}: > +The return value is of the same type and kind as @var{X}. The real part > +of the result is in degrees. If @var{X} is of the type @code{REAL}, > +the return value lies in the range @math{ -1 \leq \cosd (x) \leq 1}. ... this will cause texinfo/pdflatex to complain about an undefined function when you try to run 'make pdf'. The attached adds a mathop for cosd. Is this patch OK for trunk? Cesar 2016-10-25 Cesar Philippidisgcc/fortran/ * intrinsic.texi (cosd): New mathop. diff --git a/gcc/fortran/intrinsic.texi b/gcc/fortran/intrinsic.texi index 16e1d5c..85f781e 100644 --- a/gcc/fortran/intrinsic.texi +++ b/gcc/fortran/intrinsic.texi @@ -32,6 +32,7 @@ Some basic guidelines for editing this document: \gdef\acosh{\mathop{\rm acosh}\nolimits} \gdef\asinh{\mathop{\rm asinh}\nolimits} \gdef\atanh{\mathop{\rm atanh}\nolimits} +\gdef\cosd{\mathop{\rm cosd}\nolimits} @end tex
Re: PING! Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
On Tue, Oct 11, 2016 at 2:44 PM, Fritz Reesewrote: > On Mon, 2016-10-11 08:43 AM, Markus Trippelsdorf > wrote: >> This patch breaks bootstrap: > ... > > Sorry all!! was in a rush to get in the car this morning and made a > hasty commit. Fixed for r241001 (sorry Jerry, I was a little too > slow.) > > --- > Fritz Reese dec_math_4_fix.patch Description: Binary data
Re: PING! Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
On Mon, 2016-10-11 08:43 AM, Markus Trippelsdorfwrote: > This patch breaks bootstrap: ... Sorry all!! was in a rush to get in the car this morning and made a hasty commit. Fixed for r241001 (sorry Jerry, I was a little too slow.) --- Fritz Reese
Re: PING! Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
On Tue, Oct 11, 2016 at 11:10:13AM -0700, Jerry DeLisle wrote: > @@ -1782,6 +1784,7 @@ radians_f (mpfr_t x, mp_rnd_t rnd_mode) > mpfr_div_d (x, x, 180.0, rnd_mode); > I forgot to point out that the above should changed to mpfr_div_ui (x, x, 180, rnd_mode); In fact, any place where 180.0 and/or 360.0 should be chnage to the unsigned integer equivalent. -- Steve
Re: PING! Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
On Tue, Oct 11, 2016 at 11:10:13AM -0700, Jerry DeLisle wrote: > > diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c > index bf60f747..18135d2f 100644 > --- a/gcc/fortran/simplify.c > +++ b/gcc/fortran/simplify.c > @@ -1768,11 +1768,13 @@ degrees_f (mpfr_t x, mp_rnd_t rnd_mode) > static void > radians_f (mpfr_t x, mp_rnd_t rnd_mode) > { > -mpfr_t tmp; > +mpfr_t tmp, modtmp; > mpfr_init (tmp); > +mpfr_init (modtmp); Delete modtmp. > > /* Set x = x % 360 to avoid offsets with large angles. */ > -mpfr_fmod_d (tmp, x, 360.0, rnd_mode); > +mpfr_set_d (modtmp, 360.0, GFC_RND_MODE); Change this to mpfr_set_ui(tmp, 360, rnd_mode); > +mpfr_fmod (tmp, x, modtmp, rnd_mode); Change this to mpfr_fmod (tmp, x, tmp, rnd_mode); > > /* Set x = x * pi. */ > mpfr_const_pi (tmp, rnd_mode); > @@ -1782,6 +1784,7 @@ radians_f (mpfr_t x, mp_rnd_t rnd_mode) > mpfr_div_d (x, x, 180.0, rnd_mode); > > mpfr_clear (tmp); > +mpfr_clear (modtmp); Delete this. > } -- Steve
Re: PING! Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
On 10/11/2016 09:32 AM, Aaron Sawdey wrote: > On Tue, 2016-10-11 at 07:26 -0400, Fritz Reese wrote: >> On Mon, Oct 10, 2016 at 3:56 PM, Steve Kargl ... snip ... > > I think the first part of that cleanup didn't get applied as I am > seeing this: > > ../../gcc/gcc/fortran/iresolve.c: In function âgfc_expr* get_degrees(gfc_expr*)â: > ../../gcc/gcc/fortran/iresolve.c:2728:14: error: âtmpâ was not declared in this scope > > and also this: > > ../../gcc/gcc/fortran/simplify.c: In function âvoid radians_f(__mpfr_struct*, mpfr_rnd_t)â: > ../../gcc/gcc/fortran/simplify.c:1775:5: error: âmpfr_fmod_dâ was not declared in this scope > mpfr_fmod_d (tmp, x, 360.0, rnd_mode); > ^~~ > This allows compilation. Still have to check if its the right result. --Jerry diff --git a/gcc/fortran/iresolve.c b/gcc/fortran/iresolve.c index f4f81b2e..4334522c 100644 --- a/gcc/fortran/iresolve.c +++ b/gcc/fortran/iresolve.c @@ -2702,6 +2702,7 @@ get_degrees (gfc_expr *rad) { gfc_expr *result, *factor; gfc_actual_arglist *mod_args; + mpfr_t tmp; gcc_assert (rad->ts.type == BT_REAL); diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c index bf60f747..18135d2f 100644 --- a/gcc/fortran/simplify.c +++ b/gcc/fortran/simplify.c @@ -1768,11 +1768,13 @@ degrees_f (mpfr_t x, mp_rnd_t rnd_mode) static void radians_f (mpfr_t x, mp_rnd_t rnd_mode) { -mpfr_t tmp; +mpfr_t tmp, modtmp; mpfr_init (tmp); +mpfr_init (modtmp); /* Set x = x % 360 to avoid offsets with large angles. */ -mpfr_fmod_d (tmp, x, 360.0, rnd_mode); +mpfr_set_d (modtmp, 360.0, GFC_RND_MODE); +mpfr_fmod (tmp, x, modtmp, rnd_mode); /* Set x = x * pi. */ mpfr_const_pi (tmp, rnd_mode); @@ -1782,6 +1784,7 @@ radians_f (mpfr_t x, mp_rnd_t rnd_mode) mpfr_div_d (x, x, 180.0, rnd_mode); mpfr_clear (tmp); +mpfr_clear (modtmp); }
Re: PING! Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
Hi all, attached a small patch that resolves the issues at least on x86_64-linux/F23. - Andre On Tue, 11 Oct 2016 11:32:50 -0500 Aaron Sawdeywrote: > On Tue, 2016-10-11 at 07:26 -0400, Fritz Reese wrote: > > On Mon, Oct 10, 2016 at 3:56 PM, Steve Kargl > > wrote: > > ... > > > > > > There are a few small clean-up that can be > > > done. For example, > > > > > > +static gfc_expr * > > > +get_radians (gfc_expr *deg) > > > +{ > > > + mpfr_t tmp; > > ... > > > > > > the tmp variable is unneeded in the above. Converting the double > > > precision 180.0 to mpfr_t and then dividing is probably slower > > > than just dividing by 180. > > > > > > + /* Set factor = pi / 180. */ > > > + factor = gfc_get_constant_expr (deg->ts.type, deg->ts.kind, > > > >where); > > > + mpfr_const_pi (factor->value.real, GFC_RND_MODE); > > > + mpfr_div_ui (factor->value.real, factor->value.real, 180, > > > GFC_RND_MODE); > > > > > ... > > > > Good catch, fixed and committed r240989. Many thanks to you and > > Jerry. > > > > --- > > Fritz Reese > > > > I think the first part of that cleanup didn't get applied as I am > seeing this: > > ../../gcc/gcc/fortran/iresolve.c: In function âgfc_expr* > get_degrees(gfc_expr*)â: ../../gcc/gcc/fortran/iresolve.c:2728:14: error: > âtmpâ was not declared in this scope > > and also this: > > ../../gcc/gcc/fortran/simplify.c: In function âvoid radians_f(__mpfr_struct*, > mpfr_rnd_t)â: ../../gcc/gcc/fortran/simplify.c:1775:5: error: âmpfr_fmod_dâ > was not declared in this scope mpfr_fmod_d (tmp, x, 360.0, rnd_mode); > ^~~ > -- Andre Vehreschild * Email: vehre ad gmx dot de diff --git a/gcc/fortran/iresolve.c b/gcc/fortran/iresolve.c index f4f81b2..4334522 100644 --- a/gcc/fortran/iresolve.c +++ b/gcc/fortran/iresolve.c @@ -2702,6 +2702,7 @@ get_degrees (gfc_expr *rad) { gfc_expr *result, *factor; gfc_actual_arglist *mod_args; + mpfr_t tmp; gcc_assert (rad->ts.type == BT_REAL); diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c index bf60f74..9204962 100644 --- a/gcc/fortran/simplify.c +++ b/gcc/fortran/simplify.c @@ -1768,11 +1768,13 @@ degrees_f (mpfr_t x, mp_rnd_t rnd_mode) static void radians_f (mpfr_t x, mp_rnd_t rnd_mode) { -mpfr_t tmp; +mpfr_t tmp, tmp2; mpfr_init (tmp); +mpfr_init (tmp2); /* Set x = x % 360 to avoid offsets with large angles. */ -mpfr_fmod_d (tmp, x, 360.0, rnd_mode); +mpfr_set_d (tmp2, 360.0, rnd_mode); +mpfr_fmod (tmp, x, tmp2, rnd_mode); /* Set x = x * pi. */ mpfr_const_pi (tmp, rnd_mode); @@ -1782,6 +1784,7 @@ radians_f (mpfr_t x, mp_rnd_t rnd_mode) mpfr_div_d (x, x, 180.0, rnd_mode); mpfr_clear (tmp); +mpfr_clear (tmp2); }
Re: PING! Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
On Tue, 2016-10-11 at 07:26 -0400, Fritz Reese wrote: > On Mon, Oct 10, 2016 at 3:56 PM, Steve Kargl >wrote: > ... > > > > There are a few small clean-up that can be > > done. For example, > > > > +static gfc_expr * > > +get_radians (gfc_expr *deg) > > +{ > > + mpfr_t tmp; > ... > > > > the tmp variable is unneeded in the above. Converting the double > > precision 180.0 to mpfr_t and then dividing is probably slower > > than just dividing by 180. > > > > + /* Set factor = pi / 180. */ > > + factor = gfc_get_constant_expr (deg->ts.type, deg->ts.kind, > > >where); > > + mpfr_const_pi (factor->value.real, GFC_RND_MODE); > > + mpfr_div_ui (factor->value.real, factor->value.real, 180, > > GFC_RND_MODE); > > > ... > > Good catch, fixed and committed r240989. Many thanks to you and > Jerry. > > --- > Fritz Reese > I think the first part of that cleanup didn't get applied as I am seeing this: ../../gcc/gcc/fortran/iresolve.c: In function âgfc_expr* get_degrees(gfc_expr*)â: ../../gcc/gcc/fortran/iresolve.c:2728:14: error: âtmpâ was not declared in this scope and also this: ../../gcc/gcc/fortran/simplify.c: In function âvoid radians_f(__mpfr_struct*, mpfr_rnd_t)â: ../../gcc/gcc/fortran/simplify.c:1775:5: error: âmpfr_fmod_dâ was not declared in this scope mpfr_fmod_d (tmp, x, 360.0, rnd_mode); ^~~ -- Aaron Sawdey, Ph.D. acsaw...@linux.vnet.ibm.com 050-2/C113 (507) 253-7520 home: 507/263-0782 IBM Linux Technology Center - PPC Toolchain
Re: PING! Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
On 10/11/2016 07:37 AM, Andrew Pinski wrote: > > Looks like it was only broken for some mpfr's. That is ones which is > compiled with gcc it worked but one which comes with Ubuntu 1604 it > does not. they are exactly the same version too. > > Thanks, > Andrew Also broken on Fedora 24. Fritz, suggest you revert unless you have a quick fix. Or maybe go without using mpfr_mod_d for a while. Jerry
Re: PING! Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
On Tue, Oct 11, 2016 at 5:43 AM, Markus Trippelsdorfwrote: > On 2016.10.11 at 07:26 -0400, Fritz Reese wrote: >> On Mon, Oct 10, 2016 at 3:56 PM, Steve Kargl >> wrote: >> ... >> > There are a few small clean-up that can be >> > done. For example, >> > >> > +static gfc_expr * >> > +get_radians (gfc_expr *deg) >> > +{ >> > + mpfr_t tmp; >> ... >> > the tmp variable is unneeded in the above. Converting the double >> > precision 180.0 to mpfr_t and then dividing is probably slower >> > than just dividing by 180. >> > >> > + /* Set factor = pi / 180. */ >> > + factor = gfc_get_constant_expr (deg->ts.type, deg->ts.kind, >> > >where); >> > + mpfr_const_pi (factor->value.real, GFC_RND_MODE); >> > + mpfr_div_ui (factor->value.real, factor->value.real, 180, GFC_RND_MODE); >> > >> ... >> >> Good catch, fixed and committed r240989. Many thanks to you and Jerry. > > This patch breaks bootstrap: > > ../../gcc/gcc/fortran/iresolve.c: In function ‘gfc_expr* > get_degrees(gfc_expr*)’: > ../../gcc/gcc/fortran/iresolve.c:2728:14: error: ‘tmp’ was not declared in > this scope > mpfr_init (tmp); > > Even declaring the variable doesn't help: > > ../../gcc/gcc/fortran/simplify.c: In function ‘void radians_f(__mpfr_struct*, > mpfr_rnd_t)’: > ../../gcc/gcc/fortran/simplify.c:1775:5: error: ‘mpfr_fmod_d’ was not > declared in this scope > mpfr_fmod_d (tmp, x, 360.0, rnd_mode); > ^~~ Looks like it was only broken for some mpfr's. That is ones which is compiled with gcc it worked but one which comes with Ubuntu 1604 it does not. they are exactly the same version too. Thanks, Andrew > -- > Markus
Re: PING! Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
On 2016.10.11 at 07:26 -0400, Fritz Reese wrote: > On Mon, Oct 10, 2016 at 3:56 PM, Steve Kargl >wrote: > ... > > There are a few small clean-up that can be > > done. For example, > > > > +static gfc_expr * > > +get_radians (gfc_expr *deg) > > +{ > > + mpfr_t tmp; > ... > > the tmp variable is unneeded in the above. Converting the double > > precision 180.0 to mpfr_t and then dividing is probably slower > > than just dividing by 180. > > > > + /* Set factor = pi / 180. */ > > + factor = gfc_get_constant_expr (deg->ts.type, deg->ts.kind, >where); > > + mpfr_const_pi (factor->value.real, GFC_RND_MODE); > > + mpfr_div_ui (factor->value.real, factor->value.real, 180, GFC_RND_MODE); > > > ... > > Good catch, fixed and committed r240989. Many thanks to you and Jerry. This patch breaks bootstrap: ../../gcc/gcc/fortran/iresolve.c: In function ‘gfc_expr* get_degrees(gfc_expr*)’: ../../gcc/gcc/fortran/iresolve.c:2728:14: error: ‘tmp’ was not declared in this scope mpfr_init (tmp); Even declaring the variable doesn't help: ../../gcc/gcc/fortran/simplify.c: In function ‘void radians_f(__mpfr_struct*, mpfr_rnd_t)’: ../../gcc/gcc/fortran/simplify.c:1775:5: error: ‘mpfr_fmod_d’ was not declared in this scope mpfr_fmod_d (tmp, x, 360.0, rnd_mode); ^~~ -- Markus
Re: PING! Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
On Mon, Oct 10, 2016 at 3:56 PM, Steve Karglwrote: ... > There are a few small clean-up that can be > done. For example, > > +static gfc_expr * > +get_radians (gfc_expr *deg) > +{ > + mpfr_t tmp; ... > the tmp variable is unneeded in the above. Converting the double > precision 180.0 to mpfr_t and then dividing is probably slower > than just dividing by 180. > > + /* Set factor = pi / 180. */ > + factor = gfc_get_constant_expr (deg->ts.type, deg->ts.kind, >where); > + mpfr_const_pi (factor->value.real, GFC_RND_MODE); > + mpfr_div_ui (factor->value.real, factor->value.real, 180, GFC_RND_MODE); > ... Good catch, fixed and committed r240989. Many thanks to you and Jerry. --- Fritz Reese
Re: PING! Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
On Mon, Oct 10, 2016 at 12:29:32PM -0700, Jerry DeLisle wrote: > On 10/10/2016 08:06 AM, Fritz Reese wrote: > > https://gcc.gnu.org/ml/fortran/2016-09/msg00163.html [original] > > https://gcc.gnu.org/ml/fortran/2016-09/msg00183.html [latest] > > > > On Wed, Sep 28, 2016 at 4:14 PM, Fritz Reesewrote: > >> Attached is a patch extending the GNU Fortran front-end to support > >> some additional math intrinsics, enabled with a new compile flag > >> -fdec-math. The flag adds the COTAN intrinsic (cotangent), as well as > >> degree versions of all trigonometric intrinsics (SIND, TAND, ACOSD, > >> etc...). This extension allows for further compatibility with legacy > >> code that depends on the compiler to support such intrinsic functions. > > > > Patch is still pending. Current draft of the patch is re-attached for > > convenience, since it was amended twice since the original post. OK > > for trunk? > > > > OK, thanks for the work. > Sorry about following behind. I did intend to review the patch, but time got away from me. There are a few small clean-up that can be done. For example, +static gfc_expr * +get_radians (gfc_expr *deg) +{ + mpfr_t tmp; ... + /* Set factor = pi / 180. */ + factor = gfc_get_constant_expr (deg->ts.type, deg->ts.kind, >where); + mpfr_const_pi (factor->value.real, GFC_RND_MODE); + mpfr_init (tmp); + mpfr_set_d (tmp, 180.0, GFC_RND_MODE); + mpfr_div (factor->value.real, factor->value.real, tmp, GFC_RND_MODE); + mpfr_clear (tmp); the tmp variable is unneeded in the above. Converting the double precision 180.0 to mpfr_t and then dividing is probably slower than just dividing by 180. + /* Set factor = pi / 180. */ + factor = gfc_get_constant_expr (deg->ts.type, deg->ts.kind, >where); + mpfr_const_pi (factor->value.real, GFC_RND_MODE); + mpfr_div_ui (factor->value.real, factor->value.real, 180, GFC_RND_MODE); Of course, the clean-up can be done post-commit by Fritz. -- Steve
Re: PING! Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
On 10/10/2016 08:06 AM, Fritz Reese wrote: > https://gcc.gnu.org/ml/fortran/2016-09/msg00163.html [original] > https://gcc.gnu.org/ml/fortran/2016-09/msg00183.html [latest] > > On Wed, Sep 28, 2016 at 4:14 PM, Fritz Reesewrote: >> Attached is a patch extending the GNU Fortran front-end to support >> some additional math intrinsics, enabled with a new compile flag >> -fdec-math. The flag adds the COTAN intrinsic (cotangent), as well as >> degree versions of all trigonometric intrinsics (SIND, TAND, ACOSD, >> etc...). This extension allows for further compatibility with legacy >> code that depends on the compiler to support such intrinsic functions. > > Patch is still pending. Current draft of the patch is re-attached for > convenience, since it was amended twice since the original post. OK > for trunk? > OK, thanks for the work. Jerry
PING! Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
https://gcc.gnu.org/ml/fortran/2016-09/msg00163.html [original] https://gcc.gnu.org/ml/fortran/2016-09/msg00183.html [latest] On Wed, Sep 28, 2016 at 4:14 PM, Fritz Reesewrote: > Attached is a patch extending the GNU Fortran front-end to support > some additional math intrinsics, enabled with a new compile flag > -fdec-math. The flag adds the COTAN intrinsic (cotangent), as well as > degree versions of all trigonometric intrinsics (SIND, TAND, ACOSD, > etc...). This extension allows for further compatibility with legacy > code that depends on the compiler to support such intrinsic functions. Patch is still pending. Current draft of the patch is re-attached for convenience, since it was amended twice since the original post. OK for trunk? --- Fritz Reese 2016-09-28 Fritz Reese New flag -fdec-math for COTAN and degree trig intrinsics. gcc/fortran/ * lang.opt: New flag -fdec-math. * options.c (set_dec_flags): Enable with -fdec. * invoke.texi, gfortran.texi, intrinsic.texi: Update documentation. * intrinsics.c (add_functions, do_simplify): New intrinsics with -fdec-math. * gfortran.h (gfc_isym_id): New isym GFC_ISYM_COTAN. * gfortran.h (gfc_resolve_atan2d, gfc_resolve_cotan, gfc_resolve_trigd, gfc_resolve_atrigd): New prototypes. * iresolve.c (resolve_trig_call, get_degrees, get_radians, is_trig_resolved, gfc_resolve_cotan, gfc_resolve_trigd, gfc_resolve_atrigd, gfc_resolve_atan2d): New functions. * intrinsics.h (gfc_simplify_atan2d, gfc_simplify_atrigd, gfc_simplify_cotan, gfc_simplify_trigd): New prototypes. * simplify.c (simplify_trig_call, degrees_f, radians_f, gfc_simplify_cotan, gfc_simplify_trigd, gfc_simplify_atrigd, gfc_simplify_atan2d): New functions. gcc/testsuite/gfortran.dg/ * dec_math.f90: New testsuite. commit 126e89b660fad6b21f50c48e2af616225a727586 Author: Fritz Reese Date: Wed Sep 28 16:11:23 2016 -0400 New flag -fdec-math for COTAN and degree trig intrinsics. gcc/fortran/ * lang.opt: New flag -fdec-math. * options.c (set_dec_flags): Enable with -fdec. * invoke.texi, gfortran.texi, intrinsic.texi: Update documentation. * intrinsics.c (add_functions, do_simplify): New intrinsics with -fdec-math. * gfortran.h (gfc_isym_id): New isym GFC_ISYM_COTAN. * gfortran.h (gfc_resolve_atan2d, gfc_resolve_cotan, gfc_resolve_trigd, gfc_resolve_atrigd): New prototypes. * iresolve.c (resolve_trig_call, get_degrees, get_radians, is_trig_resolved, gfc_resolve_cotan, gfc_resolve_trigd, gfc_resolve_atrigd, gfc_resolve_atan2d): New functions. * intrinsics.h (gfc_simplify_atan2d, gfc_simplify_atrigd, gfc_simplify_cotan, gfc_simplify_trigd): New prototypes. * simplify.c (simplify_trig_call, degrees_f, radians_f, gfc_simplify_cotan, gfc_simplify_trigd, gfc_simplify_atrigd, gfc_simplify_atan2d): New functions. gcc/testsuite/gfortran.dg/ * dec_math.f90: New testsuite. diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h index d6b92a6..f8f3d4a 100644 --- a/gcc/fortran/gfortran.h +++ b/gcc/fortran/gfortran.h @@ -391,6 +391,7 @@ enum gfc_isym_id GFC_ISYM_CONVERSION, GFC_ISYM_COS, GFC_ISYM_COSH, + GFC_ISYM_COTAN, GFC_ISYM_COUNT, GFC_ISYM_CPU_TIME, GFC_ISYM_CSHIFT, diff --git a/gcc/fortran/gfortran.texi b/gcc/fortran/gfortran.texi index 3ebe3c7..a11eb84 100644 --- a/gcc/fortran/gfortran.texi +++ b/gcc/fortran/gfortran.texi @@ -1466,6 +1466,7 @@ without warning. * Form feed as whitespace:: * TYPE as an alias for PRINT:: * %LOC as an rvalue:: +* Extended math intrinsics:: @end menu @node Old-style kind specifications @@ -2519,6 +2520,42 @@ integer :: i call sub(%loc(i)) @end smallexample +@node Extended math intrinsics +@subsection Extended math intrinsics +@cindex intrinsics, math +@cindex intrinsics, trigonometric functions + +GNU Fortran supports an extended list of mathematical intrinsics with the +compile flag @option{-fdec-math} for compatability with legacy code. +These intrinsics are described fully in @ref{Intrinsic Procedures} where it is +noted that they are extensions and should be avoided whenever possible. + +Specifically, @option{-fdec-math} enables the @ref{COTAN} intrinsic, and +trigonometric intrinsics which accept or produce values in degrees instead of +radians. Here is a summary of the new intrinsics: + +@multitable @columnfractions .5 .5 +@headitem Radians @tab Degrees +@item @code{@ref{ACOS}} @tab @code{@ref{ACOSD}}* +@item @code{@ref{ASIN}} @tab @code{@ref{ASIND}}* +@item @code{@ref{ATAN}} @tab @code{@ref{ATAND}}* +@item @code{@ref{ATAN2}} @tab @code{@ref{ATAN2D}}* +@item @code{@ref{COS}}@tab @code{@ref{COSD}}* +@item @code{@ref{COTAN}}* @tab @code{@ref{COTAND}}* +@item @code{@ref{SIN}}@tab
Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
On Mon, Sep 26, 2016 at 10:01:27AM -0400, Fritz Reese wrote: > > Attached is a patch extending the GNU Fortran front-end to support > some additional math intrinsics, enabled with a new compile flag > -fdec-math. The flag adds the COTAN intrinsic (cotangent), as well as > degree versions of all trigonometric intrinsics (SIND, TAND, ACOSD, > etc...). This extension allows for further compatibility with legacy > code that depends on the compiler to support such intrinsic functions. > I plan to review this patch over the weekend. Two things to consider. 1) The documentation should note that these intrinsics are for compatibility with legacy code and should strongly discourage their use in new code. 2) In regards to Joseph and Tobias' comments, the documentation should give a hint to the quality of implementation. Argument reduction can be a real pain and without a formal numerical analysis, I can imagine large ULP errors near zeros and infinities. I haven't looked at the implementation yet, but will suggest that REAL(4) should probably be simply written in terms of REAL(8), e.g., function sind(x) result(retval) real(4) retval real(4), intent(in) :: x retval = dsind(real(x, 8)) end function sind Yes, the layer of indirection and computations in REAL(8) will be slower, but you should have much improved accuracy. -- Steve
Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
On Mon, 26 Sep 2016, Tobias Burnus wrote: > Regarding the decimal trigonometric functions, the internet suggests to > use >sin (fmod ((x), 360) * M_PI / 180) > instead of >sin (x * M_PI / 180) > to yield better results for angles which are larger than +/-360 degrees. Actually for good results you should reduce to the interval +/-45 degrees (which may require swapping which of sin and cos is used, or between tangent and cotangent, and then adjusting the sign of the result, depending on the quadrant the original argument was in). Consider e.g. cos of an argument close to 90 degrees; subtracting 90 and taking -sin of the result converted to radians gives a good result, converting 90 directly to radians and taking cos of that yields large errors (in ulps) for such input. (Unfortunately MPFR doesn't have sinpi etc., or the variants sinu etc. suggested in its TODO file, which would be helpful for implementing these functions for constant arguments.) -- Joseph S. Myers jos...@codesourcery.com
Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
Tobias, Many thanks for the comments. I will adjust the patch according to your advice shortly. - Fritz On Mon, Sep 26, 2016, 11:59 Tobias Burnuswrote: > > Fritz Reese wrote: > > Attached is a patch extending the GNU Fortran front-end to support > > some additional math intrinsics, enabled with a new compile flag > > -fdec-math. The flag adds the COTAN intrinsic (cotangent), as well as > > degree versions of all trigonometric intrinsics (SIND, TAND, ACOSD, > > etc...). > > > > + /* There is no builtin mpc_cot, so compute x = 1 / tan (x). */ > > + val = >value.complex; > > + mpc_tan (*val, *val, GFC_MPC_RND_MODE); > > + mpc_div (*val, one, *val, GFC_MPC_RND_MODE); > > The internet remarks: TAN(x) for x -> pi/2 goes to +inf or -inf - while > COTAN(pi/2) == 0. For values near pi/2, using cos(x)/sin(x) should be > numerically better than 1/tan(x). [Cf. mpfr_sin_cos and BUILT_IN_SINCOS.] > > I am not sure how big the prevision difference really is. A quick test > showed results which didn't look to bad: > > implicit none > real(8), volatile :: x > x = 3.14159265358d0/2.0d0 > print '(g0)', cotan(x), cos(x)/sin(x), cotan(x)-cos(x)/sin(x) > end > > yields: > > 0.48965888601467483E-011 > 0.48965888601467475E-011 > 0.80779356694631609E-027 > > Usint N[1/Tan[314159265358/10^11/2],200], Mathematica shows > 4.8966192313216916397514812338... * 10^12. > I am not quite sure whether I should expect that already the 5th digit > differs from the results above. > > > > mpfr_set_d (tmp, 180.0l, GFC_RND_MODE); > > With some fonts the L after 180.0 looks like a one; I was initially > puzzled why one uses not 180 but 180.01. Hence, I'd prefer an "L" instead > of an "l". > > However, the second argument of mpfr_set_d is a "double" and 180.0 is > already a double. Hence, one can even completely get rid of the "l". > > > > Regarding the decimal trigonometric functions, the internet suggests to > use >sin (fmod ((x), 360) * M_PI / 180) > instead of >sin (x * M_PI / 180) > to yield better results for angles which are larger than +/-360 degrees. > > > Cheers, > > Tobias
Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
On 09/26/2016 04:01 PM, Fritz Reese wrote: All, Attached is a patch extending the GNU Fortran front-end to support some additional math intrinsics, enabled with a new compile flag -fdec-math. The flag adds the COTAN intrinsic (cotangent), as well as degree versions of all trigonometric intrinsics (SIND, TAND, ACOSD, etc...). This extension allows for further compatibility with legacy code that depends on the compiler to support such intrinsic functions. Don't you want an option name like -fdegree-trigon-math ? Note that option name lengths are hardly a problem, as most end up in scripts and make files ... Whether support for COTAN (in radians) should be part of this option - I rather had it not. Kind regards, -- Toon Moene - e-mail: t...@moene.org - phone: +31 346 214290 Saturnushof 14, 3738 XG Maartensdijk, The Netherlands At home: http://moene.org/~toon/; weather: http://moene.org/~hirlam/ Progress of GNU Fortran: http://gcc.gnu.org/wiki/GFortran#news
Re: [PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
Fritz Reese wrote: > Attached is a patch extending the GNU Fortran front-end to support > some additional math intrinsics, enabled with a new compile flag > -fdec-math. The flag adds the COTAN intrinsic (cotangent), as well as > degree versions of all trigonometric intrinsics (SIND, TAND, ACOSD, > etc...). > > + /* There is no builtin mpc_cot, so compute x = 1 / tan (x). */ > + val = >value.complex; > + mpc_tan (*val, *val, GFC_MPC_RND_MODE); > + mpc_div (*val, one, *val, GFC_MPC_RND_MODE); The internet remarks: TAN(x) for x -> pi/2 goes to +inf or -inf - while COTAN(pi/2) == 0. For values near pi/2, using cos(x)/sin(x) should be numerically better than 1/tan(x). [Cf. mpfr_sin_cos and BUILT_IN_SINCOS.] I am not sure how big the prevision difference really is. A quick test showed results which didn't look to bad: implicit none real(8), volatile :: x x = 3.14159265358d0/2.0d0 print '(g0)', cotan(x), cos(x)/sin(x), cotan(x)-cos(x)/sin(x) end yields: 0.48965888601467483E-011 0.48965888601467475E-011 0.80779356694631609E-027 Usint N[1/Tan[314159265358/10^11/2],200], Mathematica shows 4.8966192313216916397514812338... * 10^12. I am not quite sure whether I should expect that already the 5th digit differs from the results above. > mpfr_set_d (tmp, 180.0l, GFC_RND_MODE); With some fonts the L after 180.0 looks like a one; I was initially puzzled why one uses not 180 but 180.01. Hence, I'd prefer an "L" instead of an "l". However, the second argument of mpfr_set_d is a "double" and 180.0 is already a double. Hence, one can even completely get rid of the "l". Regarding the decimal trigonometric functions, the internet suggests to use sin (fmod ((x), 360) * M_PI / 180) instead of sin (x * M_PI / 180) to yield better results for angles which are larger than +/-360 degrees. Cheers, Tobias
[PATCH, Fortran] Extension: COTAN and degree-valued trig intrinsics with -fdec-math
All, Attached is a patch extending the GNU Fortran front-end to support some additional math intrinsics, enabled with a new compile flag -fdec-math. The flag adds the COTAN intrinsic (cotangent), as well as degree versions of all trigonometric intrinsics (SIND, TAND, ACOSD, etc...). This extension allows for further compatibility with legacy code that depends on the compiler to support such intrinsic functions. I would definitely like some peer review on this patch to ensure my implementation has no unintended mathematical side-effects, such as rounding errors, or oversights with complex math: I implement the degree-valued functions by simply converting the argument or result to degrees/radians as appropriate by multiplying by the factor pi/180 (or 180/pi). In the constant case this is done using mpfr/mpc, and in the non-constant case the factor multiplication expression is just inserted into the expression tree where appropriate. The new COTAN intrinsic is implemented using mpfr_cot in the constant case, and by replacing the call with a 1/tan expression in the non-constant case (and in the complex case, since there is no mpc_cot). See the new functions in simplify.c/iresolve.c for specifics. I also added a new macro MATH_ALIAS_BUILTIN used in mathbuiltins.def to define the new COTAN intrinsic. This macro defines a new isym, but keeps everything else defined normally by DEFINE_MATH_BUILTIN the same as an existing isym (TAN in this case). The reason for this is to avoid having to define a new BUILT_IN_COTAN in gcc/builtins.def (or any other context). As I described above, the simplify/resolve code for COTAN just wraps TAN. I am not 100% convinced this is the "right" or "best" way to do this - so feel free to me know if there are any problems with this approach. By the way, I don't use intrinsic.c (make_alias) for these because they need their own simplification/resolution routines to handle the degree-radian conversion. As usual the patched compiler bootstraps and regtests on x86_64-redhat-linux. Questions, comments and concerns are very welcome. Barring any, is this OK for trunk? --- Fritz Reese 2016-09-26 Fritz ReeseNew flag -fdec-math for COTAN and degree trig intrinsics. gcc/fortran/ * lang.opt: New flag -fdec-math. * options.c (set_dec_flags): Enable with -fdec. * invoke.texi, gfortran.texi, intrinsic.texi: Update documentation. * trans-intrinsic.c (MATH_ALIAS_BUILTLIN): New macro for math aliases. * f95-lang.c (MATH_ALIAS_BUILTIN): New macro for math aliases. * mathbuiltins.def (MATH_ALIAS_BUILTIN): Use for cotan. * intrinsics.c (add_functions, do_simplify): New intrinsics with -fdec-math. * gfortran.h (gfc_isym_id): New isym GFC_ISYM_COTAN. * gfortran.h (gfc_resolve_atan2d, gfc_resolve_cotan, gfc_resolve_trigd, gfc_resolve_atrigd): New prototypes. * iresolve.c (resolve_trig_call, get_degrees, get_radians, is_trig_resolved, gfc_resolve_cotan, gfc_resolve_trigd, gfc_resolve_atrigd, gfc_resolve_atan2d): New functions. * intrinsics.h (gfc_simplify_atan2d, gfc_simplify_atrigd, gfc_simplify_cotan, gfc_simplify_trigd): New prototypes. * simplify.c (simplify_trig_call, degrees_f, radians_f, gfc_simplify_cotan, gfc_simplify_trigd, gfc_simplify_atrigd, gfc_simplify_atan2d): New functions. gcc/testsuite/gfortran.dg/ * dec_math.f90: New testsuite. diff --git a/gcc/fortran/f95-lang.c b/gcc/fortran/f95-lang.c index 2b58173..7229d8d 100644 --- a/gcc/fortran/f95-lang.c +++ b/gcc/fortran/f95-lang.c @@ -559,6 +559,8 @@ gfc_define_builtin (const char *name, tree type, enum built_in_function code, DO_DEFINE_MATH_BUILTIN (code, name, argtype, mfunc_) \ DO_DEFINE_MATH_BUILTIN (C##code, "c" name, argtype, mfunc_c) +/* Defined in trans-intrinsic.c. */ +#define MATH_ALIAS_BUILTIN(newid, id, name, type) /* Create function types for builtin functions. */ @@ -1244,6 +1246,7 @@ gfc_init_builtin_functions (void) targetm.init_builtins (); } +#undef MATH_ALIAS_BUILTIN #undef DEFINE_MATH_BUILTIN_C #undef DEFINE_MATH_BUILTIN diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h index d6b92a6..f8f3d4a 100644 --- a/gcc/fortran/gfortran.h +++ b/gcc/fortran/gfortran.h @@ -391,6 +391,7 @@ enum gfc_isym_id GFC_ISYM_CONVERSION, GFC_ISYM_COS, GFC_ISYM_COSH, + GFC_ISYM_COTAN, GFC_ISYM_COUNT, GFC_ISYM_CPU_TIME, GFC_ISYM_CSHIFT, diff --git a/gcc/fortran/gfortran.texi b/gcc/fortran/gfortran.texi index 3ebe3c7..4f8d2d6 100644 --- a/gcc/fortran/gfortran.texi +++ b/gcc/fortran/gfortran.texi @@ -1466,6 +1466,7 @@ without warning. * Form feed as whitespace:: * TYPE as an alias for PRINT:: * %LOC as an rvalue:: +* Extended math intrinsics:: @end menu @node Old-style kind specifications @@ -2519,6 +2520,33 @@ integer :: i call sub(%loc(i)) @end smallexample +@node Extended