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
/*
Copyright (C) 2011 Fredrik Johansson
Copyright (C) 2020 Daniel Schultz
This file is part of FLINT.
FLINT 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 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include "fmpq.h"
#define ROT(u,v,t) \
do { fmpz _t = *u; *u = *v; *v = *t; *t = _t; } while (0);
int
_fmpq_reconstruct_fmpz_2_naive(fmpz_t n, fmpz_t d,
const fmpz_t a, const fmpz_t m, const fmpz_t N, const fmpz_t D)
{
fmpz_t q, r, s, t;
int success = 0;
/* Quickly identify small integers */
if (fmpz_cmp(a, N) <= 0)
{
fmpz_set(n, a);
fmpz_one(d);
return 1;
}
fmpz_sub(n, a, m);
if (fmpz_cmpabs(n, N) <= 0)
{
fmpz_one(d);
return 1;
}
fmpz_init(q);
fmpz_init(r);
fmpz_init(s);
fmpz_init(t);
fmpz_set(r, m); fmpz_zero(s);
fmpz_set(n, a); fmpz_one(d);
while (fmpz_cmpabs(n, N) > 0)
{
fmpz_fdiv_q(q, r, n);
fmpz_mul(t, q, n); fmpz_sub(t, r, t); ROT(r, n, t);
fmpz_mul(t, q, d); fmpz_sub(t, s, t); ROT(s, d, t);
}
if (fmpz_sgn(d) < 0)
{
fmpz_neg(n, n);
fmpz_neg(d, d);
}
if (fmpz_cmp(d, D) <= 0)
{
fmpz_gcd(t, n, d);
success = fmpz_is_one(t);
}
fmpz_clear(q);
fmpz_clear(r);
fmpz_clear(s);
fmpz_clear(t);
return success;
}