public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Siddhesh Poyarekar <siddhesh@sourceware.org>
To: libc-alpha@sourceware.org
Subject: [PATCH 4/4] Use copysign instead of ternary for some sin/cos input ranges
Date: Tue, 27 Sep 2016 17:49:00 -0000	[thread overview]
Message-ID: <1474998553-2366-5-git-send-email-siddhesh@sourceware.org> (raw)
In-Reply-To: <1474998553-2366-1-git-send-email-siddhesh@sourceware.org>

These are remaining cases where we can deduce and conclude that the
sign of the result should be the same as the sign of the input being
checked.  For example, for sin(x), the sign of the result is the same
as the result itself for x < pi.  Likewise, for sine values where x
after range reduction falls into this range and its sign is preserved.

	* sysdeps/ieee754/dbl-64/s_sin.c (do_sincos_1): Use copysign
	instead of ternary condition.
	(do_sincos_2): Likewise.
	(__sin): Likewise.
	(__cos): Likewise.
	(slow): Likewise.
	(sloww): Likewise.
	(sloww1): Likewise.
	(bsloww): Likewise.
	(bsloww1): Likewise.
---
 sysdeps/ieee754/dbl-64/s_sin.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/sysdeps/ieee754/dbl-64/s_sin.c b/sysdeps/ieee754/dbl-64/s_sin.c
index e4333a4..40d538d 100644
--- a/sysdeps/ieee754/dbl-64/s_sin.c
+++ b/sysdeps/ieee754/dbl-64/s_sin.c
@@ -344,7 +344,7 @@ do_sincos_1 (double a, double da, double x, int4 n, int4 k)
 	{
 	  res = do_sin (a, da, &cor);
 	  cor = 1.035 * cor + __copysign (eps, cor);
-	  retval = ((res == res + cor) ? ((a > 0) ? res : -res)
+	  retval = ((res == res + cor) ? __copysign (res, a)
 		    : sloww1 (a, da, x, k));
 	}
       break;
@@ -418,7 +418,7 @@ do_sincos_2 (double a, double da, double x, int4 n, int4 k)
 	{
 	  res = do_sin (a, da, &cor);
 	  cor = 1.035 * cor + __copysign (eps, cor);
-	  retval = ((res == res + cor) ? ((a > 0) ? res : -res)
+	  retval = ((res == res + cor) ? __copysign (res, a)
 		    : bsloww1 (a, da, x, n));
 	}
       break;
@@ -479,7 +479,7 @@ __sin (double x)
     {
       res = do_sin (x, 0, &cor);
       retval = (res == res + 1.096 * cor) ? res : slow1 (x);
-      retval = m > 0 ? retval : -retval;
+      retval = __copysign (retval, x);
     }				/*   else  if (k < 0x3feb6000)    */
 
 /*----------------------- 0.855469  <|x|<2.426265  ----------------------*/
@@ -489,7 +489,7 @@ __sin (double x)
       t = hp0 - fabs (x);
       res = do_cos (t, hp1, &cor);
       retval = (res == res + 1.020 * cor) ? res : slow2 (x);
-      retval = m > 0 ? retval : -retval;
+      retval = __copysign (retval, x);
     }				/*   else  if (k < 0x400368fd)    */
 
 #ifndef IN_SINCOS
@@ -580,7 +580,7 @@ __cos (double x)
 	{
 	  res = do_sin (a, da, &cor);
 	  cor = 1.035 * cor + __copysign (1.0e-31, cor);
-	  retval = ((res == res + cor) ? ((a > 0) ? res : -res)
+	  retval = ((res == res + cor) ? __copysign (res, a)
 		    : sloww1 (a, da, x, 1));
 	}
 
@@ -634,9 +634,9 @@ slow (double x)
 
   __dubsin (fabs (x), 0, w);
   if (w[0] == w[0] + 1.000000001 * w[1])
-    return (x > 0) ? w[0] : -w[0];
+    return __copysign (w[0], x);
 
-  return (x > 0) ? __mpsin (x, 0, false) : -__mpsin (-x, 0, false);
+  return __copysign (__mpsin (fabs (x), 0, false), x);
 }
 
 /*******************************************************************************/
@@ -717,7 +717,7 @@ sloww (double x, double dx, double orig, int k)
   cor = 1.000000001 * w[1] + __copysign (eps, w[1]);
 
   if (w[0] == w[0] + cor)
-    return (x > 0) ? w[0] : -w[0];
+    return __copysign (w[0], x);
 
   t = (orig * hpinv + toint);
   xn = t - toint;
@@ -743,7 +743,7 @@ sloww (double x, double dx, double orig, int k)
   cor = 1.000000001 * w[1] + __copysign (eps, w[1]);
 
   if (w[0] == w[0] + cor)
-    return (a > 0) ? w[0] : -w[0];
+    return __copysign (w[0], a);
 
   return k ? __mpcos (orig, 0, true) : __mpsin (orig, 0, true);
 }
@@ -764,7 +764,7 @@ sloww1 (double x, double dx, double orig, int k)
   res = do_sin_slow (x, dx, 3.1e-30 * fabs (orig), &cor);
 
   if (res == res + cor)
-    return (x > 0) ? res : -res;
+    return __copysign (res, x);
 
   dx = (x > 0 ? dx : -dx);
   __dubsin (fabs (x), dx, w);
@@ -773,7 +773,7 @@ sloww1 (double x, double dx, double orig, int k)
   cor = 1.000000005 * w[1] + __copysign (eps, w[1]);
 
   if (w[0] == w[0] + cor)
-    return (x > 0) ? w[0] : -w[0];
+    return __copysign (w[0], x);
 
   return (k == 1) ? __mpcos (orig, 0, true) : __mpsin (orig, 0, true);
 }
@@ -833,7 +833,7 @@ bsloww (double x, double dx, double orig, int n)
   cor = 1.000000001 * w[1] + __copysign (1.1e-24, w[1]);
 
   if (w[0] == w[0] + cor)
-    return (x > 0) ? w[0] : -w[0];
+    return __copysign (w[0], x);
 
   return (n & 1) ? __mpcos (orig, 0, true) : __mpsin (orig, 0, true);
 }
@@ -861,7 +861,7 @@ bsloww1 (double x, double dx, double orig, int n)
   cor = 1.000000005 * w[1] + __copysign (1.1e-24, w[1]);
 
   if (w[0] == w[0] + cor)
-    return (x > 0) ? w[0] : -w[0];
+    return __copysign (w[0], x);
 
   return (n & 1) ? __mpcos (orig, 0, true) : __mpsin (orig, 0, true);
 }
-- 
2.7.4

  parent reply	other threads:[~2016-09-27 17:49 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-27 17:49 [PATCH 0/4] Use __copysign in sincos wherever possible Siddhesh Poyarekar
2016-09-27 17:49 ` [PATCH 3/4] Use copysign instead of ternary conditions for positive constants Siddhesh Poyarekar
2016-09-28 19:58   ` Carlos O'Donell
2016-09-27 17:49 ` [PATCH 2/4] Check n instead of k1 to decide on sign of sin/cos result Siddhesh Poyarekar
2016-09-28 19:55   ` Carlos O'Donell
2016-09-28 21:58     ` Siddhesh Poyarekar
2016-09-30  0:37       ` Carlos O'Donell
2016-10-05 19:08         ` Siddhesh Poyarekar
2016-09-27 17:49 ` [PATCH 1/4] consolidate sign checks for slow2 Siddhesh Poyarekar
2016-09-27 20:03   ` Carlos O'Donell
2016-09-27 17:49 ` Siddhesh Poyarekar [this message]
2016-09-28 12:59   ` [PATCH 4/4] Use copysign instead of ternary for some sin/cos input ranges Manfred
2016-09-28 20:04     ` Carlos O'Donell
2016-09-28 21:32     ` Siddhesh Poyarekar
2016-09-28 20:09   ` Carlos O'Donell
2016-09-27 19:10 ` [PATCH 0/4] Use __copysign in sincos wherever possible Carlos O'Donell
2016-09-27 19:18   ` Siddhesh Poyarekar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1474998553-2366-5-git-send-email-siddhesh@sourceware.org \
    --to=siddhesh@sourceware.org \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).