1 /* $NetBSD: sfcmp.c,v 1.1.10.1 2004/08/03 10:35:38 skrll Exp $ */
3 /* $OpenBSD: sfcmp.c,v 1.4 2001/03/29 03:58:19 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.
30 * (c) Copyright 1986 HEWLETT-PACKARD COMPANY
32 * To anyone who acknowledges that this file is provided "AS IS"
33 * without any express or implied warranty:
34 * permission to use, copy, modify, and distribute this file
35 * for any purpose is hereby granted without fee, provided that
36 * the above copyright notice and this notice appears in all
37 * copies, and that the name of Hewlett-Packard Company not be
38 * used in advertising or publicity pertaining to distribution
39 * of the software without specific, written prior permission.
40 * Hewlett-Packard Company makes no representations about the
41 * suitability of this software for any purpose.
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: sfcmp.c,v 1.1.10.1 2004/08/03 10:35:38 skrll Exp $");
47 #include "../spmath/float.h"
48 #include "../spmath/sgl_float.h"
51 * sgl_cmp: compare two values
54 sgl_fcmp(leftptr
, rightptr
, cond
, status
)
55 sgl_floating_point
*leftptr
, *rightptr
;
56 unsigned int cond
; /* The predicate to be tested */
59 register unsigned int left
, right
;
60 register int xorresult
;
62 /* Create local copies of the numbers */
68 if( (Sgl_exponent(left
) == SGL_INFINITY_EXPONENT
)
69 || (Sgl_exponent(right
) == SGL_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( ( (Sgl_exponent(left
) == SGL_INFINITY_EXPONENT
)
75 && Sgl_isnotzero_mantissa(left
)
76 && (Exception(cond
) || Sgl_isone_signaling(left
)))
78 ( (Sgl_exponent(right
) == SGL_INFINITY_EXPONENT
)
79 && Sgl_isnotzero_mantissa(right
)
80 && (Exception(cond
) || Sgl_isone_signaling(right
)) ) )
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( ((Sgl_exponent(left
) == SGL_INFINITY_EXPONENT
)
93 && Sgl_isnotzero_mantissa(left
))
95 ((Sgl_exponent(right
) == SGL_INFINITY_EXPONENT
)
96 && Sgl_isnotzero_mantissa(right
)) )
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 Sgl_xortointp1(left
,right
,xorresult
);
109 /* left negative => less, left positive => greater.
110 * equal is possible if both operands are zeros. */
111 if( Sgl_iszero_exponentmantissa(left
)
112 && Sgl_iszero_exponentmantissa(right
) )
114 Set_status_cbit(Equal(cond
));
116 else if( Sgl_isone_sign(left
) )
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( Sgl_all(left
) == Sgl_all(right
) )
129 Set_status_cbit(Equal(cond
));
131 else if( Sgl_iszero_sign(left
) )
133 /* Positive compare */
134 if( Sgl_all(left
) < Sgl_all(right
) )
136 Set_status_cbit(Lessthan(cond
));
140 Set_status_cbit(Greaterthan(cond
));
145 /* Negative compare. Signed or unsigned compares
146 * both work the same. That distinction is only
147 * important when the sign bits differ. */
148 if( Sgl_all(left
) > Sgl_all(right
) )
150 Set_status_cbit(Lessthan(cond
));
154 Set_status_cbit(Greaterthan(cond
));