xref: /relibc/openlibm/src/s_ilogbl.c (revision 830dc991f3bc055717fe0c46bd24746f8177f455)
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 #include "cdefs-compat.h"
14 //__FBSDID("$FreeBSD: src/lib/msun/src/s_ilogbl.c,v 1.2 2008/02/22 02:30:35 das Exp $");
15 
16 #include <float.h>
17 #include <limits.h>
18 #include <openlibm_math.h>
19 
20 #include "fpmath.h"
21 #include "math_private.h"
22 
23 DLLEXPORT 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