diff options
author | Andi Kleen <ak@suse.de> | 2007-07-21 11:09:59 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2007-07-21 21:37:08 -0400 |
commit | b520b85a963bf7b14b9614579aff14558d7ee264 (patch) | |
tree | a254ee00655f042c3dfaa2486a7ce8caa0767806 /include/asm-i386 | |
parent | aac57f81eb16d56afb5bd5a31ff325b5d1615580 (diff) |
i386: Move all simple string operations out of line
The compiler generally generates reasonable inline code for the simple
cases and for the rest it's better for code size for them to be out of line.
Also there they can be potentially optimized more in the future.
In fact they probably should be in a .S file because they're all pure
assembly, but that's for another day.
Also some code style cleanup on them while I was on it (this seems
to be the last untouched really early Linux code)
This saves ~12k text for a defconfig kernel with gcc 4.1.
Signed-off-by: Andi Kleen <ak@suse.de>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'include/asm-i386')
-rw-r--r-- | include/asm-i386/string.h | 243 |
1 files changed, 13 insertions, 230 deletions
diff --git a/include/asm-i386/string.h b/include/asm-i386/string.h index b9277361954b..a9b64453bdf5 100644 --- a/include/asm-i386/string.h +++ b/include/asm-i386/string.h | |||
@@ -2,203 +2,35 @@ | |||
2 | #define _I386_STRING_H_ | 2 | #define _I386_STRING_H_ |
3 | 3 | ||
4 | #ifdef __KERNEL__ | 4 | #ifdef __KERNEL__ |
5 | /* | ||
6 | * On a 486 or Pentium, we are better off not using the | ||
7 | * byte string operations. But on a 386 or a PPro the | ||
8 | * byte string ops are faster than doing it by hand | ||
9 | * (MUCH faster on a Pentium). | ||
10 | */ | ||
11 | |||
12 | /* | ||
13 | * This string-include defines all string functions as inline | ||
14 | * functions. Use gcc. It also assumes ds=es=data space, this should be | ||
15 | * normal. Most of the string-functions are rather heavily hand-optimized, | ||
16 | * see especially strsep,strstr,str[c]spn. They should work, but are not | ||
17 | * very easy to understand. Everything is done entirely within the register | ||
18 | * set, making the functions fast and clean. String instructions have been | ||
19 | * used through-out, making for "slightly" unclear code :-) | ||
20 | * | ||
21 | * NO Copyright (C) 1991, 1992 Linus Torvalds, | ||
22 | * consider these trivial functions to be PD. | ||
23 | */ | ||
24 | 5 | ||
25 | /* AK: in fact I bet it would be better to move this stuff all out of line. | 6 | /* Let gcc decide wether to inline or use the out of line functions */ |
26 | */ | ||
27 | 7 | ||
28 | #define __HAVE_ARCH_STRCPY | 8 | #define __HAVE_ARCH_STRCPY |
29 | static inline char * strcpy(char * dest,const char *src) | 9 | extern char *strcpy(char *dest, const char *src); |
30 | { | ||
31 | int d0, d1, d2; | ||
32 | __asm__ __volatile__( | ||
33 | "1:\tlodsb\n\t" | ||
34 | "stosb\n\t" | ||
35 | "testb %%al,%%al\n\t" | ||
36 | "jne 1b" | ||
37 | : "=&S" (d0), "=&D" (d1), "=&a" (d2) | ||
38 | :"0" (src),"1" (dest) : "memory"); | ||
39 | return dest; | ||
40 | } | ||
41 | 10 | ||
42 | #define __HAVE_ARCH_STRNCPY | 11 | #define __HAVE_ARCH_STRNCPY |
43 | static inline char * strncpy(char * dest,const char *src,size_t count) | 12 | extern char *strncpy(char *dest, const char *src, size_t count); |
44 | { | ||
45 | int d0, d1, d2, d3; | ||
46 | __asm__ __volatile__( | ||
47 | "1:\tdecl %2\n\t" | ||
48 | "js 2f\n\t" | ||
49 | "lodsb\n\t" | ||
50 | "stosb\n\t" | ||
51 | "testb %%al,%%al\n\t" | ||
52 | "jne 1b\n\t" | ||
53 | "rep\n\t" | ||
54 | "stosb\n" | ||
55 | "2:" | ||
56 | : "=&S" (d0), "=&D" (d1), "=&c" (d2), "=&a" (d3) | ||
57 | :"0" (src),"1" (dest),"2" (count) : "memory"); | ||
58 | return dest; | ||
59 | } | ||
60 | 13 | ||
61 | #define __HAVE_ARCH_STRCAT | 14 | #define __HAVE_ARCH_STRCAT |
62 | static inline char * strcat(char * dest,const char * src) | 15 | extern char *strcat(char *dest, const char *src); |
63 | { | ||
64 | int d0, d1, d2, d3; | ||
65 | __asm__ __volatile__( | ||
66 | "repne\n\t" | ||
67 | "scasb\n\t" | ||
68 | "decl %1\n" | ||
69 | "1:\tlodsb\n\t" | ||
70 | "stosb\n\t" | ||
71 | "testb %%al,%%al\n\t" | ||
72 | "jne 1b" | ||
73 | : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3) | ||
74 | : "0" (src), "1" (dest), "2" (0), "3" (0xffffffffu):"memory"); | ||
75 | return dest; | ||
76 | } | ||
77 | 16 | ||
78 | #define __HAVE_ARCH_STRNCAT | 17 | #define __HAVE_ARCH_STRNCAT |
79 | static inline char * strncat(char * dest,const char * src,size_t count) | 18 | extern char *strncat(char *dest, const char *src, size_t count); |
80 | { | ||
81 | int d0, d1, d2, d3; | ||
82 | __asm__ __volatile__( | ||
83 | "repne\n\t" | ||
84 | "scasb\n\t" | ||
85 | "decl %1\n\t" | ||
86 | "movl %8,%3\n" | ||
87 | "1:\tdecl %3\n\t" | ||
88 | "js 2f\n\t" | ||
89 | "lodsb\n\t" | ||
90 | "stosb\n\t" | ||
91 | "testb %%al,%%al\n\t" | ||
92 | "jne 1b\n" | ||
93 | "2:\txorl %2,%2\n\t" | ||
94 | "stosb" | ||
95 | : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3) | ||
96 | : "0" (src),"1" (dest),"2" (0),"3" (0xffffffffu), "g" (count) | ||
97 | : "memory"); | ||
98 | return dest; | ||
99 | } | ||
100 | 19 | ||
101 | #define __HAVE_ARCH_STRCMP | 20 | #define __HAVE_ARCH_STRCMP |
102 | static inline int strcmp(const char * cs,const char * ct) | 21 | extern int strcmp(const char *cs, const char *ct); |
103 | { | ||
104 | int d0, d1; | ||
105 | register int __res; | ||
106 | __asm__ __volatile__( | ||
107 | "1:\tlodsb\n\t" | ||
108 | "scasb\n\t" | ||
109 | "jne 2f\n\t" | ||
110 | "testb %%al,%%al\n\t" | ||
111 | "jne 1b\n\t" | ||
112 | "xorl %%eax,%%eax\n\t" | ||
113 | "jmp 3f\n" | ||
114 | "2:\tsbbl %%eax,%%eax\n\t" | ||
115 | "orb $1,%%al\n" | ||
116 | "3:" | ||
117 | :"=a" (__res), "=&S" (d0), "=&D" (d1) | ||
118 | :"1" (cs),"2" (ct) | ||
119 | :"memory"); | ||
120 | return __res; | ||
121 | } | ||
122 | 22 | ||
123 | #define __HAVE_ARCH_STRNCMP | 23 | #define __HAVE_ARCH_STRNCMP |
124 | static inline int strncmp(const char * cs,const char * ct,size_t count) | 24 | extern int strncmp(const char *cs, const char *ct, size_t count); |
125 | { | ||
126 | register int __res; | ||
127 | int d0, d1, d2; | ||
128 | __asm__ __volatile__( | ||
129 | "1:\tdecl %3\n\t" | ||
130 | "js 2f\n\t" | ||
131 | "lodsb\n\t" | ||
132 | "scasb\n\t" | ||
133 | "jne 3f\n\t" | ||
134 | "testb %%al,%%al\n\t" | ||
135 | "jne 1b\n" | ||
136 | "2:\txorl %%eax,%%eax\n\t" | ||
137 | "jmp 4f\n" | ||
138 | "3:\tsbbl %%eax,%%eax\n\t" | ||
139 | "orb $1,%%al\n" | ||
140 | "4:" | ||
141 | :"=a" (__res), "=&S" (d0), "=&D" (d1), "=&c" (d2) | ||
142 | :"1" (cs),"2" (ct),"3" (count) | ||
143 | :"memory"); | ||
144 | return __res; | ||
145 | } | ||
146 | 25 | ||
147 | #define __HAVE_ARCH_STRCHR | 26 | #define __HAVE_ARCH_STRCHR |
148 | static inline char * strchr(const char * s, int c) | 27 | extern char *strchr(const char *s, int c); |
149 | { | ||
150 | int d0; | ||
151 | register char * __res; | ||
152 | __asm__ __volatile__( | ||
153 | "movb %%al,%%ah\n" | ||
154 | "1:\tlodsb\n\t" | ||
155 | "cmpb %%ah,%%al\n\t" | ||
156 | "je 2f\n\t" | ||
157 | "testb %%al,%%al\n\t" | ||
158 | "jne 1b\n\t" | ||
159 | "movl $1,%1\n" | ||
160 | "2:\tmovl %1,%0\n\t" | ||
161 | "decl %0" | ||
162 | :"=a" (__res), "=&S" (d0) | ||
163 | :"1" (s),"0" (c) | ||
164 | :"memory"); | ||
165 | return __res; | ||
166 | } | ||
167 | 28 | ||
168 | #define __HAVE_ARCH_STRRCHR | 29 | #define __HAVE_ARCH_STRRCHR |
169 | static inline char * strrchr(const char * s, int c) | 30 | extern char *strrchr(const char *s, int c); |
170 | { | ||
171 | int d0, d1; | ||
172 | register char * __res; | ||
173 | __asm__ __volatile__( | ||
174 | "movb %%al,%%ah\n" | ||
175 | "1:\tlodsb\n\t" | ||
176 | "cmpb %%ah,%%al\n\t" | ||
177 | "jne 2f\n\t" | ||
178 | "leal -1(%%esi),%0\n" | ||
179 | "2:\ttestb %%al,%%al\n\t" | ||
180 | "jne 1b" | ||
181 | :"=g" (__res), "=&S" (d0), "=&a" (d1) | ||
182 | :"0" (0),"1" (s),"2" (c) | ||
183 | :"memory"); | ||
184 | return __res; | ||
185 | } | ||
186 | 31 | ||
187 | #define __HAVE_ARCH_STRLEN | 32 | #define __HAVE_ARCH_STRLEN |
188 | static inline size_t strlen(const char * s) | 33 | extern size_t strlen(const char *s); |
189 | { | ||
190 | int d0; | ||
191 | register int __res; | ||
192 | __asm__ __volatile__( | ||
193 | "repne\n\t" | ||
194 | "scasb\n\t" | ||
195 | "notl %0\n\t" | ||
196 | "decl %0" | ||
197 | :"=c" (__res), "=&D" (d0) | ||
198 | :"1" (s),"a" (0), "0" (0xffffffffu) | ||
199 | :"memory"); | ||
200 | return __res; | ||
201 | } | ||
202 | 34 | ||
203 | static __always_inline void * __memcpy(void * to, const void * from, size_t n) | 35 | static __always_inline void * __memcpy(void * to, const void * from, size_t n) |
204 | { | 36 | { |
@@ -207,9 +39,7 @@ __asm__ __volatile__( | |||
207 | "rep ; movsl\n\t" | 39 | "rep ; movsl\n\t" |
208 | "movl %4,%%ecx\n\t" | 40 | "movl %4,%%ecx\n\t" |
209 | "andl $3,%%ecx\n\t" | 41 | "andl $3,%%ecx\n\t" |
210 | #if 1 /* want to pay 2 byte penalty for a chance to skip microcoded rep? */ | ||
211 | "jz 1f\n\t" | 42 | "jz 1f\n\t" |
212 | #endif | ||
213 | "rep ; movsb\n\t" | 43 | "rep ; movsb\n\t" |
214 | "1:" | 44 | "1:" |
215 | : "=&c" (d0), "=&D" (d1), "=&S" (d2) | 45 | : "=&c" (d0), "=&D" (d1), "=&S" (d2) |
@@ -328,23 +158,7 @@ void *memmove(void * dest,const void * src, size_t n); | |||
328 | #define memcmp __builtin_memcmp | 158 | #define memcmp __builtin_memcmp |
329 | 159 | ||
330 | #define __HAVE_ARCH_MEMCHR | 160 | #define __HAVE_ARCH_MEMCHR |
331 | static inline void * memchr(const void * cs,int c,size_t count) | 161 | extern void *memchr(const void * cs,int c,size_t count); |
332 | { | ||
333 | int d0; | ||
334 | register void * __res; | ||
335 | if (!count) | ||
336 | return NULL; | ||
337 | __asm__ __volatile__( | ||
338 | "repne\n\t" | ||
339 | "scasb\n\t" | ||
340 | "je 1f\n\t" | ||
341 | "movl $1,%0\n" | ||
342 | "1:\tdecl %0" | ||
343 | :"=D" (__res), "=&c" (d0) | ||
344 | :"a" (c),"0" (cs),"1" (count) | ||
345 | :"memory"); | ||
346 | return __res; | ||
347 | } | ||
348 | 162 | ||
349 | static inline void * __memset_generic(void * s, char c,size_t count) | 163 | static inline void * __memset_generic(void * s, char c,size_t count) |
350 | { | 164 | { |
@@ -386,29 +200,10 @@ return (s); | |||
386 | 200 | ||
387 | /* Added by Gertjan van Wingerde to make minix and sysv module work */ | 201 | /* Added by Gertjan van Wingerde to make minix and sysv module work */ |
388 | #define __HAVE_ARCH_STRNLEN | 202 | #define __HAVE_ARCH_STRNLEN |
389 | static inline size_t strnlen(const char * s, size_t count) | 203 | extern size_t strnlen(const char * s, size_t count); |
390 | { | ||
391 | int d0; | ||
392 | register int __res; | ||
393 | __asm__ __volatile__( | ||
394 | "movl %2,%0\n\t" | ||
395 | "jmp 2f\n" | ||
396 | "1:\tcmpb $0,(%0)\n\t" | ||
397 | "je 3f\n\t" | ||
398 | "incl %0\n" | ||
399 | "2:\tdecl %1\n\t" | ||
400 | "cmpl $-1,%1\n\t" | ||
401 | "jne 1b\n" | ||
402 | "3:\tsubl %2,%0" | ||
403 | :"=a" (__res), "=&d" (d0) | ||
404 | :"c" (s),"1" (count) | ||
405 | :"memory"); | ||
406 | return __res; | ||
407 | } | ||
408 | /* end of additional stuff */ | 204 | /* end of additional stuff */ |
409 | 205 | ||
410 | #define __HAVE_ARCH_STRSTR | 206 | #define __HAVE_ARCH_STRSTR |
411 | |||
412 | extern char *strstr(const char *cs, const char *ct); | 207 | extern char *strstr(const char *cs, const char *ct); |
413 | 208 | ||
414 | /* | 209 | /* |
@@ -474,19 +269,7 @@ __asm__ __volatile__( \ | |||
474 | * find the first occurrence of byte 'c', or 1 past the area if none | 269 | * find the first occurrence of byte 'c', or 1 past the area if none |
475 | */ | 270 | */ |
476 | #define __HAVE_ARCH_MEMSCAN | 271 | #define __HAVE_ARCH_MEMSCAN |
477 | static inline void * memscan(void * addr, int c, size_t size) | 272 | extern void *memscan(void * addr, int c, size_t size); |
478 | { | ||
479 | if (!size) | ||
480 | return addr; | ||
481 | __asm__("repnz; scasb\n\t" | ||
482 | "jnz 1f\n\t" | ||
483 | "dec %%edi\n" | ||
484 | "1:" | ||
485 | : "=D" (addr), "=c" (size) | ||
486 | : "0" (addr), "1" (size), "a" (c) | ||
487 | : "memory"); | ||
488 | return addr; | ||
489 | } | ||
490 | 273 | ||
491 | #endif /* __KERNEL__ */ | 274 | #endif /* __KERNEL__ */ |
492 | 275 | ||