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 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 
28 #pragma weak exp10l = __exp10l
29 
30 #include "libm.h"
31 
32 /*
33  * exp10l(x)
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 <= M then use repeat multiplication. For
38  *	10**M is the largest representable integer, where
39  *		M = 10		single precision (24 bits)
40  *		M = 22		double precision (53 bits)
41  *		M = 48		quadruple precision (113 bits)
42  */
43 
44 #define	TINY 	1.0e-20L	/* single: 1e-5, double: 1e-10, quad: 1e-20 */
45 #define	LG10OVT	4933.L		/* single:  39, double:  309, quad:  4933 */
46 #define	LG10UFT	-4966.L		/* single: -45, double: -323, quad: -4966 */
47 #define	M	48
48 			/* logt2hi : last 32 bits is zero for quad prec */
49 #define	LOGT2HI	0.30102999566398119521373889472420986034688L
50 #define	LOGT2LO	2.831664213089468167896664371953e-31L
51 
52 static const long double
53 	zero	= 0.0L,
54 	tiny	= TINY * TINY,
55 	one	= 1.0L,
56 	lg10	= 3.321928094887362347870319429489390175865e+0000L,
57 	ln10	= 2.302585092994045684017991454684364207601e+0000L,
58 	logt2hi	= LOGT2HI,
59 	logt2lo	= LOGT2LO,
60 	lg10ovt	= LG10OVT,
61 	lg10uft	= LG10UFT;
62 
63 long double
exp10l(long double x)64 exp10l(long double x) {
65 	long double t, tenp;
66 	int k;
67 
68 	if (!finitel(x)) {
69 		if (isnanl(x) || x > zero)
70 			return (x + x);
71 		else
72 			return (zero);
73 	}
74 	if (fabsl(x) < tiny)
75 		return (one + x);
76 	if (x <= lg10ovt)
77 		if (x >= lg10uft) {
78 			k = (int) x;
79 			tenp = 10.0L;
80 					/* x is a small +integer */
81 			if (0 <= k && k <= M && (long double) k == x) {
82 				t = one;
83 				if (k & 1)
84 					t *= tenp;
85 				k >>= 1;
86 				while (k) {
87 					tenp *= tenp;
88 					if (k & 1)
89 						t *= tenp;
90 					k >>= 1;
91 				}
92 				return (t);
93 			}
94 			t = anintl(x * lg10);
95 			return (scalbnl(expl(ln10 * ((x - t * logt2hi) -
96 				t * logt2lo)), (int) t));
97 		} else
98 			return (scalbnl(one, -50000));	/* underflow */
99 	else
100 			return (scalbnl(one, 50000));	/* overflow  */
101 }
102