kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / i386 / gnu / fpemul / fpu_etc.c
1 /*
2  *  fpu_etc.c
3  *
4  * Implement a few FPU instructions.
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/fpu_etc.c,v 1.8 1999/08/28 00:42:51 peter Exp $
60  * $DragonFly: src/sys/i386/gnu/fpemul/Attic/fpu_etc.c,v 1.3 2003/08/07 21:17:20 dillon Exp $
61  *
62  */
63
64 #include <sys/param.h>
65 #ifdef DEBUG
66 #include <sys/systm.h>          /* for printf() in EXCEPTION() */
67 #endif
68 #include <sys/proc.h>
69 #include <machine/pcb.h>
70
71 #include "fpu_emu.h"
72 #include "fpu_system.h"
73 #include "exception.h"
74 #include "status_w.h"
75
76
77 static void
78 fchs(void)
79 {
80         if (NOT_EMPTY_0) {
81                 FPU_st0_ptr->sign ^= SIGN_POS ^ SIGN_NEG;
82                 status_word &= ~SW_C1;
83         } else
84                 stack_underflow();
85 }
86
87 static void
88 fabs(void)
89 {
90         if (FPU_st0_tag ^ TW_Empty) {
91                 FPU_st0_ptr->sign = SIGN_POS;
92                 status_word &= ~SW_C1;
93         } else
94                 stack_underflow();
95 }
96
97
98 static void
99 ftst_(void)
100 {
101         switch (FPU_st0_tag) {
102                 case TW_Zero:
103                 setcc(SW_C3);
104                 break;
105         case TW_Valid:
106
107 #ifdef DENORM_OPERAND
108                 if ((FPU_st0_ptr->exp <= EXP_UNDER) && (denormal_operand()))
109                         return;
110 #endif                          /* DENORM_OPERAND */
111
112                 if (FPU_st0_ptr->sign == SIGN_POS)
113                         setcc(0);
114                 else
115                         setcc(SW_C0);
116                 break;
117         case TW_NaN:
118                 setcc(SW_C0 | SW_C2 | SW_C3);   /* Operand is not comparable */
119                 EXCEPTION(EX_Invalid);
120                 break;
121         case TW_Infinity:
122                 if (FPU_st0_ptr->sign == SIGN_POS)
123                         setcc(0);
124                 else
125                         setcc(SW_C0);
126                 EXCEPTION(EX_Invalid);
127                 break;
128         case TW_Empty:
129                 setcc(SW_C0 | SW_C2 | SW_C3);
130                 EXCEPTION(EX_StackUnder);
131                 break;
132         default:
133                 setcc(SW_C0 | SW_C2 | SW_C3);   /* Operand is not comparable */
134                 EXCEPTION(EX_INTERNAL | 0x14);
135                 break;
136         }
137 }
138
139 static void
140 fxam(void)
141 {
142         int     c = 0;
143         switch (FPU_st0_tag) {
144         case TW_Empty:
145                 c = SW_C3 | SW_C0;
146                 break;
147         case TW_Zero:
148                 c = SW_C3;
149                 break;
150         case TW_Valid:
151                 /* This will need to be changed if TW_Denormal is ever used. */
152                 if (FPU_st0_ptr->exp <= EXP_UNDER)
153                         c = SW_C2 | SW_C3;      /* Denormal */
154                 else
155                         c = SW_C3;
156                 break;
157         case TW_NaN:
158                 c = SW_C0;
159                 break;
160         case TW_Infinity:
161                 c = SW_C2 | SW_C0;
162                 break;
163         }
164         if (FPU_st0_ptr->sign == SIGN_NEG)
165                 c |= SW_C1;
166         setcc(c);
167 }
168
169 static FUNC fp_etc_table[] = {
170         fchs, fabs, Un_impl, Un_impl, ftst_, fxam, Un_impl, Un_impl
171 };
172
173 void
174 fp_etc()
175 {
176         (fp_etc_table[FPU_rm]) ();
177 }