Skip to contentSkip to conversation

drm/panic: Add a u64 divide by 10 for arm32

PATCH In review Jocelyn Falempe 3 participants 4 messages Jun 27, 2025 – Aug 1, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 drm/panic: Add a u64 divide by 10 for arm32

On 32bits ARM, u64 divided by a constant is not optimized to a multiply by inverse by the compiler [1].
So do the multiply by inverse explicitly for this architecture.

Fixes 675008f196ca ("drm/panic: Use a decimal fifo to avoid u64 by u64 divide")
---
 drivers/gpu/drm/drm_panic_qr.rs | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_panic_qr.rs +21 -1
index dd55b1cb764d..774a17de4f2f 100644
@@ -381,6 +381,26 @@ struct DecFifo {
     len: usize,
 }
 
+// On arm32 architecture, dividing an `u64` by a constant will generate a call
+// to `__aeabi_uldivmod` which is not present in the kernel.
+// So use the multiply by inverse method for this architecture.
+fn div10(val: u64) -> u64 {
+    if cfg!(target_arch = "arm") {
+        let val_h = val >> 32;
+        let val_l = val & 0xFFFFFFFF;
+        let b_h: u64 = 0x66666666;
+        let b_l: u64 = 0x66666667;
+
+        let tmp1 = val_h * b_l + ((val_l * b_l) >> 32);
+        let tmp2 = val_l * b_h + (tmp1 & 0xffffffff);
+        let tmp3 = val_h * b_h + (tmp1 >> 32) + (tmp2 >> 32);
+
+        tmp3 >> 2
+    } else {
+        val / 10
+    }
+}
+
 impl DecFifo {
     fn push(&mut self, data: u64, len: usize) {
         let mut chunk = data;
@@ -389,7 +409,7 @@ fn push(&mut self, data: u64, len: usize) {
         }
         for i in 0..len {
             self.decimals[i] = (chunk % 10) as u8;
-            chunk /= 10;
+            chunk = div10(chunk);
         }
         self.len += len;
     }

base-commit: 3529cb5ab16b4f1f8bbc31dc39a1076a94bd1e38

Signature · 1 line
2.49.0
#1 lore raw
Quoted 3 lines · Jocelyn Falempe

On 32bits ARM, u64 divided by a constant is not optimized to a multiply by inverse by the compiler [1].
So do the multiply by inverse explicitly for this architecture.

Gentle ping.

Best regards,

Signature · 1 line
Jocelyn
Quoted 52 lines
Fixes 675008f196ca ("drm/panic: Use a decimal fifo to avoid u64 by u64 divide")
---
  drivers/gpu/drm/drm_panic_qr.rs | 22 +++++++++++++++++++++-
  1 file changed, 21 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_panic_qr.rs +21 -1
index dd55b1cb764d..774a17de4f2f 100644
@@ -381,6 +381,26 @@ struct DecFifo {
      len: usize,
  }
 
+// On arm32 architecture, dividing an `u64` by a constant will generate a call
+// to `__aeabi_uldivmod` which is not present in the kernel.
+// So use the multiply by inverse method for this architecture.
+fn div10(val: u64) -> u64 {
+    if cfg!(target_arch = "arm") {
+        let val_h = val >> 32;
+        let val_l = val & 0xFFFFFFFF;
+        let b_h: u64 = 0x66666666;
+        let b_l: u64 = 0x66666667;
+
+        let tmp1 = val_h * b_l + ((val_l * b_l) >> 32);
+        let tmp2 = val_l * b_h + (tmp1 & 0xffffffff);
+        let tmp3 = val_h * b_h + (tmp1 >> 32) + (tmp2 >> 32);
+
+        tmp3 >> 2
+    } else {
+        val / 10
+    }
+}
+
  impl DecFifo {
      fn push(&mut self, data: u64, len: usize) {
          let mut chunk = data;
@@ -389,7 +409,7 @@ fn push(&mut self, data: u64, len: usize) {
          }
          for i in 0..len {
              self.decimals[i] = (chunk % 10) as u8;
-            chunk /= 10;
+            chunk = div10(chunk);
          }
          self.len += len;
      }

base-commit: 3529cb5ab16b4f1f8bbc31dc39a1076a94bd1e38

reviewer Reviewed-by on v2 1/1
#2 lore raw
In reply to Jocelyn Falempe
Quoted 9 lines · Jocelyn Falempe

On 32bits ARM, u64 divided by a constant is not optimized to a multiply by inverse by the compiler [1].
So do the multiply by inverse explicitly for this architecture.

Fixes 675008f196ca ("drm/panic: Use a decimal fifo to avoid u64 by u64 divide")
#3 lore raw
In reply to Jocelyn Falempe
Quoted 18 lines · Jocelyn Falempe

On 32bits ARM, u64 divided by a constant is not optimized to a multiply by inverse by the compiler [1].
So do the multiply by inverse explicitly for this architecture.

Fixes 675008f196ca ("drm/panic: Use a decimal fifo to avoid u64 by u64 divide")
---
 drivers/gpu/drm/drm_panic_qr.rs | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_panic_qr.rs +0 -0
index dd55b1cb764d..774a17de4f2f 100644

drivers/gpu/drm/drm_panic_qr.rs struct DecFifo { · line 386

Quoted 6 lines
drivers/gpu/drm/drm_panic_qr.rs +3 -0
@@ -381,6 +381,26 @@ struct DecFifo {
     len: usize,
 }
 
+// On arm32 architecture, dividing an `u64` by a constant will generate a call
+// to `__aeabi_uldivmod` which is not present in the kernel.
+// So use the multiply by inverse method for this architecture.

I think the problem here is the u64 by u64 division. u64 by u32 should work. Unfortunately Rust doesn't seem to have a way to perform a mixed-type division. We already have optimized C/ASM helpers for u64 divisions. For example div_u64() does such an u64 by u32 division.
While it may be slower than the inverse multiplication, it is less code, easier to understand and will work for all architectures automatically.

Quoted 34 lines
drivers/gpu/drm/drm_panic_qr.rs +18 -1
+fn div10(val: u64) -> u64 {
+    if cfg!(target_arch = "arm") {
+        let val_h = val >> 32;
+        let val_l = val & 0xFFFFFFFF;
+        let b_h: u64 = 0x66666666;
+        let b_l: u64 = 0x66666667;
+
+        let tmp1 = val_h * b_l + ((val_l * b_l) >> 32);
+        let tmp2 = val_l * b_h + (tmp1 & 0xffffffff);
+        let tmp3 = val_h * b_h + (tmp1 >> 32) + (tmp2 >> 32);
+
+        tmp3 >> 2
+    } else {
+        val / 10
+    }
+}
+
 impl DecFifo {
     fn push(&mut self, data: u64, len: usize) {
         let mut chunk = data;
@@ -389,7 +409,7 @@ fn push(&mut self, data: u64, len: usize) {
         }
         for i in 0..len {
             self.decimals[i] = (chunk % 10) as u8;
-            chunk /= 10;
+            chunk = div10(chunk);
         }
         self.len += len;
     }

base-commit: 3529cb5ab16b4f1f8bbc31dc39a1076a94bd1e38

Signature · 1 line
2.49.0