Skip to content

Commit

Permalink
lib/div64.c: off by one in shift
Browse files Browse the repository at this point in the history
fls counts bits starting from 1 to 32 (returns 0 for zero argument).  If
we add 1 we shift right one bit more and loose precision from divisor,
what cause function incorect results with some numbers.

Corrected code was tested in user-space, see bugzilla:
   https://bugzilla.kernel.org/show_bug.cgi?id=202391

Link: http://lkml.kernel.org/r/[email protected]
Fixes: 658716d ("div64_u64(): improve precision on 32bit platforms")
Signed-off-by: Stanislaw Gruszka <[email protected]>
Reported-by: Siarhei Volkau <[email protected]>
Tested-by: Siarhei Volkau <[email protected]>
Acked-by: Oleg Nesterov <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Stanislaw Gruszka authored and torvalds committed Mar 8, 2019
1 parent 1db604f commit cdc94a3
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/div64.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
quot = div_u64_rem(dividend, divisor, &rem32);
*remainder = rem32;
} else {
int n = 1 + fls(high);
int n = fls(high);
quot = div_u64(dividend >> n, divisor >> n);

if (quot != 0)
Expand Down Expand Up @@ -147,7 +147,7 @@ u64 div64_u64(u64 dividend, u64 divisor)
if (high == 0) {
quot = div_u64(dividend, divisor);
} else {
int n = 1 + fls(high);
int n = fls(high);
quot = div_u64(dividend >> n, divisor >> n);

if (quot != 0)
Expand Down

0 comments on commit cdc94a3

Please sign in to comment.