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