xref: /openbmc/linux/lib/math/lcm.c (revision 58e16d792a6a8c6b750f637a4649967fcac853dc)
1*457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22c64e9cbSAndy Shevchenko #include <linux/compiler.h>
32c64e9cbSAndy Shevchenko #include <linux/gcd.h>
42c64e9cbSAndy Shevchenko #include <linux/export.h>
52c64e9cbSAndy Shevchenko #include <linux/lcm.h>
62c64e9cbSAndy Shevchenko 
72c64e9cbSAndy Shevchenko /* Lowest common multiple */
lcm(unsigned long a,unsigned long b)82c64e9cbSAndy Shevchenko unsigned long lcm(unsigned long a, unsigned long b)
92c64e9cbSAndy Shevchenko {
102c64e9cbSAndy Shevchenko 	if (a && b)
112c64e9cbSAndy Shevchenko 		return (a / gcd(a, b)) * b;
122c64e9cbSAndy Shevchenko 	else
132c64e9cbSAndy Shevchenko 		return 0;
142c64e9cbSAndy Shevchenko }
152c64e9cbSAndy Shevchenko EXPORT_SYMBOL_GPL(lcm);
162c64e9cbSAndy Shevchenko 
lcm_not_zero(unsigned long a,unsigned long b)172c64e9cbSAndy Shevchenko unsigned long lcm_not_zero(unsigned long a, unsigned long b)
182c64e9cbSAndy Shevchenko {
192c64e9cbSAndy Shevchenko 	unsigned long l = lcm(a, b);
202c64e9cbSAndy Shevchenko 
212c64e9cbSAndy Shevchenko 	if (l)
222c64e9cbSAndy Shevchenko 		return l;
232c64e9cbSAndy Shevchenko 
242c64e9cbSAndy Shevchenko 	return (b ? : a);
252c64e9cbSAndy Shevchenko }
262c64e9cbSAndy Shevchenko EXPORT_SYMBOL_GPL(lcm_not_zero);
27