1 /* $NetBSD: sfsub.c,v 1.4 2007/02/22 05:46:31 thorpej Exp $ */ 2 3 /* $OpenBSD: sfsub.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: sfsub.c,v 1.4 2007/02/22 05:46:31 thorpej Exp $"); 46 47 #include "../spmath/float.h" 48 #include "../spmath/sgl_float.h" 49 50 /* 51 * Single_subtract: subtract two single precision values. 52 */ 53 int 54 sgl_fsub(leftptr, rightptr, dstptr, status) 55 sgl_floating_point *leftptr, *rightptr, *dstptr; 56 unsigned int *status; 57 { 58 register unsigned int left, right, result, extent; 59 register unsigned int signless_upper_left, signless_upper_right, save; 60 61 register int result_exponent, right_exponent, diff_exponent; 62 register int sign_save, jumpsize; 63 register int inexact = false, underflowtrap; 64 65 /* Create local copies of the numbers */ 66 left = *leftptr; 67 right = *rightptr; 68 69 /* A zero "save" helps discover equal operands (for later), * 70 * and is used in swapping operands (if needed). */ 71 Sgl_xortointp1(left,right,/*to*/save); 72 73 /* 74 * check first operand for NaN's or infinity 75 */ 76 if ((result_exponent = Sgl_exponent(left)) == SGL_INFINITY_EXPONENT) 77 { 78 if (Sgl_iszero_mantissa(left)) 79 { 80 if (Sgl_isnotnan(right)) 81 { 82 if (Sgl_isinfinity(right) && save==0) 83 { 84 /* 85 * invalid since operands are same signed infinity's 86 */ 87 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION); 88 Set_invalidflag(); 89 Sgl_makequietnan(result); 90 *dstptr = result; 91 return(NOEXCEPTION); 92 } 93 /* 94 * return infinity 95 */ 96 *dstptr = left; 97 return(NOEXCEPTION); 98 } 99 } 100 else 101 { 102 /* 103 * is NaN; signaling or quiet? 104 */ 105 if (Sgl_isone_signaling(left)) 106 { 107 /* trap if INVALIDTRAP enabled */ 108 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION); 109 /* make NaN quiet */ 110 Set_invalidflag(); 111 Sgl_set_quiet(left); 112 } 113 /* 114 * is second operand a signaling NaN? 115 */ 116 else if (Sgl_is_signalingnan(right)) 117 { 118 /* trap if INVALIDTRAP enabled */ 119 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION); 120 /* make NaN quiet */ 121 Set_invalidflag(); 122 Sgl_set_quiet(right); 123 *dstptr = right; 124 return(NOEXCEPTION); 125 } 126 /* 127 * return quiet NaN 128 */ 129 *dstptr = left; 130 return(NOEXCEPTION); 131 } 132 } /* End left NaN or Infinity processing */ 133 /* 134 * check second operand for NaN's or infinity 135 */ 136 if (Sgl_isinfinity_exponent(right)) 137 { 138 if (Sgl_iszero_mantissa(right)) 139 { 140 /* return infinity */ 141 Sgl_invert_sign(right); 142 *dstptr = right; 143 return(NOEXCEPTION); 144 } 145 /* 146 * is NaN; signaling or quiet? 147 */ 148 if (Sgl_isone_signaling(right)) 149 { 150 /* trap if INVALIDTRAP enabled */ 151 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION); 152 /* make NaN quiet */ 153 Set_invalidflag(); 154 Sgl_set_quiet(right); 155 } 156 /* 157 * return quiet NaN 158 */ 159 *dstptr = right; 160 return(NOEXCEPTION); 161 } /* End right NaN or Infinity processing */ 162 163 /* Invariant: Must be dealing with finite numbers */ 164 165 /* Compare operands by removing the sign */ 166 Sgl_copytoint_exponentmantissa(left,signless_upper_left); 167 Sgl_copytoint_exponentmantissa(right,signless_upper_right); 168 169 /* sign difference selects sub or add operation. */ 170 if(Sgl_ismagnitudeless(signless_upper_left,signless_upper_right)) 171 { 172 /* Set the left operand to the larger one by XOR swap * 173 * First finish the first word using "save" */ 174 Sgl_xorfromintp1(save,right,/*to*/right); 175 Sgl_xorfromintp1(save,left,/*to*/left); 176 result_exponent = Sgl_exponent(left); 177 Sgl_invert_sign(left); 178 } 179 /* Invariant: left is not smaller than right. */ 180 181 if((right_exponent = Sgl_exponent(right)) == 0) 182 { 183 /* Denormalized operands. First look for zeroes */ 184 if(Sgl_iszero_mantissa(right)) 185 { 186 /* right is zero */ 187 if(Sgl_iszero_exponentmantissa(left)) 188 { 189 /* Both operands are zeros */ 190 Sgl_invert_sign(right); 191 if(Is_rounding_mode(ROUNDMINUS)) 192 { 193 Sgl_or_signs(left,/*with*/right); 194 } 195 else 196 { 197 Sgl_and_signs(left,/*with*/right); 198 } 199 } 200 else 201 { 202 /* Left is not a zero and must be the result. Trapped 203 * underflows are signaled if left is denormalized. Result 204 * is always exact. */ 205 if( (result_exponent == 0) && Is_underflowtrap_enabled() ) 206 { 207 /* need to normalize results mantissa */ 208 sign_save = Sgl_signextendedsign(left); 209 Sgl_leftshiftby1(left); 210 Sgl_normalize(left,result_exponent); 211 Sgl_set_sign(left,/*using*/sign_save); 212 Sgl_setwrapped_exponent(left,result_exponent,unfl); 213 *dstptr = left; 214 /* inexact = false */ 215 return(UNDERFLOWEXCEPTION); 216 } 217 } 218 *dstptr = left; 219 return(NOEXCEPTION); 220 } 221 222 /* Neither are zeroes */ 223 Sgl_clear_sign(right); /* Exponent is already cleared */ 224 if(result_exponent == 0 ) 225 { 226 /* Both operands are denormalized. The result must be exact 227 * and is simply calculated. A sum could become normalized and a 228 * difference could cancel to a true zero. */ 229 if( (/*signed*/int) save >= 0 ) 230 { 231 Sgl_subtract(left,/*minus*/right,/*into*/result); 232 if(Sgl_iszero_mantissa(result)) 233 { 234 if(Is_rounding_mode(ROUNDMINUS)) 235 { 236 Sgl_setone_sign(result); 237 } 238 else 239 { 240 Sgl_setzero_sign(result); 241 } 242 *dstptr = result; 243 return(NOEXCEPTION); 244 } 245 } 246 else 247 { 248 Sgl_addition(left,right,/*into*/result); 249 if(Sgl_isone_hidden(result)) 250 { 251 *dstptr = result; 252 return(NOEXCEPTION); 253 } 254 } 255 if(Is_underflowtrap_enabled()) 256 { 257 /* need to normalize result */ 258 sign_save = Sgl_signextendedsign(result); 259 Sgl_leftshiftby1(result); 260 Sgl_normalize(result,result_exponent); 261 Sgl_set_sign(result,/*using*/sign_save); 262 Sgl_setwrapped_exponent(result,result_exponent,unfl); 263 *dstptr = result; 264 /* inexact = false */ 265 return(UNDERFLOWEXCEPTION); 266 } 267 *dstptr = result; 268 return(NOEXCEPTION); 269 } 270 right_exponent = 1; /* Set exponent to reflect different bias 271 * with denomalized numbers. */ 272 } 273 else 274 { 275 Sgl_clear_signexponent_set_hidden(right); 276 } 277 Sgl_clear_exponent_set_hidden(left); 278 diff_exponent = result_exponent - right_exponent; 279 280 /* 281 * Special case alignment of operands that would force alignment 282 * beyond the extent of the extension. A further optimization 283 * could special case this but only reduces the path length for this 284 * infrequent case. 285 */ 286 if(diff_exponent > SGL_THRESHOLD) 287 { 288 diff_exponent = SGL_THRESHOLD; 289 } 290 291 /* Align right operand by shifting to right */ 292 Sgl_right_align(/*operand*/right,/*shifted by*/diff_exponent, 293 /*and lower to*/extent); 294 295 /* Treat sum and difference of the operands separately. */ 296 if( (/*signed*/int) save >= 0 ) 297 { 298 /* 299 * Difference of the two operands. Their can be no overflow. A 300 * borrow can occur out of the hidden bit and force a post 301 * normalization phase. 302 */ 303 Sgl_subtract_withextension(left,/*minus*/right,/*with*/extent,/*into*/result); 304 if(Sgl_iszero_hidden(result)) 305 { 306 /* Handle normalization */ 307 /* A straight foward algorithm would now shift the result 308 * and extension left until the hidden bit becomes one. Not 309 * all of the extension bits need participate in the shift. 310 * Only the two most significant bits (round and guard) are 311 * needed. If only a single shift is needed then the guard 312 * bit becomes a significant low order bit and the extension 313 * must participate in the rounding. If more than a single 314 * shift is needed, then all bits to the right of the guard 315 * bit are zeros, and the guard bit may or may not be zero. */ 316 sign_save = Sgl_signextendedsign(result); 317 Sgl_leftshiftby1_withextent(result,extent,result); 318 319 /* Need to check for a zero result. The sign and exponent 320 * fields have already been zeroed. The more efficient test 321 * of the full object can be used. 322 */ 323 if(Sgl_iszero(result)) 324 /* Must have been "x-x" or "x+(-x)". */ 325 { 326 if(Is_rounding_mode(ROUNDMINUS)) Sgl_setone_sign(result); 327 *dstptr = result; 328 return(NOEXCEPTION); 329 } 330 result_exponent--; 331 /* Look to see if normalization is finished. */ 332 if(Sgl_isone_hidden(result)) 333 { 334 if(result_exponent==0) 335 { 336 /* Denormalized, exponent should be zero. Left operand * 337 * was normalized, so extent (guard, round) was zero */ 338 goto underflow; 339 } 340 else 341 { 342 /* No further normalization is needed. */ 343 Sgl_set_sign(result,/*using*/sign_save); 344 Ext_leftshiftby1(extent); 345 goto round; 346 } 347 } 348 349 /* Check for denormalized, exponent should be zero. Left * 350 * operand was normalized, so extent (guard, round) was zero */ 351 if(!(underflowtrap = Is_underflowtrap_enabled()) && 352 result_exponent==0) goto underflow; 353 354 /* Shift extension to complete one bit of normalization and 355 * update exponent. */ 356 Ext_leftshiftby1(extent); 357 358 /* Discover first one bit to determine shift amount. Use a 359 * modified binary search. We have already shifted the result 360 * one position right and still not found a one so the remainder 361 * of the extension must be zero and simplifies rounding. */ 362 /* Scan bytes */ 363 while(Sgl_iszero_hiddenhigh7mantissa(result)) 364 { 365 Sgl_leftshiftby8(result); 366 if((result_exponent -= 8) <= 0 && !underflowtrap) 367 goto underflow; 368 } 369 /* Now narrow it down to the nibble */ 370 if(Sgl_iszero_hiddenhigh3mantissa(result)) 371 { 372 /* The lower nibble contains the normalizing one */ 373 Sgl_leftshiftby4(result); 374 if((result_exponent -= 4) <= 0 && !underflowtrap) 375 goto underflow; 376 } 377 /* Select case were first bit is set (already normalized) 378 * otherwise select the proper shift. */ 379 if((jumpsize = Sgl_hiddenhigh3mantissa(result)) > 7) 380 { 381 /* Already normalized */ 382 if(result_exponent <= 0) goto underflow; 383 Sgl_set_sign(result,/*using*/sign_save); 384 Sgl_set_exponent(result,/*using*/result_exponent); 385 *dstptr = result; 386 return(NOEXCEPTION); 387 } 388 Sgl_sethigh4bits(result,/*using*/sign_save); 389 switch(jumpsize) 390 { 391 case 1: 392 { 393 Sgl_leftshiftby3(result); 394 result_exponent -= 3; 395 break; 396 } 397 case 2: 398 case 3: 399 { 400 Sgl_leftshiftby2(result); 401 result_exponent -= 2; 402 break; 403 } 404 case 4: 405 case 5: 406 case 6: 407 case 7: 408 { 409 Sgl_leftshiftby1(result); 410 result_exponent -= 1; 411 break; 412 } 413 } 414 if(result_exponent > 0) 415 { 416 Sgl_set_exponent(result,/*using*/result_exponent); 417 *dstptr = result; /* Sign bit is already set */ 418 return(NOEXCEPTION); 419 } 420 /* Fixup potential underflows */ 421 underflow: 422 if(Is_underflowtrap_enabled()) 423 { 424 Sgl_set_sign(result,sign_save); 425 Sgl_setwrapped_exponent(result,result_exponent,unfl); 426 *dstptr = result; 427 /* inexact = false */ 428 return(UNDERFLOWEXCEPTION); 429 } 430 /* 431 * Since we cannot get an inexact denormalized result, 432 * we can now return. 433 */ 434 Sgl_right_align(result,/*by*/(1-result_exponent),extent); 435 Sgl_clear_signexponent(result); 436 Sgl_set_sign(result,sign_save); 437 *dstptr = result; 438 return(NOEXCEPTION); 439 } /* end if(hidden...)... */ 440 /* Fall through and round */ 441 } /* end if(save >= 0)... */ 442 else 443 { 444 /* Add magnitudes */ 445 Sgl_addition(left,right,/*to*/result); 446 if(Sgl_isone_hiddenoverflow(result)) 447 { 448 /* Prenormalization required. */ 449 Sgl_rightshiftby1_withextent(result,extent,extent); 450 Sgl_arithrightshiftby1(result); 451 result_exponent++; 452 } /* end if hiddenoverflow... */ 453 } /* end else ...sub magnitudes... */ 454 455 /* Round the result. If the extension is all zeros,then the result is 456 * exact. Otherwise round in the correct direction. No underflow is 457 * possible. If a postnormalization is necessary, then the mantissa is 458 * all zeros so no shift is needed. */ 459 round: 460 if(Ext_isnotzero(extent)) 461 { 462 inexact = true; 463 switch(Rounding_mode()) 464 { 465 case ROUNDNEAREST: /* The default. */ 466 if(Ext_isone_sign(extent)) 467 { 468 /* at least 1/2 ulp */ 469 if(Ext_isnotzero_lower(extent) || 470 Sgl_isone_lowmantissa(result)) 471 { 472 /* either exactly half way and odd or more than 1/2ulp */ 473 Sgl_increment(result); 474 } 475 } 476 break; 477 478 case ROUNDPLUS: 479 if(Sgl_iszero_sign(result)) 480 { 481 /* Round up positive results */ 482 Sgl_increment(result); 483 } 484 break; 485 486 case ROUNDMINUS: 487 if(Sgl_isone_sign(result)) 488 { 489 /* Round down negative results */ 490 Sgl_increment(result); 491 } 492 493 case ROUNDZERO:; 494 /* truncate is simple */ 495 } /* end switch... */ 496 if(Sgl_isone_hiddenoverflow(result)) result_exponent++; 497 } 498 if(result_exponent == SGL_INFINITY_EXPONENT) 499 { 500 /* Overflow */ 501 if(Is_overflowtrap_enabled()) 502 { 503 Sgl_setwrapped_exponent(result,result_exponent,ovfl); 504 *dstptr = result; 505 if (inexact) { 506 if (Is_inexacttrap_enabled()) 507 return(OVERFLOWEXCEPTION | INEXACTEXCEPTION); 508 else Set_inexactflag(); 509 } 510 return(OVERFLOWEXCEPTION); 511 } 512 else 513 { 514 Set_overflowflag(); 515 inexact = true; 516 Sgl_setoverflow(result); 517 } 518 } 519 else Sgl_set_exponent(result,result_exponent); 520 *dstptr = result; 521 if(inexact) { 522 if(Is_inexacttrap_enabled()) return(INEXACTEXCEPTION); 523 else Set_inexactflag(); 524 } 525 return(NOEXCEPTION); 526 } 527