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 #define __HAVE_ARCH_STRCPY
31 static inline char * strcpy(char * dest,const char *src)
39 : "=&S" (d0), "=&D" (d1), "=&a" (d2)
40 :"0" (src),"1" (dest) : "memory");
44 #define __HAVE_ARCH_STRNCPY
45 static inline char * strncpy(char * dest,const char *src,size_t count)
58 : "=&S" (d0), "=&D" (d1), "=&c" (d2), "=&a" (d3)
59 :"0" (src),"1" (dest),"2" (count) : "memory");
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.
68 static inline size_t strncpy_count(char * dest,const char *src,size_t count)
81 :"S" (src),"D" (dest),"0" (count) : "memory");
86 #define __HAVE_ARCH_STRCAT
87 static inline char * strcat(char * dest,const char * src)
98 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
99 : "0" (src), "1" (dest), "2" (0), "3" (0xffffffffu):"memory");
103 #define __HAVE_ARCH_STRNCAT
104 static inline char * strncat(char * dest,const char * src,size_t count)
107 __asm__ __volatile__(
116 "testb %%al,%%al\n\t"
120 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
121 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffffu), "g" (count)
126 #define __HAVE_ARCH_STRCMP
127 static inline int strcmp(const char * cs,const char * ct)
131 __asm__ __volatile__(
135 "testb %%al,%%al\n\t"
137 "xorl %%eax,%%eax\n\t"
139 "2:\tsbbl %%eax,%%eax\n\t"
142 :"=a" (__res), "=&S" (d0), "=&D" (d1)
147 #define __HAVE_ARCH_STRNCMP
148 static inline int strncmp(const char * cs,const char * ct,size_t count)
152 __asm__ __volatile__(
158 "testb %%al,%%al\n\t"
160 "2:\txorl %%eax,%%eax\n\t"
162 "3:\tsbbl %%eax,%%eax\n\t"
165 :"=a" (__res), "=&S" (d0), "=&D" (d1), "=&c" (d2)
166 :"1" (cs),"2" (ct),"3" (count));
170 #define __HAVE_ARCH_STRCHR
171 static inline char * strchr(const char * s, int c)
174 register char * __res;
175 __asm__ __volatile__(
180 "testb %%al,%%al\n\t"
185 :"=a" (__res), "=&S" (d0) : "1" (s),"0" (c));
189 #define __HAVE_ARCH_STRRCHR
190 static inline char * strrchr(const char * s, int c)
193 register char * __res;
194 __asm__ __volatile__(
199 "leal -1(%%esi),%0\n"
200 "2:\ttestb %%al,%%al\n\t"
202 :"=g" (__res), "=&S" (d0), "=&a" (d1) :"0" (0),"1" (s),"2" (c));
208 #define __HAVE_ARCH_STRLEN
209 static inline size_t strlen(const char * s)
213 __asm__ __volatile__(
218 :"=c" (__res), "=&D" (d0) :"1" (s),"a" (0), "0" (0xffffffffu));
222 static inline void * __memcpy(void * to, const void * from, size_t n)
225 __asm__ __volatile__(
230 "1:\ttestb $1,%b4\n\t"
234 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
235 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
241 * This looks horribly ugly, but the compiler can optimize it totally,
242 * as the count is constant.
244 static inline void * __constant_memcpy(void * to, const void * from, size_t n)
247 return __builtin_memcpy(to, from, n);
250 __asm__ __volatile__( \
253 : "=&c" (d0), "=&D" (d1), "=&S" (d2) \
254 : "0" (n/4),"1" ((long) to),"2" ((long) from) \
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;
269 #define __HAVE_ARCH_MEMCPY
271 #ifdef CONFIG_X86_USE_3DNOW
276 * This CPU favours 3DNow strongly (eg AMD Athlon)
279 static inline void * __constant_memcpy3d(void * to, const void * from, size_t len)
282 return __constant_memcpy(to, from, len);
283 return _mmx_memcpy(to, from, len);
286 static __inline__ void *__memcpy3d(void *to, const void *from, size_t len)
289 return __memcpy(to, from, len);
290 return _mmx_memcpy(to, from, len);
293 #define memcpy(t, f, n) \
294 (__builtin_constant_p(n) ? \
295 __constant_memcpy3d((t),(f),(n)) : \
296 __memcpy3d((t),(f),(n)))
304 #define memcpy(t, f, n) \
305 (__builtin_constant_p(n) ? \
306 __constant_memcpy((t),(f),(n)) : \
307 __memcpy((t),(f),(n)))
311 #define __HAVE_ARCH_MEMMOVE
312 void *memmove(void * dest,const void * src, size_t n);
314 #define memcmp __builtin_memcmp
316 #define __HAVE_ARCH_MEMCHR
317 static inline void * memchr(const void * cs,int c,size_t count)
320 register void * __res;
323 __asm__ __volatile__(
329 :"=D" (__res), "=&c" (d0) : "a" (c),"0" (cs),"1" (count));
333 static inline void * __memset_generic(void * s, char c,size_t count)
336 __asm__ __volatile__(
339 : "=&c" (d0), "=&D" (d1)
340 :"a" (c),"1" (s),"0" (count)
345 /* we might want to write optimized versions of these later */
346 #define __constant_count_memset(s,c,count) __memset_generic((s),(c),(count))
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..
353 static inline void * __constant_c_memset(void * s, unsigned long c, size_t count)
356 __asm__ __volatile__(
361 "1:\ttestb $1,%b3\n\t"
365 : "=&c" (d0), "=&D" (d1)
366 :"a" (c), "q" (count), "0" (count/4), "1" ((long) s)
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)
377 __asm__ __volatile__(
380 "1:\tcmpb $0,(%0)\n\t"
387 :"=a" (__res), "=&d" (d0)
388 :"c" (s),"1" (count));
391 /* end of additional stuff */
393 #define __HAVE_ARCH_STRSTR
395 extern char *strstr(const char *cs, const char *ct);
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..
401 static inline void * __constant_c_and_count_memset(void * s, unsigned long pattern, size_t count)
407 *(unsigned char *)s = pattern;
410 *(unsigned short *)s = pattern;
413 *(unsigned short *)s = pattern;
414 *(2+(unsigned char *)s) = pattern;
417 *(unsigned long *)s = pattern;
421 __asm__ __volatile__( \
424 : "=&c" (d0), "=&D" (d1) \
425 : "a" (pattern),"0" (count/4),"1" ((long) s) \
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;
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)))
445 #define __memset(s, c, count) \
446 (__builtin_constant_p(count) ? \
447 __constant_count_memset((s),(c),(count)) : \
448 __memset_generic((s),(c),(count)))
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)))
457 * find the first occurrence of byte 'c', or 1 past the area if none
459 #define __HAVE_ARCH_MEMSCAN
460 static inline void * memscan(void * addr, int c, size_t size)
464 __asm__("repnz; scasb\n\t"
468 : "=D" (addr), "=c" (size)
469 : "0" (addr), "1" (size), "a" (c));
473 #endif /* __KERNEL__ */