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 
14 #include <limits.h>
15 
16 int
ilogbl(long double x)17 ilogbl(long double x)
18 {
19 	union IEEEl2bits u;
20 	uint64_t m;
21 	int b;
22 
23 	u.e = x;
24 	if (u.bits.exp == 0) {
25                 if ((u.bits.manl | u.bits.manh) == 0) {
26                         (void) __math_invalidl(x);
27 			return (FP_ILOGB0);
28                 }
29 		/* denormalized */
30 #ifdef LDBL_MANL_SIZE
31 		if (u.bits.manh == 0) {
32 			m = 1llu << (LDBL_MANL_SIZE - 1);
33 			for (b = LDBL_MANH_SIZE; !(u.bits.manl & m); m >>= 1)
34 				b++;
35 		} else
36 #endif
37                 {
38 			m = 1llu << (LDBL_MANH_SIZE - 1);
39 			for (b = 0; !(u.bits.manh & m); m >>= 1)
40 				b++;
41 		}
42 #ifdef LDBL_IMPLICIT_NBIT
43 		b++;
44 #endif
45 		return (LDBL_MIN_EXP - b - 1);
46 	} else if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1)
47 		return (u.bits.exp - LDBL_MAX_EXP + 1);
48 	else if (u.bits.manl != 0 || u.bits.manh != 0) {
49                 (void) __math_invalidl(0.0L);
50 		return (FP_ILOGBNAN);
51 	} else {
52                 (void) __math_invalidl(0.0L);
53 		return (INT_MAX);
54         }
55 }
56