public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* hex_to_bin speedup
@ 2010-05-27 10:07 Fredrik Gustafsson
       [not found] ` <AANLkTinn7aEv0ueW4uRN06XdUbEltTfmjRXEABMRN-7e@mail.gmail.com>
  0 siblings, 1 reply; 4+ messages in thread
From: Fredrik Gustafsson @ 2010-05-27 10:07 UTC (permalink / raw)
  To: linux-kernel

Hi,
I looked a bit at the newly added hex_to_bin function in lib/hexdump.c.
I do believe this is a speedup (at least according to my benchmark[1]).

However in the comments to commit 
903788892ea0fc7fcaf7e8e5fac9a77379fc215b you can read
"[akpm@linux-foundation.org: use tolower(), saving 3 bytes, test the more common case first - it's quicker]"

I don't know the change that akpm has done, so I'm unsure if there's any
problems that I miss with my patch.

--
iveqy

[1] Benchmark

#include <sys/time.h>
#include <stdio.h>

int main()
{
	struct timeval start, end;
	int i,itr;
	itr = 100000;
	char c = 'A';
	time_t diff;
	gettimeofday(&start,NULL);
	for(i = 0; i < itr; i++) {
		if((c >= 'A') && (c <= 'F')) {}
	}
	gettimeofday(&end,NULL);
	diff = end.tv_usec - start.tv_usec;
	printf("if-statement: %d ms\n",diff);
	gettimeofday(&start,NULL);
	for(i = 0; i < itr; i++) {
		c = tolower(c);
	}
	gettimeofday(&end,NULL);
	diff = end.tv_usec - start.tv_usec;
	printf("tolower(): %d ms\n",diff);
}

[2] Patch

This is faster (at least on i686).
---
 lib/hexdump.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/lib/hexdump.c b/lib/hexdump.c
index 5d7a480..f01d11c 100644
--- a/lib/hexdump.c
+++ b/lib/hexdump.c
@@ -26,9 +26,10 @@ int hex_to_bin(char ch)
 {
 	if ((ch >= '0') && (ch <= '9'))
 		return ch - '0';
-	ch = tolower(ch);
 	if ((ch >= 'a') && (ch <= 'f'))
 		return ch - 'a' + 10;
+	if ((ch >= 'A') && (ch <= 'F'))
+		return ch - 'A' + 10;
 	return -1;
 }
 EXPORT_SYMBOL(hex_to_bin);

^ permalink raw reply related	[flat|nested] 4+ messages in thread
* Re: hex_to_bin speedup
@ 2010-05-28 21:38 George Spelvin
  0 siblings, 0 replies; 4+ messages in thread
From: George Spelvin @ 2010-05-28 21:38 UTC (permalink / raw)
  To: andy.shevchenko, iveqy; +Cc: linux, linux-kernel

1) First of all, I'd worry about code size far more than speed.
   this is not fast-path code.
2) Second, given that you're already doing a range test,
   the fastest way to perform a tolower() is "c |= 0x20".

Generally it's something like:
int hex_to_bin(char ch)
{
	ch -= '0';
	if ((unsigned char)ch <= 9)
		return ch;
	ch |= 0x20;
	ch -= 'a' - '0';
	if ((unsigned char)ch <= 6)
		return ch+10
	return -1;
}


that produces the even smaller code:
hex_to_bin_or:
        movb    4(%esp), %dl
        subl    $48, %edx
        cmpb    $9, %dl
        movsbl  %dl,%eax
        jbe     .L10
        orl     $32, %edx
        orl     $-1, %eax
        subl    $49, %edx
        cmpb    $6, %dl
        ja      .L10
        movsbl  %dl,%eax
        addl    $10, %eax
.L10:
        ret


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-05-28 21:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-05-27 10:07 hex_to_bin speedup Fredrik Gustafsson
     [not found] ` <AANLkTinn7aEv0ueW4uRN06XdUbEltTfmjRXEABMRN-7e@mail.gmail.com>
     [not found]   ` <20100527142848.GA23543@iveqy.com>
2010-05-27 16:35     ` Andy Shevchenko
2010-05-28 21:05       ` Fredrik Gustafsson
  -- strict thread matches above, loose matches on Subject: below --
2010-05-28 21:38 George Spelvin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox