header_utils
Loading...
Searching...
No Matches
constexpr_math.inl
1
2/*====================================================
3* Copyright(C) 2004 by Sun Microsystems, Inc.All rights reserved.
4*
5* Permission to use, copy, modify, and distribute this
6* software is freely granted, provided that this notice
7* is preserved.
8* ====================================================
9*/
10
11namespace detail
12{
13 inline constexpr auto nan_mix(auto x, auto y) noexcept { return ((x + 0.0L) + (y + 0)); }
14
15 template <typename A, typename B>
16 constexpr void extract_words(A& high, B& low, double d) noexcept
17 {
18 low = std::bit_cast<uint64_t>(d) & 0xFFFFFFFF;
19 high = (std::bit_cast<uint64_t>(d) >> 32LL) & 0xFFFFFFFF;
20 }
21
22 template <typename A>
23 constexpr void get_high_word(A& high, double d) noexcept
24 {
25 high = (std::bit_cast<uint64_t>(d) >> 32LL) & 0xFFFFFFFF;
26 }
27
28 template <typename A>
29 constexpr void get_low_word(A& low, double d) noexcept
30 {
31 low = std::bit_cast<uint64_t>(d) & 0xFFFFFFFF;
32 }
33
34 template <typename A, typename B>
35 constexpr void insert_words(double& d, A high, B low) noexcept
36 {
37 const auto bits = static_cast<uint64_t>(std::bit_cast<uint32_t>(low)) | (static_cast<uint64_t>(std::bit_cast<uint32_t>(high)) << 32ULL);
38 d = std::bit_cast<double>(bits);
39 }
40
41 template <typename A>
42 constexpr void set_high_word(double& d, A i) noexcept
43 {
44 uint32_t low = 0, high = 0;
45 ::ghassanpl::cem::detail::extract_words(high, low, d);
46 ::ghassanpl::cem::detail::insert_words(d, i, low);
47 }
48
49 template <typename A>
50 constexpr void set_low_word(double& d, A i) noexcept
51 {
52 uint32_t low = 0, high = 0;
53 ::ghassanpl::cem::detail::extract_words(high, low, d);
54 ::ghassanpl::cem::detail::insert_words(d, high, i);
55 }
56
57 static constexpr double bp[] = { 1.0, 1.5 };
58 static constexpr double dp_h[] = { 0.0, 5.84962487220764160156e-01 };
59 static constexpr double dp_l[] = { 0.0, 1.35003920212974897128e-08 };
60 static constexpr double zero = 0.0;
61 static constexpr double half = 0.5;
62 static constexpr double qrtr = 0.25;
63 static constexpr double thrd = 3.3333333333333331e-01;
64 static constexpr double one = 1.0;
65 static constexpr double two = 2.0;
66 static constexpr double two53 = 9007199254740992.0;
67 static constexpr double huge = 1.0e300;
68 static constexpr double tiny = 1.0e-300;
69 static constexpr double L1 = 5.99999999999994648725e-01;
70 static constexpr double L2 = 4.28571428578550184252e-01;
71 static constexpr double L3 = 3.33333329818377432918e-01;
72 static constexpr double L4 = 2.72728123808534006489e-01;
73 static constexpr double L5 = 2.30660745775561754067e-01;
74 static constexpr double L6 = 2.06975017800338417784e-01;
75 static constexpr double P1 = 1.66666666666666019037e-01;
76 static constexpr double P2 = -2.77777777770155933842e-03;
77 static constexpr double P3 = 6.61375632143793436117e-05;
78 static constexpr double P4 = -1.65339022054652515390e-06;
79 static constexpr double P5 = 4.13813679705723846039e-08;
80 static constexpr double lg2 = 6.93147180559945286227e-01;
81 static constexpr double lg2_h = 6.93147182464599609375e-01;
82 static constexpr double lg2_l = -1.90465429995776804525e-09;
83 static constexpr double ovt = 8.0085662595372944372e-0017;
84 static constexpr double cp = 9.61796693925975554329e-01;
85 static constexpr double cp_h = 9.61796700954437255859e-01;
86 static constexpr double cp_l = -7.02846165095275826516e-09;
87 static constexpr double ivln2 = 1.44269504088896338700e+00;
88 static constexpr double ivln2_h = 1.44269502162933349609e+00;
89 static constexpr double ivln2_l = 1.92596299112661746887e-08;
90
91 constexpr double sqrt_impl(double x) noexcept
92 {
93 double z;
94 int32_t sign = (int)0x80000000;
95 int32_t ix0, s0, q, m, t, i;
96 uint32_t r, t1, s1, ix1, q1;
97
98 extract_words(ix0, ix1, x);
99
100 /* take care of Inf and NaN */
101 if ((ix0 & 0x7ff00000) == 0x7ff00000)
102 {
103 if (x == std::numeric_limits<double>::infinity())
104 return x;
105 return std::numeric_limits<double>::quiet_NaN(); /* sqrt(NaN)=NaN, sqrt(+inf)=+inf sqrt(-inf)=sNaN */
106 }
107 /* take care of zero */
108 if (ix0 <= 0)
109 {
110 if (((ix0 & (~sign)) | ix1) == 0)
111 return x; /* sqrt(+-0) = +-0 */
112 else if (ix0 < 0)
113 return std::numeric_limits<double>::quiet_NaN(); /* sqrt(-ve) = sNaN */
114 }
115 /* normalize x */
116 m = (ix0 >> 20);
117 if (m == 0)
118 { /* subnormal x */
119 while (ix0 == 0)
120 {
121 m -= 21;
122 ix0 |= (ix1 >> 11);
123 ix1 <<= 21;
124 }
125 for (i = 0; (ix0 & 0x00100000) == 0; i++)
126 ix0 <<= 1;
127 m -= i - 1;
128 ix0 |= (ix1 >> (32 - i));
129 ix1 <<= i;
130 }
131 m -= 1023; /* unbias exponent */
132 ix0 = (ix0 & 0x000fffff) | 0x00100000;
133 if (m & 1)
134 { /* odd m, double x to make it even */
135 ix0 += ix0 + ((ix1 & sign) >> 31);
136 ix1 += ix1;
137 }
138 m >>= 1; /* m = [m/2] */
139
140 /* generate sqrt(x) bit by bit */
141 ix0 += ix0 + ((ix1 & sign) >> 31);
142 ix1 += ix1;
143 q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */
144 r = 0x00200000; /* r = moving bit from right to left */
145
146 while (r != 0)
147 {
148 t = s0 + r;
149 if (t <= ix0)
150 {
151 s0 = t + r;
152 ix0 -= t;
153 q += r;
154 }
155 ix0 += ix0 + ((ix1 & sign) >> 31);
156 ix1 += ix1;
157 r >>= 1;
158 }
159
160 r = sign;
161 while (r != 0)
162 {
163 t1 = s1 + r;
164 t = s0;
165 if ((t < ix0) || ((t == ix0) && (t1 <= ix1)))
166 {
167 s1 = t1 + r;
168 if (((t1 & sign) == sign) && (s1 & sign) == 0)
169 s0 += 1;
170 ix0 -= t;
171 if (ix1 < t1)
172 ix0 -= 1;
173 ix1 -= t1;
174 q1 += r;
175 }
176 ix0 += ix0 + ((ix1 & sign) >> 31);
177 ix1 += ix1;
178 r >>= 1;
179 }
180
181 /* use floating add to find out rounding direction */
182 if ((ix0 | ix1) != 0)
183 {
184 z = one - tiny; /* trigger inexact flag */
185 if (z >= one)
186 {
187 z = one + tiny;
188 if (q1 == (uint32_t)0xffffffff)
189 {
190 q1 = 0;
191 q += 1;
192 }
193 else if (z > one)
194 {
195 if (q1 == (uint32_t)0xfffffffe)
196 q += 1;
197 q1 += 2;
198 }
199 else
200 q1 += (q1 & 1);
201 }
202 }
203 ix0 = (q >> 1) + 0x3fe00000;
204 ix1 = q1 >> 1;
205 if ((q & 1) == 1)
206 ix1 |= sign;
207 ix0 += (m << 20);
208 insert_words(z, ix0, ix1);
209 return z;
210 }
211
212 constexpr double scalbn(double x, int n) noexcept
213 {
214 double y = x;
215
216 if (n > 1023)
217 {
218 y *= 0x1p1023;
219 n -= 1023;
220 if (n > 1023)
221 {
222 y *= 0x1p1023;
223 n -= 1023;
224 if (n > 1023)
225 n = 1023;
226 }
227 }
228 else if (n < -1022)
229 {
230 /* make sure final n < -53 to avoid double
231 rounding in the subnormal range */
232 y *= 0x1p-1022 * 0x1p53;
233 n += 1022 - 53;
234 if (n < -1022)
235 {
236 y *= 0x1p-1022 * 0x1p53;
237 n += 1022 - 53;
238 if (n < -1022)
239 n = -1022;
240 }
241 }
242 x = y * std::bit_cast<double>((uint64_t)(0x3ff + n) << 52);
243 return x;
244 }
245
246 constexpr double pow_impl(double x, double y) noexcept
247 {
248 double z, ax, z_h, z_l, p_h, p_l;
249 double y1, t1, t2, r, s, t, u, v, w;
250 int32_t i, j, k, yisint, n;
251 int32_t hx, hy, ix, iy;
252 uint32_t lx, ly;
253
254 extract_words(hx, lx, x);
255 extract_words(hy, ly, y);
256 ix = hx & 0x7fffffff;
257 iy = hy & 0x7fffffff;
258
259 /* y==zero: x**0 = 1 */
260 if ((iy | ly) == 0)
261 return one;
262
263 /* x==1: 1**y = 1, even if y is NaN */
264 if (hx == 0x3ff00000 && lx == 0)
265 return one;
266
267 /* y!=zero: result is NaN if either arg is NaN */
268 if (ix > 0x7ff00000 || ((ix == 0x7ff00000) && (lx != 0)) ||
269 iy > 0x7ff00000 || ((iy == 0x7ff00000) && (ly != 0)))
270 return std::numeric_limits<double>::quiet_NaN();
271
272 /* determine if y is an odd int when x < 0
273 * yisint = 0 ... y is not an integer
274 * yisint = 1 ... y is an odd int
275 * yisint = 2 ... y is an even int
276 */
277 yisint = 0;
278 if (hx < 0)
279 {
280 if (iy >= 0x43400000)
281 yisint = 2; /* even integer y */
282 else if (iy >= 0x3ff00000)
283 {
284 k = (iy >> 20) - 0x3ff; /* exponent */
285 if (k > 20)
286 {
287 j = ly >> (52 - k);
288 if (((uint32_t)j << (52 - k)) == ly)
289 yisint = 2 - (j & 1);
290 }
291 else if (ly == 0)
292 {
293 j = iy >> (20 - k);
294 if ((j << (20 - k)) == iy)
295 yisint = 2 - (j & 1);
296 }
297 }
298 }
299
300 /* special value of y */
301 if (ly == 0)
302 {
303 if (iy == 0x7ff00000)
304 { /* y is +-inf */
305 if (((ix - 0x3ff00000) | lx) == 0)
306 return one; /* (-1)**+-inf is 1 */
307 else if (ix >= 0x3ff00000) /* (|x|>1)**+-inf = inf,0 */
308 return (hy >= 0) ? y : zero;
309 else /* (|x|<1)**-,+inf = inf,0 */
310 return (hy < 0) ? -y : zero;
311 }
312 if (iy == 0x3ff00000)
313 { /* y is +-1 */
314 if (hy < 0)
315 return one / x;
316 else
317 return x;
318 }
319 if (hy == 0x40000000)
320 return x * x; /* y is 2 */
321 if (hy == 0x3fe00000)
322 { /* y is 0.5 */
323 if (hx >= 0) /* x >= +0 */
324 return sqrt_impl(x);
325 }
326 }
327
328 ax = cem::abs(x);
329 /* special value of x */
330 if (lx == 0)
331 {
332 if (ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000)
333 {
334 z = ax; /*x is +-0,+-inf,+-1*/
335 if (hy < 0)
336 z = one / z; /* z = (1/|x|) */
337 if (hx < 0)
338 {
339 if (((ix - 0x3ff00000) | yisint) == 0)
340 {
341 z = (z - z) / (z - z); /* (-1)**non-int is NaN */
342 }
343 else if (yisint == 1)
344 z = -z; /* (x<0)**odd = -(|x|**odd) */
345 }
346 return z;
347 }
348 }
349
350 /* CYGNUS LOCAL + fdlibm-5.3 fix: This used to be
351 n = (hx>>31)+1;
352 but ANSI C says a right shift of a signed negative quantity is
353 implementation defined. */
354 n = ((uint32_t)hx >> 31) - 1;
355
356 /* (x<0)**(non-int) is NaN */
357 if ((n | yisint) == 0)
358 return std::numeric_limits<double>::quiet_NaN();
359
360 s = one; /* s (sign of result -ve**odd) = -1 else = 1 */
361 if ((n | (yisint - 1)) == 0)
362 s = -one; /* (-ve)**(odd int) */
363
364 /* |y| is huge */
365 if (iy > 0x41e00000)
366 { /* if |y| > 2**31 */
367 if (iy > 0x43f00000)
368 { /* if |y| > 2**64, must o/uflow */
369 if (ix <= 0x3fefffff)
370 return (hy < 0) ? huge * huge : tiny * tiny;
371 if (ix >= 0x3ff00000)
372 return (hy > 0) ? huge * huge : tiny * tiny;
373 }
374 /* over/underflow if x is not close to one */
375 if (ix < 0x3fefffff)
376 return (hy < 0) ? s * huge * huge : s * tiny * tiny;
377 if (ix > 0x3ff00000)
378 return (hy > 0) ? s * huge * huge : s * tiny * tiny;
379 /* now |1-x| is tiny <= 2**-20, suffice to compute
380 log(x) by x-x^2/2+x^3/3-x^4/4 */
381 t = ax - one; /* t has 20 trailing zeros */
382 w = (t * t) * (half - t * (thrd - t * qrtr));
383 u = ivln2_h * t; /* ivln2_h has 21 sig. bits */
384 v = t * ivln2_l - w * ivln2;
385 t1 = u + v;
386 set_low_word(t1, 0);
387 t2 = v - (t1 - u);
388 }
389 else
390 {
391 double ss, s2, s_h, s_l, t_h, t_l;
392 n = 0;
393 /* take care subnormal number */
394 if (ix < 0x00100000)
395 {
396 ax *= two53;
397 n -= 53;
398 get_high_word(ix, ax);
399 }
400 n += ((ix) >> 20) - 0x3ff;
401 j = ix & 0x000fffff;
402 /* determine interval */
403 ix = j | 0x3ff00000; /* normalize ix */
404 if (j <= 0x3988E)
405 k = 0; /* |x|<sqrt(3/2) */
406 else if (j < 0xBB67A)
407 k = 1; /* |x|<sqrt(3) */
408 else
409 {
410 k = 0;
411 n += 1;
412 ix -= 0x00100000;
413 }
414 set_high_word(ax, ix);
415
416 /* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
417 u = ax - bp[k]; /* bp[0]=1.0, bp[1]=1.5 */
418 v = one / (ax + bp[k]);
419 ss = u * v;
420 s_h = ss;
421 set_low_word(s_h, 0);
422 /* t_h=ax+bp[k] High */
423 t_h = zero;
424 set_high_word(t_h, ((ix >> 1) | 0x20000000) + 0x00080000 + (k << 18));
425 t_l = ax - (t_h - bp[k]);
426 s_l = v * ((u - s_h * t_h) - s_h * t_l);
427 /* compute log(ax) */
428 s2 = ss * ss;
429 r = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
430 r += s_l * (s_h + ss);
431 s2 = s_h * s_h;
432 t_h = 3 + s2 + r;
433 set_low_word(t_h, 0);
434 t_l = r - ((t_h - 3) - s2);
435 /* u+v = ss*(1+...) */
436 u = s_h * t_h;
437 v = s_l * t_h + t_l * ss;
438 /* 2/(3log2)*(ss+...) */
439 p_h = u + v;
440 set_low_word(p_h, 0);
441 p_l = v - (p_h - u);
442 z_h = cp_h * p_h; /* cp_h+cp_l = 2/(3*log2) */
443 z_l = cp_l * p_h + p_l * cp + dp_l[k];
444 /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */
445 t = n;
446 t1 = (((z_h + z_l) + dp_h[k]) + t);
447 set_low_word(t1, 0);
448 t2 = z_l - (((t1 - t) - dp_h[k]) - z_h);
449 }
450
451 /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
452 y1 = y;
453 set_low_word(y1, 0);
454 p_l = (y - y1) * t1 + y * t2;
455 p_h = y1 * t1;
456 z = p_l + p_h;
457 extract_words(j, i, z);
458 if (j >= 0x40900000)
459 { /* z >= 1024 */
460 if (((j - 0x40900000) | i) != 0) /* if z > 1024 */
461 return s * huge * huge; /* overflow */
462 else
463 {
464 if (p_l + ovt > z - p_h)
465 return s * huge * huge; /* overflow */
466 }
467 }
468 else if ((j & 0x7fffffff) >= 0x4090cc00)
469 { /* z <= -1075 */
470 if (((j - 0xc090cc00) | i) != 0) /* z < -1075 */
471 return s * tiny * tiny; /* underflow */
472 else
473 {
474 if (p_l <= z - p_h)
475 return s * tiny * tiny; /* underflow */
476 }
477 }
478 /*
479 * compute 2**(p_h+p_l)
480 */
481 i = j & 0x7fffffff;
482 k = (i >> 20) - 0x3ff;
483 n = 0;
484 if (i > 0x3fe00000)
485 { /* if |z| > 0.5, set n = [z+0.5] */
486 n = j + (0x00100000 >> (k + 1));
487 k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */
488 t = zero;
489 set_high_word(t, n & ~(0x000fffff >> k));
490 n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
491 if (j < 0)
492 n = -n;
493 p_h -= t;
494 }
495 t = p_l + p_h;
496 set_low_word(t, 0);
497 u = t * lg2_h;
498 v = (p_l - (t - p_h)) * lg2 + t * lg2_l;
499 z = u + v;
500 w = v - (z - u);
501 t = z * z;
502 t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
503 r = (z * t1) / (t1 - two) - (w + z * w);
504 z = one - (r - z);
505 get_high_word(j, z);
506 j += (n << 20);
507
508 if ((j >> 20) <= 0)
509 z = scalbn(z, n); /* subnormal output */
510 else
511 set_high_word(z, j);
512 return s * z;
513 }
514}
The below code is based on Sun's libm library code, which is licensed under the following license:
static constexpr double P5
0xBEBBBD41; 0xC5D26BF1
static constexpr double one
0x3fd55555, 0x55555555
static constexpr double lg2
0x3E663769; 0x72BEA4D0
static constexpr double cp_l
0x3FEEC709; 0xE0000000 =(float)cp
static constexpr double lg2_l
0x3FE62E43; 0x00000000
constexpr double sqrt_impl(double x) noexcept
0x3E54AE0B; 0xF85DDF44 =1/ln2 tail
static constexpr double cp
-(1024-log2(ovfl+.5ulp))
static constexpr double lg2_h
0x3FE62E42; 0xFEFA39EF
static constexpr double ovt
0xBE205C61; 0x0CA86C39
static constexpr double P2
0x3FC55555; 0x5555553E
static constexpr double ivln2
0xBE3E2FE0; 0x145B01F5 =tail of cp_h
static constexpr double huge
0x43400000; 0x00000000
static constexpr double L3
0x3FDB6DB6; 0xDB6FABFF
static constexpr double zero
0x3E4CFDEB; 0x43CFD006
static constexpr double L4
0x3FD55555; 0x518F264D
static constexpr double P4
0x3F11566A; 0xAF25DE2C
static constexpr double dp_l[]
0x3FE2B803; 0x40000000
static constexpr double cp_h
0x3FEEC709; 0xDC3A03FD =2/(3ln2)
static constexpr double L2
0x3FE33333; 0x33333303
static constexpr double L5
0x3FD17460; 0xA91D4101
static constexpr double ivln2_h
0x3FF71547; 0x652B82FE =1/ln2
static constexpr double P1
0x3FCA7E28; 0x4A454EEF
static constexpr double P3
0xBF66C16C; 0x16BEBD93
static constexpr double L6
0x3FCD864A; 0x93C9DB65
static constexpr double ivln2_l
0x3FF71547; 0x60000000 =24b 1/ln2