xref: /illumos-gate/usr/src/lib/libc/sparc/fp/_Q_cmp.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1994-1997, by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "quad.h"
30 
31 #ifdef __sparcv9
32 #define	_Q_cmp	_Qp_cmp
33 #endif
34 
35 /*
36  * _Q_cmp(x, y) returns the condition code that would result from
37  * fcmpq *x, *y (and raises the same exceptions).
38  */
39 enum fcc_type
40 _Q_cmp(const union longdouble *x, const union longdouble *y)
41 {
42 	unsigned int	xm, ym, fsr;
43 
44 	if (QUAD_ISNAN(*x) || QUAD_ISNAN(*y)) {
45 		if ((QUAD_ISNAN(*x) && !(x->l.msw & 0x8000)) ||
46 		    (QUAD_ISNAN(*y) && !(y->l.msw & 0x8000))) {
47 			/* snan, signal invalid */
48 			__quad_getfsrp(&fsr);
49 			if (fsr & FSR_NVM) {
50 				__quad_fcmpq(x, y, &fsr);
51 				return ((fsr >> 10) & 3);
52 			} else {
53 				fsr = (fsr & ~FSR_CEXC) | FSR_NVA | FSR_NVC;
54 				__quad_setfsrp(&fsr);
55 			}
56 		}
57 		return (fcc_unordered);
58 	}
59 
60 	/* ignore sign of zero */
61 	xm = x->l.msw;
62 	if (QUAD_ISZERO(*x))
63 		xm &= 0x7fffffff;
64 	ym = y->l.msw;
65 	if (QUAD_ISZERO(*y))
66 		ym &= 0x7fffffff;
67 
68 	if ((xm ^ ym) & 0x80000000)	/* x and y have opposite signs */
69 		return ((ym & 0x80000000)? fcc_greater : fcc_less);
70 
71 	if (xm & 0x80000000) {
72 		if (xm > ym)
73 			return (fcc_less);
74 		if (xm < ym)
75 			return (fcc_greater);
76 		if (x->l.frac2 > y->l.frac2)
77 			return (fcc_less);
78 		if (x->l.frac2 < y->l.frac2)
79 			return (fcc_greater);
80 		if (x->l.frac3 > y->l.frac3)
81 			return (fcc_less);
82 		if (x->l.frac3 < y->l.frac3)
83 			return (fcc_greater);
84 		if (x->l.frac4 > y->l.frac4)
85 			return (fcc_less);
86 		if (x->l.frac4 < y->l.frac4)
87 			return (fcc_greater);
88 		return (fcc_equal);
89 	}
90 	if (xm < ym)
91 		return (fcc_less);
92 	if (xm > ym)
93 		return (fcc_greater);
94 	if (x->l.frac2 < y->l.frac2)
95 		return (fcc_less);
96 	if (x->l.frac2 > y->l.frac2)
97 		return (fcc_greater);
98 	if (x->l.frac3 < y->l.frac3)
99 		return (fcc_less);
100 	if (x->l.frac3 > y->l.frac3)
101 		return (fcc_greater);
102 	if (x->l.frac4 < y->l.frac4)
103 		return (fcc_less);
104 	if (x->l.frac4 > y->l.frac4)
105 		return (fcc_greater);
106 	return (fcc_equal);
107 }
108