[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: COPY_TWO_BYTES and C strict aliasing

From: Stefan Fuhrmann <eqfox_at_web.de>
Date: Mon, 04 Jun 2012 11:36:16 +0200

Am 01.06.2012 14:22, schrieb Philip Martin:
> GCC gives a compiler warning where the COPY_TWO_BYTES macro is used. A
> typical warning is:
>
> ../src/subversion/libsvn_subr/string.c:971:11: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
> COPY_TWO_BYTES(dest, decimal_table[(apr_size_t)number]);
> ^
>
> The code is:
>
> /* Copy the two bytes at SOURCE[0] and SOURCE[1] to DEST[0] and DEST[1] */
> #if SVN_UNALIGNED_ACCESS_IS_OK
> # define COPY_TWO_BYTES(dest,source)\
> *(apr_uint16_t*)(dest) = *(apr_uint16_t*)(source);
> #else
> # define COPY_TWO_BYTES(dest,source) \
> do { \
> (dest)[0] = (source)[0]; \
> (dest)[1] = (source)[1]; \
> } while (0)
> #endif
>
> apr_size_t
> svn__ui64toa(char * dest, apr_uint64_t number)
> {
> char buffer[SVN_INT64_BUFFER_SIZE];
> apr_uint32_t reduced; /* used for 32 bit DIV */
> char* target;
>
> /* Small numbers are by far the most common case.
> * Therefore, we use special code.
> */
> if (number< 100)
> {
> if (number< 10)
> {
> dest[0] = (char)('0' + number);
> dest[1] = 0;
> return 1;
> }
> else
> {
> COPY_TWO_BYTES(dest, decimal_table[(apr_size_t)number]);
> dest[2] = 0;
> return 2;
> }
> }
>
>
> Is the warning something we can ignore? My understanding of C aliasing
> is that writing to memory is only supposed to happen as the declared
> type of that memory.
>
> Is COPY_TWO_BYTES a significant optimisation? On Linux we can avoid the
> warning by simply using
>
> memcpy(dest, source, 2)
>
> since that memcpy call will be inlined these days.
>
I just verified that memcpy will indeed be properly inlined.
Switched to memcpy as per your suggestion in r1345883.

Thanks,
Stefan^2.
Received on 2012-06-04 11:37:03 CEST

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.