fix regression from a745c4bfc8a9b5db4e48387170da0dc1d39e3abe
[uclibc-ng.git] / libm / s_ilogb.c
blob259ae7b55445ed72b5601450396e2fee23bbc5cf
1 /*
2 * ====================================================
3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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 * ====================================================
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)
19 #include "math.h"
20 #include "math_private.h"
22 int ilogb(double x)
24 int32_t hx,lx,ix;
26 GET_HIGH_WORD(hx, x);
27 hx &= 0x7fffffff;
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);
39 /* each leading zero mantissa bit makes exponent smaller */
40 for (; lx > 0; lx <<= 1)
41 ix--;
42 return ix;
45 if (hx < 0x7ff00000) /* normal x */
46 return (hx>>20) - 1023;
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 */
54 /* NAN. ilogb(NAN) = FP_ILOGBNAN */
55 return FP_ILOGBNAN;
57 libm_hidden_def(ilogb)