12116Sjkh/*
22116Sjkh * ====================================================
32116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
42116Sjkh *
52116Sjkh * Developed at SunPro, a Sun Microsystems, Inc. business.
62116Sjkh * Permission to use, copy, modify, and distribute this
78870Srgrimes * software is freely granted, provided that this notice
82116Sjkh * is preserved.
92116Sjkh * ====================================================
102116Sjkh */
112116Sjkh
12176451Sdas#include <sys/cdefs.h>
13176451Sdas__FBSDID("$FreeBSD$");
142116Sjkh
15219361Sdas/*
16226375Sdas * Float version of e_log10.c.  See the latter for most comments.
17219361Sdas */
18219361Sdas
192116Sjkh#include "math.h"
202116Sjkh#include "math_private.h"
21219361Sdas#include "k_logf.h"
222116Sjkh
232116Sjkhstatic const float
242116Sjkhtwo25      =  3.3554432000e+07, /* 0x4c000000 */
25219361Sdasivln10hi   =  4.3432617188e-01, /* 0x3ede6000 */
26219361Sdasivln10lo   = -3.1689971365e-05, /* 0xb804ead9 */
272116Sjkhlog10_2hi  =  3.0102920532e-01, /* 0x3e9a2080 */
282116Sjkhlog10_2lo  =  7.9034151668e-07; /* 0x355427db */
292116Sjkh
302116Sjkhstatic const float zero   =  0.0;
31251024Sdasstatic volatile float vzero = 0.0;
322116Sjkh
3397413Salfredfloat
3497413Salfred__ieee754_log10f(float x)
352116Sjkh{
36226411Sdas	float f,hfsq,hi,lo,r,y;
372116Sjkh	int32_t i,k,hx;
382116Sjkh
392116Sjkh	GET_FLOAT_WORD(hx,x);
402116Sjkh
41226375Sdas	k=0;
42226375Sdas	if (hx < 0x00800000) {			/* x < 2**-126  */
43226375Sdas	    if ((hx&0x7fffffff)==0)
44251024Sdas		return -two25/vzero;		/* log(+-0)=-inf */
45226375Sdas	    if (hx<0) return (x-x)/zero;	/* log(-#) = NaN */
46226375Sdas	    k -= 25; x *= two25; /* subnormal number, scale up x */
472116Sjkh	    GET_FLOAT_WORD(hx,x);
48226375Sdas	}
492116Sjkh	if (hx >= 0x7f800000) return x+x;
50226376Sdas	if (hx == 0x3f800000)
51226376Sdas	    return zero;			/* log(1) = +0 */
522116Sjkh	k += (hx>>23)-127;
53219361Sdas	hx &= 0x007fffff;
54219361Sdas	i = (hx+(0x4afb0d))&0x800000;
55219361Sdas	SET_FLOAT_WORD(x,hx|(i^0x3f800000));	/* normalize x or x/2 */
56219361Sdas	k += (i>>23);
57219361Sdas	y = (float)k;
58226376Sdas	f = x - (float)1.0;
59226376Sdas	hfsq = (float)0.5*f*f;
60226376Sdas	r = k_log1pf(f);
61226376Sdas
62226376Sdas	/* See e_log2f.c and e_log2.c for details. */
63226376Sdas	if (sizeof(float_t) > sizeof(float))
64226376Sdas		return (r - hfsq + f) * ((float_t)ivln10lo + ivln10hi) +
65226376Sdas		    y * ((float_t)log10_2lo + log10_2hi);
66226376Sdas	hi = f - hfsq;
67226376Sdas	GET_FLOAT_WORD(hx,hi);
68219361Sdas	SET_FLOAT_WORD(hi,hx&0xfffff000);
69226376Sdas	lo = (f - hi) - hfsq + r;
70226376Sdas	return y*log10_2lo + (lo+hi)*ivln10lo + lo*ivln10hi + hi*ivln10hi +
71226376Sdas	    y*log10_2hi;
722116Sjkh}
73