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
/*
Copyright (C) 2021 Albin Ahlbäck
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 "fmpz.h"
/* b aliases neither q nor r */
static void _fmpz_ndiv_qr(fmpz_t q, fmpz_t r, const fmpz_t a, const fmpz_t b)
{
int c, rbsgn;
fmpz_tdiv_qr(q, r, a, b);
c = fmpz_cmp2abs(b, r);
if (c > 0)
return;
rbsgn = fmpz_sgn(r)*fmpz_sgn(b);
if (c < 0)
{
if (rbsgn < 0)
{
fmpz_sub_ui(q, q, 1);
fmpz_add(r, r, b);
}
else
{
fmpz_add_ui(q, q, 1);
fmpz_sub(r, r, b);
}
}
else
{
int qsgn = fmpz_sgn(q);
if (rbsgn < 0 && qsgn > 0)
{
fmpz_sub_ui(q, q, 1);
fmpz_add(r, r, b);
}
else if (rbsgn > 0 && qsgn < 0)
{
fmpz_add_ui(q, q, 1);
fmpz_sub(r, r, b);
}
}
}
void
fmpz_ndiv_qr(fmpz_t q, fmpz_t r, const fmpz_t a, const fmpz_t b)
{
slong A = *a;
slong B = *b;
if (fmpz_is_zero(b))
{
flint_throw(FLINT_DIVZERO, "Exception: division by zero in fmpz_ndiv_qr\n");
}
if (!COEFF_IS_MPZ(A) && !COEFF_IS_MPZ(B))
{
slong lquo, lrem;
_fmpz_demote(q);
_fmpz_demote(r);
if (FLINT_ABS(*b) == 1) /* avoid overflow in case */
{ /* a = 2^(SMALL_FMPZ_BITCOUNT_MAX) */
fmpz_set_si(q, A * FLINT_SGN(B));
fmpz_zero(r);
return;
}
*q = A / B;
*r = A - B * *q;
lquo = *q + FLINT_SGN(A) * FLINT_SGN(B);
lrem = A - B * lquo;
if (FLINT_ABS(lrem) < FLINT_ABS(*r))
{
*q = lquo;
*r = lrem;
}
}
else
{
if (b == q)
{
fmpz_t t;
fmpz_init(t);
_fmpz_ndiv_qr(t, r, a, b);
fmpz_swap(q, t);
fmpz_clear(t);
}
else if (b == r)
{
fmpz_t t;
fmpz_init(t);
_fmpz_ndiv_qr(q, t, a, b);
fmpz_swap(r, t);
fmpz_clear(t);
}
else
{
_fmpz_ndiv_qr(q, r, a, b);
}
}
}