Blame rate-lav/gcd.h

Packit 675970
/*
Packit 675970
 * Fast implementation of greatest common divisor using the binary algorithm.
Packit 675970
 * Copyright (c) 2007 Nicholas Kain
Packit 675970
 *
Packit 675970
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit 675970
 * of this software and associated documentation files (the "Software"), to
Packit 675970
 * deal in the Software without restriction, including without limitation the
Packit 675970
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit 675970
 * sell copies of the Software, and to permit persons to whom the Software is
Packit 675970
 * furnished to do so, subject to the following conditions:
Packit 675970
 *
Packit 675970
 * The above copyright notice and this permission notice shall be included in
Packit 675970
 * all copies or substantial portions of the Software.
Packit 675970
 *
Packit 675970
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit 675970
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit 675970
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit 675970
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit 675970
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit 675970
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit 675970
 * IN THE SOFTWARE.
Packit 675970
 */
Packit 675970
Packit 675970
/* computes gcd using binary algorithm */
Packit 675970
static int gcd(int a, int b)
Packit 675970
{
Packit 675970
	int s,d;
Packit 675970
Packit 675970
	if (!a || !b)
Packit 675970
		return a | b;
Packit 675970
Packit 675970
	for (s=0; ((a|b)&1) == 0; ++s) {
Packit 675970
		a >>= 1;
Packit 675970
		b >>= 1;
Packit 675970
	}
Packit 675970
Packit 675970
	while ((a&1) == 0)
Packit 675970
		a >>= 1;
Packit 675970
	
Packit 675970
	do {
Packit 675970
		while ((b&1) == 0) {
Packit 675970
			b >>= 1;
Packit 675970
		}
Packit 675970
		if (a
Packit 675970
			b -= a;
Packit 675970
		} else {
Packit 675970
			d = a-b;
Packit 675970
			a = b;
Packit 675970
			b = d;
Packit 675970
		}
Packit 675970
		b >>= 1;
Packit 675970
	} while (b);
Packit 675970
Packit 675970
	return a << s;
Packit 675970
}
Packit 675970