Re: [PATCH v3 1/4] lib/string_helpers: change blk_size to u32 for string_get_size() interface

From: Rasmus Villemoes
Date: Tue Nov 03 2015 - 15:57:57 EST


On Tue, Nov 03 2015, James Bottomley <jbottomley@xxxxxxxx> wrote:

>
> It was a suggestion when I explained what the missing sources of
> precision were, I don't think it's really a suggestion when it comes
> with an exemplary patch.

exÂemÂplaÂry
adjective

1.
serving as a desirable model; representing the best of its kind.

Said exemplary patch produces "1.10 KiB" for size=2047,
blk_size=1. (This is caused by the introduction of rounding, and is
probably fixable.)

James, I do understand the algorithm you're trying to use. What I don't
understand is why you insist on using the approach of reducing size and
blk_size all the way before multiplying them. It seems much simpler to
just reduce them till they're below U32_MAX (not keeping track of any
remainders at that point), multiply them, and then proceed as usual,
This avoids having to deal with weird cross-multiplication terms, gives
more accurate results (yes, I tested that) and avoids the extra 64/32
division you introduce by decrementing i.

Rasmus

To be precise, the body I suggest is

while (blk_size > U32_MAX) {
do_div(blk_size, divisor[units]);
i++;
}
while (size > U32_MAX) {
do_div(size, divisor[units]);
i++;
}
size *= blk_size;
while (size > divisor[units]) {
remainder = do_div(size, divisor[units]);
i++;
}

whether the last one should be > or >= is debatable; I think 1024 KiB is
better than 1.00 MiB.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/