I propose this patch to make gcd generic.
I needed an uintmax_t gcd for an application.
I hope the documentation in the module file is
useful - please improve it if possible.
Regards,
Oskar
diff -u -p -r1.4 gcd.c
--- lib/gcd.c 14 May 2005 06:03:58 -0000 1.4
+++ lib/gcd.c 28 Apr 2006 23:39:14 -0000
@@ -1,6 +1,7 @@
-/* Arithmetic.
- Copyright (C) 2001-2002 Free Software Foundation, Inc.
+/* Greatest common divisor.
+ Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc.
Written by Bruno Haible <[EMAIL PROTECTED]>, 2001.
+ Made generic by Oskar Liljeblad <[EMAIL PROTECTED]>, 2006.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -16,14 +17,16 @@
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
-/* Specification. */
-#include "gcd.h"
-
#include <stdlib.h>
+#ifndef gcd_t
+# define gcd_t unsigned long
+# define GCD gcd
+#endif
+
/* Return the greatest common divisor of a > 0 and b > 0. */
-unsigned long
-gcd (unsigned long a, unsigned long b)
+gcd_t
+GCD (gcd_t a, gcd_t b)
{
/* Why no division, as in Euclid's algorithm? Because in Euclid's algorithm
the division result floor(a/b) or floor(b/a) is very often = 1 or = 2,
@@ -33,7 +36,7 @@ gcd (unsigned long a, unsigned long b)
bit in a single instruction, and the algorithm uses fewer variables than
Euclid's algorithm. */
- unsigned long c = a | b;
+ gcd_t c = a | b;
c = c ^ (c - 1);
/* c = largest power of 2 that divides a and b. */
diff -u -p -r1.4 gcd
--- modules/gcd 22 Sep 2004 15:11:04 -0000 1.4
+++ modules/gcd 28 Apr 2006 23:39:15 -0000
@@ -1,5 +1,15 @@
Description:
Greatest common divisor.
+The default gcd function operates with `unsigned long' values.
+You can generate code for gcd functions that deal with other
+types of values by creating a .c source file containing code
+like this:
+ #define GCD gcdull
+ #define gcd_t unsigned long long
+ #include "gcd.c"
+Here `GCD' specifies the name of the function, and `gcd_t' the
+type of arguments and return value. Generated gcd functions
+will need to be declared manually.
Files:
lib/gcd.h