Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1433895 > unrolled thread
| Started by | zengzhaoxiu@163.com |
|---|---|
| First post | 2016-06-29 18:30 +0200 |
| Last post | 2016-06-30 16:50 +0200 |
| Articles | 3 — 3 participants |
Back to article view | Back to linux.kernel
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
[PATCH 2/2] lib: kstrtox: _parse_integer: use hex_to_bin instead local conversion, and reduce branches zengzhaoxiu@163.com - 2016-06-29 18:30 +0200
Re: [PATCH 2/2] lib: kstrtox: _parse_integer: use hex_to_bin instead local conversion, and reduce branches Alexey Dobriyan <adobriyan@gmail.com> - 2016-06-30 00:10 +0200
Re: [PATCH 2/2] lib: kstrtox: _parse_integer: use hex_to_bin instead local conversion, and reduce branches Zhaoxiu Zeng <zengzhaoxiu@163.com> - 2016-06-30 16:50 +0200
| From | zengzhaoxiu@163.com |
|---|---|
| Date | 2016-06-29 18:30 +0200 |
| Subject | [PATCH 2/2] lib: kstrtox: _parse_integer: use hex_to_bin instead local conversion, and reduce branches |
| Message-ID | <rPqxb-6tq-3@gated-at.bofh.it> |
From: Zhaoxiu Zeng <zhaoxiu.zeng@gmail.com>
Signed-off-by: Zhaoxiu Zeng <zhaoxiu.zeng@gmail.com>
---
lib/kstrtox.c | 22 +++++-----------------
1 file changed, 5 insertions(+), 17 deletions(-)
diff --git a/lib/kstrtox.c b/lib/kstrtox.c
index d8a5cf6..70d3374 100644
--- 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) {
/*
* 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;
}
res = res * base + val;
rv++;
- s++;
}
*p = res;
- if (overflow)
- rv |= KSTRTOX_OVERFLOW;
+ rv |= overflow;
return rv;
}
--
2.7.4
[toc] | [next] | [standalone]
| From | Alexey Dobriyan <adobriyan@gmail.com> |
|---|---|
| Date | 2016-06-30 00:10 +0200 |
| Subject | Re: [PATCH 2/2] lib: kstrtox: _parse_integer: use hex_to_bin instead local conversion, and reduce branches |
| Message-ID | <rPvQd-1mB-9@gated-at.bofh.it> |
| In reply to | #1433895 |
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;
> }
[toc] | [prev] | [next] | [standalone]
| From | Zhaoxiu Zeng <zengzhaoxiu@163.com> |
|---|---|
| Date | 2016-06-30 16:50 +0200 |
| Subject | Re: [PATCH 2/2] lib: kstrtox: _parse_integer: use hex_to_bin instead local conversion, and reduce branches |
| Message-ID | <rPLrX-2lA-11@gated-at.bofh.it> |
| In reply to | #1434079 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web