xref: /dflybsd-src/contrib/openbsd_libm/src/s_ilogb.c (revision 4382f29d99a100bd77a81697c2f699c11f6a472a)
1*05a0b428SJohn Marino /* @(#)s_ilogb.c 5.1 93/09/24 */
2*05a0b428SJohn Marino /*
3*05a0b428SJohn Marino  * ====================================================
4*05a0b428SJohn Marino  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*05a0b428SJohn Marino  *
6*05a0b428SJohn Marino  * Developed at SunPro, a Sun Microsystems, Inc. business.
7*05a0b428SJohn Marino  * Permission to use, copy, modify, and distribute this
8*05a0b428SJohn Marino  * software is freely granted, provided that this notice
9*05a0b428SJohn Marino  * is preserved.
10*05a0b428SJohn Marino  * ====================================================
11*05a0b428SJohn Marino  */
12*05a0b428SJohn Marino 
13*05a0b428SJohn Marino /* ilogb(double x)
14*05a0b428SJohn Marino  * return the binary exponent of non-zero x
15*05a0b428SJohn Marino  * ilogb(0) = 0x80000001
16*05a0b428SJohn Marino  * ilogb(inf/NaN) = 0x7fffffff (no signal is raised)
17*05a0b428SJohn Marino  */
18*05a0b428SJohn Marino 
19*05a0b428SJohn Marino #include <float.h>
20*05a0b428SJohn Marino #include <math.h>
21*05a0b428SJohn Marino 
22*05a0b428SJohn Marino #include "math_private.h"
23*05a0b428SJohn Marino 
24*05a0b428SJohn Marino int
ilogb(double x)25*05a0b428SJohn Marino ilogb(double x)
26*05a0b428SJohn Marino {
27*05a0b428SJohn Marino 	int32_t hx,lx,ix;
28*05a0b428SJohn Marino 
29*05a0b428SJohn Marino 	GET_HIGH_WORD(hx,x);
30*05a0b428SJohn Marino 	hx &= 0x7fffffff;
31*05a0b428SJohn Marino 	if(hx<0x00100000) {
32*05a0b428SJohn Marino 	    GET_LOW_WORD(lx,x);
33*05a0b428SJohn Marino 	    if((hx|lx)==0)
34*05a0b428SJohn Marino 		return 0x80000001;	/* ilogb(0) = 0x80000001 */
35*05a0b428SJohn Marino 	    else			/* subnormal x */
36*05a0b428SJohn Marino 		if(hx==0) {
37*05a0b428SJohn Marino 		    for (ix = -1043; lx>0; lx<<=1) ix -=1;
38*05a0b428SJohn Marino 		} else {
39*05a0b428SJohn Marino 		    for (ix = -1022,hx<<=11; hx>0; hx<<=1) ix -=1;
40*05a0b428SJohn Marino 		}
41*05a0b428SJohn Marino 	    return ix;
42*05a0b428SJohn Marino 	}
43*05a0b428SJohn Marino 	else if (hx<0x7ff00000) return (hx>>20)-1023;
44*05a0b428SJohn Marino 	else return 0x7fffffff;
45*05a0b428SJohn Marino }
46*05a0b428SJohn Marino 
47*05a0b428SJohn Marino #if	LDBL_MANT_DIG == DBL_MANT_DIG
48*05a0b428SJohn Marino __strong_alias(ilogbl, ilogb);
49*05a0b428SJohn Marino #endif	/* LDBL_MANT_DIG == DBL_MANT_DIG */
50