beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / trialdiv.c
blob09cecfd410874814ebc8087c92293759a5a57e52
1 /* mpn_trialdiv -- find small factors of an mpn number using trial division.
3 Contributed to the GNU project by Torbjorn Granlund.
5 THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE. IT IS ONLY
6 SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
7 GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
9 Copyright 2009, 2010, 2012, 2013 Free Software Foundation, Inc.
11 This file is part of the GNU MP Library.
13 The GNU MP Library is free software; you can redistribute it and/or modify
14 it under the terms of either:
16 * the GNU Lesser General Public License as published by the Free
17 Software Foundation; either version 3 of the License, or (at your
18 option) any later version.
22 * the GNU General Public License as published by the Free Software
23 Foundation; either version 2 of the License, or (at your option) any
24 later version.
26 or both in parallel, as here.
28 The GNU MP Library is distributed in the hope that it will be useful, but
29 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
30 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
31 for more details.
33 You should have received copies of the GNU General Public License and the
34 GNU Lesser General Public License along with the GNU MP Library. If not,
35 see https://www.gnu.org/licenses/. */
38 This function finds the first (smallest) factor represented in
39 trialdivtab.h. It does not stop the factoring effort just because it has
40 reached some sensible limit, such as the square root of the input number.
42 The caller can limit the factoring effort by passing NPRIMES. The function
43 will then divide until that limit, or perhaps a few primes more. A position
44 which only mpn_trialdiv can make sense of is returned in the WHERE
45 parameter. It can be used for restarting the factoring effort; the first
46 call should pass 0 here.
48 Input: 1. A non-negative number T = {tp,tn}
49 2. NPRIMES as described above,
50 3. *WHERE as described above.
51 Output: 1. *WHERE updated as described above.
52 2. Return value is non-zero if we found a factor, else zero
53 To get the actual prime factor, compute the mod B inverse
54 of the return value.
57 #include "gmp.h"
58 #include "gmp-impl.h"
60 struct gmp_primes_dtab {
61 mp_limb_t binv;
62 mp_limb_t lim;
65 struct gmp_primes_ptab {
66 mp_limb_t ppp; /* primes, multiplied together */
67 mp_limb_t cps[7]; /* ppp values pre-computed for mpn_mod_1s_4p */
68 unsigned int idx:24; /* index of first primes in dtab */
69 unsigned int np :8; /* number of primes related to this entry */
73 static const struct gmp_primes_dtab gmp_primes_dtab[] =
75 #define WANT_dtab
76 #define P(p,inv,lim) {inv,lim}
77 #include "trialdivtab.h"
78 #undef WANT_dtab
79 #undef P
80 {0,0}
83 static const struct gmp_primes_ptab gmp_primes_ptab[] =
85 #define WANT_ptab
86 #include "trialdivtab.h"
87 #undef WANT_ptab
90 #define PTAB_LINES (sizeof (gmp_primes_ptab) / sizeof (gmp_primes_ptab[0]))
92 /* FIXME: We could optimize out one of the outer loop conditions if we
93 had a final ptab entry with a huge np field. */
94 mp_limb_t
95 mpn_trialdiv (mp_srcptr tp, mp_size_t tn, mp_size_t nprimes, int *where)
97 mp_limb_t ppp;
98 const mp_limb_t *cps;
99 const struct gmp_primes_dtab *dp;
100 long i, j, idx, np;
101 mp_limb_t r, q;
103 ASSERT (tn >= 1);
105 for (i = *where; i < PTAB_LINES; i++)
107 ppp = gmp_primes_ptab[i].ppp;
108 cps = gmp_primes_ptab[i].cps;
110 r = mpn_mod_1s_4p (tp, tn, ppp << cps[1], cps);
112 idx = gmp_primes_ptab[i].idx;
113 np = gmp_primes_ptab[i].np;
115 /* Check divisibility by individual primes. */
116 dp = &gmp_primes_dtab[idx] + np;
117 for (j = -np; j < 0; j++)
119 q = r * dp[j].binv;
120 if (q <= dp[j].lim)
122 *where = i;
123 return dp[j].binv;
127 nprimes -= np;
128 if (nprimes <= 0)
129 return 0;
131 return 0;