提交 690d137f 编写于 作者: D David Howells 提交者: Linus Torvalds

Reduce the number of expensive division instructions done by _parse_integer()

_parse_integer() does one or two division instructions (which are slow)
per digit parsed to perform the overflow check.

Furthermore, these are particularly expensive examples of division
instruction as the number of clock cycles required to complete them may
go up with the position of the most significant set bit in the dividend:

	if (*res > div_u64(ULLONG_MAX - val, base))

which is as maximal as possible.

Worse, on 32-bit arches, more than one of these division instructions
may be required per digit.

So, assuming we don't support a base of more than 16, skip the check if the
top nibble of the result is not set at this point.
Signed-off-by: NDavid Howells <dhowells@redhat.com>
[ Changed it to not dereference the pointer all the time - even if the
  compiler can and does optimize it away, the code just looks cleaner.
  And edited the top nybble test slightly to make the code generated on
  x86-64 better in the loop - test against a hoisted constant instead of
  shifting and testing the result ]
Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
上级 d65b4e98
...@@ -44,12 +44,13 @@ const char *_parse_integer_fixup_radix(const char *s, unsigned int *base) ...@@ -44,12 +44,13 @@ const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
* *
* Don't you dare use this function. * Don't you dare use this function.
*/ */
unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *res) unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p)
{ {
unsigned long long res;
unsigned int rv; unsigned int rv;
int overflow; int overflow;
*res = 0; res = 0;
rv = 0; rv = 0;
overflow = 0; overflow = 0;
while (*s) { while (*s) {
...@@ -64,12 +65,19 @@ unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long ...@@ -64,12 +65,19 @@ unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long
if (val >= base) if (val >= base)
break; break;
if (*res > div_u64(ULLONG_MAX - val, base)) /*
* Check for overflow only if we are within range of
* it in the max base we support (16)
*/
if (unlikely(res & (~0ull << 60))) {
if (res > div_u64(ULLONG_MAX - val, base))
overflow = 1; overflow = 1;
*res = *res * base + val; }
res = res * base + val;
rv++; rv++;
s++; s++;
} }
*p = res;
if (overflow) if (overflow)
rv |= KSTRTOX_OVERFLOW; rv |= KSTRTOX_OVERFLOW;
return rv; return rv;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册