lib/string_helpers.c:string_get_size(): use 32 bit arithmetic when possible
authorRasmus Villemoes <linux@rasmusvillemoes.dk>
Thu, 12 Feb 2015 23:01:48 +0000 (15:01 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Fri, 13 Feb 2015 02:54:13 +0000 (18:54 -0800)
The remainder from do_div is always a u32, and after size has been reduced
to be below 1000 (or 1024), it certainly fits in u32.  So both remainder
and sf_cap can be made u32s, the format specifiers can be simplified (%lld
wasn't the right thing to use for _unsigned_ long long anyway), and we can
replace a do_div with an ordinary 32/32 bit division.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
lib/string_helpers.c

index 0d25f7aa732cdcc3378994bda7e37fcb7047d51e..2b3757f84b3b6cc1742fc3c1027db805f2073388 100644 (file)
@@ -42,7 +42,7 @@ int string_get_size(u64 size, const enum string_size_units units,
                [STRING_UNITS_2] = 1024,
        };
        int i, j;
-       u64 remainder = 0, sf_cap;
+       u32 remainder = 0, sf_cap;
        char tmp[8];
 
        tmp[0] = '\0';
@@ -59,14 +59,13 @@ int string_get_size(u64 size, const enum string_size_units units,
 
                if (j) {
                        remainder *= 1000;
-                       do_div(remainder, divisor[units]);
-                       snprintf(tmp, sizeof(tmp), ".%03lld",
-                                (unsigned long long)remainder);
+                       remainder /= divisor[units];
+                       snprintf(tmp, sizeof(tmp), ".%03u", remainder);
                        tmp[j+1] = '\0';
                }
        }
 
-       snprintf(buf, len, "%lld%s %s", (unsigned long long)size,
+       snprintf(buf, len, "%u%s %s", (u32)size,
                 tmp, units_str[units][i]);
 
        return 0;