Browse Source

[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>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
06766875ad
2 changed files with 40 additions and 21 deletions
  1. 38
    0
      src/arch/x86/core/x86_string.c
  2. 2
    21
      src/arch/x86/include/bits/string.h

+ 38
- 0
src/arch/x86/core/x86_string.c View File

@@ -104,3 +104,41 @@ void * __memmove ( void *dest, const void *src, size_t len ) {
104 104
 		return __memcpy_reverse ( dest, src, len );
105 105
 	}
106 106
 }
107
+
108
+/**
109
+ * Compare strings (up to a specified length)
110
+ *
111
+ * @v str1		First string
112
+ * @v str2		Second string
113
+ * @v len		Maximum length
114
+ * @ret diff		Difference
115
+ */
116
+int strncmp ( const char *str1, const char *str2, size_t len ) {
117
+	const void *discard_S;
118
+	const void *discard_D;
119
+	size_t discard_c;
120
+	int diff;
121
+
122
+	__asm__ __volatile__ ( "\n1:\n\t"
123
+			       "dec %2\n\t"
124
+			       "js 2f\n\t"
125
+			       "lodsb\n\t"
126
+			       "scasb\n\t"
127
+			       "jne 3f\n\t"
128
+			       "testb %b3, %b3\n\t"
129
+			       "jnz 1b\n\t"
130
+			       /* Equal */
131
+			       "\n2:\n\t"
132
+			       "xor %3, %3\n\t"
133
+			       "jmp 4f\n\t"
134
+			       /* Not equal; CF indicates difference */
135
+			       "\n3:\n\t"
136
+			       "sbb %3, %3\n\t"
137
+			       "orb $1, %b3\n\t"
138
+			       "\n4:\n\t"
139
+			       : "=&S" ( discard_S ), "=&D" ( discard_D ),
140
+				 "=&c" ( discard_c ), "=&a" ( diff )
141
+			       : "0" ( str1 ), "1" ( str2 ), "2" ( len ) );
142
+
143
+	return diff;
144
+}

+ 2
- 21
src/arch/x86/include/bits/string.h View File

@@ -230,27 +230,8 @@ return dest;
230 230
 }
231 231
 
232 232
 #define __HAVE_ARCH_STRNCMP
233
-static inline int strncmp(const char * cs,const char * ct,size_t count)
234
-{
235
-register int __res;
236
-int d0, d1, d2;
237
-__asm__ __volatile__(
238
-	"1:\tdecl %3\n\t"
239
-	"js 2f\n\t"
240
-	"lodsb\n\t"
241
-	"scasb\n\t"
242
-	"jne 3f\n\t"
243
-	"testb %%al,%%al\n\t"
244
-	"jne 1b\n"
245
-	"2:\txorl %%eax,%%eax\n\t"
246
-	"jmp 4f\n"
247
-	"3:\tsbbl %%eax,%%eax\n\t"
248
-	"orb $1,%%al\n"
249
-	"4:"
250
-		     :"=a" (__res), "=&S" (d0), "=&D" (d1), "=&c" (d2)
251
-		     :"1" (cs),"2" (ct),"3" (count));
252
-return __res;
253
-}
233
+
234
+extern int strncmp ( const char *str1, const char *str2, size_t len );
254 235
 
255 236
 #define __HAVE_ARCH_STRLEN
256 237
 static inline size_t strlen(const char * s)

Loading…
Cancel
Save