07cdb35731be63d927b963870fa56864efb1b0ac
[fw/altos] / src / core / ao_int64.c
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao_int64.h>
19
20 void ao_plus64(ao_int64_t *r, ao_int64_t *a, ao_int64_t *b) {
21         uint32_t        t;
22
23         r->high = a->high + b->high;
24         t = a->low + b->low;
25         if (t < a->low)
26                 r->high++;
27         r->low = t;
28 }
29
30 void ao_minus64(ao_int64_t *r, ao_int64_t *a, ao_int64_t *b) {
31         uint32_t        t;
32
33
34         r->high = a->high - b->high;
35         t = a->low - b->low;
36         if (t > a->low)
37                 r->high--;
38         r->low = t;
39 }
40
41 void ao_rshift64(ao_int64_t *r, ao_int64_t *a, uint8_t d) {
42         if (d < 32) {
43                 r->low = a->low >> d;
44                 if (d)
45                         r->low |= a->high << (32 - d);
46                 r->high = (int32_t) a->high >> d;
47         } else {
48                 d &= 0x1f;
49                 r->low = (int32_t) a->high >> d;
50                 r->high = 0;
51         }
52 }
53
54 void ao_lshift64(ao_int64_t *r, ao_int64_t *a, uint8_t d) {
55         if (d < 32) {
56                 r->high = a->high << d;
57                 if (d)
58                         r->high |= a->low >> (32 - d);
59                 r->low = a->low << d;
60         } else {
61                 d &= 0x1f;
62                 r->high = a->low << d;
63                 r->low = 0;
64         }
65 }
66
67 static void ao_umul64_32_32(ao_int64_t *r, uint32_t a, uint32_t b)
68 {
69         uint32_t        r1;
70         uint32_t        r2, r3, r4;
71         ao_int64_t      s,t,u,v;
72         r1 = (uint32_t) (uint16_t) a * (uint16_t) b;
73         r2 = (uint32_t) (uint16_t) (a >> 16) * (uint16_t) b;
74         r3 = (uint32_t) (uint16_t) a * (uint16_t) (b >> 16);
75         r4 = (uint32_t) (uint16_t) (a >> 16) * (uint16_t) (b >> 16);
76
77         s.low = r1;
78         s.high = r4;
79
80         t.high = r2 >> 16;
81         t.low = r2 << 16;
82         ao_plus64(&u, &s, &t);
83
84         v.high = r3 >> 16;
85         v.low = r3 << 16;
86         ao_plus64(r, &u, &v);
87 }
88
89 void ao_neg64(ao_int64_t *r, ao_int64_t *a) {
90         r->high = ~a->high;
91         r->low = ~a->low;
92         if (!++r->low)
93                 r->high++;
94 }
95
96 void ao_mul64_32_32(ao_int64_t *r, int32_t a, int32_t b) {
97         uint8_t         negative = 0;
98
99         if (a < 0) {
100                 a = -a;
101                 negative = ~0;
102         }
103         if (b < 0) {
104                 b = -b;
105                 negative = ~negative;
106         }
107         ao_umul64_32_32(r, a, b);
108         if (negative)
109                 ao_neg64(r, r);
110 }
111
112 static void ao_umul64(ao_int64_t *r, ao_int64_t *a, ao_int64_t *b) {
113         ao_int64_t      r2, r3;
114
115         ao_umul64_32_32(&r2, a->high, b->low);
116         ao_umul64_32_32(&r3, a->low, b->high);
117         ao_umul64_32_32(r, a->low, b->low);
118
119         r->high += r2.low + r3.low;
120 }
121
122 void ao_mul64(ao_int64_t *r, ao_int64_t *a, ao_int64_t *b) {
123         uint8_t negative = 0;
124         ao_int64_t      ap, bp;
125
126         if (ao_int64_negativep(a)) {
127                 ao_neg64(&ap, a);
128                 a = &ap;
129                 negative = ~0;
130         }
131         if (ao_int64_negativep(b)) {
132                 ao_neg64(&bp, b);
133                 b = &bp;
134                 negative = ~negative;
135         }
136         ao_umul64(r, a, b);
137         if (negative)
138                 ao_neg64(r, r);
139 }
140
141 void ao_umul64_64_16(ao_int64_t *r, ao_int64_t *a, uint16_t b) {
142         uint32_t h = a->high * b;
143         ao_umul64_32_32(r, a->low, b);
144         r->high += h;
145 }
146
147 void ao_mul64_64_16(ao_int64_t *r, ao_int64_t *a, uint16_t b) {
148         ao_int64_t      ap;
149         uint8_t         negative = 0;
150         if ((int32_t) a->high < 0) {
151                 ao_neg64(&ap, a);
152                 a = &ap;
153                 negative = ~0;
154         } else
155                 ao_umul64_64_16(r, a, b);
156         if (negative)
157                 ao_neg64(r, r);
158 }