Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Fastest way to single hex digit => decimal

Name: Anonymous 2012-01-06 8:46

char hexchartodec_map[256] = {
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  0,  0,  0,  0,  0,  0,
        0,  10, 11, 12, 13, 14, 15, 0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  10, 11, 12, 13, 14, 15, 0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
};

char hexchartodec(char i) {
        return hexchartodec_map[i];
}

Name: Cudder !MhMRSATORI!FBeUS42x4uM+kgp 2012-01-07 6:28

>>36
Your compiler and CPU is horrible.

I had to redefine >>20 as a function to stop my compiler from simply substituting in the values, since they're all constants. With even first-level optimizations all of them take ~18 cycles (the RDTSC latency on my CPU) since the compiler optimizes away the code entirely. I also added a loop to repeat each one 1048576 (1M) times. These are all without optimization:

rdtsc latency: 27 cycles
hexchartodec(Post >>1) Chars:1,15,13,5 time:18016572 cycles/1M = 17.18 cycles/iteration
hexchartodec2(Post >>7) Chars:1,15,13,5 time:27049857 cycles/1M = 25.80 cycles/iteration
hexchartodec3(Post >>20) Chars:1,15,13,5 time:30027546 cycles/1M = 28.64 cycles/iteration
hexchartodec4(Post >>25) Chars:1,15,13,5 time:33271920 cycles/1M = 31.73 cycles/iteration
hex2dec(Post >>34) Chars:1,15,13,5 time:369429444 cycles/1M = 352.32 cycles/iteration
hexchartodec5(mine) Chars:1,15,13,5 time:14012856 cycles/1M = 13.36 cycles/iteration


Here's mine:

and al, 4fh
mov dl, al
mov cl, al
shr dl, 6
shr cl, 3
add al, dl
add al, cl
and al, 15

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List