xref: /freebsd/lib/msun/src/s_logbl.c (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
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 <sys/cdefs.h>
14 #include <float.h>
15 #include <limits.h>
16 #include <math.h>
17 
18 #include "fpmath.h"
19 
20 long double
21 logbl(long double x)
22 {
23 	union IEEEl2bits u;
24 	unsigned long m;
25 	int b;
26 
27 	u.e = x;
28 	if (u.bits.exp == 0) {
29 		if ((u.bits.manl | u.bits.manh) == 0) {	/* x == 0 */
30 			u.bits.sign = 1;
31 			return (1.0L / u.e);
32 		}
33 		/* denormalized */
34 		if (u.bits.manh == 0) {
35 			m = 1lu << (LDBL_MANL_SIZE - 1);
36 			for (b = LDBL_MANH_SIZE; !(u.bits.manl & m); m >>= 1)
37 				b++;
38 		} else {
39 			m = 1lu << (LDBL_MANH_SIZE - 1);
40 			for (b = 0; !(u.bits.manh & m); m >>= 1)
41 				b++;
42 		}
43 #ifdef LDBL_IMPLICIT_NBIT
44 		b++;
45 #endif
46 		return ((long double)(LDBL_MIN_EXP - b - 1));
47 	}
48 	if (u.bits.exp < (LDBL_MAX_EXP << 1) - 1)	/* normal */
49 		return ((long double)(u.bits.exp - LDBL_MAX_EXP + 1));
50 	else						/* +/- inf or nan */
51 		return (x * x);
52 }
53