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