summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2024-06-24 22:34:51 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2024-06-24 22:34:51 (GMT)
commit00452431d4fb061f20a4eaa4c93b61014f7651ef (patch)
tree750800b307afb0475115a27bd0a20a672940e2b7 /src
parent1e30e2c3822f55848e2306e59a2e66d7285f6b78 (diff)
Apply cosmetics with macros for bit fields.
Diffstat (limited to 'src')
-rw-r--r--src/common/bits.c99
1 files changed, 49 insertions, 50 deletions
diff --git a/src/common/bits.c b/src/common/bits.c
index 91872e1..f730c66 100644
--- a/src/common/bits.c
+++ b/src/common/bits.c
@@ -50,7 +50,8 @@ struct _bitfield_t
/* Taille des mots intégrés */
-#define BF_WORD_SIZE (sizeof(unsigned long) * 8)
+#define BF_WORD_BIT_SIZE (sizeof(unsigned long) * 8)
+#define BF_WORD_BYTE_SIZE sizeof(unsigned long)
/* Crée un champ de bits initialisé à zéro. */
@@ -82,10 +83,10 @@ static bitfield_t *_create_bit_field(size_t length)
size_t needed; /* Nombre de mots à allouer */
size_t base; /* Allocation de base en octets*/
- needed = length / (sizeof(unsigned long) * 8);
- if (length % (sizeof(unsigned long) * 8) != 0) needed++;
+ needed = length / BF_WORD_BIT_SIZE;
+ if (length % BF_WORD_BIT_SIZE != 0) needed++;
- base = sizeof(bitfield_t) + needed * sizeof(unsigned long);
+ base = sizeof(bitfield_t) + needed * BF_WORD_BYTE_SIZE;
result = malloc(base);
@@ -148,7 +149,7 @@ bitfield_t *dup_bit_field(const bitfield_t *field)
result = _create_bit_field(field->length);
- memcpy(result->bits, field->bits, result->used_words * sizeof(unsigned long));
+ memcpy(result->bits, field->bits, result->used_words * BF_WORD_BYTE_SIZE);
return result;
@@ -191,7 +192,7 @@ void copy_bit_field(bitfield_t *dest, const bitfield_t *src)
{
assert(dest->length == src->length);
- memcpy(dest->bits, src->bits, dest->used_words * sizeof(unsigned long));
+ memcpy(dest->bits, src->bits, dest->used_words * BF_WORD_BYTE_SIZE);
}
@@ -217,8 +218,8 @@ void truncate_bit_field(bitfield_t **field, size_t length)
_field = *field;
- needed = length / (sizeof(unsigned long) * 8);
- if (length % (sizeof(unsigned long) * 8) != 0) needed++;
+ needed = length / BF_WORD_BIT_SIZE;
+ if (length % BF_WORD_BIT_SIZE != 0) needed++;
if (needed <= _field->allocated_words)
{
@@ -263,10 +264,10 @@ void resize_bit_field(bitfield_t **field, size_t length)
{
/* Redimensionnement */
- needed = length / (sizeof(unsigned long) * 8);
- if (length % (sizeof(unsigned long) * 8) != 0) needed++;
+ needed = length / BF_WORD_BIT_SIZE;
+ if (length % BF_WORD_BIT_SIZE != 0) needed++;
- base = sizeof(bitfield_t) + needed * sizeof(unsigned long);
+ base = sizeof(bitfield_t) + needed * BF_WORD_BYTE_SIZE;
/* Initialisation, si nécessaire */
@@ -275,8 +276,8 @@ void resize_bit_field(bitfield_t **field, size_t length)
*field = realloc(_field, base);
_field = *field;
- last = _field->length / (sizeof(unsigned long) * 8);
- remaining = _field->length % (sizeof(unsigned long) * 8);
+ last = _field->length / BF_WORD_BIT_SIZE;
+ remaining = _field->length % BF_WORD_BIT_SIZE;
if (remaining != 0)
{
@@ -367,7 +368,7 @@ int compare_bit_fields(const bitfield_t *a, const bitfield_t *b)
else
{
- final = a->length % (8 * sizeof(unsigned long));
+ final = a->length % BF_WORD_BIT_SIZE;
if (final == 0)
final = ~0lu;
@@ -414,7 +415,7 @@ int compare_bit_fields(const bitfield_t *a, const bitfield_t *b)
void reset_all_in_bit_field(bitfield_t *field)
{
- memset(field->bits, 0u, field->used_words * sizeof(unsigned long));
+ memset(field->bits, 0u, field->used_words * BF_WORD_BYTE_SIZE);
}
@@ -433,7 +434,7 @@ void reset_all_in_bit_field(bitfield_t *field)
void set_all_in_bit_field(bitfield_t *field)
{
- memset(field->bits, ~0u, field->used_words * sizeof(unsigned long));
+ memset(field->bits, ~0u, field->used_words * BF_WORD_BYTE_SIZE);
}
@@ -458,8 +459,8 @@ void reset_in_bit_field(bitfield_t *field, size_t first)
assert(first < field->length);
- index = first / (sizeof(unsigned long) * 8);
- remaining = first % (sizeof(unsigned long) * 8);
+ index = first / BF_WORD_BIT_SIZE;
+ remaining = first % BF_WORD_BIT_SIZE;
field->bits[index] &= ~(1ul << remaining);
@@ -493,8 +494,8 @@ void reset_multi_in_bit_field(bitfield_t *field, size_t first, size_t count)
for (i = first; i < last; i++)
{
- index = i / (sizeof(unsigned long) * 8);
- remaining = i % (sizeof(unsigned long) * 8);
+ index = i / BF_WORD_BIT_SIZE;
+ remaining = i % BF_WORD_BIT_SIZE;
field->bits[index] &= ~(1ul << remaining);
@@ -523,8 +524,8 @@ void set_in_bit_field(bitfield_t *field, size_t first)
assert(first < field->length);
- index = first / (sizeof(unsigned long) * 8);
- remaining = first % (sizeof(unsigned long) * 8);
+ index = first / BF_WORD_BIT_SIZE;
+ remaining = first % BF_WORD_BIT_SIZE;
field->bits[index] |= (1ul << remaining);
@@ -558,8 +559,8 @@ void set_multi_in_bit_field(bitfield_t *field, size_t first, size_t count)
for (i = first; i < last; i++)
{
- index = i / (sizeof(unsigned long) * 8);
- remaining = i % (sizeof(unsigned long) * 8);
+ index = i / BF_WORD_BIT_SIZE;
+ remaining = i % BF_WORD_BIT_SIZE;
field->bits[index] |= (1ul << remaining);
@@ -643,12 +644,12 @@ void or_bit_field_at(bitfield_t *dest, const bitfield_t *src, size_t first)
assert((first + src->length) <= dest->length);
- start = first / (sizeof(unsigned long) * 8);
- offset = first % (sizeof(unsigned long) * 8);
+ start = first / BF_WORD_BIT_SIZE;
+ offset = first % BF_WORD_BIT_SIZE;
- remaining = (first + src->length) % (sizeof(unsigned long) * 8);
+ remaining = (first + src->length) % BF_WORD_BIT_SIZE;
- if ((first + src->length) % (sizeof(unsigned long) * 8) > 0)
+ if ((first + src->length) % BF_WORD_BIT_SIZE > 0)
last_iter = src->used_words;
else
last_iter = src->used_words - 1;
@@ -661,7 +662,7 @@ void or_bit_field_at(bitfield_t *dest, const bitfield_t *src, size_t first)
word = 0;
if (i > 0 && offset > 0)
- word |= src->bits[i - 1] >> (sizeof(unsigned long) * 8 - offset);
+ word |= src->bits[i - 1] >> (BF_WORD_BIT_SIZE - offset);
if (i == last_iter && remaining > 0)
word &= (1ul << remaining) - 1;
@@ -694,8 +695,8 @@ bool test_in_bit_field(const bitfield_t *field, size_t n)
assert(n < field->length);
- index = n / (sizeof(unsigned long) * 8);
- remaining = n % (sizeof(unsigned long) * 8);
+ index = n / BF_WORD_BIT_SIZE;
+ remaining = n % BF_WORD_BIT_SIZE;
result = field->bits[index] & (1ul << remaining);
@@ -726,8 +727,8 @@ bool test_and_set_in_bit_field(bitfield_t *field, size_t n)
assert(n < field->length);
- index = n / (sizeof(unsigned long) * 8);
- remaining = n % (sizeof(unsigned long) * 8);
+ index = n / BF_WORD_BIT_SIZE;
+ remaining = n % BF_WORD_BIT_SIZE;
bits = field->bits + index;
@@ -769,8 +770,8 @@ static bool test_state_in_bit_field(const bitfield_t *field, size_t first, size_
for (i = first; i < last; i++)
{
- index = i / (sizeof(unsigned long) * 8);
- remaining = i % (sizeof(unsigned long) * 8);
+ index = i / BF_WORD_BIT_SIZE;
+ remaining = i % BF_WORD_BIT_SIZE;
current = field->bits[index] & (1ul << remaining);
@@ -881,10 +882,10 @@ static bool test_state_within_bit_field(const bitfield_t *field, size_t first, c
else
result = true;
- start = first / (sizeof(unsigned long) * 8);
- offset = first % (sizeof(unsigned long) * 8);
+ start = first / BF_WORD_BIT_SIZE;
+ offset = first % BF_WORD_BIT_SIZE;
- remaining = mask->length % (sizeof(unsigned long) * 8);
+ remaining = mask->length % BF_WORD_BIT_SIZE;
if (remaining == 0)
finalcut = ~0lu;
@@ -902,7 +903,7 @@ static bool test_state_within_bit_field(const bitfield_t *field, size_t first, c
{
word = field->bits[windex] >> offset;
if ((windex + 1) < field->used_words)
- word |= field->bits[windex + 1] << (sizeof(unsigned long) * 8 - offset);
+ word |= field->bits[windex + 1] << (BF_WORD_BIT_SIZE - offset);
}
bitmask = mask->bits[i];
@@ -1008,7 +1009,7 @@ size_t find_next_set_in_bit_field(const bitfield_t *field, const size_t *prev)
}
else
{
- i = *prev / BF_WORD_SIZE;
+ i = *prev / BF_WORD_BIT_SIZE;
if (i >= field->used_words)
{
@@ -1018,9 +1019,9 @@ size_t find_next_set_in_bit_field(const bitfield_t *field, const size_t *prev)
word = field->bits[i];
- last_pos = *prev % BF_WORD_SIZE;
+ last_pos = *prev % BF_WORD_BIT_SIZE;
- if ((last_pos + 1) == BF_WORD_SIZE)
+ if ((last_pos + 1) == BF_WORD_BIT_SIZE)
goto next_word;
word &= ~((1lu << (last_pos + 1)) - 1);
@@ -1031,7 +1032,7 @@ size_t find_next_set_in_bit_field(const bitfield_t *field, const size_t *prev)
if (found > 0)
{
- result = i * BF_WORD_SIZE + found - 1;
+ result = i * BF_WORD_BIT_SIZE + found - 1;
goto done;
}
@@ -1047,7 +1048,7 @@ size_t find_next_set_in_bit_field(const bitfield_t *field, const size_t *prev)
if (found > 0)
{
- result = i * BF_WORD_SIZE + found - 1;
+ result = i * BF_WORD_BIT_SIZE + found - 1;
/**
* Validation des bornes finales, pour le dernier mot.
@@ -1222,7 +1223,7 @@ size_t popcount_for_bit_field(const bitfield_t *field)
{
value = field->bits[i];
- if (remaining < (8 * sizeof(unsigned long)))
+ if (remaining < BF_WORD_BIT_SIZE)
value &= (1lu << remaining) - 1;
#if __WORDSIZE == 64
@@ -1233,7 +1234,7 @@ size_t popcount_for_bit_field(const bitfield_t *field)
# error "Unkown word size"
#endif
- remaining -= 8 * sizeof(unsigned long);
+ remaining -= BF_WORD_BIT_SIZE;
}
@@ -1264,8 +1265,6 @@ void output_bit_field(const bitfield_t *field)
printf("[len=%zu] \n", field->length);
-#define MAX_BITS (sizeof(unsigned long) * 8)
-
for (i = 0; i < field->used_words; i++)
{
value = field->bits[i];
@@ -1273,9 +1272,9 @@ void output_bit_field(const bitfield_t *field)
if (i > 0)
printf("| ");
- for (k = 0; k < MAX_BITS; k++)
+ for (k = 0; k < BF_WORD_BIT_SIZE; k++)
{
- if ((i * MAX_BITS + k) >= field->length)
+ if ((i * BF_WORD_BIT_SIZE + k) >= field->length)
break;
printf("%c", value & (1lu << k) ? '1' : '.');