summaryrefslogtreecommitdiff
path: root/src/commonlib/bsd/gcd.c
blob: fbc8103a32eecf99d80192b75f2f634dd89ae7ee (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>

uint64_t gcd(uint64_t a, uint64_t b)
{
	uint64_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;
}