/* $NetBSD: fpu_compare.c,v 1.4 2005/12/11 12:18:42 christos Exp $ */12/*-3* SPDX-License-Identifier: BSD-3-Clause4*5* Copyright (c) 1992, 19936* The Regents of the University of California. All rights reserved.7*8* This software was developed by the Computer Systems Engineering group9* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and10* contributed to Berkeley.11*12* All advertising materials mentioning features or use of this software13* must display the following acknowledgement:14* This product includes software developed by the University of15* California, Lawrence Berkeley Laboratory.16*17* Redistribution and use in source and binary forms, with or without18* modification, are permitted provided that the following conditions19* are met:20* 1. Redistributions of source code must retain the above copyright21* notice, this list of conditions and the following disclaimer.22* 2. Redistributions in binary form must reproduce the above copyright23* notice, this list of conditions and the following disclaimer in the24* documentation and/or other materials provided with the distribution.25* 3. Neither the name of the University nor the names of its contributors26* may be used to endorse or promote products derived from this software27* without specific prior written permission.28*29* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND30* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE31* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE32* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE33* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL34* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS35* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)36* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT37* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY38* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF39* SUCH DAMAGE.40*/4142/*43* FCMPU and FCMPO instructions.44*45* These rely on the fact that our internal wide format is achieved by46* adding zero bits to the end of narrower mantissas.47*/4849#include <sys/types.h>50#include <sys/systm.h>5152#include <machine/fpu.h>5354#include <powerpc/fpu/fpu_arith.h>55#include <powerpc/fpu/fpu_emu.h>5657/*58* Perform a compare instruction (with or without unordered exception).59* This updates the fcc field in the fsr.60*61* If either operand is NaN, the result is unordered. For ordered, this62* causes an NV exception. Everything else is ordered:63* |Inf| > |numbers| > |0|.64* We already arranged for fp_class(Inf) > fp_class(numbers) > fp_class(0),65* so we get this directly. Note, however, that two zeros compare equal66* regardless of sign, while everything else depends on sign.67*68* Incidentally, two Infs of the same sign compare equal (per the 8038769* manual---it would be nice if the SPARC documentation were more70* complete).71*/72void73fpu_compare(struct fpemu *fe, int ordered)74{75struct fpn *a, *b, *r;76int cc;7778a = &fe->fe_f1;79b = &fe->fe_f2;80r = &fe->fe_f3;8182if (ISNAN(a) || ISNAN(b)) {83/*84* In any case, we already got an exception for signalling85* NaNs; here we may replace that one with an identical86* exception, but so what?.87*/88cc = FPSCR_FU;89if (ISSNAN(a) || ISSNAN(b))90cc |= FPSCR_VXSNAN;91if (ordered) {92if (fe->fe_fpscr & FPSCR_VE || ISQNAN(a) || ISQNAN(b))93cc |= FPSCR_VXVC;94}95goto done;96}9798/*99* Must handle both-zero early to avoid sign goofs. Otherwise,100* at most one is 0, and if the signs differ we are done.101*/102if (ISZERO(a) && ISZERO(b)) {103cc = FPSCR_FE;104goto done;105}106if (a->fp_sign) { /* a < 0 (or -0) */107if (!b->fp_sign) { /* b >= 0 (or if a = -0, b > 0) */108cc = FPSCR_FL;109goto done;110}111} else { /* a > 0 (or +0) */112if (b->fp_sign) { /* b <= -0 (or if a = +0, b < 0) */113cc = FPSCR_FG;114goto done;115}116}117118/*119* Now the signs are the same (but may both be negative). All120* we have left are these cases:121*122* |a| < |b| [classes or values differ]123* |a| > |b| [classes or values differ]124* |a| == |b| [classes and values identical]125*126* We define `diff' here to expand these as:127*128* |a| < |b|, a,b >= 0: a < b => FSR_CC_LT129* |a| < |b|, a,b < 0: a > b => FSR_CC_GT130* |a| > |b|, a,b >= 0: a > b => FSR_CC_GT131* |a| > |b|, a,b < 0: a < b => FSR_CC_LT132*/133#define opposite_cc(cc) ((cc) == FPSCR_FL ? FPSCR_FG : FPSCR_FL)134#define diff(magnitude) (a->fp_sign ? opposite_cc(magnitude) : (magnitude))135if (a->fp_class < b->fp_class) { /* |a| < |b| */136cc = diff(FPSCR_FL);137goto done;138}139if (a->fp_class > b->fp_class) { /* |a| > |b| */140cc = diff(FPSCR_FG);141goto done;142}143/* now none can be 0: only Inf and numbers remain */144if (ISINF(a)) { /* |Inf| = |Inf| */145cc = FPSCR_FE;146goto done;147}148fpu_sub(fe);149if (ISZERO(r))150cc = FPSCR_FE;151else if (r->fp_sign)152cc = FPSCR_FL;153else154cc = FPSCR_FG;155done:156fe->fe_cx = cc;157}158159160