closedcaption: Convert bitfields to uint8_t for portability reasons

Contrary to what one might believe, this actually reduces the size of
the structs due to alignment constraints. On Linux x86-64 clang/gcc it
reduces the size of the caption_frame_t struct from 7760 bytes to 6800
bytes, on Windows x86-64 MSVC from 11600 bytes to 6800 bytes.

It also causes simpler and potentially faster assembly to be generated
as the values can be directly accessed as uint8_t instead of having to
extract the corresponding bits with bitwise operations.

It also gives us the same ABI with clang/gcc and MSVC.
This commit is contained in:
Sebastian Dröge 2020-04-14 19:41:24 +03:00
parent 70ef91fb4a
commit f1e3212477
2 changed files with 10 additions and 188 deletions

View file

@ -56,8 +56,8 @@ static inline libcaption_stauts_t libcaption_status_update(libcaption_stauts_t o
#define SCREEN_COLS 32
typedef struct {
unsigned int uln : 1; //< underline
unsigned int sty : 3; //< style
uint8_t uln; //< underline
uint8_t sty; //< style
utf8_char_t data[5]; //< 4 byte utf8 values plus null term
} caption_frame_cell_t;
@ -66,9 +66,9 @@ typedef struct {
} caption_frame_buffer_t;
typedef struct {
unsigned int uln : 1; //< underline
unsigned int sty : 3; //< style
unsigned int rup : 2; //< roll-up line count minus 1
uint8_t uln; //< underline
uint8_t sty; //< style
uint8_t rup; //< roll-up line count minus 1
int8_t row, col;
uint16_t cc_data;
} caption_frame_state_t;

View file

@ -1,86 +1,5 @@
/* automatically generated by rust-bindgen */
#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct __BindgenBitfieldUnit<Storage, Align> {
storage: Storage,
align: [Align; 0],
}
impl<Storage, Align> __BindgenBitfieldUnit<Storage, Align> {
#[inline]
pub const fn new(storage: Storage) -> Self {
Self { storage, align: [] }
}
}
impl<Storage, Align> __BindgenBitfieldUnit<Storage, Align>
where
Storage: AsRef<[u8]> + AsMut<[u8]>,
{
#[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 = if cfg!(target_endian = "big") {
7 - (index % 8)
} else {
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 = if cfg!(target_endian = "big") {
7 - (index % 8)
} else {
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) {
let index = if cfg!(target_endian = "big") {
bit_width as usize - 1 - i
} else {
i
};
val |= 1 << index;
}
}
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;
let index = if cfg!(target_endian = "big") {
bit_width as usize - 1 - i
} else {
i
};
self.set_bit(index + bit_offset, val_bit_is_set);
}
}
}
pub const EIA608_CHAR_COUNT: u32 = 176;
pub const EIA608_CHAR_NULL: &'static [u8; 1usize] = b"\0";
pub const EIA608_CHAR_SPACE: &'static [u8; 2usize] = b" \0";
@ -815,124 +734,27 @@ pub const libcaption_stauts_t_LIBCAPTION_READY: libcaption_stauts_t = 2;
pub const libcaption_stauts_t_LIBCAPTION_CLEAR: libcaption_stauts_t = 3;
pub type libcaption_stauts_t = u32;
#[repr(C)]
#[repr(align(4))]
#[derive(Debug, Copy, Clone)]
pub struct caption_frame_cell_t {
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 1usize], u8>,
pub uln: u8,
pub sty: u8,
pub data: [utf8_char_t; 5usize],
}
impl caption_frame_cell_t {
#[inline]
pub fn uln(&self) -> ::std::os::raw::c_uint {
unsafe { ::std::mem::transmute(self._bitfield_1.get(0usize, 1u8) as u32) }
}
#[inline]
pub fn set_uln(&mut self, val: ::std::os::raw::c_uint) {
unsafe {
let val: u32 = ::std::mem::transmute(val);
self._bitfield_1.set(0usize, 1u8, val as u64)
}
}
#[inline]
pub fn sty(&self) -> ::std::os::raw::c_uint {
unsafe { ::std::mem::transmute(self._bitfield_1.get(1usize, 3u8) as u32) }
}
#[inline]
pub fn set_sty(&mut self, val: ::std::os::raw::c_uint) {
unsafe {
let val: u32 = ::std::mem::transmute(val);
self._bitfield_1.set(1usize, 3u8, val as u64)
}
}
#[inline]
pub fn new_bitfield_1(
uln: ::std::os::raw::c_uint,
sty: ::std::os::raw::c_uint,
) -> __BindgenBitfieldUnit<[u8; 1usize], u8> {
let mut __bindgen_bitfield_unit: __BindgenBitfieldUnit<[u8; 1usize], u8> =
Default::default();
__bindgen_bitfield_unit.set(0usize, 1u8, {
let uln: u32 = unsafe { ::std::mem::transmute(uln) };
uln as u64
});
__bindgen_bitfield_unit.set(1usize, 3u8, {
let sty: u32 = unsafe { ::std::mem::transmute(sty) };
sty as u64
});
__bindgen_bitfield_unit
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct caption_frame_buffer_t {
pub cell: [[caption_frame_cell_t; 32usize]; 15usize],
}
#[repr(C)]
#[repr(align(4))]
#[derive(Debug, Copy, Clone)]
pub struct caption_frame_state_t {
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 1usize], u8>,
pub uln: u8,
pub sty: u8,
pub rup: u8,
pub row: i8,
pub col: i8,
pub cc_data: u16,
}
impl caption_frame_state_t {
#[inline]
pub fn uln(&self) -> ::std::os::raw::c_uint {
unsafe { ::std::mem::transmute(self._bitfield_1.get(0usize, 1u8) as u32) }
}
#[inline]
pub fn set_uln(&mut self, val: ::std::os::raw::c_uint) {
unsafe {
let val: u32 = ::std::mem::transmute(val);
self._bitfield_1.set(0usize, 1u8, val as u64)
}
}
#[inline]
pub fn sty(&self) -> ::std::os::raw::c_uint {
unsafe { ::std::mem::transmute(self._bitfield_1.get(1usize, 3u8) as u32) }
}
#[inline]
pub fn set_sty(&mut self, val: ::std::os::raw::c_uint) {
unsafe {
let val: u32 = ::std::mem::transmute(val);
self._bitfield_1.set(1usize, 3u8, val as u64)
}
}
#[inline]
pub fn rup(&self) -> ::std::os::raw::c_uint {
unsafe { ::std::mem::transmute(self._bitfield_1.get(4usize, 2u8) as u32) }
}
#[inline]
pub fn set_rup(&mut self, val: ::std::os::raw::c_uint) {
unsafe {
let val: u32 = ::std::mem::transmute(val);
self._bitfield_1.set(4usize, 2u8, val as u64)
}
}
#[inline]
pub fn new_bitfield_1(
uln: ::std::os::raw::c_uint,
sty: ::std::os::raw::c_uint,
rup: ::std::os::raw::c_uint,
) -> __BindgenBitfieldUnit<[u8; 1usize], u8> {
let mut __bindgen_bitfield_unit: __BindgenBitfieldUnit<[u8; 1usize], u8> =
Default::default();
__bindgen_bitfield_unit.set(0usize, 1u8, {
let uln: u32 = unsafe { ::std::mem::transmute(uln) };
uln as u64
});
__bindgen_bitfield_unit.set(1usize, 3u8, {
let sty: u32 = unsafe { ::std::mem::transmute(sty) };
sty as u64
});
__bindgen_bitfield_unit.set(4usize, 2u8, {
let rup: u32 = unsafe { ::std::mem::transmute(rup) };
rup as u64
});
__bindgen_bitfield_unit
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct caption_frame_t {