xref: /netbsd-src/external/lgpl3/gmp/dist/mpn/generic/bdiv_qr.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* mpn_bdiv_qr -- Hensel division with precomputed inverse, returning quotient
2    and remainder.
3 
4    Contributed to the GNU project by Torbjorn Granlund.
5 
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.
9 
10 Copyright 2006, 2007, 2009, 2012 Free Software Foundation, Inc.
11 
12 This file is part of the GNU MP Library.
13 
14 The GNU MP Library is free software; you can redistribute it and/or modify
15 it under the terms of either:
16 
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.
20 
21 or
22 
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.
26 
27 or both in parallel, as here.
28 
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.
33 
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/.  */
37 
38 #include "gmp-impl.h"
39 
40 
41 /* Computes Q = N / D mod B^n,
42 	    R = N - QD.  */
43 
44 mp_limb_t
mpn_bdiv_qr(mp_ptr qp,mp_ptr rp,mp_srcptr np,mp_size_t nn,mp_srcptr dp,mp_size_t dn,mp_ptr tp)45 mpn_bdiv_qr (mp_ptr qp, mp_ptr rp,
46 	     mp_srcptr np, mp_size_t nn,
47 	     mp_srcptr dp, mp_size_t dn,
48 	     mp_ptr tp)
49 {
50   mp_limb_t di;
51   mp_limb_t rh;
52 
53   ASSERT (nn > dn);
54   if (BELOW_THRESHOLD (dn, DC_BDIV_QR_THRESHOLD) ||
55       BELOW_THRESHOLD (nn - dn, DC_BDIV_QR_THRESHOLD))
56     {
57       MPN_COPY (tp, np, nn);
58       binvert_limb (di, dp[0]);  di = -di;
59       rh = mpn_sbpi1_bdiv_qr (qp, tp, nn, dp, dn, di);
60       MPN_COPY (rp, tp + nn - dn, dn);
61     }
62   else if (BELOW_THRESHOLD (dn, MU_BDIV_QR_THRESHOLD))
63     {
64       MPN_COPY (tp, np, nn);
65       binvert_limb (di, dp[0]);  di = -di;
66       rh = mpn_dcpi1_bdiv_qr (qp, tp, nn, dp, dn, di);
67       MPN_COPY (rp, tp + nn - dn, dn);
68     }
69   else
70     {
71       rh = mpn_mu_bdiv_qr (qp, rp, np, nn, dp, dn, tp);
72     }
73 
74   return rh;
75 }
76 
77 mp_size_t
mpn_bdiv_qr_itch(mp_size_t nn,mp_size_t dn)78 mpn_bdiv_qr_itch (mp_size_t nn, mp_size_t dn)
79 {
80   if (BELOW_THRESHOLD (dn, MU_BDIV_QR_THRESHOLD))
81     return nn;
82   else
83     return  mpn_mu_bdiv_qr_itch (nn, dn);
84 }
85