Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Rasmus Villemoes | 6016daed | 2015-02-12 15:03:21 -0800 | [diff] [blame] | 2 | #include <linux/compiler.h> |
Martin K. Petersen | 2cda272 | 2010-03-15 12:46:51 +0100 | [diff] [blame] | 3 | #include <linux/gcd.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 4 | #include <linux/export.h> |
H Hartley Sweeten | 72d3950 | 2011-07-25 17:13:20 -0700 | [diff] [blame] | 5 | #include <linux/lcm.h> |
Martin K. Petersen | 2cda272 | 2010-03-15 12:46:51 +0100 | [diff] [blame] | 6 | |
| 7 | /* Lowest common multiple */ |
| 8 | unsigned long lcm(unsigned long a, unsigned long b) |
| 9 | { |
| 10 | if (a && b) |
Rasmus Villemoes | 74a5fef | 2014-12-10 15:51:27 -0800 | [diff] [blame] | 11 | return (a / gcd(a, b)) * b; |
Rasmus Villemoes | 69c953c | 2014-12-10 15:51:29 -0800 | [diff] [blame] | 12 | else |
| 13 | return 0; |
Martin K. Petersen | 2cda272 | 2010-03-15 12:46:51 +0100 | [diff] [blame] | 14 | } |
| 15 | EXPORT_SYMBOL_GPL(lcm); |
Mike Snitzer | e963741 | 2015-03-30 13:39:09 -0400 | [diff] [blame] | 16 | |
| 17 | unsigned long lcm_not_zero(unsigned long a, unsigned long b) |
| 18 | { |
| 19 | unsigned long l = lcm(a, b); |
| 20 | |
| 21 | if (l) |
| 22 | return l; |
| 23 | |
| 24 | return (b ? : a); |
| 25 | } |
| 26 | EXPORT_SYMBOL_GPL(lcm_not_zero); |