xref: /titanic_52/usr/src/lib/libm/common/C/exp10.c (revision 6d89ca534e2138511ecb76c02bcec1bcb83f685b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
23  */
24 /*
25  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 #pragma weak exp10 = __exp10
30 
31 /* INDENT OFF */
32 /*
33  * exp10(x)
34  * Code by K.C. Ng for SUN 4.0 libm.
35  * Method :
36  *	n = nint(x*(log10/log2));
37  *	exp10(x) = 10**x = exp(x*ln(10)) = exp(n*ln2+(x*ln10-n*ln2))
38  *		 = 2**n*exp(ln10*(x-n*log2/log10)))
39  *	If x is an integer < 23 then use repeat multiplication. For
40  *	10**22 is the largest representable integer.
41  */
42 /* INDENT ON */
43 
44 #include "libm.h"
45 
46 static const double C[] = {
47 	3.3219280948736234787,	/* log(10)/log(2) */
48 	2.3025850929940456840,	/* log(10) */
49 	3.0102999565860955045E-1,	/* log(2)/log(10) high */
50 	5.3716447674669983622E-12,	/* log(2)/log(10) low */
51 	0.0,
52 	0.5,
53 	1.0,
54 	10.0,
55 	1.0e300,
56 	1.0e-300,
57 };
58 
59 #define	lg10	C[0]
60 #define	ln10	C[1]
61 #define	logt2hi	C[2]
62 #define	logt2lo	C[3]
63 #define	zero	C[4]
64 #define	half	C[5]
65 #define	one	C[6]
66 #define	ten	C[7]
67 #define	huge	C[8]
68 #define	tiny	C[9]
69 
70 double
71 exp10(double x) {
72 	double	t, pt;
73 	int	ix, hx, k;
74 
75 	ix = ((int *)&x)[HIWORD];
76 	hx = ix & ~0x80000000;
77 
78 	if (hx >= 0x4074a000) {	/* |x| >= 330 or x is nan */
79 		if (hx >= 0x7ff00000) {	/* x is inf or nan */
80 			if (ix == 0xfff00000 && ((int *)&x)[LOWORD] == 0)
81 				return (zero);
82 			return (x * x);
83 		}
84 		t = (ix < 0)? tiny : huge;
85 		return (t * t);
86 	}
87 
88 	if (hx < 0x3c000000)
89 		return (one + x);
90 
91 	k = (int)x;
92 	if (0 <= k && k < 23 && (double)k == x) {
93 		/* x is a small positive integer */
94 		t = one;
95 		pt = ten;
96 		if (k & 1)
97 			t = ten;
98 		k >>= 1;
99 		while (k) {
100 			pt *= pt;
101 			if (k & 1)
102 				t *= pt;
103 			k >>= 1;
104 		}
105 		return (t);
106 	}
107 	t = x * lg10;
108 	k = (int)((ix < 0)? t - half : t + half);
109 	return (scalbn(exp(ln10 * ((x - k * logt2hi) - k * logt2lo)), k));
110 }
111