4 * Computation of an approximation of the sin function by a polynomial
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_sin.c,v 1.10 1999/08/28 00:42:54 peter Exp $
60 * $DragonFly: src/sys/platform/pc32/gnu/fpemul/Attic/poly_sin.c,v 1.3 2003/08/07 21:17:20 dillon Exp $
65 #include <sys/types.h>
66 #include <sys/systm.h> /* for printf() in EXCEPTION() */
69 #include "exception.h"
70 #include "reg_constant.h"
71 #include "control_w.h"
75 static unsigned short lterms[HIPOWER][4] =
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},
84 static unsigned short negterms[HIPOWER][4] =
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},
94 /*--- poly_sine() -----------------------------------------------------------+
96 +---------------------------------------------------------------------------*/
98 poly_sine(FPU_REG * arg, FPU_REG * result)
101 FPU_REG Xx, Xx2, Xx4, accum, negaccum;
104 exponent = arg->exp - EXP_BIAS;
106 if (arg->tag == TW_Zero) {
108 reg_move(&CONST_Z, result);
112 if (arg->sign != 0) { /* Can't hack a number < 0.0 */
113 EXCEPTION(EX_Invalid);
114 reg_move(&CONST_QNaN, result);
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);
122 EXCEPTION(EX_Invalid);
123 reg_move(&CONST_QNaN, result);
126 #endif /* PARANOID */
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 */
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));
140 /* will be a valid positive nr with expon = 0 */
141 *(short *) &(accum.sign) = 0;
144 /* Do the basic fixed point polynomial evaluation */
145 polynomial((u_int *) &(accum.sigl), (u_int *)&(Xx4.sigl), lterms, HIPOWER - 1);
147 /* will be a valid positive nr with expon = 0 */
148 *(short *) &(negaccum.sign) = 0;
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));
156 /* Subtract the mantissas */
157 *((long long *) (&(accum.sigl))) -= *((long long *) (&(negaccum.sigl)));
159 /* Convert to 64 bit signed-compatible */
160 accum.exp = EXP_BIAS - 1 + accum.exp;
162 *(short *) &(result->sign) = *(short *) &(accum.sign);
163 result->exp = accum.exp;
164 result->sigl = accum.sigl;
165 result->sigh = accum.sigh;
169 reg_mul(result, arg, result, FULL_PRECISION);
170 reg_u_add(result, arg, result, FULL_PRECISION);
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 */
180 printk("\nEXP=%d, MS=%08x, LS=%08x\n", result->exp,
181 result->sigh, result->sigl);
183 #endif /* DEBUGGING */
184 EXCEPTION(EX_INTERNAL | 0x103);
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);
192 #endif /* DEBUGGING */
194 result->sigl = 0; /* Truncate the result to 1.00 */