github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpz/gcd_ui.c (about) 1 /* mpz_gcd_ui -- Calculate the greatest common divisor of two integers. 2 3 Copyright 1994, 1996, 1999-2004 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 <stdio.h> /* for NULL */ 32 #include "gmp.h" 33 #include "gmp-impl.h" 34 35 unsigned long int 36 mpz_gcd_ui (mpz_ptr w, mpz_srcptr u, unsigned long int v) 37 { 38 mp_size_t un; 39 mp_limb_t res; 40 41 #if BITS_PER_ULONG > GMP_NUMB_BITS /* avoid warnings about shift amount */ 42 if (v > GMP_NUMB_MAX) 43 { 44 mpz_t vz; 45 mp_limb_t vlimbs[2]; 46 vlimbs[0] = v & GMP_NUMB_MASK; 47 vlimbs[1] = v >> GMP_NUMB_BITS; 48 PTR(vz) = vlimbs; 49 SIZ(vz) = 2; 50 mpz_gcd (w, u, vz); 51 /* because v!=0 we will have w<=v hence fitting a ulong */ 52 ASSERT (mpz_fits_ulong_p (w)); 53 return mpz_get_ui (w); 54 } 55 #endif 56 57 un = ABSIZ(u); 58 59 if (un == 0) 60 res = v; 61 else if (v == 0) 62 { 63 if (w != NULL) 64 { 65 if (u != w) 66 { 67 MPZ_REALLOC (w, un); 68 MPN_COPY (PTR(w), PTR(u), un); 69 } 70 SIZ(w) = un; 71 } 72 /* Return u if it fits a ulong, otherwise 0. */ 73 res = PTR(u)[0]; 74 return (un == 1 && res <= ULONG_MAX ? res : 0); 75 } 76 else 77 res = mpn_gcd_1 (PTR(u), un, (mp_limb_t) v); 78 79 if (w != NULL) 80 { 81 PTR(w)[0] = res; 82 SIZ(w) = res != 0; 83 } 84 return res; 85 }