Home | History | Annotate | Line # | Download | only in fpe
fpu_log.c revision 1.3
      1 /*	$NetBSD: fpu_log.c,v 1.3 1996/04/30 11:52:33 briggs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995  Ken Nakata
      5  *	All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the author nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)fpu_log.c	10/8/95
     32  */
     33 
     34 #include <sys/types.h>
     35 #include <sys/systm.h>
     36 
     37 #include "fpu_emulate.h"
     38 
     39 static u_int logA6[] = { 0x3FC2499A, 0xB5E4040B };
     40 static u_int logA5[] = { 0xBFC555B5, 0x848CB7DB };
     41 static u_int logA4[] = { 0x3FC99999, 0x987D8730 };
     42 static u_int logA3[] = { 0xBFCFFFFF, 0xFF6F7E97 };
     43 static u_int logA2[] = { 0x3FD55555, 0x555555A4 };
     44 static u_int logA1[] = { 0xBFE00000, 0x00000008 };
     45 
     46 static u_int logB5[] = { 0x3F175496, 0xADD7DAD6 };
     47 static u_int logB4[] = { 0x3F3C71C2, 0xFE80C7E0 };
     48 static u_int logB3[] = { 0x3F624924, 0x928BCCFF };
     49 static u_int logB2[] = { 0x3F899999, 0x999995EC };
     50 static u_int logB1[] = { 0x3FB55555, 0x55555555 };
     51 
     52 /* sfpn = shortened fp number; can represent only positive numbers */
     53 static struct sfpn {
     54     int		sp_exp;
     55     u_int	sp_m0, sp_m1;
     56 } logtbl[] = {
     57     { 0x3FFE - 0x3fff, 0xFE03F80FU, 0xE03F80FEU },
     58     { 0x3FF7 - 0x3fff, 0xFF015358U, 0x833C47E2U },
     59     { 0x3FFE - 0x3fff, 0xFA232CF2U, 0x52138AC0U },
     60     { 0x3FF9 - 0x3fff, 0xBDC8D83EU, 0xAD88D549U },
     61     { 0x3FFE - 0x3fff, 0xF6603D98U, 0x0F6603DAU },
     62     { 0x3FFA - 0x3fff, 0x9CF43DCFU, 0xF5EAFD48U },
     63     { 0x3FFE - 0x3fff, 0xF2B9D648U, 0x0F2B9D65U },
     64     { 0x3FFA - 0x3fff, 0xDA16EB88U, 0xCB8DF614U },
     65     { 0x3FFE - 0x3fff, 0xEF2EB71FU, 0xC4345238U },
     66     { 0x3FFB - 0x3fff, 0x8B29B775U, 0x1BD70743U },
     67     { 0x3FFE - 0x3fff, 0xEBBDB2A5U, 0xC1619C8CU },
     68     { 0x3FFB - 0x3fff, 0xA8D839F8U, 0x30C1FB49U },
     69     { 0x3FFE - 0x3fff, 0xE865AC7BU, 0x7603A197U },
     70     { 0x3FFB - 0x3fff, 0xC61A2EB1U, 0x8CD907ADU },
     71     { 0x3FFE - 0x3fff, 0xE525982AU, 0xF70C880EU },
     72     { 0x3FFB - 0x3fff, 0xE2F2A47AU, 0xDE3A18AFU },
     73     { 0x3FFE - 0x3fff, 0xE1FC780EU, 0x1FC780E2U },
     74     { 0x3FFB - 0x3fff, 0xFF64898EU, 0xDF55D551U },
     75     { 0x3FFE - 0x3fff, 0xDEE95C4CU, 0xA037BA57U },
     76     { 0x3FFC - 0x3fff, 0x8DB956A9U, 0x7B3D0148U },
     77     { 0x3FFE - 0x3fff, 0xDBEB61EEU, 0xD19C5958U },
     78     { 0x3FFC - 0x3fff, 0x9B8FE100U, 0xF47BA1DEU },
     79     { 0x3FFE - 0x3fff, 0xD901B203U, 0x6406C80EU },
     80     { 0x3FFC - 0x3fff, 0xA9372F1DU, 0x0DA1BD17U },
     81     { 0x3FFE - 0x3fff, 0xD62B80D6U, 0x2B80D62CU },
     82     { 0x3FFC - 0x3fff, 0xB6B07F38U, 0xCE90E46BU },
     83     { 0x3FFE - 0x3fff, 0xD3680D36U, 0x80D3680DU },
     84     { 0x3FFC - 0x3fff, 0xC3FD0329U, 0x06488481U },
     85     { 0x3FFE - 0x3fff, 0xD0B69FCBU, 0xD2580D0BU },
     86     { 0x3FFC - 0x3fff, 0xD11DE0FFU, 0x15AB18CAU },
     87     { 0x3FFE - 0x3fff, 0xCE168A77U, 0x25080CE1U },
     88     { 0x3FFC - 0x3fff, 0xDE1433A1U, 0x6C66B150U },
     89     { 0x3FFE - 0x3fff, 0xCB8727C0U, 0x65C393E0U },
     90     { 0x3FFC - 0x3fff, 0xEAE10B5AU, 0x7DDC8ADDU },
     91     { 0x3FFE - 0x3fff, 0xC907DA4EU, 0x871146ADU },
     92     { 0x3FFC - 0x3fff, 0xF7856E5EU, 0xE2C9B291U },
     93     { 0x3FFE - 0x3fff, 0xC6980C69U, 0x80C6980CU },
     94     { 0x3FFD - 0x3fff, 0x82012CA5U, 0xA68206D7U },
     95     { 0x3FFE - 0x3fff, 0xC4372F85U, 0x5D824CA6U },
     96     { 0x3FFD - 0x3fff, 0x882C5FCDU, 0x7256A8C5U },
     97     { 0x3FFE - 0x3fff, 0xC1E4BBD5U, 0x95F6E947U },
     98     { 0x3FFD - 0x3fff, 0x8E44C60BU, 0x4CCFD7DEU },
     99     { 0x3FFE - 0x3fff, 0xBFA02FE8U, 0x0BFA02FFU },
    100     { 0x3FFD - 0x3fff, 0x944AD09EU, 0xF4351AF6U },
    101     { 0x3FFE - 0x3fff, 0xBD691047U, 0x07661AA3U },
    102     { 0x3FFD - 0x3fff, 0x9A3EECD4U, 0xC3EAA6B2U },
    103     { 0x3FFE - 0x3fff, 0xBB3EE721U, 0xA54D880CU },
    104     { 0x3FFD - 0x3fff, 0xA0218434U, 0x353F1DE8U },
    105     { 0x3FFE - 0x3fff, 0xB92143FAU, 0x36F5E02EU },
    106     { 0x3FFD - 0x3fff, 0xA5F2FCABU, 0xBBC506DAU },
    107     { 0x3FFE - 0x3fff, 0xB70FBB5AU, 0x19BE3659U },
    108     { 0x3FFD - 0x3fff, 0xABB3B8BAU, 0x2AD362A5U },
    109     { 0x3FFE - 0x3fff, 0xB509E68AU, 0x9B94821FU },
    110     { 0x3FFD - 0x3fff, 0xB1641795U, 0xCE3CA97BU },
    111     { 0x3FFE - 0x3fff, 0xB30F6352U, 0x8917C80BU },
    112     { 0x3FFD - 0x3fff, 0xB7047551U, 0x5D0F1C61U },
    113     { 0x3FFE - 0x3fff, 0xB11FD3B8U, 0x0B11FD3CU },
    114     { 0x3FFD - 0x3fff, 0xBC952AFEU, 0xEA3D13E1U },
    115     { 0x3FFE - 0x3fff, 0xAF3ADDC6U, 0x80AF3ADEU },
    116     { 0x3FFD - 0x3fff, 0xC2168ED0U, 0xF458BA4AU },
    117     { 0x3FFE - 0x3fff, 0xAD602B58U, 0x0AD602B6U },
    118     { 0x3FFD - 0x3fff, 0xC788F439U, 0xB3163BF1U },
    119     { 0x3FFE - 0x3fff, 0xAB8F69E2U, 0x8359CD11U },
    120     { 0x3FFD - 0x3fff, 0xCCECAC08U, 0xBF04565DU },
    121     { 0x3FFE - 0x3fff, 0xA9C84A47U, 0xA07F5638U },
    122     { 0x3FFD - 0x3fff, 0xD2420487U, 0x2DD85160U },
    123     { 0x3FFE - 0x3fff, 0xA80A80A8U, 0x0A80A80BU },
    124     { 0x3FFD - 0x3fff, 0xD7894992U, 0x3BC3588AU },
    125     { 0x3FFE - 0x3fff, 0xA655C439U, 0x2D7B73A8U },
    126     { 0x3FFD - 0x3fff, 0xDCC2C4B4U, 0x9887DACCU },
    127     { 0x3FFE - 0x3fff, 0xA4A9CF1DU, 0x96833751U },
    128     { 0x3FFD - 0x3fff, 0xE1EEBD3EU, 0x6D6A6B9EU },
    129     { 0x3FFE - 0x3fff, 0xA3065E3FU, 0xAE7CD0E0U },
    130     { 0x3FFD - 0x3fff, 0xE70D785CU, 0x2F9F5BDCU },
    131     { 0x3FFE - 0x3fff, 0xA16B312EU, 0xA8FC377DU },
    132     { 0x3FFD - 0x3fff, 0xEC1F392CU, 0x5179F283U },
    133     { 0x3FFE - 0x3fff, 0x9FD809FDU, 0x809FD80AU },
    134     { 0x3FFD - 0x3fff, 0xF12440D3U, 0xE36130E6U },
    135     { 0x3FFE - 0x3fff, 0x9E4CAD23U, 0xDD5F3A20U },
    136     { 0x3FFD - 0x3fff, 0xF61CCE92U, 0x346600BBU },
    137     { 0x3FFE - 0x3fff, 0x9CC8E160U, 0xC3FB19B9U },
    138     { 0x3FFD - 0x3fff, 0xFB091FD3U, 0x8145630AU },
    139     { 0x3FFE - 0x3fff, 0x9B4C6F9EU, 0xF03A3CAAU },
    140     { 0x3FFD - 0x3fff, 0xFFE97042U, 0xBFA4C2ADU },
    141     { 0x3FFE - 0x3fff, 0x99D722DAU, 0xBDE58F06U },
    142     { 0x3FFE - 0x3fff, 0x825EFCEDU, 0x49369330U },
    143     { 0x3FFE - 0x3fff, 0x9868C809U, 0x868C8098U },
    144     { 0x3FFE - 0x3fff, 0x84C37A7AU, 0xB9A905C9U },
    145     { 0x3FFE - 0x3fff, 0x97012E02U, 0x5C04B809U },
    146     { 0x3FFE - 0x3fff, 0x87224C2EU, 0x8E645FB7U },
    147     { 0x3FFE - 0x3fff, 0x95A02568U, 0x095A0257U },
    148     { 0x3FFE - 0x3fff, 0x897B8CACU, 0x9F7DE298U },
    149     { 0x3FFE - 0x3fff, 0x94458094U, 0x45809446U },
    150     { 0x3FFE - 0x3fff, 0x8BCF55DEU, 0xC4CD05FEU },
    151     { 0x3FFE - 0x3fff, 0x92F11384U, 0x0497889CU },
    152     { 0x3FFE - 0x3fff, 0x8E1DC0FBU, 0x89E125E5U },
    153     { 0x3FFE - 0x3fff, 0x91A2B3C4U, 0xD5E6F809U },
    154     { 0x3FFE - 0x3fff, 0x9066E68CU, 0x955B6C9BU },
    155     { 0x3FFE - 0x3fff, 0x905A3863U, 0x3E06C43BU },
    156     { 0x3FFE - 0x3fff, 0x92AADE74U, 0xC7BE59E0U },
    157     { 0x3FFE - 0x3fff, 0x8F1779D9U, 0xFDC3A219U },
    158     { 0x3FFE - 0x3fff, 0x94E9BFF6U, 0x15845643U },
    159     { 0x3FFE - 0x3fff, 0x8DDA5202U, 0x37694809U },
    160     { 0x3FFE - 0x3fff, 0x9723A1B7U, 0x20134203U },
    161     { 0x3FFE - 0x3fff, 0x8CA29C04U, 0x6514E023U },
    162     { 0x3FFE - 0x3fff, 0x995899C8U, 0x90EB8990U },
    163     { 0x3FFE - 0x3fff, 0x8B70344AU, 0x139BC75AU },
    164     { 0x3FFE - 0x3fff, 0x9B88BDAAU, 0x3A3DAE2FU },
    165     { 0x3FFE - 0x3fff, 0x8A42F870U, 0x5669DB46U },
    166     { 0x3FFE - 0x3fff, 0x9DB4224FU, 0xFFE1157CU },
    167     { 0x3FFE - 0x3fff, 0x891AC73AU, 0xE9819B50U },
    168     { 0x3FFE - 0x3fff, 0x9FDADC26U, 0x8B7A12DAU },
    169     { 0x3FFE - 0x3fff, 0x87F78087U, 0xF78087F8U },
    170     { 0x3FFE - 0x3fff, 0xA1FCFF17U, 0xCE733BD4U },
    171     { 0x3FFE - 0x3fff, 0x86D90544U, 0x7A34ACC6U },
    172     { 0x3FFE - 0x3fff, 0xA41A9E8FU, 0x5446FB9FU },
    173     { 0x3FFE - 0x3fff, 0x85BF3761U, 0x2CEE3C9BU },
    174     { 0x3FFE - 0x3fff, 0xA633CD7EU, 0x6771CD8BU },
    175     { 0x3FFE - 0x3fff, 0x84A9F9C8U, 0x084A9F9DU },
    176     { 0x3FFE - 0x3fff, 0xA8489E60U, 0x0B435A5EU },
    177     { 0x3FFE - 0x3fff, 0x83993052U, 0x3FBE3368U },
    178     { 0x3FFE - 0x3fff, 0xAA59233CU, 0xCCA4BD49U },
    179     { 0x3FFE - 0x3fff, 0x828CBFBEU, 0xB9A020A3U },
    180     { 0x3FFE - 0x3fff, 0xAC656DAEU, 0x6BCC4985U },
    181     { 0x3FFE - 0x3fff, 0x81848DA8U, 0xFAF0D277U },
    182     { 0x3FFE - 0x3fff, 0xAE6D8EE3U, 0x60BB2468U },
    183     { 0x3FFE - 0x3fff, 0x80808080U, 0x80808081U },
    184     { 0x3FFE - 0x3fff, 0xB07197A2U, 0x3C46C654U },
    185 };
    186 
    187 static struct fpn *__fpu_logn __P((struct fpemu *fe));
    188 
    189 /*
    190  * natural log - algorithm taken from Motorola FPSP,
    191  * except this doesn't bother to check for invalid input.
    192  */
    193 static struct fpn *
    194 __fpu_logn(fe)
    195      struct fpemu *fe;
    196 {
    197     static struct fpn X, F, U, V, W, KLOG2;
    198     struct fpn *d;
    199     int i, k;
    200 
    201     CPYFPN(&X, &fe->fe_f2);
    202 
    203     /* see if |X-1| < 1/16 approx. */
    204     if ((-1 == X.fp_exp && (0xf07d0000U >> (31 - FP_LG)) <= X.fp_mant[0]) ||
    205 	(0 == X.fp_exp && X.fp_mant[0] <= (0x88410000U >> (31 - FP_LG)))) {
    206 	/* log near 1 */
    207 	if (fpu_debug_level & DL_ARITH)
    208 	    printf("__fpu_logn: log near 1\n");
    209 
    210 	fpu_const(&fe->fe_f1, 0x32);
    211 	/* X+1 */
    212 	d = fpu_add(fe);
    213 	CPYFPN(&V, d);
    214 
    215 	CPYFPN(&fe->fe_f1, &X);
    216 	fpu_const(&fe->fe_f2, 0x32); /* 1.0 */
    217 	fe->fe_f2.fp_sign = 1; /* -1.0 */
    218 	/* X-1 */
    219 	d = fpu_add(fe);
    220 	CPYFPN(&fe->fe_f1, d);
    221 	/* 2(X-1) */
    222 	fe->fe_f1.fp_exp++; /* *= 2 */
    223 	CPYFPN(&fe->fe_f2, &V);
    224 	/* U=2(X-1)/(X+1) */
    225 	d = fpu_div(fe);
    226 	CPYFPN(&U, d);
    227 	CPYFPN(&fe->fe_f1, d);
    228 	CPYFPN(&fe->fe_f2, d);
    229 	/* V=U*U */
    230 	d = fpu_mul(fe);
    231 	CPYFPN(&V, d);
    232 	CPYFPN(&fe->fe_f1, d);
    233 	CPYFPN(&fe->fe_f2, d);
    234 	/* W=V*V */
    235 	d = fpu_mul(fe);
    236 	CPYFPN(&W, d);
    237 
    238 	/* calculate U+U*V*([B1+W*(B3+W*B5)]+[V*(B2+W*B4)]) */
    239 
    240 	/* B1+W*(B3+W*B5) part */
    241 	CPYFPN(&fe->fe_f1, d);
    242 	fpu_explode(fe, &fe->fe_f2, FTYPE_DBL, logB5);
    243 	/* W*B5 */
    244 	d = fpu_mul(fe);
    245 	CPYFPN(&fe->fe_f1, d);
    246 	fpu_explode(fe, &fe->fe_f2, FTYPE_DBL, logB3);
    247 	/* B3+W*B5 */
    248 	d = fpu_add(fe);
    249 	CPYFPN(&fe->fe_f1, d);
    250 	CPYFPN(&fe->fe_f2, &W);
    251 	/* W*(B3+W*B5) */
    252 	d = fpu_mul(fe);
    253 	CPYFPN(&fe->fe_f1, d);
    254 	fpu_explode(fe, &fe->fe_f2, FTYPE_DBL, logB1);
    255 	/* B1+W*(B3+W*B5) */
    256 	d = fpu_add(fe);
    257 	CPYFPN(&X, d);
    258 
    259 	/* [V*(B2+W*B4)] part */
    260 	CPYFPN(&fe->fe_f1, &W);
    261 	fpu_explode(fe, &fe->fe_f2, FTYPE_DBL, logB4);
    262 	/* W*B4 */
    263 	d = fpu_mul(fe);
    264 	CPYFPN(&fe->fe_f1, d);
    265 	fpu_explode(fe, &fe->fe_f2, FTYPE_DBL, logB2);
    266 	/* B2+W*B4 */
    267 	d = fpu_add(fe);
    268 	CPYFPN(&fe->fe_f1, d);
    269 	CPYFPN(&fe->fe_f2, &V);
    270 	/* V*(B2+W*B4) */
    271 	d = fpu_mul(fe);
    272 	CPYFPN(&fe->fe_f1, d);
    273 	CPYFPN(&fe->fe_f2, &X);
    274 	/* B1+W*(B3+W*B5)+V*(B2+W*B4) */
    275 	d = fpu_add(fe);
    276 	CPYFPN(&fe->fe_f1, d);
    277 	CPYFPN(&fe->fe_f2, &V);
    278 	/* V*(B1+W*(B3+W*B5)+V*(B2+W*B4)) */
    279 	d = fpu_mul(fe);
    280 	CPYFPN(&fe->fe_f1, d);
    281 	CPYFPN(&fe->fe_f2, &U);
    282 	/* U*V*(B1+W*(B3+W*B5)+V*(B2+W*B4)) */
    283 	d = fpu_mul(fe);
    284 	CPYFPN(&fe->fe_f1, d);
    285 	CPYFPN(&fe->fe_f2, &U);
    286 	/* U+U*V*(B1+W*(B3+W*B5)+V*(B2+W*B4)) */
    287 	d = fpu_add(fe);
    288     } else /* the usual case */ {
    289 	if (fpu_debug_level & DL_ARITH)
    290 	    printf("__fpu_logn: the usual case. X=(%d,%08x,%08x...)\n",
    291 		   X.fp_exp, X.fp_mant[0], X.fp_mant[1]);
    292 
    293 	k = X.fp_exp;
    294 	/* X <- Y */
    295 	X.fp_exp = fe->fe_f2.fp_exp = 0;
    296 
    297 	/* get the most significant 7 bits of X */
    298 	F.fp_class = FPC_NUM;
    299 	F.fp_sign = 0;
    300 	F.fp_exp = X.fp_exp;
    301 	F.fp_mant[0] = X.fp_mant[0] & (0xfe000000U >> (31 - FP_LG));
    302 	F.fp_mant[0] |= (0x01000000U >> (31 - FP_LG));
    303 	F.fp_mant[1] = F.fp_mant[2] = F.fp_mant[3] = 0;
    304 	F.fp_sticky = 0;
    305 
    306 	if (fpu_debug_level & DL_ARITH) {
    307 	    printf("__fpu_logn: X=Y*2^k=(%d,%08x,%08x...)*2^%d\n",
    308 		   fe->fe_f2.fp_exp, fe->fe_f2.fp_mant[0],
    309 		   fe->fe_f2.fp_mant[1], k);
    310 	    printf("__fpu_logn: F=(%d,%08x,%08x...)\n",
    311 		   F.fp_exp, F.fp_mant[0], F.fp_mant[1]);
    312 	}
    313 
    314 	/* index to the table */
    315 	i = (F.fp_mant[0] >> (FP_LG - 7)) & 0x7e;
    316 
    317 	if (fpu_debug_level & DL_ARITH)
    318 	    printf("__fpu_logn: index to logtbl i=%d(%x)\n", i, i);
    319 
    320 	CPYFPN(&fe->fe_f1, &F);
    321 	/* -F */
    322 	fe->fe_f1.fp_sign = 1;
    323 	/* Y-F */
    324 	d = fpu_add(fe);
    325 	CPYFPN(&fe->fe_f1, d);
    326 
    327 	/* fe_f2 = 1/F */
    328 	fe->fe_f2.fp_class = FPC_NUM;
    329 	fe->fe_f2.fp_sign = fe->fe_f2.fp_sticky = fe->fe_f2.fp_mant[3] = 0;
    330 	fe->fe_f2.fp_exp = logtbl[i].sp_exp;
    331 	fe->fe_f2.fp_mant[0] = (logtbl[i].sp_m0 >> (31 - FP_LG));
    332 	fe->fe_f2.fp_mant[1] = (logtbl[i].sp_m0 << (FP_LG + 1)) |
    333 	    (logtbl[i].sp_m1 >> (31 - FP_LG));
    334 	fe->fe_f2.fp_mant[2] = (u_int)(logtbl[i].sp_m1 << (FP_LG + 1));
    335 
    336 	if (fpu_debug_level & DL_ARITH)
    337 	    printf("__fpu_logn: 1/F=(%d,%08x,%08x...)\n", fe->fe_f2.fp_exp,
    338 		   fe->fe_f2.fp_mant[0], fe->fe_f2.fp_mant[1]);
    339 
    340 	/* U = (Y-F) * (1/F) */
    341 	d = fpu_mul(fe);
    342 	CPYFPN(&U, d);
    343 
    344 	/* KLOG2 = K * ln(2) */
    345 	/* fe_f1 == (fpn)k */
    346 	fpu_explode(fe, &fe->fe_f1, FTYPE_LNG, &k);
    347 	(void)fpu_const(&fe->fe_f2, 0x30 /* ln(2) */);
    348 	if (fpu_debug_level & DL_ARITH) {
    349 	    printf("__fpu_logn: fp(k)=(%d,%08x,%08x...)\n", fe->fe_f1.fp_exp,
    350 		   fe->fe_f1.fp_mant[0], fe->fe_f1.fp_mant[1]);
    351 	    printf("__fpu_logn: ln(2)=(%d,%08x,%08x...)\n", fe->fe_f2.fp_exp,
    352 		   fe->fe_f2.fp_mant[0], fe->fe_f2.fp_mant[1]);
    353 	}
    354 	/* K * LOGOF2 */
    355 	d = fpu_mul(fe);
    356 	CPYFPN(&KLOG2, d);
    357 
    358 	/* V=U*U */
    359 	CPYFPN(&fe->fe_f1, &U);
    360 	CPYFPN(&fe->fe_f2, &U);
    361 	d = fpu_mul(fe);
    362 	CPYFPN(&V, d);
    363 
    364 	/*
    365 	 * approximation of LOG(1+U) by
    366 	 * (U+V*(A1+V*(A3+V*A5)))+(U*V*(A2+V*(A4+V*A6)))
    367 	 */
    368 
    369 	/* (U+V*(A1+V*(A3+V*A5))) part */
    370 	CPYFPN(&fe->fe_f1, d);
    371 	fpu_explode(fe, &fe->fe_f2, FTYPE_DBL, logA5);
    372 	/* V*A5 */
    373 	d = fpu_mul(fe);
    374 
    375 	CPYFPN(&fe->fe_f1, d);
    376 	fpu_explode(fe, &fe->fe_f2, FTYPE_DBL, logA3);
    377 	/* A3+V*A5 */
    378 	d = fpu_add(fe);
    379 
    380 	CPYFPN(&fe->fe_f1, d);
    381 	CPYFPN(&fe->fe_f2, &V);
    382 	/* V*(A3+V*A5) */
    383 	d = fpu_mul(fe);
    384 
    385 	CPYFPN(&fe->fe_f1, d);
    386 	fpu_explode(fe, &fe->fe_f2, FTYPE_DBL, logA1);
    387 	/* A1+V*(A3+V*A5) */
    388 	d = fpu_add(fe);
    389 
    390 	CPYFPN(&fe->fe_f1, d);
    391 	CPYFPN(&fe->fe_f2, &V);
    392 	/* V*(A1+V*(A3+V*A5)) */
    393 	d = fpu_mul(fe);
    394 
    395 	CPYFPN(&fe->fe_f1, d);
    396 	CPYFPN(&fe->fe_f2, &U);
    397 	/* U+V*(A1+V*(A3+V*A5)) */
    398 	d = fpu_add(fe);
    399 
    400 	CPYFPN(&X, d);
    401 
    402 	/* (U*V*(A2+V*(A4+V*A6))) part */
    403 	CPYFPN(&fe->fe_f1, &V);
    404 	fpu_explode(fe, &fe->fe_f2, FTYPE_DBL, logA6);
    405 	/* V*A6 */
    406 	d = fpu_mul(fe);
    407 	CPYFPN(&fe->fe_f1, d);
    408 	fpu_explode(fe, &fe->fe_f2, FTYPE_DBL, logA4);
    409 	/* A4+V*A6 */
    410 	d = fpu_add(fe);
    411 	CPYFPN(&fe->fe_f1, d);
    412 	CPYFPN(&fe->fe_f2, &V);
    413 	/* V*(A4+V*A6) */
    414 	d = fpu_mul(fe);
    415 	CPYFPN(&fe->fe_f1, d);
    416 	fpu_explode(fe, &fe->fe_f2, FTYPE_DBL, logA2);
    417 	/* A2+V*(A4+V*A6) */
    418 	d = fpu_add(fe);
    419 	CPYFPN(&fe->fe_f1, d);
    420 	CPYFPN(&fe->fe_f2, &V);
    421 	/* V*(A2+V*(A4+V*A6)) */
    422 	d = fpu_mul(fe);
    423 	CPYFPN(&fe->fe_f1, d);
    424 	CPYFPN(&fe->fe_f2, &U);
    425 	/* U*V*(A2+V*(A4+V*A6)) */
    426 	d = fpu_mul(fe);
    427 	CPYFPN(&fe->fe_f1, d);
    428 	i++;
    429 	/* fe_f2 = logtbl[i+1] (== LOG(F)) */
    430 	fe->fe_f2.fp_class = FPC_NUM;
    431 	fe->fe_f2.fp_sign = fe->fe_f2.fp_sticky = fe->fe_f2.fp_mant[3] = 0;
    432 	fe->fe_f2.fp_exp = logtbl[i].sp_exp;
    433 	fe->fe_f2.fp_mant[0] = (logtbl[i].sp_m0 >> (31 - FP_LG));
    434 	fe->fe_f2.fp_mant[1] = (logtbl[i].sp_m0 << (FP_LG + 1)) |
    435 	    (logtbl[i].sp_m1 >> (31 - FP_LG));
    436 	fe->fe_f2.fp_mant[2] = (logtbl[i].sp_m1 << (FP_LG + 1));
    437 
    438 	if (fpu_debug_level & DL_ARITH)
    439 	    printf("__fpu_logn: ln(F)=(%d,%08x,%08x,...)\n", fe->fe_f2.fp_exp,
    440 		   fe->fe_f2.fp_mant[0], fe->fe_f2.fp_mant[1]);
    441 
    442 	/* LOG(F)+U*V*(A2+V*(A4+V*A6)) */
    443 	d = fpu_add(fe);
    444 	CPYFPN(&fe->fe_f1, d);
    445 	CPYFPN(&fe->fe_f2, &X);
    446 	/* LOG(F)+U+V*(A1+V*(A3+V*A5))+U*V*(A2+V*(A4+V*A6)) */
    447 	d = fpu_add(fe);
    448 
    449 	if (fpu_debug_level & DL_ARITH)
    450 	    printf("__fpu_logn: ln(Y)=(%c,%d,%08x,%08x,%08x,%08x)\n",
    451 		   d->fp_sign ? '-' : '+', d->fp_exp,
    452 		   d->fp_mant[0], d->fp_mant[1], d->fp_mant[2], d->fp_mant[3]);
    453 
    454 	CPYFPN(&fe->fe_f1, d);
    455 	CPYFPN(&fe->fe_f2, &KLOG2);
    456 	/* K*LOGOF2+LOG(F)+U+V*(A1+V*(A3+V*A5))+U*V*(A2+V*(A4+V*A6)) */
    457 	d = fpu_add(fe);
    458     }
    459 
    460     return d;
    461 }
    462 
    463 struct fpn *
    464 fpu_log10(fe)
    465      struct fpemu *fe;
    466 {
    467     struct fpn *fp = &fe->fe_f2;
    468     u_int fpsr;
    469 
    470     fpsr = fe->fe_fpsr & ~FPSR_EXCP;	/* clear all exceptions */
    471 
    472     if (fp->fp_class >= FPC_NUM) {
    473 	if (fp->fp_sign) {	/* negative number or Inf */
    474 	    fp = fpu_newnan(fe);
    475 	    fpsr |= FPSR_OPERR;
    476 	} else if (fp->fp_class == FPC_NUM) {
    477 	    /* the real work here */
    478 	    fp = __fpu_logn(fe);
    479 	    if (fp != &fe->fe_f1)
    480 		CPYFPN(&fe->fe_f1, fp);
    481 	    (void)fpu_const(&fe->fe_f2, 0x31 /* ln(10) */);
    482 	    fp = fpu_div(fe);
    483 	} /* else if fp == +Inf, return +Inf */
    484     } else if (fp->fp_class == FPC_ZERO) {
    485 	/* return -Inf */
    486 	fp->fp_class = FPC_INF;
    487 	fp->fp_sign = 1;
    488 	fpsr |= FPSR_DZ;
    489     } else if (fp->fp_class == FPC_SNAN) {
    490 	fpsr |= FPSR_SNAN;
    491 	fp = fpu_newnan(fe);
    492     } else {
    493 	fp = fpu_newnan(fe);
    494     }
    495 
    496     fe->fe_fpsr = fpsr;
    497 
    498     return fp;
    499 }
    500 
    501 struct fpn *
    502 fpu_log2(fe)
    503      struct fpemu *fe;
    504 {
    505     struct fpn *fp = &fe->fe_f2;
    506     u_int fpsr;
    507 
    508     fpsr = fe->fe_fpsr & ~FPSR_EXCP;	/* clear all exceptions */
    509 
    510     if (fp->fp_class >= FPC_NUM) {
    511 	if (fp->fp_sign) {	/* negative number or Inf */
    512 	    fp = fpu_newnan(fe);
    513 	    fpsr |= FPSR_OPERR;
    514 	} else if (fp->fp_class == FPC_NUM) {
    515 	    /* the real work here */
    516 	    if (fp->fp_mant[0] == FP_1 && fp->fp_mant[1] == 0 &&
    517 		fp->fp_mant[2] == 0 && fp->fp_mant[3] == 0) {
    518 		/* fp == 2.0 ^ exp <--> log2(fp) == exp */
    519 		fpu_explode(fe, &fe->fe_f3, FTYPE_LNG, &fp->fp_exp);
    520 		fp = &fe->fe_f3;
    521 	    } else {
    522 		fp = __fpu_logn(fe);
    523 		if (fp != &fe->fe_f1)
    524 		    CPYFPN(&fe->fe_f1, fp);
    525 		(void)fpu_const(&fe->fe_f2, 0x30 /* ln(2) */);
    526 		fp = fpu_div(fe);
    527 	    }
    528 	} /* else if fp == +Inf, return +Inf */
    529     } else if (fp->fp_class == FPC_ZERO) {
    530 	/* return -Inf */
    531 	fp->fp_class = FPC_INF;
    532 	fp->fp_sign = 1;
    533 	fpsr |= FPSR_DZ;
    534     } else if (fp->fp_class == FPC_SNAN) {
    535 	fpsr |= FPSR_SNAN;
    536 	fp = fpu_newnan(fe);
    537     } else {
    538 	fp = fpu_newnan(fe);
    539     }
    540 
    541     fe->fe_fpsr = fpsr;
    542     return fp;
    543 }
    544 
    545 struct fpn *
    546 fpu_logn(fe)
    547      struct fpemu *fe;
    548 {
    549     struct fpn *fp = &fe->fe_f2;
    550     u_int fpsr;
    551 
    552     fpsr = fe->fe_fpsr & ~FPSR_EXCP;	/* clear all exceptions */
    553 
    554     if (fp->fp_class >= FPC_NUM) {
    555 	if (fp->fp_sign) {	/* negative number or Inf */
    556 	    fp = fpu_newnan(fe);
    557 	    fpsr |= FPSR_OPERR;
    558 	} else if (fp->fp_class == FPC_NUM) {
    559 	    /* the real work here */
    560 	    fp = __fpu_logn(fe);
    561 	} /* else if fp == +Inf, return +Inf */
    562     } else if (fp->fp_class == FPC_ZERO) {
    563 	/* return -Inf */
    564 	fp->fp_class = FPC_INF;
    565 	fp->fp_sign = 1;
    566 	fpsr |= FPSR_DZ;
    567     } else if (fp->fp_class == FPC_SNAN) {
    568 	fpsr |= FPSR_SNAN;
    569 	fp = fpu_newnan(fe);
    570     } else {
    571 	fp = fpu_newnan(fe);
    572     }
    573 
    574     fe->fe_fpsr = fpsr;
    575 
    576     return fp;
    577 }
    578 
    579 struct fpn *
    580 fpu_lognp1(fe)
    581      struct fpemu *fe;
    582 {
    583     struct fpn *fp;
    584 
    585     /* build a 1.0 */
    586     fp = fpu_const(&fe->fe_f1, 0x32); /* get 1.0 */
    587     /* fp = 1.0 + f2 */
    588     fp = fpu_add(fe);
    589 
    590     /* copy the result to the src opr */
    591     if (&fe->fe_f2 != fp)
    592 	CPYFPN(&fe->fe_f2, fp);
    593 
    594     return fpu_logn(fe);
    595 }
    596