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

add bswap method for Float16 #30903

Merged
merged 2 commits into from
Feb 2, 2019
Merged

add bswap method for Float16 #30903

merged 2 commits into from
Feb 2, 2019

Conversation

JeffBezanson
Copy link
Sponsor Member

Float16 seems to be the only primitive base numeric type that doesn't support bswap. Seems to be an oversight.

base/float.jl Outdated
@@ -841,6 +841,7 @@ eps(::AbstractFloat)


## byte order swaps for arbitrary-endianness serialization/deserialization ##
bswap(x::Float16) = bswap_int(x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also just simplify all these definitions to

bswap(x::IEEEFloat) = bswap_int(x)

@Keno
Copy link
Member

Keno commented Jan 30, 2019

No particular objection to this change, but in general do we really want bswap on Float? I wouldn't be surprised if that didn't work on a number of systems because of NaN/subnormal canonicalization.

@stevengj
Copy link
Member

@Keno, I don't follow you. My understanding is that a standard IEEE 754 fp value can be stored with the bytes in bigendian or littleendian order; it doesn't change the logical interpretation.

(The terminology "bigendian" and "littleendian" doesn't make as much sense when applied to a floating-point value that is a mashup of significand and exponent bits, but it seems pretty standard.)

@Keno
Copy link
Member

Keno commented Jan 30, 2019

@Keno, I don't follow you. My understanding is that a standard IEEE 754 fp value can be stored with the bytes in bigendian or littleendian order; it doesn't change the logical interpretation.

Yes, but once you load it into registers, you've committed to a byte order and certain architectures canonicalize values on load, so if you first load it into an fp register and then bswap it, you may not get the same as bswapping it in int registers and then loading it into an fp register.

@JeffBezanson
Copy link
Sponsor Member Author

Good point; maybe we should only have it for Float16, since hardware doesn't support it 😛

@Keno
Copy link
Member

Keno commented Jan 30, 2019

Good point; maybe we should only have it for Float16, since hardware doesn't support it 😛

GPUs do

@simonbyrne
Copy link
Contributor

Doesn't that argument imply that we shouldn't allow reinterpret between floats and ints at all?

@Keno
Copy link
Member

Keno commented Jan 31, 2019

Reinterpret is generally fine though of course you can't expect it to round trip ok in all cases (e.g. Naan payloads, subnormals). That's not really too much of a problem in practice because you don't tend to use those values a lot. But for the bswap case, having these methods encourages getting it wrong by loading first and then bswapping, which is likely to give incorrect answers even for reasonable values

@StefanKarpinski
Copy link
Sponsor Member

This seems like a fairly silly debate. There's an obvious thing this should mean, it should do that.

@Keno
Copy link
Member

Keno commented Jan 31, 2019

Well, the point is it can't do the obvious thing on some architectures because fp values get normalized upon loading into fp registers. Anyway, I'm not objecting to this change in particular.

@JeffBezanson JeffBezanson merged commit 3b4786a into master Feb 2, 2019
@JeffBezanson JeffBezanson deleted the jb/f16bswap branch February 2, 2019 19:43
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 this pull request may close these issues.

None yet

6 participants