add CuTest / update README

This commit is contained in:
2024-01-11 11:37:56 +01:00
parent 20e0c4d9fc
commit c2ad81b2b5
5 changed files with 246 additions and 182 deletions

View File

@@ -16,181 +16,241 @@
#include "br.h"
#include "bitops.h"
// #include "cutest/CuTest.h"
#include "cutest/CuTest.h"
static void test_popcount()
static const struct test32_1 {
u32 t32; /* input */
int popc;
int ctz;
int clz;
int ffs;
int ffz;
int fls;
int ilog2;
} test32_1[] = {
{ 0x00000000, 0, 32, 32, 0, 1, 0, 0 }, /* C undefined for some values */
{ 0xffffffff, 32, 0, 0, 1, 0, 32, 31 },
{ 0x00000001, 1, 0, 31, 1, 2, 1, 0 },
{ 0x80000000, 1, 31, 0, 32, 1, 32, 31 },
{ 0x71800718, 10, 3, 1, 4, 1, 31, 30 },
{ 0x07eeeef7, 22, 0, 5, 1, 4, 27, 26 },
};
static const struct test64_1 {
u64 t64; /* input */
int popc;
int ctz;
int clz;
int ffs;
int ffz;
int fls;
int ilog2;
} test64_1[] = {
{ 0x0000000000000000, 0, 64, 64, 0, 1, 0, 0 }, /* undefined for some values */
{ 0xffffffffffffffff, 64, 0, 0, 1, 0, 64, 63 },
{ 0x0000000100000001, 2, 0, 31, 1, 2, 33, 32 },
{ 0x8000000000000000, 1, 63, 0, 64, 1, 64, 63 },
{ 0x7180071871800718, 20, 3, 1, 4, 1, 63, 62 },
{ 0x07eeeef707eeeef7, 44, 0, 5, 1, 4, 59, 58 },
};
static void cutest_popcount(CuTest *tc)
{
u32 t32[] = { 0x0, 0x88000101, 0xffffffff };
u64 t64[] = { 0x0ll, 0x8880000000000101LL, 0xffffffffffffffffll };
for (uint i = 0; i < ARRAY_SIZE(t32); ++i) {
printf("popcount32 (%#x): ", t32[i]);
# ifdef ___popcount32_native
printf("native:%d ", __popcount32_native(t32[i]));
# else
printf("native:XXX ");
# endif
printf("emulated:%d ", __popcount_emulated(t32[i]));
printf("\n");
for (uint i = 0; i < ARRAY_SIZE(test32_1); ++i) {
int res = popcount32(test32_1[i].t32);
CuAssertIntEquals(tc, test32_1[i].popc, res);
}
printf("\n");
for (uint i = 0; i < ARRAY_SIZE(t64); ++i) {
printf("popcount64 (%#lx): ", t64[i]);
# ifdef ___popcount64_native
printf("native:%d ", __popcount64_native(t64[i]));
# else
printf("native:XXX ");
# endif
printf("emulated:%d ", __popcount_emulated(t64[i]));
printf("\n");
for (uint i = 0; i < ARRAY_SIZE(test64_1); ++i) {
int res = popcount64(test64_1[i].t64);
CuAssertIntEquals(tc, test64_1[i].popc, res);
}
printf("\n");
}
static void test_ctz()
static void cutest_ctz(CuTest *tc)
{
u32 t32[] = {
0x88800101,
0xffffffff,
0x800,
0x80000000,
0x00800000
};
u64 t64[] = {
0x8880000000000101LL,
0xffffffffffffffffll,
0x800ll,
0x8000000000000000LL,
0x0080000000000000LL};
for (uint i = 0; i < ARRAY_SIZE(t32); ++i) {
printf("ctz32 (%#x): ", t32[i]);
# ifdef __ctz32_native
printf("native:%d ", __ctz32_native(t32[i]));
# else
printf("native:XXX ");
# endif
printf("emulated1:%d ", __ctz32_emulated(t32[i]));
printf("emulated2:%d ", __ctz32_emulated2(t32[i]));
//printf("emulated3:%d ", __ctz32_emulated3(t32[i]));
//printf("emulated4:%d ", __ctz32_emulated4(t32[i]));
printf("\n");
for (uint i = 0; i < ARRAY_SIZE(test32_1); ++i) {
int res = ctz32(test32_1[i].t32);
CuAssertIntEquals(tc, test32_1[i].ctz, res);
}
printf("\n");
for (uint i = 0; i < ARRAY_SIZE(t64); ++i) {
printf("ctz64 (%#lx): ", t64[i]);
# ifdef __ctz64_native
printf("native:%d ", __ctz64_native(t64[i]));
# else
printf("native:XXX ");
# endif
printf("emulated1:%d ", __ctz64_emulated(t64[i]));
printf("emulated2:%d ", __ctz64_emulated2(t64[i]));
//printf("emulated3:%d ", __ctz64_emulated3(t64[i]));
//printf("emulated4:%d ", __ctz64_emulated4(t64[i]));
printf("\n");
for (uint i = 0; i < ARRAY_SIZE(test64_1); ++i) {
int res = ctz64(test64_1[i].t64);
//printf("t=%#llx r=%d e=%d\n", test64_1[i].t64, res, test64_1[i].ctz);
CuAssertIntEquals(tc, test64_1[i].ctz, res);
}
printf("\n");
}
static void test_clz()
static void cutest_clz(CuTest *tc)
{
u32 t32[] = {
0x88800101,
0xffffffff,
0x800,
0x80000000,
0x00800000
};
u64 t64[] = {
0x8880000000000101LL,
0xffffffffffffffffll,
0x800ll,
0x8000000000000000LL,
0x0080000000000000LL};
for (uint i = 0; i < ARRAY_SIZE(t32); ++i) {
printf("clz32 (%#x): ", t32[i]);
# ifdef __clz32_native
printf("native:%d ", __clz32_native(t32[i]));
# else
printf("native:XXX ");
# endif
printf("emulated1:%d ", __clz32_emulated(t32[i]));
//printf("emulated2:%d ", __clz32_emulated2(t32[i]));
//printf("emulated3:%d ", __ctz32_emulated3(t32[i]));
//printf("emulated4:%d ", __ctz32_emulated4(t32[i]));
printf("\n");
for (uint i = 0; i < ARRAY_SIZE(test32_1); ++i) {
int res = clz32(test32_1[i].t32);
//printf("clz t=%#x r=%d e=%d\n", test32_1[i].t32, res, test32_1[i].clz);
CuAssertIntEquals(tc, test32_1[i].clz, res);
}
printf("\n");
for (uint i = 0; i < ARRAY_SIZE(t64); ++i) {
printf("clz64 (%#lx): ", t64[i]);
# ifdef __clz64_native
printf("native:%d ", __clz64_native(t64[i]));
# else
printf("native:XXX ");
# endif
printf("emulated1:%d ", __clz64_emulated(t64[i]));
//printf("emulated2:%d ", __ctz64_emulated2(t64[i]));
//printf("emulated3:%d ", __ctz64_emulated3(t64[i]));
//printf("emulated4:%d ", __ctz64_emulated4(t64[i]));
printf("\n");
for (uint i = 0; i < ARRAY_SIZE(test64_1); ++i) {
int res = clz64(test64_1[i].t64);
//printf("clz t=%#llx r=%d e=%d\n", test64_1[i].t64, res, test64_1[i].clz);
CuAssertIntEquals(tc, test64_1[i].clz, res);
}
printf("\n");
}
static void test_ffs()
static void cutest_ffs(CuTest *tc)
{
u32 t32[] = {
0x88800101,
0xffffffff,
0x800,
0x80000000,
0x00800000
};
u64 t64[] = {
0x8880000000000101LL,
0xffffffffffffffffll,
0x800ll,
0x8000000000000000LL,
0x0080000000000000LL};
for (uint i = 0; i < ARRAY_SIZE(test32_1); ++i) {
int res = ffs32(test32_1[i].t32);
CuAssertIntEquals(tc, test32_1[i].ffs, res);
}
for (uint i = 0; i < ARRAY_SIZE(test64_1); ++i) {
int res = ffs64(test64_1[i].t64);
//printf("ffs64 t=%#llx r=%d e=%d\n", test64_1[i].t64, res, test64_1[i].ffs);
CuAssertIntEquals(tc, test64_1[i].ffs, res);
}
}
for (uint i = 0; i < ARRAY_SIZE(t32); ++i) {
printf("ffs32 (%#x): ", t32[i]);
# ifdef __ffs32_native
printf("native:%d ", __ffs32_native(t32[i]));
# else
printf("native:XXX ");
# endif
printf("popcount:%d ", __ffs32_popcount(t32[i]));
printf("ctz:%d ", __ffs32_ctz(t32[i]));
printf("emulated:%d ", __ffs32_emulated(t32[i]));
//printf("emulated4:%d ", __ctz32_emulated4(t32[i]));
printf("\n");
static void cutest_ffz(CuTest *tc)
{
for (uint i = 0; i < ARRAY_SIZE(test32_1); ++i) {
int res = ffz32(test32_1[i].t32);
//printf("ffz32 t=%#x r=%d e=%d\n", test32_1[i].t32, res, test32_1[i].ffz);
CuAssertIntEquals(tc, test32_1[i].ffz, res);
}
printf("\n");
for (uint i = 0; i < ARRAY_SIZE(t64); ++i) {
printf("ffs64 (%#lx): ", t64[i]);
# ifdef __ffs64_native
printf("native:%d ", __ffs64_native(t64[i]));
# else
printf("native:XXX ");
# endif
printf("popcount:%d ", __ffs64_popcount(t64[i]));
printf("ctz:%d ", __ffs64_ctz(t64[i]));
printf("emulated:%d ", __ffs64_emulated(t64[i]));
//printf("emulated4:%d ", __ctz64_emulated4(t64[i]));
printf("\n");
for (uint i = 0; i < ARRAY_SIZE(test64_1); ++i) {
int res = ffz64(test64_1[i].t64);
//printf("ffz64 t=%#llx r=%d e=%d\n", test64_1[i].t64, res, test64_1[i].ffz);
CuAssertIntEquals(tc, test64_1[i].ffz, res);
}
printf("\n");
printf("\n");
}
static void cutest_fls(CuTest *tc)
{
for (uint i = 0; i < ARRAY_SIZE(test32_1); ++i) {
int res = fls32(test32_1[i].t32);
//printf("fls32 t=%#x r=%d e=%d\n", test32_1[i].t32, res, test32_1[i].fls);
CuAssertIntEquals(tc, test32_1[i].fls, res);
}
for (uint i = 0; i < ARRAY_SIZE(test64_1); ++i) {
int res = fls64(test64_1[i].t64);
//printf("fls64 t=%#llx r=%d e=%d\n", test64_1[i].t64, res, test64_1[i].fls);
CuAssertIntEquals(tc, test64_1[i].fls, res);
}
}
static void cutest_ilog(CuTest *tc)
{
for (uint i = 0; i < ARRAY_SIZE(test32_1); ++i) {
if (!test32_1[i].t32) {
printf("ilog2_32 t=%#x skipped\n", test32_1[i].t32);
continue;
}
int res = ilog2_32(test32_1[i].t32);
printf("ilog2_32_n t=%#x r=%d e=%d\n", test32_1[i].t32,
res, test32_1[i].ilog2);
CuAssertIntEquals(tc, test32_1[i].ilog2, res);
}
for (uint i = 0; i < ARRAY_SIZE(test64_1); ++i) {
if (!test64_1[i].t64) {
printf("ilog2_32 t=%#llx skipped\n", test64_1[i].t64);
continue;
}
int res = ilog2_64(test64_1[i].t64);
printf("ilog2_64_n t=%#llx r=%d e=%d\n", test64_1[i].t64,
res, test64_1[i].ilog2);
CuAssertIntEquals(tc, test64_1[i].ilog2, res);
}
}
struct test32_2 {
u32 t32; /* input */
u32 arg;
u32 rol;
u32 ror;
} test32_2[] = {
{ 0x00000000, 0, 0, 0 },
{ 0x00000000, 1, 0, 0 },
{ 0x10000001, 2, 0x40000004, 0x44000000 },
{ 0x80000008, 3, 0x00000044, 0x10000001 },
{ 0x71800718, 8, 0x80071871, 0x18718007 },
{ 0x07eeeef7, 4, 0x7eeeef70, 0x707eeeef },
};
struct test64_2 {
u64 t64; /* input */
u32 arg;
u64 rol;
u64 ror;
} test64_2[] = {
{ 0x0000000000000000, 0, 0, 0 },
{ 0x0000000000000000, 1, 0, 0 },
{ 0x1000000110000001, 2, 0x4000000440000004, 0x4400000044000000 },
{ 0x8000000880000008, 3, 0x0000004400000044, 0x1000000110000001 },
{ 0x7180071871800718, 8, 0x8007187180071871, 0x1871800718718007 },
{ 0x07eeeef707eeeef7, 4, 0x7eeeef707eeeef70, 0x707eeeef707eeeef },
};
static void cutest_rol(CuTest *tc)
{
for (uint i = 0; i < ARRAY_SIZE(test32_2); ++i) {
u32 res = rol32(test32_2[i].t32, test32_2[i].arg);
//printf("rol32 t=%#x a=%u r=%#08x e=%#08x\n", test32_2[i].t32,
// test32_2[i].arg, res, test32_2[i].rol);
CuAssertU32Equals(tc, test32_2[i].rol, res);
}
for (uint i = 0; i < ARRAY_SIZE(test64_2); ++i) {
u64 res = rol64(test64_2[i].t64, test64_2[i].arg);
//printf("rol64 t=%#llx a=%u r=%#08llx e=%#08llx\n", test64_2[i].t64,
// test64_2[i].arg, res, test64_2[i].rol);
CuAssertU64Equals(tc, test64_2[i].rol, res);
}
}
static void cutest_ror(CuTest *tc)
{
for (uint i = 0; i < ARRAY_SIZE(test32_2); ++i) {
u32 res = ror32(test32_2[i].t32, test32_2[i].arg);
//printf("ror32 t=%#x a=%u r=%#08x e=%#08x\n", test32_2[i].t32,
// test32_2[i].arg, res, test32_2[i].ror);
CuAssertU32Equals(tc, test32_2[i].ror, res);
}
for (uint i = 0; i < ARRAY_SIZE(test64_2); ++i) {
u64 res = ror64(test64_2[i].t64, test64_2[i].arg);
//printf("ror64 t=%#0llx a=%u r=%#08llx e=%#08llx\n", test64_2[i].t64,
// test64_2[i].arg, res, test64_2[i].ror);
CuAssertU64Equals(tc, test64_2[i].ror, res);
}
}
#define MAXLOOPS 8
static CuSuite *bitops_GetSuite()
{
CuSuite* suite = CuSuiteNew();
SUITE_ADD_TEST(suite, cutest_popcount);
SUITE_ADD_TEST(suite, cutest_ctz);
SUITE_ADD_TEST(suite, cutest_clz);
SUITE_ADD_TEST(suite, cutest_ffs);
SUITE_ADD_TEST(suite, cutest_ffz);
SUITE_ADD_TEST(suite, cutest_fls);
SUITE_ADD_TEST(suite, cutest_ilog);
SUITE_ADD_TEST(suite, cutest_rol);
SUITE_ADD_TEST(suite, cutest_ror);
return suite;
}
static void RunAllTests(void)
{
CuString *output = CuStringNew();
CuSuite* suite = CuSuiteNew();
CuSuiteAddSuite(suite, bitops_GetSuite());
CuSuiteRun(suite);
CuSuiteSummary(suite, output);
CuSuiteDetails(suite, output);
printf("%s\n", output->buffer);
}
int main()
{
test_popcount();
test_ctz();
test_clz();
test_ffs();
RunAllTests();
exit(0);
}