diff mbox series

[v2,kvm-unit-tests,1/2] libcflat: clean up and complete long division routines

Message ID 20210512105440.748153-2-pbonzini@redhat.com (mailing list archive)
State New, archived
Headers show
Series fix long division routines for ARM eabi | expand

Commit Message

Paolo Bonzini May 12, 2021, 10:54 a.m. UTC
Avoid possible uninitialized variables on machines where
division by zero does not trap.  Add __divmoddi4, and
use it in __moddi3 and __divdi3.

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

Comments

Alexandru Elisei May 12, 2021, 1:44 p.m. UTC | #1
Hi Paolo,

On 5/12/21 11:54 AM, Paolo Bonzini wrote:
> Avoid possible uninitialized variables on machines where
> division by zero does not trap.  Add __divmoddi4, and
> use it in __moddi3 and __divdi3.

Looks good now, I like the change to __moddi3 and __divdi3 as that means that the
tests will cover __divmoddi4, and the functions are now similar to their unsigned
counterparts:

Reviewed-by: Alexandru Elisei <alexandru.elisei@arm.com>

Thanks,

Alex

>
> Reported-by: Alexandru Elisei <alexandru.elisei@arm.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  lib/ldiv32.c | 40 ++++++++++++++++++++++++----------------
>  1 file changed, 24 insertions(+), 16 deletions(-)
>
> diff --git a/lib/ldiv32.c b/lib/ldiv32.c
> index 96f4b35..897a4b9 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,31 +39,35 @@ uint64_t __udivmoddi4(uint64_t num, uint64_t den, uint64_t *p_rem)
>  	return quot;
>  }
>  
> -int64_t __moddi3(int64_t num, int64_t den)
> +int64_t __divmoddi4(int64_t num, int64_t den, int64_t *p_rem)
>  {
> -	uint64_t mask = num < 0 ? -1 : 0;
> +	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 + mask) ^ mask;
> +	num = (num + nmask) ^ nmask;
>  	if (den < 0)
>  		den = -den;
>  
> -	/* Copy sign of num into result.  */
> -	return (__umoddi3(num, den) + mask) ^ mask;
> +	/* Copy sign of num^den into quotient, sign of num into remainder.  */
> +	quot = (__udivmoddi4(num, den, (uint64_t *)p_rem) + qmask) ^ qmask;
> +	if (p_rem)
> +		*p_rem = (*p_rem + nmask) ^ nmask;
> +	return quot;
>  }
>  
> -int64_t __divdi3(int64_t num, int64_t den)
> +int64_t __moddi3(int64_t num, int64_t den)
>  {
> -	uint64_t mask = (num ^ den) < 0 ? -1 : 0;
> -
> -	/* Compute absolute values and do an unsigned division.  */
> -	if (num < 0)
> -		num = -num;
> -	if (den < 0)
> -		den = -den;
> +	int64_t rem;
> +	__divmoddi4(num, den, &rem);
> +	return rem;
> +}
>  
> -	/* Copy sign of num^den into result.  */
> -	return (__udivdi3(num, den) + mask) ^ mask;
> +int64_t __divdi3(int64_t num, int64_t den)
> +{
> +	int64_t rem;
> +	return __divmoddi4(num, den, &rem);
>  }
>  
>  uint64_t __udivdi3(uint64_t num, uint64_t den)
diff mbox series

Patch

diff --git a/lib/ldiv32.c b/lib/ldiv32.c
index 96f4b35..897a4b9 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,31 +39,35 @@  uint64_t __udivmoddi4(uint64_t num, uint64_t den, uint64_t *p_rem)
 	return quot;
 }
 
-int64_t __moddi3(int64_t num, int64_t den)
+int64_t __divmoddi4(int64_t num, int64_t den, int64_t *p_rem)
 {
-	uint64_t mask = num < 0 ? -1 : 0;
+	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 + mask) ^ mask;
+	num = (num + nmask) ^ nmask;
 	if (den < 0)
 		den = -den;
 
-	/* Copy sign of num into result.  */
-	return (__umoddi3(num, den) + mask) ^ mask;
+	/* Copy sign of num^den into quotient, sign of num into remainder.  */
+	quot = (__udivmoddi4(num, den, (uint64_t *)p_rem) + qmask) ^ qmask;
+	if (p_rem)
+		*p_rem = (*p_rem + nmask) ^ nmask;
+	return quot;
 }
 
-int64_t __divdi3(int64_t num, int64_t den)
+int64_t __moddi3(int64_t num, int64_t den)
 {
-	uint64_t mask = (num ^ den) < 0 ? -1 : 0;
-
-	/* Compute absolute values and do an unsigned division.  */
-	if (num < 0)
-		num = -num;
-	if (den < 0)
-		den = -den;
+	int64_t rem;
+	__divmoddi4(num, den, &rem);
+	return rem;
+}
 
-	/* Copy sign of num^den into result.  */
-	return (__udivdi3(num, den) + mask) ^ mask;
+int64_t __divdi3(int64_t num, int64_t den)
+{
+	int64_t rem;
+	return __divmoddi4(num, den, &rem);
 }
 
 uint64_t __udivdi3(uint64_t num, uint64_t den)