altos: Add 64x64 multiply. Test 64 ops for dest same as either source
[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_rshift64(ao_int64_t *r, ao_int64_t *a, uint8_t d) {
31         if (d < 32) {
32                 r->low = a->low >> d;
33                 if (d)
34                         r->low |= a->high << (32 - d);
35                 r->high = (int32_t) a->high >> d;
36         } else {
37                 d &= 0x1f;
38                 r->low = (int32_t) a->high >> d;
39                 r->high = 0;
40         }
41 }
42
43 void ao_lshift64(ao_int64_t *r, ao_int64_t *a, uint8_t d) {
44         if (d < 32) {
45                 r->high = a->high << d;
46                 if (d)
47                         r->high |= a->low >> (32 - d);
48                 r->low = a->low << d;
49         } else {
50                 d &= 0x1f;
51                 r->high = a->low << d;
52                 r->low = 0;
53         }
54 }
55
56 static void ao_umul64_32_32(ao_int64_t *r, uint32_t a, uint32_t b)
57 {
58         uint32_t        r1;
59         uint32_t        r2, r3, r4;
60         ao_int64_t      s,t,u,v;
61         r1 = (uint32_t) (uint16_t) a * (uint16_t) b;
62         r2 = (uint32_t) (uint16_t) (a >> 16) * (uint16_t) b;
63         r3 = (uint32_t) (uint16_t) a * (uint16_t) (b >> 16);
64         r4 = (uint32_t) (uint16_t) (a >> 16) * (uint16_t) (b >> 16);
65
66         s.low = r1;
67         s.high = r4;
68
69         t.high = r2 >> 16;
70         t.low = r2 << 16;
71         ao_plus64(&u, &s, &t);
72
73         v.high = r3 >> 16;
74         v.low = r3 << 16;
75         ao_plus64(r, &u, &v);
76 }
77
78 void ao_neg64(ao_int64_t *r, ao_int64_t *a) {
79         r->high = ~a->high;
80         r->low = ~a->low;
81         if (!++r->low)
82                 r->high++;
83 }
84
85 void ao_mul64_32_32(ao_int64_t *r, int32_t a, int32_t b) {
86         uint8_t         negative = 0;
87
88         if (a < 0) {
89                 a = -a;
90                 negative = ~0;
91         }
92         if (b < 0) {
93                 b = -b;
94                 negative = ~negative;
95         }
96         ao_umul64_32_32(r, a, b);
97         if (negative)
98                 ao_neg64(r, r);
99 }
100
101 static void ao_umul64(ao_int64_t *r, ao_int64_t *a, ao_int64_t *b) {
102         ao_int64_t      r2, r3;
103
104         ao_umul64_32_32(&r2, a->high, b->low);
105         ao_umul64_32_32(&r3, a->low, b->high);
106         ao_umul64_32_32(r, a->low, b->low);
107
108         r->high += r2.low + r3.low;
109 }
110
111 void ao_mul64(ao_int64_t *r, ao_int64_t *a, ao_int64_t *b) {
112         uint8_t negative = 0;
113         ao_int64_t      ap, bp;
114
115         if (ao_int64_negativep(a)) {
116                 ao_neg64(&ap, a);
117                 a = &ap;
118                 negative = ~0;
119         }
120         if (ao_int64_negativep(b)) {
121                 ao_neg64(&bp, b);
122                 b = &bp;
123                 negative = ~negative;
124         }
125         ao_umul64(r, a, b);
126         if (negative)
127                 ao_neg64(r, r);
128 }
129
130 void ao_umul64_64_16(ao_int64_t *r, ao_int64_t *a, uint16_t b) {
131         uint32_t h = a->high * b;
132         ao_umul64_32_32(r, a->low, b);
133         r->high += h;
134 }
135
136 void ao_mul64_64_16(ao_int64_t *r, ao_int64_t *a, uint16_t b) {
137         ao_int64_t      ap;
138         uint8_t         negative = 0;
139         if ((int32_t) a->high < 0) {
140                 ao_neg64(&ap, a);
141                 a = &ap;
142                 negative = ~0;
143         } else
144                 ao_umul64_64_16(r, a, b);
145         if (negative)
146                 ao_neg64(r, r);
147 }