-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathmul.c
256 lines (209 loc) · 5.9 KB
/
mul.c
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
Copyright (C) 2012 Fredrik Johansson
This file is part of Arb.
Arb is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version. See <http://www.gnu.org/licenses/>.
*/
#include "acb.h"
#define a acb_realref(x)
#define b acb_imagref(x)
#define c acb_realref(y)
#define d acb_imagref(y)
#define e acb_realref(z)
#define f acb_imagref(z)
#define ar arb_radref(a)
#define br arb_radref(b)
#define cr arb_radref(c)
#define dr arb_radref(d)
static void
_acb_sqr_fast(acb_t z, const acb_t x, slong prec)
{
int inexact;
mag_t am, bm, er, fr;
mag_fast_init_set_arf(am, arb_midref(a));
mag_fast_init_set_arf(bm, arb_midref(b));
mag_init(er);
mag_init(fr);
mag_fast_addmul(er, am, ar);
mag_fast_addmul(er, bm, br);
mag_fast_mul_2exp_si(er, er, 1);
mag_fast_addmul(er, ar, ar);
mag_fast_addmul(er, br, br);
mag_fast_addmul(fr, bm, ar);
mag_fast_addmul(fr, am, br);
mag_fast_addmul(fr, ar, br);
mag_fast_mul_2exp_si(fr, fr, 1);
inexact = arf_complex_sqr(arb_midref(e), arb_midref(f),
arb_midref(a), arb_midref(b), prec, ARB_RND);
if (inexact & 1)
arf_mag_add_ulp(arb_radref(e), er, arb_midref(e), prec);
else
mag_set(arb_radref(e), er);
if (inexact & 2)
arf_mag_add_ulp(arb_radref(f), fr, arb_midref(f), prec);
else
mag_set(arb_radref(f), fr);
}
static void
_acb_sqr_slow(acb_t z, const acb_t x, slong prec)
{
int inexact;
mag_t am, bm, er, fr;
mag_init_set_arf(am, arb_midref(a));
mag_init_set_arf(bm, arb_midref(b));
mag_init(er);
mag_init(fr);
mag_addmul(er, am, ar);
mag_addmul(er, bm, br);
mag_mul_2exp_si(er, er, 1);
mag_addmul(er, ar, ar);
mag_addmul(er, br, br);
mag_addmul(fr, bm, ar);
mag_addmul(fr, am, br);
mag_addmul(fr, ar, br);
mag_mul_2exp_si(fr, fr, 1);
inexact = arf_complex_sqr(arb_midref(e), arb_midref(f),
arb_midref(a), arb_midref(b), prec, ARB_RND);
if (inexact & 1)
arf_mag_add_ulp(arb_radref(e), er, arb_midref(e), prec);
else
mag_swap(arb_radref(e), er);
if (inexact & 2)
arf_mag_add_ulp(arb_radref(f), fr, arb_midref(f), prec);
else
mag_swap(arb_radref(f), fr);
mag_clear(am);
mag_clear(bm);
mag_clear(er);
mag_clear(fr);
}
static void
_acb_mul_fast(acb_t z, const acb_t x, const acb_t y, slong prec)
{
int inexact;
mag_t am, bm, cm, dm, er, fr;
mag_fast_init_set_arf(am, arb_midref(a));
mag_fast_init_set_arf(bm, arb_midref(b));
mag_fast_init_set_arf(cm, arb_midref(c));
mag_fast_init_set_arf(dm, arb_midref(d));
mag_init(er);
mag_init(fr);
mag_fast_addmul(er, am, cr);
mag_fast_addmul(er, bm, dr);
mag_fast_addmul(er, cm, ar);
mag_fast_addmul(er, dm, br);
mag_fast_addmul(er, ar, cr);
mag_fast_addmul(er, br, dr);
mag_fast_addmul(fr, am, dr);
mag_fast_addmul(fr, bm, cr);
mag_fast_addmul(fr, cm, br);
mag_fast_addmul(fr, dm, ar);
mag_fast_addmul(fr, br, cr);
mag_fast_addmul(fr, ar, dr);
inexact = arf_complex_mul(arb_midref(e), arb_midref(f),
arb_midref(a), arb_midref(b),
arb_midref(c), arb_midref(d), prec, ARB_RND);
if (inexact & 1)
arf_mag_add_ulp(arb_radref(e), er, arb_midref(e), prec);
else
mag_set(arb_radref(e), er);
if (inexact & 2)
arf_mag_add_ulp(arb_radref(f), fr, arb_midref(f), prec);
else
mag_set(arb_radref(f), fr);
}
static void
_acb_mul_slow(acb_t z, const acb_t x, const acb_t y, slong prec)
{
int inexact;
mag_t am, bm, cm, dm, er, fr;
mag_init_set_arf(am, arb_midref(a));
mag_init_set_arf(bm, arb_midref(b));
mag_init_set_arf(cm, arb_midref(c));
mag_init_set_arf(dm, arb_midref(d));
mag_init(er);
mag_init(fr);
mag_addmul(er, am, cr);
mag_addmul(er, bm, dr);
mag_addmul(er, cm, ar);
mag_addmul(er, dm, br);
mag_addmul(er, ar, cr);
mag_addmul(er, br, dr);
mag_addmul(fr, am, dr);
mag_addmul(fr, bm, cr);
mag_addmul(fr, cm, br);
mag_addmul(fr, dm, ar);
mag_addmul(fr, br, cr);
mag_addmul(fr, ar, dr);
inexact = arf_complex_mul(arb_midref(e), arb_midref(f),
arb_midref(a), arb_midref(b),
arb_midref(c), arb_midref(d), prec, ARB_RND);
if (inexact & 1)
arf_mag_add_ulp(arb_radref(e), er, arb_midref(e), prec);
else
mag_swap(arb_radref(e), er);
if (inexact & 2)
arf_mag_add_ulp(arb_radref(f), fr, arb_midref(f), prec);
else
mag_swap(arb_radref(f), fr);
mag_clear(am);
mag_clear(bm);
mag_clear(cm);
mag_clear(dm);
mag_clear(er);
mag_clear(fr);
}
void
acb_mul(acb_t z, const acb_t x, const acb_t y, slong prec)
{
if (arb_is_zero(b))
{
arb_mul(f, d, a, prec);
arb_mul(e, c, a, prec);
}
else if (arb_is_zero(d))
{
arb_mul(f, b, c, prec);
arb_mul(e, a, c, prec);
}
else if (arb_is_zero(a))
{
arb_mul(e, c, b, prec);
arb_mul(f, d, b, prec);
acb_mul_onei(z, z);
}
else if (arb_is_zero(c))
{
arb_mul(e, a, d, prec);
arb_mul(f, b, d, prec);
acb_mul_onei(z, z);
}
/* squaring = a^2-b^2, 2ab */
else if (x == y)
{
if (ARB_IS_LAGOM(a) && ARB_IS_LAGOM(b))
_acb_sqr_fast(z, x, prec);
else
_acb_sqr_slow(z, x, prec);
}
else
{
if (ARB_IS_LAGOM(a) && ARB_IS_LAGOM(b) &&
ARB_IS_LAGOM(c) && ARB_IS_LAGOM(d))
_acb_mul_fast(z, x, y, prec);
else
_acb_mul_slow(z, x, y, prec);
}
}
#undef a
#undef b
#undef c
#undef d
#undef e
#undef f
#undef ar
#undef br
#undef cr
#undef dr