1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
4 #include <linux/export.h>
7 * This implements the binary GCD algorithm. (Often attributed to Stein,
8 * but as Knuth has noted, appears in a first-century Chinese math text.)
10 * This is faster than the division-based algorithm even on x86, which
11 * has decent hardware division.
14 #if !defined(CONFIG_CPU_NO_EFFICIENT_FFS)
16 /* If __ffs is available, the even/odd algorithm benchmarks slower. */
19 * gcd - calculate and return the greatest common divisor of 2 unsigned longs
23 unsigned long gcd(unsigned long a, unsigned long b)
25 unsigned long r = a | b;
49 /* If normalization is done by loops, the even/odd algorithm is a win. */
50 unsigned long gcd(unsigned long a, unsigned long b)
52 unsigned long r = a | b;
57 /* Isolate lsbit of r */
85 EXPORT_SYMBOL_GPL(gcd);