1 /* mpn_remove -- divide out all multiples of odd mpn number from another mpn
4 Contributed to the GNU project by Torbjorn Granlund.
6 THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE. IT IS ONLY
7 SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
8 GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
10 Copyright 2009, 2012-2014 Free Software Foundation, Inc.
12 This file is part of the GNU MP Library.
14 The GNU MP Library is free software; you can redistribute it and/or modify
15 it under the terms of either:
17 * the GNU Lesser General Public License as published by the Free
18 Software Foundation; either version 3 of the License, or (at your
19 option) any later version.
23 * the GNU General Public License as published by the Free Software
24 Foundation; either version 2 of the License, or (at your option) any
27 or both in parallel, as here.
29 The GNU MP Library is distributed in the hope that it will be useful, but
30 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
31 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
34 You should have received copies of the GNU General Public License and the
35 GNU Lesser General Public License along with the GNU MP Library. If not,
36 see https://www.gnu.org/licenses/. */
41 #if GMP_LIMB_BITS > 50
44 #define LOG GMP_LIMB_BITS
48 /* Input: U = {up,un}, V = {vp,vn} must be odd, cap
49 Ouput W = {wp,*wn} allocation need is exactly *wn
51 Set W = U / V^k, where k is the largest integer <= cap such that the
52 division yields an integer.
54 FIXME: We currently allow any operand overlap. This is quite non mpn-ish
55 and might be changed, since it cost significant temporary space.
56 * If we require W to have space for un + 1 limbs, we could save qp or qp2
57 (but we will still need to copy things into wp 50% of the time).
58 * If we allow ourselves to clobber U, we could save the other of qp and qp2,
59 and the initial COPY (but also here we would need un + 1 limbs).
62 /* FIXME: We need to wrap mpn_bdiv_qr due to the itch interface. This need
63 indicates a flaw in the current itch mechanism: Which operands not greater
64 than un,un will incur the worst itch? We need a parallel foo_maxitch set
67 mpn_bdiv_qr_wrap (mp_ptr qp
, mp_ptr rp
,
68 mp_srcptr np
, mp_size_t nn
,
69 mp_srcptr dp
, mp_size_t dn
)
75 scratch_out
= TMP_ALLOC_LIMBS (mpn_bdiv_qr_itch (nn
, dn
));
76 mpn_bdiv_qr (qp
, rp
, np
, nn
, dp
, dn
, scratch_out
);
82 mpn_remove (mp_ptr wp
, mp_size_t
*wn
,
83 mp_srcptr up
, mp_size_t un
, mp_srcptr vp
, mp_size_t vn
,
89 mp_ptr tp
, qp
, np
, qp2
;
91 mp_size_t pn
, nn
, qn
, i
;
97 ASSERT (vp
[0] % 2 != 0); /* 2-adic division wants odd numbers */
98 ASSERT (vn
> 1 || vp
[0] > 1); /* else we would loop indefinitely */
102 TMP_ALLOC_LIMBS_3 (qp
, un
+ 1, /* quotient, alternating */
103 qp2
, un
+ 1, /* quotient, alternating */
104 tp
, (un
+ 1 + vn
) / 2); /* remainder */
108 MPN_COPY (qp
, up
, un
);
115 mpn_bdiv_qr_wrap (qp2
, tp
, qp
, qn
+ 1, pp
, pn
);
116 if (!mpn_zero_p (tp
, pn
))
117 break; /* could not divide by V^npowers */
119 MP_PTR_SWAP (qp
, qp2
);
127 if (((mp_bitcnt_t
) 2 << npowers
) - 1 > cap
)
130 nn
= 2 * pn
- 1; /* next power will be at least this large */
132 break; /* next power would be overlarge */
134 if (npowers
== 1) /* Alloc once, but only if it's needed */
135 np
= TMP_ALLOC_LIMBS (qn
+ LOG
); /* powers of V */
139 mpn_sqr (np
, pp
, pn
);
140 pn
= nn
+ (np
[nn
] != 0);
144 pwr
= ((mp_bitcnt_t
) 1 << npowers
) - 1;
146 for (i
= npowers
; --i
>= 0;)
152 if (pwr
+ ((mp_bitcnt_t
) 1 << i
) > cap
)
153 continue; /* V^i would bring us past cap */
156 mpn_bdiv_qr_wrap (qp2
, tp
, qp
, qn
+ 1, pwpsp
[i
], pn
);
157 if (!mpn_zero_p (tp
, pn
))
158 continue; /* could not divide by V^i */
160 MP_PTR_SWAP (qp
, qp2
);
164 pwr
+= (mp_bitcnt_t
) 1 << i
;
167 MPN_COPY (wp
, qp
, qn
);