treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / arch / parisc / math-emu / dfsub.c
blobc4f30acf2d48d25a68848bd5dcb1135b46cf4ca7
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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>
7 */
8 /*
9 * BEGIN_DESC
11 * File:
12 * @(#) pa/spmath/dfsub.c $Revision: 1.1 $
14 * Purpose:
15 * Double_subtract: subtract two double precision values.
17 * External Interfaces:
18 * dbl_fsub(leftptr, rightptr, dstptr, status)
20 * Internal Interfaces:
22 * Theory:
23 * <<please update with a overview of the operation of this file>>
25 * END_DESC
29 #include "float.h"
30 #include "dbl_float.h"
33 * Double_subtract: subtract two double precision values.
35 int
36 dbl_fsub(
37 dbl_floating_point *leftptr,
38 dbl_floating_point *rightptr,
39 dbl_floating_point *dstptr,
40 unsigned int *status)
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)
69 /*
70 * invalid since operands are same signed infinity's
72 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
73 Set_invalidflag();
74 Dbl_makequietnan(resultp1,resultp2);
75 Dbl_copytoptr(resultp1,resultp2,dstptr);
76 return(NOEXCEPTION);
79 * return infinity
81 Dbl_copytoptr(leftp1,leftp2,dstptr);
82 return(NOEXCEPTION);
85 else
88 * is NaN; signaling or quiet?
90 if (Dbl_isone_signaling(leftp1))
92 /* trap if INVALIDTRAP enabled */
93 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
94 /* make NaN quiet */
95 Set_invalidflag();
96 Dbl_set_quiet(leftp1);
98 /*
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);
105 /* make NaN quiet */
106 Set_invalidflag();
107 Dbl_set_quiet(rightp1);
108 Dbl_copytoptr(rightp1,rightp2,dstptr);
109 return(NOEXCEPTION);
112 * return quiet NaN
114 Dbl_copytoptr(leftp1,leftp2,dstptr);
115 return(NOEXCEPTION);
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);
128 return(NOEXCEPTION);
131 * is NaN; signaling or quiet?
133 if (Dbl_isone_signaling(rightp1))
135 /* trap if INVALIDTRAP enabled */
136 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
137 /* make NaN quiet */
138 Set_invalidflag();
139 Dbl_set_quiet(rightp1);
142 * return quiet NaN
144 Dbl_copytoptr(rightp1,rightp2,dstptr);
145 return(NOEXCEPTION);
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))
172 /* right is zero */
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);
181 else
183 Dbl_and_signs(leftp1,/*with*/rightp1);
186 else
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);
205 return(NOEXCEPTION);
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);
225 else
227 Dbl_setzero_sign(resultp1);
229 Dbl_copytoptr(resultp1,resultp2,dstptr);
230 return(NOEXCEPTION);
233 else
235 Dbl_addition(leftp1,leftp2,rightp1,rightp2,
236 /*into*/resultp1,resultp2);
237 if(Dbl_isone_hidden(resultp1))
239 Dbl_copytoptr(resultp1,resultp2,dstptr);
240 return(NOEXCEPTION);
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);
256 return(NOEXCEPTION);
258 right_exponent = 1; /* Set exponent to reflect different bias
259 * with denomalized numbers. */
261 else
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
272 * infrequent case.
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);
317 return(NOEXCEPTION);
319 result_exponent--;
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 */
327 goto underflow;
329 else
331 /* No further normalization is needed. */
332 Dbl_set_sign(resultp1,/*using*/sign_save);
333 Ext_leftshiftby1(extent);
334 goto round;
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. */
351 /* Scan bytes */
352 while(Dbl_iszero_hiddenhigh7mantissa(resultp1))
354 Dbl_leftshiftby8(resultp1,resultp2);
355 if((result_exponent -= 8) <= 0 && !underflowtrap)
356 goto underflow;
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)
364 goto underflow;
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);
375 return(NOEXCEPTION);
377 Dbl_sethigh4bits(resultp1,/*using*/sign_save);
378 switch(jumpsize)
380 case 1:
382 Dbl_leftshiftby3(resultp1,resultp2);
383 result_exponent -= 3;
384 break;
386 case 2:
387 case 3:
389 Dbl_leftshiftby2(resultp1,resultp2);
390 result_exponent -= 2;
391 break;
393 case 4:
394 case 5:
395 case 6:
396 case 7:
398 Dbl_leftshiftby1(resultp1,resultp2);
399 result_exponent -= 1;
400 break;
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 */
410 underflow:
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,
421 * we can now return.
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);
427 return(NOEXCEPTION);
428 } /* end if(hidden...)... */
429 /* Fall through and round */
430 } /* end if(save >= 0)... */
431 else
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);
440 result_exponent++;
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. */
448 round:
449 if(Ext_isnotzero(extent))
451 inexact = TRUE;
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);
465 break;
467 case ROUNDPLUS:
468 if(Dbl_iszero_sign(resultp1))
470 /* Round up positive results */
471 Dbl_increment(resultp1,resultp2);
473 break;
475 case ROUNDMINUS:
476 if(Dbl_isone_sign(resultp1))
478 /* Round down negative results */
479 Dbl_increment(resultp1,resultp2);
482 case ROUNDZERO:;
483 /* truncate is simple */
484 } /* end switch... */
485 if(Dbl_isone_hiddenoverflow(resultp1)) result_exponent++;
487 if(result_exponent == DBL_INFINITY_EXPONENT)
489 /* Overflow */
490 if(Is_overflowtrap_enabled())
492 Dbl_setwrapped_exponent(resultp1,result_exponent,ovfl);
493 Dbl_copytoptr(resultp1,resultp2,dstptr);
494 if (inexact)
495 if (Is_inexacttrap_enabled())
496 return(OVERFLOWEXCEPTION | INEXACTEXCEPTION);
497 else Set_inexactflag();
498 return(OVERFLOWEXCEPTION);
500 else
502 inexact = TRUE;
503 Set_overflowflag();
504 Dbl_setoverflow(resultp1,resultp2);
507 else Dbl_set_exponent(resultp1,result_exponent);
508 Dbl_copytoptr(resultp1,resultp2,dstptr);
509 if(inexact)
510 if(Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
511 else Set_inexactflag();
512 return(NOEXCEPTION);