Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / i386 / gnu / fpemul / poly_2xm1.c
1 /*
2  *  poly_2xm1.c
3  *
4  * Function to compute 2^x-1 by a polynomial approximation.
5  *
6  *
7  * Copyright (C) 1992,1993,1994
8  *                       W. Metzenthen, 22 Parker St, Ormond, Vic 3163,
9  *                       Australia.  E-mail   billm@vaxc.cc.monash.edu.au
10  * All rights reserved.
11  *
12  * This copyright notice covers the redistribution and use of the
13  * FPU emulator developed by W. Metzenthen. It covers only its use
14  * in the 386BSD, FreeBSD and NetBSD operating systems. Any other
15  * use is not permitted under this copyright.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must include information specifying
23  *    that source code for the emulator is freely available and include
24  *    either:
25  *      a) an offer to provide the source code for a nominal distribution
26  *         fee, or
27  *      b) list at least two alternative methods whereby the source
28  *         can be obtained, e.g. a publically accessible bulletin board
29  *         and an anonymous ftp site from which the software can be
30  *         downloaded.
31  * 3. All advertising materials specifically mentioning features or use of
32  *    this emulator must acknowledge that it was developed by W. Metzenthen.
33  * 4. The name of W. Metzenthen may not be used to endorse or promote
34  *    products derived from this software without specific prior written
35  *    permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
38  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
39  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
40  * W. METZENTHEN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
41  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
42  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
43  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
44  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
45  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  *
48  *
49  * The purpose of this copyright, based upon the Berkeley copyright, is to
50  * ensure that the covered software remains freely available to everyone.
51  *
52  * The software (with necessary differences) is also available, but under
53  * the terms of the GNU copyleft, for the Linux operating system and for
54  * the djgpp ms-dos extender.
55  *
56  * W. Metzenthen   June 1994.
57  *
58  *
59  * $FreeBSD: src/sys/gnu/i386/fpemul/poly_2xm1.c,v 1.8 1999/08/28 00:42:53 peter Exp $
60  *
61  */
62
63 #include <gnu/i386/fpemul/reg_constant.h>
64
65
66
67 #define HIPOWER 13
68 static unsigned short lterms[HIPOWER][4] =
69 {
70         {0x79b5, 0xd1cf, 0x17f7, 0xb172},
71         {0x1b56, 0x058b, 0x7bff, 0x3d7f},
72         {0x8bb0, 0x8250, 0x846b, 0x0e35},
73         {0xbc65, 0xf747, 0x556d, 0x0276},
74         {0x17cb, 0x9e39, 0x61ff, 0x0057},
75         {0xe018, 0x9776, 0x1848, 0x000a},
76         {0x66f2, 0xff30, 0xffe5, 0x0000},
77         {0x682f, 0xffb6, 0x162b, 0x0000},
78         {0xb7ca, 0x2956, 0x01b5, 0x0000},
79         {0xcd3e, 0x4817, 0x001e, 0x0000},
80         {0xb7e2, 0xecbe, 0x0001, 0x0000},
81         {0x0ed5, 0x1a27, 0x0000, 0x0000},
82         {0x101d, 0x0222, 0x0000, 0x0000},
83 };
84
85
86 /*--- poly_2xm1() -----------------------------------------------------------+
87  |                                                                           |
88  +---------------------------------------------------------------------------*/
89 int
90 poly_2xm1(FPU_REG * arg, FPU_REG * result)
91 {
92         short   exponent;
93         long long Xll;
94         FPU_REG accum;
95
96
97         exponent = arg->exp - EXP_BIAS;
98
99         if (arg->tag == TW_Zero) {
100                 /* Return 0.0 */
101                 reg_move(&CONST_Z, result);
102                 return 0;
103         }
104         if (exponent >= 0) {    /* Can't hack a number >= 1.0 */
105                 arith_invalid(result);  /* Number too large */
106                 return 1;
107         }
108         if (arg->sign != SIGN_POS) {    /* Can't hack a number < 0.0 */
109                 arith_invalid(result);  /* Number negative */
110                 return 1;
111         }
112         if (exponent < -64) {
113                 reg_move(&CONST_LN2, result);
114                 return 0;
115         }
116         *(unsigned *) &Xll = arg->sigl;
117         *(((unsigned *) &Xll) + 1) = arg->sigh;
118         if (exponent < -1) {
119                 /* shift the argument right by the required places */
120                 if (shrx(&Xll, -1 - exponent) >= (unsigned)0x80000000)
121                         Xll++;  /* round up */
122         }
123         *(short *) &(accum.sign) = 0;   /* will be a valid positive nr with
124                                          * expon = 0 */
125         accum.exp = 0;
126
127         /* Do the basic fixed point polynomial evaluation */
128         polynomial((unsigned *) &accum.sigl, (unsigned *) &Xll, lterms, HIPOWER - 1);
129
130         /* Convert to 64 bit signed-compatible */
131         accum.exp += EXP_BIAS - 1;
132
133         reg_move(&accum, result);
134
135         normalize(result);
136
137         return 0;
138
139 }