1 #ifndef _I386_STRING_H_
2 #define _I386_STRING_H_
5 #include <linux/config.h>
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 static inline char * strcpy(char * dest,const char *src)
38 : "=&S" (d0), "=&D" (d1), "=&a" (d2)
39 :"0" (src),"1" (dest) : "memory");
43 static inline char * strncpy(char * dest,const char *src,size_t count)
56 : "=&S" (d0), "=&D" (d1), "=&c" (d2), "=&a" (d3)
57 :"0" (src),"1" (dest),"2" (count) : "memory");
62 * This is a more generic variant of strncpy_count() suitable for
63 * implementing string-access routines with all sorts of return
64 * code semantics. It's used by mm/usercopy.c.
66 static inline size_t strncpy_count(char * dest,const char *src,size_t count)
79 :"S" (src),"D" (dest),"0" (count) : "memory");
84 static inline char * strcat(char * dest,const char * src)
95 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
96 : "0" (src), "1" (dest), "2" (0), "3" (0xffffffffu):"memory");
100 static inline char * strncat(char * dest,const char * src,size_t count)
103 __asm__ __volatile__(
112 "testb %%al,%%al\n\t"
116 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
117 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffffu), "g" (count)
122 static inline int strcmp(const char * cs,const char * ct)
126 __asm__ __volatile__(
130 "testb %%al,%%al\n\t"
132 "xorl %%eax,%%eax\n\t"
134 "2:\tsbbl %%eax,%%eax\n\t"
137 :"=a" (__res), "=&S" (d0), "=&D" (d1)
142 static inline int strncmp(const char * cs,const char * ct,size_t count)
146 __asm__ __volatile__(
152 "testb %%al,%%al\n\t"
154 "2:\txorl %%eax,%%eax\n\t"
156 "3:\tsbbl %%eax,%%eax\n\t"
159 :"=a" (__res), "=&S" (d0), "=&D" (d1), "=&c" (d2)
160 :"1" (cs),"2" (ct),"3" (count));
164 static inline char * strchr(const char * s, int c)
167 register char * __res;
168 __asm__ __volatile__(
173 "testb %%al,%%al\n\t"
178 :"=a" (__res), "=&S" (d0) : "1" (s),"0" (c));
182 static inline char * strrchr(const char * s, int c)
185 register char * __res;
186 __asm__ __volatile__(
191 "leal -1(%%esi),%0\n"
192 "2:\ttestb %%al,%%al\n\t"
194 :"=g" (__res), "=&S" (d0), "=&a" (d1) :"0" (0),"1" (s),"2" (c));
200 #define __HAVE_ARCH_STRLEN
201 static inline size_t strlen(const char * s)
205 __asm__ __volatile__(
210 :"=c" (__res), "=&D" (d0) :"1" (s),"a" (0), "0" (0xffffffffu));
214 static inline void * __memcpy(void * to, const void * from, size_t n)
217 __asm__ __volatile__(
222 "1:\ttestb $1,%b4\n\t"
226 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
227 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
233 * This looks horribly ugly, but the compiler can optimize it totally,
234 * as the count is constant.
236 static inline void * __constant_memcpy(void * to, const void * from, size_t n)
239 return __builtin_memcpy(to, from, n);
242 __asm__ __volatile__( \
245 : "=&c" (d0), "=&D" (d1), "=&S" (d2) \
246 : "0" (n/4),"1" ((long) to),"2" ((long) from) \
251 case 0: COMMON(""); return to;
252 case 1: COMMON("\n\tmovsb"); return to;
253 case 2: COMMON("\n\tmovsw"); return to;
254 default: COMMON("\n\tmovsw\n\tmovsb"); return to;
261 #define __HAVE_ARCH_MEMCPY
263 #ifdef CONFIG_X86_USE_3DNOW
268 * This CPU favours 3DNow strongly (eg AMD Athlon)
271 static inline void * __constant_memcpy3d(void * to, const void * from, size_t len)
274 return __constant_memcpy(to, from, len);
275 return _mmx_memcpy(to, from, len);
278 static __inline__ void *__memcpy3d(void *to, const void *from, size_t len)
281 return __memcpy(to, from, len);
282 return _mmx_memcpy(to, from, len);
285 #define memcpy(t, f, n) \
286 (__builtin_constant_p(n) ? \
287 __constant_memcpy3d((t),(f),(n)) : \
288 __memcpy3d((t),(f),(n)))
296 #define memcpy(t, f, n) \
297 (__builtin_constant_p(n) ? \
298 __constant_memcpy((t),(f),(n)) : \
299 __memcpy((t),(f),(n)))
303 #define __HAVE_ARCH_MEMMOVE
304 void *memmove(void * dest,const void * src, size_t n);
306 #define memcmp __builtin_memcmp
308 #define __HAVE_ARCH_MEMCHR
309 static inline void * memchr(const void * cs,int c,size_t count)
312 register void * __res;
315 __asm__ __volatile__(
321 :"=D" (__res), "=&c" (d0) : "a" (c),"0" (cs),"1" (count));
325 static inline void * __memset_generic(void * s, char c,size_t count)
328 __asm__ __volatile__(
331 : "=&c" (d0), "=&D" (d1)
332 :"a" (c),"1" (s),"0" (count)
337 /* we might want to write optimized versions of these later */
338 #define __constant_count_memset(s,c,count) __memset_generic((s),(c),(count))
341 * memset(x,0,y) is a reasonably common thing to do, so we want to fill
342 * things 32 bits at a time even when we don't know the size of the
343 * area at compile-time..
345 static inline void * __constant_c_memset(void * s, unsigned long c, size_t count)
348 __asm__ __volatile__(
353 "1:\ttestb $1,%b3\n\t"
357 : "=&c" (d0), "=&D" (d1)
358 :"a" (c), "q" (count), "0" (count/4), "1" ((long) s)
363 /* Added by Gertjan van Wingerde to make minix and sysv module work */
364 #define __HAVE_ARCH_STRNLEN
365 static inline size_t strnlen(const char * s, size_t count)
369 __asm__ __volatile__(
372 "1:\tcmpb $0,(%0)\n\t"
379 :"=a" (__res), "=&d" (d0)
380 :"c" (s),"1" (count));
383 /* end of additional stuff */
385 #define __HAVE_ARCH_STRSTR
387 extern char *strstr(const char *cs, const char *ct);
390 * This looks horribly ugly, but the compiler can optimize it totally,
391 * as we by now know that both pattern and count is constant..
393 static inline void * __constant_c_and_count_memset(void * s, unsigned long pattern, size_t count)
399 *(unsigned char *)s = pattern;
402 *(unsigned short *)s = pattern;
405 *(unsigned short *)s = pattern;
406 *(2+(unsigned char *)s) = pattern;
409 *(unsigned long *)s = pattern;
413 __asm__ __volatile__( \
416 : "=&c" (d0), "=&D" (d1) \
417 : "a" (pattern),"0" (count/4),"1" ((long) s) \
422 case 0: COMMON(""); return s;
423 case 1: COMMON("\n\tstosb"); return s;
424 case 2: COMMON("\n\tstosw"); return s;
425 default: COMMON("\n\tstosw\n\tstosb"); return s;
432 #define __constant_c_x_memset(s, c, count) \
433 (__builtin_constant_p(count) ? \
434 __constant_c_and_count_memset((s),(c),(count)) : \
435 __constant_c_memset((s),(c),(count)))
437 #define __memset(s, c, count) \
438 (__builtin_constant_p(count) ? \
439 __constant_count_memset((s),(c),(count)) : \
440 __memset_generic((s),(c),(count)))
442 #define __HAVE_ARCH_MEMSET
443 #define memset(s, c, count) \
444 (__builtin_constant_p(c) ? \
445 __constant_c_x_memset((s),(0x01010101UL*(unsigned char)(c)),(count)) : \
446 __memset((s),(c),(count)))
449 * find the first occurrence of byte 'c', or 1 past the area if none
451 #define __HAVE_ARCH_MEMSCAN
452 static inline void * memscan(void * addr, int c, size_t size)
456 __asm__("repnz; scasb\n\t"
460 : "=D" (addr), "=c" (size)
461 : "0" (addr), "1" (size), "a" (c));
465 #endif /* __KERNEL__ */