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/dfadd.c $Revision: 1.1 $
15 * Double_add: add two double precision values.
17 * External Interfaces:
18 * dbl_fadd(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_add: add two double precision values.
36 dbl_floating_point *leftptr,
37 dbl_floating_point *rightptr,
38 dbl_floating_point *dstptr,
41 register unsigned int signless_upper_left, signless_upper_right, save;
42 register unsigned int leftp1, leftp2, rightp1, rightp2, extent;
43 register unsigned int resultp1 = 0, resultp2 = 0;
45 register int result_exponent, right_exponent, diff_exponent;
46 register int sign_save, jumpsize;
47 register boolean inexact = FALSE;
48 register boolean 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 opposite 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_copytoptr(rightp1,rightp2,dstptr);
130 * is NaN; signaling or quiet?
132 if (Dbl_isone_signaling(rightp1))
134 /* trap if INVALIDTRAP enabled */
135 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
138 Dbl_set_quiet(rightp1);
143 Dbl_copytoptr(rightp1,rightp2,dstptr);
145 } /* End right NaN or Infinity processing */
147 /* Invariant: Must be dealing with finite numbers */
149 /* Compare operands by removing the sign */
150 Dbl_copytoint_exponentmantissap1(leftp1,signless_upper_left);
151 Dbl_copytoint_exponentmantissap1(rightp1,signless_upper_right);
153 /* sign difference selects add or sub operation. */
154 if(Dbl_ismagnitudeless(leftp2,rightp2,signless_upper_left,signless_upper_right))
156 /* Set the left operand to the larger one by XOR swap *
157 * First finish the first word using "save" */
158 Dbl_xorfromintp1(save,rightp1,/*to*/rightp1);
159 Dbl_xorfromintp1(save,leftp1,/*to*/leftp1);
160 Dbl_swap_lower(leftp2,rightp2);
161 result_exponent = Dbl_exponent(leftp1);
163 /* Invariant: left is not smaller than right. */
165 if((right_exponent = Dbl_exponent(rightp1)) == 0)
167 /* Denormalized operands. First look for zeroes */
168 if(Dbl_iszero_mantissa(rightp1,rightp2))
171 if(Dbl_iszero_exponentmantissa(leftp1,leftp2))
173 /* Both operands are zeros */
174 if(Is_rounding_mode(ROUNDMINUS))
176 Dbl_or_signs(leftp1,/*with*/rightp1);
180 Dbl_and_signs(leftp1,/*with*/rightp1);
185 /* Left is not a zero and must be the result. Trapped
186 * underflows are signaled if left is denormalized. Result
187 * is always exact. */
188 if( (result_exponent == 0) && Is_underflowtrap_enabled() )
190 /* need to normalize results mantissa */
191 sign_save = Dbl_signextendedsign(leftp1);
192 Dbl_leftshiftby1(leftp1,leftp2);
193 Dbl_normalize(leftp1,leftp2,result_exponent);
194 Dbl_set_sign(leftp1,/*using*/sign_save);
195 Dbl_setwrapped_exponent(leftp1,result_exponent,unfl);
196 Dbl_copytoptr(leftp1,leftp2,dstptr);
197 /* inexact = FALSE */
198 return(UNDERFLOWEXCEPTION);
201 Dbl_copytoptr(leftp1,leftp2,dstptr);
205 /* Neither are zeroes */
206 Dbl_clear_sign(rightp1); /* Exponent is already cleared */
207 if(result_exponent == 0 )
209 /* Both operands are denormalized. The result must be exact
210 * and is simply calculated. A sum could become normalized and a
211 * difference could cancel to a true zero. */
212 if( (/*signed*/int) save < 0 )
214 Dbl_subtract(leftp1,leftp2,/*minus*/rightp1,rightp2,
215 /*into*/resultp1,resultp2);
216 if(Dbl_iszero_mantissa(resultp1,resultp2))
218 if(Is_rounding_mode(ROUNDMINUS))
220 Dbl_setone_sign(resultp1);
224 Dbl_setzero_sign(resultp1);
226 Dbl_copytoptr(resultp1,resultp2,dstptr);
232 Dbl_addition(leftp1,leftp2,rightp1,rightp2,
233 /*into*/resultp1,resultp2);
234 if(Dbl_isone_hidden(resultp1))
236 Dbl_copytoptr(resultp1,resultp2,dstptr);
240 if(Is_underflowtrap_enabled())
242 /* need to normalize result */
243 sign_save = Dbl_signextendedsign(resultp1);
244 Dbl_leftshiftby1(resultp1,resultp2);
245 Dbl_normalize(resultp1,resultp2,result_exponent);
246 Dbl_set_sign(resultp1,/*using*/sign_save);
247 Dbl_setwrapped_exponent(resultp1,result_exponent,unfl);
248 Dbl_copytoptr(resultp1,resultp2,dstptr);
249 /* inexact = FALSE */
250 return(UNDERFLOWEXCEPTION);
252 Dbl_copytoptr(resultp1,resultp2,dstptr);
255 right_exponent = 1; /* Set exponent to reflect different bias
256 * with denomalized numbers. */
260 Dbl_clear_signexponent_set_hidden(rightp1);
262 Dbl_clear_exponent_set_hidden(leftp1);
263 diff_exponent = result_exponent - right_exponent;
266 * Special case alignment of operands that would force alignment
267 * beyond the extent of the extension. A further optimization
268 * could special case this but only reduces the path length for this
271 if(diff_exponent > DBL_THRESHOLD)
273 diff_exponent = DBL_THRESHOLD;
276 /* Align right operand by shifting to right */
277 Dbl_right_align(/*operand*/rightp1,rightp2,/*shifted by*/diff_exponent,
278 /*and lower to*/extent);
280 /* Treat sum and difference of the operands separately. */
281 if( (/*signed*/int) save < 0 )
284 * Difference of the two operands. Their can be no overflow. A
285 * borrow can occur out of the hidden bit and force a post
286 * normalization phase.
288 Dbl_subtract_withextension(leftp1,leftp2,/*minus*/rightp1,rightp2,
289 /*with*/extent,/*into*/resultp1,resultp2);
290 if(Dbl_iszero_hidden(resultp1))
292 /* Handle normalization */
293 /* A straight forward algorithm would now shift the result
294 * and extension left until the hidden bit becomes one. Not
295 * all of the extension bits need participate in the shift.
296 * Only the two most significant bits (round and guard) are
297 * needed. If only a single shift is needed then the guard
298 * bit becomes a significant low order bit and the extension
299 * must participate in the rounding. If more than a single
300 * shift is needed, then all bits to the right of the guard
301 * bit are zeros, and the guard bit may or may not be zero. */
302 sign_save = Dbl_signextendedsign(resultp1);
303 Dbl_leftshiftby1_withextent(resultp1,resultp2,extent,resultp1,resultp2);
305 /* Need to check for a zero result. The sign and exponent
306 * fields have already been zeroed. The more efficient test
307 * of the full object can be used.
309 if(Dbl_iszero(resultp1,resultp2))
310 /* Must have been "x-x" or "x+(-x)". */
312 if(Is_rounding_mode(ROUNDMINUS)) Dbl_setone_sign(resultp1);
313 Dbl_copytoptr(resultp1,resultp2,dstptr);
317 /* Look to see if normalization is finished. */
318 if(Dbl_isone_hidden(resultp1))
320 if(result_exponent==0)
322 /* Denormalized, exponent should be zero. Left operand *
323 * was normalized, so extent (guard, round) was zero */
328 /* No further normalization is needed. */
329 Dbl_set_sign(resultp1,/*using*/sign_save);
330 Ext_leftshiftby1(extent);
335 /* Check for denormalized, exponent should be zero. Left *
336 * operand was normalized, so extent (guard, round) was zero */
337 if(!(underflowtrap = Is_underflowtrap_enabled()) &&
338 result_exponent==0) goto underflow;
340 /* Shift extension to complete one bit of normalization and
341 * update exponent. */
342 Ext_leftshiftby1(extent);
344 /* Discover first one bit to determine shift amount. Use a
345 * modified binary search. We have already shifted the result
346 * one position right and still not found a one so the remainder
347 * of the extension must be zero and simplifies rounding. */
349 while(Dbl_iszero_hiddenhigh7mantissa(resultp1))
351 Dbl_leftshiftby8(resultp1,resultp2);
352 if((result_exponent -= 8) <= 0 && !underflowtrap)
355 /* Now narrow it down to the nibble */
356 if(Dbl_iszero_hiddenhigh3mantissa(resultp1))
358 /* The lower nibble contains the normalizing one */
359 Dbl_leftshiftby4(resultp1,resultp2);
360 if((result_exponent -= 4) <= 0 && !underflowtrap)
363 /* Select case were first bit is set (already normalized)
364 * otherwise select the proper shift. */
365 if((jumpsize = Dbl_hiddenhigh3mantissa(resultp1)) > 7)
367 /* Already normalized */
368 if(result_exponent <= 0) goto underflow;
369 Dbl_set_sign(resultp1,/*using*/sign_save);
370 Dbl_set_exponent(resultp1,/*using*/result_exponent);
371 Dbl_copytoptr(resultp1,resultp2,dstptr);
374 Dbl_sethigh4bits(resultp1,/*using*/sign_save);
379 Dbl_leftshiftby3(resultp1,resultp2);
380 result_exponent -= 3;
386 Dbl_leftshiftby2(resultp1,resultp2);
387 result_exponent -= 2;
395 Dbl_leftshiftby1(resultp1,resultp2);
396 result_exponent -= 1;
400 if(result_exponent > 0)
402 Dbl_set_exponent(resultp1,/*using*/result_exponent);
403 Dbl_copytoptr(resultp1,resultp2,dstptr);
404 return(NOEXCEPTION); /* Sign bit is already set */
406 /* Fixup potential underflows */
408 if(Is_underflowtrap_enabled())
410 Dbl_set_sign(resultp1,sign_save);
411 Dbl_setwrapped_exponent(resultp1,result_exponent,unfl);
412 Dbl_copytoptr(resultp1,resultp2,dstptr);
413 /* inexact = FALSE */
414 return(UNDERFLOWEXCEPTION);
417 * Since we cannot get an inexact denormalized result,
420 Dbl_fix_overshift(resultp1,resultp2,(1-result_exponent),extent);
421 Dbl_clear_signexponent(resultp1);
422 Dbl_set_sign(resultp1,sign_save);
423 Dbl_copytoptr(resultp1,resultp2,dstptr);
425 } /* end if(hidden...)... */
426 /* Fall through and round */
427 } /* end if(save < 0)... */
431 Dbl_addition(leftp1,leftp2,rightp1,rightp2,/*to*/resultp1,resultp2);
432 if(Dbl_isone_hiddenoverflow(resultp1))
434 /* Prenormalization required. */
435 Dbl_rightshiftby1_withextent(resultp2,extent,extent);
436 Dbl_arithrightshiftby1(resultp1,resultp2);
438 } /* end if hiddenoverflow... */
439 } /* end else ...add magnitudes... */
441 /* Round the result. If the extension is all zeros,then the result is
442 * exact. Otherwise round in the correct direction. No underflow is
443 * possible. If a postnormalization is necessary, then the mantissa is
444 * all zeros so no shift is needed. */
446 if(Ext_isnotzero(extent))
449 switch(Rounding_mode())
451 case ROUNDNEAREST: /* The default. */
452 if(Ext_isone_sign(extent))
454 /* at least 1/2 ulp */
455 if(Ext_isnotzero_lower(extent) ||
456 Dbl_isone_lowmantissap2(resultp2))
458 /* either exactly half way and odd or more than 1/2ulp */
459 Dbl_increment(resultp1,resultp2);
465 if(Dbl_iszero_sign(resultp1))
467 /* Round up positive results */
468 Dbl_increment(resultp1,resultp2);
473 if(Dbl_isone_sign(resultp1))
475 /* Round down negative results */
476 Dbl_increment(resultp1,resultp2);
480 /* truncate is simple */
481 } /* end switch... */
482 if(Dbl_isone_hiddenoverflow(resultp1)) result_exponent++;
484 if(result_exponent == DBL_INFINITY_EXPONENT)
487 if(Is_overflowtrap_enabled())
489 Dbl_setwrapped_exponent(resultp1,result_exponent,ovfl);
490 Dbl_copytoptr(resultp1,resultp2,dstptr);
492 if (Is_inexacttrap_enabled())
493 return(OVERFLOWEXCEPTION | INEXACTEXCEPTION);
494 else Set_inexactflag();
495 return(OVERFLOWEXCEPTION);
501 Dbl_setoverflow(resultp1,resultp2);
504 else Dbl_set_exponent(resultp1,result_exponent);
505 Dbl_copytoptr(resultp1,resultp2,dstptr);
507 if(Is_inexacttrap_enabled())
508 return(INEXACTEXCEPTION);
509 else Set_inexactflag();