6 ms·
I actually use macros along the lines of: #define LOAD(TYP, ptr) (*((TYP *)memcpy((TYP[1]){ }, ptr, sizeof(TYP)))) #define REINTERPRET(AS_TYP, FROM_TYP, va
by premchai21 14y ago
I actually use macros along the lines of:
#define LOAD(TYP, ptr) (*((TYP *)memcpy((TYP[1]){ }, ptr, sizeof(TYP))))
#define REINTERPRET(AS_TYP, FROM_TYP, val) LOAD(AS_TYP, (FROM_TYP[1]){ val })
In practice (that I've found, with GCC), the memcpy and single-use temporaries get optimized away entirely. In strict C99, writing one member of a union and then reading a different member of the same union is undefined in the general case, last I checked. http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html http://cellperformance.beyond3d.com/articles/2006/06/underst... seems to agree, but suggests that every major compiler recognizes it as a de facto idiom and supports it anyway.
- mistercow 14y agoYep, type punning through a union is nonstandard and unportable, but if I'm not mistaken, it is a documented feature in GCC.
- stephencanon 14y agoType punning through a union is explicitly defined to work in C99 and C11 (footnote 95 in C11): > If the member used to read the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called ‘‘type punning’’). Please don't help spread the myth that compiler writers can break this idiom. That said, I use memcpy in my own code.
- premchai21 14y agoAll right, I see it in C99 as well now (§6.5.2.3 footnote 82, if I'm not mistaken). Thanks for the correction.
- mistercow 14y agoOh, well I stand corrected.