Coverage Report

Created: 2025-03-09 06:52

/src/gmp-6.2.1/mpz/gcd.c
Line
Count
Source
1
/* mpz/gcd.c:   Calculate the greatest common divisor of two integers.
2
3
Copyright 1991, 1993, 1994, 1996, 2000-2002, 2005, 2010, 2015, 2016
4
Free Software Foundation, Inc.
5
6
This file is part of the GNU MP Library.
7
8
The GNU MP Library is free software; you can redistribute it and/or modify
9
it under the terms of either:
10
11
  * the GNU Lesser General Public License as published by the Free
12
    Software Foundation; either version 3 of the License, or (at your
13
    option) any later version.
14
15
or
16
17
  * the GNU General Public License as published by the Free Software
18
    Foundation; either version 2 of the License, or (at your option) any
19
    later version.
20
21
or both in parallel, as here.
22
23
The GNU MP Library is distributed in the hope that it will be useful, but
24
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26
for more details.
27
28
You should have received copies of the GNU General Public License and the
29
GNU Lesser General Public License along with the GNU MP Library.  If not,
30
see https://www.gnu.org/licenses/.  */
31
32
#include "gmp-impl.h"
33
#include "longlong.h"
34
35
36
void
37
mpz_gcd (mpz_ptr g, mpz_srcptr u, mpz_srcptr v)
38
494
{
39
494
  unsigned long int g_zero_bits, u_zero_bits, v_zero_bits;
40
494
  mp_size_t g_zero_limbs, u_zero_limbs, v_zero_limbs;
41
494
  mp_ptr tp;
42
494
  mp_ptr up;
43
494
  mp_size_t usize;
44
494
  mp_ptr vp;
45
494
  mp_size_t vsize;
46
494
  mp_size_t gsize;
47
494
  TMP_DECL;
48
49
494
  up = PTR(u);
50
494
  usize = ABSIZ (u);
51
494
  vp = PTR(v);
52
494
  vsize = ABSIZ (v);
53
  /* GCD(0, V) == V.  */
54
494
  if (usize == 0)
55
27
    {
56
27
      SIZ (g) = vsize;
57
27
      if (g == v)
58
1
  return;
59
26
      tp = MPZ_NEWALLOC (g, vsize);
60
26
      MPN_COPY (tp, vp, vsize);
61
26
      return;
62
26
    }
63
64
  /* GCD(U, 0) == U.  */
65
467
  if (vsize == 0)
66
38
    {
67
38
      SIZ (g) = usize;
68
38
      if (g == u)
69
18
  return;
70
20
      tp = MPZ_NEWALLOC (g, usize);
71
20
      MPN_COPY (tp, up, usize);
72
20
      return;
73
20
    }
74
75
429
  if (usize == 1)
76
46
    {
77
46
      SIZ (g) = 1;
78
46
      MPZ_NEWALLOC (g, 1)[0] = mpn_gcd_1 (vp, vsize, up[0]);
79
46
      return;
80
46
    }
81
82
383
  if (vsize == 1)
83
11
    {
84
11
      SIZ(g) = 1;
85
11
      MPZ_NEWALLOC (g, 1)[0] = mpn_gcd_1 (up, usize, vp[0]);
86
11
      return;
87
11
    }
88
89
372
  TMP_MARK;
90
91
  /*  Eliminate low zero bits from U and V and move to temporary storage.  */
92
372
  tp = up;
93
533
  while (*tp == 0)
94
161
    tp++;
95
372
  u_zero_limbs = tp - up;
96
372
  usize -= u_zero_limbs;
97
372
  count_trailing_zeros (u_zero_bits, *tp);
98
372
  up = TMP_ALLOC_LIMBS (usize);
99
372
  if (u_zero_bits != 0)
100
209
    {
101
209
      mpn_rshift (up, tp, usize, u_zero_bits);
102
209
      usize -= up[usize - 1] == 0;
103
209
    }
104
163
  else
105
163
    MPN_COPY (up, tp, usize);
106
107
372
  tp = vp;
108
459
  while (*tp == 0)
109
87
    tp++;
110
372
  v_zero_limbs = tp - vp;
111
372
  vsize -= v_zero_limbs;
112
372
  count_trailing_zeros (v_zero_bits, *tp);
113
372
  vp = TMP_ALLOC_LIMBS (vsize);
114
372
  if (v_zero_bits != 0)
115
282
    {
116
282
      mpn_rshift (vp, tp, vsize, v_zero_bits);
117
282
      vsize -= vp[vsize - 1] == 0;
118
282
    }
119
90
  else
120
90
    MPN_COPY (vp, tp, vsize);
121
122
372
  if (u_zero_limbs > v_zero_limbs)
123
36
    {
124
36
      g_zero_limbs = v_zero_limbs;
125
36
      g_zero_bits = v_zero_bits;
126
36
    }
127
336
  else
128
336
    {
129
336
      g_zero_limbs = u_zero_limbs;
130
336
      if (u_zero_limbs < v_zero_limbs)
131
28
  g_zero_bits = u_zero_bits;
132
308
      else  /*  Equal.  */
133
308
  g_zero_bits = MIN (u_zero_bits, v_zero_bits);
134
336
    }
135
136
  /*  Call mpn_gcd.  The 2nd argument must not have more bits than the 1st.  */
137
372
  vsize = (usize < vsize || (usize == vsize && up[usize-1] < vp[vsize-1]))
138
372
    ? mpn_gcd (vp, vp, vsize, up, usize)
139
372
    : mpn_gcd (vp, up, usize, vp, vsize);
140
141
  /*  Here G <-- V << (g_zero_limbs*GMP_LIMB_BITS + g_zero_bits).  */
142
372
  gsize = vsize + g_zero_limbs;
143
372
  if (g_zero_bits != 0)
144
171
    {
145
171
      mp_limb_t cy_limb;
146
171
      gsize += (vp[vsize - 1] >> (GMP_NUMB_BITS - g_zero_bits)) != 0;
147
171
      tp = MPZ_NEWALLOC (g, gsize);
148
171
      MPN_ZERO (tp, g_zero_limbs);
149
150
171
      tp = tp + g_zero_limbs;
151
171
      cy_limb = mpn_lshift (tp, vp, vsize, g_zero_bits);
152
171
      if (cy_limb != 0)
153
19
  tp[vsize] = cy_limb;
154
171
    }
155
201
  else
156
201
    {
157
201
      tp = MPZ_NEWALLOC (g, gsize);
158
201
      MPN_ZERO (tp, g_zero_limbs);
159
201
      MPN_COPY (tp + g_zero_limbs, vp, vsize);
160
201
    }
161
162
372
  SIZ (g) = gsize;
163
372
  TMP_FREE;
164
372
}