summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/include/lib.h4
-rw-r--r--tests/lib/Makefile.inc3
-rw-r--r--tests/lib/lib-test.c60
3 files changed, 65 insertions, 2 deletions
diff --git a/src/include/lib.h b/src/include/lib.h
index 8e8bab55c19c..b3cedb571c78 100644
--- a/src/include/lib.h
+++ b/src/include/lib.h
@@ -54,8 +54,8 @@ static inline int log2(u32 x) { return sizeof(x) * 8 - clz(x) - 1; }
/* Find First Set: __ffs(1) == 0, __ffs(0) == -1, __ffs(1<<31) == 31 */
static inline int __ffs(u32 x) { return log2(x & (u32)(-(s32)x)); }
-/* Integer binary logarithm (rounding up): log2_ceil(0) == -1, log2(5) == 3 */
-static inline int log2_ceil(u32 x) { return (x == 0) ? -1 : log2(x * 2 - 1); }
+/* Integer binary logarithm (rounding up): log2_ceil(0) == -1, log2_ceil(5) == 3 */
+static inline int log2_ceil(u32 x) { return (x == 0) ? -1 : log2(x - 1) + 1; }
static inline int popcnt64(u64 x) { return __builtin_popcountll(x); }
static inline int clz64(u64 x) { return x ? __builtin_clzll(x) : sizeof(x) * 8; }
diff --git a/tests/lib/Makefile.inc b/tests/lib/Makefile.inc
index 0aea0ce62a76..9a624767ca45 100644
--- a/tests/lib/Makefile.inc
+++ b/tests/lib/Makefile.inc
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: GPL-2.0-only
+tests-y += lib-test
tests-y += string-test
tests-y += b64_decode-test
tests-y += hexstrtobin-test
@@ -40,6 +41,8 @@ tests-y += cbfs-lookup-no-mcache-test
tests-y += cbfs-lookup-has-mcache-test
tests-y += lzma-test
+lib-test-srcs += tests/lib/lib-test.c
+
string-test-srcs += tests/lib/string-test.c
string-test-srcs += src/lib/string.c
diff --git a/tests/lib/lib-test.c b/tests/lib/lib-test.c
new file mode 100644
index 000000000000..adaa2f9500ef
--- /dev/null
+++ b/tests/lib/lib-test.c
@@ -0,0 +1,60 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <lib.h>
+#include <tests/test.h>
+
+void test_popcnt(void **state)
+{
+ assert_int_equal(popcnt(0x0), 0);
+ assert_int_equal(popcnt(0x10), 1);
+ assert_int_equal(popcnt(0x10010010), 3);
+ assert_int_equal(popcnt(0xffffffff), 32);
+}
+
+void test_clz(void **state)
+{
+ assert_int_equal(clz(0x0), 32);
+ assert_int_equal(clz(0xf), 28);
+ assert_int_equal(clz(0x80000000), 0);
+ assert_int_equal(clz(0xffffffff), 0);
+}
+
+void test_log2(void **state)
+{
+ assert_int_equal(log2(0x0), -1);
+ assert_int_equal(log2(0x1), 0);
+ assert_int_equal(log2(0x5), 2);
+ assert_int_equal(log2(0x80000000), 31);
+ assert_int_equal(log2(0xffffffff), 31);
+}
+
+void test_ffs(void **state)
+{
+ assert_int_equal(__ffs(0x0), -1);
+ assert_int_equal(__ffs(0x1), 0);
+ assert_int_equal(__ffs(0x1010), 4);
+ assert_int_equal(__ffs(0x10000000), 28);
+ assert_int_equal(__ffs(0xffffffff), 0);
+}
+
+void test_log2_ceil(void **state)
+{
+ assert_int_equal(log2_ceil(0x0), -1);
+ assert_int_equal(log2_ceil(0x1), 0);
+ assert_int_equal(log2_ceil(0x5), 3);
+ assert_int_equal(log2_ceil(0x80000000), 31);
+ assert_int_equal(log2_ceil(0xffffffff), 32);
+}
+
+int main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_popcnt),
+ cmocka_unit_test(test_clz),
+ cmocka_unit_test(test_log2),
+ cmocka_unit_test(test_ffs),
+ cmocka_unit_test(test_log2_ceil),
+ };
+
+ return cb_run_group_tests(tests, NULL, NULL);
+}