Skip to content

Commit

Permalink
fixup! Clean up the erts_bs_get_integer_2() function in erl_bits.c
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorng committed Oct 27, 2024
1 parent b0cbc70 commit 718b302
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions erts/emulator/beam/erl_bits.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ erts_bs_get_integer_2(Process *p, Uint num_bits,
Uint v32;
int sgn = 0;
Eterm res = THE_NON_VALUE;
Uint byte_offset;
const byte* bp;

if (num_bits == 0) {
return SMALL_ZERO;
Expand All @@ -212,6 +214,9 @@ erts_bs_get_integer_2(Process *p, Uint num_bits,
return THE_NON_VALUE;
}

byte_offset = BYTE_OFFSET(sb->start);
bp = erl_sub_bits_get_base(sb) + byte_offset;

/*
* Special cases for field sizes up to the size of Uint.
*/
Expand All @@ -221,11 +226,10 @@ erts_bs_get_integer_2(Process *p, Uint num_bits,
* All bits are in one byte in the binary. We only need
* shift them right and mask them.
*/
Uint b = *(erl_sub_bits_get_base(sb) + BYTE_OFFSET(sb->start));
Uint mask = MAKE_MASK(num_bits);
Uint b = bp[0];
sb->start += num_bits;
b >>= 8 - offs - num_bits;
b &= mask;
b &= MAKE_MASK(num_bits);
MAYBE_SIGN_EXTEND(flags, b, num_bits);
return make_small(b);
} else if (num_bits <= 8) {
Expand All @@ -234,8 +238,6 @@ erts_bs_get_integer_2(Process *p, Uint num_bits,
* combine the bytes to a word first, and then shift right and
* mask to extract the bits.
*/
Uint byte_offset = BYTE_OFFSET(sb->start);
const byte* bp = erl_sub_bits_get_base(sb) + byte_offset;
Uint w = bp[0] << 8 | bp[1];
Uint mask = MAKE_MASK(num_bits);
sb->start += num_bits;
Expand All @@ -248,7 +250,6 @@ erts_bs_get_integer_2(Process *p, Uint num_bits,
* Handle field sizes from 9 up to SMALL_BITS-1 bits, big-endian,
* stored in at least two bytes.
*/
const byte* bp = erl_sub_bits_get_base(sb) + BYTE_OFFSET(sb->start);
Uint n;
Uint w;

Expand Down

0 comments on commit 718b302

Please sign in to comment.