|
From: Florian K. <fl...@ei...> - 2014-10-22 15:28:45
|
On 22.10.2014 17:04, Bart Van Assche wrote:
> On 10/22/14 14:55, Florian Krohm wrote:
>> The other option is to replace the cast with some type punning trickery:
>>
>> #define CONST_CAST(T,x) \
>> ({ \
>> union { \
>> const T in; \
>> T out; \
>> } var = { .in = x }; var.out; \
>> })
>>
>> and use it like so:
>>
>> if (*s == c) return CONST_CAST(HChar *,s);
>>
>> I opted for the latter as it is less intrusive. The construct is
>> guaranteed to work even with -fstrict-aliasing (which we're not using).
>
> Hello Florian,
>
> Does the above construct cause the compiler to generate additional code
> compared to a regular cast ? Some time ago I introduced the following
> construct in the Net-SNMP code base:
Hi Bart,
I just made an experiment, compiling this:
const char *from;
char *to;
void foo()
{
to = MAGIC_CAST(char *,from);
}
with -O2 -g (as we do) and replacing MAGIC_CAST with the casts we came
up with. In both cases the generated code for foo is:
movq from(%rip), %rax
movq %rax, to(%rip)
ret
Which is what I would have expected.
Florian
|