Coverage Report

Created: 2025-07-23 06:43

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