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