Line data Source code
1 : // The following is adapted from fdlibm (http://www.netlib.org/fdlibm).
2 : //
3 : // ====================================================
4 : // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 : //
6 : // Developed at SunSoft, a Sun Microsystems, Inc. business.
7 : // Permission to use, copy, modify, and distribute this
8 : // software is freely granted, provided that this notice
9 : // is preserved.
10 : // ====================================================
11 : //
12 : // The original source code covered by the above license above has been
13 : // modified significantly by Google Inc.
14 : // Copyright 2016 the V8 project authors. All rights reserved.
15 :
16 : #include "src/base/ieee754.h"
17 :
18 : #include <cmath>
19 : #include <limits>
20 :
21 : #include "src/base/build_config.h"
22 : #include "src/base/macros.h"
23 : #include "src/base/overflowing-math.h"
24 :
25 : namespace v8 {
26 : namespace base {
27 : namespace ieee754 {
28 :
29 : namespace {
30 :
31 : /* Disable "potential divide by 0" warning in Visual Studio compiler. */
32 :
33 : #if V8_CC_MSVC
34 :
35 : #pragma warning(disable : 4723)
36 :
37 : #endif
38 :
39 : /*
40 : * The original fdlibm code used statements like:
41 : * n0 = ((*(int*)&one)>>29)^1; * index of high word *
42 : * ix0 = *(n0+(int*)&x); * high word of x *
43 : * ix1 = *((1-n0)+(int*)&x); * low word of x *
44 : * to dig two 32 bit words out of the 64 bit IEEE floating point
45 : * value. That is non-ANSI, and, moreover, the gcc instruction
46 : * scheduler gets it wrong. We instead use the following macros.
47 : * Unlike the original code, we determine the endianness at compile
48 : * time, not at run time; I don't see much benefit to selecting
49 : * endianness at run time.
50 : */
51 :
52 : /*
53 : * A union which permits us to convert between a double and two 32 bit
54 : * ints.
55 : * TODO(jkummerow): This is undefined behavior. Use bit_cast instead.
56 : */
57 :
58 : #if V8_TARGET_LITTLE_ENDIAN
59 :
60 : typedef union {
61 : double value;
62 : struct {
63 : uint32_t lsw;
64 : uint32_t msw;
65 : } parts;
66 : struct {
67 : uint64_t w;
68 : } xparts;
69 : } ieee_double_shape_type;
70 :
71 : #else
72 :
73 : typedef union {
74 : double value;
75 : struct {
76 : uint32_t msw;
77 : uint32_t lsw;
78 : } parts;
79 : struct {
80 : uint64_t w;
81 : } xparts;
82 : } ieee_double_shape_type;
83 :
84 : #endif
85 :
86 : /* Get two 32 bit ints from a double. */
87 :
88 : #define EXTRACT_WORDS(ix0, ix1, d) \
89 : do { \
90 : ieee_double_shape_type ew_u; \
91 : ew_u.value = (d); \
92 : (ix0) = ew_u.parts.msw; \
93 : (ix1) = ew_u.parts.lsw; \
94 : } while (false)
95 :
96 : /* Get a 64-bit int from a double. */
97 : #define EXTRACT_WORD64(ix, d) \
98 : do { \
99 : ieee_double_shape_type ew_u; \
100 : ew_u.value = (d); \
101 : (ix) = ew_u.xparts.w; \
102 : } while (false)
103 :
104 : /* Get the more significant 32 bit int from a double. */
105 :
106 : #define GET_HIGH_WORD(i, d) \
107 : do { \
108 : ieee_double_shape_type gh_u; \
109 : gh_u.value = (d); \
110 : (i) = gh_u.parts.msw; \
111 : } while (false)
112 :
113 : /* Get the less significant 32 bit int from a double. */
114 :
115 : #define GET_LOW_WORD(i, d) \
116 : do { \
117 : ieee_double_shape_type gl_u; \
118 : gl_u.value = (d); \
119 : (i) = gl_u.parts.lsw; \
120 : } while (false)
121 :
122 : /* Set a double from two 32 bit ints. */
123 :
124 : #define INSERT_WORDS(d, ix0, ix1) \
125 : do { \
126 : ieee_double_shape_type iw_u; \
127 : iw_u.parts.msw = (ix0); \
128 : iw_u.parts.lsw = (ix1); \
129 : (d) = iw_u.value; \
130 : } while (false)
131 :
132 : /* Set a double from a 64-bit int. */
133 : #define INSERT_WORD64(d, ix) \
134 : do { \
135 : ieee_double_shape_type iw_u; \
136 : iw_u.xparts.w = (ix); \
137 : (d) = iw_u.value; \
138 : } while (false)
139 :
140 : /* Set the more significant 32 bits of a double from an int. */
141 :
142 : #define SET_HIGH_WORD(d, v) \
143 : do { \
144 : ieee_double_shape_type sh_u; \
145 : sh_u.value = (d); \
146 : sh_u.parts.msw = (v); \
147 : (d) = sh_u.value; \
148 : } while (false)
149 :
150 : /* Set the less significant 32 bits of a double from an int. */
151 :
152 : #define SET_LOW_WORD(d, v) \
153 : do { \
154 : ieee_double_shape_type sl_u; \
155 : sl_u.value = (d); \
156 : sl_u.parts.lsw = (v); \
157 : (d) = sl_u.value; \
158 : } while (false)
159 :
160 : /* Support macro. */
161 :
162 : #define STRICT_ASSIGN(type, lval, rval) ((lval) = (rval))
163 :
164 : int32_t __ieee754_rem_pio2(double x, double* y) V8_WARN_UNUSED_RESULT;
165 : double __kernel_cos(double x, double y) V8_WARN_UNUSED_RESULT;
166 : int __kernel_rem_pio2(double* x, double* y, int e0, int nx, int prec,
167 : const int32_t* ipio2) V8_WARN_UNUSED_RESULT;
168 : double __kernel_sin(double x, double y, int iy) V8_WARN_UNUSED_RESULT;
169 :
170 : /* __ieee754_rem_pio2(x,y)
171 : *
172 : * return the remainder of x rem pi/2 in y[0]+y[1]
173 : * use __kernel_rem_pio2()
174 : */
175 1440570 : int32_t __ieee754_rem_pio2(double x, double *y) {
176 : /*
177 : * Table of constants for 2/pi, 396 Hex digits (476 decimal) of 2/pi
178 : */
179 : static const int32_t two_over_pi[] = {
180 : 0xA2F983, 0x6E4E44, 0x1529FC, 0x2757D1, 0xF534DD, 0xC0DB62, 0x95993C,
181 : 0x439041, 0xFE5163, 0xABDEBB, 0xC561B7, 0x246E3A, 0x424DD2, 0xE00649,
182 : 0x2EEA09, 0xD1921C, 0xFE1DEB, 0x1CB129, 0xA73EE8, 0x8235F5, 0x2EBB44,
183 : 0x84E99C, 0x7026B4, 0x5F7E41, 0x3991D6, 0x398353, 0x39F49C, 0x845F8B,
184 : 0xBDF928, 0x3B1FF8, 0x97FFDE, 0x05980F, 0xEF2F11, 0x8B5A0A, 0x6D1F6D,
185 : 0x367ECF, 0x27CB09, 0xB74F46, 0x3F669E, 0x5FEA2D, 0x7527BA, 0xC7EBE5,
186 : 0xF17B3D, 0x0739F7, 0x8A5292, 0xEA6BFB, 0x5FB11F, 0x8D5D08, 0x560330,
187 : 0x46FC7B, 0x6BABF0, 0xCFBC20, 0x9AF436, 0x1DA9E3, 0x91615E, 0xE61B08,
188 : 0x659985, 0x5F14A0, 0x68408D, 0xFFD880, 0x4D7327, 0x310606, 0x1556CA,
189 : 0x73A8C9, 0x60E27B, 0xC08C6B,
190 : };
191 :
192 : static const int32_t npio2_hw[] = {
193 : 0x3FF921FB, 0x400921FB, 0x4012D97C, 0x401921FB, 0x401F6A7A, 0x4022D97C,
194 : 0x4025FDBB, 0x402921FB, 0x402C463A, 0x402F6A7A, 0x4031475C, 0x4032D97C,
195 : 0x40346B9C, 0x4035FDBB, 0x40378FDB, 0x403921FB, 0x403AB41B, 0x403C463A,
196 : 0x403DD85A, 0x403F6A7A, 0x40407E4C, 0x4041475C, 0x4042106C, 0x4042D97C,
197 : 0x4043A28C, 0x40446B9C, 0x404534AC, 0x4045FDBB, 0x4046C6CB, 0x40478FDB,
198 : 0x404858EB, 0x404921FB,
199 : };
200 :
201 : /*
202 : * invpio2: 53 bits of 2/pi
203 : * pio2_1: first 33 bit of pi/2
204 : * pio2_1t: pi/2 - pio2_1
205 : * pio2_2: second 33 bit of pi/2
206 : * pio2_2t: pi/2 - (pio2_1+pio2_2)
207 : * pio2_3: third 33 bit of pi/2
208 : * pio2_3t: pi/2 - (pio2_1+pio2_2+pio2_3)
209 : */
210 :
211 : static const double
212 : zero = 0.00000000000000000000e+00, /* 0x00000000, 0x00000000 */
213 : half = 5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */
214 : two24 = 1.67772160000000000000e+07, /* 0x41700000, 0x00000000 */
215 : invpio2 = 6.36619772367581382433e-01, /* 0x3FE45F30, 0x6DC9C883 */
216 : pio2_1 = 1.57079632673412561417e+00, /* 0x3FF921FB, 0x54400000 */
217 : pio2_1t = 6.07710050650619224932e-11, /* 0x3DD0B461, 0x1A626331 */
218 : pio2_2 = 6.07710050630396597660e-11, /* 0x3DD0B461, 0x1A600000 */
219 : pio2_2t = 2.02226624879595063154e-21, /* 0x3BA3198A, 0x2E037073 */
220 : pio2_3 = 2.02226624871116645580e-21, /* 0x3BA3198A, 0x2E000000 */
221 : pio2_3t = 8.47842766036889956997e-32; /* 0x397B839A, 0x252049C1 */
222 :
223 : double z, w, t, r, fn;
224 : double tx[3];
225 : int32_t e0, i, j, nx, n, ix, hx;
226 : uint32_t low;
227 :
228 : z = 0;
229 1440570 : GET_HIGH_WORD(hx, x); /* high word of x */
230 1440570 : ix = hx & 0x7FFFFFFF;
231 1440570 : if (ix <= 0x3FE921FB) { /* |x| ~<= pi/4 , no need for reduction */
232 0 : y[0] = x;
233 0 : y[1] = 0;
234 0 : return 0;
235 : }
236 1440570 : if (ix < 0x4002D97C) { /* |x| < 3pi/4, special case with n=+-1 */
237 8066 : if (hx > 0) {
238 5581 : z = x - pio2_1;
239 5581 : if (ix != 0x3FF921FB) { /* 33+53 bit pi is good enough */
240 5328 : y[0] = z - pio2_1t;
241 5328 : y[1] = (z - y[0]) - pio2_1t;
242 : } else { /* near pi/2, use 33+33+53 bit pi */
243 253 : z -= pio2_2;
244 253 : y[0] = z - pio2_2t;
245 253 : y[1] = (z - y[0]) - pio2_2t;
246 : }
247 : return 1;
248 : } else { /* negative x */
249 2485 : z = x + pio2_1;
250 2485 : if (ix != 0x3FF921FB) { /* 33+53 bit pi is good enough */
251 2310 : y[0] = z + pio2_1t;
252 2310 : y[1] = (z - y[0]) + pio2_1t;
253 : } else { /* near pi/2, use 33+33+53 bit pi */
254 175 : z += pio2_2;
255 175 : y[0] = z + pio2_2t;
256 175 : y[1] = (z - y[0]) + pio2_2t;
257 : }
258 : return -1;
259 : }
260 : }
261 1432504 : if (ix <= 0x413921FB) { /* |x| ~<= 2^19*(pi/2), medium size */
262 1415233 : t = fabs(x);
263 1415233 : n = static_cast<int32_t>(t * invpio2 + half);
264 1415233 : fn = static_cast<double>(n);
265 1415233 : r = t - fn * pio2_1;
266 1415233 : w = fn * pio2_1t; /* 1st round good to 85 bit */
267 1415233 : if (n < 32 && ix != npio2_hw[n - 1]) {
268 27736 : y[0] = r - w; /* quick check no cancellation */
269 : } else {
270 : uint32_t high;
271 1387497 : j = ix >> 20;
272 1387497 : y[0] = r - w;
273 1387497 : GET_HIGH_WORD(high, y[0]);
274 1387497 : i = j - ((high >> 20) & 0x7FF);
275 1387497 : if (i > 16) { /* 2nd iteration needed, good to 118 */
276 : t = r;
277 727421 : w = fn * pio2_2;
278 727421 : r = t - w;
279 727421 : w = fn * pio2_2t - ((t - r) - w);
280 727421 : y[0] = r - w;
281 727421 : GET_HIGH_WORD(high, y[0]);
282 727421 : i = j - ((high >> 20) & 0x7FF);
283 727421 : if (i > 49) { /* 3rd iteration need, 151 bits acc */
284 : t = r; /* will cover all possible cases */
285 164 : w = fn * pio2_3;
286 164 : r = t - w;
287 164 : w = fn * pio2_3t - ((t - r) - w);
288 164 : y[0] = r - w;
289 : }
290 : }
291 : }
292 1415233 : y[1] = (r - y[0]) - w;
293 1415233 : if (hx < 0) {
294 4116 : y[0] = -y[0];
295 4116 : y[1] = -y[1];
296 4116 : return -n;
297 : } else {
298 : return n;
299 : }
300 : }
301 : /*
302 : * all other (large) arguments
303 : */
304 17271 : if (ix >= 0x7FF00000) { /* x is inf or NaN */
305 0 : y[0] = y[1] = x - x;
306 0 : return 0;
307 : }
308 : /* set z = scalbn(|x|,ilogb(x)-23) */
309 17271 : GET_LOW_WORD(low, x);
310 17271 : SET_LOW_WORD(z, low);
311 17271 : e0 = (ix >> 20) - 1046; /* e0 = ilogb(z)-23; */
312 17271 : SET_HIGH_WORD(z, ix - static_cast<int32_t>(e0 << 20));
313 51813 : for (i = 0; i < 2; i++) {
314 34542 : tx[i] = static_cast<double>(static_cast<int32_t>(z));
315 34542 : z = (z - tx[i]) * two24;
316 : }
317 17271 : tx[2] = z;
318 : nx = 3;
319 17271 : while (tx[nx - 1] == zero) nx--; /* skip zero term */
320 17271 : n = __kernel_rem_pio2(tx, y, e0, nx, 2, two_over_pi);
321 17271 : if (hx < 0) {
322 4338 : y[0] = -y[0];
323 4338 : y[1] = -y[1];
324 4338 : return -n;
325 : }
326 : return n;
327 : }
328 :
329 : /* __kernel_cos( x, y )
330 : * kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
331 : * Input x is assumed to be bounded by ~pi/4 in magnitude.
332 : * Input y is the tail of x.
333 : *
334 : * Algorithm
335 : * 1. Since cos(-x) = cos(x), we need only to consider positive x.
336 : * 2. if x < 2^-27 (hx<0x3E400000 0), return 1 with inexact if x!=0.
337 : * 3. cos(x) is approximated by a polynomial of degree 14 on
338 : * [0,pi/4]
339 : * 4 14
340 : * cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x
341 : * where the remez error is
342 : *
343 : * | 2 4 6 8 10 12 14 | -58
344 : * |cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x +C6*x )| <= 2
345 : * | |
346 : *
347 : * 4 6 8 10 12 14
348 : * 4. let r = C1*x +C2*x +C3*x +C4*x +C5*x +C6*x , then
349 : * cos(x) = 1 - x*x/2 + r
350 : * since cos(x+y) ~ cos(x) - sin(x)*y
351 : * ~ cos(x) - x*y,
352 : * a correction term is necessary in cos(x) and hence
353 : * cos(x+y) = 1 - (x*x/2 - (r - x*y))
354 : * For better accuracy when x > 0.3, let qx = |x|/4 with
355 : * the last 32 bits mask off, and if x > 0.78125, let qx = 0.28125.
356 : * Then
357 : * cos(x+y) = (1-qx) - ((x*x/2-qx) - (r-x*y)).
358 : * Note that 1-qx and (x*x/2-qx) is EXACT here, and the
359 : * magnitude of the latter is at least a quarter of x*x/2,
360 : * thus, reducing the rounding error in the subtraction.
361 : */
362 : V8_INLINE double __kernel_cos(double x, double y) {
363 : static const double
364 : one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
365 : C1 = 4.16666666666666019037e-02, /* 0x3FA55555, 0x5555554C */
366 : C2 = -1.38888888888741095749e-03, /* 0xBF56C16C, 0x16C15177 */
367 : C3 = 2.48015872894767294178e-05, /* 0x3EFA01A0, 0x19CB1590 */
368 : C4 = -2.75573143513906633035e-07, /* 0xBE927E4F, 0x809C52AD */
369 : C5 = 2.08757232129817482790e-09, /* 0x3E21EE9E, 0xBDB4B1C4 */
370 : C6 = -1.13596475577881948265e-11; /* 0xBDA8FAE9, 0xBE8838D4 */
371 :
372 : double a, iz, z, r, qx;
373 : int32_t ix;
374 10457499 : GET_HIGH_WORD(ix, x);
375 10457499 : ix &= 0x7FFFFFFF; /* ix = |x|'s high word*/
376 10457499 : if (ix < 0x3E400000) { /* if x < 2**27 */
377 328552 : if (static_cast<int>(x) == 0) return one; /* generate inexact */
378 : }
379 10128947 : z = x * x;
380 10128947 : r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * C6)))));
381 10128947 : if (ix < 0x3FD33333) { /* if |x| < 0.3 */
382 9800701 : return one - (0.5 * z - (z * r - x * y));
383 : } else {
384 328246 : if (ix > 0x3FE90000) { /* x > 0.78125 */
385 : qx = 0.28125;
386 : } else {
387 325168 : INSERT_WORDS(qx, ix - 0x00200000, 0); /* x/4 */
388 : }
389 328246 : iz = 0.5 * z - qx;
390 328246 : a = one - qx;
391 328246 : return a - (iz - (z * r - x * y));
392 : }
393 : }
394 :
395 : /* __kernel_rem_pio2(x,y,e0,nx,prec,ipio2)
396 : * double x[],y[]; int e0,nx,prec; int ipio2[];
397 : *
398 : * __kernel_rem_pio2 return the last three digits of N with
399 : * y = x - N*pi/2
400 : * so that |y| < pi/2.
401 : *
402 : * The method is to compute the integer (mod 8) and fraction parts of
403 : * (2/pi)*x without doing the full multiplication. In general we
404 : * skip the part of the product that are known to be a huge integer (
405 : * more accurately, = 0 mod 8 ). Thus the number of operations are
406 : * independent of the exponent of the input.
407 : *
408 : * (2/pi) is represented by an array of 24-bit integers in ipio2[].
409 : *
410 : * Input parameters:
411 : * x[] The input value (must be positive) is broken into nx
412 : * pieces of 24-bit integers in double precision format.
413 : * x[i] will be the i-th 24 bit of x. The scaled exponent
414 : * of x[0] is given in input parameter e0 (i.e., x[0]*2^e0
415 : * match x's up to 24 bits.
416 : *
417 : * Example of breaking a double positive z into x[0]+x[1]+x[2]:
418 : * e0 = ilogb(z)-23
419 : * z = scalbn(z,-e0)
420 : * for i = 0,1,2
421 : * x[i] = floor(z)
422 : * z = (z-x[i])*2**24
423 : *
424 : *
425 : * y[] output result in an array of double precision numbers.
426 : * The dimension of y[] is:
427 : * 24-bit precision 1
428 : * 53-bit precision 2
429 : * 64-bit precision 2
430 : * 113-bit precision 3
431 : * The actual value is the sum of them. Thus for 113-bit
432 : * precison, one may have to do something like:
433 : *
434 : * long double t,w,r_head, r_tail;
435 : * t = (long double)y[2] + (long double)y[1];
436 : * w = (long double)y[0];
437 : * r_head = t+w;
438 : * r_tail = w - (r_head - t);
439 : *
440 : * e0 The exponent of x[0]
441 : *
442 : * nx dimension of x[]
443 : *
444 : * prec an integer indicating the precision:
445 : * 0 24 bits (single)
446 : * 1 53 bits (double)
447 : * 2 64 bits (extended)
448 : * 3 113 bits (quad)
449 : *
450 : * ipio2[]
451 : * integer array, contains the (24*i)-th to (24*i+23)-th
452 : * bit of 2/pi after binary point. The corresponding
453 : * floating value is
454 : *
455 : * ipio2[i] * 2^(-24(i+1)).
456 : *
457 : * External function:
458 : * double scalbn(), floor();
459 : *
460 : *
461 : * Here is the description of some local variables:
462 : *
463 : * jk jk+1 is the initial number of terms of ipio2[] needed
464 : * in the computation. The recommended value is 2,3,4,
465 : * 6 for single, double, extended,and quad.
466 : *
467 : * jz local integer variable indicating the number of
468 : * terms of ipio2[] used.
469 : *
470 : * jx nx - 1
471 : *
472 : * jv index for pointing to the suitable ipio2[] for the
473 : * computation. In general, we want
474 : * ( 2^e0*x[0] * ipio2[jv-1]*2^(-24jv) )/8
475 : * is an integer. Thus
476 : * e0-3-24*jv >= 0 or (e0-3)/24 >= jv
477 : * Hence jv = max(0,(e0-3)/24).
478 : *
479 : * jp jp+1 is the number of terms in PIo2[] needed, jp = jk.
480 : *
481 : * q[] double array with integral value, representing the
482 : * 24-bits chunk of the product of x and 2/pi.
483 : *
484 : * q0 the corresponding exponent of q[0]. Note that the
485 : * exponent for q[i] would be q0-24*i.
486 : *
487 : * PIo2[] double precision array, obtained by cutting pi/2
488 : * into 24 bits chunks.
489 : *
490 : * f[] ipio2[] in floating point
491 : *
492 : * iq[] integer array by breaking up q[] in 24-bits chunk.
493 : *
494 : * fq[] final product of x*(2/pi) in fq[0],..,fq[jk]
495 : *
496 : * ih integer. If >0 it indicates q[] is >= 0.5, hence
497 : * it also indicates the *sign* of the result.
498 : *
499 : */
500 17271 : int __kernel_rem_pio2(double *x, double *y, int e0, int nx, int prec,
501 : const int32_t *ipio2) {
502 : /* Constants:
503 : * The hexadecimal values are the intended ones for the following
504 : * constants. The decimal values may be used, provided that the
505 : * compiler will convert from decimal to binary accurately enough
506 : * to produce the hexadecimal values shown.
507 : */
508 : static const int init_jk[] = {2, 3, 4, 6}; /* initial value for jk */
509 :
510 : static const double PIo2[] = {
511 : 1.57079625129699707031e+00, /* 0x3FF921FB, 0x40000000 */
512 : 7.54978941586159635335e-08, /* 0x3E74442D, 0x00000000 */
513 : 5.39030252995776476554e-15, /* 0x3CF84698, 0x80000000 */
514 : 3.28200341580791294123e-22, /* 0x3B78CC51, 0x60000000 */
515 : 1.27065575308067607349e-29, /* 0x39F01B83, 0x80000000 */
516 : 1.22933308981111328932e-36, /* 0x387A2520, 0x40000000 */
517 : 2.73370053816464559624e-44, /* 0x36E38222, 0x80000000 */
518 : 2.16741683877804819444e-51, /* 0x3569F31D, 0x00000000 */
519 : };
520 :
521 : static const double
522 : zero = 0.0,
523 : one = 1.0,
524 : two24 = 1.67772160000000000000e+07, /* 0x41700000, 0x00000000 */
525 : twon24 = 5.96046447753906250000e-08; /* 0x3E700000, 0x00000000 */
526 :
527 : int32_t jz, jx, jv, jp, jk, carry, n, iq[20], i, j, k, m, q0, ih;
528 : double z, fw, f[20], fq[20], q[20];
529 :
530 : /* initialize jk*/
531 17271 : jk = init_jk[prec];
532 : jp = jk;
533 :
534 : /* determine jx,jv,q0, note that 3>q0 */
535 17271 : jx = nx - 1;
536 17271 : jv = (e0 - 3) / 24;
537 17271 : if (jv < 0) jv = 0;
538 17271 : q0 = e0 - 24 * (jv + 1);
539 :
540 : /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */
541 17271 : j = jv - jx;
542 17271 : m = jx + jk;
543 112308 : for (i = 0; i <= m; i++, j++) {
544 95037 : f[i] = (j < 0) ? zero : static_cast<double>(ipio2[j]);
545 : }
546 :
547 : /* compute q[0],q[1],...q[jk] */
548 86355 : for (i = 0; i <= jk; i++) {
549 129765 : for (j = 0, fw = 0.0; j <= jx; j++) fw += x[j] * f[jx + i - j];
550 86355 : q[i] = fw;
551 : }
552 :
553 : jz = jk;
554 : recompute:
555 : /* distill q[] into iq[] reversingly */
556 98775 : for (i = 0, j = jz, z = q[jz]; j > 0; i++, j--) {
557 79434 : fw = static_cast<double>(static_cast<int32_t>(twon24 * z));
558 79434 : iq[i] = static_cast<int32_t>(z - two24 * fw);
559 79434 : z = q[j - 1] + fw;
560 : }
561 :
562 : /* compute n */
563 19341 : z = scalbn(z, q0); /* actual value of z */
564 19341 : z -= 8.0 * floor(z * 0.125); /* trim off integer >= 8 */
565 19341 : n = static_cast<int32_t>(z);
566 19341 : z -= static_cast<double>(n);
567 : ih = 0;
568 19341 : if (q0 > 0) { /* need iq[jz-1] to determine n */
569 1824 : i = (iq[jz - 1] >> (24 - q0));
570 1824 : n += i;
571 1824 : iq[jz - 1] -= i << (24 - q0);
572 1824 : ih = iq[jz - 1] >> (23 - q0);
573 17517 : } else if (q0 == 0) {
574 750 : ih = iq[jz - 1] >> 23;
575 16767 : } else if (z >= 0.5) {
576 : ih = 2;
577 : }
578 :
579 19341 : if (ih > 0) { /* q > 0.5 */
580 10515 : n += 1;
581 : carry = 0;
582 53661 : for (i = 0; i < jz; i++) { /* compute 1-q */
583 43146 : j = iq[i];
584 43146 : if (carry == 0) {
585 13494 : if (j != 0) {
586 : carry = 1;
587 10515 : iq[i] = 0x1000000 - j;
588 : }
589 : } else {
590 29652 : iq[i] = 0xFFFFFF - j;
591 : }
592 : }
593 10515 : if (q0 > 0) { /* rare case: chance is 1 in 12 */
594 1002 : switch (q0) {
595 : case 1:
596 696 : iq[jz - 1] &= 0x7FFFFF;
597 696 : break;
598 : case 2:
599 306 : iq[jz - 1] &= 0x3FFFFF;
600 306 : break;
601 : }
602 : }
603 10515 : if (ih == 2) {
604 9123 : z = one - z;
605 9123 : if (carry != 0) z -= scalbn(one, q0);
606 : }
607 : }
608 :
609 : /* check if recomputation is needed */
610 19341 : if (z == zero) {
611 : j = 0;
612 4140 : for (i = jz - 1; i >= jk; i--) j |= iq[i];
613 4140 : if (j == 0) { /* need recomputation */
614 0 : for (k = 1; jk >= k && iq[jk - k] == 0; k++) {
615 : /* k = no. of terms needed */
616 : }
617 :
618 4140 : for (i = jz + 1; i <= jz + k; i++) { /* add q[jz+1] to q[jz+k] */
619 2070 : f[jx + i] = ipio2[jv + i];
620 2070 : for (j = 0, fw = 0.0; j <= jx; j++) fw += x[j] * f[jx + i - j];
621 2070 : q[i] = fw;
622 : }
623 : jz += k;
624 : goto recompute;
625 : }
626 : }
627 :
628 : /* chop off zero terms */
629 17271 : if (z == 0.0) {
630 2070 : jz -= 1;
631 2070 : q0 -= 24;
632 4140 : while (iq[jz] == 0) {
633 0 : jz--;
634 0 : q0 -= 24;
635 : }
636 : } else { /* break z into 24-bit if necessary */
637 15201 : z = scalbn(z, -q0);
638 15201 : if (z >= two24) {
639 9 : fw = static_cast<double>(static_cast<int32_t>(twon24 * z));
640 9 : iq[jz] = z - two24 * fw;
641 9 : jz += 1;
642 9 : q0 += 24;
643 9 : iq[jz] = fw;
644 : } else {
645 15192 : iq[jz] = z;
646 : }
647 : }
648 :
649 : /* convert integer "bit" chunk to floating-point value */
650 17271 : fw = scalbn(one, q0);
651 103635 : for (i = jz; i >= 0; i--) {
652 86364 : q[i] = fw * iq[i];
653 86364 : fw *= twon24;
654 : }
655 :
656 : /* compute PIo2[0,...,jp]*q[jz,...,0] */
657 86364 : for (i = jz; i >= 0; i--) {
658 259110 : for (fw = 0.0, k = 0; k <= jp && k <= jz - i; k++) fw += PIo2[k] * q[i + k];
659 86364 : fq[jz - i] = fw;
660 : }
661 :
662 : /* compress fq[] into y[] */
663 17271 : switch (prec) {
664 : case 0:
665 : fw = 0.0;
666 0 : for (i = jz; i >= 0; i--) fw += fq[i];
667 0 : y[0] = (ih == 0) ? fw : -fw;
668 0 : break;
669 : case 1:
670 : case 2:
671 : fw = 0.0;
672 86364 : for (i = jz; i >= 0; i--) fw += fq[i];
673 17271 : y[0] = (ih == 0) ? fw : -fw;
674 17271 : fw = fq[0] - fw;
675 17271 : for (i = 1; i <= jz; i++) fw += fq[i];
676 17271 : y[1] = (ih == 0) ? fw : -fw;
677 17271 : break;
678 : case 3: /* painful */
679 0 : for (i = jz; i > 0; i--) {
680 0 : fw = fq[i - 1] + fq[i];
681 0 : fq[i] += fq[i - 1] - fw;
682 0 : fq[i - 1] = fw;
683 : }
684 0 : for (i = jz; i > 1; i--) {
685 0 : fw = fq[i - 1] + fq[i];
686 0 : fq[i] += fq[i - 1] - fw;
687 0 : fq[i - 1] = fw;
688 : }
689 0 : for (fw = 0.0, i = jz; i >= 2; i--) fw += fq[i];
690 0 : if (ih == 0) {
691 0 : y[0] = fq[0];
692 0 : y[1] = fq[1];
693 0 : y[2] = fw;
694 : } else {
695 0 : y[0] = -fq[0];
696 0 : y[1] = -fq[1];
697 0 : y[2] = -fw;
698 : }
699 : }
700 17271 : return n & 7;
701 : }
702 :
703 : /* __kernel_sin( x, y, iy)
704 : * kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854
705 : * Input x is assumed to be bounded by ~pi/4 in magnitude.
706 : * Input y is the tail of x.
707 : * Input iy indicates whether y is 0. (if iy=0, y assume to be 0).
708 : *
709 : * Algorithm
710 : * 1. Since sin(-x) = -sin(x), we need only to consider positive x.
711 : * 2. if x < 2^-27 (hx<0x3E400000 0), return x with inexact if x!=0.
712 : * 3. sin(x) is approximated by a polynomial of degree 13 on
713 : * [0,pi/4]
714 : * 3 13
715 : * sin(x) ~ x + S1*x + ... + S6*x
716 : * where
717 : *
718 : * |sin(x) 2 4 6 8 10 12 | -58
719 : * |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2
720 : * | x |
721 : *
722 : * 4. sin(x+y) = sin(x) + sin'(x')*y
723 : * ~ sin(x) + (1-x*x/2)*y
724 : * For better accuracy, let
725 : * 3 2 2 2 2
726 : * r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6))))
727 : * then 3 2
728 : * sin(x) = x + (S1*x + (x *(r-y/2)+y))
729 : */
730 : V8_INLINE double __kernel_sin(double x, double y, int iy) {
731 : static const double
732 : half = 5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */
733 : S1 = -1.66666666666666324348e-01, /* 0xBFC55555, 0x55555549 */
734 : S2 = 8.33333333332248946124e-03, /* 0x3F811111, 0x1110F8A6 */
735 : S3 = -1.98412698298579493134e-04, /* 0xBF2A01A0, 0x19C161D5 */
736 : S4 = 2.75573137070700676789e-06, /* 0x3EC71DE3, 0x57B1FE7D */
737 : S5 = -2.50507602534068634195e-08, /* 0xBE5AE5E6, 0x8A2B9CEB */
738 : S6 = 1.58969099521155010221e-10; /* 0x3DE5D93A, 0x5ACFD57C */
739 :
740 : double z, r, v;
741 : int32_t ix;
742 10486383 : GET_HIGH_WORD(ix, x);
743 10486383 : ix &= 0x7FFFFFFF; /* high word of x */
744 10486383 : if (ix < 0x3E400000) { /* |x| < 2**-27 */
745 357621 : if (static_cast<int>(x) == 0) return x;
746 : } /* generate inexact */
747 10128762 : z = x * x;
748 10128762 : v = z * x;
749 10128762 : r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6)));
750 : if (iy == 0) {
751 9595383 : return x + v * (S1 + z * r);
752 : } else {
753 533379 : return x - ((z * (half * y - v * r) - y) - v * S1);
754 : }
755 : }
756 :
757 : /* __kernel_tan( x, y, k )
758 : * kernel tan function on [-pi/4, pi/4], pi/4 ~ 0.7854
759 : * Input x is assumed to be bounded by ~pi/4 in magnitude.
760 : * Input y is the tail of x.
761 : * Input k indicates whether tan (if k=1) or
762 : * -1/tan (if k= -1) is returned.
763 : *
764 : * Algorithm
765 : * 1. Since tan(-x) = -tan(x), we need only to consider positive x.
766 : * 2. if x < 2^-28 (hx<0x3E300000 0), return x with inexact if x!=0.
767 : * 3. tan(x) is approximated by a odd polynomial of degree 27 on
768 : * [0,0.67434]
769 : * 3 27
770 : * tan(x) ~ x + T1*x + ... + T13*x
771 : * where
772 : *
773 : * |tan(x) 2 4 26 | -59.2
774 : * |----- - (1+T1*x +T2*x +.... +T13*x )| <= 2
775 : * | x |
776 : *
777 : * Note: tan(x+y) = tan(x) + tan'(x)*y
778 : * ~ tan(x) + (1+x*x)*y
779 : * Therefore, for better accuracy in computing tan(x+y), let
780 : * 3 2 2 2 2
781 : * r = x *(T2+x *(T3+x *(...+x *(T12+x *T13))))
782 : * then
783 : * 3 2
784 : * tan(x+y) = x + (T1*x + (x *(r+y)+y))
785 : *
786 : * 4. For x in [0.67434,pi/4], let y = pi/4 - x, then
787 : * tan(x) = tan(pi/4-y) = (1-tan(y))/(1+tan(y))
788 : * = 1 - 2*(tan(y) - (tan(y)^2)/(1+tan(y)))
789 : */
790 374761 : double __kernel_tan(double x, double y, int iy) {
791 : static const double xxx[] = {
792 : 3.33333333333334091986e-01, /* 3FD55555, 55555563 */
793 : 1.33333333333201242699e-01, /* 3FC11111, 1110FE7A */
794 : 5.39682539762260521377e-02, /* 3FABA1BA, 1BB341FE */
795 : 2.18694882948595424599e-02, /* 3F9664F4, 8406D637 */
796 : 8.86323982359930005737e-03, /* 3F8226E3, E96E8493 */
797 : 3.59207910759131235356e-03, /* 3F6D6D22, C9560328 */
798 : 1.45620945432529025516e-03, /* 3F57DBC8, FEE08315 */
799 : 5.88041240820264096874e-04, /* 3F4344D8, F2F26501 */
800 : 2.46463134818469906812e-04, /* 3F3026F7, 1A8D1068 */
801 : 7.81794442939557092300e-05, /* 3F147E88, A03792A6 */
802 : 7.14072491382608190305e-05, /* 3F12B80F, 32F0A7E9 */
803 : -1.85586374855275456654e-05, /* BEF375CB, DB605373 */
804 : 2.59073051863633712884e-05, /* 3EFB2A70, 74BF7AD4 */
805 : /* one */ 1.00000000000000000000e+00, /* 3FF00000, 00000000 */
806 : /* pio4 */ 7.85398163397448278999e-01, /* 3FE921FB, 54442D18 */
807 : /* pio4lo */ 3.06161699786838301793e-17 /* 3C81A626, 33145C07 */
808 : };
809 : #define one xxx[13]
810 : #define pio4 xxx[14]
811 : #define pio4lo xxx[15]
812 : #define T xxx
813 :
814 : double z, r, v, w, s;
815 : int32_t ix, hx;
816 :
817 374761 : GET_HIGH_WORD(hx, x); /* high word of x */
818 374761 : ix = hx & 0x7FFFFFFF; /* high word of |x| */
819 374761 : if (ix < 0x3E300000) { /* x < 2**-28 */
820 1781 : if (static_cast<int>(x) == 0) { /* generate inexact */
821 : uint32_t low;
822 1781 : GET_LOW_WORD(low, x);
823 1781 : if (((ix | low) | (iy + 1)) == 0) {
824 0 : return one / fabs(x);
825 : } else {
826 1781 : if (iy == 1) {
827 : return x;
828 : } else { /* compute -1 / (x+y) carefully */
829 : double a, t;
830 :
831 148 : z = w = x + y;
832 148 : SET_LOW_WORD(z, 0);
833 148 : v = y - (z - x);
834 148 : t = a = -one / w;
835 148 : SET_LOW_WORD(t, 0);
836 148 : s = one + t * z;
837 148 : return t + a * (s + t * v);
838 : }
839 : }
840 : }
841 : }
842 372980 : if (ix >= 0x3FE59428) { /* |x| >= 0.6744 */
843 52744 : if (hx < 0) {
844 26635 : x = -x;
845 26635 : y = -y;
846 : }
847 52744 : z = pio4 - x;
848 52744 : w = pio4lo - y;
849 52744 : x = z + w;
850 : y = 0.0;
851 : }
852 372980 : z = x * x;
853 372980 : w = z * z;
854 : /*
855 : * Break x^5*(T[1]+x^2*T[2]+...) into
856 : * x^5(T[1]+x^4*T[3]+...+x^20*T[11]) +
857 : * x^5(x^2*(T[2]+x^4*T[4]+...+x^22*[T12]))
858 : */
859 372980 : r = T[1] + w * (T[3] + w * (T[5] + w * (T[7] + w * (T[9] + w * T[11]))));
860 : v = z *
861 372980 : (T[2] + w * (T[4] + w * (T[6] + w * (T[8] + w * (T[10] + w * T[12])))));
862 372980 : s = z * x;
863 372980 : r = y + z * (s * (r + v) + y);
864 372980 : r += T[0] * s;
865 372980 : w = x + r;
866 372980 : if (ix >= 0x3FE59428) {
867 52744 : v = iy;
868 52744 : return (1 - ((hx >> 30) & 2)) * (v - 2.0 * (x - (w * w / (w + v) - r)));
869 : }
870 320236 : if (iy == 1) {
871 : return w;
872 : } else {
873 : /*
874 : * if allow error up to 2 ulp, simply return
875 : * -1.0 / (x+r) here
876 : */
877 : /* compute -1.0 / (x+r) accurately */
878 : double a, t;
879 : z = w;
880 160727 : SET_LOW_WORD(z, 0);
881 160727 : v = r - (z - x); /* z+v = r+x */
882 160727 : t = a = -1.0 / w; /* a = -1.0/w */
883 160727 : SET_LOW_WORD(t, 0);
884 160727 : s = 1.0 + t * z;
885 160727 : return t + a * (s + t * v);
886 : }
887 :
888 : #undef one
889 : #undef pio4
890 : #undef pio4lo
891 : #undef T
892 : }
893 :
894 : } // namespace
895 :
896 : /* acos(x)
897 : * Method :
898 : * acos(x) = pi/2 - asin(x)
899 : * acos(-x) = pi/2 + asin(x)
900 : * For |x|<=0.5
901 : * acos(x) = pi/2 - (x + x*x^2*R(x^2)) (see asin.c)
902 : * For x>0.5
903 : * acos(x) = pi/2 - (pi/2 - 2asin(sqrt((1-x)/2)))
904 : * = 2asin(sqrt((1-x)/2))
905 : * = 2s + 2s*z*R(z) ...z=(1-x)/2, s=sqrt(z)
906 : * = 2f + (2c + 2s*z*R(z))
907 : * where f=hi part of s, and c = (z-f*f)/(s+f) is the correction term
908 : * for f so that f+c ~ sqrt(z).
909 : * For x<-0.5
910 : * acos(x) = pi - 2asin(sqrt((1-|x|)/2))
911 : * = pi - 0.5*(s+s*z*R(z)), where z=(1-|x|)/2,s=sqrt(z)
912 : *
913 : * Special cases:
914 : * if x is NaN, return x itself;
915 : * if |x|>1, return NaN with invalid signal.
916 : *
917 : * Function needed: sqrt
918 : */
919 10550 : double acos(double x) {
920 : static const double
921 : one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
922 : pi = 3.14159265358979311600e+00, /* 0x400921FB, 0x54442D18 */
923 : pio2_hi = 1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */
924 : pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */
925 : pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */
926 : pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */
927 : pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */
928 : pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */
929 : pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */
930 : pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */
931 : qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */
932 : qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */
933 : qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */
934 : qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */
935 :
936 : double z, p, q, r, w, s, c, df;
937 : int32_t hx, ix;
938 10550 : GET_HIGH_WORD(hx, x);
939 10550 : ix = hx & 0x7FFFFFFF;
940 10550 : if (ix >= 0x3FF00000) { /* |x| >= 1 */
941 : uint32_t lx;
942 7336 : GET_LOW_WORD(lx, x);
943 7336 : if (((ix - 0x3FF00000) | lx) == 0) { /* |x|==1 */
944 759 : if (hx > 0)
945 : return 0.0; /* acos(1) = 0 */
946 : else
947 370 : return pi + 2.0 * pio2_lo; /* acos(-1)= pi */
948 : }
949 : return std::numeric_limits<double>::signaling_NaN(); // acos(|x|>1) is NaN
950 : }
951 3214 : if (ix < 0x3FE00000) { /* |x| < 0.5 */
952 2510 : if (ix <= 0x3C600000) return pio2_hi + pio2_lo; /*if|x|<2**-57*/
953 1211 : z = x * x;
954 1211 : p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
955 1211 : q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
956 1211 : r = p / q;
957 1211 : return pio2_hi - (x - (pio2_lo - x * r));
958 704 : } else if (hx < 0) { /* x < -0.5 */
959 334 : z = (one + x) * 0.5;
960 334 : p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
961 334 : q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
962 334 : s = sqrt(z);
963 334 : r = p / q;
964 334 : w = r * s - pio2_lo;
965 334 : return pi - 2.0 * (s + w);
966 : } else { /* x > 0.5 */
967 370 : z = (one - x) * 0.5;
968 370 : s = sqrt(z);
969 : df = s;
970 370 : SET_LOW_WORD(df, 0);
971 370 : c = (z - df * df) / (s + df);
972 370 : p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
973 370 : q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
974 370 : r = p / q;
975 370 : w = r * s + c;
976 370 : return 2.0 * (df + w);
977 : }
978 : }
979 :
980 : /* acosh(x)
981 : * Method :
982 : * Based on
983 : * acosh(x) = log [ x + sqrt(x*x-1) ]
984 : * we have
985 : * acosh(x) := log(x)+ln2, if x is large; else
986 : * acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else
987 : * acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1.
988 : *
989 : * Special cases:
990 : * acosh(x) is NaN with signal if x<1.
991 : * acosh(NaN) is NaN without signal.
992 : */
993 988 : double acosh(double x) {
994 : static const double
995 : one = 1.0,
996 : ln2 = 6.93147180559945286227e-01; /* 0x3FE62E42, 0xFEFA39EF */
997 : double t;
998 : int32_t hx;
999 : uint32_t lx;
1000 988 : EXTRACT_WORDS(hx, lx, x);
1001 988 : if (hx < 0x3FF00000) { /* x < 1 */
1002 : return std::numeric_limits<double>::signaling_NaN();
1003 465 : } else if (hx >= 0x41B00000) { /* x > 2**28 */
1004 216 : if (hx >= 0x7FF00000) { /* x is inf of NaN */
1005 70 : return x + x;
1006 : } else {
1007 146 : return log(x) + ln2; /* acosh(huge)=log(2x) */
1008 : }
1009 249 : } else if (((hx - 0x3FF00000) | lx) == 0) {
1010 : return 0.0; /* acosh(1) = 0 */
1011 229 : } else if (hx > 0x40000000) { /* 2**28 > x > 2 */
1012 123 : t = x * x;
1013 123 : return log(2.0 * x - one / (x + sqrt(t - one)));
1014 : } else { /* 1<x<2 */
1015 106 : t = x - one;
1016 106 : return log1p(t + sqrt(2.0 * t + t * t));
1017 : }
1018 : }
1019 :
1020 : /* asin(x)
1021 : * Method :
1022 : * Since asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ...
1023 : * we approximate asin(x) on [0,0.5] by
1024 : * asin(x) = x + x*x^2*R(x^2)
1025 : * where
1026 : * R(x^2) is a rational approximation of (asin(x)-x)/x^3
1027 : * and its remez error is bounded by
1028 : * |(asin(x)-x)/x^3 - R(x^2)| < 2^(-58.75)
1029 : *
1030 : * For x in [0.5,1]
1031 : * asin(x) = pi/2-2*asin(sqrt((1-x)/2))
1032 : * Let y = (1-x), z = y/2, s := sqrt(z), and pio2_hi+pio2_lo=pi/2;
1033 : * then for x>0.98
1034 : * asin(x) = pi/2 - 2*(s+s*z*R(z))
1035 : * = pio2_hi - (2*(s+s*z*R(z)) - pio2_lo)
1036 : * For x<=0.98, let pio4_hi = pio2_hi/2, then
1037 : * f = hi part of s;
1038 : * c = sqrt(z) - f = (z-f*f)/(s+f) ...f+c=sqrt(z)
1039 : * and
1040 : * asin(x) = pi/2 - 2*(s+s*z*R(z))
1041 : * = pio4_hi+(pio4-2s)-(2s*z*R(z)-pio2_lo)
1042 : * = pio4_hi+(pio4-2f)-(2s*z*R(z)-(pio2_lo+2c))
1043 : *
1044 : * Special cases:
1045 : * if x is NaN, return x itself;
1046 : * if |x|>1, return NaN with invalid signal.
1047 : */
1048 10544 : double asin(double x) {
1049 : static const double
1050 : one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
1051 : huge = 1.000e+300,
1052 : pio2_hi = 1.57079632679489655800e+00, /* 0x3FF921FB, 0x54442D18 */
1053 : pio2_lo = 6.12323399573676603587e-17, /* 0x3C91A626, 0x33145C07 */
1054 : pio4_hi = 7.85398163397448278999e-01, /* 0x3FE921FB, 0x54442D18 */
1055 : /* coefficient for R(x^2) */
1056 : pS0 = 1.66666666666666657415e-01, /* 0x3FC55555, 0x55555555 */
1057 : pS1 = -3.25565818622400915405e-01, /* 0xBFD4D612, 0x03EB6F7D */
1058 : pS2 = 2.01212532134862925881e-01, /* 0x3FC9C155, 0x0E884455 */
1059 : pS3 = -4.00555345006794114027e-02, /* 0xBFA48228, 0xB5688F3B */
1060 : pS4 = 7.91534994289814532176e-04, /* 0x3F49EFE0, 0x7501B288 */
1061 : pS5 = 3.47933107596021167570e-05, /* 0x3F023DE1, 0x0DFDF709 */
1062 : qS1 = -2.40339491173441421878e+00, /* 0xC0033A27, 0x1C8A2D4B */
1063 : qS2 = 2.02094576023350569471e+00, /* 0x40002AE5, 0x9C598AC8 */
1064 : qS3 = -6.88283971605453293030e-01, /* 0xBFE6066C, 0x1B8D0159 */
1065 : qS4 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */
1066 :
1067 : double t, w, p, q, c, r, s;
1068 : int32_t hx, ix;
1069 :
1070 : t = 0;
1071 10544 : GET_HIGH_WORD(hx, x);
1072 10544 : ix = hx & 0x7FFFFFFF;
1073 10544 : if (ix >= 0x3FF00000) { /* |x|>= 1 */
1074 : uint32_t lx;
1075 7337 : GET_LOW_WORD(lx, x);
1076 7337 : if (((ix - 0x3FF00000) | lx) == 0) { /* asin(1)=+-pi/2 with inexact */
1077 758 : return x * pio2_hi + x * pio2_lo;
1078 : }
1079 : return std::numeric_limits<double>::signaling_NaN(); // asin(|x|>1) is NaN
1080 3207 : } else if (ix < 0x3FE00000) { /* |x|<0.5 */
1081 2503 : if (ix < 0x3E400000) { /* if |x| < 2**-27 */
1082 1558 : if (huge + x > one) return x; /* return x with inexact if x!=0*/
1083 : } else {
1084 945 : t = x * x;
1085 : }
1086 945 : p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5)))));
1087 945 : q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4)));
1088 945 : w = p / q;
1089 945 : return x + x * w;
1090 : }
1091 : /* 1> |x|>= 0.5 */
1092 704 : w = one - fabs(x);
1093 704 : t = w * 0.5;
1094 704 : p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5)))));
1095 704 : q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4)));
1096 704 : s = sqrt(t);
1097 704 : if (ix >= 0x3FEF3333) { /* if |x| > 0.975 */
1098 0 : w = p / q;
1099 0 : t = pio2_hi - (2.0 * (s + s * w) - pio2_lo);
1100 : } else {
1101 : w = s;
1102 704 : SET_LOW_WORD(w, 0);
1103 704 : c = (t - w * w) / (s + w);
1104 704 : r = p / q;
1105 704 : p = 2.0 * s * r - (pio2_lo - 2.0 * c);
1106 704 : q = pio4_hi - 2.0 * w;
1107 704 : t = pio4_hi - (p - q);
1108 : }
1109 704 : if (hx > 0)
1110 : return t;
1111 : else
1112 334 : return -t;
1113 : }
1114 : /* asinh(x)
1115 : * Method :
1116 : * Based on
1117 : * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
1118 : * we have
1119 : * asinh(x) := x if 1+x*x=1,
1120 : * := sign(x)*(log(x)+ln2)) for large |x|, else
1121 : * := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else
1122 : * := sign(x)*log1p(|x| + x^2/(1 + sqrt(1+x^2)))
1123 : */
1124 1156 : double asinh(double x) {
1125 : static const double
1126 : one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
1127 : ln2 = 6.93147180559945286227e-01, /* 0x3FE62E42, 0xFEFA39EF */
1128 : huge = 1.00000000000000000000e+300;
1129 :
1130 : double t, w;
1131 : int32_t hx, ix;
1132 1156 : GET_HIGH_WORD(hx, x);
1133 1156 : ix = hx & 0x7FFFFFFF;
1134 1156 : if (ix >= 0x7FF00000) return x + x; /* x is inf or NaN */
1135 1038 : if (ix < 0x3E300000) { /* |x|<2**-28 */
1136 228 : if (huge + x > one) return x; /* return x inexact except 0 */
1137 : }
1138 810 : if (ix > 0x41B00000) { /* |x| > 2**28 */
1139 350 : w = log(fabs(x)) + ln2;
1140 460 : } else if (ix > 0x40000000) { /* 2**28 > |x| > 2.0 */
1141 210 : t = fabs(x);
1142 210 : w = log(2.0 * t + one / (sqrt(x * x + one) + t));
1143 : } else { /* 2.0 > |x| > 2**-28 */
1144 250 : t = x * x;
1145 250 : w = log1p(fabs(x) + t / (one + sqrt(one + t)));
1146 : }
1147 810 : if (hx > 0) {
1148 : return w;
1149 : } else {
1150 346 : return -w;
1151 : }
1152 : }
1153 :
1154 : /* atan(x)
1155 : * Method
1156 : * 1. Reduce x to positive by atan(x) = -atan(-x).
1157 : * 2. According to the integer k=4t+0.25 chopped, t=x, the argument
1158 : * is further reduced to one of the following intervals and the
1159 : * arctangent of t is evaluated by the corresponding formula:
1160 : *
1161 : * [0,7/16] atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...)
1162 : * [7/16,11/16] atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) )
1163 : * [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) )
1164 : * [19/16,39/16] atan(x) = atan(3/2) + atan( (t-1.5)/(1+1.5t) )
1165 : * [39/16,INF] atan(x) = atan(INF) + atan( -1/t )
1166 : *
1167 : * Constants:
1168 : * The hexadecimal values are the intended ones for the following
1169 : * constants. The decimal values may be used, provided that the
1170 : * compiler will convert from decimal to binary accurately enough
1171 : * to produce the hexadecimal values shown.
1172 : */
1173 53880 : double atan(double x) {
1174 : static const double atanhi[] = {
1175 : 4.63647609000806093515e-01, /* atan(0.5)hi 0x3FDDAC67, 0x0561BB4F */
1176 : 7.85398163397448278999e-01, /* atan(1.0)hi 0x3FE921FB, 0x54442D18 */
1177 : 9.82793723247329054082e-01, /* atan(1.5)hi 0x3FEF730B, 0xD281F69B */
1178 : 1.57079632679489655800e+00, /* atan(inf)hi 0x3FF921FB, 0x54442D18 */
1179 : };
1180 :
1181 : static const double atanlo[] = {
1182 : 2.26987774529616870924e-17, /* atan(0.5)lo 0x3C7A2B7F, 0x222F65E2 */
1183 : 3.06161699786838301793e-17, /* atan(1.0)lo 0x3C81A626, 0x33145C07 */
1184 : 1.39033110312309984516e-17, /* atan(1.5)lo 0x3C700788, 0x7AF0CBBD */
1185 : 6.12323399573676603587e-17, /* atan(inf)lo 0x3C91A626, 0x33145C07 */
1186 : };
1187 :
1188 : static const double aT[] = {
1189 : 3.33333333333329318027e-01, /* 0x3FD55555, 0x5555550D */
1190 : -1.99999999998764832476e-01, /* 0xBFC99999, 0x9998EBC4 */
1191 : 1.42857142725034663711e-01, /* 0x3FC24924, 0x920083FF */
1192 : -1.11111104054623557880e-01, /* 0xBFBC71C6, 0xFE231671 */
1193 : 9.09088713343650656196e-02, /* 0x3FB745CD, 0xC54C206E */
1194 : -7.69187620504482999495e-02, /* 0xBFB3B0F2, 0xAF749A6D */
1195 : 6.66107313738753120669e-02, /* 0x3FB10D66, 0xA0D03D51 */
1196 : -5.83357013379057348645e-02, /* 0xBFADDE2D, 0x52DEFD9A */
1197 : 4.97687799461593236017e-02, /* 0x3FA97B4B, 0x24760DEB */
1198 : -3.65315727442169155270e-02, /* 0xBFA2B444, 0x2C6A6C2F */
1199 : 1.62858201153657823623e-02, /* 0x3F90AD3A, 0xE322DA11 */
1200 : };
1201 :
1202 : static const double one = 1.0, huge = 1.0e300;
1203 :
1204 : double w, s1, s2, z;
1205 : int32_t ix, hx, id;
1206 :
1207 53880 : GET_HIGH_WORD(hx, x);
1208 53880 : ix = hx & 0x7FFFFFFF;
1209 53880 : if (ix >= 0x44100000) { /* if |x| >= 2^66 */
1210 : uint32_t low;
1211 3251 : GET_LOW_WORD(low, x);
1212 3251 : if (ix > 0x7FF00000 || (ix == 0x7FF00000 && (low != 0)))
1213 401 : return x + x; /* NaN */
1214 2850 : if (hx > 0)
1215 1458 : return atanhi[3] + *const_cast<volatile double*>(&atanlo[3]);
1216 : else
1217 1392 : return -atanhi[3] - *const_cast<volatile double*>(&atanlo[3]);
1218 : }
1219 50629 : if (ix < 0x3FDC0000) { /* |x| < 0.4375 */
1220 26093 : if (ix < 0x3E400000) { /* |x| < 2^-27 */
1221 17942 : if (huge + x > one) return x; /* raise inexact */
1222 : }
1223 : id = -1;
1224 : } else {
1225 24536 : x = fabs(x);
1226 24536 : if (ix < 0x3FF30000) { /* |x| < 1.1875 */
1227 4964 : if (ix < 0x3FE60000) { /* 7/16 <=|x|<11/16 */
1228 : id = 0;
1229 769 : x = (2.0 * x - one) / (2.0 + x);
1230 : } else { /* 11/16<=|x|< 19/16 */
1231 : id = 1;
1232 4195 : x = (x - one) / (x + one);
1233 : }
1234 : } else {
1235 19572 : if (ix < 0x40038000) { /* |x| < 2.4375 */
1236 : id = 2;
1237 1278 : x = (x - 1.5) / (one + 1.5 * x);
1238 : } else { /* 2.4375 <= |x| < 2^66 */
1239 : id = 3;
1240 18294 : x = -1.0 / x;
1241 : }
1242 : }
1243 : }
1244 : /* end of argument reduction */
1245 32687 : z = x * x;
1246 32687 : w = z * z;
1247 : /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
1248 32687 : s1 = z * (aT[0] +
1249 65374 : w * (aT[2] + w * (aT[4] + w * (aT[6] + w * (aT[8] + w * aT[10])))));
1250 32687 : s2 = w * (aT[1] + w * (aT[3] + w * (aT[5] + w * (aT[7] + w * aT[9]))));
1251 32687 : if (id < 0) {
1252 8151 : return x - x * (s1 + s2);
1253 : } else {
1254 24536 : z = atanhi[id] - ((x * (s1 + s2) - atanlo[id]) - x);
1255 24536 : return (hx < 0) ? -z : z;
1256 : }
1257 : }
1258 :
1259 : /* atan2(y,x)
1260 : * Method :
1261 : * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
1262 : * 2. Reduce x to positive by (if x and y are unexceptional):
1263 : * ARG (x+iy) = arctan(y/x) ... if x > 0,
1264 : * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0,
1265 : *
1266 : * Special cases:
1267 : *
1268 : * ATAN2((anything), NaN ) is NaN;
1269 : * ATAN2(NAN , (anything) ) is NaN;
1270 : * ATAN2(+-0, +(anything but NaN)) is +-0 ;
1271 : * ATAN2(+-0, -(anything but NaN)) is +-pi ;
1272 : * ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;
1273 : * ATAN2(+-(anything but INF and NaN), +INF) is +-0 ;
1274 : * ATAN2(+-(anything but INF and NaN), -INF) is +-pi;
1275 : * ATAN2(+-INF,+INF ) is +-pi/4 ;
1276 : * ATAN2(+-INF,-INF ) is +-3pi/4;
1277 : * ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;
1278 : *
1279 : * Constants:
1280 : * The hexadecimal values are the intended ones for the following
1281 : * constants. The decimal values may be used, provided that the
1282 : * compiler will convert from decimal to binary accurately enough
1283 : * to produce the hexadecimal values shown.
1284 : */
1285 114226 : double atan2(double y, double x) {
1286 : static volatile double tiny = 1.0e-300;
1287 : static const double
1288 : zero = 0.0,
1289 : pi_o_4 = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */
1290 : pi_o_2 = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */
1291 : pi = 3.1415926535897931160E+00; /* 0x400921FB, 0x54442D18 */
1292 : static volatile double pi_lo =
1293 : 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */
1294 :
1295 : double z;
1296 : int32_t k, m, hx, hy, ix, iy;
1297 : uint32_t lx, ly;
1298 :
1299 114226 : EXTRACT_WORDS(hx, lx, x);
1300 114226 : ix = hx & 0x7FFFFFFF;
1301 114226 : EXTRACT_WORDS(hy, ly, y);
1302 114226 : iy = hy & 0x7FFFFFFF;
1303 337398 : if (((ix | ((lx | NegateWithWraparound<int32_t>(lx)) >> 31)) > 0x7FF00000) ||
1304 217892 : ((iy | ((ly | NegateWithWraparound<int32_t>(ly)) >> 31)) > 0x7FF00000)) {
1305 10405 : return x + y; /* x or y is NaN */
1306 : }
1307 103821 : if ((SubWithWraparound(hx, 0x3FF00000) | lx) == 0) {
1308 4610 : return atan(y); /* x=1.0 */
1309 : }
1310 99211 : m = ((hy >> 31) & 1) | ((hx >> 30) & 2); /* 2*sign(x)+sign(y) */
1311 :
1312 : /* when y = 0 */
1313 99211 : if ((iy | ly) == 0) {
1314 9456 : switch (m) {
1315 : case 0:
1316 : case 1:
1317 : return y; /* atan(+-0,+anything)=+-0 */
1318 : case 2:
1319 2474 : return pi + tiny; /* atan(+0,-anything) = pi */
1320 : case 3:
1321 2474 : return -pi - tiny; /* atan(-0,-anything) =-pi */
1322 : }
1323 : }
1324 : /* when x = 0 */
1325 89755 : if ((ix | lx) == 0) return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny;
1326 :
1327 : /* when x is INF */
1328 80471 : if (ix == 0x7FF00000) {
1329 9333 : if (iy == 0x7FF00000) {
1330 376 : switch (m) {
1331 : case 0:
1332 94 : return pi_o_4 + tiny; /* atan(+INF,+INF) */
1333 : case 1:
1334 94 : return -pi_o_4 - tiny; /* atan(-INF,+INF) */
1335 : case 2:
1336 94 : return 3.0 * pi_o_4 + tiny; /*atan(+INF,-INF)*/
1337 : case 3:
1338 94 : return -3.0 * pi_o_4 - tiny; /*atan(-INF,-INF)*/
1339 : }
1340 : } else {
1341 8957 : switch (m) {
1342 : case 0:
1343 : return zero; /* atan(+...,+INF) */
1344 : case 1:
1345 2315 : return -zero; /* atan(-...,+INF) */
1346 : case 2:
1347 2168 : return pi + tiny; /* atan(+...,-INF) */
1348 : case 3:
1349 2306 : return -pi - tiny; /* atan(-...,-INF) */
1350 : }
1351 : }
1352 : }
1353 : /* when y is INF */
1354 71138 : if (iy == 0x7FF00000) return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny;
1355 :
1356 : /* compute y/x */
1357 62354 : k = (iy - ix) >> 20;
1358 62354 : if (k > 60) { /* |y/x| > 2**60 */
1359 15876 : z = pi_o_2 + 0.5 * pi_lo;
1360 15876 : m &= 1;
1361 46478 : } else if (hx < 0 && k < -60) {
1362 : z = 0.0; /* 0 > |y|/x > -2**-60 */
1363 : } else {
1364 38748 : z = atan(fabs(y / x)); /* safe to do y/x */
1365 : }
1366 62354 : switch (m) {
1367 : case 0:
1368 : return z; /* atan(+,+) */
1369 : case 1:
1370 17814 : return -z; /* atan(-,+) */
1371 : case 2:
1372 11928 : return pi - (z - pi_lo); /* atan(+,-) */
1373 : default: /* case 3 */
1374 11214 : return (z - pi_lo) - pi; /* atan(-,-) */
1375 : }
1376 : }
1377 :
1378 : /* cos(x)
1379 : * Return cosine function of x.
1380 : *
1381 : * kernel function:
1382 : * __kernel_sin ... sine function on [-pi/4,pi/4]
1383 : * __kernel_cos ... cosine function on [-pi/4,pi/4]
1384 : * __ieee754_rem_pio2 ... argument reduction routine
1385 : *
1386 : * Method.
1387 : * Let S,C and T denote the sin, cos and tan respectively on
1388 : * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
1389 : * in [-pi/4 , +pi/4], and let n = k mod 4.
1390 : * We have
1391 : *
1392 : * n sin(x) cos(x) tan(x)
1393 : * ----------------------------------------------------------
1394 : * 0 S C T
1395 : * 1 C -S -1/T
1396 : * 2 -S -C T
1397 : * 3 -C S -1/T
1398 : * ----------------------------------------------------------
1399 : *
1400 : * Special cases:
1401 : * Let trig be any of sin, cos, or tan.
1402 : * trig(+-INF) is NaN, with signals;
1403 : * trig(NaN) is that NaN;
1404 : *
1405 : * Accuracy:
1406 : * TRIG(x) returns trig(x) nearly rounded
1407 : */
1408 9935339 : double cos(double x) {
1409 : double y[2], z = 0.0;
1410 : int32_t n, ix;
1411 :
1412 : /* High word of x. */
1413 9935339 : GET_HIGH_WORD(ix, x);
1414 :
1415 : /* |x| ~< pi/4 */
1416 9935339 : ix &= 0x7FFFFFFF;
1417 9935339 : if (ix <= 0x3FE921FB) {
1418 9922613 : return __kernel_cos(x, z);
1419 12726 : } else if (ix >= 0x7FF00000) {
1420 : /* cos(Inf or NaN) is NaN */
1421 1182 : return x - x;
1422 : } else {
1423 : /* argument reduction needed */
1424 11544 : n = __ieee754_rem_pio2(x, y);
1425 11544 : switch (n & 3) {
1426 : case 0:
1427 9788 : return __kernel_cos(y[0], y[1]);
1428 : case 1:
1429 5946 : return -__kernel_sin(y[0], y[1], 1);
1430 : case 2:
1431 1930 : return -__kernel_cos(y[0], y[1]);
1432 : default:
1433 5424 : return __kernel_sin(y[0], y[1], 1);
1434 : }
1435 : }
1436 : }
1437 :
1438 : /* exp(x)
1439 : * Returns the exponential of x.
1440 : *
1441 : * Method
1442 : * 1. Argument reduction:
1443 : * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
1444 : * Given x, find r and integer k such that
1445 : *
1446 : * x = k*ln2 + r, |r| <= 0.5*ln2.
1447 : *
1448 : * Here r will be represented as r = hi-lo for better
1449 : * accuracy.
1450 : *
1451 : * 2. Approximation of exp(r) by a special rational function on
1452 : * the interval [0,0.34658]:
1453 : * Write
1454 : * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
1455 : * We use a special Remes algorithm on [0,0.34658] to generate
1456 : * a polynomial of degree 5 to approximate R. The maximum error
1457 : * of this polynomial approximation is bounded by 2**-59. In
1458 : * other words,
1459 : * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
1460 : * (where z=r*r, and the values of P1 to P5 are listed below)
1461 : * and
1462 : * | 5 | -59
1463 : * | 2.0+P1*z+...+P5*z - R(z) | <= 2
1464 : * | |
1465 : * The computation of exp(r) thus becomes
1466 : * 2*r
1467 : * exp(r) = 1 + -------
1468 : * R - r
1469 : * r*R1(r)
1470 : * = 1 + r + ----------- (for better accuracy)
1471 : * 2 - R1(r)
1472 : * where
1473 : * 2 4 10
1474 : * R1(r) = r - (P1*r + P2*r + ... + P5*r ).
1475 : *
1476 : * 3. Scale back to obtain exp(x):
1477 : * From step 1, we have
1478 : * exp(x) = 2^k * exp(r)
1479 : *
1480 : * Special cases:
1481 : * exp(INF) is INF, exp(NaN) is NaN;
1482 : * exp(-INF) is 0, and
1483 : * for finite argument, only exp(0)=1 is exact.
1484 : *
1485 : * Accuracy:
1486 : * according to an error analysis, the error is always less than
1487 : * 1 ulp (unit in the last place).
1488 : *
1489 : * Misc. info.
1490 : * For IEEE double
1491 : * if x > 7.09782712893383973096e+02 then exp(x) overflow
1492 : * if x < -7.45133219101941108420e+02 then exp(x) underflow
1493 : *
1494 : * Constants:
1495 : * The hexadecimal values are the intended ones for the following
1496 : * constants. The decimal values may be used, provided that the
1497 : * compiler will convert from decimal to binary accurately enough
1498 : * to produce the hexadecimal values shown.
1499 : */
1500 203772 : double exp(double x) {
1501 : static const double
1502 : one = 1.0,
1503 : halF[2] = {0.5, -0.5},
1504 : o_threshold = 7.09782712893383973096e+02, /* 0x40862E42, 0xFEFA39EF */
1505 : u_threshold = -7.45133219101941108420e+02, /* 0xC0874910, 0xD52D3051 */
1506 : ln2HI[2] = {6.93147180369123816490e-01, /* 0x3FE62E42, 0xFEE00000 */
1507 : -6.93147180369123816490e-01}, /* 0xBFE62E42, 0xFEE00000 */
1508 : ln2LO[2] = {1.90821492927058770002e-10, /* 0x3DEA39EF, 0x35793C76 */
1509 : -1.90821492927058770002e-10}, /* 0xBDEA39EF, 0x35793C76 */
1510 : invln2 = 1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE */
1511 : P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */
1512 : P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */
1513 : P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */
1514 : P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */
1515 : P5 = 4.13813679705723846039e-08, /* 0x3E663769, 0x72BEA4D0 */
1516 : E = 2.718281828459045; /* 0x4005BF0A, 0x8B145769 */
1517 :
1518 : static volatile double
1519 : huge = 1.0e+300,
1520 : twom1000 = 9.33263618503218878990e-302, /* 2**-1000=0x01700000,0*/
1521 : two1023 = 8.988465674311579539e307; /* 0x1p1023 */
1522 :
1523 : double y, hi = 0.0, lo = 0.0, c, t, twopk;
1524 : int32_t k = 0, xsb;
1525 : uint32_t hx;
1526 :
1527 203772 : GET_HIGH_WORD(hx, x);
1528 203772 : xsb = (hx >> 31) & 1; /* sign bit of x */
1529 203772 : hx &= 0x7FFFFFFF; /* high word of |x| */
1530 :
1531 : /* filter out non-finite argument */
1532 203772 : if (hx >= 0x40862E42) { /* if |x|>=709.78... */
1533 5176 : if (hx >= 0x7FF00000) {
1534 : uint32_t lx;
1535 1235 : GET_LOW_WORD(lx, x);
1536 1235 : if (((hx & 0xFFFFF) | lx) != 0)
1537 431 : return x + x; /* NaN */
1538 : else
1539 804 : return (xsb == 0) ? x : 0.0; /* exp(+-inf)={inf,0} */
1540 : }
1541 3941 : if (x > o_threshold) return huge * huge; /* overflow */
1542 2065 : if (x < u_threshold) return twom1000 * twom1000; /* underflow */
1543 : }
1544 :
1545 : /* argument reduction */
1546 198599 : if (hx > 0x3FD62E42) { /* if |x| > 0.5 ln2 */
1547 196012 : if (hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */
1548 : /* TODO(rtoy): We special case exp(1) here to return the correct
1549 : * value of E, as the computation below would get the last bit
1550 : * wrong. We should probably fix the algorithm instead.
1551 : */
1552 1874 : if (x == 1.0) return E;
1553 1317 : hi = x - ln2HI[xsb];
1554 1317 : lo = ln2LO[xsb];
1555 1317 : k = 1 - xsb - xsb;
1556 : } else {
1557 194138 : k = static_cast<int>(invln2 * x + halF[xsb]);
1558 194138 : t = k;
1559 194138 : hi = x - t * ln2HI[0]; /* t*ln2HI is exact here */
1560 194138 : lo = t * ln2LO[0];
1561 : }
1562 195455 : STRICT_ASSIGN(double, x, hi - lo);
1563 2587 : } else if (hx < 0x3E300000) { /* when |x|<2**-28 */
1564 1584 : if (huge + x > one) return one + x; /* trigger inexact */
1565 : } else {
1566 : k = 0;
1567 : }
1568 :
1569 : /* x is now in primary range */
1570 196458 : t = x * x;
1571 196458 : if (k >= -1021) {
1572 196456 : INSERT_WORDS(twopk, 0x3FF00000 + (k << 20), 0);
1573 : } else {
1574 2 : INSERT_WORDS(twopk, 0x3FF00000 + ((k + 1000) << 20), 0);
1575 : }
1576 196458 : c = x - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
1577 196458 : if (k == 0) {
1578 1003 : return one - ((x * c) / (c - 2.0) - x);
1579 : } else {
1580 195455 : y = one - ((lo - (x * c) / (2.0 - c)) - hi);
1581 : }
1582 195455 : if (k >= -1021) {
1583 195453 : if (k == 1024) return y * 2.0 * two1023;
1584 195415 : return y * twopk;
1585 : } else {
1586 2 : return y * twopk * twom1000;
1587 : }
1588 : }
1589 :
1590 : /*
1591 : * Method :
1592 : * 1.Reduced x to positive by atanh(-x) = -atanh(x)
1593 : * 2.For x>=0.5
1594 : * 1 2x x
1595 : * atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------)
1596 : * 2 1 - x 1 - x
1597 : *
1598 : * For x<0.5
1599 : * atanh(x) = 0.5*log1p(2x+2x*x/(1-x))
1600 : *
1601 : * Special cases:
1602 : * atanh(x) is NaN if |x| > 1 with signal;
1603 : * atanh(NaN) is that NaN with no signal;
1604 : * atanh(+-1) is +-INF with signal.
1605 : *
1606 : */
1607 1212 : double atanh(double x) {
1608 : static const double one = 1.0, huge = 1e300;
1609 : static const double zero = 0.0;
1610 :
1611 : double t;
1612 : int32_t hx, ix;
1613 : uint32_t lx;
1614 1212 : EXTRACT_WORDS(hx, lx, x);
1615 1212 : ix = hx & 0x7FFFFFFF;
1616 2424 : if ((ix | ((lx | NegateWithWraparound<int32_t>(lx)) >> 31)) > 0x3FF00000) {
1617 : /* |x|>1 */
1618 : return std::numeric_limits<double>::signaling_NaN();
1619 : }
1620 503 : if (ix == 0x3FF00000) {
1621 : return x > 0 ? std::numeric_limits<double>::infinity()
1622 50 : : -std::numeric_limits<double>::infinity();
1623 : }
1624 453 : if (ix < 0x3E300000 && (huge + x) > zero) return x; /* x<2**-28 */
1625 219 : SET_HIGH_WORD(x, ix);
1626 219 : if (ix < 0x3FE00000) { /* x < 0.5 */
1627 90 : t = x + x;
1628 90 : t = 0.5 * log1p(t + t * x / (one - x));
1629 : } else {
1630 129 : t = 0.5 * log1p((x + x) / (one - x));
1631 : }
1632 219 : if (hx >= 0)
1633 : return t;
1634 : else
1635 86 : return -t;
1636 : }
1637 :
1638 : /* log(x)
1639 : * Return the logrithm of x
1640 : *
1641 : * Method :
1642 : * 1. Argument Reduction: find k and f such that
1643 : * x = 2^k * (1+f),
1644 : * where sqrt(2)/2 < 1+f < sqrt(2) .
1645 : *
1646 : * 2. Approximation of log(1+f).
1647 : * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
1648 : * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
1649 : * = 2s + s*R
1650 : * We use a special Reme algorithm on [0,0.1716] to generate
1651 : * a polynomial of degree 14 to approximate R The maximum error
1652 : * of this polynomial approximation is bounded by 2**-58.45. In
1653 : * other words,
1654 : * 2 4 6 8 10 12 14
1655 : * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s
1656 : * (the values of Lg1 to Lg7 are listed in the program)
1657 : * and
1658 : * | 2 14 | -58.45
1659 : * | Lg1*s +...+Lg7*s - R(z) | <= 2
1660 : * | |
1661 : * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
1662 : * In order to guarantee error in log below 1ulp, we compute log
1663 : * by
1664 : * log(1+f) = f - s*(f - R) (if f is not too large)
1665 : * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
1666 : *
1667 : * 3. Finally, log(x) = k*ln2 + log(1+f).
1668 : * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
1669 : * Here ln2 is split into two floating point number:
1670 : * ln2_hi + ln2_lo,
1671 : * where n*ln2_hi is always exact for |n| < 2000.
1672 : *
1673 : * Special cases:
1674 : * log(x) is NaN with signal if x < 0 (including -INF) ;
1675 : * log(+INF) is +INF; log(0) is -INF with signal;
1676 : * log(NaN) is that NaN with no signal.
1677 : *
1678 : * Accuracy:
1679 : * according to an error analysis, the error is always less than
1680 : * 1 ulp (unit in the last place).
1681 : *
1682 : * Constants:
1683 : * The hexadecimal values are the intended ones for the following
1684 : * constants. The decimal values may be used, provided that the
1685 : * compiler will convert from decimal to binary accurately enough
1686 : * to produce the hexadecimal values shown.
1687 : */
1688 854016 : double log(double x) {
1689 : static const double /* -- */
1690 : ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */
1691 : ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */
1692 : two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */
1693 : Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */
1694 : Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
1695 : Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */
1696 : Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
1697 : Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
1698 : Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
1699 : Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
1700 :
1701 : static const double zero = 0.0;
1702 :
1703 : double hfsq, f, s, z, R, w, t1, t2, dk;
1704 : int32_t k, hx, i, j;
1705 : uint32_t lx;
1706 :
1707 854016 : EXTRACT_WORDS(hx, lx, x);
1708 :
1709 : k = 0;
1710 854016 : if (hx < 0x00100000) { /* x < 2**-1022 */
1711 24074 : if (((hx & 0x7FFFFFFF) | lx) == 0) {
1712 : return -std::numeric_limits<double>::infinity(); /* log(+-0)=-inf */
1713 : }
1714 13292 : if (hx < 0) {
1715 : return std::numeric_limits<double>::signaling_NaN(); /* log(-#) = NaN */
1716 : }
1717 : k -= 54;
1718 2 : x *= two54; /* subnormal number, scale up x */
1719 2 : GET_HIGH_WORD(hx, x);
1720 : }
1721 829944 : if (hx >= 0x7FF00000) return x + x;
1722 820137 : k += (hx >> 20) - 1023;
1723 820137 : hx &= 0x000FFFFF;
1724 820137 : i = (hx + 0x95F64) & 0x100000;
1725 820137 : SET_HIGH_WORD(x, hx | (i ^ 0x3FF00000)); /* normalize x or x/2 */
1726 820137 : k += (i >> 20);
1727 820137 : f = x - 1.0;
1728 820137 : if ((0x000FFFFF & (2 + hx)) < 3) { /* -2**-20 <= f < 2**-20 */
1729 5397 : if (f == zero) {
1730 4732 : if (k == 0) {
1731 : return zero;
1732 : } else {
1733 4148 : dk = static_cast<double>(k);
1734 4148 : return dk * ln2_hi + dk * ln2_lo;
1735 : }
1736 : }
1737 665 : R = f * f * (0.5 - 0.33333333333333333 * f);
1738 665 : if (k == 0) {
1739 59 : return f - R;
1740 : } else {
1741 606 : dk = static_cast<double>(k);
1742 606 : return dk * ln2_hi - ((R - dk * ln2_lo) - f);
1743 : }
1744 : }
1745 814740 : s = f / (2.0 + f);
1746 814740 : dk = static_cast<double>(k);
1747 814740 : z = s * s;
1748 814740 : i = hx - 0x6147A;
1749 814740 : w = z * z;
1750 814740 : j = 0x6B851 - hx;
1751 814740 : t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
1752 814740 : t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
1753 814740 : i |= j;
1754 814740 : R = t2 + t1;
1755 814740 : if (i > 0) {
1756 32823 : hfsq = 0.5 * f * f;
1757 32823 : if (k == 0)
1758 711 : return f - (hfsq - s * (hfsq + R));
1759 : else
1760 32112 : return dk * ln2_hi - ((hfsq - (s * (hfsq + R) + dk * ln2_lo)) - f);
1761 : } else {
1762 781917 : if (k == 0)
1763 20291 : return f - s * (f - R);
1764 : else
1765 761626 : return dk * ln2_hi - ((s * (f - R) - dk * ln2_lo) - f);
1766 : }
1767 : }
1768 :
1769 : /* double log1p(double x)
1770 : *
1771 : * Method :
1772 : * 1. Argument Reduction: find k and f such that
1773 : * 1+x = 2^k * (1+f),
1774 : * where sqrt(2)/2 < 1+f < sqrt(2) .
1775 : *
1776 : * Note. If k=0, then f=x is exact. However, if k!=0, then f
1777 : * may not be representable exactly. In that case, a correction
1778 : * term is need. Let u=1+x rounded. Let c = (1+x)-u, then
1779 : * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
1780 : * and add back the correction term c/u.
1781 : * (Note: when x > 2**53, one can simply return log(x))
1782 : *
1783 : * 2. Approximation of log1p(f).
1784 : * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
1785 : * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
1786 : * = 2s + s*R
1787 : * We use a special Reme algorithm on [0,0.1716] to generate
1788 : * a polynomial of degree 14 to approximate R The maximum error
1789 : * of this polynomial approximation is bounded by 2**-58.45. In
1790 : * other words,
1791 : * 2 4 6 8 10 12 14
1792 : * R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s
1793 : * (the values of Lp1 to Lp7 are listed in the program)
1794 : * and
1795 : * | 2 14 | -58.45
1796 : * | Lp1*s +...+Lp7*s - R(z) | <= 2
1797 : * | |
1798 : * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
1799 : * In order to guarantee error in log below 1ulp, we compute log
1800 : * by
1801 : * log1p(f) = f - (hfsq - s*(hfsq+R)).
1802 : *
1803 : * 3. Finally, log1p(x) = k*ln2 + log1p(f).
1804 : * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
1805 : * Here ln2 is split into two floating point number:
1806 : * ln2_hi + ln2_lo,
1807 : * where n*ln2_hi is always exact for |n| < 2000.
1808 : *
1809 : * Special cases:
1810 : * log1p(x) is NaN with signal if x < -1 (including -INF) ;
1811 : * log1p(+INF) is +INF; log1p(-1) is -INF with signal;
1812 : * log1p(NaN) is that NaN with no signal.
1813 : *
1814 : * Accuracy:
1815 : * according to an error analysis, the error is always less than
1816 : * 1 ulp (unit in the last place).
1817 : *
1818 : * Constants:
1819 : * The hexadecimal values are the intended ones for the following
1820 : * constants. The decimal values may be used, provided that the
1821 : * compiler will convert from decimal to binary accurately enough
1822 : * to produce the hexadecimal values shown.
1823 : *
1824 : * Note: Assuming log() return accurate answer, the following
1825 : * algorithm can be used to compute log1p(x) to within a few ULP:
1826 : *
1827 : * u = 1+x;
1828 : * if(u==1.0) return x ; else
1829 : * return log(u)*(x/(u-1.0));
1830 : *
1831 : * See HP-15C Advanced Functions Handbook, p.193.
1832 : */
1833 55708 : double log1p(double x) {
1834 : static const double /* -- */
1835 : ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */
1836 : ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */
1837 : two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */
1838 : Lp1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */
1839 : Lp2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
1840 : Lp3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */
1841 : Lp4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
1842 : Lp5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
1843 : Lp6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
1844 : Lp7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
1845 :
1846 : static const double zero = 0.0;
1847 :
1848 : double hfsq, f, c, s, z, R, u;
1849 : int32_t k, hx, hu, ax;
1850 :
1851 55708 : GET_HIGH_WORD(hx, x);
1852 55708 : ax = hx & 0x7FFFFFFF;
1853 :
1854 : k = 1;
1855 55708 : if (hx < 0x3FDA827A) { /* 1+x < sqrt(2)+ */
1856 28431 : if (ax >= 0x3FF00000) { /* x <= -1.0 */
1857 253 : if (x == -1.0)
1858 : return -std::numeric_limits<double>::infinity(); /* log1p(-1)=+inf */
1859 : else
1860 228 : return std::numeric_limits<double>::signaling_NaN(); // log1p(x<-1)=NaN
1861 : }
1862 28178 : if (ax < 0x3E200000) { /* |x| < 2**-29 */
1863 27238 : if (two54 + x > zero /* raise inexact */
1864 27238 : && ax < 0x3C900000) /* |x| < 2**-54 */
1865 : return x;
1866 : else
1867 726 : return x - x * x * 0.5;
1868 : }
1869 940 : if (hx > 0 || hx <= static_cast<int32_t>(0xBFD2BEC4)) {
1870 : k = 0;
1871 : f = x;
1872 : hu = 1;
1873 : } /* sqrt(2)/2- <= 1+x < sqrt(2)+ */
1874 : }
1875 28217 : if (hx >= 0x7FF00000) return x + x;
1876 28132 : if (k != 0) {
1877 27202 : if (hx < 0x43400000) {
1878 687 : STRICT_ASSIGN(double, u, 1.0 + x);
1879 687 : GET_HIGH_WORD(hu, u);
1880 687 : k = (hu >> 20) - 1023;
1881 687 : c = (k > 0) ? 1.0 - (u - x) : x - (u - 1.0); /* correction term */
1882 687 : c /= u;
1883 : } else {
1884 : u = x;
1885 26515 : GET_HIGH_WORD(hu, u);
1886 26515 : k = (hu >> 20) - 1023;
1887 : c = 0;
1888 : }
1889 27202 : hu &= 0x000FFFFF;
1890 : /*
1891 : * The approximation to sqrt(2) used in thresholds is not
1892 : * critical. However, the ones used above must give less
1893 : * strict bounds than the one here so that the k==0 case is
1894 : * never reached from here, since here we have committed to
1895 : * using the correction term but don't use it if k==0.
1896 : */
1897 27202 : if (hu < 0x6A09E) { /* u ~< sqrt(2) */
1898 13593 : SET_HIGH_WORD(u, hu | 0x3FF00000); /* normalize u */
1899 : } else {
1900 13609 : k += 1;
1901 13609 : SET_HIGH_WORD(u, hu | 0x3FE00000); /* normalize u/2 */
1902 13609 : hu = (0x00100000 - hu) >> 2;
1903 : }
1904 27202 : f = u - 1.0;
1905 : }
1906 28132 : hfsq = 0.5 * f * f;
1907 28132 : if (hu == 0) { /* |f| < 2**-20 */
1908 187 : if (f == zero) {
1909 70 : if (k == 0) {
1910 : return zero;
1911 : } else {
1912 70 : c += k * ln2_lo;
1913 70 : return k * ln2_hi + c;
1914 : }
1915 : }
1916 117 : R = hfsq * (1.0 - 0.66666666666666666 * f);
1917 117 : if (k == 0)
1918 0 : return f - R;
1919 : else
1920 117 : return k * ln2_hi - ((R - (k * ln2_lo + c)) - f);
1921 : }
1922 27945 : s = f / (2.0 + f);
1923 27945 : z = s * s;
1924 27945 : R = z * (Lp1 +
1925 55890 : z * (Lp2 + z * (Lp3 + z * (Lp4 + z * (Lp5 + z * (Lp6 + z * Lp7))))));
1926 27945 : if (k == 0)
1927 930 : return f - (hfsq - s * (hfsq + R));
1928 : else
1929 27015 : return k * ln2_hi - ((hfsq - (s * (hfsq + R) + (k * ln2_lo + c))) - f);
1930 : }
1931 :
1932 : /*
1933 : * k_log1p(f):
1934 : * Return log(1+f) - f for 1+f in ~[sqrt(2)/2, sqrt(2)].
1935 : *
1936 : * The following describes the overall strategy for computing
1937 : * logarithms in base e. The argument reduction and adding the final
1938 : * term of the polynomial are done by the caller for increased accuracy
1939 : * when different bases are used.
1940 : *
1941 : * Method :
1942 : * 1. Argument Reduction: find k and f such that
1943 : * x = 2^k * (1+f),
1944 : * where sqrt(2)/2 < 1+f < sqrt(2) .
1945 : *
1946 : * 2. Approximation of log(1+f).
1947 : * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
1948 : * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
1949 : * = 2s + s*R
1950 : * We use a special Reme algorithm on [0,0.1716] to generate
1951 : * a polynomial of degree 14 to approximate R The maximum error
1952 : * of this polynomial approximation is bounded by 2**-58.45. In
1953 : * other words,
1954 : * 2 4 6 8 10 12 14
1955 : * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s
1956 : * (the values of Lg1 to Lg7 are listed in the program)
1957 : * and
1958 : * | 2 14 | -58.45
1959 : * | Lg1*s +...+Lg7*s - R(z) | <= 2
1960 : * | |
1961 : * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
1962 : * In order to guarantee error in log below 1ulp, we compute log
1963 : * by
1964 : * log(1+f) = f - s*(f - R) (if f is not too large)
1965 : * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
1966 : *
1967 : * 3. Finally, log(x) = k*ln2 + log(1+f).
1968 : * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
1969 : * Here ln2 is split into two floating point number:
1970 : * ln2_hi + ln2_lo,
1971 : * where n*ln2_hi is always exact for |n| < 2000.
1972 : *
1973 : * Special cases:
1974 : * log(x) is NaN with signal if x < 0 (including -INF) ;
1975 : * log(+INF) is +INF; log(0) is -INF with signal;
1976 : * log(NaN) is that NaN with no signal.
1977 : *
1978 : * Accuracy:
1979 : * according to an error analysis, the error is always less than
1980 : * 1 ulp (unit in the last place).
1981 : *
1982 : * Constants:
1983 : * The hexadecimal values are the intended ones for the following
1984 : * constants. The decimal values may be used, provided that the
1985 : * compiler will convert from decimal to binary accurately enough
1986 : * to produce the hexadecimal values shown.
1987 : */
1988 :
1989 : static const double Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */
1990 : Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
1991 : Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */
1992 : Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
1993 : Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
1994 : Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
1995 : Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
1996 :
1997 : /*
1998 : * We always inline k_log1p(), since doing so produces a
1999 : * substantial performance improvement (~40% on amd64).
2000 : */
2001 57395 : static inline double k_log1p(double f) {
2002 : double hfsq, s, z, R, w, t1, t2;
2003 :
2004 57395 : s = f / (2.0 + f);
2005 57395 : z = s * s;
2006 57395 : w = z * z;
2007 57395 : t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
2008 57395 : t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
2009 57395 : R = t2 + t1;
2010 57395 : hfsq = 0.5 * f * f;
2011 57395 : return s * (hfsq + R);
2012 : }
2013 :
2014 : /*
2015 : * Return the base 2 logarithm of x. See e_log.c and k_log.h for most
2016 : * comments.
2017 : *
2018 : * This reduces x to {k, 1+f} exactly as in e_log.c, then calls the kernel,
2019 : * then does the combining and scaling steps
2020 : * log2(x) = (f - 0.5*f*f + k_log1p(f)) / ln2 + k
2021 : * in not-quite-routine extra precision.
2022 : */
2023 57807 : double log2(double x) {
2024 : static const double
2025 : two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
2026 : ivln2hi = 1.44269504072144627571e+00, /* 0x3FF71547, 0x65200000 */
2027 : ivln2lo = 1.67517131648865118353e-10; /* 0x3DE705FC, 0x2EEFA200 */
2028 :
2029 : double f, hfsq, hi, lo, r, val_hi, val_lo, w, y;
2030 : int32_t i, k, hx;
2031 : uint32_t lx;
2032 :
2033 57807 : EXTRACT_WORDS(hx, lx, x);
2034 :
2035 : k = 0;
2036 57807 : if (hx < 0x00100000) { /* x < 2**-1022 */
2037 737 : if (((hx & 0x7FFFFFFF) | lx) == 0) {
2038 : return -std::numeric_limits<double>::infinity(); /* log(+-0)=-inf */
2039 : }
2040 687 : if (hx < 0) {
2041 : return std::numeric_limits<double>::signaling_NaN(); /* log(-#) = NaN */
2042 : }
2043 : k -= 54;
2044 468 : x *= two54; /* subnormal number, scale up x */
2045 468 : GET_HIGH_WORD(hx, x);
2046 : }
2047 57538 : if (hx >= 0x7FF00000) return x + x;
2048 57455 : if (hx == 0x3FF00000 && lx == 0) return 0.0; /* log(1) = +0 */
2049 57395 : k += (hx >> 20) - 1023;
2050 57395 : hx &= 0x000FFFFF;
2051 57395 : i = (hx + 0x95F64) & 0x100000;
2052 57395 : SET_HIGH_WORD(x, hx | (i ^ 0x3FF00000)); /* normalize x or x/2 */
2053 57395 : k += (i >> 20);
2054 57395 : y = static_cast<double>(k);
2055 57395 : f = x - 1.0;
2056 57395 : hfsq = 0.5 * f * f;
2057 57395 : r = k_log1p(f);
2058 :
2059 : /*
2060 : * f-hfsq must (for args near 1) be evaluated in extra precision
2061 : * to avoid a large cancellation when x is near sqrt(2) or 1/sqrt(2).
2062 : * This is fairly efficient since f-hfsq only depends on f, so can
2063 : * be evaluated in parallel with R. Not combining hfsq with R also
2064 : * keeps R small (though not as small as a true `lo' term would be),
2065 : * so that extra precision is not needed for terms involving R.
2066 : *
2067 : * Compiler bugs involving extra precision used to break Dekker's
2068 : * theorem for spitting f-hfsq as hi+lo, unless double_t was used
2069 : * or the multi-precision calculations were avoided when double_t
2070 : * has extra precision. These problems are now automatically
2071 : * avoided as a side effect of the optimization of combining the
2072 : * Dekker splitting step with the clear-low-bits step.
2073 : *
2074 : * y must (for args near sqrt(2) and 1/sqrt(2)) be added in extra
2075 : * precision to avoid a very large cancellation when x is very near
2076 : * these values. Unlike the above cancellations, this problem is
2077 : * specific to base 2. It is strange that adding +-1 is so much
2078 : * harder than adding +-ln2 or +-log10_2.
2079 : *
2080 : * This uses Dekker's theorem to normalize y+val_hi, so the
2081 : * compiler bugs are back in some configurations, sigh. And I
2082 : * don't want to used double_t to avoid them, since that gives a
2083 : * pessimization and the support for avoiding the pessimization
2084 : * is not yet available.
2085 : *
2086 : * The multi-precision calculations for the multiplications are
2087 : * routine.
2088 : */
2089 57395 : hi = f - hfsq;
2090 57395 : SET_LOW_WORD(hi, 0);
2091 57395 : lo = (f - hi) - hfsq + r;
2092 57395 : val_hi = hi * ivln2hi;
2093 57395 : val_lo = (lo + hi) * ivln2lo + lo * ivln2hi;
2094 :
2095 : /* spadd(val_hi, val_lo, y), except for not using double_t: */
2096 57395 : w = y + val_hi;
2097 57395 : val_lo += (y - w) + val_hi;
2098 : val_hi = w;
2099 :
2100 57395 : return val_lo + val_hi;
2101 : }
2102 :
2103 : /*
2104 : * Return the base 10 logarithm of x
2105 : *
2106 : * Method :
2107 : * Let log10_2hi = leading 40 bits of log10(2) and
2108 : * log10_2lo = log10(2) - log10_2hi,
2109 : * ivln10 = 1/log(10) rounded.
2110 : * Then
2111 : * n = ilogb(x),
2112 : * if(n<0) n = n+1;
2113 : * x = scalbn(x,-n);
2114 : * log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x))
2115 : *
2116 : * Note 1:
2117 : * To guarantee log10(10**n)=n, where 10**n is normal, the rounding
2118 : * mode must set to Round-to-Nearest.
2119 : * Note 2:
2120 : * [1/log(10)] rounded to 53 bits has error .198 ulps;
2121 : * log10 is monotonic at all binary break points.
2122 : *
2123 : * Special cases:
2124 : * log10(x) is NaN if x < 0;
2125 : * log10(+INF) is +INF; log10(0) is -INF;
2126 : * log10(NaN) is that NaN;
2127 : * log10(10**N) = N for N=0,1,...,22.
2128 : */
2129 11926 : double log10(double x) {
2130 : static const double
2131 : two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
2132 : ivln10 = 4.34294481903251816668e-01,
2133 : log10_2hi = 3.01029995663611771306e-01, /* 0x3FD34413, 0x509F6000 */
2134 : log10_2lo = 3.69423907715893078616e-13; /* 0x3D59FEF3, 0x11F12B36 */
2135 :
2136 : double y;
2137 : int32_t i, k, hx;
2138 : uint32_t lx;
2139 :
2140 11926 : EXTRACT_WORDS(hx, lx, x);
2141 :
2142 : k = 0;
2143 11926 : if (hx < 0x00100000) { /* x < 2**-1022 */
2144 323 : if (((hx & 0x7FFFFFFF) | lx) == 0) {
2145 : return -std::numeric_limits<double>::infinity(); /* log(+-0)=-inf */
2146 : }
2147 273 : if (hx < 0) {
2148 : return std::numeric_limits<double>::quiet_NaN(); /* log(-#) = NaN */
2149 : }
2150 : k -= 54;
2151 54 : x *= two54; /* subnormal number, scale up x */
2152 54 : GET_HIGH_WORD(hx, x);
2153 54 : GET_LOW_WORD(lx, x);
2154 : }
2155 11657 : if (hx >= 0x7FF00000) return x + x;
2156 11574 : if (hx == 0x3FF00000 && lx == 0) return 0.0; /* log(1) = +0 */
2157 11546 : k += (hx >> 20) - 1023;
2158 :
2159 11546 : i = (k & 0x80000000) >> 31;
2160 11546 : hx = (hx & 0x000FFFFF) | ((0x3FF - i) << 20);
2161 11546 : y = k + i;
2162 11546 : SET_HIGH_WORD(x, hx);
2163 11546 : SET_LOW_WORD(x, lx);
2164 :
2165 11546 : double z = y * log10_2lo + ivln10 * log(x);
2166 11546 : return z + y * log10_2hi;
2167 : }
2168 :
2169 : /* expm1(x)
2170 : * Returns exp(x)-1, the exponential of x minus 1.
2171 : *
2172 : * Method
2173 : * 1. Argument reduction:
2174 : * Given x, find r and integer k such that
2175 : *
2176 : * x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658
2177 : *
2178 : * Here a correction term c will be computed to compensate
2179 : * the error in r when rounded to a floating-point number.
2180 : *
2181 : * 2. Approximating expm1(r) by a special rational function on
2182 : * the interval [0,0.34658]:
2183 : * Since
2184 : * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
2185 : * we define R1(r*r) by
2186 : * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
2187 : * That is,
2188 : * R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
2189 : * = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
2190 : * = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
2191 : * We use a special Reme algorithm on [0,0.347] to generate
2192 : * a polynomial of degree 5 in r*r to approximate R1. The
2193 : * maximum error of this polynomial approximation is bounded
2194 : * by 2**-61. In other words,
2195 : * R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
2196 : * where Q1 = -1.6666666666666567384E-2,
2197 : * Q2 = 3.9682539681370365873E-4,
2198 : * Q3 = -9.9206344733435987357E-6,
2199 : * Q4 = 2.5051361420808517002E-7,
2200 : * Q5 = -6.2843505682382617102E-9;
2201 : * z = r*r,
2202 : * with error bounded by
2203 : * | 5 | -61
2204 : * | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2
2205 : * | |
2206 : *
2207 : * expm1(r) = exp(r)-1 is then computed by the following
2208 : * specific way which minimize the accumulation rounding error:
2209 : * 2 3
2210 : * r r [ 3 - (R1 + R1*r/2) ]
2211 : * expm1(r) = r + --- + --- * [--------------------]
2212 : * 2 2 [ 6 - r*(3 - R1*r/2) ]
2213 : *
2214 : * To compensate the error in the argument reduction, we use
2215 : * expm1(r+c) = expm1(r) + c + expm1(r)*c
2216 : * ~ expm1(r) + c + r*c
2217 : * Thus c+r*c will be added in as the correction terms for
2218 : * expm1(r+c). Now rearrange the term to avoid optimization
2219 : * screw up:
2220 : * ( 2 2 )
2221 : * ({ ( r [ R1 - (3 - R1*r/2) ] ) } r )
2222 : * expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
2223 : * ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 )
2224 : * ( )
2225 : *
2226 : * = r - E
2227 : * 3. Scale back to obtain expm1(x):
2228 : * From step 1, we have
2229 : * expm1(x) = either 2^k*[expm1(r)+1] - 1
2230 : * = or 2^k*[expm1(r) + (1-2^-k)]
2231 : * 4. Implementation notes:
2232 : * (A). To save one multiplication, we scale the coefficient Qi
2233 : * to Qi*2^i, and replace z by (x^2)/2.
2234 : * (B). To achieve maximum accuracy, we compute expm1(x) by
2235 : * (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
2236 : * (ii) if k=0, return r-E
2237 : * (iii) if k=-1, return 0.5*(r-E)-0.5
2238 : * (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E)
2239 : * else return 1.0+2.0*(r-E);
2240 : * (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
2241 : * (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else
2242 : * (vii) return 2^k(1-((E+2^-k)-r))
2243 : *
2244 : * Special cases:
2245 : * expm1(INF) is INF, expm1(NaN) is NaN;
2246 : * expm1(-INF) is -1, and
2247 : * for finite argument, only expm1(0)=0 is exact.
2248 : *
2249 : * Accuracy:
2250 : * according to an error analysis, the error is always less than
2251 : * 1 ulp (unit in the last place).
2252 : *
2253 : * Misc. info.
2254 : * For IEEE double
2255 : * if x > 7.09782712893383973096e+02 then expm1(x) overflow
2256 : *
2257 : * Constants:
2258 : * The hexadecimal values are the intended ones for the following
2259 : * constants. The decimal values may be used, provided that the
2260 : * compiler will convert from decimal to binary accurately enough
2261 : * to produce the hexadecimal values shown.
2262 : */
2263 80009 : double expm1(double x) {
2264 : static const double
2265 : one = 1.0,
2266 : tiny = 1.0e-300,
2267 : o_threshold = 7.09782712893383973096e+02, /* 0x40862E42, 0xFEFA39EF */
2268 : ln2_hi = 6.93147180369123816490e-01, /* 0x3FE62E42, 0xFEE00000 */
2269 : ln2_lo = 1.90821492927058770002e-10, /* 0x3DEA39EF, 0x35793C76 */
2270 : invln2 = 1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE */
2271 : /* Scaled Q's: Qn_here = 2**n * Qn_above, for R(2*z) where z = hxs =
2272 : x*x/2: */
2273 : Q1 = -3.33333333333331316428e-02, /* BFA11111 111110F4 */
2274 : Q2 = 1.58730158725481460165e-03, /* 3F5A01A0 19FE5585 */
2275 : Q3 = -7.93650757867487942473e-05, /* BF14CE19 9EAADBB7 */
2276 : Q4 = 4.00821782732936239552e-06, /* 3ED0CFCA 86E65239 */
2277 : Q5 = -2.01099218183624371326e-07; /* BE8AFDB7 6E09C32D */
2278 :
2279 : static volatile double huge = 1.0e+300;
2280 :
2281 : double y, hi, lo, c, t, e, hxs, hfx, r1, twopk;
2282 : int32_t k, xsb;
2283 : uint32_t hx;
2284 :
2285 80009 : GET_HIGH_WORD(hx, x);
2286 80009 : xsb = hx & 0x80000000; /* sign bit of x */
2287 80009 : hx &= 0x7FFFFFFF; /* high word of |x| */
2288 :
2289 : /* filter out huge and non-finite argument */
2290 80009 : if (hx >= 0x4043687A) { /* if |x|>=56*ln2 */
2291 47998 : if (hx >= 0x40862E42) { /* if |x|>=709.78... */
2292 377 : if (hx >= 0x7FF00000) {
2293 : uint32_t low;
2294 118 : GET_LOW_WORD(low, x);
2295 118 : if (((hx & 0xFFFFF) | low) != 0)
2296 68 : return x + x; /* NaN */
2297 : else
2298 50 : return (xsb == 0) ? x : -1.0; /* exp(+-inf)={inf,-1} */
2299 : }
2300 259 : if (x > o_threshold) return huge * huge; /* overflow */
2301 : }
2302 47740 : if (xsb != 0) { /* x < -56*ln2, return -1.0 with inexact */
2303 23933 : if (x + tiny < 0.0) /* raise inexact */
2304 : return tiny - one; /* return -1 */
2305 : }
2306 : }
2307 :
2308 : /* argument reduction */
2309 55818 : if (hx > 0x3FD62E42) { /* if |x| > 0.5 ln2 */
2310 27506 : if (hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */
2311 332 : if (xsb == 0) {
2312 191 : hi = x - ln2_hi;
2313 : lo = ln2_lo;
2314 : k = 1;
2315 : } else {
2316 141 : hi = x + ln2_hi;
2317 : lo = -ln2_lo;
2318 : k = -1;
2319 : }
2320 : } else {
2321 27174 : k = invln2 * x + ((xsb == 0) ? 0.5 : -0.5);
2322 27174 : t = k;
2323 27174 : hi = x - t * ln2_hi; /* t*ln2_hi is exact here */
2324 27174 : lo = t * ln2_lo;
2325 : }
2326 27506 : STRICT_ASSIGN(double, x, hi - lo);
2327 27506 : c = (hi - x) - lo;
2328 28312 : } else if (hx < 0x3C900000) { /* when |x|<2**-54, return x */
2329 26679 : t = huge + x; /* return x with inexact flags when x!=0 */
2330 26679 : return x - (t - (huge + x));
2331 : } else {
2332 : k = 0;
2333 : }
2334 :
2335 : /* x is now in primary range */
2336 29139 : hfx = 0.5 * x;
2337 29139 : hxs = x * hfx;
2338 29139 : r1 = one + hxs * (Q1 + hxs * (Q2 + hxs * (Q3 + hxs * (Q4 + hxs * Q5))));
2339 29139 : t = 3.0 - r1 * hfx;
2340 29139 : e = hxs * ((r1 - t) / (6.0 - x * t));
2341 29139 : if (k == 0) {
2342 1633 : return x - (x * e - hxs); /* c is 0 */
2343 : } else {
2344 27506 : INSERT_WORDS(twopk, 0x3FF00000 + (k << 20), 0); /* 2^k */
2345 27506 : e = (x * (e - c) - c);
2346 27506 : e -= hxs;
2347 27506 : if (k == -1) return 0.5 * (x - e) - 0.5;
2348 27365 : if (k == 1) {
2349 191 : if (x < -0.25)
2350 20 : return -2.0 * (e - (x + 0.5));
2351 : else
2352 171 : return one + 2.0 * (x - e);
2353 : }
2354 27174 : if (k <= -2 || k > 56) { /* suffice to return exp(x)-1 */
2355 25204 : y = one - (e - x);
2356 : // TODO(mvstanton): is this replacement for the hex float
2357 : // sufficient?
2358 : // if (k == 1024) y = y*2.0*0x1p1023;
2359 25204 : if (k == 1024)
2360 0 : y = y * 2.0 * 8.98846567431158e+307;
2361 : else
2362 25204 : y = y * twopk;
2363 25204 : return y - one;
2364 : }
2365 : t = one;
2366 1970 : if (k < 20) {
2367 1043 : SET_HIGH_WORD(t, 0x3FF00000 - (0x200000 >> k)); /* t=1-2^-k */
2368 1043 : y = t - (e - x);
2369 1043 : y = y * twopk;
2370 : } else {
2371 927 : SET_HIGH_WORD(t, ((0x3FF - k) << 20)); /* 2^-k */
2372 927 : y = x - (e + t);
2373 927 : y += one;
2374 927 : y = y * twopk;
2375 : }
2376 : }
2377 1970 : return y;
2378 : }
2379 :
2380 98365 : double cbrt(double x) {
2381 : static const uint32_t
2382 : B1 = 715094163, /* B1 = (1023-1023/3-0.03306235651)*2**20 */
2383 : B2 = 696219795; /* B2 = (1023-1023/3-54/3-0.03306235651)*2**20 */
2384 :
2385 : /* |1/cbrt(x) - p(x)| < 2**-23.5 (~[-7.93e-8, 7.929e-8]). */
2386 : static const double P0 = 1.87595182427177009643, /* 0x3FFE03E6, 0x0F61E692 */
2387 : P1 = -1.88497979543377169875, /* 0xBFFE28E0, 0x92F02420 */
2388 : P2 = 1.621429720105354466140, /* 0x3FF9F160, 0x4A49D6C2 */
2389 : P3 = -0.758397934778766047437, /* 0xBFE844CB, 0xBEE751D9 */
2390 : P4 = 0.145996192886612446982; /* 0x3FC2B000, 0xD4E4EDD7 */
2391 :
2392 : int32_t hx;
2393 : union {
2394 : double value;
2395 : uint64_t bits;
2396 : } u;
2397 : double r, s, t = 0.0, w;
2398 : uint32_t sign;
2399 : uint32_t high, low;
2400 :
2401 98365 : EXTRACT_WORDS(hx, low, x);
2402 98365 : sign = hx & 0x80000000; /* sign= sign(x) */
2403 98365 : hx ^= sign;
2404 98365 : if (hx >= 0x7FF00000) return (x + x); /* cbrt(NaN,INF) is itself */
2405 :
2406 : /*
2407 : * Rough cbrt to 5 bits:
2408 : * cbrt(2**e*(1+m) ~= 2**(e/3)*(1+(e%3+m)/3)
2409 : * where e is integral and >= 0, m is real and in [0, 1), and "/" and
2410 : * "%" are integer division and modulus with rounding towards minus
2411 : * infinity. The RHS is always >= the LHS and has a maximum relative
2412 : * error of about 1 in 16. Adding a bias of -0.03306235651 to the
2413 : * (e%3+m)/3 term reduces the error to about 1 in 32. With the IEEE
2414 : * floating point representation, for finite positive normal values,
2415 : * ordinary integer division of the value in bits magically gives
2416 : * almost exactly the RHS of the above provided we first subtract the
2417 : * exponent bias (1023 for doubles) and later add it back. We do the
2418 : * subtraction virtually to keep e >= 0 so that ordinary integer
2419 : * division rounds towards minus infinity; this is also efficient.
2420 : */
2421 98247 : if (hx < 0x00100000) { /* zero or subnormal? */
2422 38 : if ((hx | low) == 0) return (x); /* cbrt(0) is itself */
2423 0 : SET_HIGH_WORD(t, 0x43500000); /* set t= 2**54 */
2424 0 : t *= x;
2425 0 : GET_HIGH_WORD(high, t);
2426 0 : INSERT_WORDS(t, sign | ((high & 0x7FFFFFFF) / 3 + B2), 0);
2427 : } else {
2428 98209 : INSERT_WORDS(t, sign | (hx / 3 + B1), 0);
2429 : }
2430 :
2431 : /*
2432 : * New cbrt to 23 bits:
2433 : * cbrt(x) = t*cbrt(x/t**3) ~= t*P(t**3/x)
2434 : * where P(r) is a polynomial of degree 4 that approximates 1/cbrt(r)
2435 : * to within 2**-23.5 when |r - 1| < 1/10. The rough approximation
2436 : * has produced t such than |t/cbrt(x) - 1| ~< 1/32, and cubing this
2437 : * gives us bounds for r = t**3/x.
2438 : *
2439 : * Try to optimize for parallel evaluation as in k_tanf.c.
2440 : */
2441 98209 : r = (t * t) * (t / x);
2442 98209 : t = t * ((P0 + r * (P1 + r * P2)) + ((r * r) * r) * (P3 + r * P4));
2443 :
2444 : /*
2445 : * Round t away from zero to 23 bits (sloppily except for ensuring that
2446 : * the result is larger in magnitude than cbrt(x) but not much more than
2447 : * 2 23-bit ulps larger). With rounding towards zero, the error bound
2448 : * would be ~5/6 instead of ~4/6. With a maximum error of 2 23-bit ulps
2449 : * in the rounded t, the infinite-precision error in the Newton
2450 : * approximation barely affects third digit in the final error
2451 : * 0.667; the error in the rounded t can be up to about 3 23-bit ulps
2452 : * before the final error is larger than 0.667 ulps.
2453 : */
2454 : u.value = t;
2455 98209 : u.bits = (u.bits + 0x80000000) & 0xFFFFFFFFC0000000ULL;
2456 : t = u.value;
2457 :
2458 : /* one step Newton iteration to 53 bits with error < 0.667 ulps */
2459 98209 : s = t * t; /* t*t is exact */
2460 98209 : r = x / s; /* error <= 0.5 ulps; |r| < |t| */
2461 98209 : w = t + t; /* t+t is exact */
2462 98209 : r = (r - t) / (w + r); /* r-t is exact; w+r ~= 3*t */
2463 98209 : t = t + t * r; /* error <= 0.5 + 0.5/3 + epsilon */
2464 :
2465 98209 : return (t);
2466 : }
2467 :
2468 : /* sin(x)
2469 : * Return sine function of x.
2470 : *
2471 : * kernel function:
2472 : * __kernel_sin ... sine function on [-pi/4,pi/4]
2473 : * __kernel_cos ... cose function on [-pi/4,pi/4]
2474 : * __ieee754_rem_pio2 ... argument reduction routine
2475 : *
2476 : * Method.
2477 : * Let S,C and T denote the sin, cos and tan respectively on
2478 : * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
2479 : * in [-pi/4 , +pi/4], and let n = k mod 4.
2480 : * We have
2481 : *
2482 : * n sin(x) cos(x) tan(x)
2483 : * ----------------------------------------------------------
2484 : * 0 S C T
2485 : * 1 C -S -1/T
2486 : * 2 -S -C T
2487 : * 3 -C S -1/T
2488 : * ----------------------------------------------------------
2489 : *
2490 : * Special cases:
2491 : * Let trig be any of sin, cos, or tan.
2492 : * trig(+-INF) is NaN, with signals;
2493 : * trig(NaN) is that NaN;
2494 : *
2495 : * Accuracy:
2496 : * TRIG(x) returns trig(x) nearly rounded
2497 : */
2498 11010899 : double sin(double x) {
2499 : double y[2], z = 0.0;
2500 : int32_t n, ix;
2501 :
2502 : /* High word of x. */
2503 11010899 : GET_HIGH_WORD(ix, x);
2504 :
2505 : /* |x| ~< pi/4 */
2506 11010899 : ix &= 0x7FFFFFFF;
2507 11010899 : if (ix <= 0x3FE921FB) {
2508 9952780 : return __kernel_sin(x, z, 0);
2509 1058119 : } else if (ix >= 0x7FF00000) {
2510 : /* sin(Inf or NaN) is NaN */
2511 1174 : return x - x;
2512 : } else {
2513 : /* argument reduction needed */
2514 1056945 : n = __ieee754_rem_pio2(x, y);
2515 1056945 : switch (n & 3) {
2516 : case 0:
2517 530280 : return __kernel_sin(y[0], y[1], 1);
2518 : case 1:
2519 529870 : return __kernel_cos(y[0], y[1]);
2520 : case 2:
2521 525556 : return -__kernel_sin(y[0], y[1], 1);
2522 : default:
2523 528184 : return -__kernel_cos(y[0], y[1]);
2524 : }
2525 : }
2526 : }
2527 :
2528 : /* tan(x)
2529 : * Return tangent function of x.
2530 : *
2531 : * kernel function:
2532 : * __kernel_tan ... tangent function on [-pi/4,pi/4]
2533 : * __ieee754_rem_pio2 ... argument reduction routine
2534 : *
2535 : * Method.
2536 : * Let S,C and T denote the sin, cos and tan respectively on
2537 : * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
2538 : * in [-pi/4 , +pi/4], and let n = k mod 4.
2539 : * We have
2540 : *
2541 : * n sin(x) cos(x) tan(x)
2542 : * ----------------------------------------------------------
2543 : * 0 S C T
2544 : * 1 C -S -1/T
2545 : * 2 -S -C T
2546 : * 3 -C S -1/T
2547 : * ----------------------------------------------------------
2548 : *
2549 : * Special cases:
2550 : * Let trig be any of sin, cos, or tan.
2551 : * trig(+-INF) is NaN, with signals;
2552 : * trig(NaN) is that NaN;
2553 : *
2554 : * Accuracy:
2555 : * TRIG(x) returns trig(x) nearly rounded
2556 : */
2557 375926 : double tan(double x) {
2558 : double y[2], z = 0.0;
2559 : int32_t n, ix;
2560 :
2561 : /* High word of x. */
2562 375926 : GET_HIGH_WORD(ix, x);
2563 :
2564 : /* |x| ~< pi/4 */
2565 375926 : ix &= 0x7FFFFFFF;
2566 375926 : if (ix <= 0x3FE921FB) {
2567 2680 : return __kernel_tan(x, z, 1);
2568 373246 : } else if (ix >= 0x7FF00000) {
2569 : /* tan(Inf or NaN) is NaN */
2570 1165 : return x - x; /* NaN */
2571 : } else {
2572 : /* argument reduction needed */
2573 372081 : n = __ieee754_rem_pio2(x, y);
2574 : /* 1 -> n even, -1 -> n odd */
2575 372081 : return __kernel_tan(y[0], y[1], 1 - ((n & 1) << 1));
2576 : }
2577 : }
2578 :
2579 : /*
2580 : * ES6 draft 09-27-13, section 20.2.2.12.
2581 : * Math.cosh
2582 : * Method :
2583 : * mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2
2584 : * 1. Replace x by |x| (cosh(x) = cosh(-x)).
2585 : * 2.
2586 : * [ exp(x) - 1 ]^2
2587 : * 0 <= x <= ln2/2 : cosh(x) := 1 + -------------------
2588 : * 2*exp(x)
2589 : *
2590 : * exp(x) + 1/exp(x)
2591 : * ln2/2 <= x <= 22 : cosh(x) := -------------------
2592 : * 2
2593 : * 22 <= x <= lnovft : cosh(x) := exp(x)/2
2594 : * lnovft <= x <= ln2ovft: cosh(x) := exp(x/2)/2 * exp(x/2)
2595 : * ln2ovft < x : cosh(x) := huge*huge (overflow)
2596 : *
2597 : * Special cases:
2598 : * cosh(x) is |x| if x is +INF, -INF, or NaN.
2599 : * only cosh(0)=1 is exact for finite x.
2600 : */
2601 1120 : double cosh(double x) {
2602 : static const double KCOSH_OVERFLOW = 710.4758600739439;
2603 : static const double one = 1.0, half = 0.5;
2604 : static volatile double huge = 1.0e+300;
2605 :
2606 : int32_t ix;
2607 :
2608 : /* High word of |x|. */
2609 1120 : GET_HIGH_WORD(ix, x);
2610 1120 : ix &= 0x7FFFFFFF;
2611 :
2612 : // |x| in [0,0.5*log2], return 1+expm1(|x|)^2/(2*exp(|x|))
2613 1120 : if (ix < 0x3FD62E43) {
2614 261 : double t = expm1(fabs(x));
2615 261 : double w = one + t;
2616 : // For |x| < 2^-55, cosh(x) = 1
2617 261 : if (ix < 0x3C800000) return w;
2618 76 : return one + (t * t) / (w + w);
2619 : }
2620 :
2621 : // |x| in [0.5*log2, 22], return (exp(|x|)+1/exp(|x|)/2
2622 859 : if (ix < 0x40360000) {
2623 312 : double t = exp(fabs(x));
2624 312 : return half * t + half / t;
2625 : }
2626 :
2627 : // |x| in [22, log(maxdouble)], return half*exp(|x|)
2628 547 : if (ix < 0x40862E42) return half * exp(fabs(x));
2629 :
2630 : // |x| in [log(maxdouble), overflowthreshold]
2631 517 : if (fabs(x) <= KCOSH_OVERFLOW) {
2632 18 : double w = exp(half * fabs(x));
2633 18 : double t = half * w;
2634 18 : return t * w;
2635 : }
2636 :
2637 : /* x is INF or NaN */
2638 499 : if (ix >= 0x7FF00000) return x * x;
2639 :
2640 : // |x| > overflowthreshold.
2641 360 : return huge * huge;
2642 : }
2643 :
2644 : /*
2645 : * ES6 draft 09-27-13, section 20.2.2.30.
2646 : * Math.sinh
2647 : * Method :
2648 : * mathematically sinh(x) if defined to be (exp(x)-exp(-x))/2
2649 : * 1. Replace x by |x| (sinh(-x) = -sinh(x)).
2650 : * 2.
2651 : * E + E/(E+1)
2652 : * 0 <= x <= 22 : sinh(x) := --------------, E=expm1(x)
2653 : * 2
2654 : *
2655 : * 22 <= x <= lnovft : sinh(x) := exp(x)/2
2656 : * lnovft <= x <= ln2ovft: sinh(x) := exp(x/2)/2 * exp(x/2)
2657 : * ln2ovft < x : sinh(x) := x*shuge (overflow)
2658 : *
2659 : * Special cases:
2660 : * sinh(x) is |x| if x is +Infinity, -Infinity, or NaN.
2661 : * only sinh(0)=0 is exact for finite x.
2662 : */
2663 1219 : double sinh(double x) {
2664 : static const double KSINH_OVERFLOW = 710.4758600739439,
2665 : TWO_M28 =
2666 : 3.725290298461914e-9, // 2^-28, empty lower half
2667 : LOG_MAXD = 709.7822265625; // 0x40862E42 00000000, empty lower half
2668 : static const double shuge = 1.0e307;
2669 :
2670 1219 : double h = (x < 0) ? -0.5 : 0.5;
2671 : // |x| in [0, 22]. return sign(x)*0.5*(E+E/(E+1))
2672 1219 : double ax = fabs(x);
2673 1219 : if (ax < 22) {
2674 : // For |x| < 2^-28, sinh(x) = x
2675 636 : if (ax < TWO_M28) return x;
2676 419 : double t = expm1(ax);
2677 419 : if (ax < 1) {
2678 128 : return h * (2 * t - t * t / (t + 1));
2679 : }
2680 291 : return h * (t + t / (t + 1));
2681 : }
2682 : // |x| in [22, log(maxdouble)], return 0.5 * exp(|x|)
2683 583 : if (ax < LOG_MAXD) return h * exp(ax);
2684 : // |x| in [log(maxdouble), overflowthreshold]
2685 : // overflowthreshold = 710.4758600739426
2686 535 : if (ax <= KSINH_OVERFLOW) {
2687 18 : double w = exp(0.5 * ax);
2688 18 : double t = h * w;
2689 18 : return t * w;
2690 : }
2691 : // |x| > overflowthreshold or is NaN.
2692 : // Return Infinity of the appropriate sign or NaN.
2693 517 : return x * shuge;
2694 : }
2695 :
2696 : /* Tanh(x)
2697 : * Return the Hyperbolic Tangent of x
2698 : *
2699 : * Method :
2700 : * x -x
2701 : * e - e
2702 : * 0. tanh(x) is defined to be -----------
2703 : * x -x
2704 : * e + e
2705 : * 1. reduce x to non-negative by tanh(-x) = -tanh(x).
2706 : * 2. 0 <= x < 2**-28 : tanh(x) := x with inexact if x != 0
2707 : * -t
2708 : * 2**-28 <= x < 1 : tanh(x) := -----; t = expm1(-2x)
2709 : * t + 2
2710 : * 2
2711 : * 1 <= x < 22 : tanh(x) := 1 - -----; t = expm1(2x)
2712 : * t + 2
2713 : * 22 <= x <= INF : tanh(x) := 1.
2714 : *
2715 : * Special cases:
2716 : * tanh(NaN) is NaN;
2717 : * only tanh(0)=0 is exact for finite argument.
2718 : */
2719 1129 : double tanh(double x) {
2720 : static const volatile double tiny = 1.0e-300;
2721 : static const double one = 1.0, two = 2.0, huge = 1.0e300;
2722 : double t, z;
2723 : int32_t jx, ix;
2724 :
2725 1129 : GET_HIGH_WORD(jx, x);
2726 1129 : ix = jx & 0x7FFFFFFF;
2727 :
2728 : /* x is INF or NaN */
2729 1129 : if (ix >= 0x7FF00000) {
2730 121 : if (jx >= 0)
2731 89 : return one / x + one; /* tanh(+-inf)=+-1 */
2732 : else
2733 32 : return one / x - one; /* tanh(NaN) = NaN */
2734 : }
2735 :
2736 : /* |x| < 22 */
2737 1008 : if (ix < 0x40360000) { /* |x|<22 */
2738 618 : if (ix < 0x3E300000) { /* |x|<2**-28 */
2739 199 : if (huge + x > one) return x; /* tanh(tiny) = tiny with inexact */
2740 : }
2741 419 : if (ix >= 0x3FF00000) { /* |x|>=1 */
2742 273 : t = expm1(two * fabs(x));
2743 273 : z = one - two / (t + two);
2744 : } else {
2745 146 : t = expm1(-two * fabs(x));
2746 146 : z = -t / (t + two);
2747 : }
2748 : /* |x| >= 22, return +-1 */
2749 : } else {
2750 390 : z = one - tiny; /* raise inexact flag */
2751 : }
2752 809 : return (jx >= 0) ? z : -z;
2753 : }
2754 :
2755 : } // namespace ieee754
2756 : } // namespace base
2757 : } // namespace v8
|