]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - sysdeps/ieee754/flt-32/e_log2f.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / ieee754 / flt-32 / e_log2f.c
index 7453214516c19a1acad6dd14a9bf27d525444c86..9bee8362befe71ff9c743f0711604c39d8487c9d 100644 (file)
@@ -1,80 +1,95 @@
-/* e_logf.c -- float version of e_log.c.
- * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
- * adapted for log2 by Ulrich Drepper <drepper@cygnus.com>
- */
+/* Single-precision log2 function.
+   Copyright (C) 2017-2020 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
 
-/*
- * ====================================================
- * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
- *
- * Developed at SunPro, a Sun Microsystems, Inc. business.
- * Permission to use, copy, modify, and distribute this
- * software is freely granted, provided that this notice
- * is preserved.
- * ====================================================
- */
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
 
+#include <math.h>
+#include <stdint.h>
+#include <shlib-compat.h>
+#include <libm-alias-float.h>
+#include "math_config.h"
 
-#include "math.h"
-#include "math_private.h"
+/*
+LOG2F_TABLE_BITS = 4
+LOG2F_POLY_ORDER = 4
 
-static const float
-ln2 = 0.69314718055994530942,
-two25 =    3.355443200e+07,    /* 0x4c000000 */
-Lg1 = 6.6666668653e-01,        /* 3F2AAAAB */
-Lg2 = 4.0000000596e-01,        /* 3ECCCCCD */
-Lg3 = 2.8571429849e-01, /* 3E924925 */
-Lg4 = 2.2222198546e-01, /* 3E638E29 */
-Lg5 = 1.8183572590e-01, /* 3E3A3325 */
-Lg6 = 1.5313838422e-01, /* 3E1CD04F */
-Lg7 = 1.4798198640e-01; /* 3E178897 */
+ULP error: 0.752 (nearest rounding.)
+Relative error: 1.9 * 2^-26 (before rounding.)
+*/
 
-static const float zero   =  0.0;
+#define N (1 << LOG2F_TABLE_BITS)
+#define T __log2f_data.tab
+#define A __log2f_data.poly
+#define OFF 0x3f330000
 
 float
-__ieee754_log2f(float x)
+__log2f (float x)
 {
-       float hfsq,f,s,z,R,w,t1,t2,dk;
-       int32_t k,ix,i,j;
+  /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
+  double_t z, r, r2, p, y, y0, invc, logc;
+  uint32_t ix, iz, top, tmp;
+  int k, i;
+
+  ix = asuint (x);
+#if WANT_ROUNDING
+  /* Fix sign of zero with downward rounding when x==1.  */
+  if (__glibc_unlikely (ix == 0x3f800000))
+    return 0;
+#endif
+  if (__glibc_unlikely (ix - 0x00800000 >= 0x7f800000 - 0x00800000))
+    {
+      /* x < 0x1p-126 or inf or nan.  */
+      if (ix * 2 == 0)
+       return __math_divzerof (1);
+      if (ix == 0x7f800000) /* log2(inf) == inf.  */
+       return x;
+      if ((ix & 0x80000000) || ix * 2 >= 0xff000000)
+       return __math_invalidf (x);
+      /* x is subnormal, normalize it.  */
+      ix = asuint (x * 0x1p23f);
+      ix -= 23 << 23;
+    }
+
+  /* x = 2^k z; where z is in range [OFF,2*OFF] and exact.
+     The range is split into N subintervals.
+     The ith subinterval contains z and c is near its center.  */
+  tmp = ix - OFF;
+  i = (tmp >> (23 - LOG2F_TABLE_BITS)) % N;
+  top = tmp & 0xff800000;
+  iz = ix - top;
+  k = (int32_t) tmp >> 23; /* arithmetic shift */
+  invc = T[i].invc;
+  logc = T[i].logc;
+  z = (double_t) asfloat (iz);
 
-       GET_FLOAT_WORD(ix,x);
+  /* log2(x) = log1p(z/c-1)/ln2 + log2(c) + k */
+  r = z * invc - 1;
+  y0 = logc + (double_t) k;
 
-       k=0;
-       if (ix < 0x00800000) {                  /* x < 2**-126  */
-           if (__builtin_expect((ix&0x7fffffff)==0, 0))
-               return -two25/(x-x);            /* log(+-0)=-inf */
-           if (__builtin_expect(ix<0, 0))
-               return (x-x)/(x-x);     /* log(-#) = NaN */
-           k -= 25; x *= two25; /* subnormal number, scale up x */
-           GET_FLOAT_WORD(ix,x);
-       }
-       if (__builtin_expect(ix >= 0x7f800000, 0)) return x+x;
-       k += (ix>>23)-127;
-       ix &= 0x007fffff;
-       i = (ix+(0x95f64<<3))&0x800000;
-       SET_FLOAT_WORD(x,ix|(i^0x3f800000));    /* normalize x or x/2 */
-       k += (i>>23);
-       dk = (float)k;
-       f = x-(float)1.0;
-       if((0x007fffff&(15+ix))<16) {   /* |f| < 2**-20 */
-           if(f==zero) return dk;
-           R = f*f*((float)0.5-(float)0.33333333333333333*f);
-           return dk-(R-f)/ln2;
-       }
-       s = f/((float)2.0+f);
-       z = s*s;
-       i = ix-(0x6147a<<3);
-       w = z*z;
-       j = (0x6b851<<3)-ix;
-       t1= w*(Lg2+w*(Lg4+w*Lg6));
-       t2= z*(Lg1+w*(Lg3+w*(Lg5+w*Lg7)));
-       i |= j;
-       R = t2+t1;
-       if(i>0) {
-           hfsq=(float)0.5*f*f;
-           return dk-((hfsq-(s*(hfsq+R)))-f)/ln2;
-       } else {
-           return dk-((s*(f-R))-f)/ln2;
-       }
+  /* Pipelined polynomial evaluation to approximate log1p(r)/ln2.  */
+  r2 = r * r;
+  y = A[1] * r + A[2];
+  y = A[0] * r2 + y;
+  p = A[3] * r + y0;
+  y = y * r2 + p;
+  return (float) y;
 }
-strong_alias (__ieee754_log2f, __log2f_finite)
+#ifndef __log2f
+strong_alias (__log2f, __ieee754_log2f)
+strong_alias (__log2f, __log2f_finite)
+versioned_symbol (libm, __log2f, log2f, GLIBC_2_27);
+libm_alias_float_other (__log2, log2)
+#endif