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