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 : union ieee_double_shape_type {
61 : double value;
62 : struct {
63 : uint32_t lsw;
64 : uint32_t msw;
65 : } parts;
66 : struct {
67 : uint64_t w;
68 : } xparts;
69 : };
70 :
71 : #else
72 :
73 : union ieee_double_shape_type {
74 : double value;
75 : struct {
76 : uint32_t msw;
77 : uint32_t lsw;
78 : } parts;
79 : struct {
80 : uint64_t w;
81 : } xparts;
82 : };
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 2427498 : 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 2427498 : GET_HIGH_WORD(hx, x); /* high word of x */
230 2427498 : ix = hx & 0x7FFFFFFF;
231 2427498 : 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 2427498 : if (ix < 0x4002D97C) { /* |x| < 3pi/4, special case with n=+-1 */
237 998600 : if (hx > 0) {
238 996397 : z = x - pio2_1;
239 996397 : if (ix != 0x3FF921FB) { /* 33+53 bit pi is good enough */
240 4952 : y[0] = z - pio2_1t;
241 4952 : y[1] = (z - y[0]) - pio2_1t;
242 : } else { /* near pi/2, use 33+33+53 bit pi */
243 991445 : z -= pio2_2;
244 991445 : y[0] = z - pio2_2t;
245 991445 : y[1] = (z - y[0]) - pio2_2t;
246 : }
247 : return 1;
248 : } else { /* negative x */
249 2203 : z = x + pio2_1;
250 2203 : if (ix != 0x3FF921FB) { /* 33+53 bit pi is good enough */
251 2048 : y[0] = z + pio2_1t;
252 2048 : y[1] = (z - y[0]) + pio2_1t;
253 : } else { /* near pi/2, use 33+33+53 bit pi */
254 155 : z += pio2_2;
255 155 : y[0] = z + pio2_2t;
256 155 : y[1] = (z - y[0]) + pio2_2t;
257 : }
258 : return -1;
259 : }
260 : }
261 1428898 : if (ix <= 0x413921FB) { /* |x| ~<= 2^19*(pi/2), medium size */
262 1413574 : t = fabs(x);
263 1413574 : n = static_cast<int32_t>(t * invpio2 + half);
264 1413574 : fn = static_cast<double>(n);
265 1413574 : r = t - fn * pio2_1;
266 1413574 : w = fn * pio2_1t; /* 1st round good to 85 bit */
267 1413574 : if (n < 32 && ix != npio2_hw[n - 1]) {
268 26794 : y[0] = r - w; /* quick check no cancellation */
269 : } else {
270 : uint32_t high;
271 1386780 : j = ix >> 20;
272 1386780 : y[0] = r - w;
273 1386780 : GET_HIGH_WORD(high, y[0]);
274 1386780 : i = j - ((high >> 20) & 0x7FF);
275 1386780 : if (i > 16) { /* 2nd iteration needed, good to 118 */
276 : t = r;
277 727100 : w = fn * pio2_2;
278 727100 : r = t - w;
279 727100 : w = fn * pio2_2t - ((t - r) - w);
280 727100 : y[0] = r - w;
281 727100 : GET_HIGH_WORD(high, y[0]);
282 727100 : i = j - ((high >> 20) & 0x7FF);
283 727100 : if (i > 49) { /* 3rd iteration need, 151 bits acc */
284 : t = r; /* will cover all possible cases */
285 152 : w = fn * pio2_3;
286 152 : r = t - w;
287 152 : w = fn * pio2_3t - ((t - r) - w);
288 152 : y[0] = r - w;
289 : }
290 : }
291 : }
292 1413574 : y[1] = (r - y[0]) - w;
293 1413574 : if (hx < 0) {
294 3662 : y[0] = -y[0];
295 3662 : y[1] = -y[1];
296 3662 : return -n;
297 : } else {
298 : return n;
299 : }
300 : }
301 : /*
302 : * all other (large) arguments
303 : */
304 15324 : 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 15324 : GET_LOW_WORD(low, x);
310 15324 : SET_LOW_WORD(z, low);
311 15324 : e0 = (ix >> 20) - 1046; /* e0 = ilogb(z)-23; */
312 15324 : SET_HIGH_WORD(z, ix - static_cast<int32_t>(static_cast<uint32_t>(e0) << 20));
313 76620 : for (i = 0; i < 2; i++) {
314 30648 : tx[i] = static_cast<double>(static_cast<int32_t>(z));
315 30648 : z = (z - tx[i]) * two24;
316 : }
317 15324 : tx[2] = z;
318 : nx = 3;
319 38252 : while (tx[nx - 1] == zero) nx--; /* skip zero term */
320 15324 : n = __kernel_rem_pio2(tx, y, e0, nx, 2, two_over_pi);
321 15324 : if (hx < 0) {
322 3841 : y[0] = -y[0];
323 3841 : y[1] = -y[1];
324 3841 : 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 9463472 : GET_HIGH_WORD(ix, x);
375 9463472 : ix &= 0x7FFFFFFF; /* ix = |x|'s high word*/
376 9463472 : if (ix < 0x3E400000) { /* if x < 2**27 */
377 295690 : if (static_cast<int>(x) == 0) return one; /* generate inexact */
378 : }
379 9167782 : z = x * x;
380 9167782 : r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * C6)))));
381 9167782 : if (ix < 0x3FD33333) { /* if |x| < 0.3 */
382 8840396 : return one - (0.5 * z - (z * r - x * y));
383 : } else {
384 327386 : if (ix > 0x3FE90000) { /* x > 0.78125 */
385 : qx = 0.28125;
386 : } else {
387 324316 : INSERT_WORDS(qx, ix - 0x00200000, 0); /* x/4 */
388 : }
389 327386 : iz = 0.5 * z - qx;
390 327386 : a = one - qx;
391 327386 : 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 15324 : 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 15324 : jk = init_jk[prec];
532 : jp = jk;
533 :
534 : /* determine jx,jv,q0, note that 3>q0 */
535 15324 : jx = nx - 1;
536 15324 : jv = (e0 - 3) / 24;
537 15324 : if (jv < 0) jv = 0;
538 15324 : q0 = e0 - 24 * (jv + 1);
539 :
540 : /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */
541 15324 : j = jv - jx;
542 15324 : m = jx + jk;
543 184004 : for (i = 0; i <= m; i++, j++) {
544 84340 : f[i] = (j < 0) ? zero : static_cast<double>(ipio2[j]);
545 : }
546 :
547 : /* compute q[0],q[1],...q[jk] */
548 168564 : for (i = 0; i <= jk; i++) {
549 191840 : for (j = 0, fw = 0.0; j <= jx; j++) fw += x[j] * f[jx + i - j];
550 76620 : q[i] = fw;
551 : }
552 :
553 : jz = jk;
554 : recompute:
555 : /* distill q[] into iq[] reversingly */
556 87684 : for (i = 0, j = jz, z = q[jz]; j > 0; i++, j--) {
557 70516 : fw = static_cast<double>(static_cast<int32_t>(twon24 * z));
558 70516 : iq[i] = static_cast<int32_t>(z - two24 * fw);
559 70516 : z = q[j - 1] + fw;
560 : }
561 :
562 : /* compute n */
563 17168 : z = scalbn(z, q0); /* actual value of z */
564 17168 : z -= 8.0 * floor(z * 0.125); /* trim off integer >= 8 */
565 17168 : n = static_cast<int32_t>(z);
566 17168 : z -= static_cast<double>(n);
567 : ih = 0;
568 17168 : if (q0 > 0) { /* need iq[jz-1] to determine n */
569 1624 : i = (iq[jz - 1] >> (24 - q0));
570 1624 : n += i;
571 1624 : iq[jz - 1] -= i << (24 - q0);
572 1624 : ih = iq[jz - 1] >> (23 - q0);
573 15544 : } else if (q0 == 0) {
574 668 : ih = iq[jz - 1] >> 23;
575 14876 : } else if (z >= 0.5) {
576 : ih = 2;
577 : }
578 :
579 17168 : if (ih > 0) { /* q > 0.5 */
580 9330 : n += 1;
581 : carry = 0;
582 85906 : for (i = 0; i < jz; i++) { /* compute 1-q */
583 38288 : j = iq[i];
584 38288 : if (carry == 0) {
585 11970 : if (j != 0) {
586 : carry = 1;
587 9330 : iq[i] = 0x1000000 - j;
588 : }
589 : } else {
590 26318 : iq[i] = 0xFFFFFF - j;
591 : }
592 : }
593 9330 : if (q0 > 0) { /* rare case: chance is 1 in 12 */
594 892 : switch (q0) {
595 : case 1:
596 620 : iq[jz - 1] &= 0x7FFFFF;
597 620 : break;
598 : case 2:
599 272 : iq[jz - 1] &= 0x3FFFFF;
600 272 : break;
601 : }
602 : }
603 9330 : if (ih == 2) {
604 8090 : z = one - z;
605 8090 : if (carry != 0) z -= scalbn(one, q0);
606 : }
607 : }
608 :
609 : /* check if recomputation is needed */
610 17168 : if (z == zero) {
611 : j = 0;
612 3688 : for (i = jz - 1; i >= jk; i--) j |= iq[i];
613 3688 : if (j == 0) { /* need recomputation */
614 1844 : for (k = 1; jk >= k && iq[jk - k] == 0; k++) {
615 : /* k = no. of terms needed */
616 : }
617 :
618 3688 : for (i = jz + 1; i <= jz + k; i++) { /* add q[jz+1] to q[jz+k] */
619 1844 : f[jx + i] = ipio2[jv + i];
620 4132 : for (j = 0, fw = 0.0; j <= jx; j++) fw += x[j] * f[jx + i - j];
621 1844 : q[i] = fw;
622 : }
623 : jz += k;
624 : goto recompute;
625 : }
626 : }
627 :
628 : /* chop off zero terms */
629 15324 : if (z == 0.0) {
630 1844 : jz -= 1;
631 1844 : q0 -= 24;
632 1844 : while (iq[jz] == 0) {
633 0 : jz--;
634 0 : q0 -= 24;
635 : }
636 : } else { /* break z into 24-bit if necessary */
637 13480 : z = scalbn(z, -q0);
638 13480 : if (z >= two24) {
639 8 : fw = static_cast<double>(static_cast<int32_t>(twon24 * z));
640 8 : iq[jz] = z - two24 * fw;
641 8 : jz += 1;
642 8 : q0 += 24;
643 8 : iq[jz] = fw;
644 : } else {
645 13472 : iq[jz] = z;
646 : }
647 : }
648 :
649 : /* convert integer "bit" chunk to floating-point value */
650 15324 : fw = scalbn(one, q0);
651 168580 : for (i = jz; i >= 0; i--) {
652 76628 : q[i] = fw * iq[i];
653 76628 : fw *= twon24;
654 : }
655 :
656 : /* compute PIo2[0,...,jp]*q[jz,...,0] */
657 168580 : for (i = jz; i >= 0; i--) {
658 306528 : for (fw = 0.0, k = 0; k <= jp && k <= jz - i; k++) fw += PIo2[k] * q[i + k];
659 76628 : fq[jz - i] = fw;
660 : }
661 :
662 : /* compress fq[] into y[] */
663 15324 : 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 91952 : for (i = jz; i >= 0; i--) fw += fq[i];
673 15324 : y[0] = (ih == 0) ? fw : -fw;
674 15324 : fw = fq[0] - fw;
675 76628 : for (i = 1; i <= jz; i++) fw += fq[i];
676 15324 : y[1] = (ih == 0) ? fw : -fw;
677 15324 : 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 15324 : 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 10482528 : GET_HIGH_WORD(ix, x);
743 10482528 : ix &= 0x7FFFFFFF; /* high word of x */
744 10482528 : if (ix < 0x3E400000) { /* |x| < 2**-27 */
745 1314976 : if (static_cast<int>(x) == 0) return x;
746 : } /* generate inexact */
747 9167552 : z = x * x;
748 9167552 : v = z * x;
749 9167552 : r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6)));
750 : if (iy == 0) {
751 8635924 : return x + v * (S1 + z * r);
752 : } else {
753 531628 : 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 373701 : 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 373701 : GET_HIGH_WORD(hx, x); /* high word of x */
818 373701 : ix = hx & 0x7FFFFFFF; /* high word of |x| */
819 373701 : if (ix < 0x3E300000) { /* x < 2**-28 */
820 1590 : if (static_cast<int>(x) == 0) { /* generate inexact */
821 : uint32_t low;
822 1590 : GET_LOW_WORD(low, x);
823 1590 : if (((ix | low) | (iy + 1)) == 0) {
824 0 : return one / fabs(x);
825 : } else {
826 1590 : if (iy == 1) {
827 : return x;
828 : } else { /* compute -1 / (x+y) carefully */
829 : double a, t;
830 :
831 132 : z = w = x + y;
832 132 : SET_LOW_WORD(z, 0);
833 132 : v = y - (z - x);
834 132 : t = a = -one / w;
835 132 : SET_LOW_WORD(t, 0);
836 132 : s = one + t * z;
837 132 : return t + a * (s + t * v);
838 : }
839 : }
840 : }
841 : }
842 372111 : if (ix >= 0x3FE59428) { /* |x| >= 0.6744 */
843 52617 : if (hx < 0) {
844 26536 : x = -x;
845 26536 : y = -y;
846 : }
847 52617 : z = pio4 - x;
848 52617 : w = pio4lo - y;
849 52617 : x = z + w;
850 : y = 0.0;
851 : }
852 372111 : z = x * x;
853 372111 : 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 372111 : r = T[1] + w * (T[3] + w * (T[5] + w * (T[7] + w * (T[9] + w * T[11]))));
860 : v = z *
861 372111 : (T[2] + w * (T[4] + w * (T[6] + w * (T[8] + w * (T[10] + w * T[12])))));
862 372111 : s = z * x;
863 372111 : r = y + z * (s * (r + v) + y);
864 372111 : r += T[0] * s;
865 372111 : w = x + r;
866 372111 : if (ix >= 0x3FE59428) {
867 52617 : v = iy;
868 52617 : return (1 - ((hx >> 30) & 2)) * (v - 2.0 * (x - (w * w / (w + v) - r)));
869 : }
870 319494 : 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 160256 : SET_LOW_WORD(z, 0);
881 160256 : v = r - (z - x); /* z+v = r+x */
882 160256 : t = a = -1.0 / w; /* a = -1.0/w */
883 160256 : SET_LOW_WORD(t, 0);
884 160256 : s = 1.0 + t * z;
885 160256 : 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 9390 : 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 9390 : GET_HIGH_WORD(hx, x);
939 9390 : ix = hx & 0x7FFFFFFF;
940 9390 : if (ix >= 0x3FF00000) { /* |x| >= 1 */
941 : uint32_t lx;
942 6527 : GET_LOW_WORD(lx, x);
943 6527 : if (((ix - 0x3FF00000) | lx) == 0) { /* |x|==1 */
944 675 : if (hx > 0)
945 : return 0.0; /* acos(1) = 0 */
946 : else
947 328 : 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 2863 : if (ix < 0x3FE00000) { /* |x| < 0.5 */
952 2235 : if (ix <= 0x3C600000) return pio2_hi + pio2_lo; /*if|x|<2**-57*/
953 1074 : z = x * x;
954 1074 : p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
955 1074 : q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
956 1074 : r = p / q;
957 1074 : return pio2_hi - (x - (pio2_lo - x * r));
958 628 : } else if (hx < 0) { /* x < -0.5 */
959 296 : z = (one + x) * 0.5;
960 296 : p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
961 296 : q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
962 296 : s = sqrt(z);
963 296 : r = p / q;
964 296 : w = r * s - pio2_lo;
965 296 : return pi - 2.0 * (s + w);
966 : } else { /* x > 0.5 */
967 332 : z = (one - x) * 0.5;
968 332 : s = sqrt(z);
969 : df = s;
970 332 : SET_LOW_WORD(df, 0);
971 332 : c = (z - df * df) / (s + df);
972 332 : p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
973 332 : q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
974 332 : r = p / q;
975 332 : w = r * s + c;
976 332 : 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 909 : 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 909 : EXTRACT_WORDS(hx, lx, x);
1001 909 : if (hx < 0x3FF00000) { /* x < 1 */
1002 : return std::numeric_limits<double>::signaling_NaN();
1003 440 : } else if (hx >= 0x41B00000) { /* x > 2**28 */
1004 211 : if (hx >= 0x7FF00000) { /* x is inf of NaN */
1005 85 : return x + x;
1006 : } else {
1007 126 : return log(x) + ln2; /* acosh(huge)=log(2x) */
1008 : }
1009 229 : } else if (((hx - 0x3FF00000) | lx) == 0) {
1010 : return 0.0; /* acosh(1) = 0 */
1011 211 : } else if (hx > 0x40000000) { /* 2**28 > x > 2 */
1012 113 : t = x * x;
1013 113 : return log(2.0 * x - one / (x + sqrt(t - one)));
1014 : } else { /* 1<x<2 */
1015 98 : t = x - one;
1016 98 : 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 10977 : 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 10977 : GET_HIGH_WORD(hx, x);
1072 10977 : ix = hx & 0x7FFFFFFF;
1073 10977 : if (ix >= 0x3FF00000) { /* |x|>= 1 */
1074 : uint32_t lx;
1075 8031 : GET_LOW_WORD(lx, x);
1076 8031 : if (((ix - 0x3FF00000) | lx) == 0) { /* asin(1)=+-pi/2 with inexact */
1077 764 : return x * pio2_hi + x * pio2_lo;
1078 : }
1079 : return std::numeric_limits<double>::signaling_NaN(); // asin(|x|>1) is NaN
1080 2946 : } else if (ix < 0x3FE00000) { /* |x|<0.5 */
1081 2318 : if (ix < 0x3E400000) { /* if |x| < 2**-27 */
1082 1480 : if (huge + x > one) return x; /* return x with inexact if x!=0*/
1083 : } else {
1084 838 : t = x * x;
1085 : }
1086 838 : p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5)))));
1087 838 : q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4)));
1088 838 : w = p / q;
1089 838 : return x + x * w;
1090 : }
1091 : /* 1> |x|>= 0.5 */
1092 628 : w = one - fabs(x);
1093 628 : t = w * 0.5;
1094 628 : p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5)))));
1095 628 : q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4)));
1096 628 : s = sqrt(t);
1097 628 : 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 628 : SET_LOW_WORD(w, 0);
1103 628 : c = (t - w * w) / (s + w);
1104 628 : r = p / q;
1105 628 : p = 2.0 * s * r - (pio2_lo - 2.0 * c);
1106 628 : q = pio4_hi - 2.0 * w;
1107 628 : t = pio4_hi - (p - q);
1108 : }
1109 628 : if (hx > 0)
1110 : return t;
1111 : else
1112 296 : 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 1077 : 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 1077 : GET_HIGH_WORD(hx, x);
1133 1077 : ix = hx & 0x7FFFFFFF;
1134 1077 : if (ix >= 0x7FF00000) return x + x; /* x is inf or NaN */
1135 948 : if (ix < 0x3E300000) { /* |x|<2**-28 */
1136 216 : if (huge + x > one) return x; /* return x inexact except 0 */
1137 : }
1138 732 : if (ix > 0x41B00000) { /* |x| > 2**28 */
1139 310 : w = log(fabs(x)) + ln2;
1140 422 : } else if (ix > 0x40000000) { /* 2**28 > |x| > 2.0 */
1141 198 : t = fabs(x);
1142 198 : w = log(2.0 * t + one / (sqrt(x * x + one) + t));
1143 : } else { /* 2.0 > |x| > 2**-28 */
1144 224 : t = x * x;
1145 224 : w = log1p(fabs(x) + t / (one + sqrt(one + t)));
1146 : }
1147 732 : if (hx > 0) {
1148 : return w;
1149 : } else {
1150 316 : 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 47473 : 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 47473 : GET_HIGH_WORD(hx, x);
1208 47473 : ix = hx & 0x7FFFFFFF;
1209 47473 : if (ix >= 0x44100000) { /* if |x| >= 2^66 */
1210 : uint32_t low;
1211 2908 : GET_LOW_WORD(low, x);
1212 2908 : if (ix > 0x7FF00000 || (ix == 0x7FF00000 && (low != 0)))
1213 374 : return x + x; /* NaN */
1214 2534 : if (hx > 0)
1215 1298 : return atanhi[3] + *const_cast<volatile double*>(&atanlo[3]);
1216 : else
1217 1236 : return -atanhi[3] - *const_cast<volatile double*>(&atanlo[3]);
1218 : }
1219 44565 : if (ix < 0x3FDC0000) { /* |x| < 0.4375 */
1220 23268 : if (ix < 0x3E400000) { /* |x| < 2^-27 */
1221 16096 : if (huge + x > one) return x; /* raise inexact */
1222 : }
1223 : id = -1;
1224 : } else {
1225 21297 : x = fabs(x);
1226 21297 : if (ix < 0x3FF30000) { /* |x| < 1.1875 */
1227 4231 : if (ix < 0x3FE60000) { /* 7/16 <=|x|<11/16 */
1228 : id = 0;
1229 671 : x = (2.0 * x - one) / (2.0 + x);
1230 : } else { /* 11/16<=|x|< 19/16 */
1231 : id = 1;
1232 3560 : x = (x - one) / (x + one);
1233 : }
1234 : } else {
1235 17066 : if (ix < 0x40038000) { /* |x| < 2.4375 */
1236 : id = 2;
1237 1108 : x = (x - 1.5) / (one + 1.5 * x);
1238 : } else { /* 2.4375 <= |x| < 2^66 */
1239 : id = 3;
1240 15958 : x = -1.0 / x;
1241 : }
1242 : }
1243 : }
1244 : /* end of argument reduction */
1245 28469 : z = x * x;
1246 28469 : w = z * z;
1247 : /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
1248 28469 : s1 = z * (aT[0] +
1249 56938 : w * (aT[2] + w * (aT[4] + w * (aT[6] + w * (aT[8] + w * aT[10])))));
1250 28469 : s2 = w * (aT[1] + w * (aT[3] + w * (aT[5] + w * (aT[7] + w * aT[9]))));
1251 28469 : if (id < 0) {
1252 7172 : return x - x * (s1 + s2);
1253 : } else {
1254 21297 : z = atanhi[id] - ((x * (s1 + s2) - atanlo[id]) - x);
1255 21297 : 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 101666 : 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 101666 : EXTRACT_WORDS(hx, lx, x);
1300 101666 : ix = hx & 0x7FFFFFFF;
1301 101666 : EXTRACT_WORDS(hy, ly, y);
1302 101666 : iy = hy & 0x7FFFFFFF;
1303 300363 : if (((ix | ((lx | NegateWithWraparound<int32_t>(lx)) >> 31)) > 0x7FF00000) ||
1304 194062 : ((iy | ((ly | NegateWithWraparound<int32_t>(ly)) >> 31)) > 0x7FF00000)) {
1305 9112 : return x + y; /* x or y is NaN */
1306 : }
1307 92554 : if ((SubWithWraparound(hx, 0x3FF00000) | lx) == 0) {
1308 4056 : return atan(y); /* x=1.0 */
1309 : }
1310 88498 : m = ((hy >> 31) & 1) | ((hx >> 30) & 2); /* 2*sign(x)+sign(y) */
1311 :
1312 : /* when y = 0 */
1313 88498 : if ((iy | ly) == 0) {
1314 8368 : switch (m) {
1315 : case 0:
1316 : case 1:
1317 : return y; /* atan(+-0,+anything)=+-0 */
1318 : case 2:
1319 2194 : return pi + tiny; /* atan(+0,-anything) = pi */
1320 : case 3:
1321 2194 : return -pi - tiny; /* atan(-0,-anything) =-pi */
1322 : }
1323 : }
1324 : /* when x = 0 */
1325 80130 : if ((ix | lx) == 0) return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny;
1326 :
1327 : /* when x is INF */
1328 71914 : if (ix == 0x7FF00000) {
1329 8265 : if (iy == 0x7FF00000) {
1330 336 : switch (m) {
1331 : case 0:
1332 84 : return pi_o_4 + tiny; /* atan(+INF,+INF) */
1333 : case 1:
1334 84 : return -pi_o_4 - tiny; /* atan(-INF,+INF) */
1335 : case 2:
1336 84 : return 3.0 * pi_o_4 + tiny; /*atan(+INF,-INF)*/
1337 : case 3:
1338 84 : return -3.0 * pi_o_4 - tiny; /*atan(-INF,-INF)*/
1339 : }
1340 : } else {
1341 7929 : switch (m) {
1342 : case 0:
1343 : return zero; /* atan(+...,+INF) */
1344 : case 1:
1345 2055 : return -zero; /* atan(-...,+INF) */
1346 : case 2:
1347 1914 : return pi + tiny; /* atan(+...,-INF) */
1348 : case 3:
1349 2046 : return -pi - tiny; /* atan(-...,-INF) */
1350 : }
1351 : }
1352 : }
1353 : /* when y is INF */
1354 63649 : if (iy == 0x7FF00000) return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny;
1355 :
1356 : /* compute y/x */
1357 55873 : k = (iy - ix) >> 20;
1358 55873 : if (k > 60) { /* |y/x| > 2**60 */
1359 14710 : z = pi_o_2 + 0.5 * pi_lo;
1360 14710 : m &= 1;
1361 41163 : } else if (hx < 0 && k < -60) {
1362 : z = 0.0; /* 0 > |y|/x > -2**-60 */
1363 : } else {
1364 34059 : z = atan(fabs(y / x)); /* safe to do y/x */
1365 : }
1366 55873 : switch (m) {
1367 : case 0:
1368 : return z; /* atan(+,+) */
1369 : case 1:
1370 16136 : return -z; /* atan(-,+) */
1371 : case 2:
1372 10500 : return pi - (z - pi_lo); /* atan(+,-) */
1373 : default: /* case 3 */
1374 10072 : 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 9932932 : double cos(double x) {
1409 : double y[2], z = 0.0;
1410 : int32_t n, ix;
1411 :
1412 : /* High word of x. */
1413 9932932 : GET_HIGH_WORD(ix, x);
1414 :
1415 : /* |x| ~< pi/4 */
1416 9932932 : ix &= 0x7FFFFFFF;
1417 9932932 : if (ix <= 0x3FE921FB) {
1418 8930339 : return __kernel_cos(x, z);
1419 1002593 : } else if (ix >= 0x7FF00000) {
1420 : /* cos(Inf or NaN) is NaN */
1421 1070 : return x - x;
1422 : } else {
1423 : /* argument reduction needed */
1424 1001523 : n = __ieee754_rem_pio2(x, y);
1425 1001523 : switch (n & 3) {
1426 : case 0:
1427 8780 : return __kernel_cos(y[0], y[1]);
1428 : case 1:
1429 1987738 : return -__kernel_sin(y[0], y[1], 1);
1430 : case 2:
1431 1722 : return -__kernel_cos(y[0], y[1]);
1432 : default:
1433 4806 : 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 202560 : 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 202560 : GET_HIGH_WORD(hx, x);
1528 202560 : xsb = (hx >> 31) & 1; /* sign bit of x */
1529 202560 : hx &= 0x7FFFFFFF; /* high word of |x| */
1530 :
1531 : /* filter out non-finite argument */
1532 202560 : if (hx >= 0x40862E42) { /* if |x|>=709.78... */
1533 4607 : if (hx >= 0x7FF00000) {
1534 : uint32_t lx;
1535 1110 : GET_LOW_WORD(lx, x);
1536 1110 : if (((hx & 0xFFFFF) | lx) != 0)
1537 398 : return x + x; /* NaN */
1538 : else
1539 712 : return (xsb == 0) ? x : 0.0; /* exp(+-inf)={inf,0} */
1540 : }
1541 3497 : if (x > o_threshold) return huge * huge; /* overflow */
1542 1831 : if (x < u_threshold) return twom1000 * twom1000; /* underflow */
1543 : }
1544 :
1545 : /* argument reduction */
1546 197956 : if (hx > 0x3FD62E42) { /* if |x| > 0.5 ln2 */
1547 195647 : 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 1693 : if (x == 1.0) return E;
1553 1187 : hi = x - ln2HI[xsb];
1554 1187 : lo = ln2LO[xsb];
1555 1187 : k = 1 - xsb - xsb;
1556 : } else {
1557 193954 : k = static_cast<int>(invln2 * x + halF[xsb]);
1558 193954 : t = k;
1559 193954 : hi = x - t * ln2HI[0]; /* t*ln2HI is exact here */
1560 193954 : lo = t * ln2LO[0];
1561 : }
1562 195141 : STRICT_ASSIGN(double, x, hi - lo);
1563 2309 : } else if (hx < 0x3E300000) { /* when |x|<2**-28 */
1564 1411 : if (huge + x > one) return one + x; /* trigger inexact */
1565 : } else {
1566 : k = 0;
1567 : }
1568 :
1569 : /* x is now in primary range */
1570 196039 : t = x * x;
1571 196039 : if (k >= -1021) {
1572 196037 : INSERT_WORDS(
1573 : twopk,
1574 : 0x3FF00000 + static_cast<int32_t>(static_cast<uint32_t>(k) << 20), 0);
1575 : } else {
1576 2 : INSERT_WORDS(twopk, 0x3FF00000 + (static_cast<uint32_t>(k + 1000) << 20),
1577 : 0);
1578 : }
1579 196039 : c = x - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
1580 196039 : if (k == 0) {
1581 898 : return one - ((x * c) / (c - 2.0) - x);
1582 : } else {
1583 195141 : y = one - ((lo - (x * c) / (2.0 - c)) - hi);
1584 : }
1585 195141 : if (k >= -1021) {
1586 195139 : if (k == 1024) return y * 2.0 * two1023;
1587 195101 : return y * twopk;
1588 : } else {
1589 2 : return y * twopk * twom1000;
1590 : }
1591 : }
1592 :
1593 : /*
1594 : * Method :
1595 : * 1.Reduced x to positive by atanh(-x) = -atanh(x)
1596 : * 2.For x>=0.5
1597 : * 1 2x x
1598 : * atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------)
1599 : * 2 1 - x 1 - x
1600 : *
1601 : * For x<0.5
1602 : * atanh(x) = 0.5*log1p(2x+2x*x/(1-x))
1603 : *
1604 : * Special cases:
1605 : * atanh(x) is NaN if |x| > 1 with signal;
1606 : * atanh(NaN) is that NaN with no signal;
1607 : * atanh(+-1) is +-INF with signal.
1608 : *
1609 : */
1610 1127 : double atanh(double x) {
1611 : static const double one = 1.0, huge = 1e300;
1612 : static const double zero = 0.0;
1613 :
1614 : double t;
1615 : int32_t hx, ix;
1616 : uint32_t lx;
1617 1127 : EXTRACT_WORDS(hx, lx, x);
1618 1127 : ix = hx & 0x7FFFFFFF;
1619 2254 : if ((ix | ((lx | NegateWithWraparound<int32_t>(lx)) >> 31)) > 0x3FF00000) {
1620 : /* |x|>1 */
1621 : return std::numeric_limits<double>::signaling_NaN();
1622 : }
1623 473 : if (ix == 0x3FF00000) {
1624 : return x > 0 ? std::numeric_limits<double>::infinity()
1625 44 : : -std::numeric_limits<double>::infinity();
1626 : }
1627 429 : if (ix < 0x3E300000 && (huge + x) > zero) return x; /* x<2**-28 */
1628 209 : SET_HIGH_WORD(x, ix);
1629 209 : if (ix < 0x3FE00000) { /* x < 0.5 */
1630 84 : t = x + x;
1631 84 : t = 0.5 * log1p(t + t * x / (one - x));
1632 : } else {
1633 125 : t = 0.5 * log1p((x + x) / (one - x));
1634 : }
1635 209 : if (hx >= 0)
1636 : return t;
1637 : else
1638 84 : return -t;
1639 : }
1640 :
1641 : /* log(x)
1642 : * Return the logrithm of x
1643 : *
1644 : * Method :
1645 : * 1. Argument Reduction: find k and f such that
1646 : * x = 2^k * (1+f),
1647 : * where sqrt(2)/2 < 1+f < sqrt(2) .
1648 : *
1649 : * 2. Approximation of log(1+f).
1650 : * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
1651 : * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
1652 : * = 2s + s*R
1653 : * We use a special Reme algorithm on [0,0.1716] to generate
1654 : * a polynomial of degree 14 to approximate R The maximum error
1655 : * of this polynomial approximation is bounded by 2**-58.45. In
1656 : * other words,
1657 : * 2 4 6 8 10 12 14
1658 : * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s
1659 : * (the values of Lg1 to Lg7 are listed in the program)
1660 : * and
1661 : * | 2 14 | -58.45
1662 : * | Lg1*s +...+Lg7*s - R(z) | <= 2
1663 : * | |
1664 : * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
1665 : * In order to guarantee error in log below 1ulp, we compute log
1666 : * by
1667 : * log(1+f) = f - s*(f - R) (if f is not too large)
1668 : * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
1669 : *
1670 : * 3. Finally, log(x) = k*ln2 + log(1+f).
1671 : * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
1672 : * Here ln2 is split into two floating point number:
1673 : * ln2_hi + ln2_lo,
1674 : * where n*ln2_hi is always exact for |n| < 2000.
1675 : *
1676 : * Special cases:
1677 : * log(x) is NaN with signal if x < 0 (including -INF) ;
1678 : * log(+INF) is +INF; log(0) is -INF with signal;
1679 : * log(NaN) is that NaN with no signal.
1680 : *
1681 : * Accuracy:
1682 : * according to an error analysis, the error is always less than
1683 : * 1 ulp (unit in the last place).
1684 : *
1685 : * Constants:
1686 : * The hexadecimal values are the intended ones for the following
1687 : * constants. The decimal values may be used, provided that the
1688 : * compiler will convert from decimal to binary accurately enough
1689 : * to produce the hexadecimal values shown.
1690 : */
1691 853960 : double log(double x) {
1692 : static const double /* -- */
1693 : ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */
1694 : ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */
1695 : two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */
1696 : Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */
1697 : Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
1698 : Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */
1699 : Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
1700 : Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
1701 : Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
1702 : Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
1703 :
1704 : static const double zero = 0.0;
1705 :
1706 : double hfsq, f, s, z, R, w, t1, t2, dk;
1707 : int32_t k, hx, i, j;
1708 : uint32_t lx;
1709 :
1710 853960 : EXTRACT_WORDS(hx, lx, x);
1711 :
1712 : k = 0;
1713 853960 : if (hx < 0x00100000) { /* x < 2**-1022 */
1714 23507 : if (((hx & 0x7FFFFFFF) | lx) == 0) {
1715 : return -std::numeric_limits<double>::infinity(); /* log(+-0)=-inf */
1716 : }
1717 12811 : if (hx < 0) {
1718 : return std::numeric_limits<double>::signaling_NaN(); /* log(-#) = NaN */
1719 : }
1720 : k -= 54;
1721 2 : x *= two54; /* subnormal number, scale up x */
1722 2 : GET_HIGH_WORD(hx, x);
1723 : }
1724 830455 : if (hx >= 0x7FF00000) return x + x;
1725 820719 : k += (hx >> 20) - 1023;
1726 820719 : hx &= 0x000FFFFF;
1727 820719 : i = (hx + 0x95F64) & 0x100000;
1728 820719 : SET_HIGH_WORD(x, hx | (i ^ 0x3FF00000)); /* normalize x or x/2 */
1729 820719 : k += (i >> 20);
1730 820719 : f = x - 1.0;
1731 820719 : if ((0x000FFFFF & (2 + hx)) < 3) { /* -2**-20 <= f < 2**-20 */
1732 5118 : if (f == zero) {
1733 4553 : if (k == 0) {
1734 : return zero;
1735 : } else {
1736 4023 : dk = static_cast<double>(k);
1737 4023 : return dk * ln2_hi + dk * ln2_lo;
1738 : }
1739 : }
1740 565 : R = f * f * (0.5 - 0.33333333333333333 * f);
1741 565 : if (k == 0) {
1742 49 : return f - R;
1743 : } else {
1744 516 : dk = static_cast<double>(k);
1745 516 : return dk * ln2_hi - ((R - dk * ln2_lo) - f);
1746 : }
1747 : }
1748 815601 : s = f / (2.0 + f);
1749 815601 : dk = static_cast<double>(k);
1750 815601 : z = s * s;
1751 815601 : i = hx - 0x6147A;
1752 815601 : w = z * z;
1753 815601 : j = 0x6B851 - hx;
1754 815601 : t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
1755 815601 : t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
1756 815601 : i |= j;
1757 815601 : R = t2 + t1;
1758 815601 : if (i > 0) {
1759 32892 : hfsq = 0.5 * f * f;
1760 32892 : if (k == 0)
1761 688 : return f - (hfsq - s * (hfsq + R));
1762 : else
1763 32204 : return dk * ln2_hi - ((hfsq - (s * (hfsq + R) + dk * ln2_lo)) - f);
1764 : } else {
1765 782709 : if (k == 0)
1766 20883 : return f - s * (f - R);
1767 : else
1768 761826 : return dk * ln2_hi - ((s * (f - R) - dk * ln2_lo) - f);
1769 : }
1770 : }
1771 :
1772 : /* double log1p(double x)
1773 : *
1774 : * Method :
1775 : * 1. Argument Reduction: find k and f such that
1776 : * 1+x = 2^k * (1+f),
1777 : * where sqrt(2)/2 < 1+f < sqrt(2) .
1778 : *
1779 : * Note. If k=0, then f=x is exact. However, if k!=0, then f
1780 : * may not be representable exactly. In that case, a correction
1781 : * term is need. Let u=1+x rounded. Let c = (1+x)-u, then
1782 : * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u),
1783 : * and add back the correction term c/u.
1784 : * (Note: when x > 2**53, one can simply return log(x))
1785 : *
1786 : * 2. Approximation of log1p(f).
1787 : * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
1788 : * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
1789 : * = 2s + s*R
1790 : * We use a special Reme algorithm on [0,0.1716] to generate
1791 : * a polynomial of degree 14 to approximate R The maximum error
1792 : * of this polynomial approximation is bounded by 2**-58.45. In
1793 : * other words,
1794 : * 2 4 6 8 10 12 14
1795 : * R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s
1796 : * (the values of Lp1 to Lp7 are listed in the program)
1797 : * and
1798 : * | 2 14 | -58.45
1799 : * | Lp1*s +...+Lp7*s - R(z) | <= 2
1800 : * | |
1801 : * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
1802 : * In order to guarantee error in log below 1ulp, we compute log
1803 : * by
1804 : * log1p(f) = f - (hfsq - s*(hfsq+R)).
1805 : *
1806 : * 3. Finally, log1p(x) = k*ln2 + log1p(f).
1807 : * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
1808 : * Here ln2 is split into two floating point number:
1809 : * ln2_hi + ln2_lo,
1810 : * where n*ln2_hi is always exact for |n| < 2000.
1811 : *
1812 : * Special cases:
1813 : * log1p(x) is NaN with signal if x < -1 (including -INF) ;
1814 : * log1p(+INF) is +INF; log1p(-1) is -INF with signal;
1815 : * log1p(NaN) is that NaN with no signal.
1816 : *
1817 : * Accuracy:
1818 : * according to an error analysis, the error is always less than
1819 : * 1 ulp (unit in the last place).
1820 : *
1821 : * Constants:
1822 : * The hexadecimal values are the intended ones for the following
1823 : * constants. The decimal values may be used, provided that the
1824 : * compiler will convert from decimal to binary accurately enough
1825 : * to produce the hexadecimal values shown.
1826 : *
1827 : * Note: Assuming log() return accurate answer, the following
1828 : * algorithm can be used to compute log1p(x) to within a few ULP:
1829 : *
1830 : * u = 1+x;
1831 : * if(u==1.0) return x ; else
1832 : * return log(u)*(x/(u-1.0));
1833 : *
1834 : * See HP-15C Advanced Functions Handbook, p.193.
1835 : */
1836 55578 : double log1p(double x) {
1837 : static const double /* -- */
1838 : ln2_hi = 6.93147180369123816490e-01, /* 3fe62e42 fee00000 */
1839 : ln2_lo = 1.90821492927058770002e-10, /* 3dea39ef 35793c76 */
1840 : two54 = 1.80143985094819840000e+16, /* 43500000 00000000 */
1841 : Lp1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */
1842 : Lp2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
1843 : Lp3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */
1844 : Lp4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
1845 : Lp5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
1846 : Lp6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
1847 : Lp7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
1848 :
1849 : static const double zero = 0.0;
1850 :
1851 : double hfsq, f, c, s, z, R, u;
1852 : int32_t k, hx, hu, ax;
1853 :
1854 55578 : GET_HIGH_WORD(hx, x);
1855 55578 : ax = hx & 0x7FFFFFFF;
1856 :
1857 : k = 1;
1858 55578 : if (hx < 0x3FDA827A) { /* 1+x < sqrt(2)+ */
1859 28367 : if (ax >= 0x3FF00000) { /* x <= -1.0 */
1860 219 : if (x == -1.0)
1861 22 : return -std::numeric_limits<double>::infinity(); /* log1p(-1)=+inf */
1862 : else
1863 : return std::numeric_limits<double>::signaling_NaN(); // log1p(x<-1)=NaN
1864 : }
1865 28148 : if (ax < 0x3E200000) { /* |x| < 2**-29 */
1866 27224 : if (two54 + x > zero /* raise inexact */
1867 27224 : && ax < 0x3C900000) /* |x| < 2**-54 */
1868 : return x;
1869 : else
1870 724 : return x - x * x * 0.5;
1871 : }
1872 924 : if (hx > 0 || hx <= static_cast<int32_t>(0xBFD2BEC4)) {
1873 : k = 0;
1874 : f = x;
1875 : hu = 1;
1876 : } /* sqrt(2)/2- <= 1+x < sqrt(2)+ */
1877 : }
1878 28135 : if (hx >= 0x7FF00000) return x + x;
1879 28038 : if (k != 0) {
1880 27122 : if (hx < 0x43400000) {
1881 615 : STRICT_ASSIGN(double, u, 1.0 + x);
1882 615 : GET_HIGH_WORD(hu, u);
1883 615 : k = (hu >> 20) - 1023;
1884 615 : c = (k > 0) ? 1.0 - (u - x) : x - (u - 1.0); /* correction term */
1885 615 : c /= u;
1886 : } else {
1887 : u = x;
1888 26507 : GET_HIGH_WORD(hu, u);
1889 26507 : k = (hu >> 20) - 1023;
1890 : c = 0;
1891 : }
1892 27122 : hu &= 0x000FFFFF;
1893 : /*
1894 : * The approximation to sqrt(2) used in thresholds is not
1895 : * critical. However, the ones used above must give less
1896 : * strict bounds than the one here so that the k==0 case is
1897 : * never reached from here, since here we have committed to
1898 : * using the correction term but don't use it if k==0.
1899 : */
1900 27122 : if (hu < 0x6A09E) { /* u ~< sqrt(2) */
1901 13549 : SET_HIGH_WORD(u, hu | 0x3FF00000); /* normalize u */
1902 : } else {
1903 13573 : k += 1;
1904 13573 : SET_HIGH_WORD(u, hu | 0x3FE00000); /* normalize u/2 */
1905 13573 : hu = (0x00100000 - hu) >> 2;
1906 : }
1907 27122 : f = u - 1.0;
1908 : }
1909 28038 : hfsq = 0.5 * f * f;
1910 28038 : if (hu == 0) { /* |f| < 2**-20 */
1911 161 : if (f == zero) {
1912 58 : if (k == 0) {
1913 : return zero;
1914 : } else {
1915 58 : c += k * ln2_lo;
1916 58 : return k * ln2_hi + c;
1917 : }
1918 : }
1919 103 : R = hfsq * (1.0 - 0.66666666666666666 * f);
1920 103 : if (k == 0)
1921 0 : return f - R;
1922 : else
1923 103 : return k * ln2_hi - ((R - (k * ln2_lo + c)) - f);
1924 : }
1925 27877 : s = f / (2.0 + f);
1926 27877 : z = s * s;
1927 27877 : R = z * (Lp1 +
1928 55754 : z * (Lp2 + z * (Lp3 + z * (Lp4 + z * (Lp5 + z * (Lp6 + z * Lp7))))));
1929 27877 : if (k == 0)
1930 916 : return f - (hfsq - s * (hfsq + R));
1931 : else
1932 26961 : return k * ln2_hi - ((hfsq - (s * (hfsq + R) + (k * ln2_lo + c))) - f);
1933 : }
1934 :
1935 : /*
1936 : * k_log1p(f):
1937 : * Return log(1+f) - f for 1+f in ~[sqrt(2)/2, sqrt(2)].
1938 : *
1939 : * The following describes the overall strategy for computing
1940 : * logarithms in base e. The argument reduction and adding the final
1941 : * term of the polynomial are done by the caller for increased accuracy
1942 : * when different bases are used.
1943 : *
1944 : * Method :
1945 : * 1. Argument Reduction: find k and f such that
1946 : * x = 2^k * (1+f),
1947 : * where sqrt(2)/2 < 1+f < sqrt(2) .
1948 : *
1949 : * 2. Approximation of log(1+f).
1950 : * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
1951 : * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
1952 : * = 2s + s*R
1953 : * We use a special Reme algorithm on [0,0.1716] to generate
1954 : * a polynomial of degree 14 to approximate R The maximum error
1955 : * of this polynomial approximation is bounded by 2**-58.45. In
1956 : * other words,
1957 : * 2 4 6 8 10 12 14
1958 : * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s
1959 : * (the values of Lg1 to Lg7 are listed in the program)
1960 : * and
1961 : * | 2 14 | -58.45
1962 : * | Lg1*s +...+Lg7*s - R(z) | <= 2
1963 : * | |
1964 : * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
1965 : * In order to guarantee error in log below 1ulp, we compute log
1966 : * by
1967 : * log(1+f) = f - s*(f - R) (if f is not too large)
1968 : * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
1969 : *
1970 : * 3. Finally, log(x) = k*ln2 + log(1+f).
1971 : * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
1972 : * Here ln2 is split into two floating point number:
1973 : * ln2_hi + ln2_lo,
1974 : * where n*ln2_hi is always exact for |n| < 2000.
1975 : *
1976 : * Special cases:
1977 : * log(x) is NaN with signal if x < 0 (including -INF) ;
1978 : * log(+INF) is +INF; log(0) is -INF with signal;
1979 : * log(NaN) is that NaN with no signal.
1980 : *
1981 : * Accuracy:
1982 : * according to an error analysis, the error is always less than
1983 : * 1 ulp (unit in the last place).
1984 : *
1985 : * Constants:
1986 : * The hexadecimal values are the intended ones for the following
1987 : * constants. The decimal values may be used, provided that the
1988 : * compiler will convert from decimal to binary accurately enough
1989 : * to produce the hexadecimal values shown.
1990 : */
1991 :
1992 : static const double Lg1 = 6.666666666666735130e-01, /* 3FE55555 55555593 */
1993 : Lg2 = 3.999999999940941908e-01, /* 3FD99999 9997FA04 */
1994 : Lg3 = 2.857142874366239149e-01, /* 3FD24924 94229359 */
1995 : Lg4 = 2.222219843214978396e-01, /* 3FCC71C5 1D8E78AF */
1996 : Lg5 = 1.818357216161805012e-01, /* 3FC74664 96CB03DE */
1997 : Lg6 = 1.531383769920937332e-01, /* 3FC39A09 D078C69F */
1998 : Lg7 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
1999 :
2000 : /*
2001 : * We always inline k_log1p(), since doing so produces a
2002 : * substantial performance improvement (~40% on amd64).
2003 : */
2004 57343 : static inline double k_log1p(double f) {
2005 : double hfsq, s, z, R, w, t1, t2;
2006 :
2007 57343 : s = f / (2.0 + f);
2008 57343 : z = s * s;
2009 57343 : w = z * z;
2010 57343 : t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
2011 57343 : t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
2012 57343 : R = t2 + t1;
2013 57343 : hfsq = 0.5 * f * f;
2014 57343 : return s * (hfsq + R);
2015 : }
2016 :
2017 : /*
2018 : * Return the base 2 logarithm of x. See e_log.c and k_log.h for most
2019 : * comments.
2020 : *
2021 : * This reduces x to {k, 1+f} exactly as in e_log.c, then calls the kernel,
2022 : * then does the combining and scaling steps
2023 : * log2(x) = (f - 0.5*f*f + k_log1p(f)) / ln2 + k
2024 : * in not-quite-routine extra precision.
2025 : */
2026 57720 : double log2(double x) {
2027 : static const double
2028 : two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
2029 : ivln2hi = 1.44269504072144627571e+00, /* 0x3FF71547, 0x65200000 */
2030 : ivln2lo = 1.67517131648865118353e-10; /* 0x3DE705FC, 0x2EEFA200 */
2031 :
2032 : double f, hfsq, hi, lo, r, val_hi, val_lo, w, y;
2033 : int32_t i, k, hx;
2034 : uint32_t lx;
2035 :
2036 57720 : EXTRACT_WORDS(hx, lx, x);
2037 :
2038 : k = 0;
2039 57720 : if (hx < 0x00100000) { /* x < 2**-1022 */
2040 693 : if (((hx & 0x7FFFFFFF) | lx) == 0) {
2041 : return -std::numeric_limits<double>::infinity(); /* log(+-0)=-inf */
2042 : }
2043 649 : if (hx < 0) {
2044 : return std::numeric_limits<double>::signaling_NaN(); /* log(-#) = NaN */
2045 : }
2046 : k -= 54;
2047 468 : x *= two54; /* subnormal number, scale up x */
2048 468 : GET_HIGH_WORD(hx, x);
2049 : }
2050 57495 : if (hx >= 0x7FF00000) return x + x;
2051 57400 : if (hx == 0x3FF00000 && lx == 0) return 0.0; /* log(1) = +0 */
2052 57343 : k += (hx >> 20) - 1023;
2053 57343 : hx &= 0x000FFFFF;
2054 57343 : i = (hx + 0x95F64) & 0x100000;
2055 57343 : SET_HIGH_WORD(x, hx | (i ^ 0x3FF00000)); /* normalize x or x/2 */
2056 57343 : k += (i >> 20);
2057 57343 : y = static_cast<double>(k);
2058 57343 : f = x - 1.0;
2059 57343 : hfsq = 0.5 * f * f;
2060 57343 : r = k_log1p(f);
2061 :
2062 : /*
2063 : * f-hfsq must (for args near 1) be evaluated in extra precision
2064 : * to avoid a large cancellation when x is near sqrt(2) or 1/sqrt(2).
2065 : * This is fairly efficient since f-hfsq only depends on f, so can
2066 : * be evaluated in parallel with R. Not combining hfsq with R also
2067 : * keeps R small (though not as small as a true `lo' term would be),
2068 : * so that extra precision is not needed for terms involving R.
2069 : *
2070 : * Compiler bugs involving extra precision used to break Dekker's
2071 : * theorem for spitting f-hfsq as hi+lo, unless double_t was used
2072 : * or the multi-precision calculations were avoided when double_t
2073 : * has extra precision. These problems are now automatically
2074 : * avoided as a side effect of the optimization of combining the
2075 : * Dekker splitting step with the clear-low-bits step.
2076 : *
2077 : * y must (for args near sqrt(2) and 1/sqrt(2)) be added in extra
2078 : * precision to avoid a very large cancellation when x is very near
2079 : * these values. Unlike the above cancellations, this problem is
2080 : * specific to base 2. It is strange that adding +-1 is so much
2081 : * harder than adding +-ln2 or +-log10_2.
2082 : *
2083 : * This uses Dekker's theorem to normalize y+val_hi, so the
2084 : * compiler bugs are back in some configurations, sigh. And I
2085 : * don't want to used double_t to avoid them, since that gives a
2086 : * pessimization and the support for avoiding the pessimization
2087 : * is not yet available.
2088 : *
2089 : * The multi-precision calculations for the multiplications are
2090 : * routine.
2091 : */
2092 57343 : hi = f - hfsq;
2093 57343 : SET_LOW_WORD(hi, 0);
2094 57343 : lo = (f - hi) - hfsq + r;
2095 57343 : val_hi = hi * ivln2hi;
2096 57343 : val_lo = (lo + hi) * ivln2lo + lo * ivln2hi;
2097 :
2098 : /* spadd(val_hi, val_lo, y), except for not using double_t: */
2099 57343 : w = y + val_hi;
2100 57343 : val_lo += (y - w) + val_hi;
2101 : val_hi = w;
2102 :
2103 57343 : return val_lo + val_hi;
2104 : }
2105 :
2106 : /*
2107 : * Return the base 10 logarithm of x
2108 : *
2109 : * Method :
2110 : * Let log10_2hi = leading 40 bits of log10(2) and
2111 : * log10_2lo = log10(2) - log10_2hi,
2112 : * ivln10 = 1/log(10) rounded.
2113 : * Then
2114 : * n = ilogb(x),
2115 : * if(n<0) n = n+1;
2116 : * x = scalbn(x,-n);
2117 : * log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x))
2118 : *
2119 : * Note 1:
2120 : * To guarantee log10(10**n)=n, where 10**n is normal, the rounding
2121 : * mode must set to Round-to-Nearest.
2122 : * Note 2:
2123 : * [1/log(10)] rounded to 53 bits has error .198 ulps;
2124 : * log10 is monotonic at all binary break points.
2125 : *
2126 : * Special cases:
2127 : * log10(x) is NaN if x < 0;
2128 : * log10(+INF) is +INF; log10(0) is -INF;
2129 : * log10(NaN) is that NaN;
2130 : * log10(10**N) = N for N=0,1,...,22.
2131 : */
2132 11840 : double log10(double x) {
2133 : static const double
2134 : two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
2135 : ivln10 = 4.34294481903251816668e-01,
2136 : log10_2hi = 3.01029995663611771306e-01, /* 0x3FD34413, 0x509F6000 */
2137 : log10_2lo = 3.69423907715893078616e-13; /* 0x3D59FEF3, 0x11F12B36 */
2138 :
2139 : double y;
2140 : int32_t i, k, hx;
2141 : uint32_t lx;
2142 :
2143 11840 : EXTRACT_WORDS(hx, lx, x);
2144 :
2145 : k = 0;
2146 11840 : if (hx < 0x00100000) { /* x < 2**-1022 */
2147 279 : if (((hx & 0x7FFFFFFF) | lx) == 0) {
2148 : return -std::numeric_limits<double>::infinity(); /* log(+-0)=-inf */
2149 : }
2150 235 : if (hx < 0) {
2151 : return std::numeric_limits<double>::quiet_NaN(); /* log(-#) = NaN */
2152 : }
2153 : k -= 54;
2154 54 : x *= two54; /* subnormal number, scale up x */
2155 54 : GET_HIGH_WORD(hx, x);
2156 54 : GET_LOW_WORD(lx, x);
2157 : }
2158 11615 : if (hx >= 0x7FF00000) return x + x;
2159 11520 : if (hx == 0x3FF00000 && lx == 0) return 0.0; /* log(1) = +0 */
2160 11494 : k += (hx >> 20) - 1023;
2161 :
2162 11494 : i = (k & 0x80000000) >> 31;
2163 11494 : hx = (hx & 0x000FFFFF) | ((0x3FF - i) << 20);
2164 11494 : y = k + i;
2165 11494 : SET_HIGH_WORD(x, hx);
2166 11494 : SET_LOW_WORD(x, lx);
2167 :
2168 11494 : double z = y * log10_2lo + ivln10 * log(x);
2169 11494 : return z + y * log10_2hi;
2170 : }
2171 :
2172 : /* expm1(x)
2173 : * Returns exp(x)-1, the exponential of x minus 1.
2174 : *
2175 : * Method
2176 : * 1. Argument reduction:
2177 : * Given x, find r and integer k such that
2178 : *
2179 : * x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658
2180 : *
2181 : * Here a correction term c will be computed to compensate
2182 : * the error in r when rounded to a floating-point number.
2183 : *
2184 : * 2. Approximating expm1(r) by a special rational function on
2185 : * the interval [0,0.34658]:
2186 : * Since
2187 : * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
2188 : * we define R1(r*r) by
2189 : * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
2190 : * That is,
2191 : * R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
2192 : * = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
2193 : * = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
2194 : * We use a special Reme algorithm on [0,0.347] to generate
2195 : * a polynomial of degree 5 in r*r to approximate R1. The
2196 : * maximum error of this polynomial approximation is bounded
2197 : * by 2**-61. In other words,
2198 : * R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
2199 : * where Q1 = -1.6666666666666567384E-2,
2200 : * Q2 = 3.9682539681370365873E-4,
2201 : * Q3 = -9.9206344733435987357E-6,
2202 : * Q4 = 2.5051361420808517002E-7,
2203 : * Q5 = -6.2843505682382617102E-9;
2204 : * z = r*r,
2205 : * with error bounded by
2206 : * | 5 | -61
2207 : * | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2
2208 : * | |
2209 : *
2210 : * expm1(r) = exp(r)-1 is then computed by the following
2211 : * specific way which minimize the accumulation rounding error:
2212 : * 2 3
2213 : * r r [ 3 - (R1 + R1*r/2) ]
2214 : * expm1(r) = r + --- + --- * [--------------------]
2215 : * 2 2 [ 6 - r*(3 - R1*r/2) ]
2216 : *
2217 : * To compensate the error in the argument reduction, we use
2218 : * expm1(r+c) = expm1(r) + c + expm1(r)*c
2219 : * ~ expm1(r) + c + r*c
2220 : * Thus c+r*c will be added in as the correction terms for
2221 : * expm1(r+c). Now rearrange the term to avoid optimization
2222 : * screw up:
2223 : * ( 2 2 )
2224 : * ({ ( r [ R1 - (3 - R1*r/2) ] ) } r )
2225 : * expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
2226 : * ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 )
2227 : * ( )
2228 : *
2229 : * = r - E
2230 : * 3. Scale back to obtain expm1(x):
2231 : * From step 1, we have
2232 : * expm1(x) = either 2^k*[expm1(r)+1] - 1
2233 : * = or 2^k*[expm1(r) + (1-2^-k)]
2234 : * 4. Implementation notes:
2235 : * (A). To save one multiplication, we scale the coefficient Qi
2236 : * to Qi*2^i, and replace z by (x^2)/2.
2237 : * (B). To achieve maximum accuracy, we compute expm1(x) by
2238 : * (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
2239 : * (ii) if k=0, return r-E
2240 : * (iii) if k=-1, return 0.5*(r-E)-0.5
2241 : * (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E)
2242 : * else return 1.0+2.0*(r-E);
2243 : * (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
2244 : * (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else
2245 : * (vii) return 2^k(1-((E+2^-k)-r))
2246 : *
2247 : * Special cases:
2248 : * expm1(INF) is INF, expm1(NaN) is NaN;
2249 : * expm1(-INF) is -1, and
2250 : * for finite argument, only expm1(0)=0 is exact.
2251 : *
2252 : * Accuracy:
2253 : * according to an error analysis, the error is always less than
2254 : * 1 ulp (unit in the last place).
2255 : *
2256 : * Misc. info.
2257 : * For IEEE double
2258 : * if x > 7.09782712893383973096e+02 then expm1(x) overflow
2259 : *
2260 : * Constants:
2261 : * The hexadecimal values are the intended ones for the following
2262 : * constants. The decimal values may be used, provided that the
2263 : * compiler will convert from decimal to binary accurately enough
2264 : * to produce the hexadecimal values shown.
2265 : */
2266 79850 : double expm1(double x) {
2267 : static const double
2268 : one = 1.0,
2269 : tiny = 1.0e-300,
2270 : o_threshold = 7.09782712893383973096e+02, /* 0x40862E42, 0xFEFA39EF */
2271 : ln2_hi = 6.93147180369123816490e-01, /* 0x3FE62E42, 0xFEE00000 */
2272 : ln2_lo = 1.90821492927058770002e-10, /* 0x3DEA39EF, 0x35793C76 */
2273 : invln2 = 1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE */
2274 : /* Scaled Q's: Qn_here = 2**n * Qn_above, for R(2*z) where z = hxs =
2275 : x*x/2: */
2276 : Q1 = -3.33333333333331316428e-02, /* BFA11111 111110F4 */
2277 : Q2 = 1.58730158725481460165e-03, /* 3F5A01A0 19FE5585 */
2278 : Q3 = -7.93650757867487942473e-05, /* BF14CE19 9EAADBB7 */
2279 : Q4 = 4.00821782732936239552e-06, /* 3ED0CFCA 86E65239 */
2280 : Q5 = -2.01099218183624371326e-07; /* BE8AFDB7 6E09C32D */
2281 :
2282 : static volatile double huge = 1.0e+300;
2283 :
2284 : double y, hi, lo, c, t, e, hxs, hfx, r1, twopk;
2285 : int32_t k, xsb;
2286 : uint32_t hx;
2287 :
2288 79850 : GET_HIGH_WORD(hx, x);
2289 79850 : xsb = hx & 0x80000000; /* sign bit of x */
2290 79850 : hx &= 0x7FFFFFFF; /* high word of |x| */
2291 :
2292 : /* filter out huge and non-finite argument */
2293 79850 : if (hx >= 0x4043687A) { /* if |x|>=56*ln2 */
2294 47957 : if (hx >= 0x40862E42) { /* if |x|>=709.78... */
2295 336 : if (hx >= 0x7FF00000) {
2296 : uint32_t low;
2297 125 : GET_LOW_WORD(low, x);
2298 125 : if (((hx & 0xFFFFF) | low) != 0)
2299 81 : return x + x; /* NaN */
2300 : else
2301 44 : return (xsb == 0) ? x : -1.0; /* exp(+-inf)={inf,-1} */
2302 : }
2303 211 : if (x > o_threshold) return huge * huge; /* overflow */
2304 : }
2305 47718 : if (xsb != 0) { /* x < -56*ln2, return -1.0 with inexact */
2306 23911 : if (x + tiny < 0.0) /* raise inexact */
2307 : return tiny - one; /* return -1 */
2308 : }
2309 : }
2310 :
2311 : /* argument reduction */
2312 55700 : if (hx > 0x3FD62E42) { /* if |x| > 0.5 ln2 */
2313 27426 : if (hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */
2314 300 : if (xsb == 0) {
2315 171 : hi = x - ln2_hi;
2316 : lo = ln2_lo;
2317 : k = 1;
2318 : } else {
2319 129 : hi = x + ln2_hi;
2320 : lo = -ln2_lo;
2321 : k = -1;
2322 : }
2323 : } else {
2324 27126 : k = invln2 * x + ((xsb == 0) ? 0.5 : -0.5);
2325 27126 : t = k;
2326 27126 : hi = x - t * ln2_hi; /* t*ln2_hi is exact here */
2327 27126 : lo = t * ln2_lo;
2328 : }
2329 27426 : STRICT_ASSIGN(double, x, hi - lo);
2330 27426 : c = (hi - x) - lo;
2331 28274 : } else if (hx < 0x3C900000) { /* when |x|<2**-54, return x */
2332 26659 : t = huge + x; /* return x with inexact flags when x!=0 */
2333 26659 : return x - (t - (huge + x));
2334 : } else {
2335 : k = 0;
2336 : }
2337 :
2338 : /* x is now in primary range */
2339 29041 : hfx = 0.5 * x;
2340 29041 : hxs = x * hfx;
2341 29041 : r1 = one + hxs * (Q1 + hxs * (Q2 + hxs * (Q3 + hxs * (Q4 + hxs * Q5))));
2342 29041 : t = 3.0 - r1 * hfx;
2343 29041 : e = hxs * ((r1 - t) / (6.0 - x * t));
2344 29041 : if (k == 0) {
2345 1615 : return x - (x * e - hxs); /* c is 0 */
2346 : } else {
2347 27426 : INSERT_WORDS(
2348 : twopk,
2349 : 0x3FF00000 + static_cast<int32_t>(static_cast<uint32_t>(k) << 20),
2350 : 0); /* 2^k */
2351 27426 : e = (x * (e - c) - c);
2352 27426 : e -= hxs;
2353 27426 : if (k == -1) return 0.5 * (x - e) - 0.5;
2354 27297 : if (k == 1) {
2355 171 : if (x < -0.25)
2356 16 : return -2.0 * (e - (x + 0.5));
2357 : else
2358 155 : return one + 2.0 * (x - e);
2359 : }
2360 27126 : if (k <= -2 || k > 56) { /* suffice to return exp(x)-1 */
2361 25200 : y = one - (e - x);
2362 : // TODO(mvstanton): is this replacement for the hex float
2363 : // sufficient?
2364 : // if (k == 1024) y = y*2.0*0x1p1023;
2365 25200 : if (k == 1024)
2366 0 : y = y * 2.0 * 8.98846567431158e+307;
2367 : else
2368 25200 : y = y * twopk;
2369 25200 : return y - one;
2370 : }
2371 : t = one;
2372 1926 : if (k < 20) {
2373 999 : SET_HIGH_WORD(t, 0x3FF00000 - (0x200000 >> k)); /* t=1-2^-k */
2374 999 : y = t - (e - x);
2375 999 : y = y * twopk;
2376 : } else {
2377 927 : SET_HIGH_WORD(t, ((0x3FF - k) << 20)); /* 2^-k */
2378 927 : y = x - (e + t);
2379 927 : y += one;
2380 927 : y = y * twopk;
2381 : }
2382 : }
2383 : return y;
2384 : }
2385 :
2386 98282 : double cbrt(double x) {
2387 : static const uint32_t
2388 : B1 = 715094163, /* B1 = (1023-1023/3-0.03306235651)*2**20 */
2389 : B2 = 696219795; /* B2 = (1023-1023/3-54/3-0.03306235651)*2**20 */
2390 :
2391 : /* |1/cbrt(x) - p(x)| < 2**-23.5 (~[-7.93e-8, 7.929e-8]). */
2392 : static const double P0 = 1.87595182427177009643, /* 0x3FFE03E6, 0x0F61E692 */
2393 : P1 = -1.88497979543377169875, /* 0xBFFE28E0, 0x92F02420 */
2394 : P2 = 1.621429720105354466140, /* 0x3FF9F160, 0x4A49D6C2 */
2395 : P3 = -0.758397934778766047437, /* 0xBFE844CB, 0xBEE751D9 */
2396 : P4 = 0.145996192886612446982; /* 0x3FC2B000, 0xD4E4EDD7 */
2397 :
2398 : int32_t hx;
2399 : union {
2400 : double value;
2401 : uint64_t bits;
2402 : } u;
2403 : double r, s, t = 0.0, w;
2404 : uint32_t sign;
2405 : uint32_t high, low;
2406 :
2407 98282 : EXTRACT_WORDS(hx, low, x);
2408 98282 : sign = hx & 0x80000000; /* sign= sign(x) */
2409 98282 : hx ^= sign;
2410 98282 : if (hx >= 0x7FF00000) return (x + x); /* cbrt(NaN,INF) is itself */
2411 :
2412 : /*
2413 : * Rough cbrt to 5 bits:
2414 : * cbrt(2**e*(1+m) ~= 2**(e/3)*(1+(e%3+m)/3)
2415 : * where e is integral and >= 0, m is real and in [0, 1), and "/" and
2416 : * "%" are integer division and modulus with rounding towards minus
2417 : * infinity. The RHS is always >= the LHS and has a maximum relative
2418 : * error of about 1 in 16. Adding a bias of -0.03306235651 to the
2419 : * (e%3+m)/3 term reduces the error to about 1 in 32. With the IEEE
2420 : * floating point representation, for finite positive normal values,
2421 : * ordinary integer division of the value in bits magically gives
2422 : * almost exactly the RHS of the above provided we first subtract the
2423 : * exponent bias (1023 for doubles) and later add it back. We do the
2424 : * subtraction virtually to keep e >= 0 so that ordinary integer
2425 : * division rounds towards minus infinity; this is also efficient.
2426 : */
2427 98157 : if (hx < 0x00100000) { /* zero or subnormal? */
2428 34 : if ((hx | low) == 0) return (x); /* cbrt(0) is itself */
2429 0 : SET_HIGH_WORD(t, 0x43500000); /* set t= 2**54 */
2430 0 : t *= x;
2431 0 : GET_HIGH_WORD(high, t);
2432 0 : INSERT_WORDS(t, sign | ((high & 0x7FFFFFFF) / 3 + B2), 0);
2433 : } else {
2434 98123 : INSERT_WORDS(t, sign | (hx / 3 + B1), 0);
2435 : }
2436 :
2437 : /*
2438 : * New cbrt to 23 bits:
2439 : * cbrt(x) = t*cbrt(x/t**3) ~= t*P(t**3/x)
2440 : * where P(r) is a polynomial of degree 4 that approximates 1/cbrt(r)
2441 : * to within 2**-23.5 when |r - 1| < 1/10. The rough approximation
2442 : * has produced t such than |t/cbrt(x) - 1| ~< 1/32, and cubing this
2443 : * gives us bounds for r = t**3/x.
2444 : *
2445 : * Try to optimize for parallel evaluation as in k_tanf.c.
2446 : */
2447 98123 : r = (t * t) * (t / x);
2448 98123 : t = t * ((P0 + r * (P1 + r * P2)) + ((r * r) * r) * (P3 + r * P4));
2449 :
2450 : /*
2451 : * Round t away from zero to 23 bits (sloppily except for ensuring that
2452 : * the result is larger in magnitude than cbrt(x) but not much more than
2453 : * 2 23-bit ulps larger). With rounding towards zero, the error bound
2454 : * would be ~5/6 instead of ~4/6. With a maximum error of 2 23-bit ulps
2455 : * in the rounded t, the infinite-precision error in the Newton
2456 : * approximation barely affects third digit in the final error
2457 : * 0.667; the error in the rounded t can be up to about 3 23-bit ulps
2458 : * before the final error is larger than 0.667 ulps.
2459 : */
2460 : u.value = t;
2461 98123 : u.bits = (u.bits + 0x80000000) & 0xFFFFFFFFC0000000ULL;
2462 : t = u.value;
2463 :
2464 : /* one step Newton iteration to 53 bits with error < 0.667 ulps */
2465 98123 : s = t * t; /* t*t is exact */
2466 98123 : r = x / s; /* error <= 0.5 ulps; |r| < |t| */
2467 98123 : w = t + t; /* t+t is exact */
2468 98123 : r = (r - t) / (w + r); /* r-t is exact; w+r ~= 3*t */
2469 98123 : t = t + t * r; /* error <= 0.5 + 0.5/3 + epsilon */
2470 :
2471 98123 : return (t);
2472 : }
2473 :
2474 : /* sin(x)
2475 : * Return sine function of x.
2476 : *
2477 : * kernel function:
2478 : * __kernel_sin ... sine function on [-pi/4,pi/4]
2479 : * __kernel_cos ... cose function on [-pi/4,pi/4]
2480 : * __ieee754_rem_pio2 ... argument reduction routine
2481 : *
2482 : * Method.
2483 : * Let S,C and T denote the sin, cos and tan respectively on
2484 : * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
2485 : * in [-pi/4 , +pi/4], and let n = k mod 4.
2486 : * We have
2487 : *
2488 : * n sin(x) cos(x) tan(x)
2489 : * ----------------------------------------------------------
2490 : * 0 S C T
2491 : * 1 C -S -1/T
2492 : * 2 -S -C T
2493 : * 3 -C S -1/T
2494 : * ----------------------------------------------------------
2495 : *
2496 : * Special cases:
2497 : * Let trig be any of sin, cos, or tan.
2498 : * trig(+-INF) is NaN, with signals;
2499 : * trig(NaN) is that NaN;
2500 : *
2501 : * Accuracy:
2502 : * TRIG(x) returns trig(x) nearly rounded
2503 : */
2504 10015200 : double sin(double x) {
2505 : double y[2], z = 0.0;
2506 : int32_t n, ix;
2507 :
2508 : /* High word of x. */
2509 10015200 : GET_HIGH_WORD(ix, x);
2510 :
2511 : /* |x| ~< pi/4 */
2512 10015200 : ix &= 0x7FFFFFFF;
2513 10015200 : if (ix <= 0x3FE921FB) {
2514 8959475 : return __kernel_sin(x, z, 0);
2515 1055725 : } else if (ix >= 0x7FF00000) {
2516 : /* sin(Inf or NaN) is NaN */
2517 1062 : return x - x;
2518 : } else {
2519 : /* argument reduction needed */
2520 1054663 : n = __ieee754_rem_pio2(x, y);
2521 1054663 : switch (n & 3) {
2522 : case 0:
2523 528724 : return __kernel_sin(y[0], y[1], 1);
2524 : case 1:
2525 528690 : return __kernel_cos(y[0], y[1]);
2526 : case 2:
2527 524838 : return -__kernel_sin(y[0], y[1], 1);
2528 : default:
2529 527074 : return -__kernel_cos(y[0], y[1]);
2530 : }
2531 : }
2532 : }
2533 :
2534 : /* tan(x)
2535 : * Return tangent function of x.
2536 : *
2537 : * kernel function:
2538 : * __kernel_tan ... tangent function on [-pi/4,pi/4]
2539 : * __ieee754_rem_pio2 ... argument reduction routine
2540 : *
2541 : * Method.
2542 : * Let S,C and T denote the sin, cos and tan respectively on
2543 : * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
2544 : * in [-pi/4 , +pi/4], and let n = k mod 4.
2545 : * We have
2546 : *
2547 : * n sin(x) cos(x) tan(x)
2548 : * ----------------------------------------------------------
2549 : * 0 S C T
2550 : * 1 C -S -1/T
2551 : * 2 -S -C T
2552 : * 3 -C S -1/T
2553 : * ----------------------------------------------------------
2554 : *
2555 : * Special cases:
2556 : * Let trig be any of sin, cos, or tan.
2557 : * trig(+-INF) is NaN, with signals;
2558 : * trig(NaN) is that NaN;
2559 : *
2560 : * Accuracy:
2561 : * TRIG(x) returns trig(x) nearly rounded
2562 : */
2563 374755 : double tan(double x) {
2564 : double y[2], z = 0.0;
2565 : int32_t n, ix;
2566 :
2567 : /* High word of x. */
2568 374755 : GET_HIGH_WORD(ix, x);
2569 :
2570 : /* |x| ~< pi/4 */
2571 374755 : ix &= 0x7FFFFFFF;
2572 374755 : if (ix <= 0x3FE921FB) {
2573 2389 : return __kernel_tan(x, z, 1);
2574 372366 : } else if (ix >= 0x7FF00000) {
2575 : /* tan(Inf or NaN) is NaN */
2576 1054 : return x - x; /* NaN */
2577 : } else {
2578 : /* argument reduction needed */
2579 371312 : n = __ieee754_rem_pio2(x, y);
2580 : /* 1 -> n even, -1 -> n odd */
2581 371312 : return __kernel_tan(y[0], y[1], 1 - ((n & 1) << 1));
2582 : }
2583 : }
2584 :
2585 : /*
2586 : * ES6 draft 09-27-13, section 20.2.2.12.
2587 : * Math.cosh
2588 : * Method :
2589 : * mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2
2590 : * 1. Replace x by |x| (cosh(x) = cosh(-x)).
2591 : * 2.
2592 : * [ exp(x) - 1 ]^2
2593 : * 0 <= x <= ln2/2 : cosh(x) := 1 + -------------------
2594 : * 2*exp(x)
2595 : *
2596 : * exp(x) + 1/exp(x)
2597 : * ln2/2 <= x <= 22 : cosh(x) := -------------------
2598 : * 2
2599 : * 22 <= x <= lnovft : cosh(x) := exp(x)/2
2600 : * lnovft <= x <= ln2ovft: cosh(x) := exp(x/2)/2 * exp(x/2)
2601 : * ln2ovft < x : cosh(x) := huge*huge (overflow)
2602 : *
2603 : * Special cases:
2604 : * cosh(x) is |x| if x is +INF, -INF, or NaN.
2605 : * only cosh(0)=1 is exact for finite x.
2606 : */
2607 1039 : double cosh(double x) {
2608 : static const double KCOSH_OVERFLOW = 710.4758600739439;
2609 : static const double one = 1.0, half = 0.5;
2610 : static volatile double huge = 1.0e+300;
2611 :
2612 : int32_t ix;
2613 :
2614 : /* High word of |x|. */
2615 1039 : GET_HIGH_WORD(ix, x);
2616 1039 : ix &= 0x7FFFFFFF;
2617 :
2618 : // |x| in [0,0.5*log2], return 1+expm1(|x|)^2/(2*exp(|x|))
2619 1039 : if (ix < 0x3FD62E43) {
2620 245 : double t = expm1(fabs(x));
2621 245 : double w = one + t;
2622 : // For |x| < 2^-55, cosh(x) = 1
2623 245 : if (ix < 0x3C800000) return w;
2624 70 : return one + (t * t) / (w + w);
2625 : }
2626 :
2627 : // |x| in [0.5*log2, 22], return (exp(|x|)+1/exp(|x|)/2
2628 794 : if (ix < 0x40360000) {
2629 286 : double t = exp(fabs(x));
2630 286 : return half * t + half / t;
2631 : }
2632 :
2633 : // |x| in [22, log(maxdouble)], return half*exp(|x|)
2634 508 : if (ix < 0x40862E42) return half * exp(fabs(x));
2635 :
2636 : // |x| in [log(maxdouble), overflowthreshold]
2637 478 : if (fabs(x) <= KCOSH_OVERFLOW) {
2638 18 : double w = exp(half * fabs(x));
2639 18 : double t = half * w;
2640 18 : return t * w;
2641 : }
2642 :
2643 : /* x is INF or NaN */
2644 460 : if (ix >= 0x7FF00000) return x * x;
2645 :
2646 : // |x| > overflowthreshold.
2647 312 : return huge * huge;
2648 : }
2649 :
2650 : /*
2651 : * ES2019 Draft 2019-01-02 12.6.4
2652 : * Math.pow & Exponentiation Operator
2653 : *
2654 : * Return X raised to the Yth power
2655 : *
2656 : * Method:
2657 : * Let x = 2 * (1+f)
2658 : * 1. Compute and return log2(x) in two pieces:
2659 : * log2(x) = w1 + w2,
2660 : * where w1 has 53-24 = 29 bit trailing zeros.
2661 : * 2. Perform y*log2(x) = n+y' by simulating muti-precision
2662 : * arithmetic, where |y'|<=0.5.
2663 : * 3. Return x**y = 2**n*exp(y'*log2)
2664 : *
2665 : * Special cases:
2666 : * 1. (anything) ** 0 is 1
2667 : * 2. (anything) ** 1 is itself
2668 : * 3. (anything) ** NAN is NAN
2669 : * 4. NAN ** (anything except 0) is NAN
2670 : * 5. +-(|x| > 1) ** +INF is +INF
2671 : * 6. +-(|x| > 1) ** -INF is +0
2672 : * 7. +-(|x| < 1) ** +INF is +0
2673 : * 8. +-(|x| < 1) ** -INF is +INF
2674 : * 9. +-1 ** +-INF is NAN
2675 : * 10. +0 ** (+anything except 0, NAN) is +0
2676 : * 11. -0 ** (+anything except 0, NAN, odd integer) is +0
2677 : * 12. +0 ** (-anything except 0, NAN) is +INF
2678 : * 13. -0 ** (-anything except 0, NAN, odd integer) is +INF
2679 : * 14. -0 ** (odd integer) = -( +0 ** (odd integer) )
2680 : * 15. +INF ** (+anything except 0,NAN) is +INF
2681 : * 16. +INF ** (-anything except 0,NAN) is +0
2682 : * 17. -INF ** (anything) = -0 ** (-anything)
2683 : * 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
2684 : * 19. (-anything except 0 and inf) ** (non-integer) is NAN
2685 : *
2686 : * Accuracy:
2687 : * pow(x,y) returns x**y nearly rounded. In particular,
2688 : * pow(integer, integer) always returns the correct integer provided it is
2689 : * representable.
2690 : *
2691 : * Constants:
2692 : * The hexadecimal values are the intended ones for the following
2693 : * constants. The decimal values may be used, provided that the
2694 : * compiler will convert from decimal to binary accurately enough
2695 : * to produce the hexadecimal values shown.
2696 : */
2697 :
2698 1174683 : double pow(double x, double y) {
2699 : static const double
2700 : bp[] = {1.0, 1.5},
2701 : dp_h[] = {0.0, 5.84962487220764160156e-01}, // 0x3FE2B803, 0x40000000
2702 : dp_l[] = {0.0, 1.35003920212974897128e-08}, // 0x3E4CFDEB, 0x43CFD006
2703 : zero = 0.0, one = 1.0, two = 2.0,
2704 : two53 = 9007199254740992.0, // 0x43400000, 0x00000000
2705 : huge = 1.0e300, tiny = 1.0e-300,
2706 : // poly coefs for (3/2)*(log(x)-2s-2/3*s**3
2707 : L1 = 5.99999999999994648725e-01, // 0x3FE33333, 0x33333303
2708 : L2 = 4.28571428578550184252e-01, // 0x3FDB6DB6, 0xDB6FABFF
2709 : L3 = 3.33333329818377432918e-01, // 0x3FD55555, 0x518F264D
2710 : L4 = 2.72728123808534006489e-01, // 0x3FD17460, 0xA91D4101
2711 : L5 = 2.30660745775561754067e-01, // 0x3FCD864A, 0x93C9DB65
2712 : L6 = 2.06975017800338417784e-01, // 0x3FCA7E28, 0x4A454EEF
2713 : P1 = 1.66666666666666019037e-01, // 0x3FC55555, 0x5555553E
2714 : P2 = -2.77777777770155933842e-03, // 0xBF66C16C, 0x16BEBD93
2715 : P3 = 6.61375632143793436117e-05, // 0x3F11566A, 0xAF25DE2C
2716 : P4 = -1.65339022054652515390e-06, // 0xBEBBBD41, 0xC5D26BF1
2717 : P5 = 4.13813679705723846039e-08, // 0x3E663769, 0x72BEA4D0
2718 : lg2 = 6.93147180559945286227e-01, // 0x3FE62E42, 0xFEFA39EF
2719 : lg2_h = 6.93147182464599609375e-01, // 0x3FE62E43, 0x00000000
2720 : lg2_l = -1.90465429995776804525e-09, // 0xBE205C61, 0x0CA86C39
2721 : ovt = 8.0085662595372944372e-0017, // -(1024-log2(ovfl+.5ulp))
2722 : cp = 9.61796693925975554329e-01, // 0x3FEEC709, 0xDC3A03FD =2/(3ln2)
2723 : cp_h = 9.61796700954437255859e-01, // 0x3FEEC709, 0xE0000000 =(float)cp
2724 : cp_l = -7.02846165095275826516e-09, // 0xBE3E2FE0, 0x145B01F5 =tail cp_h
2725 : ivln2 = 1.44269504088896338700e+00, // 0x3FF71547, 0x652B82FE =1/ln2
2726 : ivln2_h =
2727 : 1.44269502162933349609e+00, // 0x3FF71547, 0x60000000 =24b 1/ln2
2728 : ivln2_l =
2729 : 1.92596299112661746887e-08; // 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail
2730 :
2731 : double z, ax, z_h, z_l, p_h, p_l;
2732 : double y1, t1, t2, r, s, t, u, v, w;
2733 : int i, j, k, yisint, n;
2734 : int hx, hy, ix, iy;
2735 : unsigned lx, ly;
2736 :
2737 1174683 : EXTRACT_WORDS(hx, lx, x);
2738 1174683 : EXTRACT_WORDS(hy, ly, y);
2739 1174683 : ix = hx & 0x7fffffff;
2740 1174683 : iy = hy & 0x7fffffff;
2741 :
2742 : /* y==zero: x**0 = 1 */
2743 1174683 : if ((iy | ly) == 0) return one;
2744 :
2745 : /* +-NaN return x+y */
2746 2328107 : if (ix > 0x7ff00000 || ((ix == 0x7ff00000) && (lx != 0)) || iy > 0x7ff00000 ||
2747 1161038 : ((iy == 0x7ff00000) && (ly != 0))) {
2748 6031 : return x + y;
2749 : }
2750 :
2751 : /* determine if y is an odd int when x < 0
2752 : * yisint = 0 ... y is not an integer
2753 : * yisint = 1 ... y is an odd int
2754 : * yisint = 2 ... y is an even int
2755 : */
2756 : yisint = 0;
2757 1161038 : if (hx < 0) {
2758 30082 : if (iy >= 0x43400000) {
2759 : yisint = 2; /* even integer y */
2760 21032 : } else if (iy >= 0x3ff00000) {
2761 12248 : k = (iy >> 20) - 0x3ff; /* exponent */
2762 12248 : if (k > 20) {
2763 4132 : j = ly >> (52 - k);
2764 4132 : if ((j << (52 - k)) == static_cast<int>(ly)) yisint = 2 - (j & 1);
2765 8116 : } else if (ly == 0) {
2766 6682 : j = iy >> (20 - k);
2767 6682 : if ((j << (20 - k)) == iy) yisint = 2 - (j & 1);
2768 : }
2769 : }
2770 : }
2771 :
2772 : /* special value of y */
2773 1161038 : if (ly == 0) {
2774 984829 : if (iy == 0x7ff00000) { /* y is +-inf */
2775 5837 : if (((ix - 0x3ff00000) | lx) == 0) {
2776 340 : return y - y; /* inf**+-1 is NaN */
2777 5497 : } else if (ix >= 0x3ff00000) { /* (|x|>1)**+-inf = inf,0 */
2778 3750 : return (hy >= 0) ? y : zero;
2779 : } else { /* (|x|<1)**-,+inf = inf,0 */
2780 1747 : return (hy < 0) ? -y : zero;
2781 : }
2782 : }
2783 978992 : if (iy == 0x3ff00000) { /* y is +-1 */
2784 8465 : if (hy < 0) {
2785 4073 : return base::Divide(one, x);
2786 : } else {
2787 : return x;
2788 : }
2789 : }
2790 970527 : if (hy == 0x40000000) return x * x; /* y is 2 */
2791 967360 : if (hy == 0x3fe00000) { /* y is 0.5 */
2792 2340 : if (hx >= 0) { /* x >= +0 */
2793 1163 : return sqrt(x);
2794 : }
2795 : }
2796 : }
2797 :
2798 1142406 : ax = fabs(x);
2799 : /* special value of x */
2800 1142406 : if (lx == 0) {
2801 976532 : if (ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000) {
2802 : z = ax; /*x is +-0,+-inf,+-1*/
2803 13663 : if (hy < 0) z = base::Divide(one, z); /* z = (1/|x|) */
2804 13664 : if (hx < 0) {
2805 7099 : if (((ix - 0x3ff00000) | yisint) == 0) {
2806 : /* (-1)**non-int is NaN */
2807 : z = std::numeric_limits<double>::signaling_NaN();
2808 6322 : } else if (yisint == 1) {
2809 984 : z = -z; /* (x<0)**odd = -(|x|**odd) */
2810 : }
2811 : }
2812 : return z;
2813 : }
2814 : }
2815 :
2816 1128743 : n = (hx >> 31) + 1;
2817 :
2818 : /* (x<0)**(non-int) is NaN */
2819 1128743 : if ((n | yisint) == 0) {
2820 : return std::numeric_limits<double>::signaling_NaN();
2821 : }
2822 :
2823 : s = one; /* s (sign of result -ve**odd) = -1 else = 1 */
2824 1119475 : if ((n | (yisint - 1)) == 0) s = -one; /* (-ve)**(odd int) */
2825 :
2826 : /* |y| is huge */
2827 1119475 : if (iy > 0x41e00000) { /* if |y| > 2**31 */
2828 9811 : if (iy > 0x43f00000) { /* if |y| > 2**64, must o/uflow */
2829 7768 : if (ix <= 0x3fefffff) return (hy < 0) ? huge * huge : tiny * tiny;
2830 4364 : if (ix >= 0x3ff00000) return (hy > 0) ? huge * huge : tiny * tiny;
2831 : }
2832 : /* over/underflow if x is not close to one */
2833 2043 : if (ix < 0x3fefffff) return (hy < 0) ? s * huge * huge : s * tiny * tiny;
2834 1299 : if (ix > 0x3ff00000) return (hy > 0) ? s * huge * huge : s * tiny * tiny;
2835 : /* now |1-x| is tiny <= 2**-20, suffice to compute
2836 : log(x) by x-x^2/2+x^3/3-x^4/4 */
2837 41 : t = ax - one; /* t has 20 trailing zeros */
2838 41 : w = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25));
2839 41 : u = ivln2_h * t; /* ivln2_h has 21 sig. bits */
2840 41 : v = t * ivln2_l - w * ivln2;
2841 41 : t1 = u + v;
2842 41 : SET_LOW_WORD(t1, 0);
2843 41 : t2 = v - (t1 - u);
2844 : } else {
2845 : double ss, s2, s_h, s_l, t_h, t_l;
2846 : n = 0;
2847 : /* take care subnormal number */
2848 1109664 : if (ix < 0x00100000) {
2849 180 : ax *= two53;
2850 : n -= 53;
2851 180 : GET_HIGH_WORD(ix, ax);
2852 : }
2853 1109664 : n += ((ix) >> 20) - 0x3ff;
2854 1109664 : j = ix & 0x000fffff;
2855 : /* determine interval */
2856 1109664 : ix = j | 0x3ff00000; /* normalize ix */
2857 1109664 : if (j <= 0x3988E) {
2858 : k = 0; /* |x|<sqrt(3/2) */
2859 163949 : } else if (j < 0xBB67A) {
2860 : k = 1; /* |x|<sqrt(3) */
2861 : } else {
2862 : k = 0;
2863 5148 : n += 1;
2864 5148 : ix -= 0x00100000;
2865 : }
2866 1109664 : SET_HIGH_WORD(ax, ix);
2867 :
2868 : /* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
2869 1109664 : u = ax - bp[k]; /* bp[0]=1.0, bp[1]=1.5 */
2870 1109664 : v = base::Divide(one, ax + bp[k]);
2871 1109665 : ss = u * v;
2872 : s_h = ss;
2873 1109665 : SET_LOW_WORD(s_h, 0);
2874 : /* t_h=ax+bp[k] High */
2875 : t_h = zero;
2876 1109665 : SET_HIGH_WORD(t_h, ((ix >> 1) | 0x20000000) + 0x00080000 + (k << 18));
2877 1109665 : t_l = ax - (t_h - bp[k]);
2878 1109665 : s_l = v * ((u - s_h * t_h) - s_h * t_l);
2879 : /* compute log(ax) */
2880 1109665 : s2 = ss * ss;
2881 1109665 : r = s2 * s2 *
2882 1109665 : (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
2883 1109665 : r += s_l * (s_h + ss);
2884 1109665 : s2 = s_h * s_h;
2885 1109665 : t_h = 3.0 + s2 + r;
2886 1109665 : SET_LOW_WORD(t_h, 0);
2887 1109665 : t_l = r - ((t_h - 3.0) - s2);
2888 : /* u+v = ss*(1+...) */
2889 1109665 : u = s_h * t_h;
2890 1109665 : v = s_l * t_h + t_l * ss;
2891 : /* 2/(3log2)*(ss+...) */
2892 1109665 : p_h = u + v;
2893 1109665 : SET_LOW_WORD(p_h, 0);
2894 1109665 : p_l = v - (p_h - u);
2895 1109665 : z_h = cp_h * p_h; /* cp_h+cp_l = 2/(3*log2) */
2896 1109665 : z_l = cp_l * p_h + p_l * cp + dp_l[k];
2897 : /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */
2898 1109665 : t = static_cast<double>(n);
2899 1109665 : t1 = (((z_h + z_l) + dp_h[k]) + t);
2900 1109665 : SET_LOW_WORD(t1, 0);
2901 1109665 : t2 = z_l - (((t1 - t) - dp_h[k]) - z_h);
2902 : }
2903 :
2904 : /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
2905 : y1 = y;
2906 1109706 : SET_LOW_WORD(y1, 0);
2907 1109706 : p_l = (y - y1) * t1 + y * t2;
2908 1109706 : p_h = y1 * t1;
2909 1109706 : z = p_l + p_h;
2910 1109706 : EXTRACT_WORDS(j, i, z);
2911 1109706 : if (j >= 0x40900000) { /* z >= 1024 */
2912 5614 : if (((j - 0x40900000) | i) != 0) { /* if z > 1024 */
2913 5193 : return s * huge * huge; /* overflow */
2914 : } else {
2915 421 : if (p_l + ovt > z - p_h) return s * huge * huge; /* overflow */
2916 : }
2917 1104092 : } else if ((j & 0x7fffffff) >= 0x4090cc00) { /* z <= -1075 */
2918 4450 : if (((j - 0xc090cc00) | i) != 0) { /* z < -1075 */
2919 4450 : return s * tiny * tiny; /* underflow */
2920 : } else {
2921 0 : if (p_l <= z - p_h) return s * tiny * tiny; /* underflow */
2922 : }
2923 : }
2924 : /*
2925 : * compute 2**(p_h+p_l)
2926 : */
2927 1099642 : i = j & 0x7fffffff;
2928 1099642 : k = (i >> 20) - 0x3ff;
2929 : n = 0;
2930 1099642 : if (i > 0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */
2931 1094252 : n = j + (0x00100000 >> (k + 1));
2932 1094252 : k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */
2933 : t = zero;
2934 1094252 : SET_HIGH_WORD(t, n & ~(0x000fffff >> k));
2935 1094252 : n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
2936 1094252 : if (j < 0) n = -n;
2937 1094252 : p_h -= t;
2938 : }
2939 1099642 : t = p_l + p_h;
2940 1099642 : SET_LOW_WORD(t, 0);
2941 1099642 : u = t * lg2_h;
2942 1099642 : v = (p_l - (t - p_h)) * lg2 + t * lg2_l;
2943 1099642 : z = u + v;
2944 1099642 : w = v - (z - u);
2945 1099642 : t = z * z;
2946 1099642 : t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
2947 1099642 : r = base::Divide(z * t1, (t1 - two) - (w + z * w));
2948 1099639 : z = one - (r - z);
2949 1099639 : GET_HIGH_WORD(j, z);
2950 1099639 : j += static_cast<int>(static_cast<uint32_t>(n) << 20);
2951 1099639 : if ((j >> 20) <= 0) {
2952 575 : z = scalbn(z, n); /* subnormal output */
2953 : } else {
2954 : int tmp;
2955 1099064 : GET_HIGH_WORD(tmp, z);
2956 1099064 : SET_HIGH_WORD(z, tmp + static_cast<int>(static_cast<uint32_t>(n) << 20));
2957 : }
2958 1099639 : return s * z;
2959 : }
2960 :
2961 : /*
2962 : * ES6 draft 09-27-13, section 20.2.2.30.
2963 : * Math.sinh
2964 : * Method :
2965 : * mathematically sinh(x) if defined to be (exp(x)-exp(-x))/2
2966 : * 1. Replace x by |x| (sinh(-x) = -sinh(x)).
2967 : * 2.
2968 : * E + E/(E+1)
2969 : * 0 <= x <= 22 : sinh(x) := --------------, E=expm1(x)
2970 : * 2
2971 : *
2972 : * 22 <= x <= lnovft : sinh(x) := exp(x)/2
2973 : * lnovft <= x <= ln2ovft: sinh(x) := exp(x/2)/2 * exp(x/2)
2974 : * ln2ovft < x : sinh(x) := x*shuge (overflow)
2975 : *
2976 : * Special cases:
2977 : * sinh(x) is |x| if x is +Infinity, -Infinity, or NaN.
2978 : * only sinh(0)=0 is exact for finite x.
2979 : */
2980 1138 : double sinh(double x) {
2981 : static const double KSINH_OVERFLOW = 710.4758600739439,
2982 : TWO_M28 =
2983 : 3.725290298461914e-9, // 2^-28, empty lower half
2984 : LOG_MAXD = 709.7822265625; // 0x40862E42 00000000, empty lower half
2985 : static const double shuge = 1.0e307;
2986 :
2987 1138 : double h = (x < 0) ? -0.5 : 0.5;
2988 : // |x| in [0, 22]. return sign(x)*0.5*(E+E/(E+1))
2989 1138 : double ax = fabs(x);
2990 1138 : if (ax < 22) {
2991 : // For |x| < 2^-28, sinh(x) = x
2992 594 : if (ax < TWO_M28) return x;
2993 389 : double t = expm1(ax);
2994 389 : if (ax < 1) {
2995 118 : return h * (2 * t - t * t / (t + 1));
2996 : }
2997 271 : return h * (t + t / (t + 1));
2998 : }
2999 : // |x| in [22, log(maxdouble)], return 0.5 * exp(|x|)
3000 544 : if (ax < LOG_MAXD) return h * exp(ax);
3001 : // |x| in [log(maxdouble), overflowthreshold]
3002 : // overflowthreshold = 710.4758600739426
3003 496 : if (ax <= KSINH_OVERFLOW) {
3004 18 : double w = exp(0.5 * ax);
3005 18 : double t = h * w;
3006 18 : return t * w;
3007 : }
3008 : // |x| > overflowthreshold or is NaN.
3009 : // Return Infinity of the appropriate sign or NaN.
3010 478 : return x * shuge;
3011 : }
3012 :
3013 : /* Tanh(x)
3014 : * Return the Hyperbolic Tangent of x
3015 : *
3016 : * Method :
3017 : * x -x
3018 : * e - e
3019 : * 0. tanh(x) is defined to be -----------
3020 : * x -x
3021 : * e + e
3022 : * 1. reduce x to non-negative by tanh(-x) = -tanh(x).
3023 : * 2. 0 <= x < 2**-28 : tanh(x) := x with inexact if x != 0
3024 : * -t
3025 : * 2**-28 <= x < 1 : tanh(x) := -----; t = expm1(-2x)
3026 : * t + 2
3027 : * 2
3028 : * 1 <= x < 22 : tanh(x) := 1 - -----; t = expm1(2x)
3029 : * t + 2
3030 : * 22 <= x <= INF : tanh(x) := 1.
3031 : *
3032 : * Special cases:
3033 : * tanh(NaN) is NaN;
3034 : * only tanh(0)=0 is exact for finite argument.
3035 : */
3036 1048 : double tanh(double x) {
3037 : static const volatile double tiny = 1.0e-300;
3038 : static const double one = 1.0, two = 2.0, huge = 1.0e300;
3039 : double t, z;
3040 : int32_t jx, ix;
3041 :
3042 1048 : GET_HIGH_WORD(jx, x);
3043 1048 : ix = jx & 0x7FFFFFFF;
3044 :
3045 : /* x is INF or NaN */
3046 1048 : if (ix >= 0x7FF00000) {
3047 130 : if (jx >= 0)
3048 102 : return one / x + one; /* tanh(+-inf)=+-1 */
3049 : else
3050 28 : return one / x - one; /* tanh(NaN) = NaN */
3051 : }
3052 :
3053 : /* |x| < 22 */
3054 918 : if (ix < 0x40360000) { /* |x|<22 */
3055 576 : if (ix < 0x3E300000) { /* |x|<2**-28 */
3056 187 : if (huge + x > one) return x; /* tanh(tiny) = tiny with inexact */
3057 : }
3058 389 : if (ix >= 0x3FF00000) { /* |x|>=1 */
3059 253 : t = expm1(two * fabs(x));
3060 253 : z = one - two / (t + two);
3061 : } else {
3062 136 : t = expm1(-two * fabs(x));
3063 136 : z = -t / (t + two);
3064 : }
3065 : /* |x| >= 22, return +-1 */
3066 : } else {
3067 342 : z = one - tiny; /* raise inexact flag */
3068 : }
3069 731 : return (jx >= 0) ? z : -z;
3070 : }
3071 :
3072 : #undef EXTRACT_WORDS
3073 : #undef EXTRACT_WORD64
3074 : #undef GET_HIGH_WORD
3075 : #undef GET_LOW_WORD
3076 : #undef INSERT_WORDS
3077 : #undef INSERT_WORD64
3078 : #undef SET_HIGH_WORD
3079 : #undef SET_LOW_WORD
3080 : #undef STRICT_ASSIGN
3081 :
3082 : } // namespace ieee754
3083 : } // namespace base
3084 : } // namespace v8
|