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