Fix qemu_strtosz testsuite failures for i686.

-----BEGIN PGP SIGNATURE-----
 
 iQFRBAABCgA7FiEEekgeeIaLTbaoWgXAZN846K9+IV8FAmBPrCgdHHJpY2hhcmQu
 aGVuZGVyc29uQGxpbmFyby5vcmcACgkQZN846K9+IV+x8Qf7BWgal1oIwkDaDIum
 Z66Bkvli52NdUlmka+BiMdW0y5b5cE+8uzW+3maZ40E1im72To+emitt36GlpQFn
 xvKxRp5E8MuwIMq8p/cMKfecKkUgjoUgbqW19dzAsgemmo0+c1vhQzXc/UG3LqLQ
 +hvnqW+sG/G+fa49Rfh7sz0LZ7HOlAwk/R/fX7G7tuuQZoI9rk/z2qvuK43pNwsE
 ncpaPf3uOIJALQKhiy3O9t55q4HBJRclZcYsQa4L2wtwU3t4Xxssz/Dm1HbB46eg
 8wvtaZG8GcW3hMQyssJpg0STK1I56dmNmVFbz4TZshvAyQhtoLVmkOif6qSS21aJ
 yXou8w==
 =KWnm
 -----END PGP SIGNATURE-----

Merge remote-tracking branch 'remotes/rth-gitlab/tags/pull-misc-20210315' into staging

Fix qemu_strtosz testsuite failures for i686.

# gpg: Signature made Mon 15 Mar 2021 18:49:12 GMT
# gpg:                using RSA key 7A481E78868B4DB6A85A05C064DF38E8AF7E215F
# gpg:                issuer "richard.henderson@linaro.org"
# gpg: Good signature from "Richard Henderson <richard.henderson@linaro.org>" [full]
# Primary key fingerprint: 7A48 1E78 868B 4DB6 A85A  05C0 64DF 38E8 AF7E 215F

* remotes/rth-gitlab/tags/pull-misc-20210315:
  utils: Use fixed-point arithmetic in qemu_strtosz

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
Peter Maydell 2021-03-16 21:08:07 +00:00
commit 0693602a23
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";
err = qemu_strtosz(str, &endptr, &res);
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);
}

View File

@ -275,10 +275,9 @@ static int do_strtosz(const char *nptr, const char **end,
int retval;
const char *endptr, *f;
unsigned char c;
bool mul_required = false, hex = false;
uint64_t val;
bool hex = false;
uint64_t val, valf = 0;
int64_t mul;
double fraction = 0.0;
/* Parse integral portion as decimal. */
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
* the entire input as invalid instead.
*/
double fraction;
f = endptr;
retval = qemu_strtod_finite(f, &endptr, &fraction);
if (retval) {
fraction = 0.0;
endptr++;
} else if (memchr(f, 'e', endptr - f) || memchr(f, 'E', endptr - f)) {
endptr = nptr;
retval = -EINVAL;
goto out;
} else if (fraction != 0) {
mul_required = true;
} else {
/* Extract into a 64-bit fixed-point fraction. */
valf = (uint64_t)(fraction * 0x1p64);
}
}
c = *endptr;
@ -333,16 +334,35 @@ static int do_strtosz(const char *nptr, const char **end,
mul = suffix_mul(default_suffix, unit);
assert(mul > 0);
}
if (mul == 1 && mul_required) {
endptr = nptr;
retval = -EINVAL;
goto out;
if (mul == 1) {
/* When a fraction is present, a scale is required. */
if (valf != 0) {
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;
goto out;
}
*result = val * mul + (uint64_t) (fraction * mul);
*result = val;
retval = 0;
out: