initial commit with v2.6.9
[linux-2.6.9-moxart.git] / include / asm-i386 / string.h
blobfc66609bb76142e19c3adf129d26243640a84299
1 #ifndef _I386_STRING_H_
2 #define _I386_STRING_H_
4 #ifdef __KERNEL__
5 #include <linux/config.h>
6 /*
7 * On a 486 or Pentium, we are better off not using the
8 * byte string operations. But on a 386 or a PPro the
9 * byte string ops are faster than doing it by hand
10 * (MUCH faster on a Pentium).
14 * This string-include defines all string functions as inline
15 * functions. Use gcc. It also assumes ds=es=data space, this should be
16 * normal. Most of the string-functions are rather heavily hand-optimized,
17 * see especially strsep,strstr,str[c]spn. They should work, but are not
18 * very easy to understand. Everything is done entirely within the register
19 * set, making the functions fast and clean. String instructions have been
20 * used through-out, making for "slightly" unclear code :-)
22 * NO Copyright (C) 1991, 1992 Linus Torvalds,
23 * consider these trivial functions to be PD.
26 /* AK: in fact I bet it would be better to move this stuff all out of line.
28 #if !defined(IN_STRING_C)
30 #define __HAVE_ARCH_STRCPY
31 static inline char * strcpy(char * dest,const char *src)
33 int d0, d1, d2;
34 __asm__ __volatile__(
35 "1:\tlodsb\n\t"
36 "stosb\n\t"
37 "testb %%al,%%al\n\t"
38 "jne 1b"
39 : "=&S" (d0), "=&D" (d1), "=&a" (d2)
40 :"0" (src),"1" (dest) : "memory");
41 return dest;
44 #define __HAVE_ARCH_STRNCPY
45 static inline char * strncpy(char * dest,const char *src,size_t count)
47 int d0, d1, d2, d3;
48 __asm__ __volatile__(
49 "1:\tdecl %2\n\t"
50 "js 2f\n\t"
51 "lodsb\n\t"
52 "stosb\n\t"
53 "testb %%al,%%al\n\t"
54 "jne 1b\n\t"
55 "rep\n\t"
56 "stosb\n"
57 "2:"
58 : "=&S" (d0), "=&D" (d1), "=&c" (d2), "=&a" (d3)
59 :"0" (src),"1" (dest),"2" (count) : "memory");
60 return dest;
63 #define __HAVE_ARCH_STRCAT
64 static inline char * strcat(char * dest,const char * src)
66 int d0, d1, d2, d3;
67 __asm__ __volatile__(
68 "repne\n\t"
69 "scasb\n\t"
70 "decl %1\n"
71 "1:\tlodsb\n\t"
72 "stosb\n\t"
73 "testb %%al,%%al\n\t"
74 "jne 1b"
75 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
76 : "0" (src), "1" (dest), "2" (0), "3" (0xffffffffu):"memory");
77 return dest;
80 #define __HAVE_ARCH_STRNCAT
81 static inline char * strncat(char * dest,const char * src,size_t count)
83 int d0, d1, d2, d3;
84 __asm__ __volatile__(
85 "repne\n\t"
86 "scasb\n\t"
87 "decl %1\n\t"
88 "movl %8,%3\n"
89 "1:\tdecl %3\n\t"
90 "js 2f\n\t"
91 "lodsb\n\t"
92 "stosb\n\t"
93 "testb %%al,%%al\n\t"
94 "jne 1b\n"
95 "2:\txorl %2,%2\n\t"
96 "stosb"
97 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
98 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffffu), "g" (count)
99 : "memory");
100 return dest;
103 #define __HAVE_ARCH_STRCMP
104 static inline int strcmp(const char * cs,const char * ct)
106 int d0, d1;
107 register int __res;
108 __asm__ __volatile__(
109 "1:\tlodsb\n\t"
110 "scasb\n\t"
111 "jne 2f\n\t"
112 "testb %%al,%%al\n\t"
113 "jne 1b\n\t"
114 "xorl %%eax,%%eax\n\t"
115 "jmp 3f\n"
116 "2:\tsbbl %%eax,%%eax\n\t"
117 "orb $1,%%al\n"
118 "3:"
119 :"=a" (__res), "=&S" (d0), "=&D" (d1)
120 :"1" (cs),"2" (ct));
121 return __res;
124 #define __HAVE_ARCH_STRNCMP
125 static inline int strncmp(const char * cs,const char * ct,size_t count)
127 register int __res;
128 int d0, d1, d2;
129 __asm__ __volatile__(
130 "1:\tdecl %3\n\t"
131 "js 2f\n\t"
132 "lodsb\n\t"
133 "scasb\n\t"
134 "jne 3f\n\t"
135 "testb %%al,%%al\n\t"
136 "jne 1b\n"
137 "2:\txorl %%eax,%%eax\n\t"
138 "jmp 4f\n"
139 "3:\tsbbl %%eax,%%eax\n\t"
140 "orb $1,%%al\n"
141 "4:"
142 :"=a" (__res), "=&S" (d0), "=&D" (d1), "=&c" (d2)
143 :"1" (cs),"2" (ct),"3" (count));
144 return __res;
147 #define __HAVE_ARCH_STRCHR
148 static inline char * strchr(const char * s, int c)
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) : "1" (s),"0" (c));
163 return __res;
166 #define __HAVE_ARCH_STRRCHR
167 static inline char * strrchr(const char * s, int c)
169 int d0, d1;
170 register char * __res;
171 __asm__ __volatile__(
172 "movb %%al,%%ah\n"
173 "1:\tlodsb\n\t"
174 "cmpb %%ah,%%al\n\t"
175 "jne 2f\n\t"
176 "leal -1(%%esi),%0\n"
177 "2:\ttestb %%al,%%al\n\t"
178 "jne 1b"
179 :"=g" (__res), "=&S" (d0), "=&a" (d1) :"0" (0),"1" (s),"2" (c));
180 return __res;
183 #endif
185 #define __HAVE_ARCH_STRLEN
186 static inline size_t strlen(const char * s)
188 int d0;
189 register int __res;
190 __asm__ __volatile__(
191 "repne\n\t"
192 "scasb\n\t"
193 "notl %0\n\t"
194 "decl %0"
195 :"=c" (__res), "=&D" (d0) :"1" (s),"a" (0), "0" (0xffffffffu));
196 return __res;
199 static inline void * __memcpy(void * to, const void * from, size_t n)
201 int d0, d1, d2;
202 __asm__ __volatile__(
203 "rep ; movsl\n\t"
204 "testb $2,%b4\n\t"
205 "je 1f\n\t"
206 "movsw\n"
207 "1:\ttestb $1,%b4\n\t"
208 "je 2f\n\t"
209 "movsb\n"
210 "2:"
211 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
212 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
213 : "memory");
214 return (to);
218 * This looks horribly ugly, but the compiler can optimize it totally,
219 * as the count is constant.
221 static inline void * __constant_memcpy(void * to, const void * from, size_t n)
223 if (n <= 128)
224 return __builtin_memcpy(to, from, n);
226 #define COMMON(x) \
227 __asm__ __volatile__( \
228 "rep ; movsl" \
230 : "=&c" (d0), "=&D" (d1), "=&S" (d2) \
231 : "0" (n/4),"1" ((long) to),"2" ((long) from) \
232 : "memory");
234 int d0, d1, d2;
235 switch (n % 4) {
236 case 0: COMMON(""); return to;
237 case 1: COMMON("\n\tmovsb"); return to;
238 case 2: COMMON("\n\tmovsw"); return to;
239 default: COMMON("\n\tmovsw\n\tmovsb"); return to;
243 #undef COMMON
246 #define __HAVE_ARCH_MEMCPY
248 #ifdef CONFIG_X86_USE_3DNOW
250 #include <asm/mmx.h>
253 * This CPU favours 3DNow strongly (eg AMD Athlon)
256 static inline void * __constant_memcpy3d(void * to, const void * from, size_t len)
258 if (len < 512)
259 return __constant_memcpy(to, from, len);
260 return _mmx_memcpy(to, from, len);
263 static __inline__ void *__memcpy3d(void *to, const void *from, size_t len)
265 if (len < 512)
266 return __memcpy(to, from, len);
267 return _mmx_memcpy(to, from, len);
270 #define memcpy(t, f, n) \
271 (__builtin_constant_p(n) ? \
272 __constant_memcpy3d((t),(f),(n)) : \
273 __memcpy3d((t),(f),(n)))
275 #else
278 * No 3D Now!
281 #define memcpy(t, f, n) \
282 (__builtin_constant_p(n) ? \
283 __constant_memcpy((t),(f),(n)) : \
284 __memcpy((t),(f),(n)))
286 #endif
288 #define __HAVE_ARCH_MEMMOVE
289 void *memmove(void * dest,const void * src, size_t n);
291 #define memcmp __builtin_memcmp
293 #define __HAVE_ARCH_MEMCHR
294 static inline void * memchr(const void * cs,int c,size_t count)
296 int d0;
297 register void * __res;
298 if (!count)
299 return NULL;
300 __asm__ __volatile__(
301 "repne\n\t"
302 "scasb\n\t"
303 "je 1f\n\t"
304 "movl $1,%0\n"
305 "1:\tdecl %0"
306 :"=D" (__res), "=&c" (d0) : "a" (c),"0" (cs),"1" (count));
307 return __res;
310 static inline void * __memset_generic(void * s, char c,size_t count)
312 int d0, d1;
313 __asm__ __volatile__(
314 "rep\n\t"
315 "stosb"
316 : "=&c" (d0), "=&D" (d1)
317 :"a" (c),"1" (s),"0" (count)
318 :"memory");
319 return s;
322 /* we might want to write optimized versions of these later */
323 #define __constant_count_memset(s,c,count) __memset_generic((s),(c),(count))
326 * memset(x,0,y) is a reasonably common thing to do, so we want to fill
327 * things 32 bits at a time even when we don't know the size of the
328 * area at compile-time..
330 static inline void * __constant_c_memset(void * s, unsigned long c, size_t count)
332 int d0, d1;
333 __asm__ __volatile__(
334 "rep ; stosl\n\t"
335 "testb $2,%b3\n\t"
336 "je 1f\n\t"
337 "stosw\n"
338 "1:\ttestb $1,%b3\n\t"
339 "je 2f\n\t"
340 "stosb\n"
341 "2:"
342 : "=&c" (d0), "=&D" (d1)
343 :"a" (c), "q" (count), "0" (count/4), "1" ((long) s)
344 :"memory");
345 return (s);
348 /* Added by Gertjan van Wingerde to make minix and sysv module work */
349 #define __HAVE_ARCH_STRNLEN
350 static inline size_t strnlen(const char * s, size_t count)
352 int d0;
353 register int __res;
354 __asm__ __volatile__(
355 "movl %2,%0\n\t"
356 "jmp 2f\n"
357 "1:\tcmpb $0,(%0)\n\t"
358 "je 3f\n\t"
359 "incl %0\n"
360 "2:\tdecl %1\n\t"
361 "cmpl $-1,%1\n\t"
362 "jne 1b\n"
363 "3:\tsubl %2,%0"
364 :"=a" (__res), "=&d" (d0)
365 :"c" (s),"1" (count));
366 return __res;
368 /* end of additional stuff */
370 #define __HAVE_ARCH_STRSTR
372 extern char *strstr(const char *cs, const char *ct);
375 * This looks horribly ugly, but the compiler can optimize it totally,
376 * as we by now know that both pattern and count is constant..
378 static inline void * __constant_c_and_count_memset(void * s, unsigned long pattern, size_t count)
380 switch (count) {
381 case 0:
382 return s;
383 case 1:
384 *(unsigned char *)s = pattern;
385 return s;
386 case 2:
387 *(unsigned short *)s = pattern;
388 return s;
389 case 3:
390 *(unsigned short *)s = pattern;
391 *(2+(unsigned char *)s) = pattern;
392 return s;
393 case 4:
394 *(unsigned long *)s = pattern;
395 return s;
397 #define COMMON(x) \
398 __asm__ __volatile__( \
399 "rep ; stosl" \
401 : "=&c" (d0), "=&D" (d1) \
402 : "a" (pattern),"0" (count/4),"1" ((long) s) \
403 : "memory")
405 int d0, d1;
406 switch (count % 4) {
407 case 0: COMMON(""); return s;
408 case 1: COMMON("\n\tstosb"); return s;
409 case 2: COMMON("\n\tstosw"); return s;
410 default: COMMON("\n\tstosw\n\tstosb"); return s;
414 #undef COMMON
417 #define __constant_c_x_memset(s, c, count) \
418 (__builtin_constant_p(count) ? \
419 __constant_c_and_count_memset((s),(c),(count)) : \
420 __constant_c_memset((s),(c),(count)))
422 #define __memset(s, c, count) \
423 (__builtin_constant_p(count) ? \
424 __constant_count_memset((s),(c),(count)) : \
425 __memset_generic((s),(c),(count)))
427 #define __HAVE_ARCH_MEMSET
428 #define memset(s, c, count) \
429 (__builtin_constant_p(c) ? \
430 __constant_c_x_memset((s),(0x01010101UL*(unsigned char)(c)),(count)) : \
431 __memset((s),(c),(count)))
434 * find the first occurrence of byte 'c', or 1 past the area if none
436 #define __HAVE_ARCH_MEMSCAN
437 static inline void * memscan(void * addr, int c, size_t size)
439 if (!size)
440 return addr;
441 __asm__("repnz; scasb\n\t"
442 "jnz 1f\n\t"
443 "dec %%edi\n"
444 "1:"
445 : "=D" (addr), "=c" (size)
446 : "0" (addr), "1" (size), "a" (c));
447 return addr;
450 #endif /* __KERNEL__ */
452 #endif