diff options
author | Yidi Lin <yidilin@chromium.org> | 2023-10-31 14:57:04 +0800 |
---|---|---|
committer | Martin L Roth <gaumless@gmail.com> | 2023-11-04 17:05:28 +0000 |
commit | 909c317b2deb184d95c7d289cbe6603e209ed72d (patch) | |
tree | 67527e94bdf82faabcededcb3a22b5bfb7faf684 | |
parent | ed62dbaf674d102d45b84b38c5de62134e5bca4c (diff) |
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>
-rw-r--r-- | src/commonlib/Makefile.inc | 3 | ||||
-rw-r--r-- | src/commonlib/bsd/gcd.c | 23 | ||||
-rw-r--r-- | src/commonlib/bsd/include/commonlib/bsd/gcd.h | 10 | ||||
-rw-r--r-- | tests/commonlib/bsd/Makefile.inc | 4 | ||||
-rw-r--r-- | tests/commonlib/bsd/gcd-test.c | 27 |
5 files changed, 67 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_ */ diff --git a/tests/commonlib/bsd/Makefile.inc b/tests/commonlib/bsd/Makefile.inc index 56664d037c..bf17b6d56d 100644 --- a/tests/commonlib/bsd/Makefile.inc +++ b/tests/commonlib/bsd/Makefile.inc @@ -1,5 +1,9 @@ # SPDX-License-Identifier: GPL-2.0-only tests-y += helpers-test +tests-y += gcd-test helpers-test-srcs += tests/commonlib/bsd/helpers-test.c + +gcd-test-srcs += tests/commonlib/bsd/gcd-test.c +gcd-test-srcs += src/commonlib/bsd/gcd.c diff --git a/tests/commonlib/bsd/gcd-test.c b/tests/commonlib/bsd/gcd-test.c new file mode 100644 index 0000000000..13fad86ec4 --- /dev/null +++ b/tests/commonlib/bsd/gcd-test.c @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <commonlib/bsd/gcd.h> +#include <tests/test.h> + +static void test_gcd32(void **state) +{ + assert_int_equal(gcd32(17, 11), 1); + assert_int_equal(gcd32(64, 36), 4); + assert_int_equal(gcd32(90, 123), 3); + assert_int_equal(gcd32(65536, 339584), 128); + assert_int_equal(gcd32(1, 1), 1); + assert_int_equal(gcd32(1, 123), 1); + assert_int_equal(gcd32(123, 1), 1); + assert_int_equal(gcd32(1, UINT32_MAX), 1); + assert_int_equal(gcd32(UINT32_MAX, 1), 1); + assert_int_equal(gcd32(UINT32_MAX, UINT32_MAX), UINT32_MAX); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_gcd32), + }; + + return cb_run_group_tests(tests, NULL, NULL); +} |