getservbyport_r: fix wrong result if getnameinfo fails with EAI_OVERFLOW
[musl] / src / math / exp2f.c
index ea50db4..0360482 100644 (file)
-/* origin: FreeBSD /usr/src/lib/msun/src/s_exp2f.c */
-/*-
- * Copyright (c) 2005 David Schultz <das@FreeBSD.ORG>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
+/*
+ * Single-precision 2^x function.
  *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
+ * Copyright (c) 2017-2018, Arm Limited.
+ * SPDX-License-Identifier: MIT
  */
 
+#include <math.h>
+#include <stdint.h>
 #include "libm.h"
+#include "exp2f_data.h"
 
-#define TBLSIZE 16
+/*
+EXP2F_TABLE_BITS = 5
+EXP2F_POLY_ORDER = 3
 
-static const float
-redux = 0x1.8p23f / TBLSIZE,
-P1    = 0x1.62e430p-1f,
-P2    = 0x1.ebfbe0p-3f,
-P3    = 0x1.c6b348p-5f,
-P4    = 0x1.3b2c9cp-7f;
+ULP error: 0.502 (nearest rounding.)
+Relative error: 1.69 * 2^-34 in [-1/64, 1/64] (before rounding.)
+Wrong count: 168353 (all nearest rounding wrong results with fma.)
+Non-nearest ULP error: 1 (rounded ULP error)
+*/
 
-static const double exp2ft[TBLSIZE] = {
-  0x1.6a09e667f3bcdp-1,
-  0x1.7a11473eb0187p-1,
-  0x1.8ace5422aa0dbp-1,
-  0x1.9c49182a3f090p-1,
-  0x1.ae89f995ad3adp-1,
-  0x1.c199bdd85529cp-1,
-  0x1.d5818dcfba487p-1,
-  0x1.ea4afa2a490dap-1,
-  0x1.0000000000000p+0,
-  0x1.0b5586cf9890fp+0,
-  0x1.172b83c7d517bp+0,
-  0x1.2387a6e756238p+0,
-  0x1.306fe0a31b715p+0,
-  0x1.3dea64c123422p+0,
-  0x1.4bfdad5362a27p+0,
-  0x1.5ab07dd485429p+0,
-};
+#define N (1 << EXP2F_TABLE_BITS)
+#define T __exp2f_data.tab
+#define C __exp2f_data.poly
+#define SHIFT __exp2f_data.shift_scaled
+
+static inline uint32_t top12(float x)
+{
+       return asuint(x) >> 20;
+}
 
-/*
- * exp2f(x): compute the base 2 exponential of x
- *
- * Accuracy: Peak error < 0.501 ulp; location of peak: -0.030110927.
- *
- * Method: (equally-spaced tables)
- *
- *   Reduce x:
- *     x = 2**k + y, for integer k and |y| <= 1/2.
- *     Thus we have exp2f(x) = 2**k * exp2(y).
- *
- *   Reduce y:
- *     y = i/TBLSIZE + z for integer i near y * TBLSIZE.
- *     Thus we have exp2(y) = exp2(i/TBLSIZE) * exp2(z),
- *     with |z| <= 2**-(TBLSIZE+1).
- *
- *   We compute exp2(i/TBLSIZE) via table lookup and exp2(z) via a
- *   degree-4 minimax polynomial with maximum error under 1.4 * 2**-33.
- *   Using double precision for everything except the reduction makes
- *   roundoff error insignificant and simplifies the scaling step.
- *
- *   This method is due to Tang, but I do not use his suggested parameters:
- *
- *      Tang, P.  Table-driven Implementation of the Exponential Function
- *      in IEEE Floating-Point Arithmetic.  TOMS 15(2), 144-157 (1989).
- */
 float exp2f(float x)
 {
-       double tv, twopk, u, z;
-       float t;
-       uint32_t hx, ix, i0, k;
+       uint32_t abstop;
+       uint64_t ki, t;
+       double_t kd, xd, z, r, r2, y, s;
 
-       /* Filter out exceptional cases. */
-       GET_FLOAT_WORD(hx, x);
-       ix = hx & 0x7fffffff;
-       if (ix >= 0x43000000) {  /* |x| >= 128 */
-               if (ix >= 0x7f800000) {
-                       if (hx == 0xff800000) /* -inf */
-                               return 0;
-                       return x;
-               }
-               if (x >= 128) {
-                       STRICT_ASSIGN(float, x, x * 0x1p127f);
-                       return x;
-               }
-               if (x <= -150) {
-                       STRICT_ASSIGN(float, x, 0x1p-100f*0x1p-100f);
-                       return x;
-               }
-       } else if (ix <= 0x33000000) {  /* |x| <= 0x1p-25 */
-               return 1.0f + x;
+       xd = (double_t)x;
+       abstop = top12(x) & 0x7ff;
+       if (predict_false(abstop >= top12(128.0f))) {
+               /* |x| >= 128 or x is nan.  */
+               if (asuint(x) == asuint(-INFINITY))
+                       return 0.0f;
+               if (abstop >= top12(INFINITY))
+                       return x + x;
+               if (x > 0.0f)
+                       return __math_oflowf(0);
+               if (x <= -150.0f)
+                       return __math_uflowf(0);
        }
 
-       /* Reduce x, computing z, i0, and k. */
-       STRICT_ASSIGN(float, t, x + redux);
-       GET_FLOAT_WORD(i0, t);
-       i0 += TBLSIZE / 2;
-       k = (i0 / TBLSIZE) << 20;
-       i0 &= TBLSIZE - 1;
-       t -= redux;
-       z = x - t;
-       INSERT_WORDS(twopk, 0x3ff00000 + k, 0);
-
-       /* 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);
+       /* x = k/N + r with r in [-1/(2N), 1/(2N)] and int k.  */
+       kd = eval_as_double(xd + SHIFT);
+       ki = asuint64(kd);
+       kd -= SHIFT; /* k/N for int k.  */
+       r = xd - kd;
 
-       /* Scale by 2**(k>>20). */
-       return tv * twopk;
+       /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
+       t = T[ki % N];
+       t += ki << (52 - EXP2F_TABLE_BITS);
+       s = asdouble(t);
+       z = C[0] * r + C[1];
+       r2 = r * r;
+       y = C[2] * r + 1;
+       y = z * r2 + y;
+       y = y * s;
+       return eval_as_float(y);
 }