xref: /dragonfly/contrib/gmp/mpn/generic/trialdiv.c (revision ec21d9fb)
1 /* mpn_trialdiv -- find small factors of an mpn number using trial division.
2 
3    Contributed to the GNU project by Torbjorn Granlund.
4 
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.
8 
9 Copyright 2009 Free Software Foundation, Inc.
10 
11 This file is part of the GNU MP Library.
12 
13 The GNU MP Library is free software; you can redistribute it and/or modify
14 it under the terms of the GNU Lesser General Public License as published by
15 the Free Software Foundation; either version 3 of the License, or (at your
16 option) any later version.
17 
18 The GNU MP Library is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
21 License for more details.
22 
23 You should have received a copy of the GNU Lesser General Public License
24 along with the GNU MP Library.	If not, see http://www.gnu.org/licenses/.  */
25 
26 /*
27    Fast, division-free trial division for GMP.
28 
29    This function will find the first (smallest) factor represented in
30    trialdivtab.h.  It does not stop the factoring effort just because it has
31    reached some sensible limit, such as the square root of the input number.
32 
33    The caller can limit the factoring effort by passing NPRIMES.  The function
34    well then divide to *at least* that limit.  A position which only
35    mpn_trialdiv can make sense of is returned in the WHERE parameter.  It can
36    be used for restarting the factoring effort; the first call should pass 0
37    here.
38 */
39 
40 #include "gmp.h"
41 #include "gmp-impl.h"
42 
43 struct gmp_primes_dtab {
44   mp_limb_t binv;
45   mp_limb_t lim;
46 };
47 
48 struct gmp_primes_ptab {
49   mp_limb_t ppp;	/* primes, multiplied together */
50   mp_limb_t cps[7];	/* ppp values pre-computed for mpn_mod_1s_4p */
51   unsigned int idx:24;	/* index of  first primes in dtab */
52   unsigned int np :8;	/* number of primes related to this entry */
53 };
54 
55 #define P(p,inv,lim) {inv,lim}
56 
57 #include "trialdivtab.h"
58 
59 #define PTAB_LINES (sizeof (gmp_primes_ptab) / sizeof (gmp_primes_ptab[0]))
60 
61 /* Attempt to find a factor of T using trial division.
62    Input: A non-negative number T.
63    Output: non-zero if we found a factor, zero otherwise.  To get the actual
64    prime factor, compute the mod B inverse of the return value.  */
65 /* FIXME: We could optimize out one of the outer loop conditions if we
66    had a final ptab entry with a huge nd field.  */
67 mp_limb_t
68 mpn_trialdiv (mp_srcptr tp, mp_size_t tn, mp_size_t nprimes, int *where)
69 {
70   mp_limb_t ppp;
71   mp_limb_t *cps;
72   struct gmp_primes_dtab *dp;
73   long i, j, idx, np;
74   mp_limb_t r, q;
75 
76   ASSERT (tn >= 1);
77 
78   for (i = *where; i < PTAB_LINES; i++)
79     {
80       ppp = gmp_primes_ptab[i].ppp;
81       cps = gmp_primes_ptab[i].cps;
82 
83 #if __GNU_MP_VERSION == 4 && __GNU_MP_VERSION_MINOR < 4
84       if (tn < 4)
85 	r = mpn_mod_1 (tp, tn, ppp); /* FIXME */
86       else
87 #endif
88 	r = mpn_mod_1s_4p (tp, tn, ppp << cps[1], cps);
89 
90       idx = gmp_primes_ptab[i].idx;
91       np = gmp_primes_ptab[i].np;
92 
93       /* Check divisibility by individual primes.  */
94       dp = &gmp_primes_dtab[idx] + np;
95       for (j = -np; j < 0; j++)
96 	{
97 	  q = r * dp[j].binv;
98 	  if (q <= dp[j].lim)
99 	    {
100 	      *where = i;
101 	      return dp[j].binv;
102 	    }
103 	}
104 
105       nprimes -= np;
106       if (nprimes <= 0)
107 	return 0;
108     }
109   return 0;
110 }
111