Print this page
5261 libm should stop using synonyms.h
5298 fabs is 0-sized, confuses dis(1) and others
Reviewed by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Approved by: Gordon Ross <gwr@nexenta.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/C/atan.c
+++ new/usr/src/lib/libm/common/C/atan.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
↓ open down ↓ |
19 lines elided |
↑ open up ↑ |
20 20 */
21 21
22 22 /*
23 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 24 */
25 25 /*
26 26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 27 * Use is subject to license terms.
28 28 */
29 29
30 -#pragma weak atan = __atan
30 +#pragma weak __atan = atan
31 31
32 32 /* INDENT OFF */
33 33 /*
34 34 * atan(x)
35 35 * Accurate Table look-up algorithm with polynomial approximation in
36 36 * partially product form.
37 37 *
38 38 * -- K.C. Ng, October 17, 2004
39 39 *
40 40 * Algorithm
41 41 *
42 42 * (1). Purge off Inf and NaN and 0
43 43 * (2). Reduce x to positive by atan(x) = -atan(-x).
44 44 * (3). For x <= 1/8 and let z = x*x, return
45 45 * (2.1) if x < 2^(-prec/2), atan(x) = x with inexact flag raised
46 46 * (2.2) if x < 2^(-prec/4-1), atan(x) = x+(x/3)(x*x)
47 47 * (2.3) if x < 2^(-prec/6-2), atan(x) = x+(z-5/3)(z*x/5)
48 48 * (2.4) Otherwise
49 49 * atan(x) = poly1(x) = x + A * B,
50 50 * where
51 51 * A = (p1*x*z) * (p2+z(p3+z))
52 52 * B = (p4+z)+z*z) * (p5+z(p6+z))
53 53 * Note: (i) domain of poly1 is [0, 1/8], (ii) remez relative
54 54 * approximation error of poly1 is bounded by
55 55 * |(atan(x)-poly1(x))/x| <= 2^-57.61
56 56 * (4). For x >= 8 then
57 57 * (3.1) if x >= 2^prec, atan(x) = atan(inf) - pio2lo
58 58 * (3.2) if x >= 2^(prec/3), atan(x) = atan(inf) - 1/x
59 59 * (3.3) if x <= 65, atan(x) = atan(inf) - poly1(1/x)
60 60 * (3.4) otherwise atan(x) = atan(inf) - poly2(1/x)
61 61 * where
62 62 * poly2(r) = (q1*r) * (q2+z(q3+z)) * (q4+z),
63 63 * its domain is [0, 0.0154]; and its remez absolute
64 64 * approximation error is bounded by
65 65 * |atan(x)-poly2(x)|<= 2^-59.45
66 66 *
67 67 * (5). Now x is in (0.125, 8).
68 68 * Recall identity
69 69 * atan(x) = atan(y) + atan((x-y)/(1+x*y)).
70 70 * Let j = (ix - 0x3fc00000) >> 16, 0 <= j < 96, where ix is the high
71 71 * part of x in IEEE double format. Then
72 72 * atan(x) = atan(y[j]) + poly2((x-y[j])/(1+x*y[j]))
73 73 * where y[j] are carefully chosen so that it matches x to around 4.5
74 74 * bits and at the same time atan(y[j]) is very close to an IEEE double
↓ open down ↓ |
34 lines elided |
↑ open up ↑ |
75 75 * floating point number. Calculation indicates that
76 76 * max|(x-y[j])/(1+x*y[j])| < 0.0154
77 77 * j,x
78 78 *
79 79 * Accuracy: Maximum error observed is bounded by 0.6 ulp after testing
80 80 * more than 10 million random arguments
81 81 */
82 82 /* INDENT ON */
83 83
84 84 #include "libm.h"
85 -#include "libm_synonyms.h"
86 85 #include "libm_protos.h"
87 86
88 87 extern const double _TBL_atan[];
89 88 static const double g[] = {
90 89 /* one = */ 1.0,
91 90 /* p1 = */ 8.02176624254765935351230154992663301527500152588e-0002,
92 91 /* p2 = */ 1.27223421700559402580665846471674740314483642578e+0000,
93 92 /* p3 = */ -1.20606901800503640842521235754247754812240600586e+0000,
94 93 /* p4 = */ -2.36088967922325565496066701598465442657470703125e+0000,
95 94 /* p5 = */ 1.38345799501389166152875986881554126739501953125e+0000,
96 95 /* p6 = */ 1.06742368078953453469637224770849570631980895996e+0000,
97 96 /* q1 = */ -1.42796626333911796935538518482644576579332351685e-0001,
98 97 /* q2 = */ 3.51427110447873227059810477159863497078605962912e+0000,
99 98 /* q3 = */ 5.92129112708164262457444237952586263418197631836e-0001,
100 99 /* q4 = */ -1.99272234785683144409063061175402253866195678711e+0000,
101 100 /* pio2hi */ 1.570796326794896558e+00,
102 101 /* pio2lo */ 6.123233995736765886e-17,
103 102 /* t1 = */ -0.333333333333333333333333333333333,
104 103 /* t2 = */ 0.2,
105 104 /* t3 = */ -1.666666666666666666666666666666666,
106 105 };
107 106
108 107 #define one g[0]
109 108 #define p1 g[1]
110 109 #define p2 g[2]
111 110 #define p3 g[3]
112 111 #define p4 g[4]
113 112 #define p5 g[5]
114 113 #define p6 g[6]
115 114 #define q1 g[7]
116 115 #define q2 g[8]
117 116 #define q3 g[9]
118 117 #define q4 g[10]
119 118 #define pio2hi g[11]
120 119 #define pio2lo g[12]
121 120 #define t1 g[13]
122 121 #define t2 g[14]
123 122 #define t3 g[15]
124 123
125 124
126 125 double
127 126 atan(double x) {
128 127 double y, z, r, p, s;
129 128 int ix, lx, hx, j;
130 129
131 130 hx = ((int *) &x)[HIWORD];
132 131 lx = ((int *) &x)[LOWORD];
133 132 ix = hx & ~0x80000000;
134 133 j = ix >> 20;
135 134
136 135 /* for |x| < 1/8 */
137 136 if (j < 0x3fc) {
138 137 if (j < 0x3f5) { /* when |x| < 2**(-prec/6-2) */
139 138 if (j < 0x3e3) { /* if |x| < 2**(-prec/2-2) */
140 139 return ((int) x == 0 ? x : one);
141 140 }
142 141 if (j < 0x3f1) { /* if |x| < 2**(-prec/4-1) */
143 142 return (x + (x * t1) * (x * x));
144 143 } else { /* if |x| < 2**(-prec/6-2) */
145 144 z = x * x;
146 145 s = t2 * x;
147 146 return (x + (t3 + z) * (s * z));
148 147 }
149 148 }
150 149 z = x * x; s = p1 * x;
151 150 return (x + ((s * z) * (p2 + z * (p3 + z))) *
152 151 (((p4 + z) + z * z) * (p5 + z * (p6 + z))));
153 152 }
154 153
155 154 /* for |x| >= 8.0 */
156 155 if (j >= 0x402) {
157 156 if (j < 0x436) {
158 157 r = one / x;
159 158 if (hx >= 0) {
160 159 y = pio2hi; p = pio2lo;
161 160 } else {
162 161 y = -pio2hi; p = -pio2lo;
163 162 }
164 163 if (ix < 0x40504000) { /* x < 65 */
165 164 z = r * r;
166 165 s = p1 * r;
167 166 return (y + ((p - r) - ((s * z) *
168 167 (p2 + z * (p3 + z))) *
169 168 (((p4 + z) + z * z) *
170 169 (p5 + z * (p6 + z)))));
171 170 } else if (j < 0x412) {
172 171 z = r * r;
173 172 return (y + (p - ((q1 * r) * (q4 + z)) *
174 173 (q2 + z * (q3 + z))));
175 174 } else
176 175 return (y + (p - r));
177 176 } else {
178 177 if (j >= 0x7ff) /* x is inf or NaN */
179 178 if (((ix - 0x7ff00000) | lx) != 0)
180 179 #if defined(FPADD_TRAPS_INCOMPLETE_ON_NAN)
181 180 return (ix >= 0x7ff80000 ? x : x - x);
182 181 /* assumes sparc-like QNaN */
183 182 #else
184 183 return (x - x);
185 184 #endif
186 185 y = -pio2lo;
187 186 return (hx >= 0 ? pio2hi - y : y - pio2hi);
188 187 }
189 188 } else { /* now x is between 1/8 and 8 */
190 189 double *w, w0, w1, s, z;
191 190 w = (double *) _TBL_atan + (((ix - 0x3fc00000) >> 16) << 1);
192 191 w0 = (hx >= 0)? w[0] : -w[0];
193 192 s = (x - w0) / (one + x * w0);
194 193 w1 = (hx >= 0)? w[1] : -w[1];
195 194 z = s * s;
196 195 return (((q1 * s) * (q4 + z)) * (q2 + z * (q3 + z)) + w1);
197 196 }
198 197 }
↓ open down ↓ |
103 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX