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.

Reply via email to