From: Ben Pfaff Date: Mon, 2 Dec 2019 03:29:31 +0000 (+0000) Subject: bit-vector: Add new functions for working with bit vectors. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=31a3c6ef4eace636410353d32c41b2b3c6643fa5;p=pspp bit-vector: Add new functions for working with bit vectors. These will receive their first users in an upcoming commit. --- diff --git a/src/language/dictionary/modify-variables.c b/src/language/dictionary/modify-variables.c index 3dbbc18a02..a1fe6d75df 100644 --- a/src/language/dictionary/modify-variables.c +++ b/src/language/dictionary/modify-variables.c @@ -26,7 +26,6 @@ #include "language/lexer/variable-parser.h" #include "libpspp/array.h" #include "libpspp/assertion.h" -#include "libpspp/bit-vector.h" #include "libpspp/compiler.h" #include "libpspp/i18n.h" #include "libpspp/message.h" diff --git a/src/libpspp/automake.mk b/src/libpspp/automake.mk index ddff61f8fc..0cde825f5b 100644 --- a/src/libpspp/automake.mk +++ b/src/libpspp/automake.mk @@ -27,6 +27,7 @@ src_libpspp_liblibpspp_la_SOURCES = \ src/libpspp/array.c \ src/libpspp/array.h \ src/libpspp/assertion.h \ + src/libpspp/bit-vector.c \ src/libpspp/bit-vector.h \ src/libpspp/bt.c \ src/libpspp/bt.h \ diff --git a/src/libpspp/bit-vector.c b/src/libpspp/bit-vector.c new file mode 100644 index 0000000000..2b98291523 --- /dev/null +++ b/src/libpspp/bit-vector.c @@ -0,0 +1,40 @@ +/* PSPP - a program for statistical analysis. + Copyright (C) 2019 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +#include "libpspp/bit-vector.h" + +#include "libpspp/misc.h" + +#include "gl/xalloc.h" + +unsigned long int * +bitvector_allocate(size_t n) +{ + return xcalloc (sizeof (unsigned long int), DIV_RND_UP (n, BITS_PER_ULONG)); +} + +size_t +bitvector_count (const unsigned long int *vec, size_t n) +{ + /* XXX This can be optimized. */ + size_t count = 0; + for (size_t i = 0; i < n; i++) + count += bitvector_is_set (vec, i); + return count; +} + diff --git a/src/libpspp/bit-vector.h b/src/libpspp/bit-vector.h index f994d4af47..53db166998 100644 --- a/src/libpspp/bit-vector.h +++ b/src/libpspp/bit-vector.h @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 1997-9, 2000 Free Software Foundation, Inc. + Copyright (C) 1997-9, 2000, 2019 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -14,26 +14,53 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#if !bitvector_h -#define bitvector_h 1 +#ifndef BITVECTOR_H +#define BITVECTOR_H 1 #include +#include +#include -/* Sets bit Y starting at address X. */ -#define SET_BIT(X, Y) \ - (((unsigned char *) X)[(Y) / CHAR_BIT] |= 1 << ((Y) % CHAR_BIT)) +enum { BITS_PER_ULONG = CHAR_BIT * sizeof (unsigned long int) }; -/* Clears bit Y starting at address X. */ -#define CLEAR_BIT(X, Y) \ - (((unsigned char *) X)[(Y) / CHAR_BIT] &= ~(1 << ((Y) % CHAR_BIT))) +unsigned long int *bitvector_allocate(size_t n); +size_t bitvector_count (const unsigned long int *, size_t); -/* Sets bit Y starting at address X to Z, which is zero/nonzero */ -#define SET_BIT_TO(X, Y, Z) \ - ((Z) ? SET_BIT(X, Y) : CLEAR_BIT(X, Y)) +static unsigned long int +bitvector_mask (size_t idx) +{ + return 1UL << (idx % BITS_PER_ULONG); +} -/* Nonzero if bit Y starting at address X is set. */ -#define TEST_BIT(X, Y) \ - (((unsigned char *) X)[(Y) / CHAR_BIT] & (1 << ((Y) % CHAR_BIT))) +static const unsigned long int * +bitvector_unit (const unsigned long int *vec, size_t idx) +{ + return &vec[idx / BITS_PER_ULONG]; +} + +static unsigned long int * +bitvector_unit_rw (unsigned long int *vec, size_t idx) +{ + return &vec[idx / BITS_PER_ULONG]; +} + +static inline void +bitvector_set1 (unsigned long int *vec, size_t idx) +{ + *bitvector_unit_rw (vec, idx) |= bitvector_mask (idx); +} + +static inline void +bitvector_set0 (unsigned long int *vec, size_t idx) +{ + *bitvector_unit_rw (vec, idx) &= ~bitvector_mask (idx); +} + +static inline bool +bitvector_is_set (const unsigned long int *vec, size_t idx) +{ + return (*bitvector_unit (vec, idx) & bitvector_mask (idx)) != 0; +} /* Returns 2**X, 0 <= X < 32. */ #define BIT_INDEX(X) (1ul << (X)) diff --git a/src/libpspp/model-checker.c b/src/libpspp/model-checker.c index 02ae9e733b..c15dac9bba 100644 --- a/src/libpspp/model-checker.c +++ b/src/libpspp/model-checker.c @@ -1150,7 +1150,7 @@ struct mc /* Array of 2**(options->hash_bits) bits representing states already visited. */ - unsigned char *hash; + unsigned long int *hash; /* State queue. */ struct mc_state **queue; /* Array of pointers to states. */ @@ -1269,7 +1269,7 @@ mc_discard_dup_state (struct mc *mc, unsigned int hash) if (!mc->state_error && mc->options->hash_bits > 0) { hash &= (1u << mc->options->hash_bits) - 1; - if (TEST_BIT (mc->hash, hash)) + if (bitvector_is_set (mc->hash, hash)) { if (mc->options->verbosity > 2) fprintf (mc->options->output_file, @@ -1278,7 +1278,7 @@ mc_discard_dup_state (struct mc *mc, unsigned int hash) next_operation (mc); return true; } - SET_BIT (mc->hash, hash); + bitvector_set1 (mc->hash, hash); } return false; } @@ -1688,7 +1688,7 @@ init_mc (struct mc *mc, const struct mc_class *class, mc->results = mc_results_create (); mc->hash = (mc->options->hash_bits > 0 - ? xcalloc (1, DIV_RND_UP (1 << mc->options->hash_bits, CHAR_BIT)) + ? bitvector_allocate (1 << mc->options->hash_bits) : NULL); mc->queue = NULL;