add bswap32/bswap64
This commit is contained in:
@@ -14,12 +14,13 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "br.h"
|
||||
#include "brlib.h"
|
||||
#include "bitops.h"
|
||||
#include "cutest/CuTest.h"
|
||||
|
||||
static const struct test32_1 {
|
||||
u32 t32; /* input */
|
||||
u32 bswap;
|
||||
int popc;
|
||||
int ctz;
|
||||
int clz;
|
||||
@@ -32,19 +33,19 @@ static const struct test32_1 {
|
||||
uchar bfe[32];
|
||||
};
|
||||
} test32_1[] = {
|
||||
{ 0x00000000, 0, 32, 32, 0, 1, 0, 0, /* sometimes undefined */
|
||||
{ 0x00000000, 0x00000000, 0, 32, 32, 0, 1, 0, 0, /* sometimes undefined */
|
||||
{ 0, { 0 } } },
|
||||
{ 0xffffffff, 32, 0, 0, 1, 0, 32, 31,
|
||||
{ 0xffffffff, 0xffffffff, 32, 0, 0, 1, 0, 32, 31,
|
||||
{ 32, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
||||
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 } } },
|
||||
{ 0x00000001, 1, 0, 31, 1, 2, 1, 0,
|
||||
{ 0x00000001, 0x01000000, 1, 0, 31, 1, 2, 1, 0,
|
||||
{ 1, { 0 } } },
|
||||
{ 0x80000000, 1, 31, 0, 32, 1, 32, 31,
|
||||
{ 0x80000000, 0x00000080, 1, 31, 0, 32, 1, 32, 31,
|
||||
{ 1, { 31 } } },
|
||||
{ 0x71800718, 10, 3, 1, 4, 1, 31, 30,
|
||||
{ 0x71800718, 0x18078071, 10, 3, 1, 4, 1, 31, 30,
|
||||
/* 0111 0001 1000 0000 0000 0111 0001 1000 */
|
||||
{ 10, { 3, 4, 8, 9, 10, 23, 24, 28, 29, 30 } } },
|
||||
{ 0x07eeeef7, 22, 0, 5, 1, 4, 27, 26,
|
||||
{ 0x07eeeef7, 0xf7eeee07, 22, 0, 5, 1, 4, 27, 26,
|
||||
/* 0000 0111 1110 1110 1110 1110 0111 1111*/
|
||||
{ 22, { 0, 1, 2, 4, 5, 6, 7, 9, 10, 11, 13,
|
||||
14, 15, 17, 18, 19, 21, 22, 23, 24, 25, 26 } } },
|
||||
@@ -52,7 +53,8 @@ static const struct test32_1 {
|
||||
|
||||
static const struct test64_1 {
|
||||
u64 t64; /* input */
|
||||
int popc;
|
||||
u64 bswap;
|
||||
int popc; /* some values may be undefined */
|
||||
int ctz;
|
||||
int clz;
|
||||
int ffs;
|
||||
@@ -64,22 +66,22 @@ static const struct test64_1 {
|
||||
uchar bfe[64];
|
||||
};
|
||||
} test64_1[] = {
|
||||
{ 0x0000000000000000, 0, 64, 64, 0, 1, 0, 0, /* sometimes undefined */
|
||||
{ 0x0000000000000000, 0x0000000000000000, 0, 64, 64, 0, 1, 0, 0,
|
||||
{ 0, { 0 } } },
|
||||
{ 0xffffffffffffffff, 64, 0, 0, 1, 0, 64, 63,
|
||||
{ 0xffffffffffffffff, 0xffffffffffffffff, 64, 0, 0, 1, 0, 64, 63,
|
||||
{ 64, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
||||
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
|
||||
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
|
||||
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63 } } },
|
||||
{ 0x0000000100000001, 2, 0, 31, 1, 2, 33, 32,
|
||||
{ 0x0000000100000001, 0x0100000001000000, 2, 0, 31, 1, 2, 33, 32,
|
||||
{ 2, { 0, 32 } } },
|
||||
{ 0x8000000000000000, 1, 63, 0, 64, 1, 64, 63,
|
||||
{ 0x8000000000000000, 0x0000000000000080, 1, 63, 0, 64, 1, 64, 63,
|
||||
{ 1, { 63 } } },
|
||||
{ 0x7180071871800718, 20, 3, 1, 4, 1, 63, 62,
|
||||
{ 0x7180071871800718, 0x1807807118078071, 20, 3, 1, 4, 1, 63, 62,
|
||||
/* 2 x 0111 0001 1000 0000 0000 0111 0001 1000 */
|
||||
{ 20, { 3, 4, 8, 9, 10, 23, 24, 28, 29, 30,
|
||||
35, 36, 40, 41, 42, 55, 56, 60, 61, 62 } } },
|
||||
{ 0x07eeeef707eeeef7, 44, 0, 5, 1, 4, 59, 58 ,
|
||||
{ 0x07eeeef707eeeef7, 0xf7eeee07f7eeee07, 44, 0, 5, 1, 4, 59, 58 ,
|
||||
/* 2 x 0000 0111 1110 1110 1110 1110 0111 1111*/
|
||||
{ 44, { 0, 1, 2, 4, 5, 6, 7, 9, 10, 11, 13,
|
||||
14, 15, 17, 18, 19, 21, 22, 23, 24, 25, 26,
|
||||
@@ -167,6 +169,20 @@ static void cutest_fls(CuTest *tc)
|
||||
}
|
||||
}
|
||||
|
||||
static void cutest_bswap(CuTest *tc)
|
||||
{
|
||||
for (uint i = 0; i < ARRAY_SIZE(test32_1); ++i) {
|
||||
int res = bswap32(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].bswap, res);
|
||||
}
|
||||
for (uint i = 0; i < ARRAY_SIZE(test64_1); ++i) {
|
||||
int res = bswap64(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].bswap, res);
|
||||
}
|
||||
}
|
||||
|
||||
static void cutest_ilog(CuTest *tc)
|
||||
{
|
||||
for (uint i = 0; i < ARRAY_SIZE(test32_1); ++i) {
|
||||
@@ -322,6 +338,7 @@ static CuSuite *bitops_GetSuite()
|
||||
SUITE_ADD_TEST(suite, cutest_fls);
|
||||
SUITE_ADD_TEST(suite, cutest_ilog);
|
||||
SUITE_ADD_TEST(suite, cutest_bfe);
|
||||
SUITE_ADD_TEST(suite, cutest_bswap);
|
||||
|
||||
SUITE_ADD_TEST(suite, cutest_rol);
|
||||
SUITE_ADD_TEST(suite, cutest_ror);
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "br.h"
|
||||
#include "brlib.h"
|
||||
#include "pool.h"
|
||||
#include "debug.h"
|
||||
|
||||
|
Reference in New Issue
Block a user