xref: /netbsd-src/sys/arch/powerpc/fpu/fpu_compare.c (revision 6cf679389aa277ff34dad7fb37904edfd6a158dc)
1*6cf67938Srin /*	$NetBSD: fpu_compare.c,v 1.8 2022/09/08 15:22:43 rin Exp $ */
218b2f7e6Ssimonb 
318b2f7e6Ssimonb /*
418b2f7e6Ssimonb  * Copyright (c) 1992, 1993
518b2f7e6Ssimonb  *	The Regents of the University of California.  All rights reserved.
618b2f7e6Ssimonb  *
718b2f7e6Ssimonb  * This software was developed by the Computer Systems Engineering group
818b2f7e6Ssimonb  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
918b2f7e6Ssimonb  * contributed to Berkeley.
1018b2f7e6Ssimonb  *
1118b2f7e6Ssimonb  * All advertising materials mentioning features or use of this software
1218b2f7e6Ssimonb  * must display the following acknowledgement:
1318b2f7e6Ssimonb  *	This product includes software developed by the University of
1418b2f7e6Ssimonb  *	California, Lawrence Berkeley Laboratory.
1518b2f7e6Ssimonb  *
1618b2f7e6Ssimonb  * Redistribution and use in source and binary forms, with or without
1718b2f7e6Ssimonb  * modification, are permitted provided that the following conditions
1818b2f7e6Ssimonb  * are met:
1918b2f7e6Ssimonb  * 1. Redistributions of source code must retain the above copyright
2018b2f7e6Ssimonb  *    notice, this list of conditions and the following disclaimer.
2118b2f7e6Ssimonb  * 2. Redistributions in binary form must reproduce the above copyright
2218b2f7e6Ssimonb  *    notice, this list of conditions and the following disclaimer in the
2318b2f7e6Ssimonb  *    documentation and/or other materials provided with the distribution.
24aad01611Sagc  * 3. Neither the name of the University nor the names of its contributors
2518b2f7e6Ssimonb  *    may be used to endorse or promote products derived from this software
2618b2f7e6Ssimonb  *    without specific prior written permission.
2718b2f7e6Ssimonb  *
2818b2f7e6Ssimonb  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2918b2f7e6Ssimonb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3018b2f7e6Ssimonb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3118b2f7e6Ssimonb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3218b2f7e6Ssimonb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3318b2f7e6Ssimonb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3418b2f7e6Ssimonb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3518b2f7e6Ssimonb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3618b2f7e6Ssimonb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3718b2f7e6Ssimonb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3818b2f7e6Ssimonb  * SUCH DAMAGE.
3918b2f7e6Ssimonb  *
4018b2f7e6Ssimonb  *	@(#)fpu_compare.c	8.1 (Berkeley) 6/11/93
4118b2f7e6Ssimonb  */
4218b2f7e6Ssimonb 
4318b2f7e6Ssimonb /*
4418b2f7e6Ssimonb  * FCMPU and FCMPO instructions.
4518b2f7e6Ssimonb  *
4618b2f7e6Ssimonb  * These rely on the fact that our internal wide format is achieved by
4718b2f7e6Ssimonb  * adding zero bits to the end of narrower mantissas.
4818b2f7e6Ssimonb  */
4918b2f7e6Ssimonb 
50ed517291Slukem #include <sys/cdefs.h>
51*6cf67938Srin __KERNEL_RCSID(0, "$NetBSD: fpu_compare.c,v 1.8 2022/09/08 15:22:43 rin Exp $");
52ed517291Slukem 
5318b2f7e6Ssimonb #include <sys/types.h>
5418b2f7e6Ssimonb 
5518b2f7e6Ssimonb #include <machine/fpu.h>
5606f65540Srin #include <machine/reg.h>
5718b2f7e6Ssimonb 
5818b2f7e6Ssimonb #include <powerpc/fpu/fpu_arith.h>
5918b2f7e6Ssimonb #include <powerpc/fpu/fpu_emu.h>
6018b2f7e6Ssimonb 
6118b2f7e6Ssimonb /*
6218b2f7e6Ssimonb  * Perform a compare instruction (with or without unordered exception).
6318b2f7e6Ssimonb  * This updates the fcc field in the fsr.
6418b2f7e6Ssimonb  *
6518b2f7e6Ssimonb  * If either operand is NaN, the result is unordered.  For ordered, this
6618b2f7e6Ssimonb  * causes an NV exception.  Everything else is ordered:
6718b2f7e6Ssimonb  *	|Inf| > |numbers| > |0|.
6818b2f7e6Ssimonb  * We already arranged for fp_class(Inf) > fp_class(numbers) > fp_class(0),
6918b2f7e6Ssimonb  * so we get this directly.  Note, however, that two zeros compare equal
7018b2f7e6Ssimonb  * regardless of sign, while everything else depends on sign.
7118b2f7e6Ssimonb  *
7218b2f7e6Ssimonb  * Incidentally, two Infs of the same sign compare equal (per the 80387
7318b2f7e6Ssimonb  * manual---it would be nice if the SPARC documentation were more
7418b2f7e6Ssimonb  * complete).
7518b2f7e6Ssimonb  */
7618b2f7e6Ssimonb void
fpu_compare(struct fpemu * fe,int ordered)7718b2f7e6Ssimonb fpu_compare(struct fpemu *fe, int ordered)
7818b2f7e6Ssimonb {
7918b2f7e6Ssimonb 	struct fpn *a, *b, *r;
8018b2f7e6Ssimonb 	int cc;
8118b2f7e6Ssimonb 
8241cf117bSrin 	fe->fe_fpscr &= ~FPSCR_FPCC;
8341cf117bSrin 
8418b2f7e6Ssimonb 	a = &fe->fe_f1;
8518b2f7e6Ssimonb 	b = &fe->fe_f2;
8618b2f7e6Ssimonb 
8718b2f7e6Ssimonb 	if (ISNAN(a) || ISNAN(b)) {
8818b2f7e6Ssimonb 		/*
8918b2f7e6Ssimonb 		 * In any case, we already got an exception for signalling
9018b2f7e6Ssimonb 		 * NaNs; here we may replace that one with an identical
9118b2f7e6Ssimonb 		 * exception, but so what?.
9218b2f7e6Ssimonb 		 */
9318b2f7e6Ssimonb 		cc = FPSCR_FU;
9418b2f7e6Ssimonb 		if (ISSNAN(a) || ISSNAN(b))
9518b2f7e6Ssimonb 			cc |= FPSCR_VXSNAN;
9618b2f7e6Ssimonb 		if (ordered) {
971a4f18c7Srin #ifdef notyet /* XXXRO */
981a4f18c7Srin 			if ((fe->fe_fpscr & FPSCR_VE) == 0 ||
991a4f18c7Srin 			    ISQNAN(a) || ISQNAN(b))
1001a4f18c7Srin #endif
10118b2f7e6Ssimonb 				cc |= FPSCR_VXVC;
10218b2f7e6Ssimonb 		}
10318b2f7e6Ssimonb 		goto done;
10418b2f7e6Ssimonb 	}
10518b2f7e6Ssimonb 
10618b2f7e6Ssimonb 	/*
10718b2f7e6Ssimonb 	 * Must handle both-zero early to avoid sign goofs.  Otherwise,
10818b2f7e6Ssimonb 	 * at most one is 0, and if the signs differ we are done.
10918b2f7e6Ssimonb 	 */
11018b2f7e6Ssimonb 	if (ISZERO(a) && ISZERO(b)) {
11118b2f7e6Ssimonb 		cc = FPSCR_FE;
11218b2f7e6Ssimonb 		goto done;
11318b2f7e6Ssimonb 	}
11418b2f7e6Ssimonb 	if (a->fp_sign) {		/* a < 0 (or -0) */
11518b2f7e6Ssimonb 		if (!b->fp_sign) {	/* b >= 0 (or if a = -0, b > 0) */
11618b2f7e6Ssimonb 			cc = FPSCR_FL;
11718b2f7e6Ssimonb 			goto done;
11818b2f7e6Ssimonb 		}
11918b2f7e6Ssimonb 	} else {			/* a > 0 (or +0) */
12018b2f7e6Ssimonb 		if (b->fp_sign) {	/* b <= -0 (or if a = +0, b < 0) */
12118b2f7e6Ssimonb 			cc = FPSCR_FG;
12218b2f7e6Ssimonb 			goto done;
12318b2f7e6Ssimonb 		}
12418b2f7e6Ssimonb 	}
12518b2f7e6Ssimonb 
12618b2f7e6Ssimonb 	/*
12718b2f7e6Ssimonb 	 * Now the signs are the same (but may both be negative).  All
12818b2f7e6Ssimonb 	 * we have left are these cases:
12918b2f7e6Ssimonb 	 *
13018b2f7e6Ssimonb 	 *	|a| < |b|		[classes or values differ]
13118b2f7e6Ssimonb 	 *	|a| > |b|		[classes or values differ]
13218b2f7e6Ssimonb 	 *	|a| == |b|		[classes and values identical]
13318b2f7e6Ssimonb 	 *
13418b2f7e6Ssimonb 	 * We define `diff' here to expand these as:
13518b2f7e6Ssimonb 	 *
13618b2f7e6Ssimonb 	 *	|a| < |b|, a,b >= 0: a < b => FSR_CC_LT
13718b2f7e6Ssimonb 	 *	|a| < |b|, a,b < 0:  a > b => FSR_CC_GT
13818b2f7e6Ssimonb 	 *	|a| > |b|, a,b >= 0: a > b => FSR_CC_GT
13918b2f7e6Ssimonb 	 *	|a| > |b|, a,b < 0:  a < b => FSR_CC_LT
14018b2f7e6Ssimonb 	 */
14118b2f7e6Ssimonb #define opposite_cc(cc) ((cc) == FPSCR_FL ? FPSCR_FG : FPSCR_FL)
14218b2f7e6Ssimonb #define	diff(magnitude) (a->fp_sign ? opposite_cc(magnitude) :  (magnitude))
14318b2f7e6Ssimonb 	if (a->fp_class < b->fp_class) {	/* |a| < |b| */
14418b2f7e6Ssimonb 		cc = diff(FPSCR_FL);
14518b2f7e6Ssimonb 		goto done;
14618b2f7e6Ssimonb 	}
14718b2f7e6Ssimonb 	if (a->fp_class > b->fp_class) {	/* |a| > |b| */
14818b2f7e6Ssimonb 		cc = diff(FPSCR_FG);
14918b2f7e6Ssimonb 		goto done;
15018b2f7e6Ssimonb 	}
15118b2f7e6Ssimonb 	/* now none can be 0: only Inf and numbers remain */
15218b2f7e6Ssimonb 	if (ISINF(a)) {				/* |Inf| = |Inf| */
15318b2f7e6Ssimonb 		cc = FPSCR_FE;
15418b2f7e6Ssimonb 		goto done;
15518b2f7e6Ssimonb 	}
156*6cf67938Srin 	r = fpu_sub(fe);
15718b2f7e6Ssimonb 	if (ISZERO(r))
15818b2f7e6Ssimonb 		cc = FPSCR_FE;
15918b2f7e6Ssimonb 	else if (r->fp_sign)
16018b2f7e6Ssimonb 		cc = FPSCR_FL;
16118b2f7e6Ssimonb 	else
16218b2f7e6Ssimonb 		cc = FPSCR_FG;
16318b2f7e6Ssimonb done:
16418b2f7e6Ssimonb 	fe->fe_cx = cc;
16518b2f7e6Ssimonb }
166