altos: Make sure we don't beep out continuity twice in idle mode
[fw/altos] / src / kernel / 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(__pdata ao_int64_t *r, __pdata ao_int64_t *a, __pdata ao_int64_t *b) __FATTR {
21         __LOCAL 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(__pdata ao_int64_t *r, __pdata ao_int64_t *a, __pdata ao_int64_t *b) __FATTR {
31         __LOCAL uint32_t        t;
32
33         r->high = a->high - b->high;
34         t = a->low - b->low;
35         if (t > a->low)
36                 r->high--;
37         r->low = t;
38 }
39
40 void ao_rshift64(__pdata ao_int64_t *r, __pdata ao_int64_t *a, uint8_t d) __FATTR {
41         if (d < 32) {
42                 r->low = a->low >> d;
43                 if (d)
44                         r->low |= a->high << (32 - d);
45                 r->high = (int32_t) a->high >> d;
46         } else {
47                 d &= 0x1f;
48                 r->low = (int32_t) a->high >> d;
49                 r->high = 0;
50         }
51 }
52
53 void ao_lshift64(__pdata ao_int64_t *r, __pdata ao_int64_t *a, uint8_t d) __FATTR {
54         if (d < 32) {
55                 r->high = a->high << d;
56                 if (d)
57                         r->high |= a->low >> (32 - d);
58                 r->low = a->low << d;
59         } else {
60                 d &= 0x1f;
61                 r->high = a->low << d;
62                 r->low = 0;
63         }
64 }
65
66 static void ao_umul64_32_32(__ARG ao_int64_t *r, uint32_t a, uint32_t b) __reentrant {
67         __LOCAL uint32_t        s;
68         __LOCAL ao_int64_t      t;
69         r->low = (uint32_t) (uint16_t) a * (uint16_t) b;
70         r->high = (uint32_t) (uint16_t) (a >> 16) * (uint16_t) (b >> 16);
71
72         s = (uint32_t) (uint16_t) (a >> 16) * (uint16_t) b;
73
74         t.high = s >> 16;
75         t.low = s << 16;
76         ao_plus64(r, r, &t);
77
78         s = (uint32_t) (uint16_t) a * (uint16_t) (b >> 16);
79
80         t.high = s >> 16;
81         t.low = s << 16;
82         ao_plus64(r, r, &t);
83 }
84
85 void ao_neg64(__pdata ao_int64_t *r, __pdata ao_int64_t *a) __FATTR {
86         r->high = ~a->high;
87         if (!(r->low = ~a->low + 1))
88                 r->high++;
89 }
90
91 void ao_mul64_32_32(__ARG ao_int64_t *r, int32_t a, int32_t b) __FATTR {
92         uint8_t         negative = 0;
93
94         if (a < 0) {
95                 a = -a;
96                 ++negative;
97         }
98         if (b < 0) {
99                 b = -b;
100                 --negative;
101         }
102         ao_umul64_32_32(r, a, b);
103         if (negative)
104                 ao_neg64(r, r);
105 }
106
107 static void ao_umul64(__ARG ao_int64_t *r, __ARG ao_int64_t *a, __ARG ao_int64_t *b) __reentrant {
108         __LOCAL ao_int64_t      r2, r3;
109
110         ao_umul64_32_32(&r2, a->high, b->low);
111         ao_umul64_32_32(&r3, a->low, b->high);
112         ao_umul64_32_32(r, a->low, b->low);
113
114         r->high += r2.low + r3.low;
115 }
116
117 static __ARG ao_int64_t ap, bp;
118
119 void ao_mul64(__ARG ao_int64_t *r, __ARG ao_int64_t *a, __ARG ao_int64_t *b) __FATTR {
120         uint8_t negative = 0;
121
122         if (ao_int64_negativep(a)) {
123                 ao_neg64(&ap, a);
124                 a = &ap;
125                 ++negative;
126         }
127         if (ao_int64_negativep(b)) {
128                 ao_neg64(&bp, b);
129                 b = &bp;
130                 --negative;
131         }
132         ao_umul64(r, a, b);
133         if (negative)
134                 ao_neg64(r, r);
135 }
136
137 static void ao_umul64_64_16(__ARG ao_int64_t *r, __ARG ao_int64_t *a, uint16_t b) __reentrant {
138         __LOCAL uint32_t h;
139
140         h = a->high * b;
141         ao_umul64_32_32(r, a->low, b);
142         r->high += h;
143 }
144
145 void ao_mul64_64_16(__ARG ao_int64_t *r, __ARG ao_int64_t *a, __ARG uint16_t b) __FATTR {
146         uint8_t         negative = 0;
147
148         if ((int32_t) a->high < 0) {
149                 ao_neg64(&ap, a);
150                 a = &ap;
151                 negative++;
152         }
153         ao_umul64_64_16(r, a, b);
154         if (negative)
155                 ao_neg64(r, r);
156 }