arm64: dts: Revert "specify console via command line"
[linux/fpc-iii.git] / arch / parisc / math-emu / sfadd.c
blob838758279d5bd26ce1d7d200ff8cdc5a9ad1faf9
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/sfadd.c $Revision: 1.1 $
14 * Purpose:
15 * Single_add: add two single precision values.
17 * External Interfaces:
18 * sgl_fadd(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 "sgl_float.h"
33 * Single_add: add two single precision values.
35 int
36 sgl_fadd(
37 sgl_floating_point *leftptr,
38 sgl_floating_point *rightptr,
39 sgl_floating_point *dstptr,
40 unsigned int *status)
42 register unsigned int left, right, result, extent;
43 register unsigned int signless_upper_left, signless_upper_right, save;
46 register int result_exponent, right_exponent, diff_exponent;
47 register int sign_save, jumpsize;
48 register boolean inexact = FALSE;
49 register boolean underflowtrap;
51 /* Create local copies of the numbers */
52 left = *leftptr;
53 right = *rightptr;
55 /* A zero "save" helps discover equal operands (for later), *
56 * and is used in swapping operands (if needed). */
57 Sgl_xortointp1(left,right,/*to*/save);
60 * check first operand for NaN's or infinity
62 if ((result_exponent = Sgl_exponent(left)) == SGL_INFINITY_EXPONENT)
64 if (Sgl_iszero_mantissa(left))
66 if (Sgl_isnotnan(right))
68 if (Sgl_isinfinity(right) && save!=0)
70 /*
71 * invalid since operands are opposite signed infinity's
73 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
74 Set_invalidflag();
75 Sgl_makequietnan(result);
76 *dstptr = result;
77 return(NOEXCEPTION);
80 * return infinity
82 *dstptr = left;
83 return(NOEXCEPTION);
86 else
89 * is NaN; signaling or quiet?
91 if (Sgl_isone_signaling(left))
93 /* trap if INVALIDTRAP enabled */
94 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
95 /* make NaN quiet */
96 Set_invalidflag();
97 Sgl_set_quiet(left);
99 /*
100 * is second operand a signaling NaN?
102 else if (Sgl_is_signalingnan(right))
104 /* trap if INVALIDTRAP enabled */
105 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
106 /* make NaN quiet */
107 Set_invalidflag();
108 Sgl_set_quiet(right);
109 *dstptr = right;
110 return(NOEXCEPTION);
113 * return quiet NaN
115 *dstptr = left;
116 return(NOEXCEPTION);
118 } /* End left NaN or Infinity processing */
120 * check second operand for NaN's or infinity
122 if (Sgl_isinfinity_exponent(right))
124 if (Sgl_iszero_mantissa(right))
126 /* return infinity */
127 *dstptr = right;
128 return(NOEXCEPTION);
131 * is NaN; signaling or quiet?
133 if (Sgl_isone_signaling(right))
135 /* trap if INVALIDTRAP enabled */
136 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
137 /* make NaN quiet */
138 Set_invalidflag();
139 Sgl_set_quiet(right);
142 * return quiet NaN
144 *dstptr = right;
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 Sgl_copytoint_exponentmantissa(left,signless_upper_left);
152 Sgl_copytoint_exponentmantissa(right,signless_upper_right);
154 /* sign difference selects add or sub operation. */
155 if(Sgl_ismagnitudeless(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 Sgl_xorfromintp1(save,right,/*to*/right);
160 Sgl_xorfromintp1(save,left,/*to*/left);
161 result_exponent = Sgl_exponent(left);
163 /* Invariant: left is not smaller than right. */
165 if((right_exponent = Sgl_exponent(right)) == 0)
167 /* Denormalized operands. First look for zeroes */
168 if(Sgl_iszero_mantissa(right))
170 /* right is zero */
171 if(Sgl_iszero_exponentmantissa(left))
173 /* Both operands are zeros */
174 if(Is_rounding_mode(ROUNDMINUS))
176 Sgl_or_signs(left,/*with*/right);
178 else
180 Sgl_and_signs(left,/*with*/right);
183 else
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 = Sgl_signextendedsign(left);
192 Sgl_leftshiftby1(left);
193 Sgl_normalize(left,result_exponent);
194 Sgl_set_sign(left,/*using*/sign_save);
195 Sgl_setwrapped_exponent(left,result_exponent,unfl);
196 *dstptr = left;
197 return(UNDERFLOWEXCEPTION);
200 *dstptr = left;
201 return(NOEXCEPTION);
204 /* Neither are zeroes */
205 Sgl_clear_sign(right); /* Exponent is already cleared */
206 if(result_exponent == 0 )
208 /* Both operands are denormalized. The result must be exact
209 * and is simply calculated. A sum could become normalized and a
210 * difference could cancel to a true zero. */
211 if( (/*signed*/int) save < 0 )
213 Sgl_subtract(left,/*minus*/right,/*into*/result);
214 if(Sgl_iszero_mantissa(result))
216 if(Is_rounding_mode(ROUNDMINUS))
218 Sgl_setone_sign(result);
220 else
222 Sgl_setzero_sign(result);
224 *dstptr = result;
225 return(NOEXCEPTION);
228 else
230 Sgl_addition(left,right,/*into*/result);
231 if(Sgl_isone_hidden(result))
233 *dstptr = result;
234 return(NOEXCEPTION);
237 if(Is_underflowtrap_enabled())
239 /* need to normalize result */
240 sign_save = Sgl_signextendedsign(result);
241 Sgl_leftshiftby1(result);
242 Sgl_normalize(result,result_exponent);
243 Sgl_set_sign(result,/*using*/sign_save);
244 Sgl_setwrapped_exponent(result,result_exponent,unfl);
245 *dstptr = result;
246 return(UNDERFLOWEXCEPTION);
248 *dstptr = result;
249 return(NOEXCEPTION);
251 right_exponent = 1; /* Set exponent to reflect different bias
252 * with denomalized numbers. */
254 else
256 Sgl_clear_signexponent_set_hidden(right);
258 Sgl_clear_exponent_set_hidden(left);
259 diff_exponent = result_exponent - right_exponent;
262 * Special case alignment of operands that would force alignment
263 * beyond the extent of the extension. A further optimization
264 * could special case this but only reduces the path length for this
265 * infrequent case.
267 if(diff_exponent > SGL_THRESHOLD)
269 diff_exponent = SGL_THRESHOLD;
272 /* Align right operand by shifting to right */
273 Sgl_right_align(/*operand*/right,/*shifted by*/diff_exponent,
274 /*and lower to*/extent);
276 /* Treat sum and difference of the operands separately. */
277 if( (/*signed*/int) save < 0 )
280 * Difference of the two operands. Their can be no overflow. A
281 * borrow can occur out of the hidden bit and force a post
282 * normalization phase.
284 Sgl_subtract_withextension(left,/*minus*/right,/*with*/extent,/*into*/result);
285 if(Sgl_iszero_hidden(result))
287 /* Handle normalization */
288 /* A straightforward algorithm would now shift the result
289 * and extension left until the hidden bit becomes one. Not
290 * all of the extension bits need participate in the shift.
291 * Only the two most significant bits (round and guard) are
292 * needed. If only a single shift is needed then the guard
293 * bit becomes a significant low order bit and the extension
294 * must participate in the rounding. If more than a single
295 * shift is needed, then all bits to the right of the guard
296 * bit are zeros, and the guard bit may or may not be zero. */
297 sign_save = Sgl_signextendedsign(result);
298 Sgl_leftshiftby1_withextent(result,extent,result);
300 /* Need to check for a zero result. The sign and exponent
301 * fields have already been zeroed. The more efficient test
302 * of the full object can be used.
304 if(Sgl_iszero(result))
305 /* Must have been "x-x" or "x+(-x)". */
307 if(Is_rounding_mode(ROUNDMINUS)) Sgl_setone_sign(result);
308 *dstptr = result;
309 return(NOEXCEPTION);
311 result_exponent--;
312 /* Look to see if normalization is finished. */
313 if(Sgl_isone_hidden(result))
315 if(result_exponent==0)
317 /* Denormalized, exponent should be zero. Left operand *
318 * was normalized, so extent (guard, round) was zero */
319 goto underflow;
321 else
323 /* No further normalization is needed. */
324 Sgl_set_sign(result,/*using*/sign_save);
325 Ext_leftshiftby1(extent);
326 goto round;
330 /* Check for denormalized, exponent should be zero. Left *
331 * operand was normalized, so extent (guard, round) was zero */
332 if(!(underflowtrap = Is_underflowtrap_enabled()) &&
333 result_exponent==0) goto underflow;
335 /* Shift extension to complete one bit of normalization and
336 * update exponent. */
337 Ext_leftshiftby1(extent);
339 /* Discover first one bit to determine shift amount. Use a
340 * modified binary search. We have already shifted the result
341 * one position right and still not found a one so the remainder
342 * of the extension must be zero and simplifies rounding. */
343 /* Scan bytes */
344 while(Sgl_iszero_hiddenhigh7mantissa(result))
346 Sgl_leftshiftby8(result);
347 if((result_exponent -= 8) <= 0 && !underflowtrap)
348 goto underflow;
350 /* Now narrow it down to the nibble */
351 if(Sgl_iszero_hiddenhigh3mantissa(result))
353 /* The lower nibble contains the normalizing one */
354 Sgl_leftshiftby4(result);
355 if((result_exponent -= 4) <= 0 && !underflowtrap)
356 goto underflow;
358 /* Select case were first bit is set (already normalized)
359 * otherwise select the proper shift. */
360 if((jumpsize = Sgl_hiddenhigh3mantissa(result)) > 7)
362 /* Already normalized */
363 if(result_exponent <= 0) goto underflow;
364 Sgl_set_sign(result,/*using*/sign_save);
365 Sgl_set_exponent(result,/*using*/result_exponent);
366 *dstptr = result;
367 return(NOEXCEPTION);
369 Sgl_sethigh4bits(result,/*using*/sign_save);
370 switch(jumpsize)
372 case 1:
374 Sgl_leftshiftby3(result);
375 result_exponent -= 3;
376 break;
378 case 2:
379 case 3:
381 Sgl_leftshiftby2(result);
382 result_exponent -= 2;
383 break;
385 case 4:
386 case 5:
387 case 6:
388 case 7:
390 Sgl_leftshiftby1(result);
391 result_exponent -= 1;
392 break;
395 if(result_exponent > 0)
397 Sgl_set_exponent(result,/*using*/result_exponent);
398 *dstptr = result;
399 return(NOEXCEPTION); /* Sign bit is already set */
401 /* Fixup potential underflows */
402 underflow:
403 if(Is_underflowtrap_enabled())
405 Sgl_set_sign(result,sign_save);
406 Sgl_setwrapped_exponent(result,result_exponent,unfl);
407 *dstptr = result;
408 /* inexact = FALSE; */
409 return(UNDERFLOWEXCEPTION);
412 * Since we cannot get an inexact denormalized result,
413 * we can now return.
415 Sgl_right_align(result,/*by*/(1-result_exponent),extent);
416 Sgl_clear_signexponent(result);
417 Sgl_set_sign(result,sign_save);
418 *dstptr = result;
419 return(NOEXCEPTION);
420 } /* end if(hidden...)... */
421 /* Fall through and round */
422 } /* end if(save < 0)... */
423 else
425 /* Add magnitudes */
426 Sgl_addition(left,right,/*to*/result);
427 if(Sgl_isone_hiddenoverflow(result))
429 /* Prenormalization required. */
430 Sgl_rightshiftby1_withextent(result,extent,extent);
431 Sgl_arithrightshiftby1(result);
432 result_exponent++;
433 } /* end if hiddenoverflow... */
434 } /* end else ...add magnitudes... */
436 /* Round the result. If the extension is all zeros,then the result is
437 * exact. Otherwise round in the correct direction. No underflow is
438 * possible. If a postnormalization is necessary, then the mantissa is
439 * all zeros so no shift is needed. */
440 round:
441 if(Ext_isnotzero(extent))
443 inexact = TRUE;
444 switch(Rounding_mode())
446 case ROUNDNEAREST: /* The default. */
447 if(Ext_isone_sign(extent))
449 /* at least 1/2 ulp */
450 if(Ext_isnotzero_lower(extent) ||
451 Sgl_isone_lowmantissa(result))
453 /* either exactly half way and odd or more than 1/2ulp */
454 Sgl_increment(result);
457 break;
459 case ROUNDPLUS:
460 if(Sgl_iszero_sign(result))
462 /* Round up positive results */
463 Sgl_increment(result);
465 break;
467 case ROUNDMINUS:
468 if(Sgl_isone_sign(result))
470 /* Round down negative results */
471 Sgl_increment(result);
474 case ROUNDZERO:;
475 /* truncate is simple */
476 } /* end switch... */
477 if(Sgl_isone_hiddenoverflow(result)) result_exponent++;
479 if(result_exponent == SGL_INFINITY_EXPONENT)
481 /* Overflow */
482 if(Is_overflowtrap_enabled())
484 Sgl_setwrapped_exponent(result,result_exponent,ovfl);
485 *dstptr = result;
486 if (inexact)
487 if (Is_inexacttrap_enabled())
488 return(OVERFLOWEXCEPTION | INEXACTEXCEPTION);
489 else Set_inexactflag();
490 return(OVERFLOWEXCEPTION);
492 else
494 Set_overflowflag();
495 inexact = TRUE;
496 Sgl_setoverflow(result);
499 else Sgl_set_exponent(result,result_exponent);
500 *dstptr = result;
501 if(inexact)
502 if(Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
503 else Set_inexactflag();
504 return(NOEXCEPTION);