Compare commits

...

4 Commits

Author SHA1 Message Date
32fd923192 move cutest_bfe data to first set 2024-01-13 10:59:24 +01:00
a8a82a3121 Makefile: fix bin generation 2024-01-13 10:58:08 +01:00
5e88bad291 clearer install instructions 2024-01-13 10:57:22 +01:00
f29821ee94 add generic-ilog2.h (for reference) 2024-01-13 10:56:48 +01:00
5 changed files with 173 additions and 93 deletions

View File

@@ -212,7 +212,7 @@ cleanbin:
cleanbindir:
$(call rmdir,$(BINDIR),binaries)
$(BINDIR)/%: $(TESTDIR)/%.c libs | $(BINDIR)
$(BINDIR)/%: $(TESTDIR)/%.c $(SLIB) $(DLIB) | $(BINDIR)
$(CC) $(CPPFLAGS) $(CFLAGS) $< $(CUTESTSRC) $(LDFLAGS) $(LIBS) -o $@
##################################### pre-processed (.i) and assembler (.s) output

View File

@@ -20,16 +20,16 @@ See [[test/cutest/license.txt][license local copy]] or
<https://spdx.org/licenses/Zlib.html>.
** Installation:
*** user...
*** Clone repository
#+BEGIN_EXAMPLE
$ git clone https://git.raoult.com:bruno/brlib.git
or
$ git clone https://github.com/braoult/brlib.git
or
$ git clone https://git.raoult.com:bruno/brlib.git
#+END_EXAMPLE
*** ...or developer
*** Add CuTest submodule (optional)
#+BEGIN_EXAMPLE
$ git clone git@git.raoult.com:bruno/brlib.git
$ cd brlib
$ git remote add github git@github.com:braoult/brlib.git
$ git submodule init
$ git submodule update
#+END_EXAMPLE

View File

@@ -0,0 +1,60 @@
/* generic-ilog2.h - generic ilog2 implementations.
*
* Copyright (C) 2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING.
*
* You should have received a copy of the GNU General Public License along with this
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef _GENERIC_ILOG2_H_
#define _GENERIC_ILOG2_H_
#include "br.h"
/*
* See https://stackoverflow.com/a/11398748/3079831
*/
static const int __ilog2_32_table[32] = {
0, 9, 1, 10, 13, 21, 2, 29,
11, 14, 16, 18, 22, 25, 3, 30,
8, 12, 20, 28, 15, 17, 24, 7,
19, 27, 23, 6, 26, 5, 4, 31
};
static const int __ilog2_64_table[64] = {
63, 0, 58, 1, 59, 47, 53, 2,
60, 39, 48, 27, 54, 33, 42, 3,
61, 51, 37, 40, 49, 18, 28, 20,
55, 30, 34, 11, 43, 14, 22, 4,
62, 57, 46, 52, 38, 26, 32, 41,
50, 36, 17, 19, 29, 10, 13, 21,
56, 45, 25, 31, 35, 16, 9, 12,
44, 24, 15, 8, 23, 7, 6, 5
};
static __always_inline __const int __ilog2_32_emulated (u32 n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return __ilog2_32_table[(n*0x07C4ACDD) >> 27];
}
static __always_inline __const int __ilog2_64_emulated (u64 n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n |= n >> 32;
return __ilog2_64_table[((n - (n >> 1))*0x07EDD5E59A4E28C2LL) >> 58];
}
#endif /* _GENERIC_ILOG2_H_ */

View File

@@ -18,13 +18,15 @@
if [[ ! -v _BRLIB_ENV_ ]]; then
export _BRLIB_ENV_=1 BRLIB_ROOT LD_LIBRARY_PATH
BRLIB_SCRIPTDIR=$(realpath -L "$(dirname "${BASH_SOURCE[0]}")")
BRLIB_ROOT=$(realpath -L "$(dirname "${BASH_SOURCE[0]}")/..")
BRLIB_LIBDIR="$BRLIB_ROOT/lib"
BRLIB_SCRIPTDIR="$BRLIB_ROOT/scripts"
BRLIB_BINDIR="$BRLIB_ROOT/bin"
LD_LIBRARY_PATH="${BRLIB_LIBDIR}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
PATH="$PATH:$BRLIB_BINDIR:$BRLIB_SCRIPTDIR"
BRLIB_LIBDIR="$BRLIB_ROOT/lib"
LD_LIBRARY_PATH="${BRLIB_LIBDIR}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
#printf "R=%s L=%s LD=%s\n" "$BRLIB_ROOT" "$BRLIB_DIR" "$LD_LIBRARY_PATH"
unset BRLIB_LIBDIR BRLIB_BINDIR BRLIB_SCRIPTDIR
unset BRLIB_SCRIPTDIR BRLIB_BINDIR BRLIB_LIBDIR
printf "brlib environment complete.\n"
fi

View File

