1 /* $NetBSD: dfcmp.c,v 1.3 2005/12/11 12:17:40 christos 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.3 2005/12/11 12:17:40 christos Exp $"); 46 47 #include "../spmath/float.h" 48 #include "../spmath/dbl_float.h" 49 50 /* 51 * dbl_cmp: compare two values 52 */ 53 int 54 dbl_fcmp(leftptr, rightptr, cond, status) 55 dbl_floating_point *leftptr, *rightptr; 56 unsigned int cond; /* The predicate to be tested */ 57 unsigned int *status; 58 { 59 register unsigned int leftp1, leftp2, rightp1, rightp2; 60 register int xorresult; 61 62 /* Create local copies of the numbers */ 63 Dbl_copyfromptr(leftptr,leftp1,leftp2); 64 Dbl_copyfromptr(rightptr,rightp1,rightp2); 65 /* 66 * Test for NaN 67 */ 68 if( (Dbl_exponent(leftp1) == DBL_INFINITY_EXPONENT) 69 || (Dbl_exponent(rightp1) == DBL_INFINITY_EXPONENT) ) 70 { 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))) 77 || 78 ((Dbl_exponent(rightp1) == DBL_INFINITY_EXPONENT) 79 && Dbl_isnotzero_mantissa(rightp1,rightp2) 80 && (Exception(cond) || Dbl_isone_signaling(rightp1))) ) 81 { 82 if( Is_invalidtrap_enabled() ) { 83 Set_status_cbit(Unordered(cond)); 84 return(INVALIDEXCEPTION); 85 } 86 else Set_invalidflag(); 87 Set_status_cbit(Unordered(cond)); 88 return(NOEXCEPTION); 89 } 90 /* All the exceptional conditions are handled, now special case 91 NaN compares */ 92 else if( ((Dbl_exponent(leftp1) == DBL_INFINITY_EXPONENT) 93 && Dbl_isnotzero_mantissa(leftp1,leftp2)) 94 || 95 ((Dbl_exponent(rightp1) == DBL_INFINITY_EXPONENT) 96 && Dbl_isnotzero_mantissa(rightp1,rightp2)) ) 97 { 98 /* NaNs always compare unordered. */ 99 Set_status_cbit(Unordered(cond)); 100 return(NOEXCEPTION); 101 } 102 /* infinities will drop down to the normal compare mechanisms */ 103 } 104 /* First compare for unequal signs => less or greater or 105 * special equal case */ 106 Dbl_xortointp1(leftp1,rightp1,xorresult); 107 if( xorresult < 0 ) 108 { 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) ) 113 { 114 Set_status_cbit(Equal(cond)); 115 } 116 else if( Dbl_isone_sign(leftp1) ) 117 { 118 Set_status_cbit(Lessthan(cond)); 119 } 120 else 121 { 122 Set_status_cbit(Greaterthan(cond)); 123 } 124 } 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)) 128 { 129 Set_status_cbit(Equal(cond)); 130 } 131 else if( Dbl_iszero_sign(leftp1) ) 132 { 133 /* Positive compare */ 134 if( Dbl_allp1(leftp1) < Dbl_allp1(rightp1) ) 135 { 136 Set_status_cbit(Lessthan(cond)); 137 } 138 else if( Dbl_allp1(leftp1) > Dbl_allp1(rightp1) ) 139 { 140 Set_status_cbit(Greaterthan(cond)); 141 } 142 else 143 { 144 /* Equal first parts. Now we must use unsigned compares to 145 * resolve the two possibilities. */ 146 if( Dbl_allp2(leftp2) < Dbl_allp2(rightp2) ) 147 { 148 Set_status_cbit(Lessthan(cond)); 149 } 150 else 151 { 152 Set_status_cbit(Greaterthan(cond)); 153 } 154 } 155 } 156 else 157 { 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) ) 162 { 163 Set_status_cbit(Lessthan(cond)); 164 } 165 else if( Dbl_allp1(leftp1) < Dbl_allp1(rightp1) ) 166 { 167 Set_status_cbit(Greaterthan(cond)); 168 } 169 else 170 { 171 /* Equal first parts. Now we must use unsigned compares to 172 * resolve the two possibilities. */ 173 if( Dbl_allp2(leftp2) > Dbl_allp2(rightp2) ) 174 { 175 Set_status_cbit(Lessthan(cond)); 176 } 177 else 178 { 179 Set_status_cbit(Greaterthan(cond)); 180 } 181 } 182 } 183 return(NOEXCEPTION); 184 } 185