[Mybusinessbasic-devel] mybb utilities.c,1.3,1.4
Status: Alpha
Brought to you by:
mikecurry1974
From: <mik...@us...> - 2003-12-30 02:31:51
|
Update of /cvsroot/mybusinessbasic/mybb In directory sc8-pr-cvs1:/tmp/cvs-serv4175 Modified Files: utilities.c Log Message: Some Assembler Optimization by Matti Heikki Hakkinen. Nice and quick! ;) Index: utilities.c =================================================================== RCS file: /cvsroot/mybusinessbasic/mybb/utilities.c,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** utilities.c 29 Dec 2003 19:36:40 -0000 1.3 --- utilities.c 30 Dec 2003 02:31:41 -0000 1.4 *************** *** 137,146 **** short BigShort(short l) { ! byte b1, b2; ! ! b1 = l & 255; ! b2 = (l >> 8) & 255; ! ! return (b1 << 8) + b2; } --- 137,153 ---- short BigShort(short l) { ! /* byte flip for a word... no need for temp variables in asm */ ! __asm("mov ax,word [l]"); /* so store word in a register */ ! __asm("xchg al,ah"); /* swap the bytes in that register */ ! __asm("retn"); /* return that register */ ! ! /* ! byte b1, b2; ! ! b1 = l & 0xFF; ! b2 = (l >> 8) & 0xFF; ! ! return (b1 << 8) + b2; ! */ } *************** *** 161,172 **** long BigLong(long l) { ! byte b1, b2, b3, b4; ! ! b1 = l & 255; ! b2 = (l >> 8) & 255; ! b3 = (l >> 16) & 255; ! b4 = (l >> 24) & 255; ! ! return ((long)b1 << 24) + ((long)b2 << 16) + ((long)b3 << 8) + b4; } --- 168,188 ---- long BigLong(long l) { ! /* byte flip for a dword... if we had 486 for sure we could use BSWAP, but this works on 386 too (though is a bit slower) */ ! __asm("mov eax,dword [l]"); ! __asm("xchg al,ah"); /* byte flip for low word */ ! __asm("ror eax,0x10"); /* word flip for a dword */ ! __asm("xchg al,ah"); /* byte flip for high word (whichs now low) */ ! __asm("retn"); ! ! /* ! byte b1, b2, b3, b4; ! ! b1 = l & 255; ! b2 = (l >> 8) & 255; ! b3 = (l >> 16) & 255; ! b4 = (l >> 24) & 255; ! ! return ((long)b1 << 24) + ((long)b2 << 16) + ((long)b3 << 8) + b4; ! */ } *************** *** 186,190 **** float BigFloat(float l) ! { union { byte b[4]; --- 202,211 ---- float BigFloat(float l) ! { ! /* i'd recomed using ! tmp = (float)BigLong((long)l); ! instead, it does the same thing... and we don't want to waste memory too ! casting takes no execution, it just tells the compiler not to raise an error ! */ union { byte b[4]; *************** *** 203,211 **** char *strLeft(char *str,int n) { ! char *tmp; ! tmp = strdup(str); ! strncpy(tmp + n,"",abs(strlen(tmp) - n)); ! ! return tmp; } --- 224,249 ---- char *strLeft(char *str,int n) { ! /* now hope i got this right; i'm not very failiar with the functions */ ! char tmp[n+1]; /* this should alloc n+1 bytes */ ! __asm("mov esi,dword [str]"); ! __asm("lea edi,[tmp]"); ! __asm("push edi"); /* trick: store ptr so we don't hafta recalculate it*/ ! __asm("xor ecx,ecx") /* define n as long to get rid of this. note that you hafta also alter the next line*/ ! __asm("mov cx,word [n]"); ! __asm("cld"); ! __asm("rep"); ! __asm("movsb"); /* string copy */ ! __asm("xor al,al"); ! __asm("stosb") /* insert zero terminator */ ! __asm("pop eax"); ! __asm("retn"); /* return ptr */ ! ! /* ! char *tmp; ! tmp = strdup(str); ! strncpy(tmp + n,"",abs(strlen(tmp) - n)); ! ! return tmp; ! */ } *************** *** 280,315 **** int itoh(byte num, char *pt) { ! unsigned char nm[2]; ! ! nm[0]=(char)((int)num/16); ! nm[1]=(char)((int)num%16); ! ! for (num=0;num<2;(num)++) ! { ! switch (nm[num]) ! { ! case 10: ! ! case 11: ! ! case 12: ! ! case 13: ! ! case 14: ! ! case 15: ! nm[num]+=55; ! break; ! ! default: ! nm[num]+=48; ! break; ! } ! } ! ! *pt=(char)nm[0]; ! *(pt+1)=(char)nm[1]; ! return(0); } --- 318,377 ---- int itoh(byte num, char *pt) { ! /* this one's much faster in asm */ ! __asm("mov edi,dword [pt]"); ! __asm("mov al,byte [num]"); ! __asm("push ax"); /* store num or second round */ ! __asm("shr al,0x04"); /* take high nibble */ ! __asm("call near itoh_3"); /* use our subfunction located below */ ! __asm("pop ax") /* reget num */ ! __asm("and al,0x0F"); /* take low nibble */ ! __asm("call near itoh_3"); /* use our subfunction located below */ ! __asm("xor ax,ax"); ! __asm("retn"); /* return zero word */ ! ! /* this one is a function that converts low nibble in al to hex stores it with stosb ! does look weird if you don't know assembly */ ! __asm("itoh_3: cmp al,0x0A"); ! __asm("jnc itoh_1"); ! __asm("add al,0x30"); /* add either 0x30... */ ! __asm("jmp itoh_2"); ! __asm("itoh_1: add al,0x37"); /* ...or 0x37 */ ! __asm("itoh_2: stosb"); /* store character */ ! __asm("retn"); /* get back */ ! ! /* ! unsigned char nm[2]; ! ! nm[0]=(char)((int)num/16); ! nm[1]=(char)((int)num%16); ! ! for (num=0;num<2;(num)++) ! { ! switch (nm[num]) ! { ! case 10: ! ! case 11: ! ! case 12: ! ! case 13: ! ! case 14: ! ! case 15: ! nm[num]+=55; ! break; ! ! default: ! nm[num]+=48; ! break; ! } ! } ! ! *pt=(char)nm[0]; ! *(pt+1)=(char)nm[1]; ! return(0); ! */ } *************** *** 931,942 **** void *SafeMalloc(long size) { ! void *ptr; ! ! ptr = malloc(size); ! ! if (!ptr) ! rterror(ERR_MEMORY_CAPACITY); ! ! return ptr; } --- 993,1016 ---- void *SafeMalloc(long size) { ! /* you can optimize even simple code like this */ ! __asm("push dword [size]"); ! __asm("call near malloc"); /* call normally, NOTE: this is a stdcall, may bug on other call systems */ ! __asm("or eax,0x00000000"); ! __asm("jz SafeMalloc_1"); /* if we got zero */ ! __asm("retn"); ! __asm("SafeMalloc_1: push dword ERR_MEMORY_CAPACITY"); ! __asm("leave"); ! __asm("jmp rterror"); /* skip double retn, NOTE: not 100% sure about this - i should test it, it depends how compiler assembles a function frame */ ! ! /* ! void *ptr; ! ! ptr = malloc(size); ! ! if (!ptr) ! rterror(ERR_MEMORY_CAPACITY); ! ! return ptr; ! */ } |