1 /* $NetBSD: sfcmp.c,v 1.3 2005/12/11 12:17:40 christos Exp $ */ 2 3 /* $OpenBSD: sfcmp.c,v 1.4 2001/03/29 03:58:19 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 /* 27 * pmk1.1 28 */ 29 /* 30 * (c) Copyright 1986 HEWLETT-PACKARD COMPANY 31 * 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. 42 */ 43 44 #include <sys/cdefs.h> 45 __KERNEL_RCSID(0, "$NetBSD: sfcmp.c,v 1.3 2005/12/11 12:17:40 christos Exp $"); 46 47 #include "../spmath/float.h" 48 #include "../spmath/sgl_float.h" 49 50 /* 51 * sgl_cmp: compare two values 52 */ 53 int 54 sgl_fcmp(leftptr, rightptr, cond, status) 55 sgl_floating_point *leftptr, *rightptr; 56 unsigned int cond; /* The predicate to be tested */ 57 unsigned int *status; 58 { 59 register unsigned int left, right; 60 register int xorresult; 61 62 /* Create local copies of the numbers */ 63 left = *leftptr; 64 right = *rightptr; 65 /* 66 * Test for NaN 67 */ 68 if( (Sgl_exponent(left) == SGL_INFINITY_EXPONENT) 69 || (Sgl_exponent(right) == SGL_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( ( (Sgl_exponent(left) == SGL_INFINITY_EXPONENT) 75 && Sgl_isnotzero_mantissa(left) 76 && (Exception(cond) || Sgl_isone_signaling(left))) 77 || 78 ( (Sgl_exponent(right) == SGL_INFINITY_EXPONENT) 79 && Sgl_isnotzero_mantissa(right) 80 && (Exception(cond) || Sgl_isone_signaling(right)) ) ) 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( ((Sgl_exponent(left) == SGL_INFINITY_EXPONENT) 93 && Sgl_isnotzero_mantissa(left)) 94 || 95 ((Sgl_exponent(right) == SGL_INFINITY_EXPONENT) 96 && Sgl_isnotzero_mantissa(right)) ) 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 Sgl_xortointp1(left,right,xorresult); 107 if( xorresult < 0 ) 108 { 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) ) 113 { 114 Set_status_cbit(Equal(cond)); 115 } 116 else if( Sgl_isone_sign(left) ) 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( Sgl_all(left) == Sgl_all(right) ) 128 { 129 Set_status_cbit(Equal(cond)); 130 } 131 else if( Sgl_iszero_sign(left) ) 132 { 133 /* Positive compare */ 134 if( Sgl_all(left) < Sgl_all(right) ) 135 { 136 Set_status_cbit(Lessthan(cond)); 137 } 138 else 139 { 140 Set_status_cbit(Greaterthan(cond)); 141 } 142 } 143 else 144 { 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) ) 149 { 150 Set_status_cbit(Lessthan(cond)); 151 } 152 else 153 { 154 Set_status_cbit(Greaterthan(cond)); 155 } 156 } 157 return(NOEXCEPTION); 158 } 159