fpu_compare.c revision 1.3.6.2 1 1.3.6.2 eeh /* $NetBSD: fpu_compare.c,v 1.3.6.2 2001/08/26 05:46:32 eeh Exp $ */
2 1.3.6.2 eeh
3 1.3.6.2 eeh /*
4 1.3.6.2 eeh * Copyright (c) 1992, 1993
5 1.3.6.2 eeh * The Regents of the University of California. All rights reserved.
6 1.3.6.2 eeh *
7 1.3.6.2 eeh * This software was developed by the Computer Systems Engineering group
8 1.3.6.2 eeh * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.3.6.2 eeh * contributed to Berkeley.
10 1.3.6.2 eeh *
11 1.3.6.2 eeh * All advertising materials mentioning features or use of this software
12 1.3.6.2 eeh * must display the following acknowledgement:
13 1.3.6.2 eeh * This product includes software developed by the University of
14 1.3.6.2 eeh * California, Lawrence Berkeley Laboratory.
15 1.3.6.2 eeh *
16 1.3.6.2 eeh * Redistribution and use in source and binary forms, with or without
17 1.3.6.2 eeh * modification, are permitted provided that the following conditions
18 1.3.6.2 eeh * are met:
19 1.3.6.2 eeh * 1. Redistributions of source code must retain the above copyright
20 1.3.6.2 eeh * notice, this list of conditions and the following disclaimer.
21 1.3.6.2 eeh * 2. Redistributions in binary form must reproduce the above copyright
22 1.3.6.2 eeh * notice, this list of conditions and the following disclaimer in the
23 1.3.6.2 eeh * documentation and/or other materials provided with the distribution.
24 1.3.6.2 eeh * 3. All advertising materials mentioning features or use of this software
25 1.3.6.2 eeh * must display the following acknowledgement:
26 1.3.6.2 eeh * This product includes software developed by the University of
27 1.3.6.2 eeh * California, Berkeley and its contributors.
28 1.3.6.2 eeh * 4. Neither the name of the University nor the names of its contributors
29 1.3.6.2 eeh * may be used to endorse or promote products derived from this software
30 1.3.6.2 eeh * without specific prior written permission.
31 1.3.6.2 eeh *
32 1.3.6.2 eeh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 1.3.6.2 eeh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 1.3.6.2 eeh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 1.3.6.2 eeh * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 1.3.6.2 eeh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 1.3.6.2 eeh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 1.3.6.2 eeh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 1.3.6.2 eeh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 1.3.6.2 eeh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 1.3.6.2 eeh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 1.3.6.2 eeh * SUCH DAMAGE.
43 1.3.6.2 eeh *
44 1.3.6.2 eeh * @(#)fpu_compare.c 8.1 (Berkeley) 6/11/93
45 1.3.6.2 eeh */
46 1.3.6.2 eeh
47 1.3.6.2 eeh /*
48 1.3.6.2 eeh * CMP and CMPE instructions.
49 1.3.6.2 eeh *
50 1.3.6.2 eeh * These rely on the fact that our internal wide format is achieved by
51 1.3.6.2 eeh * adding zero bits to the end of narrower mantissas.
52 1.3.6.2 eeh */
53 1.3.6.2 eeh
54 1.3.6.2 eeh #include <sys/types.h>
55 1.3.6.2 eeh
56 1.3.6.2 eeh #include <machine/reg.h>
57 1.3.6.2 eeh
58 1.3.6.2 eeh #include <sparc/fpu/fpu_arith.h>
59 1.3.6.2 eeh #include <sparc/fpu/fpu_emu.h>
60 1.3.6.2 eeh
61 1.3.6.2 eeh /*
62 1.3.6.2 eeh * Perform a compare instruction (with or without unordered exception).
63 1.3.6.2 eeh * This updates the fcc field in the fsr.
64 1.3.6.2 eeh *
65 1.3.6.2 eeh * If either operand is NaN, the result is unordered. For cmpe, this
66 1.3.6.2 eeh * causes an NV exception. Everything else is ordered:
67 1.3.6.2 eeh * |Inf| > |numbers| > |0|.
68 1.3.6.2 eeh * We already arranged for fp_class(Inf) > fp_class(numbers) > fp_class(0),
69 1.3.6.2 eeh * so we get this directly. Note, however, that two zeros compare equal
70 1.3.6.2 eeh * regardless of sign, while everything else depends on sign.
71 1.3.6.2 eeh *
72 1.3.6.2 eeh * Incidentally, two Infs of the same sign compare equal (per the 80387
73 1.3.6.2 eeh * manual---it would be nice if the SPARC documentation were more
74 1.3.6.2 eeh * complete).
75 1.3.6.2 eeh */
76 1.3.6.2 eeh void
77 1.3.6.2 eeh fpu_compare(struct fpemu *fe, int cmpe)
78 1.3.6.2 eeh {
79 1.3.6.2 eeh register struct fpn *a, *b;
80 1.3.6.2 eeh register int cc;
81 1.3.6.2 eeh FPU_DECL_CARRY
82 1.3.6.2 eeh
83 1.3.6.2 eeh a = &fe->fe_f1;
84 1.3.6.2 eeh b = &fe->fe_f2;
85 1.3.6.2 eeh
86 1.3.6.2 eeh if (ISNAN(a) || ISNAN(b)) {
87 1.3.6.2 eeh /*
88 1.3.6.2 eeh * In any case, we already got an exception for signalling
89 1.3.6.2 eeh * NaNs; here we may replace that one with an identical
90 1.3.6.2 eeh * exception, but so what?.
91 1.3.6.2 eeh */
92 1.3.6.2 eeh if (cmpe)
93 1.3.6.2 eeh fe->fe_cx = FSR_NV;
94 1.3.6.2 eeh cc = FSR_CC_UO;
95 1.3.6.2 eeh goto done;
96 1.3.6.2 eeh }
97 1.3.6.2 eeh
98 1.3.6.2 eeh /*
99 1.3.6.2 eeh * Must handle both-zero early to avoid sign goofs. Otherwise,
100 1.3.6.2 eeh * at most one is 0, and if the signs differ we are done.
101 1.3.6.2 eeh */
102 1.3.6.2 eeh if (ISZERO(a) && ISZERO(b)) {
103 1.3.6.2 eeh cc = FSR_CC_EQ;
104 1.3.6.2 eeh goto done;
105 1.3.6.2 eeh }
106 1.3.6.2 eeh if (a->fp_sign) { /* a < 0 (or -0) */
107 1.3.6.2 eeh if (!b->fp_sign) { /* b >= 0 (or if a = -0, b > 0) */
108 1.3.6.2 eeh cc = FSR_CC_LT;
109 1.3.6.2 eeh goto done;
110 1.3.6.2 eeh }
111 1.3.6.2 eeh } else { /* a > 0 (or +0) */
112 1.3.6.2 eeh if (b->fp_sign) { /* b <= -0 (or if a = +0, b < 0) */
113 1.3.6.2 eeh cc = FSR_CC_GT;
114 1.3.6.2 eeh goto done;
115 1.3.6.2 eeh }
116 1.3.6.2 eeh }
117 1.3.6.2 eeh
118 1.3.6.2 eeh /*
119 1.3.6.2 eeh * Now the signs are the same (but may both be negative). All
120 1.3.6.2 eeh * we have left are these cases:
121 1.3.6.2 eeh *
122 1.3.6.2 eeh * |a| < |b| [classes or values differ]
123 1.3.6.2 eeh * |a| > |b| [classes or values differ]
124 1.3.6.2 eeh * |a| == |b| [classes and values identical]
125 1.3.6.2 eeh *
126 1.3.6.2 eeh * We define `diff' here to expand these as:
127 1.3.6.2 eeh *
128 1.3.6.2 eeh * |a| < |b|, a,b >= 0: a < b => FSR_CC_LT
129 1.3.6.2 eeh * |a| < |b|, a,b < 0: a > b => FSR_CC_GT
130 1.3.6.2 eeh * |a| > |b|, a,b >= 0: a > b => FSR_CC_GT
131 1.3.6.2 eeh * |a| > |b|, a,b < 0: a < b => FSR_CC_LT
132 1.3.6.2 eeh */
133 1.3.6.2 eeh #define opposite_cc(cc) ((cc) == FSR_CC_LT ? FSR_CC_GT : FSR_CC_LT)
134 1.3.6.2 eeh #define diff(magnitude) (a->fp_sign ? opposite_cc(magnitude) : (magnitude))
135 1.3.6.2 eeh if (a->fp_class < b->fp_class) { /* |a| < |b| */
136 1.3.6.2 eeh cc = diff(FSR_CC_LT);
137 1.3.6.2 eeh goto done;
138 1.3.6.2 eeh }
139 1.3.6.2 eeh if (a->fp_class > b->fp_class) { /* |a| > |b| */
140 1.3.6.2 eeh cc = diff(FSR_CC_GT);
141 1.3.6.2 eeh goto done;
142 1.3.6.2 eeh }
143 1.3.6.2 eeh /* now none can be 0: only Inf and numbers remain */
144 1.3.6.2 eeh if (ISINF(a)) { /* |Inf| = |Inf| */
145 1.3.6.2 eeh cc = FSR_CC_EQ;
146 1.3.6.2 eeh goto done;
147 1.3.6.2 eeh }
148 1.3.6.2 eeh /*
149 1.3.6.2 eeh * Only numbers remain. To compare two numbers in magnitude, we
150 1.3.6.2 eeh * simply subtract them.
151 1.3.6.2 eeh */
152 1.3.6.2 eeh a = fpu_sub(fe);
153 1.3.6.2 eeh if (a->fp_class == FPC_ZERO)
154 1.3.6.2 eeh cc = FSR_CC_EQ;
155 1.3.6.2 eeh else
156 1.3.6.2 eeh cc = diff(FSR_CC_GT);
157 1.3.6.2 eeh
158 1.3.6.2 eeh done:
159 1.3.6.2 eeh fe->fe_fsr = (fe->fe_fsr & ~FSR_FCC) | (cc << FSR_FCC_SHIFT);
160 1.3.6.2 eeh }
161