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 "dbl_float.h"
44
45
46
47
48int
49dbl_fsub(
50 dbl_floating_point *leftptr,
51 dbl_floating_point *rightptr,
52 dbl_floating_point *dstptr,
53 unsigned int *status)
54 {
55 register unsigned int signless_upper_left, signless_upper_right, save;
56 register unsigned int leftp1, leftp2, rightp1, rightp2, extent;
57 register unsigned int resultp1 = 0, resultp2 = 0;
58
59 register int result_exponent, right_exponent, diff_exponent;
60 register int sign_save, jumpsize;
61 register boolean inexact = FALSE, underflowtrap;
62
63
64 Dbl_copyfromptr(leftptr,leftp1,leftp2);
65 Dbl_copyfromptr(rightptr,rightp1,rightp2);
66
67
68
69 Dbl_xortointp1(leftp1,rightp1,save);
70
71
72
73
74 if ((result_exponent = Dbl_exponent(leftp1)) == DBL_INFINITY_EXPONENT)
75 {
76 if (Dbl_iszero_mantissa(leftp1,leftp2))
77 {
78 if (Dbl_isnotnan(rightp1,rightp2))
79 {
80 if (Dbl_isinfinity(rightp1,rightp2) && save==0)
81 {
82
83
84
85 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
86 Set_invalidflag();
87 Dbl_makequietnan(resultp1,resultp2);
88 Dbl_copytoptr(resultp1,resultp2,dstptr);
89 return(NOEXCEPTION);
90 }
91
92
93
94 Dbl_copytoptr(leftp1,leftp2,dstptr);
95 return(NOEXCEPTION);
96 }
97 }
98 else
99 {
100
101
102
103 if (Dbl_isone_signaling(leftp1))
104 {
105
106 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
107
108 Set_invalidflag();
109 Dbl_set_quiet(leftp1);
110 }
111
112
113
114 else if (Dbl_is_signalingnan(rightp1))
115 {
116
117 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
118
119 Set_invalidflag();
120 Dbl_set_quiet(rightp1);
121 Dbl_copytoptr(rightp1,rightp2,dstptr);
122 return(NOEXCEPTION);
123 }
124
125
126
127 Dbl_copytoptr(leftp1,leftp2,dstptr);
128 return(NOEXCEPTION);
129 }
130 }
131
132
133
134 if (Dbl_isinfinity_exponent(rightp1))
135 {
136 if (Dbl_iszero_mantissa(rightp1,rightp2))
137 {
138
139 Dbl_invert_sign(rightp1);
140 Dbl_copytoptr(rightp1,rightp2,dstptr);
141 return(NOEXCEPTION);
142 }
143
144
145
146 if (Dbl_isone_signaling(rightp1))
147 {
148
149 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
150
151 Set_invalidflag();
152 Dbl_set_quiet(rightp1);
153 }
154
155
156
157 Dbl_copytoptr(rightp1,rightp2,dstptr);
158 return(NOEXCEPTION);
159 }
160
161
162
163
164 Dbl_copytoint_exponentmantissap1(leftp1,signless_upper_left);
165 Dbl_copytoint_exponentmantissap1(rightp1,signless_upper_right);
166
167
168 if(Dbl_ismagnitudeless(leftp2,rightp2,signless_upper_left,signless_upper_right))
169 {
170
171
172 Dbl_xorfromintp1(save,rightp1,rightp1);
173 Dbl_xorfromintp1(save,leftp1,leftp1);
174 Dbl_swap_lower(leftp2,rightp2);
175 result_exponent = Dbl_exponent(leftp1);
176 Dbl_invert_sign(leftp1);
177 }
178
179
180 if((right_exponent = Dbl_exponent(rightp1)) == 0)
181 {
182
183 if(Dbl_iszero_mantissa(rightp1,rightp2))
184 {
185
186 if(Dbl_iszero_exponentmantissa(leftp1,leftp2))
187 {
188
189 Dbl_invert_sign(rightp1);
190 if(Is_rounding_mode(ROUNDMINUS))
191 {
192 Dbl_or_signs(leftp1,rightp1);
193 }
194 else
195 {
196 Dbl_and_signs(leftp1,rightp1);
197 }
198 }
199 else
200 {
201
202
203
204 if( (result_exponent == 0) && Is_underflowtrap_enabled() )
205 {
206
207 sign_save = Dbl_signextendedsign(leftp1);
208 Dbl_leftshiftby1(leftp1,leftp2);
209 Dbl_normalize(leftp1,leftp2,result_exponent);
210 Dbl_set_sign(leftp1,sign_save);
211 Dbl_setwrapped_exponent(leftp1,result_exponent,unfl);
212 Dbl_copytoptr(leftp1,leftp2,dstptr);
213
214 return(UNDERFLOWEXCEPTION);
215 }
216 }
217 Dbl_copytoptr(leftp1,leftp2,dstptr);
218 return(NOEXCEPTION);
219 }
220
221
222 Dbl_clear_sign(rightp1);
223 if(result_exponent == 0 )
224 {
225
226
227
228 if( (int) save >= 0 )
229 {
230 Dbl_subtract(leftp1,leftp2,rightp1,rightp2,
231 resultp1,resultp2);
232 if(Dbl_iszero_mantissa(resultp1,resultp2))
233 {
234 if(Is_rounding_mode(ROUNDMINUS))
235 {
236 Dbl_setone_sign(resultp1);
237 }
238 else
239 {
240 Dbl_setzero_sign(resultp1);
241 }
242 Dbl_copytoptr(resultp1,resultp2,dstptr);
243 return(NOEXCEPTION);
244 }
245 }
246 else
247 {
248 Dbl_addition(leftp1,leftp2,rightp1,rightp2,
249 resultp1,resultp2);
250 if(Dbl_isone_hidden(resultp1))
251 {
252 Dbl_copytoptr(resultp1,resultp2,dstptr);
253 return(NOEXCEPTION);
254 }
255 }
256 if(Is_underflowtrap_enabled())
257 {
258
259 sign_save = Dbl_signextendedsign(resultp1);
260 Dbl_leftshiftby1(resultp1,resultp2);
261 Dbl_normalize(resultp1,resultp2,result_exponent);
262 Dbl_set_sign(resultp1,sign_save);
263 Dbl_setwrapped_exponent(resultp1,result_exponent,unfl);
264 Dbl_copytoptr(resultp1,resultp2,dstptr);
265
266 return(UNDERFLOWEXCEPTION);
267 }
268 Dbl_copytoptr(resultp1,resultp2,dstptr);
269 return(NOEXCEPTION);
270 }
271 right_exponent = 1;
272
273 }
274 else
275 {
276 Dbl_clear_signexponent_set_hidden(rightp1);
277 }
278 Dbl_clear_exponent_set_hidden(leftp1);
279 diff_exponent = result_exponent - right_exponent;
280
281
282
283
284
285
286
287 if(diff_exponent > DBL_THRESHOLD)
288 {
289 diff_exponent = DBL_THRESHOLD;
290 }
291
292
293 Dbl_right_align(rightp1,rightp2,diff_exponent,
294 extent);
295
296
297 if( (int) save >= 0 )
298 {
299
300
301
302
303
304 Dbl_subtract_withextension(leftp1,leftp2,rightp1,rightp2,
305 extent,resultp1,resultp2);
306 if(Dbl_iszero_hidden(resultp1))
307 {
308
309
310
311
312
313
314
315
316
317
318 sign_save = Dbl_signextendedsign(resultp1);
319 Dbl_leftshiftby1_withextent(resultp1,resultp2,extent,resultp1,resultp2);
320
321
322
323
324
325 if(Dbl_iszero(resultp1,resultp2))
326
327 {
328 if(Is_rounding_mode(ROUNDMINUS)) Dbl_setone_sign(resultp1);
329 Dbl_copytoptr(resultp1,resultp2,dstptr);
330 return(NOEXCEPTION);
331 }
332 result_exponent--;
333
334 if(Dbl_isone_hidden(resultp1))
335 {
336 if(result_exponent==0)
337 {
338
339
340 goto underflow;
341 }
342 else
343 {
344
345 Dbl_set_sign(resultp1,sign_save);
346 Ext_leftshiftby1(extent);
347 goto round;
348 }
349 }
350
351
352
353 if(!(underflowtrap = Is_underflowtrap_enabled()) &&
354 result_exponent==0) goto underflow;
355
356
357
358 Ext_leftshiftby1(extent);
359
360
361
362
363
364
365 while(Dbl_iszero_hiddenhigh7mantissa(resultp1))
366 {
367 Dbl_leftshiftby8(resultp1,resultp2);
368 if((result_exponent -= 8) <= 0 && !underflowtrap)
369 goto underflow;
370 }
371
372 if(Dbl_iszero_hiddenhigh3mantissa(resultp1))
373 {
374
375 Dbl_leftshiftby4(resultp1,resultp2);
376 if((result_exponent -= 4) <= 0 && !underflowtrap)
377 goto underflow;
378 }
379
380
381 if((jumpsize = Dbl_hiddenhigh3mantissa(resultp1)) > 7)
382 {
383
384 if(result_exponent <= 0) goto underflow;
385 Dbl_set_sign(resultp1,sign_save);
386 Dbl_set_exponent(resultp1,result_exponent);
387 Dbl_copytoptr(resultp1,resultp2,dstptr);
388 return(NOEXCEPTION);
389 }
390 Dbl_sethigh4bits(resultp1,sign_save);
391 switch(jumpsize)
392 {
393 case 1:
394 {
395 Dbl_leftshiftby3(resultp1,resultp2);
396 result_exponent -= 3;
397 break;
398 }
399 case 2:
400 case 3:
401 {
402 Dbl_leftshiftby2(resultp1,resultp2);
403 result_exponent -= 2;
404 break;
405 }
406 case 4:
407 case 5:
408 case 6:
409 case 7:
410 {
411 Dbl_leftshiftby1(resultp1,resultp2);
412 result_exponent -= 1;
413 break;
414 }
415 }
416 if(result_exponent > 0)
417 {
418 Dbl_set_exponent(resultp1,result_exponent);
419 Dbl_copytoptr(resultp1,resultp2,dstptr);
420 return(NOEXCEPTION);
421 }
422
423 underflow:
424 if(Is_underflowtrap_enabled())
425 {
426 Dbl_set_sign(resultp1,sign_save);
427 Dbl_setwrapped_exponent(resultp1,result_exponent,unfl);
428 Dbl_copytoptr(resultp1,resultp2,dstptr);
429
430 return(UNDERFLOWEXCEPTION);
431 }
432
433
434
435
436 Dbl_fix_overshift(resultp1,resultp2,(1-result_exponent),extent);
437 Dbl_clear_signexponent(resultp1);
438 Dbl_set_sign(resultp1,sign_save);
439 Dbl_copytoptr(resultp1,resultp2,dstptr);
440 return(NOEXCEPTION);
441 }
442
443 }
444 else
445 {
446
447 Dbl_addition(leftp1,leftp2,rightp1,rightp2,resultp1,resultp2);
448 if(Dbl_isone_hiddenoverflow(resultp1))
449 {
450
451 Dbl_rightshiftby1_withextent(resultp2,extent,extent);
452 Dbl_arithrightshiftby1(resultp1,resultp2);
453 result_exponent++;
454 }
455 }
456
457
458
459
460
461 round:
462 if(Ext_isnotzero(extent))
463 {
464 inexact = TRUE;
465 switch(Rounding_mode())
466 {
467 case ROUNDNEAREST:
468 if(Ext_isone_sign(extent))
469 {
470
471 if(Ext_isnotzero_lower(extent) ||
472 Dbl_isone_lowmantissap2(resultp2))
473 {
474
475 Dbl_increment(resultp1,resultp2);
476 }
477 }
478 break;
479
480 case ROUNDPLUS:
481 if(Dbl_iszero_sign(resultp1))
482 {
483
484 Dbl_increment(resultp1,resultp2);
485 }
486 break;
487
488 case ROUNDMINUS:
489 if(Dbl_isone_sign(resultp1))
490 {
491
492 Dbl_increment(resultp1,resultp2);
493 }
494
495 case ROUNDZERO:;
496
497 }
498 if(Dbl_isone_hiddenoverflow(resultp1)) result_exponent++;
499 }
500 if(result_exponent == DBL_INFINITY_EXPONENT)
501 {
502
503 if(Is_overflowtrap_enabled())
504 {
505 Dbl_setwrapped_exponent(resultp1,result_exponent,ovfl);
506 Dbl_copytoptr(resultp1,resultp2,dstptr);
507 if (inexact)
508 if (Is_inexacttrap_enabled())
509 return(OVERFLOWEXCEPTION | INEXACTEXCEPTION);
510 else Set_inexactflag();
511 return(OVERFLOWEXCEPTION);
512 }
513 else
514 {
515 inexact = TRUE;
516 Set_overflowflag();
517 Dbl_setoverflow(resultp1,resultp2);
518 }
519 }
520 else Dbl_set_exponent(resultp1,result_exponent);
521 Dbl_copytoptr(resultp1,resultp2,dstptr);
522 if(inexact)
523 if(Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
524 else Set_inexactflag();
525 return(NOEXCEPTION);
526 }
527