Fix ldbl-128ibm ilogbl near powers of 2 (bug 18029).
[glibc.git] / sysdeps / ieee754 / ldbl-128ibm / e_ilogbl.c
blob4088238f3043982d3a0828115de62d3823d79bbf
1 /* s_ilogbl.c -- long double version of s_ilogb.c.
2 * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz.
3 */
5 /*
6 * ====================================================
7 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
9 * Developed at SunPro, a Sun Microsystems, Inc. business.
10 * Permission to use, copy, modify, and distribute this
11 * software is freely granted, provided that this notice
12 * is preserved.
13 * ====================================================
16 #if defined(LIBM_SCCS) && !defined(lint)
17 static char rcsid[] = "$NetBSD: $";
18 #endif
20 /* ilogbl(long double x)
21 * return the binary exponent of non-zero x
22 * ilogbl(0) = FP_ILOGB0
23 * ilogbl(NaN) = FP_ILOGBNAN (no signal is raised)
24 * ilogbl(+-Inf) = INT_MAX (no signal is raised)
27 #include <limits.h>
28 #include <math.h>
29 #include <math_private.h>
30 #include <math_ldbl_opt.h>
32 int __ieee754_ilogbl(long double x)
34 int64_t hx, hxs;
35 int ix;
36 double xhi, xlo;
38 ldbl_unpack (x, &xhi, &xlo);
39 EXTRACT_WORDS64 (hx, xhi);
40 hxs = hx;
41 hx &= 0x7fffffffffffffffLL;
42 if(hx <= 0x0010000000000000LL) {
43 if(hx==0)
44 return FP_ILOGB0; /* ilogbl(0) = FP_ILOGB0 */
45 else /* subnormal x */
46 for (ix = -1022, hx<<=11; hx>0; hx<<=1) ix -=1;
47 return ix;
49 else if (hx < 0x7ff0000000000000LL)
51 int hexp = (hx >> 52) - 0x3ff;
52 /* If the high part is a power of 2, and the low part is
53 nonzero with the opposite sign, the low part affects
54 the exponent. */
55 if ((hx & 0x000fffffffffffffLL) == 0)
57 int64_t lx;
58 EXTRACT_WORDS64 (lx, xlo);
59 if ((hxs ^ lx) < 0 && (lx & 0x7fffffffffffffffLL) != 0)
60 hexp--;
62 return hexp;
64 else if (FP_ILOGBNAN != INT_MAX) {
65 /* ISO C99 requires ilogbl(+-Inf) == INT_MAX. */
66 if (hx==0x7ff0000000000000LL)
67 return INT_MAX;
69 return FP_ILOGBNAN;