Coverage Report

Created: 2025-03-09 06:52

/src/gmp-6.2.1/mpn/sbpi1_divappr_q.c
Line
Count
Source (jump to first uncovered line)
1
/* mpn_sbpi1_divappr_q -- Schoolbook division using the Möller-Granlund 3/2
2
   division algorithm, returning approximate quotient.  The quotient returned
3
   is either correct, or one too large.
4
5
   Contributed to the GNU project by Torbjorn Granlund.
6
7
   THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE.  IT IS ONLY
8
   SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
9
   GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
10
11
Copyright 2007, 2009 Free Software Foundation, Inc.
12
13
This file is part of the GNU MP Library.
14
15
The GNU MP Library is free software; you can redistribute it and/or modify
16
it under the terms of either:
17
18
  * the GNU Lesser General Public License as published by the Free
19
    Software Foundation; either version 3 of the License, or (at your
20
    option) any later version.
21
22
or
23
24
  * the GNU General Public License as published by the Free Software
25
    Foundation; either version 2 of the License, or (at your option) any
26
    later version.
27
28
or both in parallel, as here.
29
30
The GNU MP Library is distributed in the hope that it will be useful, but
31
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
32
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
33
for more details.
34
35
You should have received copies of the GNU General Public License and the
36
GNU Lesser General Public License along with the GNU MP Library.  If not,
37
see https://www.gnu.org/licenses/.  */
38
39
40
#include "gmp-impl.h"
41
#include "longlong.h"
42
43
mp_limb_t
44
mpn_sbpi1_divappr_q (mp_ptr qp,
45
         mp_ptr np, mp_size_t nn,
46
         mp_srcptr dp, mp_size_t dn,
47
         mp_limb_t dinv)
48
1.29k
{
49
1.29k
  mp_limb_t qh;
50
1.29k
  mp_size_t qn, i;
51
1.29k
  mp_limb_t n1, n0;
52
1.29k
  mp_limb_t d1, d0;
53
1.29k
  mp_limb_t cy, cy1;
54
1.29k
  mp_limb_t q;
55
1.29k
  mp_limb_t flag;
56
57
1.29k
  ASSERT (dn > 2);
58
1.29k
  ASSERT (nn >= dn);
59
1.29k
  ASSERT ((dp[dn-1] & GMP_NUMB_HIGHBIT) != 0);
60
61
1.29k
  np += nn;
62
63
1.29k
  qn = nn - dn;
64
1.29k
  if (qn + 1 < dn)
65
0
    {
66
0
      dp += dn - (qn + 1);
67
0
      dn = qn + 1;
68
0
    }
69
70
1.29k
  qh = mpn_cmp (np - dn, dp, dn) >= 0;
71
1.29k
  if (qh != 0)
72
154
    mpn_sub_n (np - dn, np - dn, dp, dn);
73
74
1.29k
  qp += qn;
75
76
1.29k
  dn -= 2;      /* offset dn by 2 for main division loops,
77
           saving two iterations in mpn_submul_1.  */
78
1.29k
  d1 = dp[dn + 1];
79
1.29k
  d0 = dp[dn + 0];
80
81
1.29k
  np -= 2;
82
83
1.29k
  n1 = np[1];
84
85
2.03k
  for (i = qn - (dn + 2); i >= 0; i--)
86
732
    {
87
732
      np--;
88
732
      if (UNLIKELY (n1 == d1) && np[1] == d0)
89
1
  {
90
1
    q = GMP_NUMB_MASK;
91
1
    mpn_submul_1 (np - dn, dp, dn + 2, q);
92
1
    n1 = np[1];   /* update n1, last loop's value will now be invalid */
93
1
  }
94
731
      else
95
731
  {
96
731
    udiv_qr_3by2 (q, n1, n0, n1, np[1], np[0], d1, d0, dinv);
97
98
731
    cy = mpn_submul_1 (np - dn, dp, dn, q);
99
100
731
    cy1 = n0 < cy;
101
731
    n0 = (n0 - cy) & GMP_NUMB_MASK;
102
731
    cy = n1 < cy1;
103
731
    n1 -= cy1;
104
731
    np[0] = n0;
105
106
731
    if (UNLIKELY (cy != 0))
107
6
      {
108
6
        n1 += d1 + mpn_add_n (np - dn, np - dn, dp, dn + 1);
109
6
        q--;
110
6
      }
111
731
  }
112
113
732
      *--qp = q;
114
732
    }
115
116
1.29k
  flag = ~CNST_LIMB(0);
117
118
1.29k
  if (dn >= 0)
119
1.29k
    {
120
11.2k
      for (i = dn; i > 0; i--)
121
9.92k
  {
122
9.92k
    np--;
123
9.92k
    if (UNLIKELY (n1 >= (d1 & flag)))
124
114
      {
125
114
        q = GMP_NUMB_MASK;
126
114
        cy = mpn_submul_1 (np - dn, dp, dn + 2, q);
127
128
114
        if (UNLIKELY (n1 != cy))
129
1
    {
130
1
      if (n1 < (cy & flag))
131
1
        {
132
1
          q--;
133
1
          mpn_add_n (np - dn, np - dn, dp, dn + 2);
134
1
        }
135
0
      else
136
0
        flag = 0;
137
1
    }
138
114
        n1 = np[1];
139
114
      }
140
9.81k
    else
141
9.81k
      {
142
9.81k
        udiv_qr_3by2 (q, n1, n0, n1, np[1], np[0], d1, d0, dinv);
143
144
9.81k
        cy = mpn_submul_1 (np - dn, dp, dn, q);
145
146
9.81k
        cy1 = n0 < cy;
147
9.81k
        n0 = (n0 - cy) & GMP_NUMB_MASK;
148
9.81k
        cy = n1 < cy1;
149
9.81k
        n1 -= cy1;
150
9.81k
        np[0] = n0;
151
152
9.81k
        if (UNLIKELY (cy != 0))
153
50
    {
154
50
      n1 += d1 + mpn_add_n (np - dn, np - dn, dp, dn + 1);
155
50
      q--;
156
50
    }
157
9.81k
      }
158
159
9.92k
    *--qp = q;
160
161
    /* Truncate operands.  */
162
9.92k
    dn--;
163
9.92k
    dp++;
164
9.92k
  }
165
166
1.29k
      np--;
167
1.29k
      if (UNLIKELY (n1 >= (d1 & flag)))
168
24
  {
169
24
    q = GMP_NUMB_MASK;
170
24
    cy = mpn_submul_1 (np, dp, 2, q);
171
172
24
    if (UNLIKELY (n1 != cy))
173
9
      {
174
9
        if (n1 < (cy & flag))
175
9
    {
176
9
      q--;
177
9
      add_ssaaaa (np[1], np[0], np[1], np[0], dp[1], dp[0]);
178
9
    }
179
0
        else
180
0
    flag = 0;
181
9
      }
182
24
    n1 = np[1];
183
24
  }
184
1.27k
      else
185
1.27k
  {
186
1.27k
    udiv_qr_3by2 (q, n1, n0, n1, np[1], np[0], d1, d0, dinv);
187
188
1.27k
    np[1] = n1;
189
1.27k
    np[0] = n0;
190
1.27k
  }
191
192
1.29k
      *--qp = q;
193
1.29k
    }
194
195
1.29k
  ASSERT_ALWAYS (np[1] == n1);
196
197
1.29k
  return qh;
198
1.29k
}