1 /* $NetBSD: dfcmp.c,v 1.1.10.1 2004/08/03 10:35:37 skrll Exp $ */
3 /* $OpenBSD: dfcmp.c,v 1.4 2001/03/29 03:58:17 mickey Exp $ */
6 * Copyright 1996 1995 by Open Software Foundation, Inc.
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.
15 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17 * FOR A PARTICULAR PURPOSE.
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.
29 * (c) Copyright 1986 HEWLETT-PACKARD COMPANY
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.
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: dfcmp.c,v 1.1.10.1 2004/08/03 10:35:37 skrll Exp $");
47 #include "../spmath/float.h"
48 #include "../spmath/dbl_float.h"
51 * dbl_cmp: compare two values
54 dbl_fcmp(leftptr
, rightptr
, cond
, status
)
55 dbl_floating_point
*leftptr
, *rightptr
;
56 unsigned int cond
; /* The predicate to be tested */
59 register unsigned int leftp1
, leftp2
, rightp1
, rightp2
;
60 register int xorresult
;
62 /* Create local copies of the numbers */
63 Dbl_copyfromptr(leftptr
,leftp1
,leftp2
);
64 Dbl_copyfromptr(rightptr
,rightp1
,rightp2
);
68 if( (Dbl_exponent(leftp1
) == DBL_INFINITY_EXPONENT
)
69 || (Dbl_exponent(rightp1
) == DBL_INFINITY_EXPONENT
) )
71 /* Check if a NaN is involved. Signal an invalid exception when
72 * comparing a signaling NaN or when comparing quiet NaNs and the
73 * low bit of the condition is set */
74 if( ((Dbl_exponent(leftp1
) == DBL_INFINITY_EXPONENT
)
75 && Dbl_isnotzero_mantissa(leftp1
,leftp2
)
76 && (Exception(cond
) || Dbl_isone_signaling(leftp1
)))
78 ((Dbl_exponent(rightp1
) == DBL_INFINITY_EXPONENT
)
79 && Dbl_isnotzero_mantissa(rightp1
,rightp2
)
80 && (Exception(cond
) || Dbl_isone_signaling(rightp1
))) )
82 if( Is_invalidtrap_enabled() ) {
83 Set_status_cbit(Unordered(cond
));
84 return(INVALIDEXCEPTION
);
86 else Set_invalidflag();
87 Set_status_cbit(Unordered(cond
));
90 /* All the exceptional conditions are handled, now special case
92 else if( ((Dbl_exponent(leftp1
) == DBL_INFINITY_EXPONENT
)
93 && Dbl_isnotzero_mantissa(leftp1
,leftp2
))
95 ((Dbl_exponent(rightp1
) == DBL_INFINITY_EXPONENT
)
96 && Dbl_isnotzero_mantissa(rightp1
,rightp2
)) )
98 /* NaNs always compare unordered. */
99 Set_status_cbit(Unordered(cond
));
102 /* infinities will drop down to the normal compare mechanisms */
104 /* First compare for unequal signs => less or greater or
105 * special equal case */
106 Dbl_xortointp1(leftp1
,rightp1
,xorresult
);
109 /* left negative => less, left positive => greater.
110 * equal is possible if both operands are zeros. */
111 if( Dbl_iszero_exponentmantissa(leftp1
,leftp2
)
112 && Dbl_iszero_exponentmantissa(rightp1
,rightp2
) )
114 Set_status_cbit(Equal(cond
));
116 else if( Dbl_isone_sign(leftp1
) )
118 Set_status_cbit(Lessthan(cond
));
122 Set_status_cbit(Greaterthan(cond
));
125 /* Signs are the same. Treat negative numbers separately
126 * from the positives because of the reversed sense. */
127 else if(Dbl_isequal(leftp1
,leftp2
,rightp1
,rightp2
))
129 Set_status_cbit(Equal(cond
));
131 else if( Dbl_iszero_sign(leftp1
) )
133 /* Positive compare */
134 if( Dbl_allp1(leftp1
) < Dbl_allp1(rightp1
) )
136 Set_status_cbit(Lessthan(cond
));
138 else if( Dbl_allp1(leftp1
) > Dbl_allp1(rightp1
) )
140 Set_status_cbit(Greaterthan(cond
));
144 /* Equal first parts. Now we must use unsigned compares to
145 * resolve the two possibilities. */
146 if( Dbl_allp2(leftp2
) < Dbl_allp2(rightp2
) )
148 Set_status_cbit(Lessthan(cond
));
152 Set_status_cbit(Greaterthan(cond
));
158 /* Negative compare. Signed or unsigned compares
159 * both work the same. That distinction is only
160 * important when the sign bits differ. */
161 if( Dbl_allp1(leftp1
) > Dbl_allp1(rightp1
) )
163 Set_status_cbit(Lessthan(cond
));
165 else if( Dbl_allp1(leftp1
) < Dbl_allp1(rightp1
) )
167 Set_status_cbit(Greaterthan(cond
));
171 /* Equal first parts. Now we must use unsigned compares to
172 * resolve the two possibilities. */
173 if( Dbl_allp2(leftp2
) > Dbl_allp2(rightp2
) )
175 Set_status_cbit(Lessthan(cond
));
179 Set_status_cbit(Greaterthan(cond
));