Skip to content

Commit

Permalink
Fix bitfields on big-endian machines.
Browse files Browse the repository at this point in the history
  • Loading branch information
psumbera committed Jun 28, 2018
1 parent 0176a76 commit 9bd4fb4
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/codegen/bitfield_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ where
let byte_index = index / 8;
let byte = self.storage.as_ref()[byte_index];

let bit_index = index % 8;
let mut bit_index = index % 8;
#[cfg(target_endian = "big")]
{
// Adjust the index for endianness.
bit_index = 7 - bit_index;
}
let mask = 1 << bit_index;

byte & mask == mask
Expand All @@ -40,7 +45,12 @@ where
let byte_index = index / 8;
let byte = &mut self.storage.as_mut()[byte_index];

let bit_index = index % 8;
let mut bit_index = index % 8;
#[cfg(target_endian = "big")]
{
// Adjust the index for endianness.
bit_index = 7 - bit_index;
}
let mask = 1 << bit_index;

if val {
Expand All @@ -60,7 +70,13 @@ where

for i in 0..(bit_width as usize) {
if self.get_bit(i + bit_offset) {
val |= 1 << i;
let mut index = i;
#[cfg(target_endian = "big")]
{
// Adjust the index for endianness.
index = bit_width as usize - 1 - index;
}
val |= 1 << index;
}
}

Expand All @@ -76,7 +92,13 @@ where
for i in 0..(bit_width as usize) {
let mask = 1 << i;
let val_bit_is_set = val & mask == mask;
self.set_bit(i + bit_offset, val_bit_is_set);
let mut index = i;
#[cfg(target_endian = "big")]
{
// Adjust the index for endianness.
index = bit_width as usize - 1 - index;
}
self.set_bit(index + bit_offset, val_bit_is_set);
}
}
}

0 comments on commit 9bd4fb4

Please sign in to comment.