[PATCH v4 next 2/9] lib: mul_u64_u64_div_u64() Combine overflow and divide by zero checks

David Laight posted 9 patches 1 month, 2 weeks ago
There is a newer version of this series
[PATCH v4 next 2/9] lib: mul_u64_u64_div_u64() Combine overflow and divide by zero checks
Posted by David Laight 1 month, 2 weeks ago
Since the overflow check always triggers when the divisor is zero
move the check for divide by zero inside the overflow check.
This means there is only one test in the normal path.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---

V3 contained a different patch 2 that did different chenges to the
error paths.

 lib/math/div64.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/lib/math/div64.c b/lib/math/div64.c
index 0ebff850fd4d..1092f41e878e 100644
--- a/lib/math/div64.c
+++ b/lib/math/div64.c
@@ -212,12 +212,16 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 d)
 
 #endif
 
-	/* make sure d is not zero, trigger runtime exception otherwise */
-	if (unlikely(d == 0)) {
-		unsigned long zero = 0;
+	if (unlikely(n_hi >= d)) {
+		/* trigger runtime exception if divisor is zero */
+		if (d == 0) {
+			unsigned long zero = 0;
 
-		OPTIMIZER_HIDE_VAR(zero);
-		return ~0UL/zero;
+			OPTIMIZER_HIDE_VAR(zero);
+			return ~0UL/zero;
+		}
+		/* overflow: result is unrepresentable in a u64 */
+		return ~0ULL;
 	}
 
 	int shift = __builtin_ctzll(d);
@@ -234,11 +238,6 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 d)
 		 */
 	}
 
-	if (n_hi >= d) {
-		/* overflow: result is unrepresentable in a u64 */
-		return -1;
-	}
-
 	/* Do the full 128 by 64 bits division */
 
 	shift = __builtin_clzll(d);
-- 
2.39.5
Re: [PATCH v4 next 2/9] lib: mul_u64_u64_div_u64() Combine overflow and divide by zero checks
Posted by Nicolas Pitre 1 month, 2 weeks ago
On Wed, 29 Oct 2025, David Laight wrote:

> Since the overflow check always triggers when the divisor is zero
> move the check for divide by zero inside the overflow check.
> This means there is only one test in the normal path.
> 
> Signed-off-by: David Laight <david.laight.linux@gmail.com>

Reviewed-by: Nicolas Pitre <npitre@baylibre.com>

> ---
> 
> V3 contained a different patch 2 that did different chenges to the
> error paths.
> 
>  lib/math/div64.c | 19 +++++++++----------
>  1 file changed, 9 insertions(+), 10 deletions(-)
> 
> diff --git a/lib/math/div64.c b/lib/math/div64.c
> index 0ebff850fd4d..1092f41e878e 100644
> --- a/lib/math/div64.c
> +++ b/lib/math/div64.c
> @@ -212,12 +212,16 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 d)
>  
>  #endif
>  
> -	/* make sure d is not zero, trigger runtime exception otherwise */
> -	if (unlikely(d == 0)) {
> -		unsigned long zero = 0;
> +	if (unlikely(n_hi >= d)) {
> +		/* trigger runtime exception if divisor is zero */
> +		if (d == 0) {
> +			unsigned long zero = 0;
>  
> -		OPTIMIZER_HIDE_VAR(zero);
> -		return ~0UL/zero;
> +			OPTIMIZER_HIDE_VAR(zero);
> +			return ~0UL/zero;
> +		}
> +		/* overflow: result is unrepresentable in a u64 */
> +		return ~0ULL;
>  	}
>  
>  	int shift = __builtin_ctzll(d);
> @@ -234,11 +238,6 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 d)
>  		 */
>  	}
>  
> -	if (n_hi >= d) {
> -		/* overflow: result is unrepresentable in a u64 */
> -		return -1;
> -	}
> -
>  	/* Do the full 128 by 64 bits division */
>  
>  	shift = __builtin_clzll(d);
> -- 
> 2.39.5
> 
>