1
0
Fork 0
mirror of https://codeberg.org/icewind/bitbuffer.git synced 2026-06-04 00:54:07 +02:00

float reading

This commit is contained in:
Robin Appelman 2019-02-19 21:45:17 +01:00
commit b804b4a59e
2 changed files with 47 additions and 2 deletions

View file

@ -1,5 +1,4 @@
#![feature(test)]
#![feature(try_from)]
#![warn(missing_docs)]
//! Tools for reading integers of arbitrary bit length and non byte-aligned integers and other data types
@ -7,7 +6,7 @@
extern crate test;
use is_signed::IsSigned;
use num_traits::PrimInt;
use num_traits::{Float, PrimInt};
use std::cmp::min;
use std::mem::size_of;
use std::ops::BitOrAssign;
@ -257,4 +256,36 @@ impl<'a> BitBuffer<'a> {
}
Ok(data)
}
/// Read a sequence of bits from the buffer as float
///
/// # Errors
///
/// - [`ReadError::NotEnoughData`](enum.ReadError.html#variant.NotEnoughData): not enough bits available in the buffer
/// - [`ReadError::TooManyBits`](enum.ReadError.html#variant.TooManyBits): to many bits requested for the chosen integer type
///
/// # Examples
///
/// ```
/// use bitbuffer::BitBuffer;
///
/// let bytes:&[u8] =&[
/// 0b1011_0101, 0b0110_1010, 0b1010_1100, 0b1001_1001,
/// 0b1001_1001, 0b1001_1001, 0b1001_1001, 0b1110_0111,
/// 0, 0, 0, 0, 0, 0, 0, 0
/// ];
/// let buffer = BitBuffer::from_padded_slice(bytes, 8);
/// let result = buffer.read_float::<f32>(10);
/// ```
pub fn read_float<T>(&self, position: usize) -> Result<T>
where T: Float
{
if size_of::<T>() == 4 {
let int = self.read::<u32>(position, 32)?;
Ok(T::from(f32::from_bits(int)).unwrap())
} else {
let int = self.read::<u64>(position, 64)?;
Ok(T::from(f64::from_bits(int)).unwrap())
}
}
}

View file

@ -77,6 +77,20 @@ fn read_i64() {
assert_eq!(buffer.read::<i64>(1, 64).unwrap(), -0b1110_01111001_1001_1001_1001_1001_1001_1001_1001_1010_1100_0110_1010_1011_010);
}
#[test]
fn read_f32() {
let buffer = BitBuffer::from_padded_slice(BYTES, 12);
assert_eq!(buffer.read_float::<f64>(6).unwrap(), 135447455835963910000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0);
}
#[test]
fn read_f64() {
let buffer = BitBuffer::from_padded_slice(BYTES, 12);
assert_eq!(buffer.read_float::<f64>(6).unwrap(), 135447455835963910000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0);
}
fn read_perf(buffer: BitBuffer) -> u16 {
let size = 5;
let mut pos = 0;