commonlib: Add GCD function
Implement a simple GCD function.
BUG=b:307790895
TEST=emerge-geralt coreboot
TEST=make tests/commonlib/bsd/gcd-test
Change-Id: I21819cda4299b3809b8ca7a95cbdc6a87e4b3481
Signed-off-by: Yidi Lin <yidilin@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/78798
Reviewed-by: Martin L Roth <gaumless@gmail.com>
Reviewed-by: Julius Werner <jwerner@chromium.org>
Reviewed-by: Yu-Ping Wu <yupingso@google.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/src/commonlib/bsd/gcd.c b/src/commonlib/bsd/gcd.c
new file mode 100644
index 0000000..92b601e
--- /dev/null
+++ b/src/commonlib/bsd/gcd.c
@@ -0,0 +1,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;
+}