No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / hppa / spmath / sfsub.c
blob4c7ffc258df6a54390482aca6ee387b2ca5eccf1
1 /* $NetBSD: sfsub.c,v 1.3 2005/12/11 12:17:40 christos Exp $ */
3 /* $OpenBSD: sfsub.c,v 1.4 2001/03/29 03:58:19 mickey Exp $ */
5 /*
6 * Copyright 1996 1995 by Open Software Foundation, Inc.
7 * All Rights Reserved
9 * Permission to use, copy, modify, and distribute this software and
10 * its documentation for any purpose and without fee is hereby granted,
11 * provided that the above copyright notice appears in all copies and
12 * that both the copyright notice and this permission notice appear in
13 * supporting documentation.
15 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17 * FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
21 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
22 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
23 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 * pmk1.1
30 * (c) Copyright 1986 HEWLETT-PACKARD COMPANY
32 * To anyone who acknowledges that this file is provided "AS IS"
33 * without any express or implied warranty:
34 * permission to use, copy, modify, and distribute this file
35 * for any purpose is hereby granted without fee, provided that
36 * the above copyright notice and this notice appears in all
37 * copies, and that the name of Hewlett-Packard Company not be
38 * used in advertising or publicity pertaining to distribution
39 * of the software without specific, written prior permission.
40 * Hewlett-Packard Company makes no representations about the
41 * suitability of this software for any purpose.
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: sfsub.c,v 1.3 2005/12/11 12:17:40 christos Exp $");
47 #include "../spmath/float.h"
48 #include "../spmath/sgl_float.h"
51 * Single_subtract: subtract two single precision values.
53 int
54 sgl_fsub(leftptr, rightptr, dstptr, status)
55 sgl_floating_point *leftptr, *rightptr, *dstptr;
56 unsigned int *status;
58 register unsigned int left, right, result, extent;
59 register unsigned int signless_upper_left, signless_upper_right, save;
61 register int result_exponent, right_exponent, diff_exponent;
62 register int sign_save, jumpsize;
63 register int inexact = false, underflowtrap;
65 /* Create local copies of the numbers */
66 left = *leftptr;
67 right = *rightptr;
69 /* A zero "save" helps discover equal operands (for later), *
70 * and is used in swapping operands (if needed). */
71 Sgl_xortointp1(left,right,/*to*/save);
74 * check first operand for NaN's or infinity
76 if ((result_exponent = Sgl_exponent(left)) == SGL_INFINITY_EXPONENT)
78 if (Sgl_iszero_mantissa(left))
80 if (Sgl_isnotnan(right))
82 if (Sgl_isinfinity(right) && save==0)
85 * invalid since operands are same signed infinity's
87 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
88 Set_invalidflag();
89 Sgl_makequietnan(result);
90 *dstptr = result;
91 return(NOEXCEPTION);
94 * return infinity
96 *dstptr = left;
97 return(NOEXCEPTION);
100 else
103 * is NaN; signaling or quiet?
105 if (Sgl_isone_signaling(left))
107 /* trap if INVALIDTRAP enabled */
108 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
109 /* make NaN quiet */
110 Set_invalidflag();
111 Sgl_set_quiet(left);
114 * is second operand a signaling NaN?
116 else if (Sgl_is_signalingnan(right))
118 /* trap if INVALIDTRAP enabled */
119 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
120 /* make NaN quiet */
121 Set_invalidflag();
122 Sgl_set_quiet(right);
123 *dstptr = right;
124 return(NOEXCEPTION);
127 * return quiet NaN
129 *dstptr = left;
130 return(NOEXCEPTION);
132 } /* End left NaN or Infinity processing */
134 * check second operand for NaN's or infinity
136 if (Sgl_isinfinity_exponent(right))
138 if (Sgl_iszero_mantissa(right))
140 /* return infinity */
141 Sgl_invert_sign(right);
142 *dstptr = right;
143 return(NOEXCEPTION);
146 * is NaN; signaling or quiet?
148 if (Sgl_isone_signaling(right))
150 /* trap if INVALIDTRAP enabled */
151 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
152 /* make NaN quiet */
153 Set_invalidflag();
154 Sgl_set_quiet(right);
157 * return quiet NaN
159 *dstptr = right;
160 return(NOEXCEPTION);
161 } /* End right NaN or Infinity processing */
163 /* Invariant: Must be dealing with finite numbers */
165 /* Compare operands by removing the sign */
166 Sgl_copytoint_exponentmantissa(left,signless_upper_left);
167 Sgl_copytoint_exponentmantissa(right,signless_upper_right);
169 /* sign difference selects sub or add operation. */
170 if(Sgl_ismagnitudeless(signless_upper_left,signless_upper_right))
172 /* Set the left operand to the larger one by XOR swap *
173 * First finish the first word using "save" */
174 Sgl_xorfromintp1(save,right,/*to*/right);
175 Sgl_xorfromintp1(save,left,/*to*/left);
176 result_exponent = Sgl_exponent(left);
177 Sgl_invert_sign(left);
179 /* Invariant: left is not smaller than right. */
181 if((right_exponent = Sgl_exponent(right)) == 0)
183 /* Denormalized operands. First look for zeroes */
184 if(Sgl_iszero_mantissa(right))
186 /* right is zero */
187 if(Sgl_iszero_exponentmantissa(left))
189 /* Both operands are zeros */
190 Sgl_invert_sign(right);
191 if(Is_rounding_mode(ROUNDMINUS))
193 Sgl_or_signs(left,/*with*/right);
195 else
197 Sgl_and_signs(left,/*with*/right);
200 else
202 /* Left is not a zero and must be the result. Trapped
203 * underflows are signaled if left is denormalized. Result
204 * is always exact. */
205 if( (result_exponent == 0) && Is_underflowtrap_enabled() )
207 /* need to normalize results mantissa */
208 sign_save = Sgl_signextendedsign(left);
209 Sgl_leftshiftby1(left);
210 Sgl_normalize(left,result_exponent);
211 Sgl_set_sign(left,/*using*/sign_save);
212 Sgl_setwrapped_exponent(left,result_exponent,unfl);
213 *dstptr = left;
214 /* inexact = false */
215 return(UNDERFLOWEXCEPTION);
218 *dstptr = left;
219 return(NOEXCEPTION);
222 /* Neither are zeroes */
223 Sgl_clear_sign(right); /* Exponent is already cleared */
224 if(result_exponent == 0 )
226 /* Both operands are denormalized. The result must be exact
227 * and is simply calculated. A sum could become normalized and a
228 * difference could cancel to a true zero. */
229 if( (/*signed*/int) save >= 0 )
231 Sgl_subtract(left,/*minus*/right,/*into*/result);
232 if(Sgl_iszero_mantissa(result))
234 if(Is_rounding_mode(ROUNDMINUS))
236 Sgl_setone_sign(result);
238 else
240 Sgl_setzero_sign(result);
242 *dstptr = result;
243 return(NOEXCEPTION);
246 else
248 Sgl_addition(left,right,/*into*/result);
249 if(Sgl_isone_hidden(result))
251 *dstptr = result;
252 return(NOEXCEPTION);
255 if(Is_underflowtrap_enabled())
257 /* need to normalize result */
258 sign_save = Sgl_signextendedsign(result);
259 Sgl_leftshiftby1(result);
260 Sgl_normalize(result,result_exponent);
261 Sgl_set_sign(result,/*using*/sign_save);
262 Sgl_setwrapped_exponent(result,result_exponent,unfl);
263 *dstptr = result;
264 /* inexact = false */
265 return(UNDERFLOWEXCEPTION);
267 *dstptr = result;
268 return(NOEXCEPTION);
270 right_exponent = 1; /* Set exponent to reflect different bias
271 * with denomalized numbers. */
273 else
275 Sgl_clear_signexponent_set_hidden(right);
277 Sgl_clear_exponent_set_hidden(left);
278 diff_exponent = result_exponent - right_exponent;
281 * Special case alignment of operands that would force alignment
282 * beyond the extent of the extension. A further optimization
283 * could special case this but only reduces the path length for this
284 * infrequent case.
286 if(diff_exponent > SGL_THRESHOLD)
288 diff_exponent = SGL_THRESHOLD;
291 /* Align right operand by shifting to right */
292 Sgl_right_align(/*operand*/right,/*shifted by*/diff_exponent,
293 /*and lower to*/extent);
295 /* Treat sum and difference of the operands separately. */
296 if( (/*signed*/int) save >= 0 )
299 * Difference of the two operands. Their can be no overflow. A
300 * borrow can occur out of the hidden bit and force a post
301 * normalization phase.
303 Sgl_subtract_withextension(left,/*minus*/right,/*with*/extent,/*into*/result);
304 if(Sgl_iszero_hidden(result))
306 /* Handle normalization */
307 /* A straight foward algorithm would now shift the result
308 * and extension left until the hidden bit becomes one. Not
309 * all of the extension bits need participate in the shift.
310 * Only the two most significant bits (round and guard) are
311 * needed. If only a single shift is needed then the guard
312 * bit becomes a significant low order bit and the extension
313 * must participate in the rounding. If more than a single
314 * shift is needed, then all bits to the right of the guard
315 * bit are zeros, and the guard bit may or may not be zero. */
316 sign_save = Sgl_signextendedsign(result);
317 Sgl_leftshiftby1_withextent(result,extent,result);
319 /* Need to check for a zero result. The sign and exponent
320 * fields have already been zeroed. The more efficient test
321 * of the full object can be used.
323 if(Sgl_iszero(result))
324 /* Must have been "x-x" or "x+(-x)". */
326 if(Is_rounding_mode(ROUNDMINUS)) Sgl_setone_sign(result);
327 *dstptr = result;
328 return(NOEXCEPTION);
330 result_exponent--;
331 /* Look to see if normalization is finished. */
332 if(Sgl_isone_hidden(result))
334 if(result_exponent==0)
336 /* Denormalized, exponent should be zero. Left operand *
337 * was normalized, so extent (guard, round) was zero */
338 goto underflow;
340 else
342 /* No further normalization is needed. */
343 Sgl_set_sign(result,/*using*/sign_save);
344 Ext_leftshiftby1(extent);
345 goto round;
349 /* Check for denormalized, exponent should be zero. Left *
350 * operand was normalized, so extent (guard, round) was zero */
351 if(!(underflowtrap = Is_underflowtrap_enabled()) &&
352 result_exponent==0) goto underflow;
354 /* Shift extension to complete one bit of normalization and
355 * update exponent. */
356 Ext_leftshiftby1(extent);
358 /* Discover first one bit to determine shift amount. Use a
359 * modified binary search. We have already shifted the result
360 * one position right and still not found a one so the remainder
361 * of the extension must be zero and simplifies rounding. */
362 /* Scan bytes */
363 while(Sgl_iszero_hiddenhigh7mantissa(result))
365 Sgl_leftshiftby8(result);
366 if((result_exponent -= 8) <= 0 && !underflowtrap)
367 goto underflow;
369 /* Now narrow it down to the nibble */
370 if(Sgl_iszero_hiddenhigh3mantissa(result))
372 /* The lower nibble contains the normalizing one */
373 Sgl_leftshiftby4(result);
374 if((result_exponent -= 4) <= 0 && !underflowtrap)
375 goto underflow;
377 /* Select case were first bit is set (already normalized)
378 * otherwise select the proper shift. */
379 if((jumpsize = Sgl_hiddenhigh3mantissa(result)) > 7)
381 /* Already normalized */
382 if(result_exponent <= 0) goto underflow;
383 Sgl_set_sign(result,/*using*/sign_save);
384 Sgl_set_exponent(result,/*using*/result_exponent);
385 *dstptr = result;
386 return(NOEXCEPTION);
388 Sgl_sethigh4bits(result,/*using*/sign_save);
389 switch(jumpsize)
391 case 1:
393 Sgl_leftshiftby3(result);
394 result_exponent -= 3;
395 break;
397 case 2:
398 case 3:
400 Sgl_leftshiftby2(result);
401 result_exponent -= 2;
402 break;
404 case 4:
405 case 5:
406 case 6:
407 case 7:
409 Sgl_leftshiftby1(result);
410 result_exponent -= 1;
411 break;
414 if(result_exponent > 0)
416 Sgl_set_exponent(result,/*using*/result_exponent);
417 *dstptr = result; /* Sign bit is already set */
418 return(NOEXCEPTION);
420 /* Fixup potential underflows */
421 underflow:
422 if(Is_underflowtrap_enabled())
424 Sgl_set_sign(result,sign_save);
425 Sgl_setwrapped_exponent(result,result_exponent,unfl);
426 *dstptr = result;
427 /* inexact = false */
428 return(UNDERFLOWEXCEPTION);
431 * Since we cannot get an inexact denormalized result,
432 * we can now return.
434 Sgl_right_align(result,/*by*/(1-result_exponent),extent);
435 Sgl_clear_signexponent(result);
436 Sgl_set_sign(result,sign_save);
437 *dstptr = result;
438 return(NOEXCEPTION);
439 } /* end if(hidden...)... */
440 /* Fall through and round */
441 } /* end if(save >= 0)... */
442 else
444 /* Add magnitudes */
445 Sgl_addition(left,right,/*to*/result);
446 if(Sgl_isone_hiddenoverflow(result))
448 /* Prenormalization required. */
449 Sgl_rightshiftby1_withextent(result,extent,extent);
450 Sgl_arithrightshiftby1(result);
451 result_exponent++;
452 } /* end if hiddenoverflow... */
453 } /* end else ...sub magnitudes... */
455 /* Round the result. If the extension is all zeros,then the result is
456 * exact. Otherwise round in the correct direction. No underflow is
457 * possible. If a postnormalization is necessary, then the mantissa is
458 * all zeros so no shift is needed. */
459 round:
460 if(Ext_isnotzero(extent))
462 inexact = true;
463 switch(Rounding_mode())
465 case ROUNDNEAREST: /* The default. */
466 if(Ext_isone_sign(extent))
468 /* at least 1/2 ulp */
469 if(Ext_isnotzero_lower(extent) ||
470 Sgl_isone_lowmantissa(result))
472 /* either exactly half way and odd or more than 1/2ulp */
473 Sgl_increment(result);
476 break;
478 case ROUNDPLUS:
479 if(Sgl_iszero_sign(result))
481 /* Round up positive results */
482 Sgl_increment(result);
484 break;
486 case ROUNDMINUS:
487 if(Sgl_isone_sign(result))
489 /* Round down negative results */
490 Sgl_increment(result);
493 case ROUNDZERO:;
494 /* truncate is simple */
495 } /* end switch... */
496 if(Sgl_isone_hiddenoverflow(result)) result_exponent++;
498 if(result_exponent == SGL_INFINITY_EXPONENT)
500 /* Overflow */
501 if(Is_overflowtrap_enabled())
503 Sgl_setwrapped_exponent(result,result_exponent,ovfl);
504 *dstptr = result;
505 if (inexact) {
506 if (Is_inexacttrap_enabled())
507 return(OVERFLOWEXCEPTION | INEXACTEXCEPTION);
508 else Set_inexactflag();
510 return(OVERFLOWEXCEPTION);
512 else
514 Set_overflowflag();
515 inexact = true;
516 Sgl_setoverflow(result);
519 else Sgl_set_exponent(result,result_exponent);
520 *dstptr = result;
521 if(inexact) {
522 if(Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
523 else Set_inexactflag();
525 return(NOEXCEPTION);