kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: kvm@vger.kernel.org
Cc: Alexandru Elisei <alexandru.elisei@arm.com>
Subject: [PATCH kvm-unit-tests 1/2] libcflat: clean up and complete long division routines
Date: Tue, 11 May 2021 19:41:05 +0200	[thread overview]
Message-ID: <20210511174106.703235-2-pbonzini@redhat.com> (raw)
In-Reply-To: <20210511174106.703235-1-pbonzini@redhat.com>

Avoid possible uninitialized variables on machines where
division by zero does not trap.  Add __divmoddi4, and
do not use 64-bit math unnecessarily in __moddi3 and __divdi3.

Reported-by: Alexandru Elisei <alexandru.elisei@arm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 lib/ldiv32.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/lib/ldiv32.c b/lib/ldiv32.c
index 96f4b35..c39fccd 100644
--- a/lib/ldiv32.c
+++ b/lib/ldiv32.c
@@ -1,6 +1,7 @@
 #include <stdint.h>
 
 extern uint64_t __udivmoddi4(uint64_t num, uint64_t den, uint64_t *p_rem);
+extern int64_t __divmoddi4(int64_t num, int64_t den, int64_t *p_rem);
 extern int64_t __moddi3(int64_t num, int64_t den);
 extern int64_t __divdi3(int64_t num, int64_t den);
 extern uint64_t __udivdi3(uint64_t num, uint64_t den);
@@ -11,8 +12,11 @@ uint64_t __udivmoddi4(uint64_t num, uint64_t den, uint64_t *p_rem)
 	uint64_t quot = 0;
 
 	/* Trigger a division by zero at run time (trick taken from iPXE).  */
-	if (den == 0)
+	if (den == 0) {
+		if (p_rem)
+			*p_rem = 0;
 		return 1/((unsigned)den);
+	}
 
 	if (num >= den) {
 		/* Align den to num to avoid wasting time on leftmost zero bits.  */
@@ -35,9 +39,27 @@ uint64_t __udivmoddi4(uint64_t num, uint64_t den, uint64_t *p_rem)
 	return quot;
 }
 
+int64_t __divmoddi4(int64_t num, int64_t den, int64_t *p_rem)
+{
+	int32_t nmask = num < 0 ? -1 : 0;
+	int32_t qmask = (num ^ den) < 0 ? -1 : 0;
+	uint64_t quot;
+
+	/* Compute absolute values and do an unsigned division.  */
+	num = (num + nmask) ^ nmask;
+	if (den < 0)
+		den = -den;
+
+	/* Copy sign of num^den into quotient, sign of num into remainder.  */
+	quot = (__divmoddi4(num, den, p_rem) + qmask) ^ qmask;
+	if (p_rem)
+		*p_rem = (*p_rem + nmask) ^ nmask;
+	return quot;
+}
+
 int64_t __moddi3(int64_t num, int64_t den)
 {
-	uint64_t mask = num < 0 ? -1 : 0;
+	int32_t mask = num < 0 ? -1 : 0;
 
 	/* Compute absolute values and do an unsigned division.  */
 	num = (num + mask) ^ mask;
@@ -50,7 +72,7 @@ int64_t __moddi3(int64_t num, int64_t den)
 
 int64_t __divdi3(int64_t num, int64_t den)
 {
-	uint64_t mask = (num ^ den) < 0 ? -1 : 0;
+	int32_t mask = (num ^ den) < 0 ? -1 : 0;
 
 	/* Compute absolute values and do an unsigned division.  */
 	if (num < 0)
-- 
2.31.1



  reply	other threads:[~2021-05-11 17:41 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-11 17:41 [PATCH kvm-unit-tests 0/2] fix long division routines for ARM eabi Paolo Bonzini
2021-05-11 17:41 ` Paolo Bonzini [this message]
2021-05-12 10:39   ` [PATCH kvm-unit-tests 1/2] libcflat: clean up and complete long division routines Alexandru Elisei
2021-05-12 10:47     ` Paolo Bonzini
2021-05-11 17:41 ` [PATCH kvm-unit-tests 2/2] arm: add eabi version of 64-bit division functions Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210511174106.703235-2-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=alexandru.elisei@arm.com \
    --cc=kvm@vger.kernel.org \
    --subject='Re: [PATCH kvm-unit-tests 1/2] libcflat: clean up and complete long division routines' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).