-
Notifications
You must be signed in to change notification settings - Fork 1
/
my.c
434 lines (388 loc) · 11.9 KB
/
my.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
Calculate Mathematical Constants Using GMP (GNU Multiple Precision)
Copyright (C) 2006-2012 Hanhong Xue (macroxue at yahoo dot 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; either version 2 of the License, or (at your option) any later
version.
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 <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "my.h"
mpf_t t1, t2;
mpf_t d1, d2;
void my_init(unsigned long prec)
{
mpf_init2(t1, prec);
mpf_init2(t2, prec);
mpf_init2(d1, prec);
mpf_init2(d2, prec);
}
void my_clear()
{
mpf_clear(t1);
mpf_clear(t2);
mpf_clear(d1);
mpf_clear(d2);
}
// r = sqrt(x)
void my_sqrt_ui(mpf_t r, unsigned long x)
{
unsigned long prec, bits, prec0;
prec0 = mpf_get_prec(r);
if (prec0<=DOUBLE_PREC) {
mpf_set_d(r, sqrt(x));
return;
}
bits = 0;
for (prec=prec0; prec>DOUBLE_PREC;) {
int bit = prec&1;
prec = (prec+bit)/2;
bits = bits*2+bit;
}
mpf_set_prec_raw(t1, DOUBLE_PREC);
mpf_set_d(t1, 1/sqrt(x));
while (prec<prec0) {
prec *=2;
if (prec<prec0) {
/* t1 = t1+t1*(1-x*t1*t1)/2; */
mpf_set_prec_raw(t2, prec);
mpf_mul(t2, t1, t1); // half x half -> full
mpf_mul_ui(t2, t2, x);
mpf_ui_sub(t2, 1, t2);
mpf_set_prec_raw(t2, prec/2);
mpf_div_2exp(t2, t2, 1);
mpf_mul(t2, t2, t1); // half x half -> half
mpf_set_prec_raw(t1, prec);
mpf_add(t1, t1, t2);
} else {
prec = prec0;
/* t2=x*t1, t1 = t2+t1*(x-t2*t2)/2; */
mpf_set_prec_raw(t2, prec/2);
mpf_mul_ui(t2, t1, x);
mpf_mul(r, t2, t2); // half x half -> full
mpf_ui_sub(r, x, r);
mpf_mul(t1, t1, r); // half x half -> half
mpf_div_2exp(t1, t1, 1);
mpf_add(r, t1, t2);
break;
}
prec -= (bits&1);
bits /=2;
}
}
// r = 1/sqrt(x)
void my_invsqrt_ui(mpf_t r, unsigned long x)
{
unsigned long prec, bits, prec0;
prec0 = mpf_get_prec(r);
if (prec0<=DOUBLE_PREC) {
mpf_set_d(r, sqrt(x));
return;
}
bits = 0;
for (prec=prec0; prec>DOUBLE_PREC;) {
int bit = prec&1;
prec = (prec+bit)/2;
bits = bits*2+bit;
}
mpf_set_prec_raw(t1, DOUBLE_PREC);
mpf_set_d(t1, 1/sqrt(x));
while (prec<prec0) {
prec *=2;
if (prec<prec0) {
/* t1 = t1+t1*(1-x*t1*t1)/2; */
mpf_set_prec_raw(t2, prec);
mpf_mul(t2, t1, t1); // half x half -> full
mpf_mul_ui(t2, t2, x);
mpf_ui_sub(t2, 1, t2);
mpf_set_prec_raw(t2, prec/2);
mpf_div_2exp(t2, t2, 1);
mpf_mul(t2, t2, t1); // half x half -> half
mpf_set_prec_raw(t1, prec);
mpf_add(t1, t1, t2);
} else {
prec = prec0;
mpf_set_prec_raw(t2, prec);
mpf_mul(t2, t1, t1); // half x half -> full
mpf_mul_ui(t2, t2, x);
mpf_ui_sub(t2, 1, t2);
mpf_set_prec_raw(t2, prec/2);
mpf_div_2exp(t2, t2, 1);
mpf_mul(t2, t2, t1); // half x half -> half
mpf_add(r, t1, t2);
break;
}
prec -= (bits&1);
bits /=2;
}
}
// r = y/x WARNING: r cannot be the same as y.
void my_div(mpf_t r, mpf_t y, mpf_t x)
{
unsigned long prec, bits, prec0;
assert(r != y);
prec0 = mpf_get_prec(r);
if (prec0<=DOUBLE_PREC) {
mpf_set_d(r, mpf_get_d(y)/mpf_get_d(x));
return;
}
bits = 0;
for (prec=prec0; prec>DOUBLE_PREC;) {
int bit = prec&1;
prec = (prec+bit)/2;
bits = bits*2+bit;
}
mpf_set_prec_raw(t1, DOUBLE_PREC);
mpf_set_d(t1, 1/mpf_get_d(x));
while (prec<prec0) {
prec *=2;
if (prec<prec0) {
/* t1 = t1+t1*(1-x*t1); */
mpf_set_prec_raw(t2, prec);
mpf_mul(t2, x, t1); // full x half -> full
mpf_ui_sub(t2, 1, t2);
mpf_set_prec_raw(t2, prec/2);
mpf_mul(t2, t2, t1); // half x half -> half
mpf_set_prec_raw(t1, prec);
mpf_add(t1, t1, t2);
} else {
prec = prec0;
/* t2=y*t1, t1 = t2+t1*(y-x*t2); */
mpf_set_prec_raw(t2, prec/2);
mpf_mul(t2, t1, y); // half x half -> half
mpf_mul(r, x, t2); // full x half -> full
mpf_sub(r, y, r);
mpf_mul(t1, t1, r); // half x half -> half
mpf_add(r, t1, t2);
break;
}
prec -= (bits&1);
bits /=2;
}
}
// r = sqrt(x)
void my_sqrt(mpf_t r, mpf_t x)
{
unsigned prec, bits, prec0;
prec0 = mpf_get_prec(r);
if (prec0 <= DOUBLE_PREC) {
mpf_set_d(r, sqrt(mpf_get_d(x)));
return;
}
bits = 0;
for (prec = prec0; prec > DOUBLE_PREC;) {
int bit = prec & 1;
prec = (prec + bit) / 2;
bits = bits * 2 + bit;
}
mpf_set_prec_raw(t1, DOUBLE_PREC);
mpf_set_d(t1, 1 / sqrt(mpf_get_d(x)));
while (prec < prec0) {
prec *= 2;
/*printf("prec=%d, prec0=%d\n", prec, prec0); */
if (prec < prec0) {
/* t1 = t1+t1*(1-x*t1*t1)/2; */
mpf_set_prec_raw(t2, prec);
mpf_mul(t2, t1, t1);
mpf_set_prec_raw(x, prec/2);
mpf_mul(t2, t2, x);
mpf_ui_sub(t2, 1, t2);
mpf_set_prec_raw(t2, prec/2);
mpf_div_2exp(t2, t2, 1);
mpf_mul(t2, t2, t1);
mpf_set_prec_raw(t1, prec);
mpf_add(t1, t1, t2);
} else {
prec = prec0;
/* t2=x*t1, t1 = t2+t1*(x-t2*t2)/2; */
mpf_set_prec_raw(t2, prec/2);
mpf_set_prec_raw(x, prec/2);
mpf_mul(t2, t1, x);
mpf_mul(r, t2, t2);
mpf_set_prec_raw(x, prec);
mpf_sub(r, x, r);
mpf_mul(t1, t1, r);
mpf_div_2exp(t1, t1, 1);
mpf_add(r, t1, t2);
break;
}
prec -= (bits & 1);
bits /= 2;
}
}
// Exact Division
unsigned long div_threshold = 1404000;
void my_divexact(mpz_t r, mpz_t y, mpz_t x)
{
unsigned long prec = mpz_sizeinbase(y, 2);
unsigned long prec2 = mpz_sizeinbase(x, 2);
if (prec >= div_threshold) {
mpf_set_prec_raw(d1, prec);
mpf_set_prec_raw(d2, prec);
mpf_set_z(d1, y);
mpf_set_z(d2, x);
mpf_div_2exp(d1, d1, prec);
mpf_div_2exp(d2, d2, prec2);
my_div(d2, d1, d2);
mpf_mul_2exp(d2, d2, prec-prec2);
mpf_set_d(d1, 0.5);
mpf_add(d2, d2, d1);
mpz_set_f(y, d2);
} else {
mpz_divexact(y, y, x);
//mpz_tdiv_q(y, y, x);
}
}
unsigned long my_div_estimate_threshold()
{
const double log10 = 3.321928;
clock_t begin, end, diff1, diff2;
mpz_t y, x;
unsigned long digits = 100, min_digits, max_digits, prec, stage = 1;
div_threshold = 0;
for (;;) {
if (stage == 1)
digits *= 2;
else
digits = (min_digits + max_digits)/2;
prec = (unsigned long) (digits * log10) + 32;
my_init(prec);
mpz_init(y);
mpz_init(x);
mpz_ui_pow_ui(y, 10, digits);
mpz_ui_pow_ui(x, 10, digits/10);
begin = clock();
my_divexact(y, y, x);
end = clock();
diff1 = end - begin;
mpz_ui_pow_ui(y, 10, digits);
begin = clock();
mpz_divexact(y, y, x);
end = clock();
diff2 = end - begin;
my_clear();
mpz_clear(y);
mpz_clear(x);
printf("digits %ld, my %ld, mpz %ld\n", digits, diff1, diff2);
if (stage == 1) {
if (diff1 >= 10 && diff1 < diff2) {
stage = 2;
min_digits = digits/2;
max_digits = digits;
}
} else {
if (diff1 < diff2)
max_digits = digits;
else
min_digits = digits;
if (max_digits - min_digits < 100)
break;
}
}
return (unsigned long) (digits * log10);
}
void my_div_set_threshold(unsigned long t)
{
div_threshold = t;
}
// Convert Hexadecimal to Decimal
#define UNIT_SIZE 5
#define UNIT_MOD (int)1e5
#define LINE_SIZE (UNIT_SIZE * 10)
#define NUM_BLOCKS 4
char out_buf[(LINE_SIZE+30)*10], *out_ptr = out_buf;
mpf_t trunk;
void utoa(unsigned long i, unsigned long d)
{
static unsigned long pow10[] = { 0, 10, 100, 1000, 10000, 100000,
1000000, 10000000, 100000000, 1000000000
};
if (d == 0) {
for (d = 0; d < sizeof(pow10)/sizeof(pow10[0]); d++)
if (i < pow10[d]) break;
}
while (--d) {
*out_ptr = i / pow10[d];
i = i - *out_ptr * pow10[d];
*out_ptr++ += '0';
}
*out_ptr++ = i + '0';
}
void flush_out(FILE *fp)
{
*out_ptr++ = '\n';
*out_ptr = '\0';
fputs(out_buf, fp);
out_ptr = out_buf;
}
void my_out_str_raw(FILE *fp, unsigned long digits, mpf_t f, unsigned long offset)
{
unsigned long d;
if (digits <= LINE_SIZE*NUM_BLOCKS) {
unsigned long cursor = offset % LINE_SIZE;
for (d = 0; d < digits; ) {
mpf_set_prec_raw(f, (int)((digits-d)*BITS_PER_DIGIT+1));
mpf_mul_ui(f, f, UNIT_MOD);
unsigned long i = mpf_get_ui(f);
mpf_sub_ui(f, f, i);
utoa(i, UNIT_SIZE);
*out_ptr++ = ' ';
d += UNIT_SIZE;
cursor += UNIT_SIZE;
if (cursor == LINE_SIZE) {
cursor = 0;
*out_ptr++ = ':';
*out_ptr++ = ' ';
utoa(offset + d, 0);
*out_ptr++ = '\n';
if ((offset + d) % (LINE_SIZE*10) == 0)
flush_out(fp);
}
}
} else {
mpf_t block, mod;
unsigned long num_units = (digits + UNIT_SIZE-1)/UNIT_SIZE;
unsigned long block_size = (num_units + NUM_BLOCKS-1)/NUM_BLOCKS*UNIT_SIZE;
mpf_set_default_prec((int)(block_size*BITS_PER_DIGIT+1));
mpf_init(block);
mpf_init_set_ui(mod, 10);
mpf_pow_ui(mod, mod, block_size);
for (d = 0; d < digits; d += block_size) {
unsigned long size = block_size < digits - d ? block_size : digits - d;
mpf_set_prec_raw(block, (int)(size*BITS_PER_DIGIT+1));
mpf_set(block, f);
my_out_str_raw(fp, size, block, offset+d);
if (block_size < digits - d) {
mpf_set_prec_raw(f, (int)((digits-d)*BITS_PER_DIGIT+1));
mpf_mul(f, f, mod);
mpf_floor(trunk, f);
mpf_sub(f, f, trunk);
}
}
mpf_clear(block);
mpf_clear(mod);
}
}
void my_out_str(FILE *fp, unsigned long base, unsigned long digits, mpf_t f)
{
unsigned long num_units = (digits + UNIT_SIZE-1)/UNIT_SIZE;
unsigned long block_size = (num_units + NUM_BLOCKS-1)/NUM_BLOCKS*UNIT_SIZE;
mpf_init2(trunk, (int)(block_size*BITS_PER_DIGIT+1));
unsigned long i = mpf_get_ui(f);
fprintf(fp, "%lu.\n", i);
mpf_sub_ui(f, f, i);
my_out_str_raw(fp, digits, f, 0);
if (out_ptr != out_buf)
flush_out(fp);
mpf_clear(trunk);
}