1 /*-
2 * Copyright (c) 2007, 2010-2013 Steven G. Kargl
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
10 * disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * s_sinl.c and s_cosl.c merged by Steven G. Kargl.
27 */
28
29 #include <sys/types.h>
30 #include <machine/ieee.h>
31 #include <float.h>
32 #include <math.h>
33
34 #include "math_private.h"
35
36 #if LDBL_MANT_DIG == 64
37 #include "ld80/k_sincosl.h"
38 #define NX 3
39 #define PREC 2
40 #elif LDBL_MANT_DIG == 113
41 #include "ld128/k_sincosl.h"
42 #define NX 5
43 #define PREC 3
44 #else
45 #error "Unsupported long double format"
46 #endif
47
48 static const long double two24 = 1.67772160000000000000e+07L;
49
50 void
sincosl(long double x,long double * sn,long double * cs)51 sincosl(long double x, long double *sn, long double *cs)
52 {
53 union {
54 long double e;
55 struct ieee_ext bits;
56 } z;
57 int i, e0;
58 double xd[NX], yd[PREC];
59 long double hi, lo;
60
61 z.e = x;
62 z.bits.ext_sign = 0;
63
64 /* Optimize the case where x is already within range. */
65 if (z.e < M_PI_4) {
66 /*
67 * If x = +-0 or x is a subnormal number, then sin(x) = x and
68 * cos(x) = 1.
69 */
70 if (z.bits.ext_exp == 0) {
71 *sn = x;
72 *cs = 1;
73 } else
74 __kernel_sincosl(x, 0, 0, sn, cs);
75 }
76
77 /* If x = NaN or Inf, then sin(x) and cos(x) are NaN. */
78 if (z.bits.ext_exp == 32767) {
79 *sn = x - x;
80 *cs = x - x;
81 }
82
83 /* Split z.e into a 24-bit representation. */
84 e0 = ilogbl(z.e) - 23;
85 z.e = scalbnl(z.e, -e0);
86 for (i = 0; i < NX; i++) {
87 xd[i] = (double)((int32_t)z.e);
88 z.e = (z.e - xd[i]) * two24;
89 }
90
91 /* yd contains the pieces of xd rem pi/2 such that |yd| < pi/4. */
92 e0 = __kernel_rem_pio2(xd, yd, e0, NX, PREC);
93
94 #if PREC == 2
95 hi = (long double)yd[0] + yd[1];
96 lo = yd[1] - (hi - yd[0]);
97 #else /* PREC == 3 */
98 long double t;
99 t = (long double)yd[2] + yd[1];
100 hi = t + yd[0];
101 lo = yd[0] - (hi - t);
102 #endif
103
104 switch (e0 & 3) {
105 case 0:
106 __kernel_sincosl(hi, lo, 1, sn, cs);
107 break;
108 case 1:
109 __kernel_sincosl(hi, lo, 1, cs, sn);
110 *cs = -*cs;
111 break;
112 case 2:
113 __kernel_sincosl(hi, lo, 1, sn, cs);
114 *sn = -*sn;
115 *cs = -*cs;
116 break;
117 default:
118 __kernel_sincosl(hi, lo, 1, cs, sn);
119 *sn = -*sn;
120 }
121 }
122