Skip to content

Commit

Permalink
Auto merge of #1342 - psumbera:master, r=emilio
Browse files Browse the repository at this point in the history
Fix bitfields on big-endian machines.

Fixes #1340
  • Loading branch information
bors-servo authored Jul 1, 2018
2 parents 0176a76 + a642894 commit c3955dd
Show file tree
Hide file tree
Showing 29 changed files with 754 additions and 144 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);
}
}
}
30 changes: 26 additions & 4 deletions tests/expectations/tests/bitfield-32bit-overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 @@ -41,7 +46,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 @@ -61,7 +71,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 @@ -77,7 +93,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);
}
}
}
Expand Down
32 changes: 26 additions & 6 deletions tests/expectations/tests/bitfield-large.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* automatically generated by rust-bindgen */


#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]


#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct __BindgenBitfieldUnit<Storage, Align>
Expand All @@ -30,7 +28,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 @@ -43,7 +46,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 @@ -63,7 +71,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 @@ -79,7 +93,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);
}
}
}
Expand Down
32 changes: 26 additions & 6 deletions tests/expectations/tests/bitfield-method-same-name.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* automatically generated by rust-bindgen */


#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]


#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct __BindgenBitfieldUnit<Storage, Align>
Expand All @@ -30,7 +28,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 @@ -43,7 +46,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 @@ -63,7 +71,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 @@ -79,7 +93,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);
}
}
}
Expand Down
30 changes: 26 additions & 4 deletions tests/expectations/tests/bitfield_align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 @@ -41,7 +46,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 @@ -61,7 +71,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 @@ -77,7 +93,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);
}
}
}
Expand Down
30 changes: 26 additions & 4 deletions tests/expectations/tests/bitfield_align_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 @@ -41,7 +46,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 @@ -61,7 +71,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 @@ -77,7 +93,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);
}
}
}
Expand Down
Loading

0 comments on commit c3955dd

Please sign in to comment.