1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Linux/PA-RISC Project (http://www.parisc-linux.org/)
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Floating-point emulation code
6*4882a593Smuzhiyun * Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun * BEGIN_DESC
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * File:
12*4882a593Smuzhiyun * @(#) pa/spmath/sfsub.c $Revision: 1.1 $
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * Purpose:
15*4882a593Smuzhiyun * Single_subtract: subtract two single precision values.
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * External Interfaces:
18*4882a593Smuzhiyun * sgl_fsub(leftptr, rightptr, dstptr, status)
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * Internal Interfaces:
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * Theory:
23*4882a593Smuzhiyun * <<please update with a overview of the operation of this file>>
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * END_DESC
26*4882a593Smuzhiyun */
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include "float.h"
30*4882a593Smuzhiyun #include "sgl_float.h"
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun * Single_subtract: subtract two single precision values.
34*4882a593Smuzhiyun */
35*4882a593Smuzhiyun int
sgl_fsub(sgl_floating_point * leftptr,sgl_floating_point * rightptr,sgl_floating_point * dstptr,unsigned int * status)36*4882a593Smuzhiyun sgl_fsub(
37*4882a593Smuzhiyun sgl_floating_point *leftptr,
38*4882a593Smuzhiyun sgl_floating_point *rightptr,
39*4882a593Smuzhiyun sgl_floating_point *dstptr,
40*4882a593Smuzhiyun unsigned int *status)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun register unsigned int left, right, result, extent;
43*4882a593Smuzhiyun register unsigned int signless_upper_left, signless_upper_right, save;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun register int result_exponent, right_exponent, diff_exponent;
46*4882a593Smuzhiyun register int sign_save, jumpsize;
47*4882a593Smuzhiyun register boolean inexact = FALSE, underflowtrap;
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun /* Create local copies of the numbers */
50*4882a593Smuzhiyun left = *leftptr;
51*4882a593Smuzhiyun right = *rightptr;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /* A zero "save" helps discover equal operands (for later), *
54*4882a593Smuzhiyun * and is used in swapping operands (if needed). */
55*4882a593Smuzhiyun Sgl_xortointp1(left,right,/*to*/save);
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun * check first operand for NaN's or infinity
59*4882a593Smuzhiyun */
60*4882a593Smuzhiyun if ((result_exponent = Sgl_exponent(left)) == SGL_INFINITY_EXPONENT)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun if (Sgl_iszero_mantissa(left))
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun if (Sgl_isnotnan(right))
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun if (Sgl_isinfinity(right) && save==0)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun /*
69*4882a593Smuzhiyun * invalid since operands are same signed infinity's
70*4882a593Smuzhiyun */
71*4882a593Smuzhiyun if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
72*4882a593Smuzhiyun Set_invalidflag();
73*4882a593Smuzhiyun Sgl_makequietnan(result);
74*4882a593Smuzhiyun *dstptr = result;
75*4882a593Smuzhiyun return(NOEXCEPTION);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun /*
78*4882a593Smuzhiyun * return infinity
79*4882a593Smuzhiyun */
80*4882a593Smuzhiyun *dstptr = left;
81*4882a593Smuzhiyun return(NOEXCEPTION);
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun else
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun /*
87*4882a593Smuzhiyun * is NaN; signaling or quiet?
88*4882a593Smuzhiyun */
89*4882a593Smuzhiyun if (Sgl_isone_signaling(left))
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun /* trap if INVALIDTRAP enabled */
92*4882a593Smuzhiyun if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
93*4882a593Smuzhiyun /* make NaN quiet */
94*4882a593Smuzhiyun Set_invalidflag();
95*4882a593Smuzhiyun Sgl_set_quiet(left);
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun /*
98*4882a593Smuzhiyun * is second operand a signaling NaN?
99*4882a593Smuzhiyun */
100*4882a593Smuzhiyun else if (Sgl_is_signalingnan(right))
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun /* trap if INVALIDTRAP enabled */
103*4882a593Smuzhiyun if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
104*4882a593Smuzhiyun /* make NaN quiet */
105*4882a593Smuzhiyun Set_invalidflag();
106*4882a593Smuzhiyun Sgl_set_quiet(right);
107*4882a593Smuzhiyun *dstptr = right;
108*4882a593Smuzhiyun return(NOEXCEPTION);
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun /*
111*4882a593Smuzhiyun * return quiet NaN
112*4882a593Smuzhiyun */
113*4882a593Smuzhiyun *dstptr = left;
114*4882a593Smuzhiyun return(NOEXCEPTION);
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun } /* End left NaN or Infinity processing */
117*4882a593Smuzhiyun /*
118*4882a593Smuzhiyun * check second operand for NaN's or infinity
119*4882a593Smuzhiyun */
120*4882a593Smuzhiyun if (Sgl_isinfinity_exponent(right))
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun if (Sgl_iszero_mantissa(right))
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun /* return infinity */
125*4882a593Smuzhiyun Sgl_invert_sign(right);
126*4882a593Smuzhiyun *dstptr = right;
127*4882a593Smuzhiyun return(NOEXCEPTION);
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun /*
130*4882a593Smuzhiyun * is NaN; signaling or quiet?
131*4882a593Smuzhiyun */
132*4882a593Smuzhiyun if (Sgl_isone_signaling(right))
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun /* trap if INVALIDTRAP enabled */
135*4882a593Smuzhiyun if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
136*4882a593Smuzhiyun /* make NaN quiet */
137*4882a593Smuzhiyun Set_invalidflag();
138*4882a593Smuzhiyun Sgl_set_quiet(right);
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun /*
141*4882a593Smuzhiyun * return quiet NaN
142*4882a593Smuzhiyun */
143*4882a593Smuzhiyun *dstptr = right;
144*4882a593Smuzhiyun return(NOEXCEPTION);
145*4882a593Smuzhiyun } /* End right NaN or Infinity processing */
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun /* Invariant: Must be dealing with finite numbers */
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun /* Compare operands by removing the sign */
150*4882a593Smuzhiyun Sgl_copytoint_exponentmantissa(left,signless_upper_left);
151*4882a593Smuzhiyun Sgl_copytoint_exponentmantissa(right,signless_upper_right);
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /* sign difference selects sub or add operation. */
154*4882a593Smuzhiyun if(Sgl_ismagnitudeless(signless_upper_left,signless_upper_right))
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun /* Set the left operand to the larger one by XOR swap *
157*4882a593Smuzhiyun * First finish the first word using "save" */
158*4882a593Smuzhiyun Sgl_xorfromintp1(save,right,/*to*/right);
159*4882a593Smuzhiyun Sgl_xorfromintp1(save,left,/*to*/left);
160*4882a593Smuzhiyun result_exponent = Sgl_exponent(left);
161*4882a593Smuzhiyun Sgl_invert_sign(left);
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun /* Invariant: left is not smaller than right. */
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun if((right_exponent = Sgl_exponent(right)) == 0)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun /* Denormalized operands. First look for zeroes */
168*4882a593Smuzhiyun if(Sgl_iszero_mantissa(right))
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun /* right is zero */
171*4882a593Smuzhiyun if(Sgl_iszero_exponentmantissa(left))
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun /* Both operands are zeros */
174*4882a593Smuzhiyun Sgl_invert_sign(right);
175*4882a593Smuzhiyun if(Is_rounding_mode(ROUNDMINUS))
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun Sgl_or_signs(left,/*with*/right);
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun else
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun Sgl_and_signs(left,/*with*/right);
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun else
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun /* Left is not a zero and must be the result. Trapped
187*4882a593Smuzhiyun * underflows are signaled if left is denormalized. Result
188*4882a593Smuzhiyun * is always exact. */
189*4882a593Smuzhiyun if( (result_exponent == 0) && Is_underflowtrap_enabled() )
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun /* need to normalize results mantissa */
192*4882a593Smuzhiyun sign_save = Sgl_signextendedsign(left);
193*4882a593Smuzhiyun Sgl_leftshiftby1(left);
194*4882a593Smuzhiyun Sgl_normalize(left,result_exponent);
195*4882a593Smuzhiyun Sgl_set_sign(left,/*using*/sign_save);
196*4882a593Smuzhiyun Sgl_setwrapped_exponent(left,result_exponent,unfl);
197*4882a593Smuzhiyun *dstptr = left;
198*4882a593Smuzhiyun /* inexact = FALSE */
199*4882a593Smuzhiyun return(UNDERFLOWEXCEPTION);
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun *dstptr = left;
203*4882a593Smuzhiyun return(NOEXCEPTION);
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /* Neither are zeroes */
207*4882a593Smuzhiyun Sgl_clear_sign(right); /* Exponent is already cleared */
208*4882a593Smuzhiyun if(result_exponent == 0 )
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun /* Both operands are denormalized. The result must be exact
211*4882a593Smuzhiyun * and is simply calculated. A sum could become normalized and a
212*4882a593Smuzhiyun * difference could cancel to a true zero. */
213*4882a593Smuzhiyun if( (/*signed*/int) save >= 0 )
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun Sgl_subtract(left,/*minus*/right,/*into*/result);
216*4882a593Smuzhiyun if(Sgl_iszero_mantissa(result))
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun if(Is_rounding_mode(ROUNDMINUS))
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun Sgl_setone_sign(result);
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun else
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun Sgl_setzero_sign(result);
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun *dstptr = result;
227*4882a593Smuzhiyun return(NOEXCEPTION);
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun else
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun Sgl_addition(left,right,/*into*/result);
233*4882a593Smuzhiyun if(Sgl_isone_hidden(result))
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun *dstptr = result;
236*4882a593Smuzhiyun return(NOEXCEPTION);
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun if(Is_underflowtrap_enabled())
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun /* need to normalize result */
242*4882a593Smuzhiyun sign_save = Sgl_signextendedsign(result);
243*4882a593Smuzhiyun Sgl_leftshiftby1(result);
244*4882a593Smuzhiyun Sgl_normalize(result,result_exponent);
245*4882a593Smuzhiyun Sgl_set_sign(result,/*using*/sign_save);
246*4882a593Smuzhiyun Sgl_setwrapped_exponent(result,result_exponent,unfl);
247*4882a593Smuzhiyun *dstptr = result;
248*4882a593Smuzhiyun /* inexact = FALSE */
249*4882a593Smuzhiyun return(UNDERFLOWEXCEPTION);
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun *dstptr = result;
252*4882a593Smuzhiyun return(NOEXCEPTION);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun right_exponent = 1; /* Set exponent to reflect different bias
255*4882a593Smuzhiyun * with denomalized numbers. */
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun else
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun Sgl_clear_signexponent_set_hidden(right);
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun Sgl_clear_exponent_set_hidden(left);
262*4882a593Smuzhiyun diff_exponent = result_exponent - right_exponent;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun /*
265*4882a593Smuzhiyun * Special case alignment of operands that would force alignment
266*4882a593Smuzhiyun * beyond the extent of the extension. A further optimization
267*4882a593Smuzhiyun * could special case this but only reduces the path length for this
268*4882a593Smuzhiyun * infrequent case.
269*4882a593Smuzhiyun */
270*4882a593Smuzhiyun if(diff_exponent > SGL_THRESHOLD)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun diff_exponent = SGL_THRESHOLD;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun /* Align right operand by shifting to right */
276*4882a593Smuzhiyun Sgl_right_align(/*operand*/right,/*shifted by*/diff_exponent,
277*4882a593Smuzhiyun /*and lower to*/extent);
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun /* Treat sum and difference of the operands separately. */
280*4882a593Smuzhiyun if( (/*signed*/int) save >= 0 )
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun /*
283*4882a593Smuzhiyun * Difference of the two operands. Their can be no overflow. A
284*4882a593Smuzhiyun * borrow can occur out of the hidden bit and force a post
285*4882a593Smuzhiyun * normalization phase.
286*4882a593Smuzhiyun */
287*4882a593Smuzhiyun Sgl_subtract_withextension(left,/*minus*/right,/*with*/extent,/*into*/result);
288*4882a593Smuzhiyun if(Sgl_iszero_hidden(result))
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun /* Handle normalization */
291*4882a593Smuzhiyun /* A straightforward algorithm would now shift the result
292*4882a593Smuzhiyun * and extension left until the hidden bit becomes one. Not
293*4882a593Smuzhiyun * all of the extension bits need participate in the shift.
294*4882a593Smuzhiyun * Only the two most significant bits (round and guard) are
295*4882a593Smuzhiyun * needed. If only a single shift is needed then the guard
296*4882a593Smuzhiyun * bit becomes a significant low order bit and the extension
297*4882a593Smuzhiyun * must participate in the rounding. If more than a single
298*4882a593Smuzhiyun * shift is needed, then all bits to the right of the guard
299*4882a593Smuzhiyun * bit are zeros, and the guard bit may or may not be zero. */
300*4882a593Smuzhiyun sign_save = Sgl_signextendedsign(result);
301*4882a593Smuzhiyun Sgl_leftshiftby1_withextent(result,extent,result);
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun /* Need to check for a zero result. The sign and exponent
304*4882a593Smuzhiyun * fields have already been zeroed. The more efficient test
305*4882a593Smuzhiyun * of the full object can be used.
306*4882a593Smuzhiyun */
307*4882a593Smuzhiyun if(Sgl_iszero(result))
308*4882a593Smuzhiyun /* Must have been "x-x" or "x+(-x)". */
309*4882a593Smuzhiyun {
310*4882a593Smuzhiyun if(Is_rounding_mode(ROUNDMINUS)) Sgl_setone_sign(result);
311*4882a593Smuzhiyun *dstptr = result;
312*4882a593Smuzhiyun return(NOEXCEPTION);
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun result_exponent--;
315*4882a593Smuzhiyun /* Look to see if normalization is finished. */
316*4882a593Smuzhiyun if(Sgl_isone_hidden(result))
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun if(result_exponent==0)
319*4882a593Smuzhiyun {
320*4882a593Smuzhiyun /* Denormalized, exponent should be zero. Left operand *
321*4882a593Smuzhiyun * was normalized, so extent (guard, round) was zero */
322*4882a593Smuzhiyun goto underflow;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun else
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun /* No further normalization is needed. */
327*4882a593Smuzhiyun Sgl_set_sign(result,/*using*/sign_save);
328*4882a593Smuzhiyun Ext_leftshiftby1(extent);
329*4882a593Smuzhiyun goto round;
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun /* Check for denormalized, exponent should be zero. Left *
334*4882a593Smuzhiyun * operand was normalized, so extent (guard, round) was zero */
335*4882a593Smuzhiyun if(!(underflowtrap = Is_underflowtrap_enabled()) &&
336*4882a593Smuzhiyun result_exponent==0) goto underflow;
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun /* Shift extension to complete one bit of normalization and
339*4882a593Smuzhiyun * update exponent. */
340*4882a593Smuzhiyun Ext_leftshiftby1(extent);
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun /* Discover first one bit to determine shift amount. Use a
343*4882a593Smuzhiyun * modified binary search. We have already shifted the result
344*4882a593Smuzhiyun * one position right and still not found a one so the remainder
345*4882a593Smuzhiyun * of the extension must be zero and simplifies rounding. */
346*4882a593Smuzhiyun /* Scan bytes */
347*4882a593Smuzhiyun while(Sgl_iszero_hiddenhigh7mantissa(result))
348*4882a593Smuzhiyun {
349*4882a593Smuzhiyun Sgl_leftshiftby8(result);
350*4882a593Smuzhiyun if((result_exponent -= 8) <= 0 && !underflowtrap)
351*4882a593Smuzhiyun goto underflow;
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun /* Now narrow it down to the nibble */
354*4882a593Smuzhiyun if(Sgl_iszero_hiddenhigh3mantissa(result))
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun /* The lower nibble contains the normalizing one */
357*4882a593Smuzhiyun Sgl_leftshiftby4(result);
358*4882a593Smuzhiyun if((result_exponent -= 4) <= 0 && !underflowtrap)
359*4882a593Smuzhiyun goto underflow;
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun /* Select case were first bit is set (already normalized)
362*4882a593Smuzhiyun * otherwise select the proper shift. */
363*4882a593Smuzhiyun if((jumpsize = Sgl_hiddenhigh3mantissa(result)) > 7)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun /* Already normalized */
366*4882a593Smuzhiyun if(result_exponent <= 0) goto underflow;
367*4882a593Smuzhiyun Sgl_set_sign(result,/*using*/sign_save);
368*4882a593Smuzhiyun Sgl_set_exponent(result,/*using*/result_exponent);
369*4882a593Smuzhiyun *dstptr = result;
370*4882a593Smuzhiyun return(NOEXCEPTION);
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun Sgl_sethigh4bits(result,/*using*/sign_save);
373*4882a593Smuzhiyun switch(jumpsize)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun case 1:
376*4882a593Smuzhiyun {
377*4882a593Smuzhiyun Sgl_leftshiftby3(result);
378*4882a593Smuzhiyun result_exponent -= 3;
379*4882a593Smuzhiyun break;
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun case 2:
382*4882a593Smuzhiyun case 3:
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun Sgl_leftshiftby2(result);
385*4882a593Smuzhiyun result_exponent -= 2;
386*4882a593Smuzhiyun break;
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun case 4:
389*4882a593Smuzhiyun case 5:
390*4882a593Smuzhiyun case 6:
391*4882a593Smuzhiyun case 7:
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun Sgl_leftshiftby1(result);
394*4882a593Smuzhiyun result_exponent -= 1;
395*4882a593Smuzhiyun break;
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun if(result_exponent > 0)
399*4882a593Smuzhiyun {
400*4882a593Smuzhiyun Sgl_set_exponent(result,/*using*/result_exponent);
401*4882a593Smuzhiyun *dstptr = result; /* Sign bit is already set */
402*4882a593Smuzhiyun return(NOEXCEPTION);
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun /* Fixup potential underflows */
405*4882a593Smuzhiyun underflow:
406*4882a593Smuzhiyun if(Is_underflowtrap_enabled())
407*4882a593Smuzhiyun {
408*4882a593Smuzhiyun Sgl_set_sign(result,sign_save);
409*4882a593Smuzhiyun Sgl_setwrapped_exponent(result,result_exponent,unfl);
410*4882a593Smuzhiyun *dstptr = result;
411*4882a593Smuzhiyun /* inexact = FALSE */
412*4882a593Smuzhiyun return(UNDERFLOWEXCEPTION);
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun /*
415*4882a593Smuzhiyun * Since we cannot get an inexact denormalized result,
416*4882a593Smuzhiyun * we can now return.
417*4882a593Smuzhiyun */
418*4882a593Smuzhiyun Sgl_right_align(result,/*by*/(1-result_exponent),extent);
419*4882a593Smuzhiyun Sgl_clear_signexponent(result);
420*4882a593Smuzhiyun Sgl_set_sign(result,sign_save);
421*4882a593Smuzhiyun *dstptr = result;
422*4882a593Smuzhiyun return(NOEXCEPTION);
423*4882a593Smuzhiyun } /* end if(hidden...)... */
424*4882a593Smuzhiyun /* Fall through and round */
425*4882a593Smuzhiyun } /* end if(save >= 0)... */
426*4882a593Smuzhiyun else
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun /* Add magnitudes */
429*4882a593Smuzhiyun Sgl_addition(left,right,/*to*/result);
430*4882a593Smuzhiyun if(Sgl_isone_hiddenoverflow(result))
431*4882a593Smuzhiyun {
432*4882a593Smuzhiyun /* Prenormalization required. */
433*4882a593Smuzhiyun Sgl_rightshiftby1_withextent(result,extent,extent);
434*4882a593Smuzhiyun Sgl_arithrightshiftby1(result);
435*4882a593Smuzhiyun result_exponent++;
436*4882a593Smuzhiyun } /* end if hiddenoverflow... */
437*4882a593Smuzhiyun } /* end else ...sub magnitudes... */
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun /* Round the result. If the extension is all zeros,then the result is
440*4882a593Smuzhiyun * exact. Otherwise round in the correct direction. No underflow is
441*4882a593Smuzhiyun * possible. If a postnormalization is necessary, then the mantissa is
442*4882a593Smuzhiyun * all zeros so no shift is needed. */
443*4882a593Smuzhiyun round:
444*4882a593Smuzhiyun if(Ext_isnotzero(extent))
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun inexact = TRUE;
447*4882a593Smuzhiyun switch(Rounding_mode())
448*4882a593Smuzhiyun {
449*4882a593Smuzhiyun case ROUNDNEAREST: /* The default. */
450*4882a593Smuzhiyun if(Ext_isone_sign(extent))
451*4882a593Smuzhiyun {
452*4882a593Smuzhiyun /* at least 1/2 ulp */
453*4882a593Smuzhiyun if(Ext_isnotzero_lower(extent) ||
454*4882a593Smuzhiyun Sgl_isone_lowmantissa(result))
455*4882a593Smuzhiyun {
456*4882a593Smuzhiyun /* either exactly half way and odd or more than 1/2ulp */
457*4882a593Smuzhiyun Sgl_increment(result);
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun break;
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun case ROUNDPLUS:
463*4882a593Smuzhiyun if(Sgl_iszero_sign(result))
464*4882a593Smuzhiyun {
465*4882a593Smuzhiyun /* Round up positive results */
466*4882a593Smuzhiyun Sgl_increment(result);
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun break;
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun case ROUNDMINUS:
471*4882a593Smuzhiyun if(Sgl_isone_sign(result))
472*4882a593Smuzhiyun {
473*4882a593Smuzhiyun /* Round down negative results */
474*4882a593Smuzhiyun Sgl_increment(result);
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun case ROUNDZERO:;
478*4882a593Smuzhiyun /* truncate is simple */
479*4882a593Smuzhiyun } /* end switch... */
480*4882a593Smuzhiyun if(Sgl_isone_hiddenoverflow(result)) result_exponent++;
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun if(result_exponent == SGL_INFINITY_EXPONENT)
483*4882a593Smuzhiyun {
484*4882a593Smuzhiyun /* Overflow */
485*4882a593Smuzhiyun if(Is_overflowtrap_enabled())
486*4882a593Smuzhiyun {
487*4882a593Smuzhiyun Sgl_setwrapped_exponent(result,result_exponent,ovfl);
488*4882a593Smuzhiyun *dstptr = result;
489*4882a593Smuzhiyun if (inexact)
490*4882a593Smuzhiyun if (Is_inexacttrap_enabled())
491*4882a593Smuzhiyun return(OVERFLOWEXCEPTION | INEXACTEXCEPTION);
492*4882a593Smuzhiyun else Set_inexactflag();
493*4882a593Smuzhiyun return(OVERFLOWEXCEPTION);
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun else
496*4882a593Smuzhiyun {
497*4882a593Smuzhiyun Set_overflowflag();
498*4882a593Smuzhiyun inexact = TRUE;
499*4882a593Smuzhiyun Sgl_setoverflow(result);
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun else Sgl_set_exponent(result,result_exponent);
503*4882a593Smuzhiyun *dstptr = result;
504*4882a593Smuzhiyun if(inexact)
505*4882a593Smuzhiyun if(Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
506*4882a593Smuzhiyun else Set_inexactflag();
507*4882a593Smuzhiyun return(NOEXCEPTION);
508*4882a593Smuzhiyun }
509