qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v2 03/19] host-utils: move checks out of divu128/divs128


From: Richard Henderson
Subject: Re: [PATCH v2 03/19] host-utils: move checks out of divu128/divs128
Date: Tue, 31 Aug 2021 10:21:55 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0

On 8/31/21 9:39 AM, Luis Pires wrote:
-static inline int divs128(int64_t *plow, int64_t *phigh, int64_t divisor)
+static inline void 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 result = dividend / divisor;
-        *plow = result;
-        *phigh = dividend % divisor;
-        return result != *plow;
-    }
+    __int128_t dividend = ((__int128_t)*phigh << 64) | *plow;

This is incorrect, before and after: *plow must be zero-extended here.


@@ -97,13 +101,11 @@ int divu128(uint64_t *plow, uint64_t *phigh, uint64_t 
divisor)
      uint64_t carry = 0;
if (divisor == 0) {
-        return 1;
+        /* intentionally cause a division by 0 */
+        *plow = 1 / divisor;
      } else if (dhi == 0) {
          *plow  = dlo / divisor;
          *phigh = dlo % divisor;

Let's not do two undefined things and leave *phigh uninitialized (e.g. riscv host, where div-by-zero does not trap). Just fold the div-by-zero case into the dhi == 0 case.


r~



reply via email to

[Prev in Thread] Current Thread [Next in Thread]