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 2011 Nexenta Systems, Inc.  All rights reserved.
  24  */
  25 
  26 /*
  27  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  28  * Use is subject to license terms.
  29  */
  30 
  31 
  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 
  43 #include "libm.h"
  44 
  45 static const double C[] = {
  46         3.3219280948736234787,          /* log(10)/log(2) */
  47         2.3025850929940456840,          /* log(10) */
  48         3.0102999565860955045E-1,       /* log(2)/log(10) high */
  49         5.3716447674669983622E-12,      /* log(2)/log(10) low */
  50         0.0,
  51         0.5,
  52         1.0,
  53         10.0,
  54         1.0e300,
  55         1.0e-300,
  56 };
  57 
  58 #define lg10            C[0]
  59 #define ln10            C[1]
  60 #define logt2hi         C[2]
  61 #define logt2lo         C[3]
  62 #define zero            C[4]
  63 #define half            C[5]
  64 #define one             C[6]
  65 #define ten             C[7]
  66 #define huge            C[8]
  67 #define tiny            C[9]
  68 
  69 double
  70 exp10(double x)
  71 {
  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 
  83                         return (x * x);
  84                 }
  85 
  86                 t = (ix < 0) ? tiny : huge;
  87                 return (t * t);
  88         }
  89 
  90         if (hx < 0x3c000000)
  91                 return (one + x);
  92 
  93         k = (int)x;
  94 
  95         if (0 <= k && k < 23 && (double)k == x) {
  96                 /* x is a small positive integer */
  97                 t = one;
  98                 pt = ten;
  99 
 100                 if (k & 1)
 101                         t = ten;
 102 
 103                 k >>= 1;
 104 
 105                 while (k) {
 106                         pt *= pt;
 107 
 108                         if (k & 1)
 109                                 t *= pt;
 110 
 111                         k >>= 1;
 112                 }
 113 
 114                 return (t);
 115         }
 116 
 117         t = x * lg10;
 118         k = (int)((ix < 0) ? t - half : t + half);
 119         return (scalbn(exp(ln10 * ((x - k * logt2hi) - k * logt2lo)), k));
 120 }