summaryrefslogtreecommitdiff
path: root/src/commonlib/bsd/gcd.c
blob: 92b601e8d01dc8673a07124f27f50629f014dff0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* SPDX-License-Identifier: BSD-3-Clause */

#include <commonlib/bsd/gcd.h>
#include <commonlib/bsd/helpers.h>
#include <stdint.h>

uint32_t gcd32(uint32_t a, uint32_t b)
{
	uint32_t c;

	if (a == 0 || b == 0)
		return MAX(a, b);

	c = a % b;

	while (c > 0) {
		a = b;
		b = c;
		c = a % b;
	}

	return b;
}