From: Nicolas Pitre <nico@cam.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Junio C Hamano <junkio@cox.net>, Git Mailing List <git@vger.kernel.org>
Subject: Re: [PATCH] fix signed range problems with hex conversions
Date: Wed, 30 May 2007 14:20:21 -0400 (EDT) [thread overview]
Message-ID: <alpine.LFD.0.99.0705301346580.11491@xanadu.home> (raw)
In-Reply-To: <alpine.LFD.0.98.0705301025170.26602@woody.linux-foundation.org>
On Wed, 30 May 2007, Linus Torvalds wrote:
> On Wed, 30 May 2007, Nicolas Pitre wrote:
> >
> > This patch adds a range test to hexval() in order to prevent this. Also
> > let's index the hexval_table array directly in get_sha1_hex() using
> > explicitly unsigned chars to avoid the range test producing faster
> > code.
>
> Please just make hexval() take a "unsigned char" instead, solving the
> problem in a manner that makes it much easier for the compiler to realize
> that it never needs to sign-extend the value or test the end result..
Well, I did it the way I did for two reasons, the first being that
random hexval() usage won't mask a bad value if it is passed an int
which happens to be out of range (think EOF, or better yet -208, -207,
etc).
Yet gcc appears to be smart enough to skip the test if it is passed a
value that cannot exceed the test range, like an unsigned char.
> Ie I think your patch would be better off something like the following
> instead (it would be a one-liner, but I agree that marking hexval_table
> "const" is also a good idea).
>
> With this, gcc can just generate:
>
> movzbl (%rdi), %eax
> movsbl hexval_table(%rax),%edx
> movzbl 1(%rdi), %eax
> movsbl hexval_table(%rax),%eax
> sall $4, %edx
> orl %eax, %edx
>
> for the code to generate a byte from two hex characters.
My patch already produces code that looks like that for get_sha1_hex()
on i386. But my second reason for the patch is that on ARM it
should becomes:
/* r4 = hexval_table, ip = hex */
ldrb r1, [ip], #1
ldrb r2, [ip], #1
ldrsb r0, [r4, r1]
ldrsb r3, [r4, r2]
orr r0, r3, r0, asl #4
I.e. the compiler has a greater chance to fold the post increment with
the load byte instruction as above without the need for an extra
add instruction.
Nicolas
prev parent reply other threads:[~2007-05-30 18:20 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-05-30 17:09 [PATCH] fix signed range problems with hex conversions Nicolas Pitre
2007-05-30 17:32 ` Linus Torvalds
2007-05-30 17:42 ` Linus Torvalds
2007-05-30 18:20 ` Nicolas Pitre [this message]
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=alpine.LFD.0.99.0705301346580.11491@xanadu.home \
--to=nico@cam.org \
--cc=git@vger.kernel.org \
--cc=junkio@cox.net \
--cc=torvalds@linux-foundation.org \
/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;
as well as URLs for NNTP newsgroup(s).