Set the FPU control word as needed for 'long double' computations.
authorBruno Haible <bruno@clisp.org>
Fri, 30 Mar 2007 00:19:34 +0000 (00:19 +0000)
committerBruno Haible <bruno@clisp.org>
Fri, 30 Mar 2007 00:19:34 +0000 (00:19 +0000)
ChangeLog
lib/ldexpl.c
modules/ldexpl

index 7c42d3b..70487b3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2007-03-29  Bruno Haible  <bruno@clisp.org>
 
+       * lib/ldexpl.c: Include fpucw.h.
+       (ldexpl): Use BEGIN/END_LONG_DOUBLE_ROUNDING. Skip the last unneeded
+       multiplication.
+       * modules/ldexpl (Depends-on): Add fpucw.
+
+2007-03-29  Bruno Haible  <bruno@clisp.org>
+
        * modules/ldexpl: New file.
        * m4/ldexpl.m4: New file.
        * lib/math_.h (ldexpl): Define to a replacement if REPLACE_LDEXPL is
index f85847b..bb16be6 100644 (file)
@@ -25,6 +25,7 @@
 #include <math.h>
 
 #include <float.h>
+#include "fpucw.h"
 #include "isnanl.h"
 
 long double
@@ -32,22 +33,36 @@ ldexpl(long double x, int exp)
 {
   long double factor;
   int bit;
+  DECL_LONG_DOUBLE_ROUNDING
 
-  /* Check for zero, nan and infinity. */
-  if (isnanl (x) || x + x == x)
-    return x;
+  BEGIN_LONG_DOUBLE_ROUNDING ();
 
-  if (exp < 0)
+  /* Check for zero, nan and infinity. */
+  if (!(isnanl (x) || x + x == x))
     {
-      exp = -exp;
-      factor = 0.5L;
+      if (exp < 0)
+       {
+         exp = -exp;
+         factor = 0.5L;
+       }
+      else
+       factor = 2.0L;
+
+      if (exp > 0)
+       for (bit = 1;;)
+         {
+           /* Invariant: Here bit = 2^i, factor = 2^-2^i or = 2^2^i,
+              and bit <= exp.  */ 
+           if (exp & bit)
+             x *= factor;
+           bit <<= 1;
+           if (bit > exp)
+             break;
+           factor = factor * factor;
+         }
     }
-  else
-    factor = 2.0L;
 
-  for (bit = 1; bit <= exp; bit <<= 1, factor *= factor)
-    if (exp & bit)
-      x *= factor;
+  END_LONG_DOUBLE_ROUNDING ();
 
   return x;
 }
index c89187a..1d759d0 100644 (file)
@@ -8,6 +8,7 @@ m4/ldexpl.m4
 Depends-on:
 math
 isnanl-nolibm
+fpucw
 
 configure.ac:
 gl_FUNC_LDEXPL