2 Copyright (C) 2001-2002 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2001.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 /* Return the greatest common divisor of a > 0 and b > 0. */
26 gcd (unsigned long a
, unsigned long b
)
28 /* Why no division, as in Euclid's algorithm? Because in Euclid's algorithm
29 the division result floor(a/b) or floor(b/a) is very often = 1 or = 2,
30 and nearly always < 8. A sequence of a few subtractions and tests is
31 faster than a division. */
32 /* Why not Euclid's algorithm? Because the two integers can be shifted by 1
33 bit in a single instruction, and the algorithm uses fewer variables than
34 Euclid's algorithm. */
36 unsigned long c
= a
| b
;
38 /* c = largest power of 2 that divides a and b. */
57 odd_odd
: /* a/c and b/c both odd */
63 even_odd
: /* a/c even, b/c odd */
71 odd_even
: /* a/c odd, b/c even */