aboutsummaryrefslogtreecommitdiff
path: root/src/commonlib
diff options
context:
space:
mode:
Diffstat (limited to 'src/commonlib')
-rw-r--r--src/commonlib/Makefile.inc3
-rw-r--r--src/commonlib/bsd/gcd.c23
-rw-r--r--src/commonlib/bsd/include/commonlib/bsd/gcd.h10
3 files changed, 36 insertions, 0 deletions
diff --git a/src/commonlib/Makefile.inc b/src/commonlib/Makefile.inc
index 86e8c5695c..70e731df35 100644
--- a/src/commonlib/Makefile.inc
+++ b/src/commonlib/Makefile.inc
@@ -58,3 +58,6 @@ ramstage-y += sort.c
romstage-y += bsd/elog.c
ramstage-y += bsd/elog.c
smm-y += bsd/elog.c
+
+decompressor-y += bsd/gcd.c
+all-y += bsd/gcd.c
diff --git a/src/commonlib/bsd/gcd.c b/src/commonlib/bsd/gcd.c
new file mode 100644
index 0000000000..92b601e8d0
--- /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;
+}
diff --git a/src/commonlib/bsd/include/commonlib/bsd/gcd.h b/src/commonlib/bsd/include/commonlib/bsd/gcd.h
new file mode 100644
index 0000000000..20949ded09
--- /dev/null
+++ b/src/commonlib/bsd/include/commonlib/bsd/gcd.h
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+
+#ifndef _COMMONLIB_BSD_GCD_H_
+#define _COMMONLIB_BSD_GCD_H_
+
+#include <stdint.h>
+
+uint32_t gcd32(uint32_t a, uint32_t b);
+
+#endif /* _COMMONLIB_BSD_GCD_H_ */