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 return x * x + x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
104 sqrt(-inf)=sNaN */
105 }
106 /* take care of zero */
107 if (ix0 <= 0)
108 {
109 if (((ix0 & (~sign)) | ix1) == 0)
110 return x; /* sqrt(+-0) = +-0 */
111 else if (ix0 < 0)
112 return std::numeric_limits<double>::quiet_NaN(); /* sqrt(-ve) = sNaN */
113 }
114 /* normalize x */
115 m = (ix0 >> 20);
116 if (m == 0)
117 { /* subnormal x */
118 while (ix0 == 0)
119 {
120 m -= 21;
121 ix0 |= (ix1 >> 11);
122 ix1 <<= 21;
123 }
124 for (i = 0; (ix0 & 0x00100000) == 0; i++)
125 ix0 <<= 1;
126 m -= i - 1;
127 ix0 |= (ix1 >> (32 - i));
128 ix1 <<= i;
129 }
130 m -= 1023; /* unbias exponent */
131 ix0 = (ix0 & 0x000fffff) | 0x00100000;
132 if (m & 1)
133 { /* odd m, double x to make it even */
134 ix0 += ix0 + ((ix1 & sign) >> 31);
135 ix1 += ix1;
136 }
137 m >>= 1; /* m = [m/2] */
138
139 /* generate sqrt(x) bit by bit */
140 ix0 += ix0 + ((ix1 & sign) >> 31);
141 ix1 += ix1;
142 q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */
143 r = 0x00200000; /* r = moving bit from right to left */
144
145 while (r != 0)
146 {
147 t = s0 + r;
148 if (t <= ix0)
149 {
150 s0 = t + r;
151 ix0 -= t;
152 q += r;
153 }
154 ix0 += ix0 + ((ix1 & sign) >> 31);
155 ix1 += ix1;
156 r >>= 1;
157 }
158
159 r = sign;
160 while (r != 0)
161 {
162 t1 = s1 + r;
163 t = s0;
164 if ((t < ix0) || ((t == ix0) && (t1 <= ix1)))
165 {
166 s1 = t1 + r;
167 if (((t1 & sign) == sign) && (s1 & sign) == 0)
168 s0 += 1;
169 ix0 -= t;
170 if (ix1 < t1)
171 ix0 -= 1;
172 ix1 -= t1;
173 q1 += r;
174 }
175 ix0 += ix0 + ((ix1 & sign) >> 31);
176 ix1 += ix1;
177 r >>= 1;
178 }
179
180 /* use floating add to find out rounding direction */
181 if ((ix0 | ix1) != 0)
182 {
183 z = one - tiny; /* trigger inexact flag */
184 if (z >= one)
185 {
186 z = one + tiny;
187 if (q1 == (uint32_t)0xffffffff)
188 {
189 q1 = 0;
190 q += 1;
191 }
192 else if (z > one)
193 {
194 if (q1 == (uint32_t)0xfffffffe)
195 q += 1;
196 q1 += 2;
197 }
198 else
199 q1 += (q1 & 1);
200 }
201 }
202 ix0 = (q >> 1) + 0x3fe00000;
203 ix1 = q1 >> 1;
204 if ((q & 1) == 1)
205 ix1 |= sign;
206 ix0 += (m << 20);
207 insert_words(z, ix0, ix1);
208 return z;
209 }
210
211 constexpr double scalbn(double x, int n) noexcept
212 {
213 double y = x;
214
215 if (n > 1023)
216 {
217 y *= 0x1p1023;
218 n -= 1023;
219 if (n > 1023)
220 {
221 y *= 0x1p1023;
222 n -= 1023;
223 if (n > 1023)
224 n = 1023;
225 }
226 }
227 else if (n < -1022)
228 {
229 /* make sure final n < -53 to avoid double
230 rounding in the subnormal range */
231 y *= 0x1p-1022 * 0x1p53;
232 n += 1022 - 53;
233 if (n < -1022)
234 {
235 y *= 0x1p-1022 * 0x1p53;
236 n += 1022 - 53;
237 if (n < -1022)
238 n = -1022;
239 }
240 }
241 x = y * std::bit_cast<double>((uint64_t)(0x3ff + n) << 52);
242 return x;
243 }
244
245 constexpr double pow_impl(double x, double y) noexcept
246 {
247 double z, ax, z_h, z_l, p_h, p_l;
248 double y1, t1, t2, r, s, t, u, v, w;
249 int32_t i, j, k, yisint, n;
250 int32_t hx, hy, ix, iy;
251 uint32_t lx, ly;
252
253 extract_words(hx, lx, x);
254 extract_words(hy, ly, y);
255 ix = hx & 0x7fffffff;
256 iy = hy & 0x7fffffff;
257
258 /* y==zero: x**0 = 1 */
259 if ((iy | ly) == 0)
260 return one;
261
262 /* x==1: 1**y = 1, even if y is NaN */
263 if (hx == 0x3ff00000 && lx == 0)
264 return one;
265
266 /* y!=zero: result is NaN if either arg is NaN */
267 if (ix > 0x7ff00000 || ((ix == 0x7ff00000) && (lx != 0)) ||
268 iy > 0x7ff00000 || ((iy == 0x7ff00000) && (ly != 0)))
269 return nan_mix(x, y);
270
271 /* determine if y is an odd int when x < 0
272 * yisint = 0 ... y is not an integer
273 * yisint = 1 ... y is an odd int
274 * yisint = 2 ... y is an even int
275 */
276 yisint = 0;
277 if (hx < 0)
278 {
279 if (iy >= 0x43400000)
280 yisint = 2; /* even integer y */
281 else if (iy >= 0x3ff00000)
282 {
283 k = (iy >> 20) - 0x3ff; /* exponent */
284 if (k > 20)
285 {
286 j = ly >> (52 - k);
287 if (((uint32_t)j << (52 - k)) == ly)
288 yisint = 2 - (j & 1);
289 }
290 else if (ly == 0)
291 {
292 j = iy >> (20 - k);
293 if ((j << (20 - k)) == iy)
294 yisint = 2 - (j & 1);
295 }
296 }
297 }
298
299 /* special value of y */
300 if (ly == 0)
301 {
302 if (iy == 0x7ff00000)
303 { /* y is +-inf */
304 if (((ix - 0x3ff00000) | lx) == 0)
305 return one; /* (-1)**+-inf is 1 */
306 else if (ix >= 0x3ff00000) /* (|x|>1)**+-inf = inf,0 */
307 return (hy >= 0) ? y : zero;
308 else /* (|x|<1)**-,+inf = inf,0 */
309 return (hy < 0) ? -y : zero;
310 }
311 if (iy == 0x3ff00000)
312 { /* y is +-1 */
313 if (hy < 0)
314 return one / x;
315 else
316 return x;
317 }
318 if (hy == 0x40000000)
319 return x * x; /* y is 2 */
320 if (hy == 0x3fe00000)
321 { /* y is 0.5 */
322 if (hx >= 0) /* x >= +0 */
323 return sqrt_impl(x);
324 }
325 }
326
327 ax = cem::abs(x);
328 /* special value of x */
329 if (lx == 0)
330 {
331 if (ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000)
332 {
333 z = ax; /*x is +-0,+-inf,+-1*/
334 if (hy < 0)
335 z = one / z; /* z = (1/|x|) */
336 if (hx < 0)
337 {
338 if (((ix - 0x3ff00000) | yisint) == 0)
339 {
340 z = (z - z) / (z - z); /* (-1)**non-int is NaN */
341 }
342 else if (yisint == 1)
343 z = -z; /* (x<0)**odd = -(|x|**odd) */
344 }
345 return z;
346 }
347 }
348
349 /* CYGNUS LOCAL + fdlibm-5.3 fix: This used to be
350 n = (hx>>31)+1;
351 but ANSI C says a right shift of a signed negative quantity is
352 implementation defined. */
353 n = ((uint32_t)hx >> 31) - 1;
354
355 /* (x<0)**(non-int) is NaN */
356 if ((n | yisint) == 0)
357 return std::numeric_limits<double>::quiet_NaN();
358
359 s = one; /* s (sign of result -ve**odd) = -1 else = 1 */
360 if ((n | (yisint - 1)) == 0)
361 s = -one; /* (-ve)**(odd int) */
362
363 /* |y| is huge */
364 if (iy > 0x41e00000)
365 { /* if |y| > 2**31 */
366 if (iy > 0x43f00000)
367 { /* if |y| > 2**64, must o/uflow */
368 if (ix <= 0x3fefffff)
369 return (hy < 0) ? huge * huge : tiny * tiny;
370 if (ix >= 0x3ff00000)
371 return (hy > 0) ? huge * huge : tiny * tiny;
372 }
373 /* over/underflow if x is not close to one */
374 if (ix < 0x3fefffff)
375 return (hy < 0) ? s * huge * huge : s * tiny * tiny;
376 if (ix > 0x3ff00000)
377 return (hy > 0) ? s * huge * huge : s * tiny * tiny;
378 /* now |1-x| is tiny <= 2**-20, suffice to compute
379 log(x) by x-x^2/2+x^3/3-x^4/4 */
380 t = ax - one; /* t has 20 trailing zeros */
381 w = (t * t) * (half - t * (thrd - t * qrtr));
382 u = ivln2_h * t; /* ivln2_h has 21 sig. bits */
383 v = t * ivln2_l - w * ivln2;
384 t1 = u + v;
385 set_low_word(t1, 0);
386 t2 = v - (t1 - u);
387 }
388 else
389 {
390 double ss, s2, s_h, s_l, t_h, t_l;
391 n = 0;
392 /* take care subnormal number */
393 if (ix < 0x00100000)
394 {
395 ax *= two53;
396 n -= 53;
397 get_high_word(ix, ax);
398 }
399 n += ((ix) >> 20) - 0x3ff;
400 j = ix & 0x000fffff;
401 /* determine interval */
402 ix = j | 0x3ff00000; /* normalize ix */
403 if (j <= 0x3988E)
404 k = 0; /* |x|<sqrt(3/2) */
405 else if (j < 0xBB67A)
406 k = 1; /* |x|<sqrt(3) */
407 else
408 {
409 k = 0;
410 n += 1;
411 ix -= 0x00100000;
412 }
413 set_high_word(ax, ix);
414
415 /* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
416 u = ax - bp[k]; /* bp[0]=1.0, bp[1]=1.5 */
417 v = one / (ax + bp[k]);
418 ss = u * v;
419 s_h = ss;
420 set_low_word(s_h, 0);
421 /* t_h=ax+bp[k] High */
422 t_h = zero;
423 set_high_word(t_h, ((ix >> 1) | 0x20000000) + 0x00080000 + (k << 18));
424 t_l = ax - (t_h - bp[k]);
425 s_l = v * ((u - s_h * t_h) - s_h * t_l);
426 /* compute log(ax) */
427 s2 = ss * ss;
428 r = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
429 r += s_l * (s_h + ss);
430 s2 = s_h * s_h;
431 t_h = 3 + s2 + r;
432 set_low_word(t_h, 0);
433 t_l = r - ((t_h - 3) - s2);
434 /* u+v = ss*(1+...) */
435 u = s_h * t_h;
436 v = s_l * t_h + t_l * ss;
437 /* 2/(3log2)*(ss+...) */
438 p_h = u + v;
439 set_low_word(p_h, 0);
440 p_l = v - (p_h - u);
441 z_h = cp_h * p_h; /* cp_h+cp_l = 2/(3*log2) */
442 z_l = cp_l * p_h + p_l * cp + dp_l[k];
443 /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */
444 t = n;
445 t1 = (((z_h + z_l) + dp_h[k]) + t);
446 set_low_word(t1, 0);
447 t2 = z_l - (((t1 - t) - dp_h[k]) - z_h);
448 }
449
450 /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
451 y1 = y;
452 set_low_word(y1, 0);
453 p_l = (y - y1) * t1 + y * t2;
454 p_h = y1 * t1;
455 z = p_l + p_h;
456 extract_words(j, i, z);
457 if (j >= 0x40900000)
458 { /* z >= 1024 */
459 if (((j - 0x40900000) | i) != 0) /* if z > 1024 */
460 return s * huge * huge; /* overflow */
461 else
462 {
463 if (p_l + ovt > z - p_h)
464 return s * huge * huge; /* overflow */
465 }
466 }
467 else if ((j & 0x7fffffff) >= 0x4090cc00)
468 { /* z <= -1075 */
469 if (((j - 0xc090cc00) | i) != 0) /* z < -1075 */
470 return s * tiny * tiny; /* underflow */
471 else
472 {
473 if (p_l <= z - p_h)
474 return s * tiny * tiny; /* underflow */
475 }
476 }
477 /*
478 * compute 2**(p_h+p_l)
479 */
480 i = j & 0x7fffffff;
481 k = (i >> 20) - 0x3ff;
482 n = 0;
483 if (i > 0x3fe00000)
484 { /* if |z| > 0.5, set n = [z+0.5] */
485 n = j + (0x00100000 >> (k + 1));
486 k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */
487 t = zero;
488 set_high_word(t, n & ~(0x000fffff >> k));
489 n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
490 if (j < 0)
491 n = -n;
492 p_h -= t;
493 }
494 t = p_l + p_h;
495 set_low_word(t, 0);
496 u = t * lg2_h;
497 v = (p_l - (t - p_h)) * lg2 + t * lg2_l;
498 z = u + v;
499 w = v - (z - u);
500 t = z * z;
501 t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
502 r = (z * t1) / (t1 - two) - (w + z * w);
503 z = one - (r - z);
504 get_high_word(j, z);
505 j += (n << 20);
506
507 if ((j >> 20) <= 0)
508 z = scalbn(z, n); /* subnormal output */
509 else
510 set_high_word(z, j);
511 return s * z;
512 }
513}
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
constexpr auto abs(T num)
TODO: round.