12fe8fb19SBen Gras /* @(#)s_logb.c 5.1 93/09/24 */
22fe8fb19SBen Gras /*
32fe8fb19SBen Gras * ====================================================
42fe8fb19SBen Gras * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
52fe8fb19SBen Gras *
62fe8fb19SBen Gras * Developed at SunPro, a Sun Microsystems, Inc. business.
72fe8fb19SBen Gras * Permission to use, copy, modify, and distribute this
82fe8fb19SBen Gras * software is freely granted, provided that this notice
92fe8fb19SBen Gras * is preserved.
102fe8fb19SBen Gras * ====================================================
112fe8fb19SBen Gras */
122fe8fb19SBen Gras
132fe8fb19SBen Gras #include <sys/cdefs.h>
142fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
15*84d9c625SLionel Sambuc __RCSID("$NetBSD: s_logb.c,v 1.12 2011/08/03 14:13:07 joerg Exp $");
162fe8fb19SBen Gras #endif
172fe8fb19SBen Gras
182fe8fb19SBen Gras /*
192fe8fb19SBen Gras * double logb(x)
202fe8fb19SBen Gras * IEEE 754 logb. Included to pass IEEE test suite. Not recommend.
212fe8fb19SBen Gras * Use ilogb instead.
222fe8fb19SBen Gras */
232fe8fb19SBen Gras
242fe8fb19SBen Gras #include "math.h"
252fe8fb19SBen Gras #include "math_private.h"
262fe8fb19SBen Gras
27*84d9c625SLionel Sambuc #ifndef __HAVE_LONG_DOUBLE
__strong_alias(logbl,logb)28*84d9c625SLionel Sambuc __strong_alias(logbl,logb)
29*84d9c625SLionel Sambuc #endif
30*84d9c625SLionel Sambuc
312fe8fb19SBen Gras double
322fe8fb19SBen Gras logb(double x)
332fe8fb19SBen Gras {
342fe8fb19SBen Gras int32_t lx,ix;
352fe8fb19SBen Gras EXTRACT_WORDS(ix,lx,x);
362fe8fb19SBen Gras ix &= 0x7fffffff; /* high |x| */
372fe8fb19SBen Gras if((ix|lx)==0) return -1.0/fabs(x);
382fe8fb19SBen Gras if(ix>=0x7ff00000) return x*x;
392fe8fb19SBen Gras if((ix>>=20)==0) /* IEEE 754 logb */
402fe8fb19SBen Gras return -1022.0;
412fe8fb19SBen Gras else
422fe8fb19SBen Gras return (double) (ix-1023);
432fe8fb19SBen Gras }
44