xref: /third_party/musl/src/math/__expo2.c (revision 570af302)
1570af302Sopenharmony_ci#include "libm.h"
2570af302Sopenharmony_ci
3570af302Sopenharmony_ci/* k is such that k*ln2 has minimal relative error and x - kln2 > log(DBL_MIN) */
4570af302Sopenharmony_cistatic const int k = 2043;
5570af302Sopenharmony_cistatic const double kln2 = 0x1.62066151add8bp+10;
6570af302Sopenharmony_ci
7570af302Sopenharmony_ci/* exp(x)/2 for x >= log(DBL_MAX), slightly better than 0.5*exp(x/2)*exp(x/2) */
8570af302Sopenharmony_cidouble __expo2(double x, double sign)
9570af302Sopenharmony_ci{
10570af302Sopenharmony_ci	double scale;
11570af302Sopenharmony_ci
12570af302Sopenharmony_ci	/* note that k is odd and scale*scale overflows */
13570af302Sopenharmony_ci	INSERT_WORDS(scale, (uint32_t)(0x3ff + k/2) << 20, 0);
14570af302Sopenharmony_ci	/* exp(x - k ln2) * 2**(k-1) */
15570af302Sopenharmony_ci	/* in directed rounding correct sign before rounding or overflow is important */
16570af302Sopenharmony_ci	return exp(x - kln2) * (sign * scale) * scale;
17570af302Sopenharmony_ci}
18