[PATCH v3 02/22] host-utils: fix missing zero-extension in divs128

Luis Pires posted 22 patches 4 years, 5 months ago
Maintainers: Aurelien Jarno <aurelien@aurel32.net>, Greg Kurz <groug@kaod.org>, Damien Hedde <damien.hedde@greensocs.com>, Luc Michel <luc@lmichel.fr>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, "Alex Bennée" <alex.bennee@linaro.org>, Peter Maydell <peter.maydell@linaro.org>, Paolo Bonzini <pbonzini@redhat.com>, "Michael S. Tsirkin" <mst@redhat.com>, Richard Henderson <richard.henderson@linaro.org>, David Gibson <david@gibson.dropbear.id.au>, Eduardo Habkost <ehabkost@redhat.com>
There is a newer version of this series
[PATCH v3 02/22] host-utils: fix missing zero-extension in divs128
Posted by Luis Pires 4 years, 5 months ago
*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 <richard.henderson@linaro.org>
Signed-off-by: Luis Pires <luis.pires@eldorado.org.br>
---
 include/qemu/host-utils.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h
index 711b221704..753b9fb89f 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;
-- 
2.25.1


Re: [PATCH v3 02/22] host-utils: fix missing zero-extension in divs128
Posted by Richard Henderson 4 years, 3 months ago
On 9/10/21 4:26 AM, Luis Pires wrote:
> *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<richard.henderson@linaro.org>
> Signed-off-by: Luis Pires<luis.pires@eldorado.org.br>
> ---
>   include/qemu/host-utils.h | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~