xref: /inferno-os/libmath/fdlibm/s_ilogb.c (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1 /* derived from /netlib/fdlibm */
2 
3 /* @(#)s_ilogb.c 1.3 95/01/18 */
4 /*
5  * ====================================================
6  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7  *
8  * Developed at SunSoft, a Sun Microsystems, Inc. business.
9  * Permission to use, copy, modify, and distribute this
10  * software is freely granted, provided that this notice
11  * is preserved.
12  * ====================================================
13  */
14 
15 /* ilogb(double x)
16  * return the binary exponent of non-zero x
17  * ilogb(0) = 0x80000001
18  * ilogb(inf/NaN) = 0x7fffffff (no signal is raised)
19  */
20 
21 #include "fdlibm.h"
22 
ilogb(double x)23 	int ilogb(double x)
24 {
25 	int hx,lx,ix;
26 
27 	hx  = (__HI(x))&0x7fffffff;	/* high word of x */
28 	if(hx<0x00100000) {
29 	    lx = __LO(x);
30 	    if((hx|lx)==0)
31 		return 0x80000001;	/* ilogb(0) = 0x80000001 */
32 	    else			/* subnormal x */
33 		if(hx==0) {
34 		    for (ix = -1043; lx>0; lx<<=1) ix -=1;
35 		} else {
36 		    for (ix = -1022,hx<<=11; hx>0; hx<<=1) ix -=1;
37 		}
38 	    return ix;
39 	}
40 	else if (hx<0x7ff00000) return (hx>>20)-1023;
41 	else return 0x7fffffff;
42 }
43