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

Convert an u16 array to f16 array #14

Closed
johannesvollmer opened this issue Aug 30, 2018 · 4 comments · Fixed by #15
Closed

Convert an u16 array to f16 array #14

johannesvollmer opened this issue Aug 30, 2018 · 4 comments · Fixed by #15

Comments

@johannesvollmer
Copy link
Contributor

johannesvollmer commented Aug 30, 2018

I am currently implementing a popular file format in Rust which stores binary f16 arrays. As byteorder does not support f16, I need to read these f16 arrays as u16 arrays and then convert them to f16, in order to correctly handle endianness.

I wondered if there could be some functionality in this library, which prevents users to just do array.iter().map(|u| f16::from_bits(u)).collect::<Vec<f16>>(). This code iterates the whole array and allocates a new vector, but (I assume that) in memory, not a single bit is changed. All it probably does is basically copying the contents of the array element by element. But all it needs to do is really just interpreting the bits in a different way by representing it with a different type at compile time.

Is it maybe possible to reinterpret an array of u16 as an array of f16 by simply unsafely casting the raw pointers?

If not, certainly it's possible to provide an F16Slice <'a> (&'a [u16]) which maybe even implements ::std::ops::Index with f16::from_bits(self.0[index]), or something similar.

On the other hand, I can imagine that my use case is rather rare and not many people need to convert between large arrays of u16 and f16. Anyways, what do you think of that?

Also, Please correct me if I have overlooked something.

@starkat99
Copy link
Owner

Yes, it should be safe to reinterpret the whole array. And this use case is probably common, so probably should add some slice/array conversion helpers

@johannesvollmer
Copy link
Contributor Author

That would be great. I think the biggest challenge here is proving that it will work everywhere, the actual implementation is just three or less lines per conversion.

@johannesvollmer
Copy link
Contributor Author

I have prototyped the implementation including some simple tests, see my fork.

As this commit also supports converting Vecs, it contains a conditional module that is only included when feature="std" is enabled. The tests were performed with Rust version 1.15.0.

Please note that I am not that experienced in writing unsafe Rust code, and that maybe the code only works because I did not test it well enough.

Would you welcome a a pull request from that fork?

@starkat99
Copy link
Owner

Yes, a pull request would be great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants