From: Kostya Shishkov Date: Sat, 16 Nov 2019 16:58:10 +0000 (+0100) Subject: core/byteio: add floating point read-write functions X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=02a5bdaf3a2cad57fa03bf88be1a6de2e8599d6a;p=nihav.git core/byteio: add floating point read-write functions --- diff --git a/nihav-core/src/io/byteio.rs b/nihav-core/src/io/byteio.rs index 9fc380a..d80572b 100644 --- a/nihav-core/src/io/byteio.rs +++ b/nihav-core/src/io/byteio.rs @@ -94,6 +94,10 @@ pub fn read_u24le(src: &[u8]) -> ByteIOResult { if src.len() < 3 { return Err(ByteIOError::ReadError); } Ok((u32::from(src[2]) << 16) | (u32::from(src[1]) << 8) | u32::from(src[0])) } +pub fn read_f32be(src: &[u8]) -> ByteIOResult { Ok(f32::from_bits(read_u32be(src)?)) } +pub fn read_f32le(src: &[u8]) -> ByteIOResult { Ok(f32::from_bits(read_u32le(src)?)) } +pub fn read_f64be(src: &[u8]) -> ByteIOResult { Ok(f64::from_bits(read_u64be(src)?)) } +pub fn read_f64le(src: &[u8]) -> ByteIOResult { Ok(f64::from_bits(read_u64le(src)?)) } macro_rules! write_int_func { ($s: ident, $inttype: ty, $size: expr, $which: ident) => { @@ -129,6 +133,10 @@ pub fn write_u24le(dst: &mut [u8], val: u32) -> ByteIOResult<()> { dst[2] = (val >> 16) as u8; Ok(()) } +pub fn write_f32be(dst: &mut [u8], val: f32) -> ByteIOResult<()> { write_u32be(dst, val.to_bits()) } +pub fn write_f32le(dst: &mut [u8], val: f32) -> ByteIOResult<()> { write_u32le(dst, val.to_bits()) } +pub fn write_f64be(dst: &mut [u8], val: f64) -> ByteIOResult<()> { write_u64be(dst, val.to_bits()) } +pub fn write_f64le(dst: &mut [u8], val: f64) -> ByteIOResult<()> { write_u64le(dst, val.to_bits()) } impl<'a> ByteReader<'a> { pub fn new(io: &'a mut ByteIO) -> Self { ByteReader { io } } @@ -189,6 +197,22 @@ impl<'a> ByteReader<'a> { peek_int!(self, u64, 8, to_be) } + pub fn read_f32be(&mut self) -> ByteIOResult { + Ok(f32::from_bits(self.read_u32be()?)) + } + + pub fn peek_f32be(&mut self) -> ByteIOResult { + Ok(f32::from_bits(self.peek_u32be()?)) + } + + pub fn read_f64be(&mut self) -> ByteIOResult { + Ok(f64::from_bits(self.read_u64be()?)) + } + + pub fn peek_f64be(&mut self) -> ByteIOResult { + Ok(f64::from_bits(self.peek_u64be()?)) + } + pub fn read_u16le(&mut self) -> ByteIOResult { read_int!(self, u16, 2, to_le) } @@ -225,6 +249,22 @@ impl<'a> ByteReader<'a> { peek_int!(self, u64, 8, to_le) } + pub fn read_f32le(&mut self) -> ByteIOResult { + Ok(f32::from_bits(self.read_u32le()?)) + } + + pub fn peek_f32le(&mut self) -> ByteIOResult { + Ok(f32::from_bits(self.peek_u32le()?)) + } + + pub fn read_f64le(&mut self) -> ByteIOResult { + Ok(f64::from_bits(self.read_u64le()?)) + } + + pub fn peek_f64le(&mut self) -> ByteIOResult { + Ok(f64::from_bits(self.peek_u64le()?)) + } + pub fn read_skip(&mut self, len: usize) -> ByteIOResult<()> { if self.io.is_seekable() { self.io.seek(SeekFrom::Current(len as i64))?; @@ -490,6 +530,22 @@ impl<'a> ByteWriter<'a> { self.write_u32le((val >> 32) as u32) } + pub fn write_f32be(&mut self, val: f32) -> ByteIOResult<()> { + self.write_u32be(val.to_bits()) + } + + pub fn write_f32le(&mut self, val: f32) -> ByteIOResult<()> { + self.write_u32le(val.to_bits()) + } + + pub fn write_f64be(&mut self, val: f64) -> ByteIOResult<()> { + self.write_u64be(val.to_bits()) + } + + pub fn write_f64le(&mut self, val: f64) -> ByteIOResult<()> { + self.write_u64le(val.to_bits()) + } + pub fn tell(&mut self) -> u64 { self.io.tell() }