2 * Copyright © 2013 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 void ao_plus64(ao_int64_t *r, ao_int64_t *a, ao_int64_t *b) {
23 r->high = a->high + b->high;
30 void ao_rshift64(ao_int64_t *r, ao_int64_t *a, uint8_t d) {
34 r->low |= a->high << (32 - d);
35 r->high = (int32_t) a->high >> d;
38 r->low = (int32_t) a->high >> d;
43 void ao_lshift64(ao_int64_t *r, ao_int64_t *a, uint8_t d) {
45 r->high = a->high << d;
47 r->high |= a->low >> (32 - d);
51 r->high = a->low << d;
56 static void ao_umul64_32_32(ao_int64_t *r, uint32_t a, uint32_t b)
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);
71 ao_plus64(&u, &s, &t);
78 void ao_neg64(ao_int64_t *r, ao_int64_t *a) {
85 void ao_mul64_32_32(ao_int64_t *r, int32_t a, int32_t b) {
96 ao_umul64_32_32(r, a, b);
101 static void ao_umul64(ao_int64_t *r, ao_int64_t *a, ao_int64_t *b) {
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);
108 r->high += r2.low + r3.low;
111 void ao_mul64(ao_int64_t *r, ao_int64_t *a, ao_int64_t *b) {
112 uint8_t negative = 0;
115 if (ao_int64_negativep(a)) {
120 if (ao_int64_negativep(b)) {
123 negative = ~negative;
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);
136 void ao_mul64_64_16(ao_int64_t *r, ao_int64_t *a, uint16_t b) {
138 uint8_t negative = 0;
139 if ((int32_t) a->high < 0) {
144 ao_umul64_64_16(r, a, b);