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