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 x
14  * ilogb(+-0) = FP_ILOGB0
15  * ilogb(+-inf) = INT_MAX
16  * ilogb(NaN) = FP_ILOGBNAN (no signal is raised)
17  */
18 
19 #include "math.h"
20 #include "math_private.h"
21 
ilogb(double x)22 int ilogb(double x)
23 {
24 	int32_t hx,lx,ix;
25 
26 	GET_HIGH_WORD(hx, x);
27 	hx &= 0x7fffffff;
28 
29 	if (hx < 0x00100000) {
30 		GET_LOW_WORD(lx, x);
31 		if ((hx|lx)==0)  /* +-0, ilogb(0) = FP_ILOGB0 */
32 			return FP_ILOGB0;
33 		/* subnormal x */
34 		ix = -1043;
35 		if (hx != 0) {
36 			ix = -1022;
37 			lx = (hx << 11);
38 		}
39 		/* each leading zero mantissa bit makes exponent smaller */
40 		for (; lx > 0; lx <<= 1)
41 			ix--;
42 		return ix;
43 	}
44 
45 	if (hx < 0x7ff00000) /* normal x */
46 		return (hx>>20) - 1023;
47 
48 	if (FP_ILOGBNAN != (~0U >> 1)) {
49 		GET_LOW_WORD(lx, x);
50 		if (hx == 0x7ff00000 && lx == 0)  /* +-inf */
51 			return ~0U >> 1; /* = INT_MAX */
52 	}
53 
54 	/* NAN. ilogb(NAN) = FP_ILOGBNAN */
55 	return FP_ILOGBNAN;
56 }
57 libm_hidden_def(ilogb)
58