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: F r o z e n V o i d !!mJCwdV5J0Xy2A21 2012-01-07 3:00

/*
C:\Program Files\dmc8.50\dmc\dm\bin\code>hex
hexchartodec(Post >>1) Chars:1,15,13,5 time:584 cycles
hexchartodec2(Post >>7) Chars:1,15,13,5 time:1243 cycl
hexchartodec3(Post >>20) Chars:1,15,13,5 time:90 cycle
hexchartodec4(Post >>25) Chars:1,15,13,5 time:3772 cyc
hex2dec(Post >>34) Chars:1,15,13,5 time:5921 cycles
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#define u1 unsigned char
#define u2 unsigned short
#define u4 unsigned int
#define u8 unsigned long long
#define s1 signed char
#define s2 signed short
#define s4 signed int
#define s8 signed long long
#define f2 short float
#define f4 float
#define f8 double
#define f10 long double
#define hexchartodec3(x) (((x>>6)+((x>>6)<<3))+(x&0xf))


int hex2dec(int c) {
    static const char *s = "0123456789ABCDEF";
    char *p;
    if (p = memchr(s, toupper(c), 16))
        return p - s;
    else {
        errno = EDOM;
        return -1;
    }
}




u8 inline rdtsc(){__asm{RDTSC}}
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];
}
char hexchartodec2(const char c) {
    char d = c - '0';
    /* This if kind of means c >= '@', the compiler can't assume that c < 128
     * (i also can't do that, but i don't give a fuck) */
    if (c & 0x40) { //c to avoid a pipeline stall (we will have more below)
        /* The point here is that ('1'+16='A')+32='a'.*/
        --d; //or maybe d = c - '1';
        d &= 0xf; /* from the alignment of the ascii table, the least
                   * significant decimal is ready */
        d += 10;
    }
    return d;
}
char  hexchartodec4(char i){
switch(i){//since multiple IFS in chain are retarded
case '0':;return 0;
case '1':;return 1;
case '2':;return 2;
case '3':;return 3;
case '4':;return 4;
case '5':;return 5;
case '6':;return 6;
case '7':;return 7;
case '8':;return 8;
case '9':;return 9;
case 'a':;return 10;
case 'b':;return 11;
case 'c':;return 12;
case 'd':;return 13;
case 'e':;return 14;
case 'f':;return 15;
default:return 0;
}


}
main(s4 argc,s1**argv){
u8 start=rdtsc(),end=rdtsc();//rdtsc_delay ~85 cycles;


s1 n1,n2,n3,n4;
//hexchartodec   
start=rdtsc();
n1=hexchartodec('1');
n2=hexchartodec('f');
n3=hexchartodec('d');
n4=hexchartodec('5');
end=rdtsc();
printf("hexchartodec(Post >>1) Chars:%d,%d,%d,%d time:%llu cycles\n",n1,n2,n3,n4,(end-start));
//hexchartodec2
start=rdtsc();
n2=hexchartodec2('1');
n1=hexchartodec2('f');
n4=hexchartodec2('d');
n3=hexchartodec2('5');
end=rdtsc();
printf("hexchartodec2(Post >>7) Chars:%d,%d,%d,%d time:%llu cycles\n",n2,n1,n4,n3,(end-start));
//hexchartodec3
start=rdtsc();
n3=hexchartodec3('1');
n2=hexchartodec3('f');
n4=hexchartodec3('d');
n1=hexchartodec3('5');
end=rdtsc();
printf("hexchartodec3(Post >>20) Chars:%d,%d,%d,%d time:%llu cycles\n",n3,n2,n4,n1,(end-start));
//hexchartodec4start=rdtsc();
start=rdtsc();
n3=hexchartodec4('1');
n2=hexchartodec4('f');
n4=hexchartodec4('d');
n1=hexchartodec4('5');
end=rdtsc();
printf("hexchartodec4(Post >>25) Chars:%d,%d,%d,%d time:%llu cycles\n",n3,n2,n4,n1,(end-start));
//hex2dec
start=rdtsc();
n4=hex2dec('1');
n1=hex2dec('f');
n2=hex2dec('d');
n3=hex2dec('5');
end=rdtsc();
printf("hex2dec(Post >>34) Chars:%d,%d,%d,%d time:%llu cycles\n",n4,n1,n2,n3,(end-start));



}

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