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
/*
Copyright (C) 2009 William Hart
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 "gmpcompat.h"
#include "fmpz.h"
void
fmpz_mod(fmpz_t f, const fmpz_t g, const fmpz_t h)
{
fmpz c1 = *g;
fmpz c2 = *h;
if (!COEFF_IS_MPZ(c1)) /* g is small */
{
if (!COEFF_IS_MPZ(c2)) /* h is also small */
{
slong r;
if (c2 < WORD(0))
c2 = -c2;
if (c1 < WORD(0))
{
r = c2 - (-c1 % c2); /* C doesn't correctly handle negative mods */
if (r == c2)
r = 0;
}
else
r = c1 % c2;
fmpz_set_si(f, r);
}
else /* h is large and g is small */
{
if (c1 < WORD(0))
{
fmpz_abs(f, h);
fmpz_sub_ui(f, f, -c1);
}
else
fmpz_set_ui(f, c1);
}
}
else /* g is large */
{
if (!COEFF_IS_MPZ(c2)) /* h is small */
{
if (c2 < WORD(0))
fmpz_set_si(f, flint_mpz_fdiv_ui(COEFF_TO_PTR(c1), -c2));
else
fmpz_set_ui(f, flint_mpz_fdiv_ui(COEFF_TO_PTR(c1), c2));
}
else /* both are large */
{
if (MPZ_WANT_FLINT_DIVISION(COEFF_TO_PTR(c1), COEFF_TO_PTR(c2)))
{
_fmpz_mod_newton(f, g, h);
}
else
{
mpz_ptr mf = _fmpz_promote(f);
mpz_mod(mf, COEFF_TO_PTR(c1), COEFF_TO_PTR(c2));
_fmpz_demote_val(f); /* reduction mod h may result in small value */
}
}
}
}