xref: /minix3/lib/libm/src/s_ilogb.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
12fe8fb19SBen Gras /* @(#)s_ilogb.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_ilogb.c,v 1.14 2013/02/09 22:56:00 matt Exp $");
162fe8fb19SBen Gras #endif
172fe8fb19SBen Gras 
182fe8fb19SBen Gras /* ilogb(double x)
192fe8fb19SBen Gras  * return the binary exponent of non-zero x
202fe8fb19SBen Gras  * ilogb(0) = 0x80000001
212fe8fb19SBen Gras  * ilogb(inf/NaN) = 0x7fffffff (no signal is raised)
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(ilogbl,ilogb)28*84d9c625SLionel Sambuc __strong_alias(ilogbl,ilogb)
29*84d9c625SLionel Sambuc #endif
30*84d9c625SLionel Sambuc 
312fe8fb19SBen Gras int
322fe8fb19SBen Gras ilogb(double x)
332fe8fb19SBen Gras {
342fe8fb19SBen Gras 	int32_t hx,lx,ix;
352fe8fb19SBen Gras 
362fe8fb19SBen Gras 	GET_HIGH_WORD(hx,x);
372fe8fb19SBen Gras 	hx &= 0x7fffffff;
382fe8fb19SBen Gras 	if(hx<0x00100000) {
392fe8fb19SBen Gras 	    GET_LOW_WORD(lx,x);
402fe8fb19SBen Gras 	    if((hx|lx)==0)
41*84d9c625SLionel Sambuc 		return FP_ILOGB0;	/* ilogb(0) = 0x80000001 */
422fe8fb19SBen Gras 	    else			/* subnormal x */
432fe8fb19SBen Gras 		if(hx==0) {
442fe8fb19SBen Gras 		    for (ix = -1043; lx>0; lx<<=1) ix -=1;
452fe8fb19SBen Gras 		} else {
462fe8fb19SBen Gras 		    for (ix = -1022,hx<<=11; hx>0; hx<<=1) ix -=1;
472fe8fb19SBen Gras 		}
482fe8fb19SBen Gras 	    return ix;
492fe8fb19SBen Gras 	}
502fe8fb19SBen Gras 	else if (hx<0x7ff00000) return (hx>>20)-1023;
51*84d9c625SLionel Sambuc 	else return FP_ILOGBNAN;	/* inf too */
522fe8fb19SBen Gras }
53