Coverage Report

Created: 2025-03-09 06:52

/src/gmp-6.2.1/mpz/lcm.c
Line
Count
Source
1
/* mpz_lcm -- mpz/mpz least common multiple.
2
3
Copyright 1996, 2000, 2001, 2005, 2012 Free Software Foundation, Inc.
4
5
This file is part of the GNU MP Library.
6
7
The GNU MP Library is free software; you can redistribute it and/or modify
8
it under the terms of either:
9
10
  * the GNU Lesser General Public License as published by the Free
11
    Software Foundation; either version 3 of the License, or (at your
12
    option) any later version.
13
14
or
15
16
  * the GNU General Public License as published by the Free Software
17
    Foundation; either version 2 of the License, or (at your option) any
18
    later version.
19
20
or both in parallel, as here.
21
22
The GNU MP Library is distributed in the hope that it will be useful, but
23
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25
for more details.
26
27
You should have received copies of the GNU General Public License and the
28
GNU Lesser General Public License along with the GNU MP Library.  If not,
29
see https://www.gnu.org/licenses/.  */
30
31
#include "gmp-impl.h"
32
33
void
34
mpz_lcm (mpz_ptr r, mpz_srcptr u, mpz_srcptr v)
35
272
{
36
272
  mpz_t g;
37
272
  mp_size_t usize, vsize;
38
272
  TMP_DECL;
39
40
272
  usize = SIZ (u);
41
272
  vsize = SIZ (v);
42
272
  if (usize == 0 || vsize == 0)
43
23
    {
44
23
      SIZ (r) = 0;
45
23
      return;
46
23
    }
47
249
  usize = ABS (usize);
48
249
  vsize = ABS (vsize);
49
50
249
  if (vsize == 1 || usize == 1)
51
68
    {
52
68
      mp_limb_t  vl, gl, c;
53
68
      mp_srcptr  up;
54
68
      mp_ptr     rp;
55
56
68
      if (usize == 1)
57
44
  {
58
44
    usize = vsize;
59
44
    MPZ_SRCPTR_SWAP (u, v);
60
44
  }
61
62
68
      MPZ_REALLOC (r, usize+1);
63
64
68
      up = PTR(u);
65
68
      vl = PTR(v)[0];
66
68
      gl = mpn_gcd_1 (up, usize, vl);
67
68
      vl /= gl;
68
69
68
      rp = PTR(r);
70
68
      c = mpn_mul_1 (rp, up, usize, vl);
71
68
      rp[usize] = c;
72
68
      usize += (c != 0);
73
68
      SIZ(r) = usize;
74
68
      return;
75
68
    }
76
77
181
  TMP_MARK;
78
181
  MPZ_TMP_INIT (g, usize); /* v != 0 implies |gcd(u,v)| <= |u| */
79
80
181
  mpz_gcd (g, u, v);
81
181
  mpz_divexact (g, u, g);
82
181
  mpz_mul (r, g, v);
83
84
181
  SIZ (r) = ABS (SIZ (r)); /* result always positive */
85
86
181
  TMP_FREE;
87
181
}