xref: /dflybsd-src/contrib/gmp/mpn/generic/sbpi1_bdiv_q.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* mpn_sbpi1_bdiv_q -- schoolbook Hensel division with precomputed inverse,
286d7f5d3SJohn Marino    returning quotient only.
386d7f5d3SJohn Marino 
486d7f5d3SJohn Marino    Contributed to the GNU project by Niels M�ller.
586d7f5d3SJohn Marino 
686d7f5d3SJohn Marino    THE FUNCTIONS IN THIS FILE ARE INTERNAL FUNCTIONS WITH MUTABLE INTERFACES.
786d7f5d3SJohn Marino    IT IS ONLY SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS
886d7f5d3SJohn Marino    ALMOST GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
986d7f5d3SJohn Marino 
1086d7f5d3SJohn Marino Copyright 2005, 2006, 2009 Free Software Foundation, Inc.
1186d7f5d3SJohn Marino 
1286d7f5d3SJohn Marino This file is part of the GNU MP Library.
1386d7f5d3SJohn Marino 
1486d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
1586d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
1686d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1786d7f5d3SJohn Marino option) any later version.
1886d7f5d3SJohn Marino 
1986d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
2086d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
2186d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
2286d7f5d3SJohn Marino License for more details.
2386d7f5d3SJohn Marino 
2486d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
2586d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
2686d7f5d3SJohn Marino 
2786d7f5d3SJohn Marino #include "gmp.h"
2886d7f5d3SJohn Marino #include "gmp-impl.h"
2986d7f5d3SJohn Marino 
3086d7f5d3SJohn Marino 
3186d7f5d3SJohn Marino /* Computes Q = N / D mod B^nn, destroys N.
3286d7f5d3SJohn Marino 
3386d7f5d3SJohn Marino    D must be odd. dinv is (-D)^-1 mod B.
3486d7f5d3SJohn Marino 
3586d7f5d3SJohn Marino 
3686d7f5d3SJohn Marino    The straightforward way to compute Q is to cancel one limb at a time, using
3786d7f5d3SJohn Marino 
3886d7f5d3SJohn Marino      qp[i] = D^{-1} * np[i] (mod B)
3986d7f5d3SJohn Marino      N -= B^i * qp[i] * D
4086d7f5d3SJohn Marino 
4186d7f5d3SJohn Marino    But we prefer addition to subtraction, since mpn_addmul_1 is often faster
4286d7f5d3SJohn Marino    than mpn_submul_1.  Q = - N / D can be computed by iterating
4386d7f5d3SJohn Marino 
4486d7f5d3SJohn Marino      qp[i] = (-D)^{-1} * np[i] (mod B)
4586d7f5d3SJohn Marino      N += B^i * qp[i] * D
4686d7f5d3SJohn Marino 
4786d7f5d3SJohn Marino    And then we flip the sign, -Q = (not Q) + 1. */
4886d7f5d3SJohn Marino 
4986d7f5d3SJohn Marino void
mpn_sbpi1_bdiv_q(mp_ptr qp,mp_ptr np,mp_size_t nn,mp_srcptr dp,mp_size_t dn,mp_limb_t dinv)5086d7f5d3SJohn Marino mpn_sbpi1_bdiv_q (mp_ptr qp,
5186d7f5d3SJohn Marino 		  mp_ptr np, mp_size_t nn,
5286d7f5d3SJohn Marino 		  mp_srcptr dp, mp_size_t dn,
5386d7f5d3SJohn Marino 		  mp_limb_t dinv)
5486d7f5d3SJohn Marino {
5586d7f5d3SJohn Marino   mp_size_t i;
5686d7f5d3SJohn Marino   mp_limb_t cy, q;
5786d7f5d3SJohn Marino 
5886d7f5d3SJohn Marino   ASSERT (dn > 0);
5986d7f5d3SJohn Marino   ASSERT (nn >= dn);
6086d7f5d3SJohn Marino   ASSERT ((dp[0] & 1) != 0);
6186d7f5d3SJohn Marino 
6286d7f5d3SJohn Marino   for (i = nn - dn; i > 0; i--)
6386d7f5d3SJohn Marino     {
6486d7f5d3SJohn Marino       q = dinv * np[0];
6586d7f5d3SJohn Marino       qp[0] = ~q;
6686d7f5d3SJohn Marino       qp++;
6786d7f5d3SJohn Marino       cy = mpn_addmul_1 (np, dp, dn, q);
6886d7f5d3SJohn Marino       mpn_add_1 (np + dn, np + dn, i, cy);
6986d7f5d3SJohn Marino       ASSERT (np[0] == 0);
7086d7f5d3SJohn Marino       np++;
7186d7f5d3SJohn Marino     }
7286d7f5d3SJohn Marino 
7386d7f5d3SJohn Marino   for (i = dn; i > 1; i--)
7486d7f5d3SJohn Marino     {
7586d7f5d3SJohn Marino       q = dinv * np[0];
7686d7f5d3SJohn Marino       qp[0] = ~q;
7786d7f5d3SJohn Marino       qp++;
7886d7f5d3SJohn Marino       mpn_addmul_1 (np, dp, i, q);
7986d7f5d3SJohn Marino       ASSERT (np[0] == 0);
8086d7f5d3SJohn Marino       np++;
8186d7f5d3SJohn Marino     }
8286d7f5d3SJohn Marino 
8386d7f5d3SJohn Marino   /* Final limb */
8486d7f5d3SJohn Marino   q = dinv * np[0];
8586d7f5d3SJohn Marino   qp[0] = ~q;
8686d7f5d3SJohn Marino   mpn_add_1 (qp - nn + 1, qp - nn + 1, nn, 1);
8786d7f5d3SJohn Marino }
88