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

Relationship between From/Into and FromBits traits? #413

Closed
hdevalence opened this issue Apr 3, 2018 · 4 comments
Closed

Relationship between From/Into and FromBits traits? #413

hdevalence opened this issue Apr 3, 2018 · 4 comments

Comments

@hdevalence
Copy link
Contributor

This change is mentioned in #338 , but I didn't see discussion of it there, so sorry if I'm missing something.

What's the intended relationship between the FromBits trait and the From/Into traits? I guess that FromBits is a more specialized trait to indicate that it's a lossless bitcast, but I am curious if there is a reason why that's better than allowing the use of From and Into.

Previously, the platform-specific intrinsics took portable vector types (e.g., u32x8) as arguments; these vector types implemented From/Into, so for the "overtyped" intrinsics it was possible to use .into() in appropriate places to do bitcasts. Now, all of the intrinsics have been changed to use bag-of-bits types, like __m256i, but there's no implementation of From/Into between the bag-of-bits types and the typed vectors of the same widths.

While there is the FromBits trait, because it's different from From, it's no longer possible to use .into() on the results of a platform intrinsic. Is there a strong reason to have FromBits distinct from From?

@gnzlbg
Copy link
Contributor

gnzlbg commented Apr 3, 2018

It was discussed tangentially in #151 .

  • From / Into perform lossless value preserving conversions, vor example, from an integer 32 to the 32.000000 floating point number

  • FromBits/IntoBits perform lossless bitcasts but bitcasting from an integer 32 to a floating-point number of the same size doesn't necessarily produce the value 32.

EDIT: Currently there are also too many From/Into implementations.P er the RFC some of them will be removed when as is implemented for casting between simd vector types. The RFC does not propose a solution for FromBits/IntoBits so they will initially remain unstable. In stable Rust one will need to use mem::transmute to perform bitcast until a better solution than FromBits/IntoBits for this problem is found (the RFC points to a couple of places where these have been discussed).

@hdevalence
Copy link
Contributor Author

Thanks for the pointer, the value/bitwise distinction makes sense.

Are all of the From/Into conversions supposed to be lossless or just value preserving? For instance impl From<u32x8> for f32x8 can't be lossless since not every u32 is exactly representable as an f32, right?

@gnzlbg
Copy link
Contributor

gnzlbg commented Apr 4, 2018

Are all of the From/Into conversions supposed to be lossless or just value preserving? For instance impl From for f32x8 can't be lossless since not every u32 is exactly representable as an f32, right?

Yes that's correct. So I am going to differentiate between the RFC and stdsimd. In the RFC impl From<u32x8> for f32x8 is not present, that is, trying to use results in a compilation error (error: From<u32x8 is not implemented for f32x8). The right way to do that is using as which is neither lossless nor value preserving.

Right now as for vector types is not implemented in rustc, so for the time being, stdsimd provides impl From<u32x8> for f32x8 which behaves like as. Once that is implemented in rustc, we'll remove those implementations - note that From<u32> for f32 is not implemented either for this exact same reason.

Does that make sense?

@hdevalence
Copy link
Contributor Author

Yep, that completely explains my question, thank you!

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

No branches or pull requests

2 participants