-
-
Notifications
You must be signed in to change notification settings - Fork 51
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
Comments
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 |
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. |
I have prototyped the implementation including some simple tests, see my fork. As this commit also supports converting 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? |
Yes, a pull request would be great |
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
withf16::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.
The text was updated successfully, but these errors were encountered: