Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: V9
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 178 lines (170 sloc) 4.779 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
From 5709b6c1152c4f1e98cfbccedca1068147279a08 Mon Sep 17 00:00:00 2001
From: River Zhou <riverzhou2000@gmail.com>
Date: Sat, 6 Oct 2012 14:12:59 +0800
Subject: [PATCH 3/4] bionic: libm: linaro: proper sincos implementation

---
 libm/sincos.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 131 insertions(+), 13 deletions(-)

diff --git a/libm/sincos.c b/libm/sincos.c
index e9f6dcc..603a00b 100644
--- a/libm/sincos.c
+++ b/libm/sincos.c
@@ -26,29 +26,147 @@
  */
 #define _GNU_SOURCE 1
 #include <math.h>
+#define INLINE_KERNEL_COSDF
+#define INLINE_KERNEL_SINDF
+#include "src/math_private.h"
+#include "src/k_cosf.c"
+#include "src/k_sinf.c"
 
-// Disable sincos optimization for all functions in this file,
-// otherwise gcc would generate infinite calls.
-// Refer to gcc PR46926.
-// -fno-builtin-sin or -fno-builtin-cos can disable sincos optimization,
-// but these two options do not work inside optimize pragma in-file.
-// Thus we just enforce -O0 when compiling this file.
-#pragma GCC optimize ("O0")
+/* Small multiples of pi/2 rounded to double precision. */
+static const double
+s1pio2 = 1*M_PI_2, /* 0x3FF921FB, 0x54442D18 */
+s2pio2 = 2*M_PI_2, /* 0x400921FB, 0x54442D18 */
+s3pio2 = 3*M_PI_2, /* 0x4012D97C, 0x7F3321D2 */
+s4pio2 = 4*M_PI_2; /* 0x401921FB, 0x54442D18 */
 
+/* For implementation details, see src/s_sin.c, src/s_cos.c */
 void sincos(double x, double *psin, double *pcos)
 {
- *psin = sin(x);
- *pcos = cos(x);
+ double y[2], z=0.0;
+ int32_t n, ix;
+
+ /* High word of x. */
+ GET_HIGH_WORD(ix, x);
+
+ /* |x| ~< pi/4 */
+ ix &= 0x7fffffff;
+ if(ix <= 0x3fe921fb) {
+ if(ix < 0x3e400000) { /* \x\ < 2**-27 */
+ if((int)x==0) { /* generate inexact */
+ *psin = x;
+ *pcos = 1.0;
+ return;
+ }
+ }
+ *psin = __kernel_sin(x, z, 0);
+ *pcos = __kernel_cos(x, z);
+ return;
+ } else if(ix>=0x7ff00000) { /* sin(Inf or NaN) and cos(Inf or NaN) is NaN */
+ *psin = *pcos = x-x;
+ return;
+ } else {
+ n = __ieee754_rem_pio2(x, y);
+ switch(n&3) {
+ case 0:
+ *psin = __kernel_sin(y[0],y[1],1);
+ *pcos = __kernel_cos(y[0],y[1]);
+ return;
+ case 1:
+ *psin = __kernel_cos(y[0],y[1]);
+ *pcos = -__kernel_sin(y[0],y[1],1);
+ return;
+ case 2:
+ *psin = -__kernel_sin(y[0],y[1],1);
+ *pcos = -__kernel_cos(y[0],y[1]);
+ return;
+ default:
+ *psin = -__kernel_cos(y[0],y[1]);
+ *pcos = __kernel_sin(y[0],y[1],1);
+ return;
+ }
+ }
 }
 
+/* For implementation details, see src/s_sinf.c, src/s_cosf.c */
 void sincosf(float x, float *psin, float *pcos)
 {
- *psin = sinf(x);
- *pcos = cosf(x);
+ float y[2];
+ int32_t n, hx, ix;
+
+ GET_FLOAT_WORD(hx, x);
+ ix = hx & 0x7fffffff;
+
+ if(ix <= 0x3f490fda) { /* |x| ~<= pi/4 */
+ if(ix < 0x39800000) { /* |x| < 2**-12 */
+ if(((int)x)==0) { /* x with inexact if x != 0 */
+ *psin = x;
+ *pcos = 1.0;
+ return;
+ }
+ }
+ *psin = __kernel_sindf(x);
+ *pcos = __kernel_cosdf(x);
+ return;
+ } else if(ix <= 0x407b53d1) { /* |x| ~<= 5*pi/4 */
+ if(ix <= 0x4016cbe3) { /* |x| ~<= 3pi/4 */
+ if(hx>0) {
+ *psin = __kernel_cosdf(x - s1pio2);
+ *pcos = __kernel_sindf(s1pio2 - x);
+ return;
+ } else {
+ *psin = -__kernel_cosdf(x + s1pio2);
+ *pcos = __kernel_sindf(x + s1pio2);
+ return;
+ }
+ } else {
+ *psin = __kernel_sindf((hx > 0 ? s2pio2 : -s2pio2) - x);
+ *pcos = -__kernel_cosdf(x + (hx > 0 ? -s2pio2 : s2pio2));
+ return;
+ }
+ } else if(ix <= 0x40e231d5) { /* |x| ~<= 9*pi/4 */
+ if(ix <= 0x40afeddf) { /* |x| ~<= 7*pi/4 */
+ if(hx>0) {
+ *psin = -__kernel_cosdf(x - s3pio2);
+ *pcos = __kernel_sindf(x - s3pio2);
+ return;
+ } else {
+ *psin = __kernel_cosdf(x + s3pio2);
+ *pcos = __kernel_sindf(-s3pio2 - x);
+ return;
+ }
+ } else {
+ *psin = __kernel_sindf(x + (hx > 0 ? -s4pio2 : s4pio2));
+ *pcos = __kernel_cosdf(x + (hx > 0 ? -s4pio2 : s4pio2));
+ return;
+ }
+ } else if(ix>=0x7f800000) { /* sin and cos (Inf or NaN) is NaN */
+ *psin = *pcos = x-x;
+ return;
+ } else {
+ n = __ieee754_rem_pio2f(x,y);
+ switch(n&3) {
+ case 0:
+ *psin = __kernel_sindf((double)y[0]+y[1]);
+ *pcos = __kernel_cosdf((double)y[0]+y[1]);
+ return;
+ case 1:
+ *psin = __kernel_cosdf((double)y[0]+y[1]);
+ *pcos = __kernel_sindf(-(double)y[0]-y[1]);
+ return;
+ case 2:
+ *psin = __kernel_sindf(-(double)y[0]-y[1]);
+ *pcos = -__kernel_cosdf((double)y[0]+y[1]);
+ return;
+ default:
+ *psin = -__kernel_cosdf((double)y[0]+y[1]);
+ *pcos = __kernel_sindf((double)y[0]+y[1]);
+ return;
+ }
+ }
 }
 
 void sincosl(long double x, long double *psin, long double *pcos)
 {
- *psin = sin(x);
- *pcos = cos(x);
+ *psin = sin(x);
+ *pcos = cos(x);
 }
--
1.7.9.5

Something went wrong with that request. Please try again.