Line | Count | Source (jump to first uncovered line) |
1 | | /* mpz_gcd_ui -- Calculate the greatest common divisor of two integers. |
2 | | |
3 | | Copyright 1994, 1996, 1999-2004, 2015, 2022 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-impl.h" |
33 | | |
34 | | unsigned long int |
35 | | mpz_gcd_ui (mpz_ptr w, mpz_srcptr u, unsigned long int v) |
36 | 7 | { |
37 | 7 | mp_size_t un; |
38 | 7 | mp_limb_t res; |
39 | | |
40 | | #if BITS_PER_ULONG > GMP_NUMB_BITS /* avoid warnings about shift amount */ |
41 | | if (v > GMP_NUMB_MAX) |
42 | | { |
43 | | mpz_t vz, lw; |
44 | | mp_limb_t vlimbs[2], wlimbs[2]; |
45 | | |
46 | | if (w == NULL) |
47 | | { |
48 | | PTR(lw) = wlimbs; |
49 | | ALLOC(lw) = 2; |
50 | | SIZ(lw) = 0; |
51 | | w = lw; |
52 | | } |
53 | | vlimbs[0] = v & GMP_NUMB_MASK; |
54 | | vlimbs[1] = v >> GMP_NUMB_BITS; |
55 | | PTR(vz) = vlimbs; |
56 | | SIZ(vz) = 2; |
57 | | mpz_gcd (w, u, vz); |
58 | | /* because v!=0 we will have w<=v hence fitting a ulong */ |
59 | | ASSERT (mpz_fits_ulong_p (w)); |
60 | | return mpz_get_ui (w); |
61 | | } |
62 | | #endif |
63 | | |
64 | 7 | un = ABSIZ(u); |
65 | | |
66 | 7 | if (un == 0) |
67 | 2 | res = v; |
68 | 5 | else if (v == 0) |
69 | 3 | { |
70 | 3 | if (w != NULL) |
71 | 3 | { |
72 | 3 | if (u != w) |
73 | 2 | { |
74 | 2 | MPZ_NEWALLOC (w, un); |
75 | 2 | MPN_COPY (PTR(w), PTR(u), un); |
76 | 2 | } |
77 | 3 | SIZ(w) = un; |
78 | 3 | } |
79 | | /* Return u if it fits a ulong, otherwise 0. */ |
80 | 3 | res = PTR(u)[0]; |
81 | 3 | return (un == 1 && res <= ULONG_MAX ? res : 0); |
82 | 3 | } |
83 | 2 | else |
84 | 2 | res = mpn_gcd_1 (PTR(u), un, (mp_limb_t) v); |
85 | | |
86 | 4 | if (w != NULL) |
87 | 4 | { |
88 | 4 | MPZ_NEWALLOC (w, 1)[0] = res; |
89 | 4 | SIZ(w) = res != 0; |
90 | 4 | } |
91 | 4 | return res; |
92 | 7 | } |