xref: /third_party/musl/src/math/__expo2f.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(FLT_MIN) */
4570af302Sopenharmony_cistatic const int k = 235;
5570af302Sopenharmony_cistatic const float kln2 = 0x1.45c778p+7f;
6570af302Sopenharmony_ci
7570af302Sopenharmony_ci/* expf(x)/2 for x >= log(FLT_MAX), slightly better than 0.5f*expf(x/2)*expf(x/2) */
8570af302Sopenharmony_cifloat __expo2f(float x, float sign)
9570af302Sopenharmony_ci{
10570af302Sopenharmony_ci	float scale;
11570af302Sopenharmony_ci
12570af302Sopenharmony_ci	/* note that k is odd and scale*scale overflows */
13570af302Sopenharmony_ci	SET_FLOAT_WORD(scale, (uint32_t)(0x7f + k/2) << 23);
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 expf(x - kln2) * (sign * scale) * scale;
17570af302Sopenharmony_ci}
18