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 $
60 * $DragonFly: src/sys/i386/gnu/fpemul/Attic/reg_compare.c,v 1.3 2003/08/07 21:17:20 dillon Exp $
64 /*---------------------------------------------------------------------------+
65 | compare() is the core FPU_REG comparison function |
66 +---------------------------------------------------------------------------*/
67 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <machine/pcb.h>
73 #include "fpu_system.h"
74 #include "exception.h"
75 #include "control_w.h"
84 if (FPU_st0_ptr->tag | b->tag) {
85 if (FPU_st0_ptr->tag == TW_Zero) {
86 if (b->tag == TW_Zero)
88 if (b->tag == TW_Valid) {
90 if ((b->exp <= EXP_UNDER) && (denormal_operand()))
92 #endif /* DENORM_OPERAND */
93 return (b->sign == SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B;
96 if (b->tag == TW_Zero) {
97 if (FPU_st0_ptr->tag == TW_Valid) {
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;
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;
114 if (b->tag == TW_Infinity) {
115 /* The 80486 book says that infinities
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);
120 /* Fall through to the NaN code */
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;
132 /* Fall through to the NaN code */
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;
142 /* Neither is a signaling NaN */
143 return COMP_No_Comp | COMP_NaN;
145 EXCEPTION(EX_Invalid);
148 if (!(FPU_st0_ptr->sigh & 0x80000000))
149 EXCEPTION(EX_Invalid);
150 if (!(b->sigh & 0x80000000))
151 EXCEPTION(EX_Invalid);
152 #endif /* PARANOID */
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 */
160 if (FPU_st0_ptr->sign != b->sign)
161 return (FPU_st0_ptr->sign == SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B;
163 diff = FPU_st0_ptr->exp - b->exp;
165 diff = FPU_st0_ptr->sigh - b->sigh; /* Works only if ms bits
168 diff = FPU_st0_ptr->sigl > b->sigl;
170 diff = -(FPU_st0_ptr->sigl < b->sigl);
174 return (FPU_st0_ptr->sign == SIGN_POS) ? COMP_A_gt_B : COMP_A_lt_B;
176 return (FPU_st0_ptr->sign == SIGN_POS) ? COMP_A_lt_B : COMP_A_gt_B;
182 /* This function requires that st(0) is not empty */
184 compare_st_data(void)
188 c = compare(&FPU_loaded_data);
190 if (c & (COMP_NaN | COMP_Denormal)) {
192 EXCEPTION(EX_Invalid);
193 f = SW_C3 | SW_C2 | SW_C0;
195 /* One of the operands is a de-normal */
210 f = SW_C3 | SW_C2 | SW_C0;
214 EXCEPTION(EX_INTERNAL | 0x121);
215 f = SW_C3 | SW_C2 | SW_C0;
217 #endif /* PARANOID */
225 compare_st_st(int nr)
229 if (!NOT_EMPTY_0 || !NOT_EMPTY(nr)) {
230 setcc(SW_C3 | SW_C2 | SW_C0);
232 EXCEPTION(EX_StackUnder);
233 return control_word & CW_Invalid;
235 c = compare(&st(nr));
236 if (c & (COMP_NaN | COMP_Denormal)) {
238 setcc(SW_C3 | SW_C2 | SW_C0);
239 EXCEPTION(EX_Invalid);
240 return control_word & CW_Invalid;
242 /* One of the operands is a de-normal */
243 return control_word & CW_Denormal;
257 f = SW_C3 | SW_C2 | SW_C0;
261 EXCEPTION(EX_INTERNAL | 0x122);
262 f = SW_C3 | SW_C2 | SW_C0;
264 #endif /* PARANOID */
272 compare_u_st_st(int nr)
276 if (!NOT_EMPTY_0 || !NOT_EMPTY(nr)) {
277 setcc(SW_C3 | SW_C2 | SW_C0);
279 EXCEPTION(EX_StackUnder);
280 return control_word & CW_Invalid;
282 c = compare(&st(nr));
283 if (c & (COMP_NaN | COMP_Denormal)) {
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;
294 /* One of the operands is a de-normal */
295 return control_word & CW_Denormal;
309 f = SW_C3 | SW_C2 | SW_C0;
313 EXCEPTION(EX_INTERNAL | 0x123);
314 f = SW_C3 | SW_C2 | SW_C0;
316 #endif /* PARANOID */
321 /*---------------------------------------------------------------------------*/
327 compare_st_st(FPU_rm);
335 if (compare_st_st(FPU_rm))
348 if (compare_st_st(1)) {
350 FPU_st0_ptr = &st(0);
360 compare_u_st_st(FPU_rm);
369 if (compare_u_st_st(FPU_rm))
379 if (compare_u_st_st(1)) {
381 FPU_st0_ptr = &st(0);