1 /* $NetBSD: sfadd.c,v 1.3 2005/12/11 12:17:40 christos Exp $ */
3 /* $OpenBSD: sfadd.c,v 1.4 2001/03/29 03:58:19 mickey Exp $ */
6 * Copyright 1996 1995 by Open Software Foundation, Inc.
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.
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: sfadd.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_add: add two single precision values.
54 sgl_fadd(leftptr
, rightptr
, dstptr
, status
)
55 sgl_floating_point
*leftptr
, *rightptr
, *dstptr
;
58 register unsigned int left
, right
, result
, extent
;
59 register unsigned int signless_upper_left
, signless_upper_right
, save
;
62 register int result_exponent
, right_exponent
, diff_exponent
;
63 register int sign_save
, jumpsize
;
64 register int inexact
= false;
65 register int underflowtrap
;
67 /* Create local copies of the numbers */
71 /* A zero "save" helps discover equal operands (for later), *
72 * and is used in swapping operands (if needed). */
73 Sgl_xortointp1(left
,right
,/*to*/save
);
76 * check first operand for NaN's or infinity
78 if ((result_exponent
= Sgl_exponent(left
)) == SGL_INFINITY_EXPONENT
)
80 if (Sgl_iszero_mantissa(left
))
82 if (Sgl_isnotnan(right
))
84 if (Sgl_isinfinity(right
) && save
!=0)
87 * invalid since operands are opposite signed infinity's
89 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
91 Sgl_makequietnan(result
);
105 * is NaN; signaling or quiet?
107 if (Sgl_isone_signaling(left
))
109 /* trap if INVALIDTRAP enabled */
110 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
116 * is second operand a signaling NaN?
118 else if (Sgl_is_signalingnan(right
))
120 /* trap if INVALIDTRAP enabled */
121 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
124 Sgl_set_quiet(right
);
134 } /* End left NaN or Infinity processing */
136 * check second operand for NaN's or infinity
138 if (Sgl_isinfinity_exponent(right
))
140 if (Sgl_iszero_mantissa(right
))
142 /* return infinity */
147 * is NaN; signaling or quiet?
149 if (Sgl_isone_signaling(right
))
151 /* trap if INVALIDTRAP enabled */
152 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
155 Sgl_set_quiet(right
);
162 } /* End right NaN or Infinity processing */
164 /* Invariant: Must be dealing with finite numbers */
166 /* Compare operands by removing the sign */
167 Sgl_copytoint_exponentmantissa(left
,signless_upper_left
);
168 Sgl_copytoint_exponentmantissa(right
,signless_upper_right
);
170 /* sign difference selects add or sub operation. */
171 if(Sgl_ismagnitudeless(signless_upper_left
,signless_upper_right
))
173 /* Set the left operand to the larger one by XOR swap *
174 * First finish the first word using "save" */
175 Sgl_xorfromintp1(save
,right
,/*to*/right
);
176 Sgl_xorfromintp1(save
,left
,/*to*/left
);
177 result_exponent
= Sgl_exponent(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
))
187 if(Sgl_iszero_exponentmantissa(left
))
189 /* Both operands are zeros */
190 if(Is_rounding_mode(ROUNDMINUS
))
192 Sgl_or_signs(left
,/*with*/right
);
196 Sgl_and_signs(left
,/*with*/right
);
201 /* Left is not a zero and must be the result. Trapped
202 * underflows are signaled if left is denormalized. Result
203 * is always exact. */
204 if( (result_exponent
== 0) && Is_underflowtrap_enabled() )
206 /* need to normalize results mantissa */
207 sign_save
= Sgl_signextendedsign(left
);
208 Sgl_leftshiftby1(left
);
209 Sgl_normalize(left
,result_exponent
);
210 Sgl_set_sign(left
,/*using*/sign_save
);
211 Sgl_setwrapped_exponent(left
,result_exponent
,unfl
);
213 return(UNDERFLOWEXCEPTION
);
220 /* Neither are zeroes */
221 Sgl_clear_sign(right
); /* Exponent is already cleared */
222 if(result_exponent
== 0 )
224 /* Both operands are denormalized. The result must be exact
225 * and is simply calculated. A sum could become normalized and a
226 * difference could cancel to a true zero. */
227 if( (/*signed*/int) save
< 0 )
229 Sgl_subtract(left
,/*minus*/right
,/*into*/result
);
230 if(Sgl_iszero_mantissa(result
))
232 if(Is_rounding_mode(ROUNDMINUS
))
234 Sgl_setone_sign(result
);
238 Sgl_setzero_sign(result
);
246 Sgl_addition(left
,right
,/*into*/result
);
247 if(Sgl_isone_hidden(result
))
253 if(Is_underflowtrap_enabled())
255 /* need to normalize result */
256 sign_save
= Sgl_signextendedsign(result
);
257 Sgl_leftshiftby1(result
);
258 Sgl_normalize(result
,result_exponent
);
259 Sgl_set_sign(result
,/*using*/sign_save
);
260 Sgl_setwrapped_exponent(result
,result_exponent
,unfl
);
262 return(UNDERFLOWEXCEPTION
);
267 right_exponent
= 1; /* Set exponent to reflect different bias
268 * with denomalized numbers. */
272 Sgl_clear_signexponent_set_hidden(right
);
274 Sgl_clear_exponent_set_hidden(left
);
275 diff_exponent
= result_exponent
- right_exponent
;
278 * Special case alignment of operands that would force alignment
279 * beyond the extent of the extension. A further optimization
280 * could special case this but only reduces the path length for this
283 if(diff_exponent
> SGL_THRESHOLD
)
285 diff_exponent
= SGL_THRESHOLD
;
288 /* Align right operand by shifting to right */
289 Sgl_right_align(/*operand*/right
,/*shifted by*/diff_exponent
,
290 /*and lower to*/extent
);
292 /* Treat sum and difference of the operands separately. */
293 if( (/*signed*/int) save
< 0 )
296 * Difference of the two operands. Their can be no overflow. A
297 * borrow can occur out of the hidden bit and force a post
298 * normalization phase.
300 Sgl_subtract_withextension(left
,/*minus*/right
,/*with*/extent
,/*into*/result
);
301 if(Sgl_iszero_hidden(result
))
303 /* Handle normalization */
304 /* A straight foward algorithm would now shift the result
305 * and extension left until the hidden bit becomes one. Not
306 * all of the extension bits need participate in the shift.
307 * Only the two most significant bits (round and guard) are
308 * needed. If only a single shift is needed then the guard
309 * bit becomes a significant low order bit and the extension
310 * must participate in the rounding. If more than a single
311 * shift is needed, then all bits to the right of the guard
312 * bit are zeros, and the guard bit may or may not be zero. */
313 sign_save
= Sgl_signextendedsign(result
);
314 Sgl_leftshiftby1_withextent(result
,extent
,result
);
316 /* Need to check for a zero result. The sign and exponent
317 * fields have already been zeroed. The more efficient test
318 * of the full object can be used.
320 if(Sgl_iszero(result
))
321 /* Must have been "x-x" or "x+(-x)". */
323 if(Is_rounding_mode(ROUNDMINUS
)) Sgl_setone_sign(result
);
328 /* Look to see if normalization is finished. */
329 if(Sgl_isone_hidden(result
))
331 if(result_exponent
==0)
333 /* Denormalized, exponent should be zero. Left operand *
334 * was normalized, so extent (guard, round) was zero */
339 /* No further normalization is needed. */
340 Sgl_set_sign(result
,/*using*/sign_save
);
341 Ext_leftshiftby1(extent
);
346 /* Check for denormalized, exponent should be zero. Left *
347 * operand was normalized, so extent (guard, round) was zero */
348 if(!(underflowtrap
= Is_underflowtrap_enabled()) &&
349 result_exponent
==0) goto underflow
;
351 /* Shift extension to complete one bit of normalization and
352 * update exponent. */
353 Ext_leftshiftby1(extent
);
355 /* Discover first one bit to determine shift amount. Use a
356 * modified binary search. We have already shifted the result
357 * one position right and still not found a one so the remainder
358 * of the extension must be zero and simplifies rounding. */
360 while(Sgl_iszero_hiddenhigh7mantissa(result
))
362 Sgl_leftshiftby8(result
);
363 if((result_exponent
-= 8) <= 0 && !underflowtrap
)
366 /* Now narrow it down to the nibble */
367 if(Sgl_iszero_hiddenhigh3mantissa(result
))
369 /* The lower nibble contains the normalizing one */
370 Sgl_leftshiftby4(result
);
371 if((result_exponent
-= 4) <= 0 && !underflowtrap
)
374 /* Select case were first bit is set (already normalized)
375 * otherwise select the proper shift. */
376 if((jumpsize
= Sgl_hiddenhigh3mantissa(result
)) > 7)
378 /* Already normalized */
379 if(result_exponent
<= 0) goto underflow
;
380 Sgl_set_sign(result
,/*using*/sign_save
);
381 Sgl_set_exponent(result
,/*using*/result_exponent
);
385 Sgl_sethigh4bits(result
,/*using*/sign_save
);
390 Sgl_leftshiftby3(result
);
391 result_exponent
-= 3;
397 Sgl_leftshiftby2(result
);
398 result_exponent
-= 2;
406 Sgl_leftshiftby1(result
);
407 result_exponent
-= 1;
411 if(result_exponent
> 0)
413 Sgl_set_exponent(result
,/*using*/result_exponent
);
415 return(NOEXCEPTION
); /* Sign bit is already set */
417 /* Fixup potential underflows */
419 if(Is_underflowtrap_enabled())
421 Sgl_set_sign(result
,sign_save
);
422 Sgl_setwrapped_exponent(result
,result_exponent
,unfl
);
424 /* inexact = false; */
425 return(UNDERFLOWEXCEPTION
);
428 * Since we cannot get an inexact denormalized result,
431 Sgl_right_align(result
,/*by*/(1-result_exponent
),extent
);
432 Sgl_clear_signexponent(result
);
433 Sgl_set_sign(result
,sign_save
);
436 } /* end if(hidden...)... */
437 /* Fall through and round */
438 } /* end if(save < 0)... */
442 Sgl_addition(left
,right
,/*to*/result
);
443 if(Sgl_isone_hiddenoverflow(result
))
445 /* Prenormalization required. */
446 Sgl_rightshiftby1_withextent(result
,extent
,extent
);
447 Sgl_arithrightshiftby1(result
);
449 } /* end if hiddenoverflow... */
450 } /* end else ...add magnitudes... */
452 /* Round the result. If the extension is all zeros,then the result is
453 * exact. Otherwise round in the correct direction. No underflow is
454 * possible. If a postnormalization is necessary, then the mantissa is
455 * all zeros so no shift is needed. */
457 if(Ext_isnotzero(extent
))
460 switch(Rounding_mode())
462 case ROUNDNEAREST
: /* The default. */
463 if(Ext_isone_sign(extent
))
465 /* at least 1/2 ulp */
466 if(Ext_isnotzero_lower(extent
) ||
467 Sgl_isone_lowmantissa(result
))
469 /* either exactly half way and odd or more than 1/2ulp */
470 Sgl_increment(result
);
476 if(Sgl_iszero_sign(result
))
478 /* Round up positive results */
479 Sgl_increment(result
);
484 if(Sgl_isone_sign(result
))
486 /* Round down negative results */
487 Sgl_increment(result
);
491 /* truncate is simple */
492 } /* end switch... */
493 if(Sgl_isone_hiddenoverflow(result
)) result_exponent
++;
495 if(result_exponent
== SGL_INFINITY_EXPONENT
)
498 if(Is_overflowtrap_enabled())
500 Sgl_setwrapped_exponent(result
,result_exponent
,ovfl
);
503 if (Is_inexacttrap_enabled())
504 return(OVERFLOWEXCEPTION
| INEXACTEXCEPTION
);
505 else Set_inexactflag();
507 return(OVERFLOWEXCEPTION
);
513 Sgl_setoverflow(result
);
516 else Sgl_set_exponent(result
,result_exponent
);
519 if(Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
520 else Set_inexactflag();