xref: /freebsd/lib/msun/src/s_logbl.c (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
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 long double
24 logbl(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) {	/* x == 0 */
33 			u.bits.sign = 1;
34 			return (1.0L / u.e);
35 		}
36 		/* denormalized */
37 		if (u.bits.manh == 0) {
38 			m = 1lu << (LDBL_MANL_SIZE - 1);
39 			for (b = LDBL_MANH_SIZE; !(u.bits.manl & m); m >>= 1)
40 				b++;
41 		} else {
42 			m = 1lu << (LDBL_MANH_SIZE - 1);
43 			for (b = 0; !(u.bits.manh & m); m >>= 1)
44 				b++;
45 		}
46 #ifdef LDBL_IMPLICIT_NBIT
47 		b++;
48 #endif
49 		return ((long double)(LDBL_MIN_EXP - b - 1));
50 	}
51 	if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1)	/* normal */
52 		return ((long double)(u.bits.exp - LDBL_MAX_EXP + 1));
53 	else						/* +/- inf or nan */
54 		return (x * x);
55 }
56