utils: Use fixed-point arithmetic in qemu_strtosz

Once we've parsed the fractional value, extract it into an integral
64-bit fraction.  Perform the scaling with integer arithmetic, and
simplify the overflow detection.

Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Message-Id: <20210315185117.1986240-2-richard.henderson@linaro.org>
This commit is contained in:
Richard Henderson 2021-03-15 12:51:17 -06:00 committed by Alex Bennée
parent 5b7f5586d1
commit 6567ba0c60
2 changed files with 36 additions and 16 deletions

View File

@ -2128,7 +2128,7 @@ static void test_qemu_strtosz_float(void)
str = "12.345M"; str = "12.345M";
err = qemu_strtosz(str, &endptr, &res); err = qemu_strtosz(str, &endptr, &res);
g_assert_cmpint(err, ==, 0); g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, (uint64_t) (12.345 * MiB)); g_assert_cmpint(res, ==, (uint64_t) (12.345 * MiB + 0.5));
g_assert(endptr == str + 7); g_assert(endptr == str + 7);
} }

View File

@ -275,10 +275,9 @@ static int do_strtosz(const char *nptr, const char **end,
int retval; int retval;
const char *endptr, *f; const char *endptr, *f;
unsigned char c; unsigned char c;
bool mul_required = false, hex = false; bool hex = false;
uint64_t val; uint64_t val, valf = 0;
int64_t mul; int64_t mul;
double fraction = 0.0;
/* Parse integral portion as decimal. */ /* Parse integral portion as decimal. */
retval = qemu_strtou64(nptr, &endptr, 10, &val); retval = qemu_strtou64(nptr, &endptr, 10, &val);
@ -308,17 +307,19 @@ static int do_strtosz(const char *nptr, const char **end,
* without fractional digits. If we see an exponent, treat * without fractional digits. If we see an exponent, treat
* the entire input as invalid instead. * the entire input as invalid instead.
*/ */
double fraction;
f = endptr; f = endptr;
retval = qemu_strtod_finite(f, &endptr, &fraction); retval = qemu_strtod_finite(f, &endptr, &fraction);
if (retval) { if (retval) {
fraction = 0.0;
endptr++; endptr++;
} else if (memchr(f, 'e', endptr - f) || memchr(f, 'E', endptr - f)) { } else if (memchr(f, 'e', endptr - f) || memchr(f, 'E', endptr - f)) {
endptr = nptr; endptr = nptr;
retval = -EINVAL; retval = -EINVAL;
goto out; goto out;
} else if (fraction != 0) { } else {
mul_required = true; /* Extract into a 64-bit fixed-point fraction. */
valf = (uint64_t)(fraction * 0x1p64);
} }
} }
c = *endptr; c = *endptr;
@ -333,16 +334,35 @@ static int do_strtosz(const char *nptr, const char **end,
mul = suffix_mul(default_suffix, unit); mul = suffix_mul(default_suffix, unit);
assert(mul > 0); assert(mul > 0);
} }
if (mul == 1 && mul_required) { if (mul == 1) {
endptr = nptr; /* When a fraction is present, a scale is required. */
retval = -EINVAL; if (valf != 0) {
goto out; endptr = nptr;
retval = -EINVAL;
goto out;
}
} else {
uint64_t valh, tmp;
/* Compute exact result: 64.64 x 64.0 -> 128.64 fixed point */
mulu64(&val, &valh, val, mul);
mulu64(&valf, &tmp, valf, mul);
val += tmp;
valh += val < tmp;
/* Round 0.5 upward. */
tmp = valf >> 63;
val += tmp;
valh += val < tmp;
/* Report overflow. */
if (valh != 0) {
retval = -ERANGE;
goto out;
}
} }
if (val > (UINT64_MAX - ((uint64_t) (fraction * mul))) / mul) {
retval = -ERANGE; *result = val;
goto out;
}
*result = val * mul + (uint64_t) (fraction * mul);
retval = 0; retval = 0;
out: out: