Skip to contentSkip to conversation

mul_u64_u64_div_u64: fix the division-by-zero behavior

PATCH Superseded Nicolas Pitre 2 participants 3 messages Jun 16, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mul_u64_u64_div_u64: fix the division-by-zero behavior

The current implementation forces a compile-time 1/0 division, which generates an undefined instruction (ud2 on x86) rather than a proper runtime division-by-zero exception.

Change to trigger an actual div-by-0 exception at runtime, consistent with other division operations. Use a non-1 dividend to prevent the compiler from optimizing the division into a comparison.

lib/math/div64.c +7 -6
index 5faa29208bdb..eaa0c7e8b974 100644
@@ -212,12 +212,13 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c)
 
 #endif
 
-	/* make sure c is not zero, trigger exception otherwise */
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wdiv-by-zero"
-	if (unlikely(c == 0))
-		return 1/0;
-#pragma GCC diagnostic pop
+	/* make sure c is not zero, trigger runtime exception otherwise */
+	if (unlikely(c == 0)) {
+		unsigned long zero = 0;
+
+		asm ("" : "+r" (zero)); /* hide actual value from the compiler */
+		return ~0UL/zero;
+	}
 
 	int shift = __builtin_ctzll(c);
 
#1 lore raw
Quoted 19 lines · Nicolas Pitre

The current implementation forces a compile-time 1/0 division, which generates an undefined instruction (ud2 on x86) rather than a proper runtime division-by-zero exception.

Change to trigger an actual div-by-0 exception at runtime, consistent with other division operations. Use a non-1 dividend to prevent the compiler from optimizing the division into a comparison.

lib/math/div64.c +0 -1
index 5faa29208bdb..eaa0c7e8b974 100644
@@ -212,12 +212,13 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c)
 
 #endif
 
-	/* make sure c is not zero, trigger exception otherwise */

lib/math/div64.c u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c) · line 219

Quoted 10 lines
lib/math/div64.c +5 -5
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wdiv-by-zero"
-	if (unlikely(c == 0))
-		return 1/0;
-#pragma GCC diagnostic pop
+	/* make sure c is not zero, trigger runtime exception otherwise */
+	if (unlikely(c == 0)) {
+		unsigned long zero = 0;
+
+		asm ("" : "+r" (zero)); /* hide actual value from the compiler */

This is OPTIMIZER_HIDE_VAR().

Quoted 5 lines
lib/math/div64.c +2 -0
+		return ~0UL/zero;
+	}
 
 	int shift = __builtin_ctzll(c);
 
#2 lore raw
Quoted 19 lines · Thomas Weißschuh
Quoted 18 lines · Nicolas Pitre

The current implementation forces a compile-time 1/0 division, which generates an undefined instruction (ud2 on x86) rather than a proper runtime division-by-zero exception.

Change to trigger an actual div-by-0 exception at runtime, consistent with other division operations. Use a non-1 dividend to prevent the compiler from optimizing the division into a comparison.

lib/math/div64.c +0 -0
index 5faa29208bdb..eaa0c7e8b974 100644
@@ -212,12 +212,13 @@ u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c)
 
 #endif
 
Quoted 13 lines

lib/math/div64.c u64 mul_u64_u64_div_u64(u64 a, u64 b, u64 c) · line 219

Quoted 11 lines
lib/math/div64.c +5 -6
-	/* make sure c is not zero, trigger exception otherwise */
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wdiv-by-zero"
-	if (unlikely(c == 0))
-		return 1/0;
-#pragma GCC diagnostic pop
+	/* make sure c is not zero, trigger runtime exception otherwise */
+	if (unlikely(c == 0)) {
+		unsigned long zero = 0;
+
+		asm ("" : "+r" (zero)); /* hide actual value from the compiler */

This is OPTIMIZER_HIDE_VAR().

Excellent! I thought I'd seen that somewhere but couldn't remember it.

Quoted 7 lines
Quoted 6 lines
lib/math/div64.c +2 -0
+		return ~0UL/zero;
+	}
 
 	int shift = __builtin_ctzll(c);