Merge from vendor branch GCC:
[dragonfly.git] / sys / i386 / gnu / fpemul / poly_sin.c
1 /*
2  *  poly_sin.c
3  *
4  *  Computation of an approximation of the sin function by a polynomial
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_sin.c,v 1.10 1999/08/28 00:42:54 peter Exp $
60  * $DragonFly: src/sys/i386/gnu/fpemul/Attic/poly_sin.c,v 1.3 2003/08/07 21:17:20 dillon Exp $
61  *
62  */
63
64 #ifdef DEBUG
65 #include <sys/types.h>
66 #include <sys/systm.h>          /* for printf() in EXCEPTION() */
67 #endif
68
69 #include "exception.h"
70 #include "reg_constant.h"
71 #include "control_w.h"
72
73
74 #define HIPOWER 5
75 static unsigned short lterms[HIPOWER][4] =
76 {
77         {0x846a, 0x42d1, 0xb544, 0x921f},
78         {0xe110, 0x75aa, 0xbc67, 0x1466},
79         {0x503d, 0xa43f, 0x83c1, 0x000a},
80         {0x8f9d, 0x7a19, 0x00f4, 0x0000},
81         {0xda03, 0x06aa, 0x0000, 0x0000},
82 };
83
84 static unsigned short negterms[HIPOWER][4] =
85 {
86         {0x95ed, 0x2df2, 0xe731, 0xa55d},
87         {0xd159, 0xe62b, 0xd2cc, 0x0132},
88         {0x6342, 0xe9fb, 0x3c60, 0x0000},
89         {0x6256, 0xdf5a, 0x0002, 0x0000},
90         {0xf279, 0x000b, 0x0000, 0x0000},
91 };
92
93
94 /*--- poly_sine() -----------------------------------------------------------+
95  |                                                                           |
96  +---------------------------------------------------------------------------*/
97 void
98 poly_sine(FPU_REG * arg, FPU_REG * result)
99 {
100         short   exponent;
101         FPU_REG Xx, Xx2, Xx4, accum, negaccum;
102
103
104         exponent = arg->exp - EXP_BIAS;
105
106         if (arg->tag == TW_Zero) {
107                 /* Return 0.0 */
108                 reg_move(&CONST_Z, result);
109                 return;
110         }
111 #ifdef PARANOID
112         if (arg->sign != 0) {   /* Can't hack a number < 0.0 */
113                 EXCEPTION(EX_Invalid);
114                 reg_move(&CONST_QNaN, result);
115                 return;
116         }
117         if (exponent >= 0) {    /* Can't hack a number > 1.0 */
118                 if ((exponent == 0) && (arg->sigl == 0) && (arg->sigh == 0x80000000)) {
119                         reg_move(&CONST_1, result);
120                         return;
121                 }
122                 EXCEPTION(EX_Invalid);
123                 reg_move(&CONST_QNaN, result);
124                 return;
125         }
126 #endif                          /* PARANOID */
127
128         Xx.sigl = arg->sigl;
129         Xx.sigh = arg->sigh;
130         if (exponent < -1) {
131                 /* shift the argument right by the required places */
132                 if (shrx(&(Xx.sigl), -1 - exponent) >= (unsigned)0x80000000)
133                         (*((long long *) (&(Xx.sigl))))++;      /* round up */
134         }
135         mul64((long long *) &(Xx.sigl), (long long *) &(Xx.sigl),
136             (long long *) &(Xx2.sigl));
137         mul64((long long *) &(Xx2.sigl), (long long *) &(Xx2.sigl),
138             (long long *) &(Xx4.sigl));
139
140         /* will be a valid positive nr with expon = 0 */
141         *(short *) &(accum.sign) = 0;
142         accum.exp = 0;
143
144         /* Do the basic fixed point polynomial evaluation */
145         polynomial((u_int *) &(accum.sigl), (u_int *)&(Xx4.sigl), lterms, HIPOWER - 1);
146
147         /* will be a valid positive nr with expon = 0 */
148         *(short *) &(negaccum.sign) = 0;
149         negaccum.exp = 0;
150
151         /* Do the basic fixed point polynomial evaluation */
152         polynomial((u_int *) &(negaccum.sigl), (u_int *)&(Xx4.sigl), negterms, HIPOWER - 1);
153         mul64((long long *) &(Xx2.sigl), (long long *) &(negaccum.sigl),
154             (long long *) &(negaccum.sigl));
155
156         /* Subtract the mantissas */
157         *((long long *) (&(accum.sigl))) -= *((long long *) (&(negaccum.sigl)));
158
159         /* Convert to 64 bit signed-compatible */
160         accum.exp = EXP_BIAS - 1 + accum.exp;
161
162         *(short *) &(result->sign) = *(short *) &(accum.sign);
163         result->exp = accum.exp;
164         result->sigl = accum.sigl;
165         result->sigh = accum.sigh;
166
167         normalize(result);
168
169         reg_mul(result, arg, result, FULL_PRECISION);
170         reg_u_add(result, arg, result, FULL_PRECISION);
171
172         /* A small overflow may be possible... but an illegal result. */
173         if (result->exp >= EXP_BIAS) {
174                 if ((result->exp > EXP_BIAS)    /* Larger or equal 2.0 */
175                     ||(result->sigl > 1)        /* Larger than 1.0+msb */
176                     ||(result->sigh != 0x80000000)      /* Much > 1.0 */
177                     ) {
178 #ifdef DEBUGGING
179                         RE_ENTRANT_CHECK_OFF
180                             printk("\nEXP=%d, MS=%08x, LS=%08x\n", result->exp,
181                             result->sigh, result->sigl);
182                         RE_ENTRANT_CHECK_ON
183 #endif                          /* DEBUGGING */
184                             EXCEPTION(EX_INTERNAL | 0x103);
185                 }
186 #ifdef DEBUGGING
187                 RE_ENTRANT_CHECK_OFF
188                     printk("\n***CORRECTING ILLEGAL RESULT*** in poly_sin() computation\n");
189                 printk("EXP=%d, MS=%08x, LS=%08x\n", result->exp,
190                     result->sigh, result->sigl);
191                 RE_ENTRANT_CHECK_ON
192 #endif                          /* DEBUGGING */
193
194                     result->sigl = 0;   /* Truncate the result to 1.00 */
195         }
196 }