Blame sysdeps/ieee754/dbl-64/e_log10.c

Packit 6c4009
/* @(#)e_log10.c 5.1 93/09/24 */
Packit 6c4009
/*
Packit 6c4009
 * ====================================================
Packit 6c4009
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
Packit 6c4009
 *
Packit 6c4009
 * Developed at SunPro, a Sun Microsystems, Inc. business.
Packit 6c4009
 * Permission to use, copy, modify, and distribute this
Packit 6c4009
 * software is freely granted, provided that this notice
Packit 6c4009
 * is preserved.
Packit 6c4009
 * ====================================================
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/* __ieee754_log10(x)
Packit 6c4009
 * Return the base 10 logarithm of x
Packit 6c4009
 *
Packit 6c4009
 * Method :
Packit 6c4009
 *	Let log10_2hi = leading 40 bits of log10(2) and
Packit 6c4009
 *	    log10_2lo = log10(2) - log10_2hi,
Packit 6c4009
 *	    ivln10   = 1/log(10) rounded.
Packit 6c4009
 *	Then
Packit 6c4009
 *		n = ilogb(x),
Packit 6c4009
 *		if(n<0)  n = n+1;
Packit 6c4009
 *		x = scalbn(x,-n);
Packit 6c4009
 *		log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x))
Packit 6c4009
 *
Packit 6c4009
 * Note 1:
Packit 6c4009
 *	To guarantee log10(10**n)=n, where 10**n is normal, the rounding
Packit 6c4009
 *	mode must set to Round-to-Nearest.
Packit 6c4009
 * Note 2:
Packit 6c4009
 *	[1/log(10)] rounded to 53 bits has error  .198   ulps;
Packit 6c4009
 *	log10 is monotonic at all binary break points.
Packit 6c4009
 *
Packit 6c4009
 * Special cases:
Packit 6c4009
 *	log10(x) is NaN with signal if x < 0;
Packit 6c4009
 *	log10(+INF) is +INF with no signal; log10(0) is -INF with signal;
Packit 6c4009
 *	log10(NaN) is that NaN with no signal;
Packit 6c4009
 *	log10(10**N) = N  for N=0,1,...,22.
Packit 6c4009
 *
Packit 6c4009
 * Constants:
Packit 6c4009
 * The hexadecimal values are the intended ones for the following constants.
Packit 6c4009
 * The decimal values may be used, provided that the compiler will convert
Packit 6c4009
 * from decimal to binary accurately enough to produce the hexadecimal values
Packit 6c4009
 * shown.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <fix-int-fp-convert-zero.h>
Packit 6c4009
Packit 6c4009
static const double two54 = 1.80143985094819840000e+16;         /* 0x43500000, 0x00000000 */
Packit 6c4009
static const double ivln10 = 4.34294481903251816668e-01;        /* 0x3FDBCB7B, 0x1526E50E */
Packit 6c4009
static const double log10_2hi = 3.01029995663611771306e-01;     /* 0x3FD34413, 0x509F6000 */
Packit 6c4009
static const double log10_2lo = 3.69423907715893078616e-13;     /* 0x3D59FEF3, 0x11F12B36 */
Packit 6c4009
Packit 6c4009
double
Packit 6c4009
__ieee754_log10 (double x)
Packit 6c4009
{
Packit 6c4009
  double y, z;
Packit 6c4009
  int32_t i, k, hx;
Packit 6c4009
  uint32_t lx;
Packit 6c4009
Packit 6c4009
  EXTRACT_WORDS (hx, lx, x);
Packit 6c4009
Packit 6c4009
  k = 0;
Packit 6c4009
  if (hx < 0x00100000)
Packit 6c4009
    {                           /* x < 2**-1022  */
Packit 6c4009
      if (__glibc_unlikely (((hx & 0x7fffffff) | lx) == 0))
Packit 6c4009
	return -two54 / fabs (x);	/* log(+-0)=-inf  */
Packit 6c4009
      if (__glibc_unlikely (hx < 0))
Packit 6c4009
	return (x - x) / (x - x);       /* log(-#) = NaN */
Packit 6c4009
      k -= 54;
Packit 6c4009
      x *= two54;               /* subnormal number, scale up x */
Packit 6c4009
      GET_HIGH_WORD (hx, x);
Packit 6c4009
    }
Packit 6c4009
  if (__glibc_unlikely (hx >= 0x7ff00000))
Packit 6c4009
    return x + x;
Packit 6c4009
  k += (hx >> 20) - 1023;
Packit 6c4009
  i = ((uint32_t) k & 0x80000000) >> 31;
Packit 6c4009
  hx = (hx & 0x000fffff) | ((0x3ff - i) << 20);
Packit 6c4009
  y = (double) (k + i);
Packit 6c4009
  if (FIX_INT_FP_CONVERT_ZERO && y == 0.0)
Packit 6c4009
    y = 0.0;
Packit 6c4009
  SET_HIGH_WORD (x, hx);
Packit 6c4009
  z = y * log10_2lo + ivln10 * __ieee754_log (x);
Packit 6c4009
  return z + y * log10_2hi;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
strong_alias (__ieee754_log10, __log10_finite)