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