xref: /freebsd/lib/msun/src/s_ilogbl.c (revision c8e7f78a3d28ff6e6223ed136ada8e1e2f34965e)
1 /*
2  * ====================================================
3  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4  *
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  * ====================================================
10  */
11 
12 #include <sys/cdefs.h>
13 #include <float.h>
14 #include <limits.h>
15 #include <math.h>
16 
17 #include "fpmath.h"
18 
19 int
20 ilogbl(long double x)
21 {
22 	union IEEEl2bits u;
23 	unsigned long m;
24 	int b;
25 
26 	u.e = x;
27 	if (u.bits.exp == 0) {
28 		if ((u.bits.manl | u.bits.manh) == 0)
29 			return (FP_ILOGB0);
30 		/* denormalized */
31 		if (u.bits.manh == 0) {
32 			m = 1lu << (LDBL_MANL_SIZE - 1);
33 			for (b = LDBL_MANH_SIZE; !(u.bits.manl & m); m >>= 1)
34 				b++;
35 		} else {
36 			m = 1lu << (LDBL_MANH_SIZE - 1);
37 			for (b = 0; !(u.bits.manh & m); m >>= 1)
38 				b++;
39 		}
40 #ifdef LDBL_IMPLICIT_NBIT
41 		b++;
42 #endif
43 		return (LDBL_MIN_EXP - b - 1);
44 	} else if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1)
45 		return (u.bits.exp - LDBL_MAX_EXP + 1);
46 	else if (u.bits.manl != 0 || u.bits.manh != 0)
47 		return (FP_ILOGBNAN);
48 	else
49 		return (INT_MAX);
50 }
51