1803abf25Szrj /*
2803abf25Szrj * Copyright (c) 2015 Rimvydas Jasinskas
3803abf25Szrj * All rights reserved.
4803abf25Szrj *
5803abf25Szrj * Redistribution and use in source and binary forms, with or without
6803abf25Szrj * modification, are permitted provided that the following conditions
7803abf25Szrj * are met:
8803abf25Szrj * 1. Redistributions of source code must retain the above copyright
9803abf25Szrj * notice unmodified, this list of conditions, and the following
10803abf25Szrj * disclaimer.
11803abf25Szrj * 2. Redistributions in binary form must reproduce the above copyright
12803abf25Szrj * notice, this list of conditions and the following disclaimer in the
13803abf25Szrj * documentation and/or other materials provided with the distribution.
14803abf25Szrj *
15803abf25Szrj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16803abf25Szrj * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17803abf25Szrj * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18803abf25Szrj * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19803abf25Szrj * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20803abf25Szrj * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21803abf25Szrj * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22803abf25Szrj * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23803abf25Szrj * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24803abf25Szrj * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25803abf25Szrj */
26803abf25Szrj
27803abf25Szrj /*
28803abf25Szrj * Standard math function to compute biggest common divisor,
29803abf25Szrj * based on Euclids algorithm.
30803abf25Szrj *
31803abf25Szrj * Long implementation adapted-from:
32803abf25Szrj * lib/libc/stdlib/getopt_long.c
33803abf25Szrj *
34803abf25Szrj * Uses:
35803abf25Szrj * 64-bit values;
36803abf25Szrj * simplified loop form [if(MIN(a,b) == 0) return MAX(a,b)];
37803abf25Szrj * swap() arguments just in case..
38803abf25Szrj */
39803abf25Szrj
40803abf25Szrj #ifndef _GCD_H
41803abf25Szrj #define _GCD_H
42803abf25Szrj
43803abf25Szrj #include <linux/kernel.h>
44803abf25Szrj
45803abf25Szrj static inline uint64_t gcd64(uint64_t a, uint64_t b) __pure2;
46803abf25Szrj
47803abf25Szrj /*
48803abf25Szrj * Compute the greatest common divisor of a and b.
49803abf25Szrj */
50803abf25Szrj static inline uint64_t
gcd64(uint64_t a,uint64_t b)51803abf25Szrj gcd64(uint64_t a, uint64_t b)
52803abf25Szrj {
53803abf25Szrj uint64_t c;
54803abf25Szrj
55803abf25Szrj if (a < b)
56803abf25Szrj swap(a, b);
57803abf25Szrj
58803abf25Szrj if (b == 0)
59803abf25Szrj return a;
60803abf25Szrj
61803abf25Szrj while ((c = a % b) != 0) {
62803abf25Szrj a = b;
63803abf25Szrj b = c;
64803abf25Szrj }
65803abf25Szrj
66803abf25Szrj return (b);
67803abf25Szrj }
68803abf25Szrj
69*5b3bd696SFrançois Tigeot static inline unsigned long
gcd(unsigned long a,unsigned long b)70*5b3bd696SFrançois Tigeot gcd(unsigned long a, unsigned long b)
71*5b3bd696SFrançois Tigeot {
72*5b3bd696SFrançois Tigeot return gcd64(a, b);
73*5b3bd696SFrançois Tigeot }
74*5b3bd696SFrançois Tigeot
75803abf25Szrj #endif /* _GCD_H */
76