kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / i386 / gnu / fpemul / poly_tan.c
1 /*
2  *  poly_tan.c
3  *
4  * Compute the tan of a FPU_REG, using 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_tan.c,v 1.10 1999/08/28 00:42:54 peter Exp $
60  * $DragonFly: src/sys/i386/gnu/fpemul/Attic/poly_tan.c,v 1.3 2003/08/07 21:17:20 dillon Exp $
61  *
62  */
63
64 #include "reg_constant.h"
65 #include "control_w.h"
66
67
68 #define HIPOWERop       3       /* odd poly, positive terms */
69 static unsigned short oddplterms[HIPOWERop][4] =
70 {
71         {0x846a, 0x42d1, 0xb544, 0x921f},
72         {0x6fb2, 0x0215, 0x95c0, 0x099c},
73         {0xfce6, 0x0cc8, 0x1c9a, 0x0000}
74 };
75 #define HIPOWERon       2       /* odd poly, negative terms */
76 static unsigned short oddnegterms[HIPOWERon][4] =
77 {
78         {0x6906, 0xe205, 0x25c8, 0x8838},
79         {0x1dd7, 0x3fe3, 0x944e, 0x002c}
80 };
81 #define HIPOWERep       2       /* even poly, positive terms */
82 static unsigned short evenplterms[HIPOWERep][4] =
83 {
84         {0xdb8f, 0x3761, 0x1432, 0x2acf},
85         {0x16eb, 0x13c1, 0x3099, 0x0003}
86 };
87 #define HIPOWERen       2       /* even poly, negative terms */
88 static unsigned short evennegterms[HIPOWERen][4] =
89 {
90         {0x3a7c, 0xe4c5, 0x7f87, 0x2945},
91         {0x572b, 0x664c, 0xc543, 0x018c}
92 };
93
94
95 /*--- poly_tan() ------------------------------------------------------------+
96  |                                                                           |
97  +---------------------------------------------------------------------------*/
98 void
99 poly_tan(FPU_REG * arg, FPU_REG * y_reg)
100 {
101         char    invert = 0;
102         short   exponent;
103         FPU_REG odd_poly, even_poly, pos_poly, neg_poly;
104         FPU_REG argSq;
105         long long arg_signif, argSqSq;
106
107
108         exponent = arg->exp - EXP_BIAS;
109
110         if (arg->tag == TW_Zero) {
111                 /* Return 0.0 */
112                 reg_move(&CONST_Z, y_reg);
113                 return;
114         }
115         if (exponent >= -1) {
116                 /* argument is in the range  [0.5 .. 1.0] */
117                 if (exponent >= 0) {
118 #ifdef PARANOID
119                         if ((exponent == 0) &&
120                             (arg->sigl == 0) && (arg->sigh == 0x80000000))
121 #endif                          /* PARANOID */
122                         {
123                                 arith_overflow(y_reg);
124                                 return;
125                         }
126 #ifdef PARANOID
127                         EXCEPTION(EX_INTERNAL | 0x104); /* There must be a logic
128                                                          * error */
129                         return;
130 #endif                          /* PARANOID */
131                 }
132                 /* The argument is in the range  [0.5 .. 1.0) */
133                 /* Convert the argument to a number in the range  (0.0 .. 0.5] */
134                 *((long long *) (&arg->sigl)) = -*((long long *) (&arg->sigl));
135                 normalize(arg); /* Needed later */
136                 exponent = arg->exp - EXP_BIAS;
137                 invert = 1;
138         }
139 #ifdef PARANOID
140         if (arg->sign != 0) {   /* Can't hack a number < 0.0 */
141                 arith_invalid(y_reg);
142                 return;
143         }                       /* Need a positive number */
144 #endif                          /* PARANOID */
145
146         *(long long *) &arg_signif = *(long long *) &(arg->sigl);
147         if (exponent < -1) {
148                 /* shift the argument right by the required places */
149                 if (shrx(&arg_signif, -1 - exponent) >= (unsigned)0x80000000)
150                         arg_signif++;   /* round up */
151         }
152         mul64(&arg_signif, &arg_signif, (long long *) (&argSq.sigl));
153         mul64((long long *) (&argSq.sigl), (long long *) (&argSq.sigl), &argSqSq);
154
155         /* will be a valid positive nr with expon = 0 */
156         *(short *) &(pos_poly.sign) = 0;
157         pos_poly.exp = EXP_BIAS;
158
159         /* Do the basic fixed point polynomial evaluation */
160         polynomial((u_int *) &pos_poly.sigl, (unsigned *) &argSqSq, oddplterms, HIPOWERop - 1);
161
162         /* will be a valid positive nr with expon = 0 */
163         *(short *) &(neg_poly.sign) = 0;
164         neg_poly.exp = EXP_BIAS;
165
166         /* Do the basic fixed point polynomial evaluation */
167         polynomial((u_int *) &neg_poly.sigl, (unsigned *) &argSqSq, oddnegterms, HIPOWERon - 1);
168         mul64((long long *) (&argSq.sigl), (long long *) (&neg_poly.sigl),
169             (long long *) (&neg_poly.sigl));
170
171         /* Subtract the mantissas */
172         *((long long *) (&pos_poly.sigl)) -= *((long long *) (&neg_poly.sigl));
173
174         /* Convert to 64 bit signed-compatible */
175         pos_poly.exp -= 1;
176
177         reg_move(&pos_poly, &odd_poly);
178         normalize(&odd_poly);
179
180         reg_mul(&odd_poly, arg, &odd_poly, FULL_PRECISION);
181         reg_u_add(&odd_poly, arg, &odd_poly, FULL_PRECISION);   /* This is just the odd
182                                                                  * polynomial */
183
184
185         /* will be a valid positive nr with expon = 0 */
186         *(short *) &(pos_poly.sign) = 0;
187         pos_poly.exp = EXP_BIAS;
188
189         /* Do the basic fixed point polynomial evaluation */
190         polynomial((u_int *) &pos_poly.sigl, (unsigned *) &argSqSq, evenplterms, HIPOWERep - 1);
191         mul64((long long *) (&argSq.sigl),
192             (long long *) (&pos_poly.sigl), (long long *) (&pos_poly.sigl));
193
194         /* will be a valid positive nr with expon = 0 */
195         *(short *) &(neg_poly.sign) = 0;
196         neg_poly.exp = EXP_BIAS;
197
198         /* Do the basic fixed point polynomial evaluation */
199         polynomial((u_int *) &neg_poly.sigl, (unsigned *) &argSqSq, evennegterms, HIPOWERen - 1);
200
201         /* Subtract the mantissas */
202         *((long long *) (&neg_poly.sigl)) -= *((long long *) (&pos_poly.sigl));
203         /* and multiply by argSq */
204
205         /* Convert argSq to a valid reg number */
206         *(short *) &(argSq.sign) = 0;
207         argSq.exp = EXP_BIAS - 1;
208         normalize(&argSq);
209
210         /* Convert to 64 bit signed-compatible */
211         neg_poly.exp -= 1;
212
213         reg_move(&neg_poly, &even_poly);
214         normalize(&even_poly);
215
216         reg_mul(&even_poly, &argSq, &even_poly, FULL_PRECISION);
217         reg_add(&even_poly, &argSq, &even_poly, FULL_PRECISION);
218         reg_sub(&CONST_1, &even_poly, &even_poly, FULL_PRECISION);      /* This is just the even
219                                                                          * polynomial */
220
221         /* Now ready to copy the results */
222         if (invert) {
223                 reg_div(&even_poly, &odd_poly, y_reg, FULL_PRECISION);
224         } else {
225                 reg_div(&odd_poly, &even_poly, y_reg, FULL_PRECISION);
226         }
227
228 }