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