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