cpu_to_endian() and endian_to_cpu() can be extended to handle constant expressions. That way the programmer doesn't need to remember the const_X() API exists.
Suggested-by: Stefan Hajnoczi <stefa...@gmail.com> Signed-off-by: Philippe Mathieu-Daudé <phi...@redhat.com> --- include/qemu/bswap.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/qemu/bswap.h b/include/qemu/bswap.h index de256cea3ab..8827e4760b9 100644 --- a/include/qemu/bswap.h +++ b/include/qemu/bswap.h @@ -191,12 +191,16 @@ static inline void bswap64s(uint64_t *s) #define CPU_CONVERT(endian, size, type)\ static inline type endian ## size ## _to_cpu(type v)\ {\ - return glue(endian, _bswap)(v, size);\ + return __builtin_constant_p(v) ?\ + const_ ## endian ## size(v) :\ + glue(endian, _bswap)(v, size);\ }\ \ static inline type cpu_to_ ## endian ## size(type v)\ {\ - return glue(endian, _bswap)(v, size);\ + return __builtin_constant_p(v) ?\ + const_ ## endian ## size(v) :\ + glue(endian, _bswap)(v, size);\ }\ \ static inline void endian ## size ## _to_cpus(type *p)\ -- 2.26.2