add support for non-option arguments extension to getopt
[musl] / src / math / exp2f.c
index 55c22ea..cf6126e 100644 (file)
 
 #include "libm.h"
 
-#define TBLBITS 4
-#define TBLSIZE (1 << TBLBITS)
+#define TBLSIZE 16
 
 static const float
-huge  = 0x1p100f,
 redux = 0x1.8p23f / TBLSIZE,
 P1    = 0x1.62e430p-1f,
 P2    = 0x1.ebfbe0p-3f,
 P3    = 0x1.c6b348p-5f,
 P4    = 0x1.3b2c9cp-7f;
 
-static const volatile float twom100 = 0x1p-100f;
-
 static const double exp2ft[TBLSIZE] = {
   0x1.6a09e667f3bcdp-1,
   0x1.7a11473eb0187p-1,
@@ -67,7 +63,7 @@ static const double exp2ft[TBLSIZE] = {
  * Method: (equally-spaced tables)
  *
  *   Reduce x:
- *     x = 2**k + y, for integer k and |y| <= 1/2.
+ *     x = k + y, for integer k and |y| <= 1/2.
  *     Thus we have exp2f(x) = 2**k * exp2(y).
  *
  *   Reduce y:
@@ -87,44 +83,42 @@ static const double exp2ft[TBLSIZE] = {
  */
 float exp2f(float x)
 {
-       double tv, twopk, u, z;
-       float t;
-       uint32_t hx, ix, i0;
-       int32_t k;
+       double_t t, r, z;
+       union {float f; uint32_t i;} u = {x};
+       union {double f; uint64_t i;} uk;
+       uint32_t ix, i0, k;
 
        /* Filter out exceptional cases. */
-       GET_FLOAT_WORD(hx, x);
-       ix = hx & 0x7fffffff;
-       if (ix >= 0x43000000) {  /* |x| >= 128 */
-               if (ix >= 0x7f800000) {
-                       if ((ix & 0x7fffff) != 0 || (hx & 0x80000000) == 0)
-                               return x + x; /* x is NaN or +Inf */
-                       else
-                               return 0.0;   /* x is -Inf */
+       ix = u.i & 0x7fffffff;
+       if (ix > 0x42fc0000) {  /* |x| > 126 */
+               if (u.i >= 0x43000000 && u.i < 0x80000000) {  /* x >= 128 */
+                       x *= 0x1p127f;
+                       return x;
+               }
+               if (u.i >= 0x80000000) {  /* x < -126 */
+                       if (u.i >= 0xc3160000 || (u.i & 0x0000ffff))
+                               FORCE_EVAL(-0x1p-149f/x);
+                       if (u.i >= 0xc3160000)  /* x <= -150 */
+                               return 0;
                }
-               if (x >= 0x1.0p7f)
-                       return huge * huge;   /* overflow */
-               if (x <= -0x1.2cp7f)
-                       return twom100 * twom100; /* underflow */
        } else if (ix <= 0x33000000) {  /* |x| <= 0x1p-25 */
                return 1.0f + x;
        }
 
        /* Reduce x, computing z, i0, and k. */
-       STRICT_ASSIGN(float, t, x + redux);
-       GET_FLOAT_WORD(i0, t);
+       u.f = x + redux;
+       i0 = u.i;
        i0 += TBLSIZE / 2;
-       k = (i0 >> TBLBITS) << 20;
+       k = i0 / TBLSIZE;
+       uk.i = (uint64_t)(0x3ff + k)<<52;
        i0 &= TBLSIZE - 1;
-       t -= redux;
-       z = x - t;
-       INSERT_WORDS(twopk, 0x3ff00000 + k, 0);
-
+       u.f -= redux;
+       z = x - u.f;
        /* Compute r = exp2(y) = exp2ft[i0] * p(z). */
-       tv = exp2ft[i0];
-       u = tv * z;
-       tv = tv + u * (P1 + z * P2) + u * (z * z) * (P3 + z * P4);
+       r = exp2ft[i0];
+       t = r * z;
+       r = r + t * (P1 + z * P2) + t * (z * z) * (P3 + z * P4);
 
-       /* Scale by 2**(k>>20). */
-       return tv * twopk;
+       /* Scale by 2**k */
+       return r * uk.f;
 }