Pristine Ack-5.5
[Ack-5.5.git] / modules / src / flt_arith / flt_add.c
1 /*
2   (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
3   See the copyright notice in the ACK home directory, in the file "Copyright".
4 */
5
6 /* $Id: flt_add.c,v 1.7 1994/06/24 11:15:33 ceriel Exp $ */
7
8 #include "flt_misc.h"
9
10 void
11 flt_add(e1,e2,e3)
12         register flt_arith *e1,*e2,*e3;
13 {
14         /*      Add two extended numbers e1 and e2, and put the result
15                 in e3
16         */
17         flt_arith ce1, ce2;
18         int diff;
19
20         flt_status = 0;
21         if ((e2->m1 | e2->m2) == 0L) {
22                 *e3 = *e1;
23                 return;
24         }
25         if ((e1->m1 | e1->m2) == 0L) {
26                 *e3 = *e2;
27                 return;
28         }
29         ce2 = *e2;
30         ce1 = *e1;
31         e1 = &ce1;
32         e2 = &ce2;
33
34         /* adjust mantissas to equal power */
35         diff = e2->flt_exp - e1->flt_exp;
36         if (diff < 0) {
37                 diff = -diff;
38                 e2->flt_exp += diff;
39                 flt_b64_sft(&(e2->flt_mantissa), diff);
40         }
41         else if (diff > 0) {
42                 e1->flt_exp += diff;
43                 flt_b64_sft(&(e1->flt_mantissa), diff);
44         }
45         if (e1->flt_sign != e2->flt_sign) {
46                 /* e2 + e1 = e2 - (-e1) */
47                 int tmp = ucmp(e1->m1, e2->m1);
48                 int tmp2 = ucmp(e1->m2, e2->m2);
49                 if (tmp > 0 || (tmp == 0 && tmp2 > 0)) {
50                         /*      abs(e1) > abs(e2) */
51                         if (tmp2 < 0) {
52                                 e1->m1 -= 1;    /* carry in */
53                         }
54                         e1->m1 -= e2->m1;
55                         e1->m2 -= e2->m2;
56                         *e3 = *e1;
57                 }
58                 else {
59                         if (tmp2 > 0)
60                                 e2->m1 -= 1;    /* carry in */
61                         e2->m1 -= e1->m1;
62                         e2->m2 -= e1->m2;
63                         *e3 = *e2;
64                 }
65         }
66         else {
67                 *e3 = *e2;
68                 if (flt_b64_add(&e3->flt_mantissa,&e1->flt_mantissa)) {/* addition carry */
69                         flt_b64_sft(&e3->flt_mantissa, 1);
70                         e3->m1 |= 0x80000000L;  /* set max bit  */
71                         e3->flt_exp++;          /* increase the exponent */
72                 }
73         }
74         flt_nrm(e3);
75         flt_chk(e3);
76 }
77
78 void
79 flt_sub(e1,e2,e3)
80         flt_arith *e1,*e2,*e3;
81 {
82         e2->flt_sign = ! e2->flt_sign;
83         flt_add(e1,e2,e3);
84         e2->flt_sign = ! e2->flt_sign;
85 }