diff options
author | Andy Shevchenko <andriy.shevchenko@linux.intel.com> | 2019-05-14 18:43:05 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2019-05-14 22:52:49 -0400 |
commit | 2c64e9cb0b6b858901e9a386860d7d929d1cbaeb (patch) | |
tree | 749da0ef8f5d478680a523c877fb0e16fc18409c /lib/math/gcd.c | |
parent | b5c56e0cdd62979dd538e5363b06be5bdf735a09 (diff) |
lib: Move mathematic helpers to separate folder
For better maintenance and expansion move the mathematic helpers to the
separate folder.
No functional change intended.
Note, the int_sqrt() is not used as a part of lib, so, moved to regular
obj.
Link: http://lkml.kernel.org/r/20190323172531.80025-1-andriy.shevchenko@linux.intel.com
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: Thierry Reding <thierry.reding@gmail.com>
Cc: Lee Jones <lee.jones@linaro.org>
Cc: Daniel Thompson <daniel.thompson@linaro.org>
Cc: Ray Jui <rjui@broadcom.com>
[mchehab+samsung@kernel.org: fix broken doc references for div64.c and gcd.c]
Link: http://lkml.kernel.org/r/734f49bae5d4052b3c25691dfefad59bea2e5843.1555580999.git.mchehab+samsung@kernel.org
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib/math/gcd.c')
-rw-r--r-- | lib/math/gcd.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/lib/math/gcd.c b/lib/math/gcd.c new file mode 100644 index 000000000000..7948ab27f0a4 --- /dev/null +++ b/lib/math/gcd.c | |||
@@ -0,0 +1,84 @@ | |||
1 | #include <linux/kernel.h> | ||
2 | #include <linux/gcd.h> | ||
3 | #include <linux/export.h> | ||
4 | |||
5 | /* | ||
6 | * This implements the binary GCD algorithm. (Often attributed to Stein, | ||
7 | * but as Knuth has noted, appears in a first-century Chinese math text.) | ||
8 | * | ||
9 | * This is faster than the division-based algorithm even on x86, which | ||
10 | * has decent hardware division. | ||
11 | */ | ||
12 | |||
13 | #if !defined(CONFIG_CPU_NO_EFFICIENT_FFS) | ||
14 | |||
15 | /* If __ffs is available, the even/odd algorithm benchmarks slower. */ | ||
16 | |||
17 | /** | ||
18 | * gcd - calculate and return the greatest common divisor of 2 unsigned longs | ||
19 | * @a: first value | ||
20 | * @b: second value | ||
21 | */ | ||
22 | unsigned long gcd(unsigned long a, unsigned long b) | ||
23 | { | ||
24 | unsigned long r = a | b; | ||
25 | |||
26 | if (!a || !b) | ||
27 | return r; | ||
28 | |||
29 | b >>= __ffs(b); | ||
30 | if (b == 1) | ||
31 | return r & -r; | ||
32 | |||
33 | for (;;) { | ||
34 | a >>= __ffs(a); | ||
35 | if (a == 1) | ||
36 | return r & -r; | ||
37 | if (a == b) | ||
38 | return a << __ffs(r); | ||
39 | |||
40 | if (a < b) | ||
41 | swap(a, b); | ||
42 | a -= b; | ||
43 | } | ||
44 | } | ||
45 | |||
46 | #else | ||
47 | |||
48 | /* If normalization is done by loops, the even/odd algorithm is a win. */ | ||
49 | unsigned long gcd(unsigned long a, unsigned long b) | ||
50 | { | ||
51 | unsigned long r = a | b; | ||
52 | |||
53 | if (!a || !b) | ||
54 | return r; | ||
55 | |||
56 | /* Isolate lsbit of r */ | ||
57 | r &= -r; | ||
58 | |||
59 | while (!(b & r)) | ||
60 | b >>= 1; | ||
61 | if (b == r) | ||
62 | return r; | ||
63 | |||
64 | for (;;) { | ||
65 | while (!(a & r)) | ||
66 | a >>= 1; | ||
67 | if (a == r) | ||
68 | return r; | ||
69 | if (a == b) | ||
70 | return a; | ||
71 | |||
72 | if (a < b) | ||
73 | swap(a, b); | ||
74 | a -= b; | ||
75 | a >>= 1; | ||
76 | if (a & r) | ||
77 | a += b; | ||
78 | a >>= 1; | ||
79 | } | ||
80 | } | ||
81 | |||
82 | #endif | ||
83 | |||
84 | EXPORT_SYMBOL_GPL(gcd); | ||