beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / bdiv_qr.c
blob6a5eedbbc2f5032964167d77bd0d27d93df5b5c2
1 /* mpn_bdiv_qr -- Hensel division with precomputed inverse, returning quotient
2 and remainder.
4 Contributed to the GNU project by Torbjorn Granlund.
6 THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES. IT IS ONLY
7 SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
8 GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
10 Copyright 2006, 2007, 2009, 2012 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
25 later version.
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
32 for more details.
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/. */
38 #include "gmp.h"
39 #include "gmp-impl.h"
42 /* Computes Q = N / D mod B^n,
43 R = N - QD. */
45 mp_limb_t
46 mpn_bdiv_qr (mp_ptr qp, mp_ptr rp,
47 mp_srcptr np, mp_size_t nn,
48 mp_srcptr dp, mp_size_t dn,
49 mp_ptr tp)
51 mp_limb_t di;
52 mp_limb_t rh;
54 ASSERT (nn > dn);
55 if (BELOW_THRESHOLD (dn, DC_BDIV_QR_THRESHOLD) ||
56 BELOW_THRESHOLD (nn - dn, DC_BDIV_QR_THRESHOLD))
58 MPN_COPY (tp, np, nn);
59 binvert_limb (di, dp[0]); di = -di;
60 rh = mpn_sbpi1_bdiv_qr (qp, tp, nn, dp, dn, di);
61 MPN_COPY (rp, tp + nn - dn, dn);
63 else if (BELOW_THRESHOLD (dn, MU_BDIV_QR_THRESHOLD))
65 MPN_COPY (tp, np, nn);
66 binvert_limb (di, dp[0]); di = -di;
67 rh = mpn_dcpi1_bdiv_qr (qp, tp, nn, dp, dn, di);
68 MPN_COPY (rp, tp + nn - dn, dn);
70 else
72 rh = mpn_mu_bdiv_qr (qp, rp, np, nn, dp, dn, tp);
75 return rh;
78 mp_size_t
79 mpn_bdiv_qr_itch (mp_size_t nn, mp_size_t dn)
81 if (BELOW_THRESHOLD (dn, MU_BDIV_QR_THRESHOLD))
82 return nn;
83 else
84 return mpn_mu_bdiv_qr_itch (nn, dn);