xref: /freebsd/lib/msun/src/s_logb.c (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
1 /*
2  * ====================================================
3  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4  *
5  * Developed at SunPro, a Sun Microsystems, Inc. business.
6  * Permission to use, copy, modify, and distribute this
7  * software is freely granted, provided that this notice
8  * is preserved.
9  * ====================================================
10  */
11 
12 /*
13  * double logb(x)
14  * IEEE 754 logb. Included to pass IEEE test suite. Not recommend.
15  * Use ilogb instead.
16  */
17 
18 #include <float.h>
19 
20 #include "math.h"
21 #include "math_private.h"
22 
23 static const double
24 two54 = 1.80143985094819840000e+16;	/* 43500000 00000000 */
25 
26 double
27 logb(double x)
28 {
29 	int32_t lx,ix;
30 	EXTRACT_WORDS(ix,lx,x);
31 	ix &= 0x7fffffff;			/* high |x| */
32 	if((ix|lx)==0) return -1.0/fabs(x);
33 	if(ix>=0x7ff00000) return x*x;
34 	if(ix<0x00100000) {
35 		x *= two54;		 /* convert subnormal x to normal */
36 		GET_HIGH_WORD(ix,x);
37 		ix &= 0x7fffffff;
38 		return (double) ((ix>>20)-1023-54);
39 	} else
40 		return (double) ((ix>>20)-1023);
41 }
42 
43 #if (LDBL_MANT_DIG == 53)
44 __weak_reference(logb, logbl);
45 #endif
46