arm64: dts: Revert "specify console via command line"
[linux/fpc-iii.git] / arch / parisc / math-emu / sfsub.c
blob583d3ace46346c084993478ddd7c392a8b5b5c33
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/sfsub.c $Revision: 1.1 $
14 * Purpose:
15 * Single_subtract: subtract two single precision values.
17 * External Interfaces:
18 * sgl_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 "sgl_float.h"
33 * Single_subtract: subtract two single precision values.
35 int
36 sgl_fsub(
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;
45 register int result_exponent, right_exponent, diff_exponent;
46 register int sign_save, jumpsize;
47 register boolean inexact = FALSE, underflowtrap;
49 /* Create local copies of the numbers */
50 left = *leftptr;
51 right = *rightptr;
53 /* A zero "save" helps discover equal operands (for later), *
54 * and is used in swapping operands (if needed). */
55 Sgl_xortointp1(left,right,/*to*/save);
58 * check first operand for NaN's or infinity
60 if ((result_exponent = Sgl_exponent(left)) == SGL_INFINITY_EXPONENT)
62 if (Sgl_iszero_mantissa(left))
64 if (Sgl_isnotnan(right))
66 if (Sgl_isinfinity(right) && save==0)
68 /*
69 * invalid since operands are same signed infinity's
71 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
72 Set_invalidflag();
73 Sgl_makequietnan(result);
74 *dstptr = result;
75 return(NOEXCEPTION);
78 * return infinity
80 *dstptr = left;
81 return(NOEXCEPTION);
84 else
87 * is NaN; signaling or quiet?
89 if (Sgl_isone_signaling(left))
91 /* trap if INVALIDTRAP enabled */
92 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
93 /* make NaN quiet */
94 Set_invalidflag();
95 Sgl_set_quiet(left);
97 /*
98 * is second operand a signaling NaN?
100 else if (Sgl_is_signalingnan(right))
102 /* trap if INVALIDTRAP enabled */
103 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
104 /* make NaN quiet */
105 Set_invalidflag();
106 Sgl_set_quiet(right);
107 *dstptr = right;
108 return(NOEXCEPTION);
111 * return quiet NaN
113 *dstptr = left;
114 return(NOEXCEPTION);
116 } /* End left NaN or Infinity processing */
118 * check second operand for NaN's or infinity
120 if (Sgl_isinfinity_exponent(right))
122 if (Sgl_iszero_mantissa(right))
124 /* return infinity */
125 Sgl_invert_sign(right);
126 *dstptr = right;
127 return(NOEXCEPTION);
130 * is NaN; signaling or quiet?
132 if (Sgl_isone_signaling(right))
134 /* trap if INVALIDTRAP enabled */
135 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
136 /* make NaN quiet */
137 Set_invalidflag();
138 Sgl_set_quiet(right);
141 * return quiet NaN
143 *dstptr = right;
144 return(NOEXCEPTION);
145 } /* End right NaN or Infinity processing */
147 /* Invariant: Must be dealing with finite numbers */
149 /* Compare operands by removing the sign */
150 Sgl_copytoint_exponentmantissa(left,signless_upper_left);
151 Sgl_copytoint_exponentmantissa(right,signless_upper_right);
153 /* sign difference selects sub or add operation. */
154 if(Sgl_ismagnitudeless(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 Sgl_xorfromintp1(save,right,/*to*/right);
159 Sgl_xorfromintp1(save,left,/*to*/left);
160 result_exponent = Sgl_exponent(left);
161 Sgl_invert_sign(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 Sgl_invert_sign(right);
175 if(Is_rounding_mode(ROUNDMINUS))
177 Sgl_or_signs(left,/*with*/right);
179 else
181 Sgl_and_signs(left,/*with*/right);
184 else
186 /* Left is not a zero and must be the result. Trapped
187 * underflows are signaled if left is denormalized. Result
188 * is always exact. */
189 if( (result_exponent == 0) && Is_underflowtrap_enabled() )
191 /* need to normalize results mantissa */
192 sign_save = Sgl_signextendedsign(left);
193 Sgl_leftshiftby1(left);
194 Sgl_normalize(left,result_exponent);
195 Sgl_set_sign(left,/*using*/sign_save);
196 Sgl_setwrapped_exponent(left,result_exponent,unfl);
197 *dstptr = left;
198 /* inexact = FALSE */
199 return(UNDERFLOWEXCEPTION);
202 *dstptr = left;
203 return(NOEXCEPTION);
206 /* Neither are zeroes */
207 Sgl_clear_sign(right); /* Exponent is already cleared */
208 if(result_exponent == 0 )
210 /* Both operands are denormalized. The result must be exact
211 * and is simply calculated. A sum could become normalized and a
212 * difference could cancel to a true zero. */
213 if( (/*signed*/int) save >= 0 )
215 Sgl_subtract(left,/*minus*/right,/*into*/result);
216 if(Sgl_iszero_mantissa(result))
218 if(Is_rounding_mode(ROUNDMINUS))
220 Sgl_setone_sign(result);
222 else
224 Sgl_setzero_sign(result);
226 *dstptr = result;
227 return(NOEXCEPTION);
230 else
232 Sgl_addition(left,right,/*into*/result);
233 if(Sgl_isone_hidden(result))
235 *dstptr = result;
236 return(NOEXCEPTION);
239 if(Is_underflowtrap_enabled())
241 /* need to normalize result */
242 sign_save = Sgl_signextendedsign(result);
243 Sgl_leftshiftby1(result);
244 Sgl_normalize(result,result_exponent);
245 Sgl_set_sign(result,/*using*/sign_save);
246 Sgl_setwrapped_exponent(result,result_exponent,unfl);
247 *dstptr = result;
248 /* inexact = FALSE */
249 return(UNDERFLOWEXCEPTION);
251 *dstptr = result;
252 return(NOEXCEPTION);
254 right_exponent = 1; /* Set exponent to reflect different bias
255 * with denomalized numbers. */
257 else
259 Sgl_clear_signexponent_set_hidden(right);
261 Sgl_clear_exponent_set_hidden(left);
262 diff_exponent = result_exponent - right_exponent;
265 * Special case alignment of operands that would force alignment
266 * beyond the extent of the extension. A further optimization
267 * could special case this but only reduces the path length for this
268 * infrequent case.
270 if(diff_exponent > SGL_THRESHOLD)
272 diff_exponent = SGL_THRESHOLD;
275 /* Align right operand by shifting to right */
276 Sgl_right_align(/*operand*/right,/*shifted by*/diff_exponent,
277 /*and lower to*/extent);
279 /* Treat sum and difference of the operands separately. */
280 if( (/*signed*/int) save >= 0 )
283 * Difference of the two operands. Their can be no overflow. A
284 * borrow can occur out of the hidden bit and force a post
285 * normalization phase.
287 Sgl_subtract_withextension(left,/*minus*/right,/*with*/extent,/*into*/result);
288 if(Sgl_iszero_hidden(result))
290 /* Handle normalization */
291 /* A straightforward algorithm would now shift the result
292 * and extension left until the hidden bit becomes one. Not
293 * all of the extension bits need participate in the shift.
294 * Only the two most significant bits (round and guard) are
295 * needed. If only a single shift is needed then the guard
296 * bit becomes a significant low order bit and the extension
297 * must participate in the rounding. If more than a single
298 * shift is needed, then all bits to the right of the guard
299 * bit are zeros, and the guard bit may or may not be zero. */
300 sign_save = Sgl_signextendedsign(result);
301 Sgl_leftshiftby1_withextent(result,extent,result);
303 /* Need to check for a zero result. The sign and exponent
304 * fields have already been zeroed. The more efficient test
305 * of the full object can be used.
307 if(Sgl_iszero(result))
308 /* Must have been "x-x" or "x+(-x)". */
310 if(Is_rounding_mode(ROUNDMINUS)) Sgl_setone_sign(result);
311 *dstptr = result;
312 return(NOEXCEPTION);
314 result_exponent--;
315 /* Look to see if normalization is finished. */
316 if(Sgl_isone_hidden(result))
318 if(result_exponent==0)
320 /* Denormalized, exponent should be zero. Left operand *
321 * was normalized, so extent (guard, round) was zero */
322 goto underflow;
324 else
326 /* No further normalization is needed. */
327 Sgl_set_sign(result,/*using*/sign_save);
328 Ext_leftshiftby1(extent);
329 goto round;
333 /* Check for denormalized, exponent should be zero. Left *
334 * operand was normalized, so extent (guard, round) was zero */
335 if(!(underflowtrap = Is_underflowtrap_enabled()) &&
336 result_exponent==0) goto underflow;
338 /* Shift extension to complete one bit of normalization and
339 * update exponent. */
340 Ext_leftshiftby1(extent);
342 /* Discover first one bit to determine shift amount. Use a
343 * modified binary search. We have already shifted the result
344 * one position right and still not found a one so the remainder
345 * of the extension must be zero and simplifies rounding. */
346 /* Scan bytes */
347 while(Sgl_iszero_hiddenhigh7mantissa(result))
349 Sgl_leftshiftby8(result);
350 if((result_exponent -= 8) <= 0 && !underflowtrap)
351 goto underflow;
353 /* Now narrow it down to the nibble */
354 if(Sgl_iszero_hiddenhigh3mantissa(result))
356 /* The lower nibble contains the normalizing one */
357 Sgl_leftshiftby4(result);
358 if((result_exponent -= 4) <= 0 && !underflowtrap)
359 goto underflow;
361 /* Select case were first bit is set (already normalized)
362 * otherwise select the proper shift. */
363 if((jumpsize = Sgl_hiddenhigh3mantissa(result)) > 7)
365 /* Already normalized */
366 if(result_exponent <= 0) goto underflow;
367 Sgl_set_sign(result,/*using*/sign_save);
368 Sgl_set_exponent(result,/*using*/result_exponent);
369 *dstptr = result;
370 return(NOEXCEPTION);
372 Sgl_sethigh4bits(result,/*using*/sign_save);
373 switch(jumpsize)
375 case 1:
377 Sgl_leftshiftby3(result);
378 result_exponent -= 3;
379 break;
381 case 2:
382 case 3:
384 Sgl_leftshiftby2(result);
385 result_exponent -= 2;
386 break;
388 case 4:
389 case 5:
390 case 6:
391 case 7:
393 Sgl_leftshiftby1(result);
394 result_exponent -= 1;
395 break;
398 if(result_exponent > 0)
400 Sgl_set_exponent(result,/*using*/result_exponent);
401 *dstptr = result; /* Sign bit is already set */
402 return(NOEXCEPTION);
404 /* Fixup potential underflows */
405 underflow:
406 if(Is_underflowtrap_enabled())
408 Sgl_set_sign(result,sign_save);
409 Sgl_setwrapped_exponent(result,result_exponent,unfl);
410 *dstptr = result;
411 /* inexact = FALSE */
412 return(UNDERFLOWEXCEPTION);
415 * Since we cannot get an inexact denormalized result,
416 * we can now return.
418 Sgl_right_align(result,/*by*/(1-result_exponent),extent);
419 Sgl_clear_signexponent(result);
420 Sgl_set_sign(result,sign_save);
421 *dstptr = result;
422 return(NOEXCEPTION);
423 } /* end if(hidden...)... */
424 /* Fall through and round */
425 } /* end if(save >= 0)... */
426 else
428 /* Add magnitudes */
429 Sgl_addition(left,right,/*to*/result);
430 if(Sgl_isone_hiddenoverflow(result))
432 /* Prenormalization required. */
433 Sgl_rightshiftby1_withextent(result,extent,extent);
434 Sgl_arithrightshiftby1(result);
435 result_exponent++;
436 } /* end if hiddenoverflow... */
437 } /* end else ...sub magnitudes... */
439 /* Round the result. If the extension is all zeros,then the result is
440 * exact. Otherwise round in the correct direction. No underflow is
441 * possible. If a postnormalization is necessary, then the mantissa is
442 * all zeros so no shift is needed. */
443 round:
444 if(Ext_isnotzero(extent))
446 inexact = TRUE;
447 switch(Rounding_mode())
449 case ROUNDNEAREST: /* The default. */
450 if(Ext_isone_sign(extent))
452 /* at least 1/2 ulp */
453 if(Ext_isnotzero_lower(extent) ||
454 Sgl_isone_lowmantissa(result))
456 /* either exactly half way and odd or more than 1/2ulp */
457 Sgl_increment(result);
460 break;
462 case ROUNDPLUS:
463 if(Sgl_iszero_sign(result))
465 /* Round up positive results */
466 Sgl_increment(result);
468 break;
470 case ROUNDMINUS:
471 if(Sgl_isone_sign(result))
473 /* Round down negative results */
474 Sgl_increment(result);
477 case ROUNDZERO:;
478 /* truncate is simple */
479 } /* end switch... */
480 if(Sgl_isone_hiddenoverflow(result)) result_exponent++;
482 if(result_exponent == SGL_INFINITY_EXPONENT)
484 /* Overflow */
485 if(Is_overflowtrap_enabled())
487 Sgl_setwrapped_exponent(result,result_exponent,ovfl);
488 *dstptr = result;
489 if (inexact)
490 if (Is_inexacttrap_enabled())
491 return(OVERFLOWEXCEPTION | INEXACTEXCEPTION);
492 else Set_inexactflag();
493 return(OVERFLOWEXCEPTION);
495 else
497 Set_overflowflag();
498 inexact = TRUE;
499 Sgl_setoverflow(result);
502 else Sgl_set_exponent(result,result_exponent);
503 *dstptr = result;
504 if(inexact)
505 if(Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
506 else Set_inexactflag();
507 return(NOEXCEPTION);