Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Support f16 #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ impl Deserialize for bool {
}
}

impl Deserialize for f16 {
fn deserialize(raw: &mut Deserializer) -> Result<Self> {
raw.float().map(|f| f as f16)
}
}

impl Deserialize for f32 {
fn deserialize(raw: &mut Deserializer) -> Result<Self> {
raw.float().map(|f| f as f32)
Expand Down Expand Up @@ -772,9 +778,9 @@ impl Deserializer {
Ok(Special::Unassigned(b as u8))
}
0x19 => {
let f = self.u16(1)?;
let f = self.u16(1)? as u16;
self.advance(3)?;
Ok(Special::Float(f as f64))
Ok(Special::Float(f16::from_bits(f) as f64))
}
0x1a => {
let f = self.u32(1)? as u32;
Expand Down Expand Up @@ -974,6 +980,16 @@ mod test {
assert_eq!(float, 100000.0);
}

#[test]
fn float16() {
let vec = vec![0xf9, 0x3c, 0x00];
let mut raw = Deserializer::from(vec);

let float = raw.float().unwrap();

assert_eq!(float, 1.0);
}

#[test]
fn array() {
let vec = vec![0x86, 0, 1, 2, 3, 4, 5];
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
//! ```

#![no_std]
#![feature(f16)]

Check failure on line 62 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy

`#![feature]` may not be used on the stable release channel

Check failure on line 62 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust project (1.58.0)

`#![feature]` may not be used on the stable release channel

Check failure on line 62 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust project (stable)

`#![feature]` may not be used on the stable release channel

Check failure on line 62 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust project (stable, wasm32-unknown-unknown, false)

`#![feature]` may not be used on the stable release channel

Check failure on line 62 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Rust project (stable, x86_64-apple-ios)

`#![feature]` may not be used on the stable release channel
#[cfg(test)]
#[macro_use]
extern crate quickcheck;
Expand Down
17 changes: 17 additions & 0 deletions src/se.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ impl Serialize for bool {
serializer.write_special(Special::Bool(*self))
}
}
impl Serialize for f16 {
fn serialize<'a>(&self, serializer: &'a mut Serializer) -> Result<&'a mut Serializer> {
serializer.write_special(Special::Float((*self) as f64))
}
}
impl Serialize for f32 {
fn serialize<'a>(&self, serializer: &'a mut Serializer) -> Result<&'a mut Serializer> {
serializer.write_special(Special::Float((*self) as f64))
Expand Down Expand Up @@ -302,6 +307,18 @@ impl Serializer {
Ok(self)
}

#[inline]
fn write_f16(&mut self, value: f16) -> Result<&mut Self> {
self.data.extend_from_slice(&value.to_be_bytes());
Ok(self)
}

#[inline]
fn write_f32(&mut self, value: f32) -> Result<&mut Self> {
self.data.extend_from_slice(&value.to_be_bytes());
Ok(self)
}

#[inline]
fn write_f64(&mut self, value: f64) -> Result<&mut Self> {
self.data.extend_from_slice(&value.to_be_bytes());
Expand Down
Loading