Re: Hex to NSString or NSData
Re: Hex to NSString or NSData
- Subject: Re: Hex to NSString or NSData
- From: Andreas Grosam <email@hidden>
- Date: Sun, 10 May 2009 18:55:49 +0200
On May 9, 2009, at 11:20 PM, Marcel Weiher wrote:
Hi Andreas,
that's an interesting way to do the conversion.
On May 9, 2009, at 10:50 , Andreas Grosam wrote:
int hexDigitToInt(char d)
{
int result;
switch (d) {
case '0': result = 0; break;
case '1': result = 1; break;
[snip]
case 'E': result = 14; break;
case 'F': result = 15; break;
default:
result = 0xFF;
}
return result;
}
Although it might look ugly, this is one of the fasted methods
using standard C that converts a hex digit (nibble) to an int (2
machine instructions).
OK, how do you get it to compile to just two instructions? I've
been looking at various optimization options, and as far as I can
tell it is at least doing the following (non-contiguous):
You are right - within a loop I actually got this for the whole main
function:
int main (void) {
volatile int c;
for (int i = 0; i < 100000000; ++i)
c = hexDigitToInt1(i & 255);
return 0;
}
0x00001dda <+0000> push ëp
0x00001ddb <+0001> xor íx,íx
0x00001ddd <+0003> mov %esp,ëp
0x00001ddf <+0005> sub $0x10,%esp
0x00001de2 <+0008> movsbl %dl,êx
0x00001de5 <+0011> sub $0x30,êx
0x00001de8 <+0014> cmp $0x36,êx
0x00001deb <+0017> ja 0x1dfb <main+33>
0x00001ded <+0019> jmp *0x1e8c(,êx,4)
0x00001df4 <+0026> mov $0xf,êx
0x00001df9 <+0031> jmp 0x1e66 <main+140>
0x00001dfb <+0033> mov $0xff,êx
0x00001e00 <+0038> jmp 0x1e66 <main+140>
0x00001e02 <+0040> xor êx,êx
0x00001e04 <+0042> jmp 0x1e66 <main+140>
0x00001e06 <+0044> mov $0x1,êx
0x00001e0b <+0049> jmp 0x1e66 <main+140>
...
0x00001e66 <+0140> inc íx
0x00001e67 <+0141> cmp $0x5f5e100,íx
0x00001e6d <+0147> mov êx,-0x4(ëp)
0x00001e70 <+0150> jne 0x1de2 <main+8>
0x00001e76 <+0156> leave
0x00001e77 <+0157> xor êx,êx
0x00001e79 <+0159> ret
So, I guess the average number of instructions is about 6 to 7 which
counts for conversion.
Sorry for being inaccurate.
Regards
Andreas
00001e67 subl $0x30,êx
00001e6d cmpl $0x36,êx
00001e70 jal 0x00001f67
00001e76 movl 0x00000038(ëx,êx,4),êx
00001e7d addl ëx,êx
00001e7f jmp *êx
So that's 6 instructions to set up and perform the indexed jump to
which the switch/case is being compiled.
At the jump sites, typically another 2 instructions:
00001f60 movl $0x0000000f,êx
00001f65 jmp 0x00001fd2
So that's a total of 8 instructions, with 2 taken and one typically
non-taken jump and one of the taken jumps computed, so probably not
predictable. I must be missing something obvious.
Marcel
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden