diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-07-24 09:00:49 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-07-24 10:18:07 +0200 |
| commit | 7c3701364542355c29b2e4ebc6d719ddd123c0f2 (patch) | |
| tree | 78a7aed4aea3a8f438a61ff2af4dff481767becd /test | |
| parent | 836d626e7caba9578c3440ec0484e7ff1b4cc48e (diff) | |
| download | nissy-core-7c3701364542355c29b2e4ebc6d719ddd123c0f2.tar.gz nissy-core-7c3701364542355c29b2e4ebc6d719ddd123c0f2.zip | |
Use bit trick for portable and arm popcount
Diffstat (limited to 'test')
| -rw-r--r-- | test/016_popcount_u32/00_all.in | 0 | ||||
| -rw-r--r-- | test/016_popcount_u32/00_all.out | 1 | ||||
| -rw-r--r-- | test/016_popcount_u32/popcount_u32_tests.c | 43 |
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 | |||
| 3 | int popcount_u32(uint32_t x); | ||
| 4 | |||
| 5 | int | ||
| 6 | popcount_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 | |||
| 16 | bool | ||
| 17 | correct(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 | |||
| 29 | void 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 | } | ||
