1 /* $OpenBSD: sfrem.c,v 1.4 2001/03/29 03:58:19 mickey Exp $ */ 2 3 /* 4 * Copyright 1996 1995 by Open Software Foundation, Inc. 5 * All Rights Reserved 6 * 7 * Permission to use, copy, modify, and distribute this software and 8 * its documentation for any purpose and without fee is hereby granted, 9 * provided that the above copyright notice appears in all copies and 10 * that both the copyright notice and this permission notice appear in 11 * supporting documentation. 12 * 13 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE 14 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 15 * FOR A PARTICULAR PURPOSE. 16 * 17 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR 18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 19 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT, 20 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 21 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 22 * 23 */ 24 /* 25 * pmk1.1 26 */ 27 /* 28 * (c) Copyright 1986 HEWLETT-PACKARD COMPANY 29 * 30 * To anyone who acknowledges that this file is provided "AS IS" 31 * without any express or implied warranty: 32 * permission to use, copy, modify, and distribute this file 33 * for any purpose is hereby granted without fee, provided that 34 * the above copyright notice and this notice appears in all 35 * copies, and that the name of Hewlett-Packard Company not be 36 * used in advertising or publicity pertaining to distribution 37 * of the software without specific, written prior permission. 38 * Hewlett-Packard Company makes no representations about the 39 * suitability of this software for any purpose. 40 */ 41 42 43 #include "../spmath/float.h" 44 #include "../spmath/sgl_float.h" 45 46 /* 47 * Single Precision Floating-point Remainder 48 */ 49 int 50 sgl_frem(srcptr1,srcptr2,dstptr,status) 51 52 sgl_floating_point *srcptr1, *srcptr2, *dstptr; 53 unsigned int *status; 54 { 55 register unsigned int opnd1, opnd2, result; 56 register int opnd1_exponent, opnd2_exponent, dest_exponent, stepcount; 57 register int roundup = FALSE; 58 59 opnd1 = *srcptr1; 60 opnd2 = *srcptr2; 61 /* 62 * check first operand for NaN's or infinity 63 */ 64 if ((opnd1_exponent = Sgl_exponent(opnd1)) == SGL_INFINITY_EXPONENT) { 65 if (Sgl_iszero_mantissa(opnd1)) { 66 if (Sgl_isnotnan(opnd2)) { 67 /* invalid since first operand is infinity */ 68 if (Is_invalidtrap_enabled()) 69 return(INVALIDEXCEPTION); 70 Set_invalidflag(); 71 Sgl_makequietnan(result); 72 *dstptr = result; 73 return(NOEXCEPTION); 74 } 75 } 76 else { 77 /* 78 * is NaN; signaling or quiet? 79 */ 80 if (Sgl_isone_signaling(opnd1)) { 81 /* trap if INVALIDTRAP enabled */ 82 if (Is_invalidtrap_enabled()) 83 return(INVALIDEXCEPTION); 84 /* make NaN quiet */ 85 Set_invalidflag(); 86 Sgl_set_quiet(opnd1); 87 } 88 /* 89 * is second operand a signaling NaN? 90 */ 91 else if (Sgl_is_signalingnan(opnd2)) { 92 /* trap if INVALIDTRAP enabled */ 93 if (Is_invalidtrap_enabled()) 94 return(INVALIDEXCEPTION); 95 /* make NaN quiet */ 96 Set_invalidflag(); 97 Sgl_set_quiet(opnd2); 98 *dstptr = opnd2; 99 return(NOEXCEPTION); 100 } 101 /* 102 * return quiet NaN 103 */ 104 *dstptr = opnd1; 105 return(NOEXCEPTION); 106 } 107 } 108 /* 109 * check second operand for NaN's or infinity 110 */ 111 if ((opnd2_exponent = Sgl_exponent(opnd2)) == SGL_INFINITY_EXPONENT) { 112 if (Sgl_iszero_mantissa(opnd2)) { 113 /* 114 * return first operand 115 */ 116 *dstptr = opnd1; 117 return(NOEXCEPTION); 118 } 119 /* 120 * is NaN; signaling or quiet? 121 */ 122 if (Sgl_isone_signaling(opnd2)) { 123 /* trap if INVALIDTRAP enabled */ 124 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION); 125 /* make NaN quiet */ 126 Set_invalidflag(); 127 Sgl_set_quiet(opnd2); 128 } 129 /* 130 * return quiet NaN 131 */ 132 *dstptr = opnd2; 133 return(NOEXCEPTION); 134 } 135 /* 136 * check second operand for zero 137 */ 138 if (Sgl_iszero_exponentmantissa(opnd2)) { 139 /* invalid since second operand is zero */ 140 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION); 141 Set_invalidflag(); 142 Sgl_makequietnan(result); 143 *dstptr = result; 144 return(NOEXCEPTION); 145 } 146 147 /* 148 * get sign of result 149 */ 150 result = opnd1; 151 152 /* 153 * check for denormalized operands 154 */ 155 if (opnd1_exponent == 0) { 156 /* check for zero */ 157 if (Sgl_iszero_mantissa(opnd1)) { 158 *dstptr = opnd1; 159 return(NOEXCEPTION); 160 } 161 /* normalize, then continue */ 162 opnd1_exponent = 1; 163 Sgl_normalize(opnd1,opnd1_exponent); 164 } 165 else { 166 Sgl_clear_signexponent_set_hidden(opnd1); 167 } 168 if (opnd2_exponent == 0) { 169 /* normalize, then continue */ 170 opnd2_exponent = 1; 171 Sgl_normalize(opnd2,opnd2_exponent); 172 } 173 else { 174 Sgl_clear_signexponent_set_hidden(opnd2); 175 } 176 177 /* find result exponent and divide step loop count */ 178 dest_exponent = opnd2_exponent - 1; 179 stepcount = opnd1_exponent - opnd2_exponent; 180 181 /* 182 * check for opnd1/opnd2 < 1 183 */ 184 if (stepcount < 0) { 185 /* 186 * check for opnd1/opnd2 > 1/2 187 * 188 * In this case n will round to 1, so 189 * r = opnd1 - opnd2 190 */ 191 if (stepcount == -1 && Sgl_isgreaterthan(opnd1,opnd2)) { 192 Sgl_all(result) = ~Sgl_all(result); /* set sign */ 193 /* align opnd2 with opnd1 */ 194 Sgl_leftshiftby1(opnd2); 195 Sgl_subtract(opnd2,opnd1,opnd2); 196 /* now normalize */ 197 while (Sgl_iszero_hidden(opnd2)) { 198 Sgl_leftshiftby1(opnd2); 199 dest_exponent--; 200 } 201 Sgl_set_exponentmantissa(result,opnd2); 202 goto testforunderflow; 203 } 204 /* 205 * opnd1/opnd2 <= 1/2 206 * 207 * In this case n will round to zero, so 208 * r = opnd1 209 */ 210 Sgl_set_exponentmantissa(result,opnd1); 211 dest_exponent = opnd1_exponent; 212 goto testforunderflow; 213 } 214 215 /* 216 * Generate result 217 * 218 * Do iterative subtract until remainder is less than operand 2. 219 */ 220 while (stepcount-- > 0 && Sgl_all(opnd1)) { 221 if (Sgl_isnotlessthan(opnd1,opnd2)) 222 Sgl_subtract(opnd1,opnd2,opnd1); 223 Sgl_leftshiftby1(opnd1); 224 } 225 /* 226 * Do last subtract, then determine which way to round if remainder 227 * is exactly 1/2 of opnd2 228 */ 229 if (Sgl_isnotlessthan(opnd1,opnd2)) { 230 Sgl_subtract(opnd1,opnd2,opnd1); 231 roundup = TRUE; 232 } 233 if (stepcount > 0 || Sgl_iszero(opnd1)) { 234 /* division is exact, remainder is zero */ 235 Sgl_setzero_exponentmantissa(result); 236 *dstptr = result; 237 return(NOEXCEPTION); 238 } 239 240 /* 241 * Check for cases where opnd1/opnd2 < n 242 * 243 * In this case the result's sign will be opposite that of 244 * opnd1. The mantissa also needs some correction. 245 */ 246 Sgl_leftshiftby1(opnd1); 247 if (Sgl_isgreaterthan(opnd1,opnd2)) { 248 Sgl_invert_sign(result); 249 Sgl_subtract((opnd2<<1),opnd1,opnd1); 250 } 251 /* check for remainder being exactly 1/2 of opnd2 */ 252 else if (Sgl_isequal(opnd1,opnd2) && roundup) { 253 Sgl_invert_sign(result); 254 } 255 256 /* normalize result's mantissa */ 257 while (Sgl_iszero_hidden(opnd1)) { 258 dest_exponent--; 259 Sgl_leftshiftby1(opnd1); 260 } 261 Sgl_set_exponentmantissa(result,opnd1); 262 263 /* 264 * Test for underflow 265 */ 266 testforunderflow: 267 if (dest_exponent <= 0) { 268 /* trap if UNDERFLOWTRAP enabled */ 269 if (Is_underflowtrap_enabled()) { 270 /* 271 * Adjust bias of result 272 */ 273 Sgl_setwrapped_exponent(result,dest_exponent,unfl); 274 *dstptr = result; 275 /* frem is always exact */ 276 return(UNDERFLOWEXCEPTION); 277 } 278 /* 279 * denormalize result or set to signed zero 280 */ 281 if (dest_exponent >= (1 - SGL_P)) { 282 Sgl_rightshift_exponentmantissa(result,1-dest_exponent); 283 } else { 284 Sgl_setzero_exponentmantissa(result); 285 } 286 } 287 else Sgl_set_exponent(result,dest_exponent); 288 *dstptr = result; 289 return(NOEXCEPTION); 290 } 291