xref: /freebsd/lib/msun/src/s_logb.c (revision 315ee00f)
1 /* @(#)s_logb.c 5.1 93/09/24 */
2 /*
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 /*
15  * double logb(x)
16  * IEEE 754 logb. Included to pass IEEE test suite. Not recommend.
17  * Use ilogb instead.
18  */
19 
20 #include <float.h>
21 
22 #include "math.h"
23 #include "math_private.h"
24 
25 static const double
26 two54 = 1.80143985094819840000e+16;	/* 43500000 00000000 */
27 
28 double
29 logb(double x)
30 {
31 	int32_t lx,ix;
32 	EXTRACT_WORDS(ix,lx,x);
33 	ix &= 0x7fffffff;			/* high |x| */
34 	if((ix|lx)==0) return -1.0/fabs(x);
35 	if(ix>=0x7ff00000) return x*x;
36 	if(ix<0x00100000) {
37 		x *= two54;		 /* convert subnormal x to normal */
38 		GET_HIGH_WORD(ix,x);
39 		ix &= 0x7fffffff;
40 		return (double) ((ix>>20)-1023-54);
41 	} else
42 		return (double) ((ix>>20)-1023);
43 }
44 
45 #if (LDBL_MANT_DIG == 53)
46 __weak_reference(logb, logbl);
47 #endif
48