Patchwork libquadmath string <-> __float128 API changes (PR fortran/46625)

login
register
mail settings
Submitter Jakub Jelinek
Date Jan. 7, 2011, 3:13 p.m.
Message ID <20110107151355.GC16156@tyan-ft48-01.lab.bos.redhat.com>
Download mbox | patch
Permalink /patch/77886/
State New
Headers show

Comments

Jakub Jelinek - Jan. 7, 2011, 3:13 p.m.
Hi!

This patch renames the string <-> __float128 functions to something that
looks a tiny bit saner to me.
For string -> __float128 conversion it also changes API to look similarly
to strtof/strtod/strtold (later on we should change it internally to
also handle C99 hex floating point format).
The __float128 -> string routine doesn't have a nice standard counterpart,
ecvt/fcvt are both obsolete and not very usable, and the current routine
isn't very flexible, so for now it keeps that under a quadmath_ prefix
as a routine usable mainly in libgfortran.
For C/C++ use best would be to let libquadmath constructor register
printf hooks, so that printf ("%Qe %.43Qf %Qg %Qa", ...) etc.
just work (assuming Q is a good modified for __float128).  That would
be glibc specific (of course could be checked during configure), not sure
if other OSes have something similar.  We could follow libdfp example here,
which also registers printf hooks for the _Decimal* types.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2011-01-07  Jakub Jelinek  <jakub@redhat.com>

	PR fortran/46625
	* quadmath.map (QUADMATH_1.0): Remove quadmath_strtopQ
	and quadmath_dtoaq.  Add strtoflt128 and quadmath_flt128tostr.
	* quadmath_weak.h (quadmath_strtopQ, quadmath_dtoaq): Remove.
	(strtoflt128, quadmath_flt128tostr): Add.
	* gdtoa/strtopQ.c (quadmath_strtopQ): Rename to...
	(strtoflt128): ... this.  Return __float128, instead of writing
	to memory pointed by last argument.
	* quadmath.h: Use C style comments instead of C++ style.
	(quadmath_strtopQ, quadmath_dtoaq): Remove prototypes.
	(strtoflt128, quadmath_flt128tostr): Add prototypes.
	* libquadmath.texi (quadmath_dtoaq): Rename to quadmath_flt128tostr.
	(quadmath_strtopQ): Rename to strtoflt128.  Adjust prototype,
	adjust examples.
	* quadmath_io.c (quadmath_dtoaq): Rename to...
	(quadmath_flt128tostr): ... this.
libgfortran/
	* io/write_float.def (DTOAQ): Use quadmath_flt128tostr
	instead of quadmath_dtoa.
	* io/transfer128.c (tmp1, tmp2): New variables, bring in
	strtoflt128 and quadmath_flt128tostr.
	(transfer_real128, transfer_real128_write, transfer_complex128,
	transfer_complex128_write): Remove tmp1/tmp2 variables.
	* io/read.c (convert_real): Use strtoflt128 instead of
	quadmath_strtopQ, adjust for the changed arguments and return
	value.


	Jakub

Patch

