xref: /titanic_50/usr/src/lib/libbc/libc/gen/common/_Qfcompare.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
23 
24 /*
25  * Copyright (c) 1988 by Sun Microsystems, Inc.
26  */
27 
28 #include "_Qquad.h"
29 #include "_Qglobals.h"
30 
31 enum fcc_type
_fp_compare(px,py,strict)32 _fp_compare(px, py, strict)
33 	unpacked       *px, *py;
34 	int             strict;	/* 0 if quiet NaN unexceptional, 1 if
35 				 * exceptional */
36 
37 {
38 	enum fcc_type   cc;
39 	int  k,n;
40 
41 	if ((px->fpclass == fp_quiet) || (py->fpclass == fp_quiet) ||
42 	    (px->fpclass == fp_signaling) || (py->fpclass == fp_signaling)) {
43 		if (strict)				/* NaN */
44 			fpu_set_exception(fp_invalid);
45 		cc = fcc_unordered;
46 	} else if ((px->fpclass == fp_zero) && (py->fpclass == fp_zero))
47 		cc = fcc_equal;
48 	/* both zeros */
49 	else if (px->sign < py->sign)
50 		cc = fcc_greater;
51 	else if (px->sign > py->sign)
52 		cc = fcc_less;
53 	else {			/* signs the same, compute magnitude cc */
54 		if ((int) px->fpclass > (int) py->fpclass)
55 			cc = fcc_greater;
56 		else if ((int) px->fpclass < (int) py->fpclass)
57 			cc = fcc_less;
58 		else
59 		 /* same classes */ if (px->fpclass == fp_infinity)
60 			cc = fcc_equal;	/* same infinity */
61 		else if (px->exponent > py->exponent)
62 			cc = fcc_greater;
63 		else if (px->exponent < py->exponent)
64 			cc = fcc_less;
65 		else {	/* equal exponents */
66 			n = fpu_cmpli(px->significand,py->significand,4);
67 			if(n>0) cc = fcc_greater;
68 			else if(n<0) cc = fcc_less;
69 			else cc = fcc_equal;
70 		}
71 		if (px->sign)
72 			switch (cc) {	/* negative numbers */
73 			case fcc_less:
74 				cc = fcc_greater;
75 				break;
76 			case fcc_greater:
77 				cc = fcc_less;
78 				break;
79 			}
80 	}
81 	return (cc);
82 }
83