Coverage Report

Created: 2025-03-09 06:52

/src/gmp-6.2.1/mpn/dcpi1_bdiv_q.c
Line
Count
Source (jump to first uncovered line)
1
/* mpn_dcpi1_bdiv_q -- divide-and-conquer Hensel division with precomputed
2
   inverse, returning quotient.
3
4
   Contributed to the GNU project by Niels Möller and 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-2011, 2017 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
#if 0       /* unused, so leave out for now */
41
static mp_size_t
42
mpn_dcpi1_bdiv_q_n_itch (mp_size_t n)
43
{
44
  /* NOTE: Depends on mullo_n and mpn_dcpi1_bdiv_qr_n interface */
45
  return n;
46
}
47
#endif
48
49
/* Computes Q = - N / D mod B^n, destroys N.
50
51
   N = {np,n}
52
   D = {dp,n}
53
*/
54
55
static void
56
mpn_dcpi1_bdiv_q_n (mp_ptr qp,
57
        mp_ptr np, mp_srcptr dp, mp_size_t n,
58
        mp_limb_t dinv, mp_ptr tp)
59
1.26k
{
60
4.65k
  while (ABOVE_THRESHOLD (n, DC_BDIV_Q_THRESHOLD))
61
3.39k
    {
62
3.39k
      mp_size_t lo, hi;
63
3.39k
      mp_limb_t cy;
64
65
3.39k
      lo = n >> 1;      /* floor(n/2) */
66
3.39k
      hi = n - lo;      /* ceil(n/2) */
67
68
3.39k
      cy = mpn_dcpi1_bdiv_qr_n (qp, np, dp, lo, dinv, tp);
69
70
3.39k
      mpn_mullo_n (tp, qp, dp + hi, lo);
71
3.39k
      mpn_add_n (np + hi, np + hi, tp, lo);
72
73
3.39k
      if (lo < hi)
74
1.68k
  {
75
1.68k
    cy += mpn_addmul_1 (np + lo, qp, lo, dp[lo]);
76
1.68k
    np[n - 1] += cy;
77
1.68k
  }
78
3.39k
      qp += lo;
79
3.39k
      np += lo;
80
3.39k
      n -= lo;
81
3.39k
    }
82
1.26k
  mpn_sbpi1_bdiv_q (qp, np, n, dp, n, dinv);
83
1.26k
}
84
85
/* Computes Q = - N / D mod B^nn, destroys N.
86
87
   N = {np,nn}
88
   D = {dp,dn}
89
*/
90
91
void
92
mpn_dcpi1_bdiv_q (mp_ptr qp,
93
      mp_ptr np, mp_size_t nn,
94
      mp_srcptr dp, mp_size_t dn,
95
      mp_limb_t dinv)
96
1.26k
{
97
1.26k
  mp_size_t qn;
98
1.26k
  mp_limb_t cy;
99
1.26k
  mp_ptr tp;
100
1.26k
  TMP_DECL;
101
102
1.26k
  TMP_MARK;
103
104
1.26k
  ASSERT (dn >= 2);
105
1.26k
  ASSERT (nn - dn >= 0);
106
1.26k
  ASSERT (dp[0] & 1);
107
108
1.26k
  tp = TMP_SALLOC_LIMBS (dn);
109
110
1.26k
  qn = nn;
111
112
1.26k
  if (qn > dn)
113
29
    {
114
      /* Reduce qn mod dn in a super-efficient manner.  */
115
29
      do
116
29
  qn -= dn;
117
29
      while (qn > dn);
118
119
      /* Perform the typically smaller block first.  */
120
29
      if (BELOW_THRESHOLD (qn, DC_BDIV_QR_THRESHOLD))
121
17
  cy = mpn_sbpi1_bdiv_qr (qp, np, 2 * qn, dp, qn, dinv);
122
12
      else
123
12
  cy = mpn_dcpi1_bdiv_qr_n (qp, np, dp, qn, dinv, tp);
124
125
29
      if (qn != dn)
126
29
  {
127
29
    if (qn > dn - qn)
128
5
      mpn_mul (tp, qp, qn, dp + qn, dn - qn);
129
24
    else
130
24
      mpn_mul (tp, dp + qn, dn - qn, qp, qn);
131
29
    mpn_incr_u (tp + qn, cy);
132
133
29
    mpn_add (np + qn, np + qn, nn - qn, tp, dn);
134
29
    cy = 0;
135
29
  }
136
137
29
      np += qn;
138
29
      qp += qn;
139
140
29
      qn = nn - qn;
141
29
      while (qn > dn)
142
0
  {
143
0
    mpn_add_1 (np + dn, np + dn, qn - dn, cy);
144
0
    cy = mpn_dcpi1_bdiv_qr_n (qp, np, dp, dn, dinv, tp);
145
0
    qp += dn;
146
0
    np += dn;
147
0
    qn -= dn;
148
0
  }
149
29
      mpn_dcpi1_bdiv_q_n (qp, np, dp, dn, dinv, tp);
150
29
    }
151
1.23k
  else
152
1.23k
    {
153
1.23k
      if (BELOW_THRESHOLD (qn, DC_BDIV_Q_THRESHOLD))
154
0
  mpn_sbpi1_bdiv_q (qp, np, qn, dp, qn, dinv);
155
1.23k
      else
156
1.23k
  mpn_dcpi1_bdiv_q_n (qp, np, dp, qn, dinv, tp);
157
1.23k
    }
158
159
1.26k
  TMP_FREE;
160
1.26k
}