--- libquadmath/quadmath.map.jj	2011-01-06 20:07:14.000000000 +0100
+++ libquadmath/quadmath.map	2011-01-07 11:38:02.000000000 +0100
@@ -65,6 +65,7 @@  QUADMATH_1.0 {
     y0q;
     y1q;
     ynq;
+
     cabsq;
     cargq;
     cimagq;
@@ -89,8 +90,9 @@  QUADMATH_1.0 {
     csqrtq;
     ctanq;
     ctanhq;
-    quadmath_strtopQ;
-    quadmath_dtoaq;
+
+    strtoflt128;
+    quadmath_flt128tostr;
   local:
     *;
 };
--- libquadmath/quadmath_weak.h.jj	2011-01-06 19:34:57.000000000 +0100
+++ libquadmath/quadmath_weak.h	2011-01-07 11:45:33.000000000 +0100
@@ -130,8 +130,8 @@  __qmath3 (ctanq)
 __qmath3 (ctanhq)
 
 
-// Prototypes for our I/O functions
-__qmath3 (quadmath_strtopQ)
-__qmath3 (quadmath_dtoaq)
+// Prototypes for string <-> flt128 conversion functions
+__qmath3 (strtoflt128)
+__qmath3 (quadmath_flt128tostr)
 
 #endif
--- libquadmath/gdtoa/strtopQ.c.jj	2010-11-19 20:56:37.000000000 +0100
+++ libquadmath/gdtoa/strtopQ.c	2011-01-07 11:44:29.000000000 +0100
@@ -49,18 +49,15 @@  THIS SOFTWARE.
 #define _3 0
 #endif
 
- int
-#ifdef KR_headers
-quadmath_strtopQ(s, sp, V) CONST char *s; char **sp; void *V;
-#else
-quadmath_strtopQ(CONST char *s, char **sp, void *V)
-#endif
+__float128
+strtoflt128(CONST char *s, char **sp)
 {
 	static FPI fpi0 = { 113, 1-16383-113+1, 32766 - 16383 - 113 + 1, 1, SI };
 	ULong bits[4];
 	Long exp;
 	int k;
-	ULong *L = (ULong*)V;
+	union { __float128 f; ULong L[4]; } u;
+	ULong *L = &u.L[0];
 #ifdef Honor_FLT_ROUNDS
 #include "gdtoa_fltrnds.h"
 #else
@@ -102,5 +99,5 @@  quadmath_strtopQ(CONST char *s, char **s
 	  }
 	if (k & STRTOG_Neg)
 		L[_0] |= 0x80000000L;
-	return k;
-	}
+	return u.f;
+}
--- libquadmath/quadmath.h.jj	2011-01-07 10:43:03.000000000 +0100
+++ libquadmath/quadmath.h	2011-01-07 11:37:30.000000000 +0100
@@ -23,8 +23,8 @@  Boston, MA 02110-1301, USA.  */
 
 #include <stdlib.h>
 
-// Define the complex type corresponding to __float128
-// ("_Complex __float128" is not allowed)
+/* Define the complex type corresponding to __float128
+   ("_Complex __float128" is not allowed) */
 typedef _Complex float __attribute__((mode(TC))) __complex128;
 
 #ifdef __cplusplus
