Commit 2c10d3e6 authored by Nikolaus Schulz's avatar Nikolaus Schulz Committed by Ben Hutchings

iio: core: Fix IIO_VAL_FRACTIONAL_LOG2 for negative values

commit 7fd6592d upstream.

Fix formatting of negative values of type IIO_VAL_FRACTIONAL_LOG2 by
switching from do_div(), which can't handle negative numbers, to
div_s64_rem().  Also use shift_right for shifting, which is safe with
negative values.
Signed-off-by: default avatarNikolaus Schulz <nikolaus.schulz@avionic-design.de>
Reviewed-by: default avatarLars-Peter Clausen <lars@metafoo.de>
Signed-off-by: default avatarJonathan Cameron <jic23@kernel.org>
[bwh: Backported to 3.16:
 - Use vals[] instead of tmp{0,1}
 - Keep using sprintf()]
Signed-off-by: default avatarBen Hutchings <ben@decadent.org.uk>
parent bc11e0dc
......@@ -406,10 +406,9 @@ ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
vals[0] = (int)div_s64_rem(tmp, 1000000000, &vals[1]);
return sprintf(buf, "%d.%09u\n", vals[0], abs(vals[1]));
case IIO_VAL_FRACTIONAL_LOG2:
tmp = (s64)vals[0] * 1000000000LL >> vals[1];
vals[1] = do_div(tmp, 1000000000LL);
vals[0] = tmp;
return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
vals[0] = (int)div_s64_rem(tmp, 1000000000LL, &vals[1]);
return sprintf(buf, "%d.%09u\n", vals[0], abs(vals[1]));
case IIO_VAL_INT_MULTIPLE:
{
int i;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment