]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
test: provide unit tests for the RISC-V private GCC library
authorHeinrich Schuchardt <heinrich.schuchardt@canonical.com>
Mon, 1 Dec 2025 17:49:04 +0000 (18:49 +0100)
committerLeo Yu-Chi Liang <ycliang@andestech.com>
Mon, 8 Dec 2025 04:11:01 +0000 (12:11 +0800)
Add unit tests for the functions for counting leading and trailing zero
bits.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
test/lib/Makefile
test/lib/test_clz.c [new file with mode: 0644]
test/lib/test_ctz.c [new file with mode: 0644]

index 35b40b584c46a8b8f9e583cfa22d24d3a008e3e9..7c9dc180c8d0cca881ac487eafb348b18006ac92 100644 (file)
@@ -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 (file)
index 0000000..11fd527
--- /dev/null
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright 2025, Heinrich Schuchardt <heinrich.schuchardt@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 (file)
index 0000000..96c0820
--- /dev/null
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright 2025, Heinrich Schuchardt <heinrich.schuchardt@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);