From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932163AbcF3Oqb (ORCPT ); Thu, 30 Jun 2016 10:46:31 -0400 Received: from m12-12.163.com ([220.181.12.12]:53475 "EHLO m12-12.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932094AbcF3Oqa (ORCPT ); Thu, 30 Jun 2016 10:46:30 -0400 Subject: Re: [PATCH 2/2] lib: kstrtox: _parse_integer: use hex_to_bin instead local conversion, and reduce branches To: Alexey Dobriyan References: <1467216957-58885-1-git-send-email-zengzhaoxiu@163.com> <1467217334-58990-1-git-send-email-zengzhaoxiu@163.com> <20160629220611.GA13735@p183.telecom.by> Cc: linux-kernel@vger.kernel.org, Zhaoxiu Zeng , Andrew Morton , Kees Cook From: Zhaoxiu Zeng Message-ID: <79ee7e49-3780-5e00-528e-9814f467501a@163.com> Date: Thu, 30 Jun 2016 22:45:03 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.1.1 MIME-Version: 1.0 In-Reply-To: <20160629220611.GA13735@p183.telecom.by> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CM-TRANSID: DMCowABXhMZxMHVXa++0Aw--.15463S3 X-Coremail-Antispam: 1Uf129KBjvJXoW7Zw4xCr1kGry3tr4rXrW5ZFb_yoW8WFy7pw 47Kr48ta1YgF12krW0vw48GF1rJrWDWr97JFZxJFW5JFZ5JanagFyFg345Wa97Wrn7CFy0 vFy3Jr9rCF4FqaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07bbDGOUUUUU= X-Originating-IP: [14.215.35.116] X-CM-SenderInfo: p2hqw6xkdr5xrx6rljoofrz/1tbivxSYgFWBStyNGwAAsn Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2016/6/30 6:06, Alexey Dobriyan wrote: > 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). How about this? for (;;) { unsigned int val = hex_to_bin(*s++); if (val >= base) break; > 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. The previous tests are useless until reach the end of s. The '\0' will be caught by "if (val >= base)" too. >> /* >> * 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. I thought so too at first, but finally I decided to reserve the varaible "overflow", because this hack depend on the definition of KSTRTOX_OVERFLOW.