/*
C:\Program Files\dmc8.50\dmc\dm\bin\code>hex
hexchartodec Chars:1,15,13,5 time:474 cycles
hexchartodec2 Chars:1,15,13,5 time:1195 cycles
hexchartodec3 Chars:1,15,13,5 time:85 cycles
hexchartodec4 Chars:1,15,13,5 time:2024 cycles
hex2dec Chars:1,15,13,5 time:7266 cycles
hexchartodec3m Chars:1,15,13,5 time:190 cycles
hexToDec Chars:1,15,13,5 time:1904 cycles
hexchartodec3m2 Chars:1,15,13,5 time:97 cycles
hexToDecChar Chars:1,15,13,5 time:1622 cycles
macroHexToDecChar Chars:1,15,13,5 time:656 cycles
strtoldec Chars:1,15,13,5 time:7642 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))
#define hexchartodec3m(x) ((((x&64)
>>3)+((x&64)
>>6)+x)&15)
unsigned int
hexToDec(unsigned char x)
{
return ( (x & 0xf) * ((x & 0x10) >> 4))
+ (((x & 0xf) + 9) * ((x & 0x40) >> 6));
}
char hexToDecChar(char x)
{
return ( (x & 0xf) * ((x & 0x10) >> 4))
+ (((x & 0xf) + 9) * ((x & 0x40) >> 6));
}
#define macroHexToDecChar(x) ( (x & 0xf) * ((x & 0x10) >> 4)) + (((x & 0xf) + 9) * ((x & 0x40) >> 6))
#define hexchartodec3m2(x) ((((x
>>6)<<3)+(x
>>6)+x)&15)
#define timefunc(F) ;start=rdtsc();n1=F('1');n2=F('f');n3=F('d');n4=F('5');end=rdtsc();printf("%s Chars:%d,%d,%d,%d time:%llu cycles\n",#F,n1,n2,n3,n4,(end-start));
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;
}
}
long int strtoldec(char c){
return strtol(&c,NULL,16);
}
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,end;//rdtsc_delay ~85 cycles;
s1 n1,n2,n3,n4;
timefunc(hexchartodec);//
>>1
timefunc(hexchartodec2);//
>>7
timefunc(hexchartodec3);//
>>20
timefunc(hexchartodec4);//
>>25
timefunc(hex2dec);//
>>34
timefunc(hexchartodec3m);//
>>45
timefunc(hexToDec);//
>>47
timefunc(hexchartodec3m2);//
>>50
timefunc(hexToDecChar);
timefunc(macroHexToDecChar);
timefunc(strtoldec);//
>>82
}