Coverage Report

Created: 2025-03-18 06:55

/src/gmp/mpn/hgcd_step.c
Line
Count
Source (jump to first uncovered line)
1
/* hgcd_step.c.
2
3
   THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES.  IT IS ONLY
4
   SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
5
   GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
6
7
Copyright 2003-2005, 2008, 2011, 2012 Free Software Foundation, Inc.
8
9
This file is part of the GNU MP Library.
10
11
The GNU MP Library is free software; you can redistribute it and/or modify
12
it under the terms of either:
13
14
  * the GNU Lesser General Public License as published by the Free
15
    Software Foundation; either version 3 of the License, or (at your
16
    option) any later version.
17
18
or
19
20
  * the GNU General Public License as published by the Free Software
21
    Foundation; either version 2 of the License, or (at your option) any
22
    later version.
23
24
or both in parallel, as here.
25
26
The GNU MP Library is distributed in the hope that it will be useful, but
27
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
29
for more details.
30
31
You should have received copies of the GNU General Public License and the
32
GNU Lesser General Public License along with the GNU MP Library.  If not,
33
see https://www.gnu.org/licenses/.  */
34
35
#include "gmp-impl.h"
36
#include "longlong.h"
37
38
39
static void
40
hgcd_hook (void *p, mp_srcptr gp, mp_size_t gn,
41
     mp_srcptr qp, mp_size_t qn, int d)
42
0
{
43
0
  ASSERT (!gp);
44
0
  ASSERT (d >= 0);
45
0
  ASSERT (d <= 1);
46
47
0
  MPN_NORMALIZE (qp, qn);
48
0
  if (qn > 0)
49
0
    {
50
0
      struct hgcd_matrix *M = (struct hgcd_matrix *) p;
51
      /* NOTES: This is a bit ugly. A tp area is passed to
52
   gcd_subdiv_step, which stores q at the start of that area. We
53
   now use the rest. */
54
0
      mp_ptr tp = (mp_ptr) qp + qn;
55
0
      mpn_hgcd_matrix_update_q (M, qp, qn, d, tp);
56
0
    }
57
0
}
58
59
/* Perform a few steps, using some of mpn_hgcd2, subtraction and
60
   division. Reduces the size by almost one limb or more, but never
61
   below the given size s. Return new size for a and b, or 0 if no
62
   more steps are possible.
63
64
   If hgcd2 succeeds, needs temporary space for hgcd_matrix_mul_1, M->n
65
   limbs, and hgcd_mul_matrix1_inverse_vector, n limbs. If hgcd2
66
   fails, needs space for the quotient, qn <= n - s limbs, for and
67
   hgcd_matrix_update_q, qn + (size of the appropriate column of M) <=
68
   (resulting size of M) + 1.
69
70
   If N is the input size to the calling hgcd, then s = floor(N/2) +
71
   1, M->n < N, qn + product size <= n - s + n - s + 1 = 2 (n - s) + 1
72
   <= N.
73
*/
74
75
mp_size_t
76
mpn_hgcd_step (mp_size_t n, mp_ptr ap, mp_ptr bp, mp_size_t s,
77
         struct hgcd_matrix *M, mp_ptr tp)
78
0
{
79
0
  struct hgcd_matrix1 M1;
80
0
  mp_limb_t mask;
81
0
  mp_limb_t ah, al, bh, bl;
82
83
0
  ASSERT (n > s);
84
85
0
  mask = ap[n-1] | bp[n-1];
86
0
  ASSERT (mask > 0);
87
88
0
  if (n == s + 1)
89
0
    {
90
0
      if (mask < 4)
91
0
  goto subtract;
92
93
0
      ah = ap[n-1]; al = ap[n-2];
94
0
      bh = bp[n-1]; bl = bp[n-2];
95
0
    }
96
0
  else if (mask & GMP_NUMB_HIGHBIT)
97
0
    {
98
0
      ah = ap[n-1]; al = ap[n-2];
99
0
      bh = bp[n-1]; bl = bp[n-2];
100
0
    }
101
0
  else
102
0
    {
103
0
      int shift;
104
105
0
      count_leading_zeros (shift, mask);
106
0
      ah = MPN_EXTRACT_NUMB (shift, ap[n-1], ap[n-2]);
107
0
      al = MPN_EXTRACT_NUMB (shift, ap[n-2], ap[n-3]);
108
0
      bh = MPN_EXTRACT_NUMB (shift, bp[n-1], bp[n-2]);
109
0
      bl = MPN_EXTRACT_NUMB (shift, bp[n-2], bp[n-3]);
110
0
    }
111
112
  /* Try an mpn_hgcd2 step */
113
0
  if (mpn_hgcd2 (ah, al, bh, bl, &M1))
114
0
    {
115
      /* Multiply M <- M * M1 */
116
0
      mpn_hgcd_matrix_mul_1 (M, &M1, tp);
117
118
      /* Can't swap inputs, so we need to copy. */
119
0
      MPN_COPY (tp, ap, n);
120
      /* Multiply M1^{-1} (a;b) */
121
0
      return mpn_matrix22_mul1_inverse_vector (&M1, ap, tp, bp, n);
122
0
    }
123
124
0
 subtract:
125
126
0
  return mpn_gcd_subdiv_step (ap, bp, n, s, hgcd_hook, M, tp);
127
0
}