blob: 6e0b2e7367007db3a7a1edaa148f2b08bedcc1c6 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Rasmus Villemoes6016daed2015-02-12 15:03:21 -08002#include <linux/compiler.h>
Martin K. Petersen2cda2722010-03-15 12:46:51 +01003#include <linux/gcd.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05004#include <linux/export.h>
H Hartley Sweeten72d39502011-07-25 17:13:20 -07005#include <linux/lcm.h>
Martin K. Petersen2cda2722010-03-15 12:46:51 +01006
7/* Lowest common multiple */
8unsigned long lcm(unsigned long a, unsigned long b)
9{
10 if (a && b)
Rasmus Villemoes74a5fef2014-12-10 15:51:27 -080011 return (a / gcd(a, b)) * b;
Rasmus Villemoes69c953c2014-12-10 15:51:29 -080012 else
13 return 0;
Martin K. Petersen2cda2722010-03-15 12:46:51 +010014}
15EXPORT_SYMBOL_GPL(lcm);
Mike Snitzere9637412015-03-30 13:39:09 -040016
17unsigned 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}
26EXPORT_SYMBOL_GPL(lcm_not_zero);