Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[libc] Reduce overall code size by externalising strncmp()
Typical saving is 20-30 bytes in each file using strncmp().

Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Nov 12, 2012
1 parent f8ece72 commit 0676687
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
38 changes: 38 additions & 0 deletions src/arch/x86/core/x86_string.c
Expand Up @@ -104,3 +104,41 @@ void * __memmove ( void *dest, const void *src, size_t len ) {
return __memcpy_reverse ( dest, src, len );
}
}

/**
* Compare strings (up to a specified length)
*
* @v str1 First string
* @v str2 Second string
* @v len Maximum length
* @ret diff Difference
*/
int strncmp ( const char *str1, const char *str2, size_t len ) {
const void *discard_S;
const void *discard_D;
size_t discard_c;
int diff;

__asm__ __volatile__ ( "\n1:\n\t"
"dec %2\n\t"
"js 2f\n\t"
"lodsb\n\t"
"scasb\n\t"
"jne 3f\n\t"
"testb %b3, %b3\n\t"
"jnz 1b\n\t"
/* Equal */
"\n2:\n\t"
"xor %3, %3\n\t"
"jmp 4f\n\t"
/* Not equal; CF indicates difference */
"\n3:\n\t"
"sbb %3, %3\n\t"
"orb $1, %b3\n\t"
"\n4:\n\t"
: "=&S" ( discard_S ), "=&D" ( discard_D ),
"=&c" ( discard_c ), "=&a" ( diff )
: "0" ( str1 ), "1" ( str2 ), "2" ( len ) );

return diff;
}
23 changes: 2 additions & 21 deletions src/arch/x86/include/bits/string.h
Expand Up @@ -230,27 +230,8 @@ return dest;
}

#define __HAVE_ARCH_STRNCMP
static inline int strncmp(const char * cs,const char * ct,size_t count)
{
register int __res;
int d0, d1, d2;
__asm__ __volatile__(
"1:\tdecl %3\n\t"
"js 2f\n\t"
"lodsb\n\t"
"scasb\n\t"
"jne 3f\n\t"
"testb %%al,%%al\n\t"
"jne 1b\n"
"2:\txorl %%eax,%%eax\n\t"
"jmp 4f\n"
"3:\tsbbl %%eax,%%eax\n\t"
"orb $1,%%al\n"
"4:"
:"=a" (__res), "=&S" (d0), "=&D" (d1), "=&c" (d2)
:"1" (cs),"2" (ct),"3" (count));
return __res;
}

extern int strncmp ( const char *str1, const char *str2, size_t len );

#define __HAVE_ARCH_STRLEN
static inline size_t strlen(const char * s)
Expand Down

0 comments on commit 0676687

Please sign in to comment.