1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include "float.h"
30#include "sgl_float.h"
31
32
33
34
35int
36sgl_fadd(
37 sgl_floating_point *leftptr,
38 sgl_floating_point *rightptr,
39 sgl_floating_point *dstptr,
40 unsigned int *status)
41 {
42 register unsigned int left, right, result, extent;
43 register unsigned int signless_upper_left, signless_upper_right, save;
44
45
46 register int result_exponent, right_exponent, diff_exponent;
47 register int sign_save, jumpsize;
48 register boolean inexact = FALSE;
49 register boolean underflowtrap;
50
51
52 left = *leftptr;
53 right = *rightptr;
54
55
56
57 Sgl_xortointp1(left,right,save);
58
59
60
61
62 if ((result_exponent = Sgl_exponent(left)) == SGL_INFINITY_EXPONENT)
63 {
64 if (Sgl_iszero_mantissa(left))
65 {
66 if (Sgl_isnotnan(right))
67 {
68 if (Sgl_isinfinity(right) && save!=0)
69 {
70
71
72
73 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
74 Set_invalidflag();
75 Sgl_makequietnan(result);
76 *dstptr = result;
77 return(NOEXCEPTION);
78 }
79
80
81
82 *dstptr = left;
83 return(NOEXCEPTION);
84 }
85 }
86 else
87 {
88
89
90
91 if (Sgl_isone_signaling(left))
92 {
93
94 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
95
96 Set_invalidflag();
97 Sgl_set_quiet(left);
98 }
99
100
101
102 else if (Sgl_is_signalingnan(right))
103 {
104
105 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
106
107 Set_invalidflag();
108 Sgl_set_quiet(right);
109 *dstptr = right;
110 return(NOEXCEPTION);
111 }
112
113
114
115 *dstptr = left;
116 return(NOEXCEPTION);
117 }
118 }
119
120
121
122 if (Sgl_isinfinity_exponent(right))
123 {
124 if (Sgl_iszero_mantissa(right))
125 {
126
127 *dstptr = right;
128 return(NOEXCEPTION);
129 }
130
131
132
133 if (Sgl_isone_signaling(right))
134 {
135
136 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
137
138 Set_invalidflag();
139 Sgl_set_quiet(right);
140 }
141
142
143
144 *dstptr = right;
145 return(NOEXCEPTION);
146 }
147
148
149
150
151 Sgl_copytoint_exponentmantissa(left,signless_upper_left);
152 Sgl_copytoint_exponentmantissa(right,signless_upper_right);
153
154
155 if(Sgl_ismagnitudeless(signless_upper_left,signless_upper_right))
156 {
157
158
159 Sgl_xorfromintp1(save,right,right);
160 Sgl_xorfromintp1(save,left,left);
161 result_exponent = Sgl_exponent(left);
162 }
163
164
165 if((right_exponent = Sgl_exponent(right)) == 0)
166 {
167
168 if(Sgl_iszero_mantissa(right))
169 {
170
171 if(Sgl_iszero_exponentmantissa(left))
172 {
173
174 if(Is_rounding_mode(ROUNDMINUS))
175 {
176 Sgl_or_signs(left,right);
177 }
178 else
179 {
180 Sgl_and_signs(left,right);
181 }
182 }
183 else
184 {
185
186
187
188 if( (result_exponent == 0) && Is_underflowtrap_enabled() )
189 {
190
191 sign_save = Sgl_signextendedsign(left);
192 Sgl_leftshiftby1(left);
193 Sgl_normalize(left,result_exponent);
194 Sgl_set_sign(left,sign_save);
195 Sgl_setwrapped_exponent(left,result_exponent,unfl);
196 *dstptr = left;
197 return(UNDERFLOWEXCEPTION);
198 }
199 }
200 *dstptr = left;
201 return(NOEXCEPTION);
202 }
203
204
205 Sgl_clear_sign(right);
206 if(result_exponent == 0 )
207 {
208
209
210
211 if( (int) save < 0 )
212 {
213 Sgl_subtract(left,right,result);
214 if(Sgl_iszero_mantissa(result))
215 {
216 if(Is_rounding_mode(ROUNDMINUS))
217 {
218 Sgl_setone_sign(result);
219 }
220 else
221 {
222 Sgl_setzero_sign(result);
223 }
224 *dstptr = result;
225 return(NOEXCEPTION);
226 }
227 }
228 else
229 {
230 Sgl_addition(left,right,result);
231 if(Sgl_isone_hidden(result))
232 {
233 *dstptr = result;
234 return(NOEXCEPTION);
235 }
236 }
237 if(Is_underflowtrap_enabled())
238 {
239
240 sign_save = Sgl_signextendedsign(result);
241 Sgl_leftshiftby1(result);
242 Sgl_normalize(result,result_exponent);
243 Sgl_set_sign(result,sign_save);
244 Sgl_setwrapped_exponent(result,result_exponent,unfl);
245 *dstptr = result;
246 return(UNDERFLOWEXCEPTION);
247 }
248 *dstptr = result;
249 return(NOEXCEPTION);
250 }
251 right_exponent = 1;
252
253 }
254 else
255 {
256 Sgl_clear_signexponent_set_hidden(right);
257 }
258 Sgl_clear_exponent_set_hidden(left);
259 diff_exponent = result_exponent - right_exponent;
260
261
262
263
264
265
266
267 if(diff_exponent > SGL_THRESHOLD)
268 {
269 diff_exponent = SGL_THRESHOLD;
270 }
271
272
273 Sgl_right_align(right,diff_exponent,
274 extent);
275
276
277 if( (int) save < 0 )
278 {
279
280
281
282
283
284 Sgl_subtract_withextension(left,right,extent,result);
285 if(Sgl_iszero_hidden(result))
286 {
287
288
289
290
291
292
293
294
295
296
297 sign_save = Sgl_signextendedsign(result);
298 Sgl_leftshiftby1_withextent(result,extent,result);
299
300
301
302
303
304 if(Sgl_iszero(result))
305
306 {
307 if(Is_rounding_mode(ROUNDMINUS)) Sgl_setone_sign(result);
308 *dstptr = result;
309 return(NOEXCEPTION);
310 }
311 result_exponent--;
312
313 if(Sgl_isone_hidden(result))
314 {
315 if(result_exponent==0)
316 {
317
318
319 goto underflow;
320 }
321 else
322 {
323
324 Sgl_set_sign(result,sign_save);
325 Ext_leftshiftby1(extent);
326 goto round;
327 }
328 }
329
330
331
332 if(!(underflowtrap = Is_underflowtrap_enabled()) &&
333 result_exponent==0) goto underflow;
334
335
336
337 Ext_leftshiftby1(extent);
338
339
340
341
342
343
344 while(Sgl_iszero_hiddenhigh7mantissa(result))
345 {
346 Sgl_leftshiftby8(result);
347 if((result_exponent -= 8) <= 0 && !underflowtrap)
348 goto underflow;
349 }
350
351 if(Sgl_iszero_hiddenhigh3mantissa(result))
352 {
353
354 Sgl_leftshiftby4(result);
355 if((result_exponent -= 4) <= 0 && !underflowtrap)
356 goto underflow;
357 }
358
359
360 if((jumpsize = Sgl_hiddenhigh3mantissa(result)) > 7)
361 {
362
363 if(result_exponent <= 0) goto underflow;
364 Sgl_set_sign(result,sign_save);
365 Sgl_set_exponent(result,result_exponent);
366 *dstptr = result;
367 return(NOEXCEPTION);
368 }
369 Sgl_sethigh4bits(result,sign_save);
370 switch(jumpsize)
371 {
372 case 1:
373 {
374 Sgl_leftshiftby3(result);
375 result_exponent -= 3;
376 break;
377 }
378 case 2:
379 case 3:
380 {
381 Sgl_leftshiftby2(result);
382 result_exponent -= 2;
383 break;
384 }
385 case 4:
386 case 5:
387 case 6:
388 case 7:
389 {
390 Sgl_leftshiftby1(result);
391 result_exponent -= 1;
392 break;
393 }
394 }
395 if(result_exponent > 0)
396 {
397 Sgl_set_exponent(result,result_exponent);
398 *dstptr = result;
399 return(NOEXCEPTION);
400 }
401
402 underflow:
403 if(Is_underflowtrap_enabled())
404 {
405 Sgl_set_sign(result,sign_save);
406 Sgl_setwrapped_exponent(result,result_exponent,unfl);
407 *dstptr = result;
408
409 return(UNDERFLOWEXCEPTION);
410 }
411
412
413
414
415 Sgl_right_align(result,(1-result_exponent),extent);
416 Sgl_clear_signexponent(result);
417 Sgl_set_sign(result,sign_save);
418 *dstptr = result;
419 return(NOEXCEPTION);
420 }
421
422 }
423 else
424 {
425
426 Sgl_addition(left,right,result);
427 if(Sgl_isone_hiddenoverflow(result))
428 {
429
430 Sgl_rightshiftby1_withextent(result,extent,extent);
431 Sgl_arithrightshiftby1(result);
432 result_exponent++;
433 }
434 }
435
436
437
438
439
440 round:
441 if(Ext_isnotzero(extent))
442 {
443 inexact = TRUE;
444 switch(Rounding_mode())
445 {
446 case ROUNDNEAREST:
447 if(Ext_isone_sign(extent))
448 {
449
450 if(Ext_isnotzero_lower(extent) ||
451 Sgl_isone_lowmantissa(result))
452 {
453
454 Sgl_increment(result);
455 }
456 }
457 break;
458
459 case ROUNDPLUS:
460 if(Sgl_iszero_sign(result))
461 {
462
463 Sgl_increment(result);
464 }
465 break;
466
467 case ROUNDMINUS:
468 if(Sgl_isone_sign(result))
469 {
470
471 Sgl_increment(result);
472 }
473
474 case ROUNDZERO:;
475
476 }
477 if(Sgl_isone_hiddenoverflow(result)) result_exponent++;
478 }
479 if(result_exponent == SGL_INFINITY_EXPONENT)
480 {
481
482 if(Is_overflowtrap_enabled())
483 {
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);
491 }
492 else
493 {
494 Set_overflowflag();
495 inexact = TRUE;
496 Sgl_setoverflow(result);
497 }
498 }
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);
505 }
506