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

Packit 6c4009
/* Single-precision log2 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 <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
LOG2F_TABLE_BITS = 4
Packit 6c4009
LOG2F_POLY_ORDER = 4
Packit 6c4009
Packit 6c4009
ULP error: 0.752 (nearest rounding.)
Packit 6c4009
Relative error: 1.9 * 2^-26 (before rounding.)
Packit 6c4009
*/
Packit 6c4009
Packit 6c4009
#define N (1 << LOG2F_TABLE_BITS)
Packit 6c4009
#define T __log2f_data.tab
Packit 6c4009
#define A __log2f_data.poly
Packit 6c4009
#define OFF 0x3f330000
Packit 6c4009
Packit 6c4009
float
Packit 6c4009
__log2f (float x)
Packit 6c4009
{
Packit 6c4009
  /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
Packit 6c4009
  double_t z, r, r2, p, y, y0, invc, logc;
Packit 6c4009
  uint32_t ix, iz, top, tmp;
Packit 6c4009
  int k, i;
Packit 6c4009
Packit 6c4009
  ix = asuint (x);
Packit 6c4009
#if WANT_ROUNDING
Packit 6c4009
  /* Fix sign of zero with downward rounding when x==1.  */
Packit 6c4009
  if (__glibc_unlikely (ix == 0x3f800000))
Packit 6c4009
    return 0;
Packit 6c4009
#endif
Packit 6c4009
  if (__glibc_unlikely (ix - 0x00800000 >= 0x7f800000 - 0x00800000))
Packit 6c4009
    {
Packit 6c4009
      /* x < 0x1p-126 or inf or nan.  */
Packit 6c4009
      if (ix * 2 == 0)
Packit 6c4009
	return __math_divzerof (1);
Packit 6c4009
      if (ix == 0x7f800000) /* log2(inf) == inf.  */
Packit 6c4009
	return x;
Packit 6c4009
      if ((ix & 0x80000000) || ix * 2 >= 0xff000000)
Packit 6c4009
	return __math_invalidf (x);
Packit 6c4009
      /* x is subnormal, normalize it.  */
Packit 6c4009
      ix = asuint (x * 0x1p23f);
Packit 6c4009
      ix -= 23 << 23;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* x = 2^k z; where z is in range [OFF,2*OFF] and exact.
Packit 6c4009
     The range is split into N subintervals.
Packit 6c4009
     The ith subinterval contains z and c is near its center.  */
Packit 6c4009
  tmp = ix - OFF;
Packit 6c4009
  i = (tmp >> (23 - LOG2F_TABLE_BITS)) % N;
Packit 6c4009
  top = tmp & 0xff800000;
Packit 6c4009
  iz = ix - top;
Packit 6c4009
  k = (int32_t) tmp >> 23; /* arithmetic shift */
Packit 6c4009
  invc = T[i].invc;
Packit 6c4009
  logc = T[i].logc;
Packit 6c4009
  z = (double_t) asfloat (iz);
Packit 6c4009
Packit 6c4009
  /* log2(x) = log1p(z/c-1)/ln2 + log2(c) + k */
Packit 6c4009
  r = z * invc - 1;
Packit 6c4009
  y0 = logc + (double_t) k;
Packit 6c4009
Packit 6c4009
  /* Pipelined polynomial evaluation to approximate log1p(r)/ln2.  */
Packit 6c4009
  r2 = r * r;
Packit 6c4009
  y = A[1] * r + A[2];
Packit 6c4009
  y = A[0] * r2 + y;
Packit 6c4009
  p = A[3] * r + y0;
Packit 6c4009
  y = y * r2 + p;
Packit 6c4009
  return (float) y;
Packit 6c4009
}
Packit 6c4009
#ifndef __log2f
Packit 6c4009
strong_alias (__log2f, __ieee754_log2f)
Packit 6c4009
strong_alias (__log2f, __log2f_finite)
Packit 6c4009
versioned_symbol (libm, __log2f, log2f, GLIBC_2_27);
Packit 6c4009
libm_alias_float_other (__log2, log2)
Packit 6c4009
#endif