1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux/PA-RISC Project (http://www.parisc-linux.org/)
5 * Floating-point emulation code
6 * Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
12 * @(#) pa/spmath/dfsub.c $Revision: 1.1 $
15 * Double_subtract: subtract two double precision values.
17 * External Interfaces:
18 * dbl_fsub(leftptr, rightptr, dstptr, status)
20 * Internal Interfaces:
23 * <<please update with a overview of the operation of this file>>
30 #include "dbl_float.h"
33 * Double_subtract: subtract two double precision values.
37 dbl_floating_point
*leftptr
,
38 dbl_floating_point
*rightptr
,
39 dbl_floating_point
*dstptr
,
42 register unsigned int signless_upper_left
, signless_upper_right
, save
;
43 register unsigned int leftp1
, leftp2
, rightp1
, rightp2
, extent
;
44 register unsigned int resultp1
= 0, resultp2
= 0;
46 register int result_exponent
, right_exponent
, diff_exponent
;
47 register int sign_save
, jumpsize
;
48 register boolean inexact
= FALSE
, underflowtrap
;
50 /* Create local copies of the numbers */
51 Dbl_copyfromptr(leftptr
,leftp1
,leftp2
);
52 Dbl_copyfromptr(rightptr
,rightp1
,rightp2
);
54 /* A zero "save" helps discover equal operands (for later), *
55 * and is used in swapping operands (if needed). */
56 Dbl_xortointp1(leftp1
,rightp1
,/*to*/save
);
59 * check first operand for NaN's or infinity
61 if ((result_exponent
= Dbl_exponent(leftp1
)) == DBL_INFINITY_EXPONENT
)
63 if (Dbl_iszero_mantissa(leftp1
,leftp2
))
65 if (Dbl_isnotnan(rightp1
,rightp2
))
67 if (Dbl_isinfinity(rightp1
,rightp2
) && save
==0)
70 * invalid since operands are same signed infinity's
72 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
74 Dbl_makequietnan(resultp1
,resultp2
);
75 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
81 Dbl_copytoptr(leftp1
,leftp2
,dstptr
);
88 * is NaN; signaling or quiet?
90 if (Dbl_isone_signaling(leftp1
))
92 /* trap if INVALIDTRAP enabled */
93 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
96 Dbl_set_quiet(leftp1
);
99 * is second operand a signaling NaN?
101 else if (Dbl_is_signalingnan(rightp1
))
103 /* trap if INVALIDTRAP enabled */
104 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
107 Dbl_set_quiet(rightp1
);
108 Dbl_copytoptr(rightp1
,rightp2
,dstptr
);
114 Dbl_copytoptr(leftp1
,leftp2
,dstptr
);
117 } /* End left NaN or Infinity processing */
119 * check second operand for NaN's or infinity
121 if (Dbl_isinfinity_exponent(rightp1
))
123 if (Dbl_iszero_mantissa(rightp1
,rightp2
))
125 /* return infinity */
126 Dbl_invert_sign(rightp1
);
127 Dbl_copytoptr(rightp1
,rightp2
,dstptr
);
131 * is NaN; signaling or quiet?
133 if (Dbl_isone_signaling(rightp1
))
135 /* trap if INVALIDTRAP enabled */
136 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
139 Dbl_set_quiet(rightp1
);
144 Dbl_copytoptr(rightp1
,rightp2
,dstptr
);
146 } /* End right NaN or Infinity processing */
148 /* Invariant: Must be dealing with finite numbers */
150 /* Compare operands by removing the sign */
151 Dbl_copytoint_exponentmantissap1(leftp1
,signless_upper_left
);
152 Dbl_copytoint_exponentmantissap1(rightp1
,signless_upper_right
);
154 /* sign difference selects add or sub operation. */
155 if(Dbl_ismagnitudeless(leftp2
,rightp2
,signless_upper_left
,signless_upper_right
))
157 /* Set the left operand to the larger one by XOR swap *
158 * First finish the first word using "save" */
159 Dbl_xorfromintp1(save
,rightp1
,/*to*/rightp1
);
160 Dbl_xorfromintp1(save
,leftp1
,/*to*/leftp1
);
161 Dbl_swap_lower(leftp2
,rightp2
);
162 result_exponent
= Dbl_exponent(leftp1
);
163 Dbl_invert_sign(leftp1
);
165 /* Invariant: left is not smaller than right. */
167 if((right_exponent
= Dbl_exponent(rightp1
)) == 0)
169 /* Denormalized operands. First look for zeroes */
170 if(Dbl_iszero_mantissa(rightp1
,rightp2
))
173 if(Dbl_iszero_exponentmantissa(leftp1
,leftp2
))
175 /* Both operands are zeros */
176 Dbl_invert_sign(rightp1
);
177 if(Is_rounding_mode(ROUNDMINUS
))
179 Dbl_or_signs(leftp1
,/*with*/rightp1
);
183 Dbl_and_signs(leftp1
,/*with*/rightp1
);
188 /* Left is not a zero and must be the result. Trapped
189 * underflows are signaled if left is denormalized. Result
190 * is always exact. */
191 if( (result_exponent
== 0) && Is_underflowtrap_enabled() )
193 /* need to normalize results mantissa */
194 sign_save
= Dbl_signextendedsign(leftp1
);
195 Dbl_leftshiftby1(leftp1
,leftp2
);
196 Dbl_normalize(leftp1
,leftp2
,result_exponent
);
197 Dbl_set_sign(leftp1
,/*using*/sign_save
);
198 Dbl_setwrapped_exponent(leftp1
,result_exponent
,unfl
);
199 Dbl_copytoptr(leftp1
,leftp2
,dstptr
);
200 /* inexact = FALSE */
201 return(UNDERFLOWEXCEPTION
);
204 Dbl_copytoptr(leftp1
,leftp2
,dstptr
);
208 /* Neither are zeroes */
209 Dbl_clear_sign(rightp1
); /* Exponent is already cleared */
210 if(result_exponent
== 0 )
212 /* Both operands are denormalized. The result must be exact
213 * and is simply calculated. A sum could become normalized and a
214 * difference could cancel to a true zero. */
215 if( (/*signed*/int) save
>= 0 )
217 Dbl_subtract(leftp1
,leftp2
,/*minus*/rightp1
,rightp2
,
218 /*into*/resultp1
,resultp2
);
219 if(Dbl_iszero_mantissa(resultp1
,resultp2
))
221 if(Is_rounding_mode(ROUNDMINUS
))
223 Dbl_setone_sign(resultp1
);
227 Dbl_setzero_sign(resultp1
);
229 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
235 Dbl_addition(leftp1
,leftp2
,rightp1
,rightp2
,
236 /*into*/resultp1
,resultp2
);
237 if(Dbl_isone_hidden(resultp1
))
239 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
243 if(Is_underflowtrap_enabled())
245 /* need to normalize result */
246 sign_save
= Dbl_signextendedsign(resultp1
);
247 Dbl_leftshiftby1(resultp1
,resultp2
);
248 Dbl_normalize(resultp1
,resultp2
,result_exponent
);
249 Dbl_set_sign(resultp1
,/*using*/sign_save
);
250 Dbl_setwrapped_exponent(resultp1
,result_exponent
,unfl
);
251 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
252 /* inexact = FALSE */
253 return(UNDERFLOWEXCEPTION
);
255 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
258 right_exponent
= 1; /* Set exponent to reflect different bias
259 * with denomalized numbers. */
263 Dbl_clear_signexponent_set_hidden(rightp1
);
265 Dbl_clear_exponent_set_hidden(leftp1
);
266 diff_exponent
= result_exponent
- right_exponent
;
269 * Special case alignment of operands that would force alignment
270 * beyond the extent of the extension. A further optimization
271 * could special case this but only reduces the path length for this
274 if(diff_exponent
> DBL_THRESHOLD
)
276 diff_exponent
= DBL_THRESHOLD
;
279 /* Align right operand by shifting to right */
280 Dbl_right_align(/*operand*/rightp1
,rightp2
,/*shifted by*/diff_exponent
,
281 /*and lower to*/extent
);
283 /* Treat sum and difference of the operands separately. */
284 if( (/*signed*/int) save
>= 0 )
287 * Difference of the two operands. Their can be no overflow. A
288 * borrow can occur out of the hidden bit and force a post
289 * normalization phase.
291 Dbl_subtract_withextension(leftp1
,leftp2
,/*minus*/rightp1
,rightp2
,
292 /*with*/extent
,/*into*/resultp1
,resultp2
);
293 if(Dbl_iszero_hidden(resultp1
))
295 /* Handle normalization */
296 /* A straight forward algorithm would now shift the result
297 * and extension left until the hidden bit becomes one. Not
298 * all of the extension bits need participate in the shift.
299 * Only the two most significant bits (round and guard) are
300 * needed. If only a single shift is needed then the guard
301 * bit becomes a significant low order bit and the extension
302 * must participate in the rounding. If more than a single
303 * shift is needed, then all bits to the right of the guard
304 * bit are zeros, and the guard bit may or may not be zero. */
305 sign_save
= Dbl_signextendedsign(resultp1
);
306 Dbl_leftshiftby1_withextent(resultp1
,resultp2
,extent
,resultp1
,resultp2
);
308 /* Need to check for a zero result. The sign and exponent
309 * fields have already been zeroed. The more efficient test
310 * of the full object can be used.
312 if(Dbl_iszero(resultp1
,resultp2
))
313 /* Must have been "x-x" or "x+(-x)". */
315 if(Is_rounding_mode(ROUNDMINUS
)) Dbl_setone_sign(resultp1
);
316 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
320 /* Look to see if normalization is finished. */
321 if(Dbl_isone_hidden(resultp1
))
323 if(result_exponent
==0)
325 /* Denormalized, exponent should be zero. Left operand *
326 * was normalized, so extent (guard, round) was zero */
331 /* No further normalization is needed. */
332 Dbl_set_sign(resultp1
,/*using*/sign_save
);
333 Ext_leftshiftby1(extent
);
338 /* Check for denormalized, exponent should be zero. Left *
339 * operand was normalized, so extent (guard, round) was zero */
340 if(!(underflowtrap
= Is_underflowtrap_enabled()) &&
341 result_exponent
==0) goto underflow
;
343 /* Shift extension to complete one bit of normalization and
344 * update exponent. */
345 Ext_leftshiftby1(extent
);
347 /* Discover first one bit to determine shift amount. Use a
348 * modified binary search. We have already shifted the result
349 * one position right and still not found a one so the remainder
350 * of the extension must be zero and simplifies rounding. */
352 while(Dbl_iszero_hiddenhigh7mantissa(resultp1
))
354 Dbl_leftshiftby8(resultp1
,resultp2
);
355 if((result_exponent
-= 8) <= 0 && !underflowtrap
)
358 /* Now narrow it down to the nibble */
359 if(Dbl_iszero_hiddenhigh3mantissa(resultp1
))
361 /* The lower nibble contains the normalizing one */
362 Dbl_leftshiftby4(resultp1
,resultp2
);
363 if((result_exponent
-= 4) <= 0 && !underflowtrap
)
366 /* Select case were first bit is set (already normalized)
367 * otherwise select the proper shift. */
368 if((jumpsize
= Dbl_hiddenhigh3mantissa(resultp1
)) > 7)
370 /* Already normalized */
371 if(result_exponent
<= 0) goto underflow
;
372 Dbl_set_sign(resultp1
,/*using*/sign_save
);
373 Dbl_set_exponent(resultp1
,/*using*/result_exponent
);
374 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
377 Dbl_sethigh4bits(resultp1
,/*using*/sign_save
);
382 Dbl_leftshiftby3(resultp1
,resultp2
);
383 result_exponent
-= 3;
389 Dbl_leftshiftby2(resultp1
,resultp2
);
390 result_exponent
-= 2;
398 Dbl_leftshiftby1(resultp1
,resultp2
);
399 result_exponent
-= 1;
403 if(result_exponent
> 0)
405 Dbl_set_exponent(resultp1
,/*using*/result_exponent
);
406 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
407 return(NOEXCEPTION
); /* Sign bit is already set */
409 /* Fixup potential underflows */
411 if(Is_underflowtrap_enabled())
413 Dbl_set_sign(resultp1
,sign_save
);
414 Dbl_setwrapped_exponent(resultp1
,result_exponent
,unfl
);
415 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
416 /* inexact = FALSE */
417 return(UNDERFLOWEXCEPTION
);
420 * Since we cannot get an inexact denormalized result,
423 Dbl_fix_overshift(resultp1
,resultp2
,(1-result_exponent
),extent
);
424 Dbl_clear_signexponent(resultp1
);
425 Dbl_set_sign(resultp1
,sign_save
);
426 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
428 } /* end if(hidden...)... */
429 /* Fall through and round */
430 } /* end if(save >= 0)... */
433 /* Subtract magnitudes */
434 Dbl_addition(leftp1
,leftp2
,rightp1
,rightp2
,/*to*/resultp1
,resultp2
);
435 if(Dbl_isone_hiddenoverflow(resultp1
))
437 /* Prenormalization required. */
438 Dbl_rightshiftby1_withextent(resultp2
,extent
,extent
);
439 Dbl_arithrightshiftby1(resultp1
,resultp2
);
441 } /* end if hiddenoverflow... */
442 } /* end else ...subtract magnitudes... */
444 /* Round the result. If the extension is all zeros,then the result is
445 * exact. Otherwise round in the correct direction. No underflow is
446 * possible. If a postnormalization is necessary, then the mantissa is
447 * all zeros so no shift is needed. */
449 if(Ext_isnotzero(extent
))
452 switch(Rounding_mode())
454 case ROUNDNEAREST
: /* The default. */
455 if(Ext_isone_sign(extent
))
457 /* at least 1/2 ulp */
458 if(Ext_isnotzero_lower(extent
) ||
459 Dbl_isone_lowmantissap2(resultp2
))
461 /* either exactly half way and odd or more than 1/2ulp */
462 Dbl_increment(resultp1
,resultp2
);
468 if(Dbl_iszero_sign(resultp1
))
470 /* Round up positive results */
471 Dbl_increment(resultp1
,resultp2
);
476 if(Dbl_isone_sign(resultp1
))
478 /* Round down negative results */
479 Dbl_increment(resultp1
,resultp2
);
483 /* truncate is simple */
484 } /* end switch... */
485 if(Dbl_isone_hiddenoverflow(resultp1
)) result_exponent
++;
487 if(result_exponent
== DBL_INFINITY_EXPONENT
)
490 if(Is_overflowtrap_enabled())
492 Dbl_setwrapped_exponent(resultp1
,result_exponent
,ovfl
);
493 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
495 if (Is_inexacttrap_enabled())
496 return(OVERFLOWEXCEPTION
| INEXACTEXCEPTION
);
497 else Set_inexactflag();
498 return(OVERFLOWEXCEPTION
);
504 Dbl_setoverflow(resultp1
,resultp2
);
507 else Dbl_set_exponent(resultp1
,result_exponent
);
508 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
510 if(Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
511 else Set_inexactflag();