Merge branch 'vendor/GCC50'
[dragonfly.git] / lib / libm / src / s_ilogbl.c
1 /*
2  * From: @(#)s_ilogb.c 5.1 93/09/24
3  * $FreeBSD: head/lib/msun/src/s_ilogbl.c 176451 2008-02-22 02:30:36Z das $
4  * ====================================================
5  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6  *
7  * Developed at SunPro, a Sun Microsystems, Inc. business.
8  * Permission to use, copy, modify, and distribute this
9  * software is freely granted, provided that this notice
10  * is preserved.
11  * ====================================================
12  */
13
14 #include <float.h>
15 #include <limits.h>
16 #include <math.h>
17
18 #include "fpmath.h"
19
20 int
21 ilogbl(long double x)
22 {
23         union IEEEl2bits u;
24         unsigned long m;
25         int b;
26
27         u.e = x;
28         if (u.bits.exp == 0) {
29                 if ((u.bits.manl | u.bits.manh) == 0)
30                         return (FP_ILOGB0);
31                 /* denormalized */
32                 if (u.bits.manh == 0) {
33                         m = 1lu << (LDBL_MANL_SIZE - 1);
34                         for (b = LDBL_MANH_SIZE; !(u.bits.manl & m); m >>= 1)
35                                 b++;
36                 } else {
37                         m = 1lu << (LDBL_MANH_SIZE - 1);
38                         for (b = 0; !(u.bits.manh & m); m >>= 1)
39                                 b++;
40                 }
41 #ifdef LDBL_IMPLICIT_NBIT
42                 b++;
43 #endif
44                 return (LDBL_MIN_EXP - b - 1);
45         } else if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1)
46                 return (u.bits.exp - LDBL_MAX_EXP + 1);
47         else if (u.bits.manl != 0 || u.bits.manh != 0)
48                 return (FP_ILOGBNAN);
49         else
50                 return (INT_MAX);
51 }