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 | 242 | { |
36 | 242 | mpz_t g; |
37 | 242 | mp_size_t usize, vsize; |
38 | 242 | TMP_DECL; |
39 | | |
40 | 242 | usize = SIZ (u); |
41 | 242 | vsize = SIZ (v); |
42 | 242 | if (usize == 0 || vsize == 0) |
43 | 25 | { |
44 | 25 | SIZ (r) = 0; |
45 | 25 | return; |
46 | 25 | } |
47 | 217 | usize = ABS (usize); |
48 | 217 | vsize = ABS (vsize); |
49 | | |
50 | 217 | if (vsize == 1 || usize == 1) |
51 | 23 | { |
52 | 23 | mp_limb_t vl, gl, c; |
53 | 23 | mp_srcptr up; |
54 | 23 | mp_ptr rp; |
55 | | |
56 | 23 | if (usize == 1) |
57 | 18 | { |
58 | 18 | usize = vsize; |
59 | 18 | MPZ_SRCPTR_SWAP (u, v); |
60 | 18 | } |
61 | | |
62 | 23 | MPZ_REALLOC (r, usize+1); |
63 | | |
64 | 23 | up = PTR(u); |
65 | 23 | vl = PTR(v)[0]; |
66 | 23 | gl = mpn_gcd_1 (up, usize, vl); |
67 | 23 | vl /= gl; |
68 | | |
69 | 23 | rp = PTR(r); |
70 | 23 | c = mpn_mul_1 (rp, up, usize, vl); |
71 | 23 | rp[usize] = c; |
72 | 23 | usize += (c != 0); |
73 | 23 | SIZ(r) = usize; |
74 | 23 | return; |
75 | 23 | } |
76 | | |
77 | 194 | TMP_MARK; |
78 | 194 | MPZ_TMP_INIT (g, usize); /* v != 0 implies |gcd(u,v)| <= |u| */ |
79 | | |
80 | 194 | mpz_gcd (g, u, v); |
81 | 194 | mpz_divexact (g, u, g); |
82 | 194 | mpz_mul (r, g, v); |
83 | | |
84 | 194 | SIZ (r) = ABS (SIZ (r)); /* result always positive */ |
85 | | |
86 | 194 | TMP_FREE; |
87 | 194 | } |