xref: /freebsd/lib/msun/src/s_ilogbl.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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 #ifndef lint
14 static char rcsid[] = "$FreeBSD$";
15 #endif
16 
17 #include <float.h>
18 #include <limits.h>
19 #include <math.h>
20 
21 #include "fpmath.h"
22 
23 int
24 ilogbl(long double x)
25 {
26 	union IEEEl2bits u;
27 	unsigned long m;
28 	int b;
29 
30 	u.e = x;
31 	if (u.bits.exp == 0) {
32 		if ((u.bits.manl | u.bits.manh) == 0)
33 			return (FP_ILOGB0);
34 		/* denormalized */
35 		if (u.bits.manh == 0) {
36 			m = 1lu << (LDBL_MANL_SIZE - 1);
37 			for (b = LDBL_MANH_SIZE; !(u.bits.manl & m); m >>= 1)
38 				b++;
39 		} else {
40 			m = 1lu << (LDBL_MANH_SIZE - 1);
41 			for (b = 0; !(u.bits.manh & m); m >>= 1)
42 				b++;
43 		}
44 #ifdef LDBL_IMPLICIT_NBIT
45 		b++;
46 #endif
47 		return (LDBL_MIN_EXP - b - 1);
48 	} else if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1)
49 		return (u.bits.exp - LDBL_MAX_EXP + 1);
50 	else if (u.bits.manl != 0 || u.bits.manh != 0)
51 		return (FP_ILOGBNAN);
52 	else
53 		return (INT_MAX);
54 }
55