[PATCH 2/2] test: provide unit tests for the RISC-V private GCC library

Heinrich Schuchardt heinrich.schuchardt at canonical.com
Mon Dec 1 18:49:04 CET 2025


Add unit tests for the functions for counting leading and trailing zero
bits.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt at canonical.com>
---
 test/lib/Makefile   |  4 ++++
 test/lib/test_clz.c | 53 +++++++++++++++++++++++++++++++++++++++++++++
 test/lib/test_ctz.c | 53 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 110 insertions(+)
 create mode 100644 test/lib/test_clz.c
 create mode 100644 test/lib/test_ctz.c

diff --git a/test/lib/Makefile b/test/lib/Makefile
index 35b40b584c4..7c9dc180c8d 100644
--- a/test/lib/Makefile
+++ b/test/lib/Makefile
@@ -10,6 +10,10 @@ obj-y += abuf.o
 obj-y += alist.o
 obj-$(CONFIG_EFI_LOADER) += efi_device_path.o efi_memory.o
 obj-$(CONFIG_EFI_SECURE_BOOT) += efi_image_region.o
+ifdef CONFIG_RISCV
+obj-$(CONFIG_USE_PRIVATE_LIBGCC) += test_clz.o
+obj-$(CONFIG_USE_PRIVATE_LIBGCC) += test_ctz.o
+endif
 obj-y += hexdump.o
 obj-$(CONFIG_SANDBOX) += kconfig.o
 obj-y += lmb.o
diff --git a/test/lib/test_clz.c b/test/lib/test_clz.c
new file mode 100644
index 00000000000..11fd527d063
--- /dev/null
+++ b/test/lib/test_clz.c
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright 2025, Heinrich Schuchardt <heinrich.schuchardt at canonical.com>
+ */
+
+#include <test/lib.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+int __clzsi2(int a);
+int __clzdi2(long a);
+int __clzti2(long long a);
+
+/**
+ * test_clz() - test library functions to count leading zero bits
+ *
+ * @uts:	unit test state
+ */
+static int test_clz(struct unit_test_state *uts)
+{
+	ut_asserteq(0, __clzti2(0xffffffffffffffffLL));
+	ut_asserteq(0, __clzti2(0x8000000000000000LL));
+	ut_asserteq(1, __clzti2(0x4000000000000000LL));
+	ut_asserteq(17, __clzti2(0x0000500000a00000LL));
+	ut_asserteq(62, __clzti2(0x0000000000000002LL));
+	ut_asserteq(63, __clzti2(0x0000000000000001LL));
+
+#if BITS_PER_LONG == 64
+	ut_asserteq(0, __clzdi2(0xffffffffffffffffLL));
+	ut_asserteq(0, __clzti2(0x8000000000000000LL));
+	ut_asserteq(1, __clzti2(0x4000000000000000LL));
+	ut_asserteq(17, __clzdi2(0x0000500000a00000LL));
+	ut_asserteq(62, __clzdi2(0x0000000000000002LL));
+	ut_asserteq(63, __clzdi2(0x0000000000000001LL));
+#else
+	ut_asserteq(0, __clzdi2(0xffffffff));
+	ut_asserteq(0, __clzdi2(0x80000000));
+	ut_asserteq(1, __clzdi2(0x40000000));
+	ut_asserteq(9, __clzdi2(0x0050a000));
+	ut_asserteq(30, __clzdi2(0x00000002));
+	ut_asserteq(31, __clzdi2(0x00000001));
+#endif
+
+	ut_asserteq(0, __clzsi2(0xffffffff));
+	ut_asserteq(0, __clzsi2(0x80000000));
+	ut_asserteq(1, __clzsi2(0x40000000));
+	ut_asserteq(9, __clzsi2(0x0050a000));
+	ut_asserteq(30, __clzsi2(0x00000002));
+	ut_asserteq(31, __clzsi2(0x00000001));
+
+	return 0;
+}
+LIB_TEST(test_clz, 0);
diff --git a/test/lib/test_ctz.c b/test/lib/test_ctz.c
new file mode 100644
index 00000000000..96c08202dc2
--- /dev/null
+++ b/test/lib/test_ctz.c
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright 2025, Heinrich Schuchardt <heinrich.schuchardt at canonical.com>
+ */
+
+#include <test/lib.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+int __ctzsi2(int a);
+int __ctzdi2(long a);
+int __ctzti2(long long a);
+
+/**
+ * test_ctz() - test library functions to count trailing zero bits
+ *
+ * @uts:	unit test state
+ */
+static int test_ctz(struct unit_test_state *uts)
+{
+	ut_asserteq(0, __ctzti2(0xffffffffffffffffLL));
+	ut_asserteq(63, __ctzti2(0x8000000000000000LL));
+	ut_asserteq(62, __ctzti2(0x4000000000000000LL));
+	ut_asserteq(21, __ctzti2(0x0000500000a00000LL));
+	ut_asserteq(1, __ctzti2(0x0000000000000002LL));
+	ut_asserteq(0, __ctzti2(0x0000000000000001LL));
+
+#if BITS_PER_LONG == 64
+	ut_asserteq(0, __ctzdi2(0xffffffffffffffffLL));
+	ut_asserteq(63, __ctzdi2(0x8000000000000000LL));
+	ut_asserteq(62, __ctzdi2(0x4000000000000000LL));
+	ut_asserteq(21, __ctzdi2(0x0000500000a00000LL));
+	ut_asserteq(1, __ctzdi2(0x0000000000000002LL));
+	ut_asserteq(0, __ctzdi2(0x0000000000000001LL));
+#else
+	ut_asserteq(0, __ctzdi2(0xffffffff));
+	ut_asserteq(31, __ctzdi2(0x80000000));
+	ut_asserteq(30, __ctzdi2(0x40000000));
+	ut_asserteq(13, __ctzdi2(0x0050a000));
+	ut_asserteq(1, __ctzdi2(0x00000002));
+	ut_asserteq(0, __ctzdi2(0x00000001));
+#endif
+
+	ut_asserteq(0, __ctzsi2(0xffffffff));
+	ut_asserteq(31, __ctzsi2(0x80000000));
+	ut_asserteq(30, __ctzsi2(0x40000000));
+	ut_asserteq(13, __ctzsi2(0x0050a000));
+	ut_asserteq(1, __ctzsi2(0x00000002));
+	ut_asserteq(0, __ctzsi2(0x00000001));
+
+	return 0;
+}
+LIB_TEST(test_ctz, 0);
-- 
2.51.0



More information about the U-Boot mailing list