public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] frexpl: Support smaller long double.
@ 2021-12-02  8:07 Paul Zimmermann
  2021-12-02  9:50 ` Takashi Yano
  0 siblings, 1 reply; 3+ messages in thread
From: Paul Zimmermann @ 2021-12-02  8:07 UTC (permalink / raw)
  To: newlib; +Cc: Vincent.Lefevre

about https://sourceware.org/pipermail/newlib/2021/018738.html:

> This patch add support for smaller long double, LDBL_MANT_DIG is 24 or 53.

are you sure LDBL_MANT_DIG can be 24 in radix 2? As far as I know, the standard
requires LDBL_DIG >= 10, and a floating-point number with LDBL_DIG decimal
digits should be converted to a long double and back to LDBL_DIG digits
without any modification, which requires LDBL_MANT_DIG >= 34.

Also the standard says in 6.2.5: "the set of values of the
type double is a subset of the set of values of the type long double."

Paul Zimmermann

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] frexpl: Support smaller long double.
  2021-12-02  8:07 [PATCH] frexpl: Support smaller long double Paul Zimmermann
@ 2021-12-02  9:50 ` Takashi Yano
  0 siblings, 0 replies; 3+ messages in thread
From: Takashi Yano @ 2021-12-02  9:50 UTC (permalink / raw)
  To: newlib

On Thu, 02 Dec 2021 09:07:43 +0100
Paul Zimmermann wrote:
> about https://sourceware.org/pipermail/newlib/2021/018738.html:
> 
> > This patch add support for smaller long double, LDBL_MANT_DIG is 24 or 53.
> 
> are you sure LDBL_MANT_DIG can be 24 in radix 2? As far as I know, the standard
> requires LDBL_DIG >= 10, and a floating-point number with LDBL_DIG decimal
> digits should be converted to a long double and back to LDBL_DIG digits
> without any modification, which requires LDBL_MANT_DIG >= 34.
> 
> Also the standard says in 6.2.5: "the set of values of the
> type double is a subset of the set of values of the type long double."

Thanks for pointing out this.

I now have read ISO/IEC9899 standard draft, and confirm that it requires
LDBL_DIG >= 10. It also says DBL_DIG >= 10. If this means LDBL_MANT_DIG
and DBL_MANT_DIG >= 34, double also should not be DBL_MANT_DIG == 24.

However, newlib supports _DOUBLE_IS_32BITS. I guess some platfors
does not comply with the standard. So I consider long double can be
smaller in such platforms. However if both double and long double are
32bit, frexpl() calls frexp() (double version). So this patch does more
than necessary.

I will submit v2 patch, which removes following LDBM_MANT_DIG == 24
case.

+# if (LDBL_MANT_DIG == 24) /* 32-bit long double */
+static const double scale = 0x1p25;
+
+union ldbl {
+  long double x;
+  struct {
+#  ifdef __IEEE_LITTLE_ENDIAN /* for Intel CPU */
+    __uint32_t frac:23;
+    __uint32_t exp:8;
+    __uint32_t sign:1;
+#  endif
+#  ifdef __IEEE_BIG_ENDIAN
+#   ifndef ___IEEE_BYTES_LITTLE_ENDIAN
+    __uint32_t sign:1;
+    __uint32_t exp:8;
+    __uint32_t frac:23;
+#   else /* ARMEL without __VFP_FP__ */
+    __uint32_t frac:23;
+    __uint32_t exp:8;
+    __uint32_t sign:1;
+#   endif
+#  endif
+  } u32;
+};

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] frexpl: Support smaller long double.
@ 2021-12-01 19:34 Takashi Yano
  0 siblings, 0 replies; 3+ messages in thread
From: Takashi Yano @ 2021-12-01 19:34 UTC (permalink / raw)
  To: newlib

- Currently, frexpl() supports only the following cases.
    1) LDBL_MANT_DIG == 64 or 113
    2) 'long double' is equivalent to 'double'
  This patch add support for smaller long double, LDBL_MANT_DIG is
  24 or 53.
---
 newlib/libm/common/frexpl.c | 78 +++++++++++++++++++++++++++++++------
 1 file changed, 66 insertions(+), 12 deletions(-)

diff --git a/newlib/libm/common/frexpl.c b/newlib/libm/common/frexpl.c
index 22c811689..8fc6b4a9c 100644
--- a/newlib/libm/common/frexpl.c
+++ b/newlib/libm/common/frexpl.c
@@ -33,16 +33,66 @@ POSSIBILITY OF SUCH DAMAGE.
 #include "local.h"
 
 /* On platforms where long double is as wide as double.  */
