Correct a bug in the last FPU optimized bcopy commit. The user FPU state
[dragonfly.git] / sys / i386 / gnu / fpemul / reg_compare.c
1 /*
2  *  reg_compare.c
3  *
4  * Compare two floating point registers
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/reg_compare.c,v 1.11 1999/08/28 00:42:55 peter Exp $
60  * $DragonFly: src/sys/i386/gnu/fpemul/Attic/reg_compare.c,v 1.3 2003/08/07 21:17:20 dillon Exp $
61  *
62  */
63
64 /*---------------------------------------------------------------------------+
65  | compare() is the core FPU_REG comparison function                         |
66  +---------------------------------------------------------------------------*/
67 #include <sys/param.h>
68 #include <sys/proc.h>
69 #include <sys/systm.h>
70 #include <machine/pcb.h>
71
72 #include "fpu_emu.h"
73 #include "fpu_system.h"
74 #include "exception.h"
75 #include "control_w.h"
76 #include "status_w.h"
77
78
79 int
80 compare(FPU_REG * b)
81 {
82         int     diff;
83
84         if (FPU_st0_ptr->tag | b->tag) {
85                 if (FPU_st0_ptr->tag == TW_Zero) {
86                         if (b->tag == TW_Zero)
87                                 return COMP_A_eq_B;
88                         if (b->tag == TW_Valid) {
89 #ifdef DENORM_OPERAND
90                                 if ((b->exp <= EXP_UNDER) && (denormal_operand()))
91                                         return COMP_Denormal;
92 #endif                          /* DENORM_OPERAND */
93                                 return (b->sign == SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B;
94                         }
95                 } else
96                         if (b->tag == TW_Zero) {
97                                 if (FPU_st0_ptr->tag == TW_Valid) {
98 #ifdef DENORM_OPERAND
99                                         if ((FPU_st0_ptr->exp <= EXP_UNDER) && (denormal_operand()))
100                                                 return COMP_Denormal;
101 #endif                          /* DENORM_OPERAND */
102                                         return (FPU_st0_ptr->sign == SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B;
103                                 }
104                         }
105                 if (FPU_st0_ptr->tag == TW_Infinity) {
106                         if ((b->tag == TW_Valid) || (b->tag == TW_Zero)) {
107 #ifdef DENORM_OPERAND
108                                 if ((b->tag == TW_Valid) && (b->exp <= EXP_UNDER)
109                                     && (denormal_operand()))
110                                         return COMP_Denormal;
111 #endif                          /* DENORM_OPERAND */
112                                 return (FPU_st0_ptr->sign == SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B;
113                         } else
114                                 if (b->tag == TW_Infinity) {
115                                         /* The 80486 book says that infinities
116                                          * can be equal! */
117                                         return (FPU_st0_ptr->sign == b->sign) ? COMP_A_eq_B :
118                                             ((FPU_st0_ptr->sign == SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B);
119                                 }
120                         /* Fall through to the NaN code */
121                 } else
122                         if (b->tag == TW_Infinity) {
123                                 if ((FPU_st0_ptr->tag == TW_Valid) || (FPU_st0_ptr->tag == TW_Zero)) {
124 #ifdef DENORM_OPERAND
125                                         if ((FPU_st0_ptr->tag == TW_Valid)
126                                             && (FPU_st0_ptr->exp <= EXP_UNDER)
127                                             && (denormal_operand()))
128                                                 return COMP_Denormal;
129 #endif                          /* DENORM_OPERAND */
130                                         return (b->sign == SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B;
131                                 }
132                                 /* Fall through to the NaN code */
133                         }
134                 /* The only possibility now should be that one of the
135                  * arguments is a NaN */
136                 if ((FPU_st0_ptr->tag == TW_NaN) || (b->tag == TW_NaN)) {
137                         if (((FPU_st0_ptr->tag == TW_NaN) && !(FPU_st0_ptr->sigh & 0x40000000))
138                             || ((b->tag == TW_NaN) && !(b->sigh & 0x40000000)))
139                                 /* At least one arg is a signaling NaN */
140                                 return COMP_No_Comp | COMP_SNaN | COMP_NaN;
141                         else
142                                 /* Neither is a signaling NaN */
143                                 return COMP_No_Comp | COMP_NaN;
144                 }
145                 EXCEPTION(EX_Invalid);
146         }
147 #ifdef PARANOID
148         if (!(FPU_st0_ptr->sigh & 0x80000000))
149                 EXCEPTION(EX_Invalid);
150         if (!(b->sigh & 0x80000000))
151                 EXCEPTION(EX_Invalid);
152 #endif                          /* PARANOID */
153
154 #ifdef DENORM_OPERAND
155         if (((FPU_st0_ptr->exp <= EXP_UNDER) ||
156                 (b->exp <= EXP_UNDER)) && (denormal_operand()))
157                 return COMP_Denormal;
158 #endif                          /* DENORM_OPERAND */
159
160         if (FPU_st0_ptr->sign != b->sign)
161                 return (FPU_st0_ptr->sign == SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B;
162
163         diff = FPU_st0_ptr->exp - b->exp;
164         if (diff == 0) {
165                 diff = FPU_st0_ptr->sigh - b->sigh;     /* Works only if ms bits
166                                                          * are identical */
167                 if (diff == 0) {
168                         diff = FPU_st0_ptr->sigl > b->sigl;
169                         if (diff == 0)
170                                 diff = -(FPU_st0_ptr->sigl < b->sigl);
171                 }
172         }
173         if (diff > 0)
174                 return (FPU_st0_ptr->sign == SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B;
175         if (diff < 0)
176                 return (FPU_st0_ptr->sign == SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B;
177         return COMP_A_eq_B;
178
179 }
180
181
182 /* This function requires that st(0) is not empty */
183 int
184 compare_st_data(void)
185 {
186         int     f = 0, c;
187
188         c = compare(&FPU_loaded_data);
189
190         if (c & (COMP_NaN | COMP_Denormal)) {
191                 if (c & COMP_NaN) {
192                         EXCEPTION(EX_Invalid);
193                         f = SW_C3 | SW_C2 | SW_C0;
194                 } else {
195                         /* One of the operands is a de-normal */
196                         return 0;
197                 }
198         } else
199                 switch (c) {
200                 case COMP_A_lt_B:
201                         f = SW_C0;
202                         break;
203                 case COMP_A_eq_B:
204                         f = SW_C3;
205                         break;
206                 case COMP_A_gt_B:
207                         f = 0;
208                         break;
209                 case COMP_No_Comp:
210                         f = SW_C3 | SW_C2 | SW_C0;
211                         break;
212 #ifdef PARANOID
213                 default:
214                         EXCEPTION(EX_INTERNAL | 0x121);
215                         f = SW_C3 | SW_C2 | SW_C0;
216                         break;
217 #endif                          /* PARANOID */
218                 }
219         setcc(f);
220         return 1;
221 }
222
223
224 static int
225 compare_st_st(int nr)
226 {
227         int     f = 0, c;
228
229         if (!NOT_EMPTY_0 || !NOT_EMPTY(nr)) {
230                 setcc(SW_C3 | SW_C2 | SW_C0);
231                 /* Stack fault */
232                 EXCEPTION(EX_StackUnder);
233                 return control_word & CW_Invalid;
234         }
235         c = compare(&st(nr));
236         if (c & (COMP_NaN | COMP_Denormal)) {
237                 if (c & COMP_NaN) {
238                         setcc(SW_C3 | SW_C2 | SW_C0);
239                         EXCEPTION(EX_Invalid);
240                         return control_word & CW_Invalid;
241                 } else {
242                         /* One of the operands is a de-normal */
243                         return control_word & CW_Denormal;
244                 }
245         } else
246                 switch (c) {
247                 case COMP_A_lt_B:
248                         f = SW_C0;
249                         break;
250                 case COMP_A_eq_B:
251                         f = SW_C3;
252                         break;
253                 case COMP_A_gt_B:
254                         f = 0;
255                         break;
256                 case COMP_No_Comp:
257                         f = SW_C3 | SW_C2 | SW_C0;
258                         break;
259 #ifdef PARANOID
260                 default:
261                         EXCEPTION(EX_INTERNAL | 0x122);
262                         f = SW_C3 | SW_C2 | SW_C0;
263                         break;
264 #endif                          /* PARANOID */
265                 }
266         setcc(f);
267         return 1;
268 }
269
270
271 static int
272 compare_u_st_st(int nr)
273 {
274         int     f = 0, c;
275
276         if (!NOT_EMPTY_0 || !NOT_EMPTY(nr)) {
277                 setcc(SW_C3 | SW_C2 | SW_C0);
278                 /* Stack fault */
279                 EXCEPTION(EX_StackUnder);
280                 return control_word & CW_Invalid;
281         }
282         c = compare(&st(nr));
283         if (c & (COMP_NaN | COMP_Denormal)) {
284                 if (c & COMP_NaN) {
285                         setcc(SW_C3 | SW_C2 | SW_C0);
286                         if (c & COMP_SNaN) {    /* This is the only difference
287                                                  * between un-ordered and
288                                                  * ordinary comparisons */
289                                 EXCEPTION(EX_Invalid);
290                                 return control_word & CW_Invalid;
291                         }
292                         return 1;
293                 } else {
294                         /* One of the operands is a de-normal */
295                         return control_word & CW_Denormal;
296                 }
297         } else
298                 switch (c) {
299                 case COMP_A_lt_B:
300                         f = SW_C0;
301                         break;
302                 case COMP_A_eq_B:
303                         f = SW_C3;
304                         break;
305                 case COMP_A_gt_B:
306                         f = 0;
307                         break;
308                 case COMP_No_Comp:
309                         f = SW_C3 | SW_C2 | SW_C0;
310                         break;
311 #ifdef PARANOID
312                 default:
313                         EXCEPTION(EX_INTERNAL | 0x123);
314                         f = SW_C3 | SW_C2 | SW_C0;
315                         break;
316 #endif                          /* PARANOID */
317                 }
318         setcc(f);
319         return 1;
320 }
321 /*---------------------------------------------------------------------------*/
322
323 void
324 fcom_st()
325 {
326         /* fcom st(i) */
327         compare_st_st(FPU_rm);
328 }
329
330
331 void
332 fcompst()
333 {
334         /* fcomp st(i) */
335         if (compare_st_st(FPU_rm))
336                 pop();
337 }
338
339
340 void
341 fcompp()
342 {
343         /* fcompp */
344         if (FPU_rm != 1) {
345                 Un_impl();
346                 return;
347         }
348         if (compare_st_st(1)) {
349                 pop();
350                 FPU_st0_ptr = &st(0);
351                 pop();
352         }
353 }
354
355
356 void
357 fucom_()
358 {
359         /* fucom st(i) */
360         compare_u_st_st(FPU_rm);
361
362 }
363
364
365 void
366 fucomp()
367 {
368         /* fucomp st(i) */
369         if (compare_u_st_st(FPU_rm))
370                 pop();
371 }
372
373
374 void
375 fucompp()
376 {
377         /* fucompp */
378         if (FPU_rm == 1) {
379                 if (compare_u_st_st(1)) {
380                         pop();
381                         FPU_st0_ptr = &st(0);
382                         pop();
383                 }
384         } else
385                 Un_impl();
386 }