There is a bug in the CKRM CPU scheduler. This has been reported to the
[linux-2.6.git] / include / asm-i386 / string.h
1 #ifndef _I386_STRING_H_
2 #define _I386_STRING_H_
3
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).
11  */
12
13 /*
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 :-)
21  *
22  *              NO Copyright (C) 1991, 1992 Linus Torvalds,
23  *              consider these trivial functions to be PD.
24  */
25
26 /* AK: in fact I bet it would be better to move this stuff all out of line.
27  */
28 #if !defined(IN_STRING_C)
29
30 #define __HAVE_ARCH_STRCPY
31 static inline char * strcpy(char * dest,const char *src)
32 {
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;
42 }
43
44 #define __HAVE_ARCH_STRNCPY
45 static inline char * strncpy(char * dest,const char *src,size_t count)
46 {
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;
61 }
62
63 /*
64  * This is a more generic variant of strncpy_count() suitable for
65  * implementing string-access routines with all sorts of return
66  * code semantics. It's used by mm/usercopy.c.
67  */
68 static inline size_t strncpy_count(char * dest,const char *src,size_t count)
69 {
70         __asm__ __volatile__(
71
72         "1:\tdecl %0\n\t"
73         "js 2f\n\t"
74         "lodsb\n\t"
75         "stosb\n\t"
76         "testb %%al,%%al\n\t"
77         "jne 1b\n\t"
78         "2:"
79         "incl %0"
80         : "=c" (count)
81         :"S" (src),"D" (dest),"0" (count) : "memory");
82
83         return count;
84 }
85
86 #define __HAVE_ARCH_STRCAT
87 static inline char * strcat(char * dest,const char * src)
88 {
89 int d0, d1, d2, d3;
90 __asm__ __volatile__(
91         "repne\n\t"
92         "scasb\n\t"
93         "decl %1\n"
94         "1:\tlodsb\n\t"
95         "stosb\n\t"
96         "testb %%al,%%al\n\t"
97         "jne 1b"
98         : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
99         : "0" (src), "1" (dest), "2" (0), "3" (0xffffffffu):"memory");
100 return dest;
101 }
102
103 #define __HAVE_ARCH_STRNCAT
104 static inline char * strncat(char * dest,const char * src,size_t count)
105 {
106 int d0, d1, d2, d3;
107 __asm__ __volatile__(
108         "repne\n\t"
109         "scasb\n\t"
110         "decl %1\n\t"
111         "movl %8,%3\n"
112         "1:\tdecl %3\n\t"
113         "js 2f\n\t"
114         "lodsb\n\t"
115         "stosb\n\t"
116         "testb %%al,%%al\n\t"
117         "jne 1b\n"
118         "2:\txorl %2,%2\n\t"
119         "stosb"
120         : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
121         : "0" (src),"1" (dest),"2" (0),"3" (0xffffffffu), "g" (count)
122         : "memory");
123 return dest;
124 }
125
126 #define __HAVE_ARCH_STRCMP
127 static inline int strcmp(const char * cs,const char * ct)
128 {
129 int d0, d1;
130 register int __res;
131 __asm__ __volatile__(
132         "1:\tlodsb\n\t"
133         "scasb\n\t"
134         "jne 2f\n\t"
135         "testb %%al,%%al\n\t"
136         "jne 1b\n\t"
137         "xorl %%eax,%%eax\n\t"
138         "jmp 3f\n"
139         "2:\tsbbl %%eax,%%eax\n\t"
140         "orb $1,%%al\n"
141         "3:"
142         :"=a" (__res), "=&S" (d0), "=&D" (d1)
143                      :"1" (cs),"2" (ct));
144 return __res;
145 }
146
147 #define __HAVE_ARCH_STRNCMP
148 static inline int strncmp(const char * cs,const char * ct,size_t count)
149 {
150 register int __res;
151 int d0, d1, d2;
152 __asm__ __volatile__(
153         "1:\tdecl %3\n\t"
154         "js 2f\n\t"
155         "lodsb\n\t"
156         "scasb\n\t"
157         "jne 3f\n\t"
158         "testb %%al,%%al\n\t"
159         "jne 1b\n"
160         "2:\txorl %%eax,%%eax\n\t"
161         "jmp 4f\n"
162         "3:\tsbbl %%eax,%%eax\n\t"
163         "orb $1,%%al\n"
164         "4:"
165                      :"=a" (__res), "=&S" (d0), "=&D" (d1), "=&c" (d2)
166                      :"1" (cs),"2" (ct),"3" (count));
167 return __res;
168 }
169
170 #define __HAVE_ARCH_STRCHR
171 static inline char * strchr(const char * s, int c)
172 {
173 int d0;
174 register char * __res;
175 __asm__ __volatile__(
176         "movb %%al,%%ah\n"
177         "1:\tlodsb\n\t"
178         "cmpb %%ah,%%al\n\t"
179         "je 2f\n\t"
180         "testb %%al,%%al\n\t"
181         "jne 1b\n\t"
182         "movl $1,%1\n"
183         "2:\tmovl %1,%0\n\t"
184         "decl %0"
185         :"=a" (__res), "=&S" (d0) : "1" (s),"0" (c));
186 return __res;
187 }
188
189 #define __HAVE_ARCH_STRRCHR
190 static inline char * strrchr(const char * s, int c)
191 {
192 int d0, d1;
193 register char * __res;
194 __asm__ __volatile__(
195         "movb %%al,%%ah\n"
196         "1:\tlodsb\n\t"
197         "cmpb %%ah,%%al\n\t"
198         "jne 2f\n\t"
199         "leal -1(%%esi),%0\n"
200         "2:\ttestb %%al,%%al\n\t"
201         "jne 1b"
202         :"=g" (__res), "=&S" (d0), "=&a" (d1) :"0" (0),"1" (s),"2" (c));
203 return __res;
204 }
205
206 #endif
207
208 #define __HAVE_ARCH_STRLEN
209 static inline size_t strlen(const char * s)
210 {
211 int d0;
212 register int __res;
213 __asm__ __volatile__(
214         "repne\n\t"
215         "scasb\n\t"
216         "notl %0\n\t"
217         "decl %0"
218         :"=c" (__res), "=&D" (d0) :"1" (s),"a" (0), "0" (0xffffffffu));
219 return __res;
220 }
221
222 static inline void * __memcpy(void * to, const void * from, size_t n)
223 {
224 int d0, d1, d2;
225 __asm__ __volatile__(
226         "rep ; movsl\n\t"
227         "testb $2,%b4\n\t"
228         "je 1f\n\t"
229         "movsw\n"
230         "1:\ttestb $1,%b4\n\t"
231         "je 2f\n\t"
232         "movsb\n"
233         "2:"
234         : "=&c" (d0), "=&D" (d1), "=&S" (d2)
235         :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
236         : "memory");
237 return (to);
238 }
239
240 /*
241  * This looks horribly ugly, but the compiler can optimize it totally,
242  * as the count is constant.
243  */
244 static inline void * __constant_memcpy(void * to, const void * from, size_t n)
245 {
246         if (n <= 128)
247                 return __builtin_memcpy(to, from, n);
248
249 #define COMMON(x) \
250 __asm__ __volatile__( \
251         "rep ; movsl" \
252         x \
253         : "=&c" (d0), "=&D" (d1), "=&S" (d2) \
254         : "0" (n/4),"1" ((long) to),"2" ((long) from) \
255         : "memory");
256 {
257         int d0, d1, d2;
258         switch (n % 4) {
259                 case 0: COMMON(""); return to;
260                 case 1: COMMON("\n\tmovsb"); return to;
261                 case 2: COMMON("\n\tmovsw"); return to;
262                 default: COMMON("\n\tmovsw\n\tmovsb"); return to;
263         }
264 }
265   
266 #undef COMMON
267 }
268
269 #define __HAVE_ARCH_MEMCPY
270
271 #ifdef CONFIG_X86_USE_3DNOW
272
273 #include <asm/mmx.h>
274
275 /*
276  *      This CPU favours 3DNow strongly (eg AMD Athlon)
277  */
278
279 static inline void * __constant_memcpy3d(void * to, const void * from, size_t len)
280 {
281         if (len < 512)
282                 return __constant_memcpy(to, from, len);
283         return _mmx_memcpy(to, from, len);
284 }
285
286 static __inline__ void *__memcpy3d(void *to, const void *from, size_t len)
287 {
288         if (len < 512)
289                 return __memcpy(to, from, len);
290         return _mmx_memcpy(to, from, len);
291 }
292
293 #define memcpy(t, f, n) \
294 (__builtin_constant_p(n) ? \
295  __constant_memcpy3d((t),(f),(n)) : \
296  __memcpy3d((t),(f),(n)))
297
298 #else
299
300 /*
301  *      No 3D Now!
302  */
303  
304 #define memcpy(t, f, n) \
305 (__builtin_constant_p(n) ? \
306  __constant_memcpy((t),(f),(n)) : \
307  __memcpy((t),(f),(n)))
308
309 #endif
310
311 #define __HAVE_ARCH_MEMMOVE
312 void *memmove(void * dest,const void * src, size_t n);
313
314 #define memcmp __builtin_memcmp
315
316 #define __HAVE_ARCH_MEMCHR
317 static inline void * memchr(const void * cs,int c,size_t count)
318 {
319 int d0;
320 register void * __res;
321 if (!count)
322         return NULL;
323 __asm__ __volatile__(
324         "repne\n\t"
325         "scasb\n\t"
326         "je 1f\n\t"
327         "movl $1,%0\n"
328         "1:\tdecl %0"
329         :"=D" (__res), "=&c" (d0) : "a" (c),"0" (cs),"1" (count));
330 return __res;
331 }
332
333 static inline void * __memset_generic(void * s, char c,size_t count)
334 {
335 int d0, d1;
336 __asm__ __volatile__(
337         "rep\n\t"
338         "stosb"
339         : "=&c" (d0), "=&D" (d1)
340         :"a" (c),"1" (s),"0" (count)
341         :"memory");
342 return s;
343 }
344
345 /* we might want to write optimized versions of these later */
346 #define __constant_count_memset(s,c,count) __memset_generic((s),(c),(count))
347
348 /*
349  * memset(x,0,y) is a reasonably common thing to do, so we want to fill
350  * things 32 bits at a time even when we don't know the size of the
351  * area at compile-time..
352  */
353 static inline void * __constant_c_memset(void * s, unsigned long c, size_t count)
354 {
355 int d0, d1;
356 __asm__ __volatile__(
357         "rep ; stosl\n\t"
358         "testb $2,%b3\n\t"
359         "je 1f\n\t"
360         "stosw\n"
361         "1:\ttestb $1,%b3\n\t"
362         "je 2f\n\t"
363         "stosb\n"
364         "2:"
365         : "=&c" (d0), "=&D" (d1)
366         :"a" (c), "q" (count), "0" (count/4), "1" ((long) s)
367         :"memory");
368 return (s);     
369 }
370
371 /* Added by Gertjan van Wingerde to make minix and sysv module work */
372 #define __HAVE_ARCH_STRNLEN
373 static inline size_t strnlen(const char * s, size_t count)
374 {
375 int d0;
376 register int __res;
377 __asm__ __volatile__(
378         "movl %2,%0\n\t"
379         "jmp 2f\n"
380         "1:\tcmpb $0,(%0)\n\t"
381         "je 3f\n\t"
382         "incl %0\n"
383         "2:\tdecl %1\n\t"
384         "cmpl $-1,%1\n\t"
385         "jne 1b\n"
386         "3:\tsubl %2,%0"
387         :"=a" (__res), "=&d" (d0)
388         :"c" (s),"1" (count));
389 return __res;
390 }
391 /* end of additional stuff */
392
393 #define __HAVE_ARCH_STRSTR
394
395 extern char *strstr(const char *cs, const char *ct);
396
397 /*
398  * This looks horribly ugly, but the compiler can optimize it totally,
399  * as we by now know that both pattern and count is constant..
400  */
401 static inline void * __constant_c_and_count_memset(void * s, unsigned long pattern, size_t count)
402 {
403         switch (count) {
404                 case 0:
405                         return s;
406                 case 1:
407                         *(unsigned char *)s = pattern;
408                         return s;
409                 case 2:
410                         *(unsigned short *)s = pattern;
411                         return s;
412                 case 3:
413                         *(unsigned short *)s = pattern;
414                         *(2+(unsigned char *)s) = pattern;
415                         return s;
416                 case 4:
417                         *(unsigned long *)s = pattern;
418                         return s;
419         }
420 #define COMMON(x) \
421 __asm__  __volatile__( \
422         "rep ; stosl" \
423         x \
424         : "=&c" (d0), "=&D" (d1) \
425         : "a" (pattern),"0" (count/4),"1" ((long) s) \
426         : "memory")
427 {
428         int d0, d1;
429         switch (count % 4) {
430                 case 0: COMMON(""); return s;
431                 case 1: COMMON("\n\tstosb"); return s;
432                 case 2: COMMON("\n\tstosw"); return s;
433                 default: COMMON("\n\tstosw\n\tstosb"); return s;
434         }
435 }
436   
437 #undef COMMON
438 }
439
440 #define __constant_c_x_memset(s, c, count) \
441 (__builtin_constant_p(count) ? \
442  __constant_c_and_count_memset((s),(c),(count)) : \
443  __constant_c_memset((s),(c),(count)))
444
445 #define __memset(s, c, count) \
446 (__builtin_constant_p(count) ? \
447  __constant_count_memset((s),(c),(count)) : \
448  __memset_generic((s),(c),(count)))
449
450 #define __HAVE_ARCH_MEMSET
451 #define memset(s, c, count) \
452 (__builtin_constant_p(c) ? \
453  __constant_c_x_memset((s),(0x01010101UL*(unsigned char)(c)),(count)) : \
454  __memset((s),(c),(count)))
455
456 /*
457  * find the first occurrence of byte 'c', or 1 past the area if none
458  */
459 #define __HAVE_ARCH_MEMSCAN
460 static inline void * memscan(void * addr, int c, size_t size)
461 {
462         if (!size)
463                 return addr;
464         __asm__("repnz; scasb\n\t"
465                 "jnz 1f\n\t"
466                 "dec %%edi\n"
467                 "1:"
468                 : "=D" (addr), "=c" (size)
469                 : "0" (addr), "1" (size), "a" (c));
470         return addr;
471 }
472
473 #endif /* __KERNEL__ */
474
475 #endif