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