xref: /netbsd-src/sys/arch/hppa/spmath/dfcmp.c (revision d72340ff8e2fb5c3ca49ab85c798389cda4408d6)
1 /*	$NetBSD: dfcmp.c,v 1.4 2012/02/04 17:03:09 skrll Exp $	*/
2 
3 /*	$OpenBSD: dfcmp.c,v 1.4 2001/03/29 03:58:17 mickey Exp $	*/
4 
5 /*
6  * Copyright 1996 1995 by Open Software Foundation, Inc.
7  *              All Rights Reserved
8  *
9  * Permission to use, copy, modify, and distribute this software and
10  * its documentation for any purpose and without fee is hereby granted,
11  * provided that the above copyright notice appears in all copies and
12  * that both the copyright notice and this permission notice appear in
13  * supporting documentation.
14  *
15  * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17  * FOR A PARTICULAR PURPOSE.
18  *
19  * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
20  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
21  * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
22  * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
23  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24  */
25 /*
26  * pmk1.1
27  */
28 /*
29  * (c) Copyright 1986 HEWLETT-PACKARD COMPANY
30  *
31  * To anyone who acknowledges that this file is provided "AS IS"
32  * without any express or implied warranty:
33  *     permission to use, copy, modify, and distribute this file
34  * for any purpose is hereby granted without fee, provided that
35  * the above copyright notice and this notice appears in all
36  * copies, and that the name of Hewlett-Packard Company not be
37  * used in advertising or publicity pertaining to distribution
38  * of the software without specific, written prior permission.
39  * Hewlett-Packard Company makes no representations about the
40  * suitability of this software for any purpose.
41  */
42 
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: dfcmp.c,v 1.4 2012/02/04 17:03:09 skrll Exp $");
46 
47 #include "../spmath/float.h"
48 #include "../spmath/dbl_float.h"
49 
50 /*
51  * dbl_cmp: compare two values
52  */
53 int
dbl_fcmp(dbl_floating_point * leftptr,dbl_floating_point * rightptr,unsigned int cond,unsigned int * status)54 dbl_fcmp(dbl_floating_point *leftptr, dbl_floating_point *rightptr,
55     unsigned int cond, unsigned int *status)
56 {
57     register unsigned int leftp1, leftp2, rightp1, rightp2;
58     register int xorresult;
59 
60     /* Create local copies of the numbers */
61     Dbl_copyfromptr(leftptr,leftp1,leftp2);
62     Dbl_copyfromptr(rightptr,rightp1,rightp2);
63     /*
64      * Test for NaN
65      */
66     if(    (Dbl_exponent(leftp1) == DBL_INFINITY_EXPONENT)
67 	|| (Dbl_exponent(rightp1) == DBL_INFINITY_EXPONENT) )
68 	{
69 	/* Check if a NaN is involved.  Signal an invalid exception when
70 	 * comparing a signaling NaN or when comparing quiet NaNs and the
71 	 * low bit of the condition is set */
72 	if( ((Dbl_exponent(leftp1) == DBL_INFINITY_EXPONENT)
73 	    && Dbl_isnotzero_mantissa(leftp1,leftp2)
74 	    && (Exception(cond) || Dbl_isone_signaling(leftp1)))
75 	   ||
76 	    ((Dbl_exponent(rightp1) == DBL_INFINITY_EXPONENT)
77 	    && Dbl_isnotzero_mantissa(rightp1,rightp2)
78 	    && (Exception(cond) || Dbl_isone_signaling(rightp1))) )
79 	    {
80 	    if( Is_invalidtrap_enabled() ) {
81 		Set_status_cbit(Unordered(cond));
82 		return(INVALIDEXCEPTION);
83 	    }
84 	    else Set_invalidflag();
85 	    Set_status_cbit(Unordered(cond));
86 	    return(NOEXCEPTION);
87 	    }
88 	/* All the exceptional conditions are handled, now special case
89 	   NaN compares */
90 	else if( ((Dbl_exponent(leftp1) == DBL_INFINITY_EXPONENT)
91 	    && Dbl_isnotzero_mantissa(leftp1,leftp2))
92 	   ||
93 	    ((Dbl_exponent(rightp1) == DBL_INFINITY_EXPONENT)
94 	    && Dbl_isnotzero_mantissa(rightp1,rightp2)) )
95 	    {
96 	    /* NaNs always compare unordered. */
97 	    Set_status_cbit(Unordered(cond));
98 	    return(NOEXCEPTION);
99 	    }
100 	/* infinities will drop down to the normal compare mechanisms */
101 	}
102     /* First compare for unequal signs => less or greater or
103      * special equal case */
104     Dbl_xortointp1(leftp1,rightp1,xorresult);
105     if( xorresult < 0 )
106 	{
107 	/* left negative => less, left positive => greater.
108 	 * equal is possible if both operands are zeros. */
109 	if( Dbl_iszero_exponentmantissa(leftp1,leftp2)
110 	  && Dbl_iszero_exponentmantissa(rightp1,rightp2) )
111 	    {
112 	    Set_status_cbit(Equal(cond));
113 	    }
114 	else if( Dbl_isone_sign(leftp1) )
115 	    {
116 	    Set_status_cbit(Lessthan(cond));
117 	    }
118 	else
119 	    {
120 	    Set_status_cbit(Greaterthan(cond));
121 	    }
122 	}
123     /* Signs are the same.  Treat negative numbers separately
124      * from the positives because of the reversed sense.  */
125     else if(Dbl_isequal(leftp1,leftp2,rightp1,rightp2))
126 	{
127 	Set_status_cbit(Equal(cond));
128 	}
129     else if( Dbl_iszero_sign(leftp1) )
130 	{
131 	/* Positive compare */
132 	if( Dbl_allp1(leftp1) < Dbl_allp1(rightp1) )
133 	    {
134 	    Set_status_cbit(Lessthan(cond));
135 	    }
136 	else if( Dbl_allp1(leftp1) > Dbl_allp1(rightp1) )
137 	    {
138 	    Set_status_cbit(Greaterthan(cond));
139 	    }
140 	else
141 	    {
142 	    /* Equal first parts.  Now we must use unsigned compares to
143 	     * resolve the two possibilities. */
144 	    if( Dbl_allp2(leftp2) < Dbl_allp2(rightp2) )
145 		{
146 		Set_status_cbit(Lessthan(cond));
147 		}
148 	    else
149 		{
150 		Set_status_cbit(Greaterthan(cond));
151 		}
152 	    }
153 	}
154     else
155 	{
156 	/* Negative compare.  Signed or unsigned compares
157  	 * both work the same.  That distinction is only
158  	 * important when the sign bits differ. */
159 	if( Dbl_allp1(leftp1) > Dbl_allp1(rightp1) )
160 	    {
161 	    Set_status_cbit(Lessthan(cond));
162 	    }
163 	else if( Dbl_allp1(leftp1) < Dbl_allp1(rightp1) )
164 	    {
165 	    Set_status_cbit(Greaterthan(cond));
166 	    }
167 	else
168 	    {
169 	    /* Equal first parts.  Now we must use unsigned compares to
170 	     * resolve the two possibilities. */
171 	    if( Dbl_allp2(leftp2) > Dbl_allp2(rightp2) )
172 		{
173 		Set_status_cbit(Lessthan(cond));
174 		}
175 	    else
176 		{
177 		Set_status_cbit(Greaterthan(cond));
178 		}
179 	    }
180 	}
181 	return(NOEXCEPTION);
182     }
183