-#if defined(_LDBL_EQ_DBL) || (LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024)
+#if defined(_LDBL_EQ_DBL)
 long double
 frexpl (long double x, int *eptr)
 {
   return frexp(x, eptr);
 }
-#endif
+#else  /* !_DBL_EQ_DBL */
+# if (LDBL_MANT_DIG == 24) /* 32-bit long double */
+static const double scale = 0x1p25;
+
+union ldbl {
+  long double x;
+  struct {
+#  ifdef __IEEE_LITTLE_ENDIAN /* for Intel CPU */
+    __uint32_t frac:23;
+    __uint32_t exp:8;
+    __uint32_t sign:1;
+#  endif
+#  ifdef __IEEE_BIG_ENDIAN
+#   ifndef ___IEEE_BYTES_LITTLE_ENDIAN
+    __uint32_t sign:1;
+    __uint32_t exp:8;
+    __uint32_t frac:23;
+#   else /* ARMEL without __VFP_FP__ */
+    __uint32_t frac:23;
+    __uint32_t exp:8;
+    __uint32_t sign:1;
+#   endif
+#  endif
+  } u32;
+};
+# elif (LDBL_MANT_DIG == 53) /* 64-bit long double */
+static const double scale = 0x1p54;
+
+union ldbl {
+  long double x;
+  struct {
+#  ifdef __IEEE_LITTLE_ENDIAN /* for Intel CPU */
+    __uint32_t fracl;
+    __uint32_t frach:20;
+    __uint32_t exp:11;
+    __uint32_t sign:1;
+#  endif
+#  ifdef __IEEE_BIG_ENDIAN
+    __uint32_t sign:1;
+    __uint32_t exp:11;
+    __uint32_t frach:20;
+#   ifndef ___IEEE_BYTES_LITTLE_ENDIAN
+#   else /* ARMEL without __VFP_FP__ */
+    __uint32_t frach:20;
+    __uint32_t exp:11;
+    __uint32_t sign:1;
+#   endif
+    __uint32_t fracl;
+#  endif
+  } u32;
+};
+# elif (LDBL_MANT_DIG == 64) /* 80-bit long double */
+static const double scale = 0x1p65;
 
-#if (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
-# if (LDBL_MANT_DIG == 64) /* 80-bit long double */
 union ldbl {
   long double x;
   struct {
@@ -68,7 +118,9 @@ union ldbl {
 #  endif
   } u32;
 };
-# else /* LDBL_MANT_DIG == 113, 128-bit long double */
+# elif (LDBL_MANT_DIG == 113) /* 128-bit long double */
+static const double scale = 0x1p114;
+
 union ldbl {
   long double x;
   struct {
@@ -96,9 +148,11 @@ union ldbl {
 #  endif
   } u32;
 };
+# else
+#  error Unsupported long double format.
 # endif
 
-static const double two114 = 0x1p114;
+static const int scale_exp = LDBL_MANT_DIG + 1;
 
 long double
 frexpl (long double x, int *eptr)
@@ -107,16 +161,16 @@ frexpl (long double x, int *eptr)
   u.x = x;
   int e = u.u32.exp;
   *eptr = 0;
-  if (e == 0x7fff || x == 0)
+  if (e == (LDBL_MAX_EXP*2 - 1) || x == 0)
     return x; /* inf,nan,0 */
   if (e == 0) /* subnormal */
     {
-      u.x *= two114;
+      u.x *= scale;
       e = u.u32.exp;
-      *eptr -= 114;
+      *eptr -= scale_exp;
     }
-  *eptr += e - 16382;
-  u.u32.exp = 0x3ffe; /* 0 */
+  *eptr += e - (LDBL_MAX_EXP - 2);
+  u.u32.exp = LDBL_MAX_EXP - 2; /* -1 */
   return u.x;
 }
-#endif /* End of 80-bit or 128-bit long double */
+#endif /* !_LDBL_EQ_DBL */
-- 
2.33.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-12-02  9:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-02  8:07 [PATCH] frexpl: Support smaller long double Paul Zimmermann
2021-12-02  9:50 ` Takashi Yano
  -- strict thread matches above, loose matches on Subject: below --
2021-12-01 19:34 Takashi Yano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).