From 4ff2a971f4dd533140c01607a0c776ba8d837bc7 Mon Sep 17 00:00:00 2001 From: Luis Pires Date: Fri, 10 Sep 2021 08:26:04 -0300 Subject: host-utils: fix missing zero-extension in divs128 *plow (lower 64 bits of the dividend) is passed into divs128() as a signed 64-bit integer. When building an __int128_t from it, it must be zero-extended, instead of sign-extended. Suggested-by: Richard Henderson Signed-off-by: Luis Pires Message-Id: <20210910112624.72748-3-luis.pires@eldorado.org.br> Signed-off-by: David Gibson --- include/qemu/host-utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/qemu') diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h index 711b221..753b9fb 100644 --- a/include/qemu/host-utils.h +++ b/include/qemu/host-utils.h @@ -70,7 +70,7 @@ static inline int divs128(int64_t *plow, int64_t *phigh, int64_t divisor) if (divisor == 0) { return 1; } else { - __int128_t dividend = ((__int128_t)*phigh << 64) | *plow; + __int128_t dividend = ((__int128_t)*phigh << 64) | (uint64_t)*plow; __int128_t result = dividend / divisor; *plow = result; *phigh = dividend % divisor; -- cgit v1.1 From d03bba0bfbe6dcacc9d7be2b664d70fde081cc47 Mon Sep 17 00:00:00 2001 From: Luis Pires Date: Fri, 10 Sep 2021 08:26:05 -0300 Subject: 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 --- include/qemu/host-utils.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'include/qemu') diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h index 753b9fb..ca9f3f0 100644 --- a/include/qemu/host-utils.h +++ b/include/qemu/host-utils.h @@ -358,6 +358,14 @@ static inline uint64_t revbit64(uint64_t x) } /** + * 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 * @ret: Output for sum -- cgit v1.1