Skip to content

Commit

Permalink
Adds esl_bitfield_Count(); ESL_BITFIELD is now a struct, not just uin…
Browse files Browse the repository at this point in the history
…t64_t *.
  • Loading branch information
cryptogenomicon committed Apr 18, 2019
1 parent 33b65ee commit 7a79899
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
34 changes: 30 additions & 4 deletions esl_bitfield.c
Expand Up @@ -33,8 +33,12 @@ esl_bitfield_Create(int nb)

ESL_DASSERT1(( nb >= 1 ));

ESL_ALLOC(b, sizeof(uint64_t) * nu);
memset((void *) b, 0, sizeof(uint64_t) * nu);
ESL_ALLOC(b, sizeof(ESL_BITFIELD));
b->b = NULL;

ESL_ALLOC(b->b, sizeof(uint64_t) * nu);
memset((void *) b->b, 0, sizeof(uint64_t) * nu);
b->nb = nb;
return b;

ERROR:
Expand All @@ -43,13 +47,29 @@ esl_bitfield_Create(int nb)
}


/* Function: esl_bitfield_Count()
* Synopsis: Return the number of bits that are set.
* Incept: SRE, Thu 18 Apr 2019
*/
int
esl_bitfield_Count(const ESL_BITFIELD *b)
{
int n = 0;
int i;

for (i = 0; i < b->nb; i++)
if (esl_bitfield_IsSet(b, i)) n++;
return n;
}


/* Function: esl_bitfield_Destroy()
* Synopsis: Frees an <ESL_BITFIELD>
*/
void
esl_bitfield_Destroy(ESL_BITFIELD *b)
{
free(b);
if (b) free(b->b);
}


Expand Down Expand Up @@ -82,12 +102,18 @@ utest_randpattern(ESL_RANDOMNESS *rng)
for (i = 0; i < nset; i++) bigflags[deal[i]] = TRUE;
for (i = 0; i < nset; i++) esl_bitfield_Set(b, deal[i]);

if (esl_bitfield_Count(b) != nset) esl_fatal(msg);

for (i = 0; i < nb; i++) if (bigflags[i] != esl_bitfield_IsSet(b, i)) esl_fatal(msg);
for (i = 0; i < nb; i++) esl_bitfield_Toggle(b, i);

if (esl_bitfield_Count(b) != nb - nset) esl_fatal(msg);

for (i = 0; i < nb; i++) if (bigflags[i] == esl_bitfield_IsSet(b, i)) esl_fatal(msg);
for (i = 0; i < nb; i++) esl_bitfield_Clear(b, i); // do all bits, to test that clearing 0 bits leaves them 0

for (i = 0; i < nu; i++) if (b[i]) esl_fatal(msg); // note <nu>. this reaches inside the "opaque" ESL_BITFIELD and can break if that structure changes.
if (esl_bitfield_Count(b) != 0) esl_fatal(msg);
for (i = 0; i < nu; i++) if (b->b[i]) esl_fatal(msg); // note <nu>. this reaches inside the "opaque" ESL_BITFIELD and can break if that structure changes.

free(deal);
free(bigflags);
Expand Down
18 changes: 11 additions & 7 deletions esl_bitfield.h
Expand Up @@ -4,34 +4,38 @@

#include "easel.h"

typedef uint64_t ESL_BITFIELD;
typedef struct {
uint64_t *b; // packed storage of flags
int nb; // number of flags
} ESL_BITFIELD;

static inline void
esl_bitfield_Set(ESL_BITFIELD *b, int i)
{
b[i/64] |= (1ull << (i%64));
b->b[i/64] |= (1ull << (i%64));
}

static inline void
esl_bitfield_Clear(ESL_BITFIELD *b, int i)
{
b[i/64] &= ~(1ull << (i%64));
b->b[i/64] &= ~(1ull << (i%64));
}

static inline void
esl_bitfield_Toggle(ESL_BITFIELD *b, int i)
{
b[i/64] ^= (1ull << (i%64));
b->b[i/64] ^= (1ull << (i%64));
}

static inline int
esl_bitfield_IsSet(ESL_BITFIELD *b, int i)
esl_bitfield_IsSet(const ESL_BITFIELD *b, int i)
{
return ((b[i/64] & (1ull << (i%64))) ? TRUE : FALSE);
return ((b->b[i/64] & (1ull << (i%64))) ? TRUE : FALSE);
}


extern ESL_BITFIELD *esl_bitfield_Create(int nb);
extern ESL_BITFIELD *esl_bitfield_Create (int nb);
extern int esl_bitfield_Count (const ESL_BITFIELD *b);
extern void esl_bitfield_Destroy(ESL_BITFIELD *b);

#endif //eslBITFIELD_INCLUDED
Expand Down
2 changes: 1 addition & 1 deletion miniapps/cmd_filter.c
Expand Up @@ -121,7 +121,7 @@ esl_cmd_filter(const char *topcmd, const ESL_SUBCMD *sub, int argc, char **argv)
/* The filter miniapp has a multipart help page.
* This is a copy of esl_subcmd_CreateDefaultApp() with its help output customized.
*/
ESL_GETOPTS *
static ESL_GETOPTS *
process_cmdline(const char *topcmd, const ESL_SUBCMD *sub, const ESL_OPTIONS *suboptions, int argc, char **argv)
{
ESL_GETOPTS *go = esl_getopts_Create(suboptions);
Expand Down

0 comments on commit 7a79899

Please sign in to comment.