1176491Smarcel/* $NetBSD: fpu_compare.c,v 1.4 2005/12/11 12:18:42 christos Exp $ */ 2176491Smarcel 3331722Seadler/* 4176491Smarcel * Copyright (c) 1992, 1993 5176491Smarcel * The Regents of the University of California. All rights reserved. 6176491Smarcel * 7176491Smarcel * This software was developed by the Computer Systems Engineering group 8176491Smarcel * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9176491Smarcel * contributed to Berkeley. 10176491Smarcel * 11176491Smarcel * All advertising materials mentioning features or use of this software 12176491Smarcel * must display the following acknowledgement: 13176491Smarcel * This product includes software developed by the University of 14176491Smarcel * California, Lawrence Berkeley Laboratory. 15176491Smarcel * 16176491Smarcel * Redistribution and use in source and binary forms, with or without 17176491Smarcel * modification, are permitted provided that the following conditions 18176491Smarcel * are met: 19176491Smarcel * 1. Redistributions of source code must retain the above copyright 20176491Smarcel * notice, this list of conditions and the following disclaimer. 21176491Smarcel * 2. Redistributions in binary form must reproduce the above copyright 22176491Smarcel * notice, this list of conditions and the following disclaimer in the 23176491Smarcel * documentation and/or other materials provided with the distribution. 24176491Smarcel * 3. Neither the name of the University nor the names of its contributors 25176491Smarcel * may be used to endorse or promote products derived from this software 26176491Smarcel * without specific prior written permission. 27176491Smarcel * 28176491Smarcel * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29176491Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30176491Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31176491Smarcel * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32176491Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33176491Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34176491Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35176491Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36176491Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37176491Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38176491Smarcel * SUCH DAMAGE. 39176491Smarcel * 40176491Smarcel * @(#)fpu_compare.c 8.1 (Berkeley) 6/11/93 41176491Smarcel */ 42176491Smarcel 43176491Smarcel/* 44176491Smarcel * FCMPU and FCMPO instructions. 45176491Smarcel * 46176491Smarcel * These rely on the fact that our internal wide format is achieved by 47176491Smarcel * adding zero bits to the end of narrower mantissas. 48176491Smarcel */ 49176491Smarcel 50176491Smarcel#include <sys/cdefs.h> 51176491Smarcel__FBSDID("$FreeBSD$"); 52176491Smarcel 53178030Sgrehan#include <sys/types.h> 54176491Smarcel#include <sys/systm.h> 55176491Smarcel 56176491Smarcel#include <machine/fpu.h> 57176491Smarcel#include <machine/reg.h> 58176491Smarcel 59176491Smarcel#include <powerpc/fpu/fpu_arith.h> 60176491Smarcel#include <powerpc/fpu/fpu_emu.h> 61176491Smarcel 62176491Smarcel/* 63176491Smarcel * Perform a compare instruction (with or without unordered exception). 64176491Smarcel * This updates the fcc field in the fsr. 65176491Smarcel * 66176491Smarcel * If either operand is NaN, the result is unordered. For ordered, this 67176491Smarcel * causes an NV exception. Everything else is ordered: 68176491Smarcel * |Inf| > |numbers| > |0|. 69176491Smarcel * We already arranged for fp_class(Inf) > fp_class(numbers) > fp_class(0), 70176491Smarcel * so we get this directly. Note, however, that two zeros compare equal 71176491Smarcel * regardless of sign, while everything else depends on sign. 72176491Smarcel * 73176491Smarcel * Incidentally, two Infs of the same sign compare equal (per the 80387 74176491Smarcel * manual---it would be nice if the SPARC documentation were more 75176491Smarcel * complete). 76176491Smarcel */ 77176491Smarcelvoid 78176491Smarcelfpu_compare(struct fpemu *fe, int ordered) 79176491Smarcel{ 80176491Smarcel struct fpn *a, *b, *r; 81176491Smarcel int cc; 82176491Smarcel 83176491Smarcel a = &fe->fe_f1; 84176491Smarcel b = &fe->fe_f2; 85176491Smarcel r = &fe->fe_f3; 86176491Smarcel 87176491Smarcel if (ISNAN(a) || ISNAN(b)) { 88176491Smarcel /* 89176491Smarcel * In any case, we already got an exception for signalling 90176491Smarcel * NaNs; here we may replace that one with an identical 91176491Smarcel * exception, but so what?. 92176491Smarcel */ 93176491Smarcel cc = FPSCR_FU; 94176491Smarcel if (ISSNAN(a) || ISSNAN(b)) 95176491Smarcel cc |= FPSCR_VXSNAN; 96176491Smarcel if (ordered) { 97176491Smarcel if (fe->fe_fpscr & FPSCR_VE || ISQNAN(a) || ISQNAN(b)) 98176491Smarcel cc |= FPSCR_VXVC; 99176491Smarcel } 100176491Smarcel goto done; 101176491Smarcel } 102176491Smarcel 103176491Smarcel /* 104176491Smarcel * Must handle both-zero early to avoid sign goofs. Otherwise, 105176491Smarcel * at most one is 0, and if the signs differ we are done. 106176491Smarcel */ 107176491Smarcel if (ISZERO(a) && ISZERO(b)) { 108176491Smarcel cc = FPSCR_FE; 109176491Smarcel goto done; 110176491Smarcel } 111176491Smarcel if (a->fp_sign) { /* a < 0 (or -0) */ 112176491Smarcel if (!b->fp_sign) { /* b >= 0 (or if a = -0, b > 0) */ 113176491Smarcel cc = FPSCR_FL; 114176491Smarcel goto done; 115176491Smarcel } 116176491Smarcel } else { /* a > 0 (or +0) */ 117176491Smarcel if (b->fp_sign) { /* b <= -0 (or if a = +0, b < 0) */ 118176491Smarcel cc = FPSCR_FG; 119176491Smarcel goto done; 120176491Smarcel } 121176491Smarcel } 122176491Smarcel 123176491Smarcel /* 124176491Smarcel * Now the signs are the same (but may both be negative). All 125176491Smarcel * we have left are these cases: 126176491Smarcel * 127176491Smarcel * |a| < |b| [classes or values differ] 128176491Smarcel * |a| > |b| [classes or values differ] 129176491Smarcel * |a| == |b| [classes and values identical] 130176491Smarcel * 131176491Smarcel * We define `diff' here to expand these as: 132176491Smarcel * 133176491Smarcel * |a| < |b|, a,b >= 0: a < b => FSR_CC_LT 134176491Smarcel * |a| < |b|, a,b < 0: a > b => FSR_CC_GT 135176491Smarcel * |a| > |b|, a,b >= 0: a > b => FSR_CC_GT 136176491Smarcel * |a| > |b|, a,b < 0: a < b => FSR_CC_LT 137176491Smarcel */ 138176491Smarcel#define opposite_cc(cc) ((cc) == FPSCR_FL ? FPSCR_FG : FPSCR_FL) 139176491Smarcel#define diff(magnitude) (a->fp_sign ? opposite_cc(magnitude) : (magnitude)) 140176491Smarcel if (a->fp_class < b->fp_class) { /* |a| < |b| */ 141176491Smarcel cc = diff(FPSCR_FL); 142176491Smarcel goto done; 143176491Smarcel } 144176491Smarcel if (a->fp_class > b->fp_class) { /* |a| > |b| */ 145176491Smarcel cc = diff(FPSCR_FG); 146176491Smarcel goto done; 147176491Smarcel } 148176491Smarcel /* now none can be 0: only Inf and numbers remain */ 149176491Smarcel if (ISINF(a)) { /* |Inf| = |Inf| */ 150176491Smarcel cc = FPSCR_FE; 151176491Smarcel goto done; 152176491Smarcel } 153176491Smarcel fpu_sub(fe); 154176491Smarcel if (ISZERO(r)) 155176491Smarcel cc = FPSCR_FE; 156176491Smarcel else if (r->fp_sign) 157176491Smarcel cc = FPSCR_FL; 158176491Smarcel else 159176491Smarcel cc = FPSCR_FG; 160176491Smarceldone: 161176491Smarcel fe->fe_cx = cc; 162176491Smarcel} 163