From f5ba1b40779d74afac9822b7f4d81742b552e8c9 Mon Sep 17 00:00:00 2001 From: Daniel_Cortez Date: Tue, 17 Oct 2017 14:54:38 +0700 Subject: [PATCH] Improved itoh() implementation --- source/compiler/sc2.c | 40 ++++++++++++---------------------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/source/compiler/sc2.c b/source/compiler/sc2.c index 2f4befc..2a13c15 100644 --- a/source/compiler/sc2.c +++ b/source/compiler/sc2.c @@ -3156,33 +3156,17 @@ SC_FUNC int getlabel(void) */ SC_FUNC char *itoh(ucell val) { - const char *hex = "0123456789abcdef"; - static char itohstr[30]; - char *ptr; - int i,nibble[16]; /* a 64-bit hexadecimal cell has 16 nibbles */ - int max; + static const char hex[16]= + { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; + static char itohstr[17]= + { '\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0' }; + char *ptr=&itohstr[15]; - #if PAWN_CELL_SIZE==16 - max=4; - #elif PAWN_CELL_SIZE==32 - max=8; - #elif PAWN_CELL_SIZE==64 - max=16; - #else - #error Unsupported cell size - #endif - ptr=itohstr; - for (i=0; i>=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; +#if PAWN_CELL_SIZE>64 + #error Unsupported cell size +#endif + do { + *ptr-- = hex[val&(ucell)0x0f]; + } while ((val>>=4)!=0); + return ptr+1; }