From f3f7db016abb60cbfbdc5c4ffda376f5a90587ab Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Fri, 19 Dec 2008 00:25:56 +0000 Subject: string/i386: smaller, and hopefully easier to read, strnlen(). text data bss dec hex filename - 25 0 0 25 19 libc/string/i386/strnlen.os + 24 0 0 24 18 libc/string/i386/strnlen.os --- libc/string/i386/strnlen.c | 47 ++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) (limited to 'libc/string/i386') diff --git a/libc/string/i386/strnlen.c b/libc/string/i386/strnlen.c index 02c72f530..066a11ef7 100644 --- a/libc/string/i386/strnlen.c +++ b/libc/string/i386/strnlen.c @@ -33,24 +33,39 @@ #include #ifdef __USE_GNU -/* Experimentally off - libc_hidden_proto(strnlen) */ + size_t strnlen(const char *s, size_t count) { - int d0; - register int __res; - __asm__ __volatile__( - "movl %2,%0\n\t" - "incl %1\n" - "jmp 2f\n" - "1:\tcmpb $0,(%0)\n\t" - "je 3f\n\t" - "incl %0\n" - "2:\tdecl %1\n\t" - "jne 1b\n" - "3:\tsubl %2,%0" - :"=a" (__res), "=&d" (d0) - :"c" (s),"1" (count)); - return __res; + int edx; + int eax; + __asm__ __volatile__( + " leal -1(%%ecx), %%eax\n" + "1: incl %%eax\n" + " decl %%edx\n" + " jz 3f\n" + " cmpb $0, (%%eax)\n" + " jnz 1b\n" + "3: subl %%ecx, %%eax" + : "=a" (eax), "=&d" (edx) + : "c" (s), "1" (count + 1) + ); + return eax; } libc_hidden_def(strnlen) + +#if 0 +int main() +{ + printf(strnlen2("abc\0def", -2) == 3 ? "ok\n" : "BAD!\n"); + printf(strnlen2("abc\0def", -1) == 3 ? "ok\n" : "BAD!\n"); + printf(strnlen2("abc\0def", 0) == 0 ? "ok\n" : "BAD!\n"); + printf(strnlen2("abc\0def", 1) == 1 ? "ok\n" : "BAD!\n"); + printf(strnlen2("abc\0def", 2) == 2 ? "ok\n" : "BAD!\n"); + printf(strnlen2("abc\0def", 3) == 3 ? "ok\n" : "BAD!\n"); + printf(strnlen2("abc\0def", 4) == 3 ? "ok\n" : "BAD!\n"); + printf(strnlen2("abc\0def", 5) == 3 ? "ok\n" : "BAD!\n"); + return 0; +} +#endif + #endif -- cgit v1.2.3