aboutsummaryrefslogtreecommitdiff
path: root/test/016_popcount_u32
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-07-24 09:00:49 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2025-07-24 10:18:07 +0200
commit7c3701364542355c29b2e4ebc6d719ddd123c0f2 (patch)
tree78a7aed4aea3a8f438a61ff2af4dff481767becd /test/016_popcount_u32
parent836d626e7caba9578c3440ec0484e7ff1b4cc48e (diff)
downloadnissy-core-7c3701364542355c29b2e4ebc6d719ddd123c0f2.tar.gz
nissy-core-7c3701364542355c29b2e4ebc6d719ddd123c0f2.zip
Use bit trick for portable and arm popcount
Diffstat (limited to 'test/016_popcount_u32')
-rw-r--r--test/016_popcount_u32/00_all.in0
-rw-r--r--test/016_popcount_u32/00_all.out1
-rw-r--r--test/016_popcount_u32/popcount_u32_tests.c43
3 files changed, 44 insertions, 0 deletions
diff --git a/test/016_popcount_u32/00_all.in b/test/016_popcount_u32/00_all.in
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/016_popcount_u32/00_all.in
diff --git a/test/016_popcount_u32/00_all.out b/test/016_popcount_u32/00_all.out
new file mode 100644
index 0000000..7326d96
--- /dev/null
+++ b/test/016_popcount_u32/00_all.out
@@ -0,0 +1 @@
Ok
diff --git a/test/016_popcount_u32/popcount_u32_tests.c b/test/016_popcount_u32/popcount_u32_tests.c
new file mode 100644
index 0000000..ff60c68
--- /dev/null
+++ b/test/016_popcount_u32/popcount_u32_tests.c
@@ -0,0 +1,43 @@
1#include "../test.h"
2
3int popcount_u32(uint32_t x);
4
5int
6popcount_u32_simple(uint32_t x)
7{
8 int ret;
9
10 for (ret = 0; x != 0; x >>= 1)
11 ret += x & 1;
12
13 return ret;
14}
15
16bool
17correct(uint32_t x)
18{
19 int expected = popcount_u32_simple(x);
20 int actual = popcount_u32(x);
21 if (actual != expected) {
22 printf("Error at %" PRIu32 ": expected %d bits, found %d\n",
23 x, expected, actual);
24 return false;
25 }
26 return true;
27}
28
29void run(void) {
30 uint32_t i;
31
32 /* Test all numbers up to 2^16, and other ranges of 2^16 numbers */
33 for (i = 0; i < 0xFFFF; i++) {
34 if (!correct(i) ||
35 !correct(i + UINT32_C(0xFFFF0000)) ||
36 !correct(i + UINT32_C(1000000)) ||
37 !correct(i + UINT32_C(1)) ||
38 !correct(i + UINT32_C(1234567)))
39 return;
40 }
41
42 printf("Ok\n");
43}

Generated with cgit - Back to sebastiano.tronto.net