This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: is portable aliasing possible in C++?
- From: Hei Chan <structurechart at yahoo dot com>
- To: Andrew Haley <aph at redhat dot com>, "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Mon, 15 Sep 2014 04:29:28 -0700
- Subject: Re: is portable aliasing possible in C++?
- Authentication-results: sourceware.org; auth=none
- References: <1410390231 dot 39617 dot YahooMailNeo at web140202 dot mail dot bf1 dot yahoo dot com> <54115917 dot 1040602 at redhat dot com> <1410477938 dot 56522 dot YahooMailNeo at web140205 dot mail dot bf1 dot yahoo dot com> <5412AF85 dot 1080200 at redhat dot com> <1410562688 dot 66898 dot YahooMailNeo at web140201 dot mail dot bf1 dot yahoo dot com> <5413F0D4 dot 5010806 at redhat dot com> <1410748615 dot 48628 dot YahooMailNeo at web165004 dot mail dot bf1 dot yahoo dot com> <5416A4E1 dot 1010000 at redhat dot com> <1410779226 dot 12412 dot YahooMailNeo at web165006 dot mail dot bf1 dot yahoo dot com> <5416CBC1 dot 50900 at redhat dot com>
- Reply-to: Hei Chan <structurechart at yahoo dot com>
On Monday, September 15, 2014 7:22 PM, Andrew Haley <aph@redhat.com> wrote:
On 09/15/2014 12:07 PM, Hei Chan wrote:
>
>> On Monday, September 15, 2014 4:36 PM, Andrew Haley <aph@redhat.com> wrote:
>> On 15/09/14 03:36, Hei Chan wrote:
>>>
>>> This is an interesting thread.
>>>
>>> I think it is very common that people try to avoid making a copy
>>> from the buffer filled by recv() (or alike) to achieve lowest
>>> latency.
>>>
>>> Given that
>>> 1. The "union trick" has always worked with GCC, and is now hallowed
>>> by the standard. So it sounds like GCC might change in the future.
>>
>> Why?
>
> Your statement that the trick "is now hallowed by the standard"
> makes it sounds like at some point GCC won't guarantee it work
> anymore.
I disagree. It does not say that. GCC will not change this
behaviour.
>>> 2. Somewhere in the code that might manipulate the buffer via
>>> somehow casted packed C struct. Hence, any compiler is unlikely
>>> able to avoid making call if memcpy() is used.
>>
>> I don't understand what you mean by this. You can always write a
>> function which takes a pointer to a character type and calls memcpy()
>> to copy it into any scalar type, and it won't unnecessarily call
>> anything; or if it does that's a missed-optimization bug.
>
>
> Sorry, it is a typo -- I mean "compiler is unlikely able to avoid
> making *a copy* if memcpy() is used".
The compiler is likely to be able to avoid making a copy if memcpy() is
used.
> Using the unsafe reinterpret_cast (C fashion cast), it won't have an
> extra copy.
The alignment requirement is a property of the hardware. It is not a
property of the software. If the type needs aligning, it'll have to
be aligned somehow. Using reinterpret_cast does not help.
> Using memcpy(), the compiler will have to make a copy
> because it sees that few lines, for example, down, the program tries
> to manipulate the copy.
So, don't manipulate the copy, then. Use it once, then throw it away.
Sometimes, due to the endianness, I am forced to manipulate the copy...
>>> Then, I have the following questions:
>>> A. I use GCC and portability isn't an issue. What is the best type
>>> punning method to achieve lowest latency?
>
>> A union. You need a union to guarantee alignment.
>
> So I guess there is no way to avoid a copy if the code manipulates
> the member of the union, right?
There is no need for a copy. I already produced an example which
proves that.
> I understand that union and memcpy() would guarantee alignment. I
> was just hoping that there is a way of guaranteeing alignment
> without an extra copy. Sounds like there is no way?
It depends on the processor; on x86 and some ARMs and others yes. On
some others no.
Andrew.