xref: /titanic_44/usr/src/lib/libm/common/C/exp10.c (revision a60349c89adffc0902b2353230891d8e7f2b24d9)
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 /* INDENT OFF */
30 /*
31  * exp10(x)
32  * Code by K.C. Ng for SUN 4.0 libm.
33  * Method :
34  *	n = nint(x*(log10/log2));
35  *	exp10(x) = 10**x = exp(x*ln(10)) = exp(n*ln2+(x*ln10-n*ln2))
36  *		 = 2**n*exp(ln10*(x-n*log2/log10)))
37  *	If x is an integer < 23 then use repeat multiplication. For
38  *	10**22 is the largest representable integer.
39  */
40 /* INDENT ON */
41 
42 #include "libm.h"
43 
44 static const double C[] = {
45 	3.3219280948736234787,	/* log(10)/log(2) */
46 	2.3025850929940456840,	/* log(10) */
47 	3.0102999565860955045E-1,	/* log(2)/log(10) high */
48 	5.3716447674669983622E-12,	/* log(2)/log(10) low */
49 	0.0,
50 	0.5,
51 	1.0,
52 	10.0,
53 	1.0e300,
54 	1.0e-300,
55 };
56 
57 #define	lg10	C[0]
58 #define	ln10	C[1]
59 #define	logt2hi	C[2]
60 #define	logt2lo	C[3]
61 #define	zero	C[4]
62 #define	half	C[5]
63 #define	one	C[6]
64 #define	ten	C[7]
65 #define	huge	C[8]
66 #define	tiny	C[9]
67 
68 double
69 exp10(double x) {
70 	double	t, pt;
71 	int	ix, hx, k;
72 
73 	ix = ((int *)&x)[HIWORD];
74 	hx = ix & ~0x80000000;
75 
76 	if (hx >= 0x4074a000) {	/* |x| >= 330 or x is nan */
77 		if (hx >= 0x7ff00000) {	/* x is inf or nan */
78 			if (ix == 0xfff00000 && ((int *)&x)[LOWORD] == 0)
79 				return (zero);
80 			return (x * x);
81 		}
82 		t = (ix < 0)? tiny : huge;
83 		return (t * t);
84 	}
85 
86 	if (hx < 0x3c000000)
87 		return (one + x);
88 
89 	k = (int)x;
90 	if (0 <= k && k < 23 && (double)k == x) {
91 		/* x is a small positive integer */
92 		t = one;
93 		pt = ten;
94 		if (k & 1)
95 			t = ten;
96 		k >>= 1;
97 		while (k) {
98 			pt *= pt;
99 			if (k & 1)
100 				t *= pt;
101 			k >>= 1;
102 		}
103 		return (t);
104 	}
105 	t = x * lg10;
106 	k = (int)((ix < 0)? t - half : t + half);
107 	return (scalbn(exp(ln10 * ((x - k * logt2hi) - k * logt2lo)), k));
108 }
109