Blame sysdeps/ieee754/flt-32/e_exp2f.c

Packit 6c4009
/* Single-precision 2^x function.
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <math-narrow-eval.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <shlib-compat.h>
Packit 6c4009
#include <libm-alias-float.h>
Packit 6c4009
#include "math_config.h"
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
EXP2F_TABLE_BITS = 5
Packit 6c4009
EXP2F_POLY_ORDER = 3
Packit 6c4009
Packit 6c4009
ULP error: 0.502 (nearest rounding.)
Packit 6c4009
Relative error: 1.69 * 2^-34 in [-1/64, 1/64] (before rounding.)
Packit 6c4009
Wrong count: 168353 (all nearest rounding wrong results with fma.)
Packit 6c4009
Non-nearest ULP error: 1 (rounded ULP error)
Packit 6c4009
*/
Packit 6c4009
Packit 6c4009
#define N (1 << EXP2F_TABLE_BITS)
Packit 6c4009
#define T __exp2f_data.tab
Packit 6c4009
#define C __exp2f_data.poly
Packit 6c4009
#define SHIFT __exp2f_data.shift_scaled
Packit 6c4009
Packit 6c4009
static inline uint32_t
Packit 6c4009
top12 (float x)
Packit 6c4009
{
Packit 6c4009
  return asuint (x) >> 20;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
float
Packit 6c4009
__exp2f (float x)
Packit 6c4009
{
Packit 6c4009
  uint32_t abstop;
Packit 6c4009
  uint64_t ki, t;
Packit 6c4009
  /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
Packit 6c4009
  double_t kd, xd, z, r, r2, y, s;
Packit 6c4009
Packit 6c4009
  xd = (double_t) x;
Packit 6c4009
  abstop = top12 (x) & 0x7ff;
Packit 6c4009
  if (__glibc_unlikely (abstop >= top12 (128.0f)))
Packit 6c4009
    {
Packit 6c4009
      /* |x| >= 128 or x is nan.  */
Packit 6c4009
      if (asuint (x) == asuint (-INFINITY))
Packit 6c4009
	return 0.0f;
Packit 6c4009
      if (abstop >= top12 (INFINITY))
Packit 6c4009
	return x + x;
Packit 6c4009
      if (x > 0.0f)
Packit 6c4009
	return __math_oflowf (0);
Packit 6c4009
      if (x <= -150.0f)
Packit 6c4009
	return __math_uflowf (0);
Packit 6c4009
#if WANT_ERRNO_UFLOW
Packit 6c4009
      if (x < -149.0f)
Packit 6c4009
	return __math_may_uflowf (0);
Packit 6c4009
#endif
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* x = k/N + r with r in [-1/(2N), 1/(2N)] and int k.  */
Packit 6c4009
  kd = math_narrow_eval ((double) (xd + SHIFT)); /* Needs to be double.  */
Packit 6c4009
  ki = asuint64 (kd);
Packit 6c4009
  kd -= SHIFT; /* k/N for int k.  */
Packit 6c4009
  r = xd - kd;
Packit 6c4009
Packit 6c4009
  /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
Packit 6c4009
  t = T[ki % N];
Packit 6c4009
  t += ki << (52 - EXP2F_TABLE_BITS);
Packit 6c4009
  s = asdouble (t);
Packit 6c4009
  z = C[0] * r + C[1];
Packit 6c4009
  r2 = r * r;
Packit 6c4009
  y = C[2] * r + 1;
Packit 6c4009
  y = z * r2 + y;
Packit 6c4009
  y = y * s;
Packit 6c4009
  return (float) y;
Packit 6c4009
}
Packit 6c4009
#ifndef __exp2f
Packit 6c4009
strong_alias (__exp2f, __ieee754_exp2f)
Packit 6c4009
strong_alias (__exp2f, __exp2f_finite)
Packit 6c4009
versioned_symbol (libm, __exp2f, exp2f, GLIBC_2_27);
Packit 6c4009
libm_alias_float_other (__exp2, exp2)
Packit 6c4009
#endif