Re: [PATCH, libgfortran] Protect the trigd functions in libgfortran from unavailable math functions [PR94586, PR94694]

2020-04-23 Thread Jakub Jelinek via Gcc-patches
On Wed, Apr 22, 2020 at 01:13:47PM -0400, Fritz Reese via Gcc-patches wrote:
> Jakub has OK'd the patch and I recently authored the original trigd

My patch is already in.

> code being modified here. After his patch is committed I will commit
> this one unless others have comments or concerns.
> 
> libgfortran/ChangeLog:
> 
> 2020-04-22  Fritz Reese  
> 
> * intrinsics/trigd.c, intrinsics/trigd_lib.inc, intrinsics/trigd.inc:
> Guard against unavailable math functions.
> Use suffixes from kinds.h based on the REAL kind.
> 
> gcc/fortran/ChangeLog:
> 
> 2020-04-22  Fritz Reese  
> 
> * trigd_fe.inc: Use mpfr to compute cosd(30) rather than a host-
> precision floating point literal based on an invalid macro.

Ok for trunk, thanks.

Jakub



[PATCH, libgfortran] Protect the trigd functions in libgfortran from unavailable math functions [PR94586, PR94694]

2020-04-22 Thread Fritz Reese via Gcc-patches
This patch here is a follow-on to Jakub's in his message "[PATCH]
libgfortran: Provide some further math library fallbacks [PR94694]" at
https://gcc.gnu.org/pipermail/fortran/2020-April/054252.html.

I think this should be committed along with Jakub's patch in response
to PRs 94586 and 94694. Though this patch alone does not fix either
PR, it prevents the build from failing when the degree-valued
trigonometric functions are ultimately unavailable on the target due
to lack of math library builtins.

If HAVE_* is not defined for the math functions required by the trigd
implementations, the intrinsic functions are built only to throw a
runtime_error. Prior to this, the libgfortran build would fail
completely. These intrinsics are extensions, and before the original
trigd patch the library could build even when these math functions
were missing. Therefore, I think the compiler should not fail to build
simply because the intrinsics are unsupported on the target.

In theory, this patch would also fix the trigd build for targets which
don't have a floating-point INFINITY defined, though I am unsure if
any such targets exist.

Furthermore, this patch replaces hard-coded floating point literal
suffixes with the use of GFC_REAL__LITERAL_SUFFIX, as defined by
libgfortran's kinds.h. This could help targets with quirky definitions
for real(16) such as HPUX, as in PR 94586. Similarly, the precision
used for the constants COSD_SMALL, COSD30, et al. for the REAL(16)
case are derived from the precision of GFC_REAL_16 rather than its
long-double-ness, since for example on HPUX long double is actually
128 bits.

This patch also fixes an issue which could have occurred in the
front-end: the front-end attemped to use a host-sized floating-point
literal to represent cosine of 30 degrees. This is of course
nonsensical. The patch replaces this with the use of mpfr functions to
compute the value as sqrt(3)/2 to the correct precision. I include
this in the patch because it is also a target-specific issue with the
degree-valued trigonometric intrinsics.

Jakub has OK'd the patch and I recently authored the original trigd
code being modified here. After his patch is committed I will commit
this one unless others have comments or concerns.

libgfortran/ChangeLog:

2020-04-22  Fritz Reese  

* intrinsics/trigd.c, intrinsics/trigd_lib.inc, intrinsics/trigd.inc:
Guard against unavailable math functions.
Use suffixes from kinds.h based on the REAL kind.

gcc/fortran/ChangeLog:

2020-04-22  Fritz Reese  

* trigd_fe.inc: Use mpfr to compute cosd(30) rather than a host-
precision floating point literal based on an invalid macro.

---
Fritz Reese

[Note: the only differences between this patch and the one in the PR
is whitespace.]
commit dd9c65c7338ceed5ef30e768e4530d73141918c5
Author: Fritz Reese 
Date:   Wed Apr 22 11:45:22 2020 -0400

Follow-on to: Provide some further math library fallbacks [PR94694]

Protect the trigd functions in libgfortran from unavailable math functions.

libgfortran/ChangeLog:

2020-04-22  Fritz Reese  

* intrinsics/trigd.c, intrinsics/trigd_lib.inc, intrinsics/trigd.inc:
Guard against unavailable math functions.
Use suffixes from kinds.h based on the REAL kind.

gcc/fortran/ChangeLog:

2020-04-22  Fritz Reese  

* trigd_fe.inc: Use mpfr to compute cosd(30) rather than a host-
precision floating point literal based on an invalid macro.

diff --git a/gcc/fortran/trigd_fe.inc b/gcc/fortran/trigd_fe.inc
index 78ca4416a21..f94c36773c1 100644
--- a/gcc/fortran/trigd_fe.inc
+++ b/gcc/fortran/trigd_fe.inc
@@ -29,17 +29,20 @@ along with GCC; see the file COPYING3.  If not see
 #define ISFINITE(x) mpfr_number_p(x)
 #define D2R(x) deg2rad(x)
 
+#define ENABLE_SIND
+#define ENABLE_COSD
+#define ENABLE_TAND
+
 #define SIND simplify_sind
 #define COSD simplify_cosd
 #define TAND simplify_tand
 
-#ifdef HAVE_GFC_REAL_16
-#define COSD30 8.66025403784438646763723170752936183e-01Q
-#else
-#define COSD30 8.66025403784438646763723170752936183e-01L
-#endif
-
-#define SET_COSD30(x) mpfr_set_ld((x), COSD30, GFC_RND_MODE)
+/* cosd(30) === sqrt(3) / 2.  */
+#define SET_COSD30(x) do { \
+mpfr_set_ui (x, 3, GFC_RND_MODE); \
+mpfr_sqrt (x, x, GFC_RND_MODE); \
+mpfr_div_ui (x, x, 2, GFC_RND_MODE); \
+  } while (0)
 
 static RETTYPE SIND (FTYPE);
 static RETTYPE COSD (FTYPE);
diff --git a/libgfortran/intrinsics/trigd.c b/libgfortran/intrinsics/trigd.c
index 81699069545..e1c51c7b2ef 100644
--- a/libgfortran/intrinsics/trigd.c
+++ b/libgfortran/intrinsics/trigd.c
@@ -27,6 +27,14 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
 #include 
 
+/* Body of library functions which are cannot be implemented on the current
+ * platform because it lacks a capability, such as an underlying trigonometric
+ * function (sin, cos, tan) or C99 floating-point function (fabs, fmod). */