4 ms·
There's no "bitcast" operator that changes the type of a value without affecting the value. The most common workaround involves going through memory and praying
by andrepd 7y ago
There's no "bitcast" operator that changes the type of a value without affecting the value. The most common workaround involves going through memory and praying the compiler will optimize that away (while violating strict aliasing semantics to boot).
Access through a union allow the same region of memory to be interpreted as different types.
No support for multiple return values (in registers). There's structs, but that means returning via memory because, well, see point 1.
No mainstream compiler will ever not optimise that.
- saagarjha 7y ago> Access through a union allow the same region of memory to be interpreted as different types. …only in C. > No mainstream compiler will ever not optimise that. Actually, it's mandated by the System-V ABI for structures of an appropriate size.
- kevingadd 7y agoUsing a union still has the going-through-memory problem, though. I spent a day or two trying to coax clang into doing the efficient thing with a union a couple years back when I could have gotten the job done in 30 minutes with raw assembly (alas...) RE: Return values, you'd be surprised. You can't assume you'll get properly optimized code for something like that in WebAssembly for example, despite the fact that you're using an industry-standard compiler (clang) and runtime (v8 or spidermonkey).
- saagarjha 7y ago> Using a union still has the going-through-memory problem, though. Not with modern compilers: https://godbolt.org/z/e6sRqh https://godbolt.org/z/e6sRqh