cast __swapXX in _endian.h to help the compiler

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

cast __swapXX in _endian.h to help the compiler

David Gwynne-5
this silences the warnings when building dhclient.

according to guenther@, the c standard says ternary operators are
subject to type promotion rules, ie, it ends up being at least an
int. this adds explicit casts to the expected type, which makes
printf happier when you tell it that it's a %hu coming up.

ok?

Index: _endian.h
===================================================================
RCS file: /cvs/src/sys/sys/_endian.h,v
retrieving revision 1.7
diff -u -p -r1.7 _endian.h
--- _endian.h 7 Jan 2018 15:17:42 -0000 1.7
+++ _endian.h 11 Jan 2018 03:31:01 -0000
@@ -83,9 +83,12 @@ __swap64md(__uint64_t x)
 }
 #endif
 
-#define __swap16(x) (__builtin_constant_p(x) ? __swap16gen(x) : __swap16md(x))
-#define __swap32(x) (__builtin_constant_p(x) ? __swap32gen(x) : __swap32md(x))
-#define __swap64(x) (__builtin_constant_p(x) ? __swap64gen(x) : __swap64md(x))
+#define __swap16(x) \
+ (__uint16_t)(__builtin_constant_p(x) ? __swap16gen(x) : __swap16md(x))
+#define __swap32(x) \
+ (__uint32_t)(__builtin_constant_p(x) ? __swap32gen(x) : __swap32md(x))
+#define __swap64(x) \
+ (__uint64_t)(__builtin_constant_p(x) ? __swap64gen(x) : __swap64md(x))
 
 #if _BYTE_ORDER == _LITTLE_ENDIAN
 

Reply | Threaded
Open this post in threaded view
|

Re: cast __swapXX in _endian.h to help the compiler

Mark Kettenis
> Date: Thu, 11 Jan 2018 13:53:00 +1000
> From: David Gwynne <[hidden email]>
>
> this silences the warnings when building dhclient.
>
> according to guenther@, the c standard says ternary operators are
> subject to type promotion rules, ie, it ends up being at least an
> int. this adds explicit casts to the expected type, which makes
> printf happier when you tell it that it's a %hu coming up.
>
> ok?

ok kettenis@

> Index: _endian.h
> ===================================================================
> RCS file: /cvs/src/sys/sys/_endian.h,v
> retrieving revision 1.7
> diff -u -p -r1.7 _endian.h
> --- _endian.h 7 Jan 2018 15:17:42 -0000 1.7
> +++ _endian.h 11 Jan 2018 03:31:01 -0000
> @@ -83,9 +83,12 @@ __swap64md(__uint64_t x)
>  }
>  #endif
>  
> -#define __swap16(x) (__builtin_constant_p(x) ? __swap16gen(x) : __swap16md(x))
> -#define __swap32(x) (__builtin_constant_p(x) ? __swap32gen(x) : __swap32md(x))
> -#define __swap64(x) (__builtin_constant_p(x) ? __swap64gen(x) : __swap64md(x))
> +#define __swap16(x) \
> + (__uint16_t)(__builtin_constant_p(x) ? __swap16gen(x) : __swap16md(x))
> +#define __swap32(x) \
> + (__uint32_t)(__builtin_constant_p(x) ? __swap32gen(x) : __swap32md(x))
> +#define __swap64(x) \
> + (__uint64_t)(__builtin_constant_p(x) ? __swap64gen(x) : __swap64md(x))
>  
>  #if _BYTE_ORDER == _LITTLE_ENDIAN
>  
>
>

Reply | Threaded
Open this post in threaded view
|

Re: cast __swapXX in _endian.h to help the compiler

Joerg Sonnenberger-2
In reply to this post by David Gwynne-5
On Thu, Jan 11, 2018 at 01:53:00PM +1000, David Gwynne wrote:
> this silences the warnings when building dhclient.

You might want to check if __builtin_bswap16 and friends exist first and
prefer to use them directly.

Joerg