-
Notifications
You must be signed in to change notification settings - Fork 707
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #1158 - glyn:large-bitfield-units, r=emilio
Support bitfield allocation units larger than 64 bits Individual bitfields are still limited to at most 64 bits, but this restriction can be weakened when Rust supports `u128`. This implements issue #816. Usage notes: * Since common code is added to each generated binding, a program which uses more than one binding may need to work around the duplication by including each binding in its own module. * The values created by bitfield allocation unit constructors can be assigned directly to the corresponding struct fields with no need for transmutation. Implementation notes: `__BindgenBitfieldUnit` represents a bitfield allocation unit using a `Storage` type accessible as a slice of `u8`. The alignment of the unit is inherited from an `Align` type by virtue of the field: ``` align: [Align; 0], ``` The position of this field in the struct is irrelevant. It is assumed that the alignment of the `Storage` type is no larger than the alignment of the `Align` type, which will be true if the `Storage` type is, for example, an array of `u8`. This assumption is checked in a debug assertion. Although the double underscore (__) prefix is reserved for implementations of C++, there are precedents for this convention elsewhere in bindgen and so the convention is adopted here too. Acknowledgement: Thanks to @fitzgen for an initial implementation of `__BindgenBitfieldUnit` and code to integrate it into bindgen. r? @emilio
- Loading branch information
Showing
36 changed files
with
4,147 additions
and
4,629 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#[repr(C)] | ||
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] | ||
pub struct __BindgenBitfieldUnit<Storage, Align> | ||
where | ||
Storage: AsRef<[u8]> + AsMut<[u8]>, | ||
{ | ||
storage: Storage, | ||
align: [Align; 0], | ||
} | ||
|
||
impl<Storage, Align> __BindgenBitfieldUnit<Storage, Align> | ||
where | ||
Storage: AsRef<[u8]> + AsMut<[u8]>, | ||
{ | ||
#[inline] | ||
pub fn new(storage: Storage) -> Self { | ||
Self { | ||
storage, | ||
align: [], | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn get_bit(&self, index: usize) -> bool { | ||
debug_assert!(index / 8 < self.storage.as_ref().len()); | ||
|
||
let byte_index = index / 8; | ||
let byte = self.storage.as_ref()[byte_index]; | ||
|
||
let bit_index = index % 8; | ||
let mask = 1 << bit_index; | ||
|
||
byte & mask == mask | ||
} | ||
|
||
#[inline] | ||
pub fn set_bit(&mut self, index: usize, val: bool) { | ||
debug_assert!(index / 8 < self.storage.as_ref().len()); | ||
|
||
let byte_index = index / 8; | ||
let byte = &mut self.storage.as_mut()[byte_index]; | ||
|
||
let bit_index = index % 8; | ||
let mask = 1 << bit_index; | ||
|
||
if val { | ||
*byte |= mask; | ||
} else { | ||
*byte &= !mask; | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { | ||
debug_assert!(bit_width <= 64); | ||
debug_assert!(bit_offset / 8 < self.storage.as_ref().len()); | ||
debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len()); | ||
|
||
let mut val = 0; | ||
|
||
for i in 0..(bit_width as usize) { | ||
if self.get_bit(i + bit_offset) { | ||
val |= 1 << i; | ||
} | ||
} | ||
|
||
val | ||
} | ||
|
||
#[inline] | ||
pub fn set(&mut self, bit_offset: usize, bit_width: u8, val: u64) { | ||
debug_assert!(bit_width <= 64); | ||
debug_assert!(bit_offset / 8 < self.storage.as_ref().len()); | ||
debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len()); | ||
|
||
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.