4 * Compare two floating point registers
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/reg_compare.c,v 1.11 1999/08/28 00:42:55 peter Exp $
63 /*---------------------------------------------------------------------------+
64 | compare() is the core FPU_REG comparison function |
65 +---------------------------------------------------------------------------*/
66 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <machine/pcb.h>
71 #include <gnu/i386/fpemul/fpu_emu.h>
72 #include <gnu/i386/fpemul/fpu_system.h>
73 #include <gnu/i386/fpemul/exception.h>
74 #include <gnu/i386/fpemul/control_w.h>
75 #include <gnu/i386/fpemul/status_w.h>
83 if (FPU_st0_ptr->tag | b->tag) {
84 if (FPU_st0_ptr->tag == TW_Zero) {
85 if (b->tag == TW_Zero)
87 if (b->tag == TW_Valid) {
89 if ((b->exp <= EXP_UNDER) && (denormal_operand()))
91 #endif /* DENORM_OPERAND */
92 return (b->sign == SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B;
95 if (b->tag == TW_Zero) {
96 if (FPU_st0_ptr->tag == TW_Valid) {
98 if ((FPU_st0_ptr->exp <= EXP_UNDER) && (denormal_operand()))
100 #endif /* DENORM_OPERAND */
101 return (FPU_st0_ptr->sign == SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B;
104 if (FPU_st0_ptr->tag == TW_Infinity) {
105 if ((b->tag == TW_Valid) || (b->tag == TW_Zero)) {
106 #ifdef DENORM_OPERAND
107 if ((b->tag == TW_Valid) && (b->exp <= EXP_UNDER)
108 && (denormal_operand()))
109 return COMP_Denormal;
110 #endif /* DENORM_OPERAND */
111 return (FPU_st0_ptr->sign == SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B;
113 if (b->tag == TW_Infinity) {
114 /* The 80486 book says that infinities
116 return (FPU_st0_ptr->sign == b->sign) ? COMP_A_eq_B :
117 ((FPU_st0_ptr->sign == SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B);
119 /* Fall through to the NaN code */
121 if (b->tag == TW_Infinity) {
122 if ((FPU_st0_ptr->tag == TW_Valid) || (FPU_st0_ptr->tag == TW_Zero)) {
123 #ifdef DENORM_OPERAND
124 if ((FPU_st0_ptr->tag == TW_Valid)
125 && (FPU_st0_ptr->exp <= EXP_UNDER)
126 && (denormal_operand()))
127 return COMP_Denormal;
128 #endif /* DENORM_OPERAND */
129 return (b->sign == SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B;
131 /* Fall through to the NaN code */
133 /* The only possibility now should be that one of the
134 * arguments is a NaN */
135 if ((FPU_st0_ptr->tag == TW_NaN) || (b->tag == TW_NaN)) {
136 if (((FPU_st0_ptr->tag == TW_NaN) && !(FPU_st0_ptr->sigh & 0x40000000))
137 || ((b->tag == TW_NaN) && !(b->sigh & 0x40000000)))
138 /* At least one arg is a signaling NaN */
139 return COMP_No_Comp | COMP_SNaN | COMP_NaN;
141 /* Neither is a signaling NaN */
142 return COMP_No_Comp | COMP_NaN;
144 EXCEPTION(EX_Invalid);
147 if (!(FPU_st0_ptr->sigh & 0x80000000))
148 EXCEPTION(EX_Invalid);
149 if (!(b->sigh & 0x80000000))
150 EXCEPTION(EX_Invalid);
151 #endif /* PARANOID */
153 #ifdef DENORM_OPERAND
154 if (((FPU_st0_ptr->exp <= EXP_UNDER) ||
155 (b->exp <= EXP_UNDER)) && (denormal_operand()))
156 return COMP_Denormal;
157 #endif /* DENORM_OPERAND */
159 if (FPU_st0_ptr->sign != b->sign)
160 return (FPU_st0_ptr->sign == SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B;
162 diff = FPU_st0_ptr->exp - b->exp;
164 diff = FPU_st0_ptr->sigh - b->sigh; /* Works only if ms bits
167 diff = FPU_st0_ptr->sigl > b->sigl;
169 diff = -(FPU_st0_ptr->sigl < b->sigl);
173 return (FPU_st0_ptr->sign == SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B;
175 return (FPU_st0_ptr->sign == SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B;
181 /* This function requires that st(0) is not empty */
183 compare_st_data(void)
187 c = compare(&FPU_loaded_data);
189 if (c & (COMP_NaN | COMP_Denormal)) {
191 EXCEPTION(EX_Invalid);
192 f = SW_C3 | SW_C2 | SW_C0;
194 /* One of the operands is a de-normal */
209 f = SW_C3 | SW_C2 | SW_C0;
213 EXCEPTION(EX_INTERNAL | 0x121);
214 f = SW_C3 | SW_C2 | SW_C0;
216 #endif /* PARANOID */
224 compare_st_st(int nr)
228 if (!NOT_EMPTY_0 || !NOT_EMPTY(nr)) {
229 setcc(SW_C3 | SW_C2 | SW_C0);
231 EXCEPTION(EX_StackUnder);
232 return control_word & CW_Invalid;
234 c = compare(&st(nr));
235 if (c & (COMP_NaN | COMP_Denormal)) {
237 setcc(SW_C3 | SW_C2 | SW_C0);
238 EXCEPTION(EX_Invalid);
239 return control_word & CW_Invalid;
241 /* One of the operands is a de-normal */
242 return control_word & CW_Denormal;
256 f = SW_C3 | SW_C2 | SW_C0;
260 EXCEPTION(EX_INTERNAL | 0x122);
261 f = SW_C3 | SW_C2 | SW_C0;
263 #endif /* PARANOID */
271 compare_u_st_st(int nr)
275 if (!NOT_EMPTY_0 || !NOT_EMPTY(nr)) {
276 setcc(SW_C3 | SW_C2 | SW_C0);
278 EXCEPTION(EX_StackUnder);
279 return control_word & CW_Invalid;
281 c = compare(&st(nr));
282 if (c & (COMP_NaN | COMP_Denormal)) {
284 setcc(SW_C3 | SW_C2 | SW_C0);
285 if (c & COMP_SNaN) { /* This is the only difference
286 * between un-ordered and
287 * ordinary comparisons */
288 EXCEPTION(EX_Invalid);
289 return control_word & CW_Invalid;
293 /* One of the operands is a de-normal */
294 return control_word & CW_Denormal;
308 f = SW_C3 | SW_C2 | SW_C0;
312 EXCEPTION(EX_INTERNAL | 0x123);
313 f = SW_C3 | SW_C2 | SW_C0;
315 #endif /* PARANOID */
320 /*---------------------------------------------------------------------------*/
326 compare_st_st(FPU_rm);
334 if (compare_st_st(FPU_rm))
347 if (compare_st_st(1)) {
349 FPU_st0_ptr = &st(0);
359 compare_u_st_st(FPU_rm);
368 if (compare_u_st_st(FPU_rm))
378 if (compare_u_st_st(1)) {
380 FPU_st0_ptr = &st(0);