From: Luis Pires Date: Fri, 10 Sep 2021 11:26:05 +0000 (-0300) Subject: host-utils: introduce uabs64() X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=d03bba0bfbe6dcacc9d7be2b664d70fde081cc47;p=qemu.git host-utils: introduce uabs64() Introduce uabs64(), a function that returns the absolute value of a 64-bit int as an unsigned value. This avoids the undefined behavior for common abs implementations, where abs of the most negative value is undefined. Signed-off-by: Luis Pires Reviewed-by: Richard Henderson Reviewed-by: Eduardo Habkost Message-Id: <20210910112624.72748-4-luis.pires@eldorado.org.br> Signed-off-by: David Gibson --- diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h index 753b9fb89f..ca9f3f021b 100644 --- a/include/qemu/host-utils.h +++ b/include/qemu/host-utils.h @@ -357,6 +357,14 @@ static inline uint64_t revbit64(uint64_t x) #endif } +/** + * Return the absolute value of a 64-bit integer as an unsigned 64-bit value + */ +static inline uint64_t uabs64(int64_t v) +{ + return v < 0 ? -v : v; +} + /** * sadd32_overflow - addition with overflow indication * @x, @y: addends