mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alexey Dobriyan <adobriyan@gmail.com>
To: zengzhaoxiu@163.com
Cc: linux-kernel@vger.kernel.org,
	Zhaoxiu Zeng <zhaoxiu.zeng@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Kees Cook <keescook@chromium.org>
Subject: Re: [PATCH 2/2] lib: kstrtox: _parse_integer: use hex_to_bin instead local conversion, and reduce branches
Date: Thu, 30 Jun 2016 01:06:11 +0300	[thread overview]
Message-ID: <20160629220611.GA13735@p183.telecom.by> (raw)
In-Reply-To: <1467217334-58990-1-git-send-email-zengzhaoxiu@163.com>

On Thu, Jun 30, 2016 at 12:22:13AM +0800, zengzhaoxiu@163.com wrote:
> --- a/lib/kstrtox.c
> +++ b/lib/kstrtox.c
> @@ -48,38 +48,26 @@ unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long
>  {
>  	unsigned long long res;
>  	unsigned int rv;
> -	int overflow;
> +	unsigned int overflow;
> +	unsigned int val;
>  
>  	res = 0;
>  	rv = 0;
>  	overflow = 0;
> -	while (*s) {
> -		unsigned int val;
> -
> -		if ('0' <= *s && *s <= '9')
> -			val = *s - '0';
> -		else if ('a' <= _tolower(*s) && _tolower(*s) <= 'f')
> -			val = _tolower(*s) - 'a' + 10;
> -		else
> -			break;
> -
> -		if (val >= base)
> -			break;
> +	while ((val = hex_to_bin(*s++)) < base) {

I hate this function. And it has a branch if your table patch doesn't
go it. And it is beartrap (unsigned int = -1 < base).

ACK *s++ bit, though. Should make code smaller in my experience.
Please, change to "unsigned char c; while ((c = *s++)".
This is about maximum code compression I can understand.

>  		/*
>  		 * 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 = KSTRTOX_OVERFLOW;

Just do |= KSTRTOX_OVERFLOW here directly, it is the leftmost bit.

>  		}
>  		res = res * base + val;
>  		rv++;
> -		s++;
>  	}
>  	*p = res;
> -	if (overflow)
> -		rv |= KSTRTOX_OVERFLOW;
> +	rv |= overflow;
>  	return rv;
>  }

  reply	other threads:[~2016-06-29 22:06 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-29 16:15 [PATCH 1/2] lib: hexdump: use a look-up table to do hex_to_bin zengzhaoxiu
2016-06-29 16:22 ` [PATCH 2/2] lib: kstrtox: _parse_integer: use hex_to_bin instead local conversion, and reduce branches zengzhaoxiu
2016-06-29 22:06   ` Alexey Dobriyan [this message]
2016-06-30 14:45     ` Zhaoxiu Zeng
2016-06-29 16:22 ` [PATCH 1/2] lib: hexdump: use a look-up table to do hex_to_bin Andy Shevchenko
2016-06-29 16:24 ` Steven Rostedt
2016-06-29 18:31 ` Michal Nazarewicz
2016-06-29 18:52   ` Andy Shevchenko
2016-06-30 19:26     ` Joe Perches
2016-06-30 19:42       ` Geert Uytterhoeven
2016-06-30 20:06         ` Joe Perches
2016-06-30 20:44           ` Andy Shevchenko
2016-06-30 21:18       ` Michal Nazarewicz
2016-06-30 14:21   ` Zhaoxiu Zeng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160629220611.GA13735@p183.telecom.by \
    --to=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=zengzhaoxiu@163.com \
    --cc=zhaoxiu.zeng@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®