4 * Compute the tan of a FPU_REG, using a polynomial approximation.
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.
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.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
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
25 * a) an offer to provide the source code for a nominal distribution
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
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
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.
49 * The purpose of this copyright, based upon the Berkeley copyright, is to
50 * ensure that the covered software remains freely available to everyone.
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.
56 * W. Metzenthen June 1994.
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 $
64 #include "reg_constant.h"
65 #include "control_w.h"
68 #define HIPOWERop 3 /* odd poly, positive terms */
69 static unsigned short oddplterms[HIPOWERop][4] =
71 {0x846a, 0x42d1, 0xb544, 0x921f},
72 {0x6fb2, 0x0215, 0x95c0, 0x099c},
73 {0xfce6, 0x0cc8, 0x1c9a, 0x0000}
75 #define HIPOWERon 2 /* odd poly, negative terms */
76 static unsigned short oddnegterms[HIPOWERon][4] =
78 {0x6906, 0xe205, 0x25c8, 0x8838},
79 {0x1dd7, 0x3fe3, 0x944e, 0x002c}
81 #define HIPOWERep 2 /* even poly, positive terms */
82 static unsigned short evenplterms[HIPOWERep][4] =
84 {0xdb8f, 0x3761, 0x1432, 0x2acf},
85 {0x16eb, 0x13c1, 0x3099, 0x0003}
87 #define HIPOWERen 2 /* even poly, negative terms */
88 static unsigned short evennegterms[HIPOWERen][4] =
90 {0x3a7c, 0xe4c5, 0x7f87, 0x2945},
91 {0x572b, 0x664c, 0xc543, 0x018c}
95 /*--- poly_tan() ------------------------------------------------------------+
97 +---------------------------------------------------------------------------*/
99 poly_tan(FPU_REG * arg, FPU_REG * y_reg)
103 FPU_REG odd_poly, even_poly, pos_poly, neg_poly;
105 long long arg_signif, argSqSq;
108 exponent = arg->exp - EXP_BIAS;
110 if (arg->tag == TW_Zero) {
112 reg_move(&CONST_Z, y_reg);
115 if (exponent >= -1) {
116 /* argument is in the range [0.5 .. 1.0] */
119 if ((exponent == 0) &&
120 (arg->sigl == 0) && (arg->sigh == 0x80000000))
121 #endif /* PARANOID */
123 arith_overflow(y_reg);
127 EXCEPTION(EX_INTERNAL | 0x104); /* There must be a logic
130 #endif /* PARANOID */
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;
140 if (arg->sign != 0) { /* Can't hack a number < 0.0 */
141 arith_invalid(y_reg);
143 } /* Need a positive number */
144 #endif /* PARANOID */
146 *(long long *) &arg_signif = *(long long *) &(arg->sigl);
148 /* shift the argument right by the required places */
149 if (shrx(&arg_signif, -1 - exponent) >= (unsigned)0x80000000)
150 arg_signif++; /* round up */
152 mul64(&arg_signif, &arg_signif, (long long *) (&argSq.sigl));
153 mul64((long long *) (&argSq.sigl), (long long *) (&argSq.sigl), &argSqSq);
155 /* will be a valid positive nr with expon = 0 */
156 *(short *) &(pos_poly.sign) = 0;
157 pos_poly.exp = EXP_BIAS;
159 /* Do the basic fixed point polynomial evaluation */
160 polynomial((u_int *) &pos_poly.sigl, (unsigned *) &argSqSq, oddplterms, HIPOWERop - 1);
162 /* will be a valid positive nr with expon = 0 */
163 *(short *) &(neg_poly.sign) = 0;
164 neg_poly.exp = EXP_BIAS;
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));
171 /* Subtract the mantissas */
172 *((long long *) (&pos_poly.sigl)) -= *((long long *) (&neg_poly.sigl));
174 /* Convert to 64 bit signed-compatible */
177 reg_move(&pos_poly, &odd_poly);
178 normalize(&odd_poly);
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
185 /* will be a valid positive nr with expon = 0 */
186 *(short *) &(pos_poly.sign) = 0;
187 pos_poly.exp = EXP_BIAS;
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));
194 /* will be a valid positive nr with expon = 0 */
195 *(short *) &(neg_poly.sign) = 0;
196 neg_poly.exp = EXP_BIAS;
198 /* Do the basic fixed point polynomial evaluation */
199 polynomial((u_int *) &neg_poly.sigl, (unsigned *) &argSqSq, evennegterms, HIPOWERen - 1);
201 /* Subtract the mantissas */
202 *((long long *) (&neg_poly.sigl)) -= *((long long *) (&pos_poly.sigl));
203 /* and multiply by argSq */
205 /* Convert argSq to a valid reg number */
206 *(short *) &(argSq.sign) = 0;
207 argSq.exp = EXP_BIAS - 1;
210 /* Convert to 64 bit signed-compatible */
213 reg_move(&neg_poly, &even_poly);
214 normalize(&even_poly);
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
221 /* Now ready to copy the results */
223 reg_div(&even_poly, &odd_poly, y_reg, FULL_PRECISION);
225 reg_div(&odd_poly, &even_poly, y_reg, FULL_PRECISION);