diff mbox

[2/3] Consolidate sin and cos code for 105414350 <|x|< 281474976710656

Message ID 1449835910-4651-3-git-send-email-siddhesh.poyarekar@linaro.org
State New
Headers show

Commit Message

Siddhesh Poyarekar Dec. 11, 2015, 12:11 p.m. UTC
The sin and cos computation for this range of input is identical
except for a difference in quadrants by 1.  Exploit that fact and the
common argument reduction to reduce computations for sincos.

Siddhesh

	* sysdeps/ieee754/dbl-64/s_sincos.c (__sin_local, __cos_local):
	Move common code...
	(do_sincos_2): ... a new function.
	(__sincos): Call do_sincos_2 for sin as well as cos.

---
 sysdeps/ieee754/dbl-64/s_sincos.c | 231 ++++++++++++++------------------------
 1 file changed, 84 insertions(+), 147 deletions(-)

-- 
2.5.0
diff mbox

Patch

diff --git a/sysdeps/ieee754/dbl-64/s_sincos.c b/sysdeps/ieee754/dbl-64/s_sincos.c
index 078d65d..24dc43c 100644
--- a/sysdeps/ieee754/dbl-64/s_sincos.c
+++ b/sysdeps/ieee754/dbl-64/s_sincos.c
@@ -33,8 +33,7 @@  static double
 __always_inline
 __sin_local (double x, int4 k)
 {
-  double xx, res, t, cor, y, s, c, sn, ssn, cs, ccs, xn, a, da, db, eps, xn1,
-    xn2;
+  double xx, res, t, cor, y, s, c, sn, ssn, cs, ccs, xn, a, da, eps;
   mynumber u, v;
   int4 m, n;
   double retval = 0;
@@ -94,7 +93,7 @@  __sin_local (double x, int4 k)
     }				/*   else  if (k < 0x400368fd)    */
 
 /*-------------------------- 2.426265<|x|< 105414350 ----------------------*/
-  else if (k < 0x419921FB)
+  else
     {
       t = (x * hpinv + toint);
       xn = t - toint;
@@ -159,82 +158,6 @@  __sin_local (double x, int4 k)
 	}
     }				/*   else  if (k <  0x419921FB )    */
 
-/*---------------------105414350 <|x|< 281474976710656 --------------------*/
-  else
-    {
-      t = (x * hpinv + toint);
-      xn = t - toint;
-      v.x = t;
-      xn1 = (xn + 8.0e22) - 8.0e22;
-      xn2 = xn - xn1;
-      y = ((((x - xn1 * mp1) - xn1 * mp2) - xn2 * mp1) - xn2 * mp2);
-      n = v.i[LOW_HALF] & 3;
-      da = xn1 * pp3;
-      t = y - da;
-      da = (y - t) - da;
-      da = (da - xn2 * pp3) - xn * pp4;
-      a = t + da;
-      da = (t - a) + da;
-      eps = 1.0e-24;
-
-      switch (n)
-	{
-	case 0:
-	case 2:
-	  xx = a * a;
-	  if (n)
-	    {
-	      a = -a;
-	      da = -da;
-	    }
-	  if (xx < 0.01588)
-	    {
-	      /* Taylor series.  */
-	      res = TAYLOR_SIN (xx, a, da, cor);
-	      cor = (cor > 0) ? 1.02 * cor + eps : 1.02 * cor - eps;
-	      retval = (res == res + cor) ? res : bsloww (a, da, x, n);
-	    }
-	  else
-	    {
-	      double t;
-	      if (a > 0)
-		{
-		  m = 1;
-		  t = a;
-		  db = da;
-		}
-	      else
-		{
-		  m = 0;
-		  t = -a;
-		  db = -da;
-		}
-	      u.x = big + t;
-	      y = t - (u.x - big);
-	      res = do_sin (u, y, db, &cor);
-	      cor = (cor > 0) ? 1.035 * cor + eps : 1.035 * cor - eps;
-	      retval = ((res == res + cor) ? ((m) ? res : -res)
-			: bsloww1 (a, da, x, n));
-	    }
-	  break;
-
-	case 1:
-	case 3:
-	  if (a < 0)
-	    {
-	      a = -a;
-	      da = -da;
-	    }
-	  u.x = big + a;
-	  y = a - (u.x - big) + da;
-	  res = do_cos (u, y, &cor);
-	  cor = (cor > 0) ? 1.025 * cor + eps : 1.025 * cor - eps;
-	  retval = ((res == res + cor) ? ((n & 2) ? -res : res)
-		    : bsloww2 (a, da, x, n));
-	  break;
-	}
-    }				/*   else  if (k <  0x42F00000 )   */
-
   return retval;
 }
 
@@ -245,8 +168,7 @@  static double
 __always_inline
 __cos_local (double x, int4 k)
 {
-  double y, xx, res, t, cor, xn, a, da, db, eps, xn1,
-    xn2;
+  double y, xx, res, t, cor, xn, a, da, eps;
   mynumber u, v;
   int4 m, n;
 
@@ -366,79 +288,75 @@  __cos_local (double x, int4 k)
 	}
     }				/*   else  if (k <  0x419921FB )    */
 