@@ -35,7 +35,7 @@  typedef _Complex float __attribute__((mo
 # define __quadmath_nth(fct) __attribute__((__nothrow__)) fct
 #endif
 
-// Prototypes for real functions
+/* Prototypes for real functions */
 extern __float128 acosq (__float128) __quadmath_throw;
 extern __float128 acoshq (__float128) __quadmath_throw;
 extern __float128 asinq (__float128) __quadmath_throw;
@@ -103,7 +103,7 @@  extern __float128 y1q (__float128) __qua
 extern __float128 ynq (int, __float128) __quadmath_throw;
 
 
-// Prototypes for complex functions
+/* Prototypes for complex functions */
 extern __float128 cabsq (__complex128) __quadmath_throw;
 extern __float128 cargq (__complex128) __quadmath_throw;
 extern __float128 cimagq (__complex128) __quadmath_throw;
@@ -130,13 +130,13 @@  extern __complex128 ctanq (__complex128)
 extern __complex128 ctanhq (__complex128) __quadmath_throw;
 
 
-// Prototypes for our I/O functions
-extern int quadmath_strtopQ (const char *, char **, void *) __quadmath_throw;
-extern void quadmath_dtoaq (char *, size_t, size_t, __float128)
+/* Prototypes for string <-> __float128 conversion functions */
+extern __float128 strtoflt128 (const char *, char **) __quadmath_throw;
+extern void quadmath_flt128tostr (char *, size_t, size_t, __float128)
   __quadmath_throw;
 
 
-// Macros
+/* Macros */
 #define FLT128_MAX 1.18973149535723176508575932662800702e4932Q
 #define FLT128_MIN 3.36210314311209350626267781732175260e-4932Q
 #define FLT128_EPSILON 1.92592994438723585305597794258492732e-34Q
@@ -144,8 +144,8 @@  extern void quadmath_dtoaq (char *, size
 #define FLT128_MANT_DIG 113
 #define FLT128_MIN_EXP (-16381)
 #define FLT128_MAX_EXP 16384
-// TODO -- One day, we need to add the following macros:
-// FLT128_DIG, FLT128_MIN_10_EXP, FLT128_MAX_10_EXP
+/* TODO -- One day, we need to add the following macros:
+   FLT128_DIG, FLT128_MIN_10_EXP, FLT128_MAX_10_EXP */
 
 
 #define HUGE_VALQ __builtin_huge_valq()
--- libquadmath/libquadmath.texi.jj	2011-01-06 19:46:40.000000000 +0100
+++ libquadmath/libquadmath.texi	2011-01-07 11:49:20.000000000 +0100
@@ -198,20 +198,20 @@  The following mathematical functions are
 @chapter I/O Library Routines
 
 @menu
-* @code{quadmath_strtopQ}:  quadmath_strtopQ,  Convert from string
-* @code{quadmath_dtoaq}:    quadmath_dtoaq,    Convert to string
+* @code{strtoflt128}:          strtoflt128,          Convert from string
+* @code{quadmath_flt128tostr}: quadmath_flt128tostr, Convert to string
 @end menu
 
 
-@node quadmath_strtopQ
-@section @code{quadmath_strtopQ} --- Convert from string
+@node strtoflt128
+@section @code{strtoflt128} --- Convert from string
 
 The function @code{dmath_strtopQ} converts a string into a
 @code{__float128} number.
 
 @table @asis
 @item Syntax
-@code{int quadmath_strtopQ (const char *s, char **sp, void *V)}
+@code{__float128 strtoflt128 (const char *s, char **sp)}
 
 @c The return values are defined in gdtoa/gdtoa.h STRTOG_*
 @c However, the values are currently not exported - thus we
@@ -221,7 +221,6 @@  The function @code{dmath_strtopQ} conver
 @multitable @columnfractions .15 .70
 @item @var{s}  @tab input string
 @item @var{sp} @tab the address of the next character in the string
-@item @var{V}  @tab @code{__float128} containing the converted number
 @end multitable
 
 The argument @var{sp} contains, if not @code{NULL}, the address of the
@@ -234,9 +233,8 @@  next character following the parts of th
 int main ()
 @{
   __float128 r;
-  char str[200];
 
-  quadmath_strtopQ ("1.2345678", NULL, &r);
+  r = strtoflt128 ("1.2345678", NULL);
 
   return 0;
 @}
@@ -244,15 +242,15 @@  int main ()
 @end table
 
 
-@node quadmath_dtoaq
-@section @code{quadmath_dtoaq} --- Convert to string
+@node quadmath_flt128tostr
+@section @code{quadmath_flt128tostr} --- Convert to string
 
-The function @code{quadmath_dtoaq} converts a @code{__float128} floating-point
+The function @code{quadmath_flt128tostr} converts a @code{__float128} floating-point
 number into a string.
 
 @table @asis
 @item Syntax
-@code{void quadmath_dtoaq (char *s, size_t size, size_t n, __float128 x)}
+@code{void quadmath_flt128tostr (char *s, size_t size, size_t n, __float128 x)}
 
 @item @emph{Arguments}:
 @multitable @columnfractions .15 .70
@@ -273,7 +271,7 @@  int main ()
 
   r = 2.0q;
   r = sqrtq(r);
-  quadmath_dtoaq (str, sizeof (str), 20, r);
+  quadmath_flt128tostr (str, sizeof (str), 20, r);
   printf("%s\n", str);
   /* Prints: +1.41421356237309504880e+00 */
   return 0;
--- libquadmath/quadmath_io.c.jj	2010-12-07 17:32:50.000000000 +0100
+++ libquadmath/quadmath_io.c	2011-01-07 11:38:31.000000000 +0100
@@ -101,7 +101,7 @@  format (char * res, const __float128 x, 
 
 
 void
-quadmath_dtoaq (char *s, size_t size, size_t n, __float128 x)
+quadmath_flt128tostr (char *s, size_t size, size_t n, __float128 x)
 {
   char buffer[1024];
   memset (buffer, 0, sizeof(buffer));
--- libgfortran/io/write_float.def.jj	2010-11-19 20:56:57.000000000 +0100
+++ libgfortran/io/write_float.def	2011-01-07 11:54:23.000000000 +0100
@@ -975,7 +975,7 @@  sprintf (buffer, "%+-#" STR(MIN_FIELD_WI
 
 #if defined(GFC_REAL_16_IS_FLOAT128)
 #define DTOAQ \
-__qmath_(quadmath_dtoaq) (buffer, size, ndigits - 1, tmp);
+__qmath_(quadmath_flt128tostr) (buffer, size, ndigits - 1, tmp);
 #endif
 
 #define WRITE_FLOAT(x,y)\
--- libgfortran/io/transfer128.c.jj	2010-11-19 20:56:57.000000000 +0100
+++ libgfortran/io/transfer128.c	2011-01-07 11:56:08.000000000 +0100
@@ -61,18 +61,17 @@  extern void transfer_complex128_write (s
 export_proto(transfer_complex128_write);
 
 
-/* Make sure that libquadmath is pulled in. The functions quadmath_strtopQ
-   and quadmath_dtoaq are weakly referrenced in convert_real and write_float;
-   the pointer assignment with USED attribute make sure that there is a
-   non-weakref dependence if the quadmath functions are used. That avoids
-   segfault when libquad is statically linked.  */
+/* Make sure that libquadmath is pulled in. The functions strtoflt128
+   and quadmath_flt128tostr are weakly referrenced in convert_real and
+   write_float; the pointer assignment with USED attribute make sure
+   that there is a non-weakref dependence if the quadmath functions
+   are used. That avoids segfault when libquadmath is statically linked.  */
+static void __attribute__((used)) *tmp1 = strtoflt128;
+static void __attribute__((used)) *tmp2 = quadmath_flt128tostr;
 
 void
 transfer_real128 (st_parameter_dt *dtp, void *p, int kind)
 {
-  static void __attribute__((used)) *tmp1 = quadmath_strtopQ;
-  static void __attribute__((used)) *tmp2 = quadmath_dtoaq;
-
   transfer_real (dtp, p, kind);
 }
 
@@ -80,9 +79,6 @@  transfer_real128 (st_parameter_dt *dtp, 
 void
 transfer_real128_write (st_parameter_dt *dtp, void *p, int kind)
 {
-  static void __attribute__((used)) *tmp1 = quadmath_strtopQ;
-  static void __attribute__((used)) *tmp2 = quadmath_dtoaq;
-
   transfer_real (dtp, p, kind);
 }
 
@@ -90,9 +86,6 @@  transfer_real128_write (st_parameter_dt 
 void
 transfer_complex128 (st_parameter_dt *dtp, void *p, int kind)
 {
-  static void __attribute__((used)) *tmp1 = quadmath_strtopQ;
-  static void __attribute__((used)) *tmp2 = quadmath_dtoaq;
-
   transfer_complex (dtp, p, kind);
 }
 
@@ -100,9 +93,6 @@  transfer_complex128 (st_parameter_dt *dt
 void
 transfer_complex128_write (st_parameter_dt *dtp, void *p, int kind)
 {
-/*  static void __attribute__((used)) *tmp1 = quadmath_strtopQ;
-  static void __attribute__((used)) *tmp2 = quadmath_dtoaq;*/
-
   transfer_complex_write (dtp, p, kind);
 }
 #endif
--- libgfortran/io/read.c.jj	2010-11-19 20:56:57.000000000 +0100
+++ libgfortran/io/read.c	2011-01-07 11:54:00.000000000 +0100
@@ -165,7 +165,7 @@  convert_real (st_parameter_dt *dtp, void
 #if defined(HAVE_GFC_REAL_16)
 # if defined(GFC_REAL_16_IS_FLOAT128)
     case 16:
-      __qmath_(quadmath_strtopQ) (buffer, NULL, dest);
+      *((GFC_REAL_16*) dest) = __qmath_(strtoflt128) (buffer, NULL);
       break;
 # elif defined(HAVE_STRTOLD)
     case 16: