bitvec_read_field(): optimize by expanding bytenum_from_bitnum()

The bitvec_read_field() is used in performance critical places,
such as the CSN.1 decoder in osmo-pcu.  Thus the less conditional
statements we have in the parsing loop, the better.

The bitvec_get_bit_pos() alone is quite a complex function, which
does check the boundaries and even supports the L/H syntax.  Even
if it gets inlined by the compiler, we don't really want to run
redundant checks and run bitval2mask() on each iteration.

Change-Id: I438fc82d33ab2edbabd4215ec7bc46afb07d50ab
This commit is contained in:
Vadim Yanitskiy 2021-11-17 06:21:28 +03:00 committed by fixeria
parent 8a55a6c571
commit 49b6040048
1 changed files with 4 additions and 2 deletions

View File

@ -491,8 +491,10 @@ uint64_t bitvec_read_field(struct bitvec *bv, unsigned int *read_index, unsigned
errno = 0;
for (i = 0; i < len; i++) {
int bit = bitvec_get_bit_pos((const struct bitvec *)bv, bv->cur_bit);
if (bit)
unsigned int bytenum = bytenum_from_bitnum(bv->cur_bit);
unsigned int bitnum = 7 - (bv->cur_bit % 8);
if (bv->data[bytenum] & (1 << bitnum))
ui |= ((uint64_t)1 << (len - i - 1));
bv->cur_bit++;
}