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