cmd: mtd: benchmark: use lldiv() instead of 64-bit division
authorMikhail Kshevetskiy <mikhail.kshevetskiy@iopsys.eu>
Sat, 1 Nov 2025 06:24:23 +0000 (09:24 +0300)
committerMichael Trimarchi <michael@amarulasolutions.com>
Tue, 18 Nov 2025 19:06:21 +0000 (20:06 +0100)
As was noted by Heinrich Schuchardt, some SoCs may not support 64-bit
divisions. Fix an issue by using lldiv() instead.

The code assumes that the benchmark never takes more than 4294 seconds
and thus the difference will be less than U32_MAX.

Also replace (speed / 1024) by (speed >> 10) to avoid potential 64-bit
division.

Signed-off-by: Mikhail Kshevetskiy <mikhail.kshevetskiy@iopsys.eu>
cmd/mtd.c

index d007212..7f25144 100644 (file)
--- a/cmd/mtd.c
+++ b/cmd/mtd.c
@@ -10,6 +10,7 @@
 
 #include <command.h>
 #include <console.h>
+#include <div64.h>
 #include <led.h>
 #if CONFIG_IS_ENABLED(CMD_MTD_OTP)
 #include <hexdump.h>
@@ -595,10 +596,10 @@ static int do_mtd_io(struct cmd_tbl *cmdtp, int flag, int argc,
 
        if (benchmark && bench_start) {
                bench_end = timer_get_us();
-               speed = (len * 1000000) / (bench_end - bench_start);
+               speed = lldiv(len * 1000000, bench_end - bench_start);
                printf("%s speed: %lukiB/s\n",
                       read ? "Read" : "Write",
-                      (unsigned long)(speed / 1024));
+                      (unsigned long)(speed >> 10));
        }
 
        led_activity_off();