Improved itoh() implementation

This commit is contained in:
Daniel_Cortez 2017-10-17 14:54:38 +07:00 committed by Alex Martin
parent 8665f8b03c
commit f5ba1b4077

View File

@ -3156,33 +3156,17 @@ SC_FUNC int getlabel(void)
*/ */
SC_FUNC char *itoh(ucell val) SC_FUNC char *itoh(ucell val)
{ {
const char *hex = "0123456789abcdef"; static const char hex[16]=
static char itohstr[30]; { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
char *ptr; static char itohstr[17]=
int i,nibble[16]; /* a 64-bit hexadecimal cell has 16 nibbles */ { '\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0' };
int max; char *ptr=&itohstr[15];
#if PAWN_CELL_SIZE==16 #if PAWN_CELL_SIZE>64
max=4; #error Unsupported cell size
#elif PAWN_CELL_SIZE==32 #endif
max=8; do {
#elif PAWN_CELL_SIZE==64 *ptr-- = hex[val&(ucell)0x0f];
max=16; } while ((val>>=4)!=0);
#else return ptr+1;
#error Unsupported cell size
#endif
ptr=itohstr;
for (i=0; i<max; i+=1){
nibble[i]=(int)(val & 0x0f); /* nibble 0 is lowest nibble */
val>>=4;
} /* endfor */
i=max-1;
while (nibble[i]==0 && i>0) /* search for highest non-zero nibble */
i-=1;
while (i>=0){
*ptr++ = hex[nibble[i]];
i-=1;
} /* while */
*ptr='\0'; /* and a zero-terminator */
return itohstr;
} }