xref: /dragonfly/contrib/gmp/mpn/generic/divis.c (revision dca3c15d)
1 /* mpn_divisible_p -- mpn by mpn divisibility test
2 
3    THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
4    CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
5    FUTURE GNU MP RELEASES.
6 
7 Copyright 2001, 2002, 2005 Free Software Foundation, Inc.
8 
9 This file is part of the GNU MP Library.
10 
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
15 
16 The GNU MP Library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
19 License for more details.
20 
21 You should have received a copy of the GNU Lesser General Public License
22 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
23 
24 #include "gmp.h"
25 #include "gmp-impl.h"
26 #include "longlong.h"
27 
28 
29 /* Determine whether {ap,asize} is divisible by {dp,dsize}.  Must have both
30    operands normalized, meaning high limbs non-zero, except that asize==0 is
31    allowed.
32 
33    There usually won't be many low zero bits on d, but the checks for this
34    are fast and might pick up a few operand combinations, in particular they
35    might reduce d to fit the single-limb mod_1/modexact_1 code.
36 
37    Future:
38 
39    This is currently not much faster than the user doing an mpz_tdiv_r
40    and testing for a zero remainder, but hopefully it can be improved.
41 
42    mpn_bdivmod is one possibility, but it only trades udiv_qrnnd's for
43    multiplies, it won't save crossproducts the way it can in mpz_divexact.
44    Definitely worthwhile on small operands for most processors, but a
45    sub-quadratic version will be wanted before it can be used on all sizes.
46 
47    Getting the remainder limb by limb would make an early exit possible on
48    finding a non-zero.  This would probably have to be bdivmod style so
49    there's no addback, but it would need a multi-precision inverse and so
50    might be slower than the plain method (on small sizes at least).
51 
52    When d must be normalized (shifted to high bit set), it's possible to
53    just append a low zero limb to "a" rather than bit-shifting as
54    mpn_tdiv_qr does internally, so long as it's already been checked that a
55    has at least as many trailing zeros bits as d.  Or equivalently, pass
56    qxn==1 to mpn_tdiv_qr, if/when it accepts that.
57 
58    When called from mpz_congruent_p, {ap,asize} is a temporary which can be
59    destroyed.  Maybe it'd be possible to get into mpn_tdiv_qr at a lower
60    level to save copying it, or maybe that function could accept rp==ap.
61 
62    Could use __attribute__ ((regparm (2))) on i386, so the parameters
63    wouldn't need extra stack when called from mpz_divisible_p, but a
64    pre-release gcc 3 didn't generate particularly good register juggling in
65    that case, so this isn't done for now.  */
66 
67 int
68 mpn_divisible_p (mp_srcptr ap, mp_size_t asize,
69 		 mp_srcptr dp, mp_size_t dsize)
70 {
71   mp_limb_t  alow, dlow, dmask;
72   mp_ptr     qp, rp;
73   mp_size_t  i;
74   TMP_DECL;
75 
76   ASSERT (asize >= 0);
77   ASSERT (asize == 0 || ap[asize-1] != 0);
78   ASSERT (dsize >= 1);
79   ASSERT (dp[dsize-1] != 0);
80   ASSERT_MPN (ap, asize);
81   ASSERT_MPN (dp, dsize);
82 
83   /* When a<d only a==0 is divisible.
84      Notice this test covers all cases of asize==0. */
85   if (asize < dsize)
86     return (asize == 0);
87 
88   /* Strip low zero limbs from d, requiring a==0 on those. */
89   for (;;)
90     {
91       alow = *ap;
92       dlow = *dp;
93 
94       if (dlow != 0)
95 	break;
96 
97       if (alow != 0)
98 	return 0;  /* a has fewer low zero limbs than d, so not divisible */
99 
100       /* a!=0 and d!=0 so won't get to size==0 */
101       asize--; ASSERT (asize >= 1);
102       dsize--; ASSERT (dsize >= 1);
103       ap++;
104       dp++;
105     }
106 
107   /* a must have at least as many low zero bits as d */
108   dmask = LOW_ZEROS_MASK (dlow);
109   if ((alow & dmask) != 0)
110     return 0;
111 
112   if (dsize == 1)
113     {
114       if (BELOW_THRESHOLD (asize, MODEXACT_1_ODD_THRESHOLD))
115 	return mpn_mod_1 (ap, asize, dlow) == 0;
116 
117       if ((dlow & 1) == 0)
118 	{
119 	  unsigned  twos;
120 	  count_trailing_zeros (twos, dlow);
121 	  dlow >>= twos;
122 	}
123       return mpn_modexact_1_odd (ap, asize, dlow) == 0;
124     }
125 
126   if (dsize == 2)
127     {
128       mp_limb_t  dsecond = dp[1];
129       if (dsecond <= dmask)
130 	{
131 	  unsigned  twos;
132 	  count_trailing_zeros (twos, dlow);
133 	  dlow = (dlow >> twos) | (dsecond << (GMP_NUMB_BITS-twos));
134 	  ASSERT_LIMB (dlow);
135 	  return MPN_MOD_OR_MODEXACT_1_ODD (ap, asize, dlow) == 0;
136 	}
137     }
138 
139   TMP_MARK;
140 
141   rp = TMP_ALLOC_LIMBS (asize+1);
142   qp = rp + dsize;
143 
144   mpn_tdiv_qr (qp, rp, (mp_size_t) 0, ap, asize, dp, dsize);
145 
146   /* test for {rp,dsize} zero or non-zero */
147   i = 0;
148   do
149     {
150       if (rp[i] != 0)
151 	{
152 	  TMP_FREE;
153 	  return 0;
154 	}
155     }
156   while (++i < dsize);
157 
158   TMP_FREE;
159   return 1;
160 }
161