[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#561249: Spurious warning with -Wconversion (uint16_t/htons) in -O2 mode



Eric Estievenart a écrit :
> Package: g++-4.4
> Version: 4.4.2-5
> Severity: normal
> 
> 
> The bug may be rather in libc6-dev, which defines:
> htons to __bswap16 if optimize && little endian,
> and __bswap16 in bits/byteswap.h
> # define __bswap_16(x) \
>      (__extension__                                                           \
>       ({ register unsigned short int __v, __x = (x);                          \
>          if (__builtin_constant_p (__x))                                      \
>            __v = __bswap_constant_16 (__x);                                   \
>          else                                                                 \
>            __asm__ ("rorw $8, %w0"                                            \
>                     : "=r" (__v)                                              \
>                     : "0" (__x)                                               \
>                     : "cc");                                                  \
>          __v; }))
> I'm not expert enough with gcc inline assembly to guess what the exact
> problem is, but it is definitely in this macro or the way gcc handles it,
> but since gcc <= 4.2 noes not show any warning in optimize it, I'd rather
> think it is a gcc (4.3 & 4.4) regression

The problem is not the asm code, but actually the shift that comes from
__bswap_constant_16(). The problem can be reproduced with this simple code:

#include <stdint.h>

void test ()
{
  uint16_t s = 123;
  uint16_t t = (s & 0xff) << 8;
  uint16_t u = ((uint8_t)s) << 8;
}

It looks like the << operator implicitely convert the value to int. I
haven't checked the standards about that.

-- 
Aurelien Jarno                          GPG: 1024D/F1BCDB73
aurelien@aurel32.net                 http://www.aurel32.net



Reply to: