This is an archive of the discontinued LLVM Phabricator instance.

Add getSwappedBytes implementations for float / double
ClosedPublic

Authored by arsenm on Dec 18 2014, 1:53 PM.

Details

Reviewers
arsenm
Summary

Alternatively these could be special cased in endian::Writer

Diff Detail

Event Timeline

arsenm updated this revision to Diff 17471.Dec 18 2014, 1:53 PM
arsenm retitled this revision from to Add getSwappedBytes implementations for float / double .
arsenm updated this object.
arsenm edited the test plan for this revision. (Show Details)
arsenm set the repository for this revision to rL LLVM.
arsenm added a subscriber: Unknown Object (MLST).

Why are you using a memcpy for the implementation?
You may have a good reason, but since it is not obvious to me it may deserve a comment so it won't get remove in the future.

It does not seems true in C, I'm unsure for C++.

See the first answer here for instance: http://stackoverflow.com/questions/11442708/type-punning-and-unions-in-c

Because we are writing C++ and not C I tried to find what C++11 says about type punning:

"An lvalue expression of type T1 can be cast to the type “reference to T2” if an expression of type “pointer to T1” can be explicitly converted to the type “pointer to T2” using a reinterpret_cast. That is, a reference cast reinterpret_cast<T&>(x) has the same effect as the conversion *reinterpret_cast<T*>(&x) with the built-in & and * operators (and similarly for reinterpret_cast<T&&>(x)). The result refers to the same object as the source lvalue, but with a different type. The result is an lvalue for an lvalue reference type or an rvalue reference to function type and an xvalue for an rvalue reference to object type. No temporary is created, no copy is made, and constructors (12.1) or conversion functions (12.3) are not called."

So I assume this should be valid:

return SwapByteOrder_64(reinterpret_cast<int &>(C));
arsenm updated this revision to Diff 18104.Jan 13 2015, 12:08 PM
arsenm removed rL LLVM as the repository for this revision.

Alternative version which specializes endian::Writer

arsenm updated this revision to Diff 19355.Feb 4 2015, 2:50 PM

Add tests for EndianStream

arsenm accepted this revision.Feb 4 2015, 7:32 PM
arsenm added a reviewer: arsenm.

r228269

This revision is now accepted and ready to land.Feb 4 2015, 7:32 PM
arsenm closed this revision.Feb 4 2015, 7:32 PM