xref: /freebsd/lib/msun/src/s_ilogb.c (revision 5f757f3f)
1 /*
2  * ====================================================
3  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4  *
5  * Developed at SunPro, a Sun Microsystems, Inc. business.
6  * Permission to use, copy, modify, and distribute this
7  * software is freely granted, provided that this notice
8  * is preserved.
9  * ====================================================
10  */
11 
12 /* ilogb(double x)
13  * return the binary exponent of non-zero x
14  * ilogb(0) = FP_ILOGB0
15  * ilogb(NaN) = FP_ILOGBNAN (no signal is raised)
16  * ilogb(inf) = INT_MAX (no signal is raised)
17  */
18 
19 #include <limits.h>
20 
21 #include "math.h"
22 #include "math_private.h"
23 
24 	int ilogb(double x)
25 {
26 	int32_t hx,lx,ix;
27 
28 	EXTRACT_WORDS(hx,lx,x);
29 	hx &= 0x7fffffff;
30 	if(hx<0x00100000) {
31 	    if((hx|lx)==0)
32 		return FP_ILOGB0;
33 	    else			/* subnormal x */
34 		if(hx==0) {
35 		    for (ix = -1043; lx>0; lx<<=1) ix -=1;
36 		} else {
37 		    for (ix = -1022,hx<<=11; hx>0; hx<<=1) ix -=1;
38 		}
39 	    return ix;
40 	}
41 	else if (hx<0x7ff00000) return (hx>>20)-1023;
42 	else if (hx>0x7ff00000 || lx!=0) return FP_ILOGBNAN;
43 	else return INT_MAX;
44 }
45