aboutsummaryrefslogtreecommitdiff
path: root/gmp-6.3.0/mpn/generic/divis.c
blob: f989ddb65179583af42dbcf4bede6ca4d7dbadc6 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* mpn_divisible_p -- mpn by mpn divisibility test

   THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
   CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
   FUTURE GNU MP RELEASES.

Copyright 2001, 2002, 2005, 2009, 2014, 2017, 2018 Free Software
Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:

  * the GNU Lesser General Public License as published by the Free
    Software Foundation; either version 3 of the License, or (at your
    option) any later version.

or

  * the GNU General Public License as published by the Free Software
    Foundation; either version 2 of the License, or (at your option) any
    later version.

or both in parallel, as here.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library.  If not,
see https://www.gnu.org/licenses/.  */

#include "gmp-impl.h"
#include "longlong.h"


/* Determine whether A={ap,an} is divisible by D={dp,dn}.  Must have both
   operands normalized, meaning high limbs non-zero, except that an==0 is
   allowed.

   There usually won't be many low zero bits on D, but the checks for this
   are fast and might pick up a few operand combinations, in particular they
   might reduce D to fit the single-limb mod_1/modexact_1 code.

   Future:

   Getting the remainder limb by limb would make an early exit possible on
   finding a non-zero.  This would probably have to be bdivmod style so
   there's no addback, but it would need a multi-precision inverse and so
   might be slower than the plain method (on small sizes at least).

   When D must be normalized (shifted to low bit set), it's possible to
   suppress the bit-shifting of A down, as long as it's already been checked
   that A has at least as many trailing zero bits as D.  */

int
mpn_divisible_p (mp_srcptr ap, mp_size_t an,
		 mp_srcptr dp, mp_size_t dn)
{
  mp_limb_t  alow, dlow, dmask;
  mp_ptr     qp, rp, tp;
  mp_limb_t di;
  unsigned  twos;
  int c;
  TMP_DECL;

  ASSERT (an >= 0);
  ASSERT (an == 0 || ap[an-1] != 0);
  ASSERT (dn >= 1);
  ASSERT (dp[dn-1] != 0);
  ASSERT_MPN (ap, an);
  ASSERT_MPN (dp, dn);

  /* When a<d only a==0 is divisible.
     Notice this test covers all cases of an==0. */
  if (an < dn)
    return (an == 0);

  /* Strip low zero limbs from d, requiring a==0 on those. */
  for (;;)
    {
      alow = *ap;
      dlow = *dp;

      if (dlow != 0)
	break;

      if (alow != 0)
	return 0;  /* a has fewer low zero limbs than d, so not divisible */

      /* a!=0 and d!=0 so won't get to n==0 */
      an--; ASSERT (an >= 1);
      dn--; ASSERT (dn >= 1);
      ap++;
      dp++;
    }

  /* a must have at least as many low zero bits as d */
  dmask = LOW_ZEROS_MASK (dlow);
  if ((alow & dmask) != 0)
    return 0;

  if (dn == 1)
    {
      if (ABOVE_THRESHOLD (an, BMOD_1_TO_MOD_1_THRESHOLD))
	return mpn_mod_1 (ap, an, dlow) == 0;

      count_trailing_zeros (twos, dlow);
      dlow >>= twos;
      return mpn_modexact_1_odd (ap, an, dlow) == 0;
    }

  count_trailing_zeros (twos, dlow);
  if (dn == 2)
    {
      mp_limb_t  dsecond = dp[1];
      if (dsecond <= dmask)
	{
	  dlow = (dlow >> twos) | (dsecond << (GMP_NUMB_BITS-twos));
	  ASSERT_LIMB (dlow);
	  return MPN_MOD_OR_MODEXACT_1_ODD (ap, an, dlow) == 0;
	}
    }

  /* Should we compute Q = A * D^(-1) mod B^k,
                       R = A - Q * D  mod B^k
     here, for some small values of k?  Then check if R = 0 (mod B^k).  */

  /* We could also compute A' = A mod T and D' = D mod P, for some
     P = 3 * 5 * 7 * 11 ..., and then check if any prime factor from P
     dividing D' also divides A'.  */

  TMP_MARK;

  TMP_ALLOC_LIMBS_2 (rp, an + 1,
		     qp, an - dn + 1); /* FIXME: Could we avoid this? */

  if (twos != 0)
    {
      tp = TMP_ALLOC_LIMBS (dn);
      ASSERT_NOCARRY (mpn_rshift (tp, dp, dn, twos));
      dp = tp;

      ASSERT_NOCARRY (mpn_rshift (rp, ap, an, twos));
    }
  else
    {
      MPN_COPY (rp, ap, an);
    }
  if (rp[an - 1] >= dp[dn - 1])
    {
      rp[an] = 0;
      an++;
    }
  else if (an == dn)
    {
      TMP_FREE;
      return 0;
    }

  ASSERT (an > dn);		/* requirement of functions below */

  if (BELOW_THRESHOLD (dn, DC_BDIV_QR_THRESHOLD) ||
      BELOW_THRESHOLD (an - dn, DC_BDIV_QR_THRESHOLD))
    {
      binvert_limb (di, dp[0]);
      mpn_sbpi1_bdiv_qr (qp, rp, an, dp, dn, -di);
      rp += an - dn;
    }
  else if (BELOW_THRESHOLD (dn, MU_BDIV_QR_THRESHOLD))
    {
      binvert_limb (di, dp[0]);
      mpn_dcpi1_bdiv_qr (qp, rp, an, dp, dn, -di);
      rp += an - dn;
    }
  else
    {
      tp = TMP_ALLOC_LIMBS (mpn_mu_bdiv_qr_itch (an, dn));
      mpn_mu_bdiv_qr (qp, rp, rp, an, dp, dn, tp);
    }

  /* In general, bdiv may return either R = 0 or R = D when D divides
     A. But R = 0 can happen only when A = 0, which we already have
     excluded. Furthermore, R == D (mod B^{dn}) implies no carry, so
     we don't need to check the carry returned from bdiv. */

  MPN_CMP (c, rp, dp, dn);

  TMP_FREE;
  return c == 0;
}