-  else
+  return retval;
+}
+
+/* Compute sin (A + DA).  cos can be computed by shifting the quadrant N
+   clockwise.  */
+static double
+__always_inline
+do_sincos_2 (double a, double da, double x, unsigned int n)
+{
+  double res, retval, cor, xx;
+  mynumber u;
+
+  double eps = 1.0e-24;
+
+  switch (n)
     {
-      t = (x * hpinv + toint);
-      xn = t - toint;
-      v.x = t;
-      xn1 = (xn + 8.0e22) - 8.0e22;
-      xn2 = xn - xn1;
-      y = ((((x - xn1 * mp1) - xn1 * mp2) - xn2 * mp1) - xn2 * mp2);
-      n = v.i[LOW_HALF] & 3;
-      da = xn1 * pp3;
-      t = y - da;
-      da = (y - t) - da;
-      da = (da - xn2 * pp3) - xn * pp4;
-      a = t + da;
-      da = (t - a) + da;
-      eps = 1.0e-24;
-
-      switch (n)
+    case 2:
+      a = -a;
+      da = -da;
+      /* Fall through.  */
+    case 0:
+      xx = a * a;
+      if (xx < 0.01588)
 	{
-	case 1:
-	case 3:
-	  xx = a * a;
-	  if (n == 1)
-	    {
-	      a = -a;
-	      da = -da;
-	    }
-	  if (xx < 0.01588)
+	  /* Taylor series.  */
+	  res = TAYLOR_SIN (xx, a, da, cor);
+	  cor = (cor > 0) ? 1.02 * cor + eps : 1.02 * cor - eps;
+	  retval = (res == res + cor) ? res : bsloww (a, da, x, n);
+	}
+      else
+	{
+	  double t, db, y;
+	  int m;
+	  if (a > 0)
 	    {
-	      res = TAYLOR_SIN (xx, a, da, cor);
-	      cor = (cor > 0) ? 1.02 * cor + eps : 1.02 * cor - eps;
-	      retval = (res == res + cor) ? res : bsloww (a, da, x, n);
+	      m = 1;
+	      t = a;
+	      db = da;
 	    }
 	  else
 	    {
-	      double t;
-	      if (a > 0)
-		{
-		  m = 1;
-		  t = a;
-		  db = da;
-		}
-	      else
-		{
-		  m = 0;
-		  t = -a;
-		  db = -da;
-		}
-	      u.x = big + t;
-	      y = t - (u.x - big);
-	      res = do_sin (u, y, db, &cor);
-	      cor = (cor > 0) ? 1.035 * cor + eps : 1.035 * cor - eps;
-	      retval = ((res == res + cor) ? ((m) ? res : -res)
-			: bsloww1 (a, da, x, n));
+	      m = 0;
+	      t = -a;
+	      db = -da;
 	    }
-	  break;
+	  u.x = big + t;
+	  y = t - (u.x - big);
+	  res = do_sin (u, y, db, &cor);
+	  cor = (cor > 0) ? 1.035 * cor + eps : 1.035 * cor - eps;
+	  retval = ((res == res + cor) ? ((m) ? res : -res)
+		    : bsloww1 (a, da, x, n));
+	}
+      break;
 
-	case 0:
-	case 2:
-	  if (a < 0)
-	    {
-	      a = -a;
-	      da = -da;
-	    }
-	  u.x = big + a;
-	  y = a - (u.x - big) + da;
-	  res = do_cos (u, y, &cor);
-	  cor = (cor > 0) ? 1.025 * cor + eps : 1.025 * cor - eps;
-	  retval = ((res == res + cor) ? ((n) ? -res : res)
-		    : bsloww2 (a, da, x, n));
-	  break;
+    case 1:
+    case 3:
+      if (a < 0)
+	{
+	  a = -a;
+	  da = -da;
 	}
-    }				/*   else  if (k <  0x42F00000 )    */
+      u.x = big + a;
+      double y = a - (u.x - big) + da;
+      res = do_cos (u, y, &cor);
+      cor = (cor > 0) ? 1.025 * cor + eps : 1.025 * cor - eps;
+      retval = ((res == res + cor) ? ((n & 2) ? -res : res)
+		: bsloww2 (a, da, x, n));
+      break;
+    }
 
   return retval;
 }
@@ -501,13 +419,32 @@  __sincos (double x, double *sinx, double *cosx)
 
   u.x = x;
   k = 0x7fffffff & u.i[HIGH_HALF];
-
-  if (k < 0x42F00000)
+  if (k < 0x419921FB)
     {
       *sinx = __sin_local (x, k);
       *cosx = __cos_local (x, k);
       return;
     }
+  if (k < 0x42F00000)
+    {
+      double t = (x * hpinv + toint);
+      double xn = t - toint;
+      u.x = t;
+      double xn1 = (xn + 8.0e22) - 8.0e22;
+      double xn2 = xn - xn1;
+      double y = ((((x - xn1 * mp1) - xn1 * mp2) - xn2 * mp1) - xn2 * mp2);
+      int4 n = u.i[LOW_HALF] & 3;
+      double da = xn1 * pp3;
+      t = y - da;
+      da = (y - t) - da;
+      da = (da - xn2 * pp3) - xn * pp4;
+      double a = t + da;
+      da = (t - a) + da;
+
+      *sinx = do_sincos_2 (a, da, x, n);
+      *cosx = do_sincos_2 (a, da, x, (n + 1) & 3);
+      return;
+    }
   if (k < 0x7ff00000)
     {
       reduce_and_compute_sincos (x, sinx, cosx);