2 * Linux/PA-RISC Project (http://www.parisc-linux.org/)
4 * Floating-point emulation code
5 * Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * @(#) pa/spmath/dfsub.c $Revision: 1.1 $
28 * Double_subtract: subtract two double precision values.
30 * External Interfaces:
31 * dbl_fsub(leftptr, rightptr, dstptr, status)
33 * Internal Interfaces:
36 * <<please update with a overview of the operation of this file>>
43 #include "dbl_float.h"
46 * Double_subtract: subtract two double precision values.
50 dbl_floating_point *leftptr,
51 dbl_floating_point *rightptr,
52 dbl_floating_point *dstptr,
55 register unsigned int signless_upper_left, signless_upper_right, save;
56 register unsigned int leftp1, leftp2, rightp1, rightp2, extent;
57 register unsigned int resultp1 = 0, resultp2 = 0;
59 register int result_exponent, right_exponent, diff_exponent;
60 register int sign_save, jumpsize;
61 register boolean inexact = FALSE, underflowtrap;
63 /* Create local copies of the numbers */
64 Dbl_copyfromptr(leftptr,leftp1,leftp2);
65 Dbl_copyfromptr(rightptr,rightp1,rightp2);
67 /* A zero "save" helps discover equal operands (for later), *
68 * and is used in swapping operands (if needed). */
69 Dbl_xortointp1(leftp1,rightp1,/*to*/save);
72 * check first operand for NaN's or infinity
74 if ((result_exponent = Dbl_exponent(leftp1)) == DBL_INFINITY_EXPONENT)
76 if (Dbl_iszero_mantissa(leftp1,leftp2))
78 if (Dbl_isnotnan(rightp1,rightp2))
80 if (Dbl_isinfinity(rightp1,rightp2) && save==0)
83 * invalid since operands are same signed infinity's
85 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
87 Dbl_makequietnan(resultp1,resultp2);
88 Dbl_copytoptr(resultp1,resultp2,dstptr);
94 Dbl_copytoptr(leftp1,leftp2,dstptr);
101 * is NaN; signaling or quiet?
103 if (Dbl_isone_signaling(leftp1))
105 /* trap if INVALIDTRAP enabled */
106 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
109 Dbl_set_quiet(leftp1);
112 * is second operand a signaling NaN?
114 else if (Dbl_is_signalingnan(rightp1))
116 /* trap if INVALIDTRAP enabled */
117 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
120 Dbl_set_quiet(rightp1);
121 Dbl_copytoptr(rightp1,rightp2,dstptr);
127 Dbl_copytoptr(leftp1,leftp2,dstptr);
130 } /* End left NaN or Infinity processing */
132 * check second operand for NaN's or infinity
134 if (Dbl_isinfinity_exponent(rightp1))
136 if (Dbl_iszero_mantissa(rightp1,rightp2))
138 /* return infinity */
139 Dbl_invert_sign(rightp1);
140 Dbl_copytoptr(rightp1,rightp2,dstptr);
144 * is NaN; signaling or quiet?
146 if (Dbl_isone_signaling(rightp1))
148 /* trap if INVALIDTRAP enabled */
149 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
152 Dbl_set_quiet(rightp1);
157 Dbl_copytoptr(rightp1,rightp2,dstptr);
159 } /* End right NaN or Infinity processing */
161 /* Invariant: Must be dealing with finite numbers */
163 /* Compare operands by removing the sign */
164 Dbl_copytoint_exponentmantissap1(leftp1,signless_upper_left);
165 Dbl_copytoint_exponentmantissap1(rightp1,signless_upper_right);
167 /* sign difference selects add or sub operation. */
168 if(Dbl_ismagnitudeless(leftp2,rightp2,signless_upper_left,signless_upper_right))
170 /* Set the left operand to the larger one by XOR swap *
171 * First finish the first word using "save" */
172 Dbl_xorfromintp1(save,rightp1,/*to*/rightp1);
173 Dbl_xorfromintp1(save,leftp1,/*to*/leftp1);
174 Dbl_swap_lower(leftp2,rightp2);
175 result_exponent = Dbl_exponent(leftp1);
176 Dbl_invert_sign(leftp1);
178 /* Invariant: left is not smaller than right. */
180 if((right_exponent = Dbl_exponent(rightp1)) == 0)
182 /* Denormalized operands. First look for zeroes */
183 if(Dbl_iszero_mantissa(rightp1,rightp2))
186 if(Dbl_iszero_exponentmantissa(leftp1,leftp2))
188 /* Both operands are zeros */
189 Dbl_invert_sign(rightp1);
190 if(Is_rounding_mode(ROUNDMINUS))
192 Dbl_or_signs(leftp1,/*with*/rightp1);
196 Dbl_and_signs(leftp1,/*with*/rightp1);
201 /* Left is not a zero and must be the result. Trapped
202 * underflows are signaled if left is denormalized. Result
203 * is always exact. */
204 if( (result_exponent == 0) && Is_underflowtrap_enabled() )
206 /* need to normalize results mantissa */
207 sign_save = Dbl_signextendedsign(leftp1);
208 Dbl_leftshiftby1(leftp1,leftp2);
209 Dbl_normalize(leftp1,leftp2,result_exponent);
210 Dbl_set_sign(leftp1,/*using*/sign_save);
211 Dbl_setwrapped_exponent(leftp1,result_exponent,unfl);
212 Dbl_copytoptr(leftp1,leftp2,dstptr);
213 /* inexact = FALSE */
214 return(UNDERFLOWEXCEPTION);
217 Dbl_copytoptr(leftp1,leftp2,dstptr);
221 /* Neither are zeroes */
222 Dbl_clear_sign(rightp1); /* Exponent is already cleared */
223 if(result_exponent == 0 )
225 /* Both operands are denormalized. The result must be exact
226 * and is simply calculated. A sum could become normalized and a
227 * difference could cancel to a true zero. */
228 if( (/*signed*/int) save >= 0 )
230 Dbl_subtract(leftp1,leftp2,/*minus*/rightp1,rightp2,
231 /*into*/resultp1,resultp2);
232 if(Dbl_iszero_mantissa(resultp1,resultp2))
234 if(Is_rounding_mode(ROUNDMINUS))
236 Dbl_setone_sign(resultp1);
240 Dbl_setzero_sign(resultp1);
242 Dbl_copytoptr(resultp1,resultp2,dstptr);
248 Dbl_addition(leftp1,leftp2,rightp1,rightp2,
249 /*into*/resultp1,resultp2);
250 if(Dbl_isone_hidden(resultp1))
252 Dbl_copytoptr(resultp1,resultp2,dstptr);
256 if(Is_underflowtrap_enabled())
258 /* need to normalize result */
259 sign_save = Dbl_signextendedsign(resultp1);
260 Dbl_leftshiftby1(resultp1,resultp2);
261 Dbl_normalize(resultp1,resultp2,result_exponent);
262 Dbl_set_sign(resultp1,/*using*/sign_save);
263 Dbl_setwrapped_exponent(resultp1,result_exponent,unfl);
264 Dbl_copytoptr(resultp1,resultp2,dstptr);
265 /* inexact = FALSE */
266 return(UNDERFLOWEXCEPTION);
268 Dbl_copytoptr(resultp1,resultp2,dstptr);
271 right_exponent = 1; /* Set exponent to reflect different bias
272 * with denomalized numbers. */
276 Dbl_clear_signexponent_set_hidden(rightp1);
278 Dbl_clear_exponent_set_hidden(leftp1);
279 diff_exponent = result_exponent - right_exponent;
282 * Special case alignment of operands that would force alignment
283 * beyond the extent of the extension. A further optimization
284 * could special case this but only reduces the path length for this
287 if(diff_exponent > DBL_THRESHOLD)
289 diff_exponent = DBL_THRESHOLD;
292 /* Align right operand by shifting to right */
293 Dbl_right_align(/*operand*/rightp1,rightp2,/*shifted by*/diff_exponent,
294 /*and lower to*/extent);
296 /* Treat sum and difference of the operands separately. */
297 if( (/*signed*/int) save >= 0 )
300 * Difference of the two operands. Their can be no overflow. A
301 * borrow can occur out of the hidden bit and force a post
302 * normalization phase.
304 Dbl_subtract_withextension(leftp1,leftp2,/*minus*/rightp1,rightp2,
305 /*with*/extent,/*into*/resultp1,resultp2);
306 if(Dbl_iszero_hidden(resultp1))
308 /* Handle normalization */
309 /* A straight foward algorithm would now shift the result
310 * and extension left until the hidden bit becomes one. Not
311 * all of the extension bits need participate in the shift.
312 * Only the two most significant bits (round and guard) are
313 * needed. If only a single shift is needed then the guard
314 * bit becomes a significant low order bit and the extension
315 * must participate in the rounding. If more than a single
316 * shift is needed, then all bits to the right of the guard
317 * bit are zeros, and the guard bit may or may not be zero. */
318 sign_save = Dbl_signextendedsign(resultp1);
319 Dbl_leftshiftby1_withextent(resultp1,resultp2,extent,resultp1,resultp2);
321 /* Need to check for a zero result. The sign and exponent
322 * fields have already been zeroed. The more efficient test
323 * of the full object can be used.
325 if(Dbl_iszero(resultp1,resultp2))
326 /* Must have been "x-x" or "x+(-x)". */
328 if(Is_rounding_mode(ROUNDMINUS)) Dbl_setone_sign(resultp1);
329 Dbl_copytoptr(resultp1,resultp2,dstptr);
333 /* Look to see if normalization is finished. */
334 if(Dbl_isone_hidden(resultp1))
336 if(result_exponent==0)
338 /* Denormalized, exponent should be zero. Left operand *
339 * was normalized, so extent (guard, round) was zero */
344 /* No further normalization is needed. */
345 Dbl_set_sign(resultp1,/*using*/sign_save);
346 Ext_leftshiftby1(extent);
351 /* Check for denormalized, exponent should be zero. Left *
352 * operand was normalized, so extent (guard, round) was zero */
353 if(!(underflowtrap = Is_underflowtrap_enabled()) &&
354 result_exponent==0) goto underflow;
356 /* Shift extension to complete one bit of normalization and
357 * update exponent. */
358 Ext_leftshiftby1(extent);
360 /* Discover first one bit to determine shift amount. Use a
361 * modified binary search. We have already shifted the result
362 * one position right and still not found a one so the remainder
363 * of the extension must be zero and simplifies rounding. */
365 while(Dbl_iszero_hiddenhigh7mantissa(resultp1))
367 Dbl_leftshiftby8(resultp1,resultp2);
368 if((result_exponent -= 8) <= 0 && !underflowtrap)
371 /* Now narrow it down to the nibble */
372 if(Dbl_iszero_hiddenhigh3mantissa(resultp1))
374 /* The lower nibble contains the normalizing one */
375 Dbl_leftshiftby4(resultp1,resultp2);
376 if((result_exponent -= 4) <= 0 && !underflowtrap)
379 /* Select case were first bit is set (already normalized)
380 * otherwise select the proper shift. */
381 if((jumpsize = Dbl_hiddenhigh3mantissa(resultp1)) > 7)
383 /* Already normalized */
384 if(result_exponent <= 0) goto underflow;
385 Dbl_set_sign(resultp1,/*using*/sign_save);
386 Dbl_set_exponent(resultp1,/*using*/result_exponent);
387 Dbl_copytoptr(resultp1,resultp2,dstptr);
390 Dbl_sethigh4bits(resultp1,/*using*/sign_save);
395 Dbl_leftshiftby3(resultp1,resultp2);
396 result_exponent -= 3;
402 Dbl_leftshiftby2(resultp1,resultp2);
403 result_exponent -= 2;
411 Dbl_leftshiftby1(resultp1,resultp2);
412 result_exponent -= 1;
416 if(result_exponent > 0)
418 Dbl_set_exponent(resultp1,/*using*/result_exponent);
419 Dbl_copytoptr(resultp1,resultp2,dstptr);
420 return(NOEXCEPTION); /* Sign bit is already set */
422 /* Fixup potential underflows */
424 if(Is_underflowtrap_enabled())
426 Dbl_set_sign(resultp1,sign_save);
427 Dbl_setwrapped_exponent(resultp1,result_exponent,unfl);
428 Dbl_copytoptr(resultp1,resultp2,dstptr);
429 /* inexact = FALSE */
430 return(UNDERFLOWEXCEPTION);
433 * Since we cannot get an inexact denormalized result,
436 Dbl_fix_overshift(resultp1,resultp2,(1-result_exponent),extent);
437 Dbl_clear_signexponent(resultp1);
438 Dbl_set_sign(resultp1,sign_save);
439 Dbl_copytoptr(resultp1,resultp2,dstptr);
441 } /* end if(hidden...)... */
442 /* Fall through and round */
443 } /* end if(save >= 0)... */
446 /* Subtract magnitudes */
447 Dbl_addition(leftp1,leftp2,rightp1,rightp2,/*to*/resultp1,resultp2);
448 if(Dbl_isone_hiddenoverflow(resultp1))
450 /* Prenormalization required. */
451 Dbl_rightshiftby1_withextent(resultp2,extent,extent);
452 Dbl_arithrightshiftby1(resultp1,resultp2);
454 } /* end if hiddenoverflow... */
455 } /* end else ...subtract magnitudes... */
457 /* Round the result. If the extension is all zeros,then the result is
458 * exact. Otherwise round in the correct direction. No underflow is
459 * possible. If a postnormalization is necessary, then the mantissa is
460 * all zeros so no shift is needed. */
462 if(Ext_isnotzero(extent))
465 switch(Rounding_mode())
467 case ROUNDNEAREST: /* The default. */
468 if(Ext_isone_sign(extent))
470 /* at least 1/2 ulp */
471 if(Ext_isnotzero_lower(extent) ||
472 Dbl_isone_lowmantissap2(resultp2))
474 /* either exactly half way and odd or more than 1/2ulp */
475 Dbl_increment(resultp1,resultp2);
481 if(Dbl_iszero_sign(resultp1))
483 /* Round up positive results */
484 Dbl_increment(resultp1,resultp2);
489 if(Dbl_isone_sign(resultp1))
491 /* Round down negative results */
492 Dbl_increment(resultp1,resultp2);
496 /* truncate is simple */
497 } /* end switch... */
498 if(Dbl_isone_hiddenoverflow(resultp1)) result_exponent++;
500 if(result_exponent == DBL_INFINITY_EXPONENT)
503 if(Is_overflowtrap_enabled())
505 Dbl_setwrapped_exponent(resultp1,result_exponent,ovfl);
506 Dbl_copytoptr(resultp1,resultp2,dstptr);
508 if (Is_inexacttrap_enabled())
509 return(OVERFLOWEXCEPTION | INEXACTEXCEPTION);
510 else Set_inexactflag();
511 return(OVERFLOWEXCEPTION);
517 Dbl_setoverflow(resultp1,resultp2);
520 else Dbl_set_exponent(resultp1,result_exponent);
521 Dbl_copytoptr(resultp1,resultp2,dstptr);
523 if(Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
524 else Set_inexactflag();