@@ -27,13 +27,27 @@ static const struct test32_1 {
int ffz;
int fls;
int ilog2;
struct {
int size;
uchar bfe[32];
};
} 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 },
{ 0x00000000, 0, 32, 32, 0, 1, 0, 0, /* sometimes undefined */
{ 0, { 0 } } },
{ 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,
{ 1, { 0 } } },
{ 0x80000000, 1, 31, 0, 32, 1, 32, 31,
{ 1, { 31 } } },
{ 0x71800718, 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,
/* 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 } } },
};
static const struct test64_1 {
@@ -45,13 +59,32 @@ static const struct test64_1 {
int ffz;
int fls;
int ilog2;
struct {
int size;
uchar bfe[64];
};
} 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 },
{ 0x0000000000000000, 0, 64, 64, 0, 1, 0, 0, /* sometimes undefined */
{ 0, { 0 } } },
{ 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,
{ 2, { 0, 32 } } },
{ 0x8000000000000000, 1, 63, 0, 64, 1, 64, 63,
{ 1, { 63 } } },
{ 0x7180071871800718, 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 ,
/* 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,
32, 33, 34, 36, 37, 38, 39, 41, 42, 43, 45,
46, 47, 49, 50, 51, 53, 54, 55, 56, 57, 58 } } },
};
static void cutest_popcount(CuTest *tc)
@@ -158,6 +191,54 @@ static void cutest_ilog(CuTest *tc)
}
}
static void cutest_bfe(CuTest *tc)
{
char s[64];
for (uint i = 0; i < ARRAY_SIZE(test32_1); ++i) {
u32 tmp;
int cur, nb = 0;
char bfe_ffs[32];
/* create bit_for_each32_ffs values */
for (int j = 0; j < test32_1[i].size; ++j)
bfe_ffs[j] = test32_1[i].bfe[j] + 1;
bit_for_each32(cur, tmp, test32_1[i].t32) {
s[nb++] = cur;
}
CuAssertIntEquals(tc, test32_1[i].size, nb);
CuAssertMemEquals(tc, test32_1[i].bfe, s, test32_1[i].size);
nb = 0;
bit_for_each32_ffs(cur, tmp, test32_1[i].t32) {
s[nb++] = cur;
}
CuAssertMemEquals(tc, bfe_ffs, s, test32_1[i].size);
}
for (uint i = 0; i < ARRAY_SIZE(test64_1); ++i) {
u64 tmp;
int cur, nb = 0;
char bfe_ffs[64];
/* create bit_for_each64_ffs values */
for (int j = 0; j < test64_1[i].size; ++j)
bfe_ffs[j] = test64_1[i].bfe[j] + 1;
bit_for_each64(cur, tmp, test64_1[i].t64) {
s[nb++] = cur;
}
//printf("\n64 nb=%d size=%d\n", nb, test64_1[i].size);
CuAssertIntEquals(tc, test64_1[i].size, nb);
CuAssertMemEquals(tc, test64_1[i].bfe, s, test64_1[i].size);
nb = 0;
bit_for_each64_ffs(cur, tmp, test64_1[i].t64) {
s[nb++] = cur;
}
CuAssertMemEquals(tc, bfe_ffs, s, test64_1[i].size);
}
}
struct test32_2 {
u32 t32; /* input */
u32 arg;
@@ -189,28 +270,13 @@ struct test64_2 {
u32 arg;
u64 rol;
u64 ror;
struct {
int size;
uchar bfe[64];
};
} test64_2[] = {
{ 0x0000000000000000, 0, 0, 0,
{ 0, { 0 } } },
{ 0x0000000000000000, 1, 0, 0,
{ 0, { 0 } } },
{ 0x1000000110000001, 2, 0x4000000440000004, 0x4400000044000000,
{ 4, { 0, 28, 32, 60 } } },
{ 0x8000000880000008, 3, 0x0000004400000044, 0x1000000110000001,
{ 4, { 3, 31, 35, 63 } } },
{ 0x7180071871800718, 8, 0x8007187180071871, 0x1871800718718007,
{ 20, { 3, 4, 8, 9, 10, 23, 24, 28, 29, 30,
35, 36, 40, 41, 42, 55, 56, 60, 61, 62 } } },
{ 0x07eeeef707eeeef7, 4, 0x7eeeef707eeeef70, 0x707eeeef707eeeef,
{ 44, { 0, 1, 2, 4, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17, 18, 19,
21, 22, 23, 24, 25, 26,
32, 33, 34, 36, 37, 38, 39, 41, 42, 43, 45, 46, 47, 49, 50, 51,
53, 54, 55, 56, 57, 58 } } },
{ 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)
@@ -245,54 +311,6 @@ static void cutest_ror(CuTest *tc)
}
}
static void cutest_bfe(CuTest *tc)
{
char s[64];
for (uint i = 0; i < ARRAY_SIZE(test32_2); ++i) {
u32 tmp;
int cur, nb = 0;
char bfe_ffs[32];
/* create bit_for_each32_ffs values */
for (int j = 0; j < test32_2[i].size; ++j)
bfe_ffs[j] = test32_2[i].bfe[j] + 1;
bit_for_each32(cur, tmp, test32_2[i].t32) {
s[nb++] = cur;
}
CuAssertIntEquals(tc, test32_2[i].size, nb);
CuAssertMemEquals(tc, test32_2[i].bfe, s, test32_2[i].size);
nb = 0;
bit_for_each32_ffs(cur, tmp, test32_2[i].t32) {
s[nb++] = cur;
}
CuAssertMemEquals(tc, bfe_ffs, s, test32_2[i].size);
}
for (uint i = 0; i < ARRAY_SIZE(test64_2); ++i) {
u64 tmp;
int cur, nb = 0;
char bfe_ffs[64];
/* create bit_for_each64_ffs values */
for (int j = 0; j < test64_2[i].size; ++j)
bfe_ffs[j] = test64_2[i].bfe[j] + 1;
bit_for_each64(cur, tmp, test64_2[i].t64) {
s[nb++] = cur;
}
//printf("\n64 nb=%d size=%d\n", nb, test64_2[i].size);
CuAssertIntEquals(tc, test64_2[i].size, nb);
CuAssertMemEquals(tc, test64_2[i].bfe, s, test64_2[i].size);
nb = 0;
bit_for_each64_ffs(cur, tmp, test64_2[i].t64) {
s[nb++] = cur;
}
CuAssertMemEquals(tc, bfe_ffs, s, test64_2[i].size);
}
}
static CuSuite *bitops_GetSuite()
{
CuSuite* suite = CuSuiteNew();
@@ -303,10 +321,10 @@ static CuSuite *bitops_GetSuite()
SUITE_ADD_TEST(suite, cutest_ffz);
SUITE_ADD_TEST(suite, cutest_fls);
SUITE_ADD_TEST(suite, cutest_ilog);
SUITE_ADD_TEST(suite, cutest_bfe);
SUITE_ADD_TEST(suite, cutest_rol);
SUITE_ADD_TEST(suite, cutest_ror);
SUITE_ADD_TEST(suite, cutest_bfe);
return suite;
}