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.
29 #define __HAVE_ARCH_STRCPY
30 static inline char * strcpy(char * dest,const char *src)
38 : "=&S" (d0), "=&D" (d1), "=&a" (d2)
39 :"0" (src),"1" (dest) : "memory");
43 #define __HAVE_ARCH_STRNCPY
44 static inline char * strncpy(char * dest,const char *src,size_t count)
57 : "=&S" (d0), "=&D" (d1), "=&c" (d2), "=&a" (d3)
58 :"0" (src),"1" (dest),"2" (count) : "memory");
62 #define __HAVE_ARCH_STRCAT
63 static inline char * strcat(char * dest,const char * src)
74 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
75 : "0" (src), "1" (dest), "2" (0), "3" (0xffffffffu):"memory");
79 #define __HAVE_ARCH_STRNCAT
80 static inline char * strncat(char * dest,const char * src,size_t count)
96 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
97 : "0" (src),"1" (dest),"2" (0),"3" (0xffffffffu), "g" (count)
102 #define __HAVE_ARCH_STRCMP
103 static inline int strcmp(const char * cs,const char * ct)
107 __asm__ __volatile__(
111 "testb %%al,%%al\n\t"
113 "xorl %%eax,%%eax\n\t"
115 "2:\tsbbl %%eax,%%eax\n\t"
118 :"=a" (__res), "=&S" (d0), "=&D" (d1)
123 #define __HAVE_ARCH_STRNCMP
124 static inline int strncmp(const char * cs,const char * ct,size_t count)
128 __asm__ __volatile__(
134 "testb %%al,%%al\n\t"
136 "2:\txorl %%eax,%%eax\n\t"
138 "3:\tsbbl %%eax,%%eax\n\t"
141 :"=a" (__res), "=&S" (d0), "=&D" (d1), "=&c" (d2)
142 :"1" (cs),"2" (ct),"3" (count));
146 #define __HAVE_ARCH_STRCHR
147 static inline char * strchr(const char * s, int c)
150 register char * __res;
151 __asm__ __volatile__(
156 "testb %%al,%%al\n\t"
161 :"=a" (__res), "=&S" (d0) : "1" (s),"0" (c));
165 #define __HAVE_ARCH_STRRCHR
166 static inline char * strrchr(const char * s, int c)
169 register char * __res;
170 __asm__ __volatile__(
175 "leal -1(%%esi),%0\n"
176 "2:\ttestb %%al,%%al\n\t"
178 :"=g" (__res), "=&S" (d0), "=&a" (d1) :"0" (0),"1" (s),"2" (c));
182 #define __HAVE_ARCH_STRLEN
183 static inline size_t strlen(const char * s)
187 __asm__ __volatile__(
192 :"=c" (__res), "=&D" (d0) :"1" (s),"a" (0), "0" (0xffffffffu));
196 static inline void * __memcpy(void * to, const void * from, size_t n)
199 __asm__ __volatile__(
204 "1:\ttestb $1,%b4\n\t"
208 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
209 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
215 * This looks horribly ugly, but the compiler can optimize it totally,
216 * as the count is constant.
218 static inline void * __constant_memcpy(void * to, const void * from, size_t n)
221 return __builtin_memcpy(to, from, n);
224 __asm__ __volatile__( \
227 : "=&c" (d0), "=&D" (d1), "=&S" (d2) \
228 : "0" (n/4),"1" ((long) to),"2" ((long) from) \
233 case 0: COMMON(""); return to;
234 case 1: COMMON("\n\tmovsb"); return to;
235 case 2: COMMON("\n\tmovsw"); return to;
236 default: COMMON("\n\tmovsw\n\tmovsb"); return to;
243 #define __HAVE_ARCH_MEMCPY
245 #ifdef CONFIG_X86_USE_3DNOW
250 * This CPU favours 3DNow strongly (eg AMD Athlon)
253 static inline void * __constant_memcpy3d(void * to, const void * from, size_t len)
256 return __constant_memcpy(to, from, len);
257 return _mmx_memcpy(to, from, len);
260 static __inline__ void *__memcpy3d(void *to, const void *from, size_t len)
263 return __memcpy(to, from, len);
264 return _mmx_memcpy(to, from, len);
267 #define memcpy(t, f, n) \
268 (__builtin_constant_p(n) ? \
269 __constant_memcpy3d((t),(f),(n)) : \
270 __memcpy3d((t),(f),(n)))
278 #define memcpy(t, f, n) \
279 (__builtin_constant_p(n) ? \
280 __constant_memcpy((t),(f),(n)) : \
281 __memcpy((t),(f),(n)))
285 #define __HAVE_ARCH_MEMMOVE
286 void *memmove(void * dest,const void * src, size_t n);
288 #define memcmp __builtin_memcmp
290 #define __HAVE_ARCH_MEMCHR
291 static inline void * memchr(const void * cs,int c,size_t count)
294 register void * __res;
297 __asm__ __volatile__(
303 :"=D" (__res), "=&c" (d0) : "a" (c),"0" (cs),"1" (count));
307 static inline void * __memset_generic(void * s, char c,size_t count)
310 __asm__ __volatile__(
313 : "=&c" (d0), "=&D" (d1)
314 :"a" (c),"1" (s),"0" (count)
319 /* we might want to write optimized versions of these later */
320 #define __constant_count_memset(s,c,count) __memset_generic((s),(c),(count))
323 * memset(x,0,y) is a reasonably common thing to do, so we want to fill
324 * things 32 bits at a time even when we don't know the size of the
325 * area at compile-time..
327 static inline void * __constant_c_memset(void * s, unsigned long c, size_t count)
330 __asm__ __volatile__(
335 "1:\ttestb $1,%b3\n\t"
339 : "=&c" (d0), "=&D" (d1)
340 :"a" (c), "q" (count), "0" (count/4), "1" ((long) s)
345 /* Added by Gertjan van Wingerde to make minix and sysv module work */
346 #define __HAVE_ARCH_STRNLEN
347 static inline size_t strnlen(const char * s, size_t count)
351 __asm__ __volatile__(
354 "1:\tcmpb $0,(%0)\n\t"
361 :"=a" (__res), "=&d" (d0)
362 :"c" (s),"1" (count));
365 /* end of additional stuff */
367 #define __HAVE_ARCH_STRSTR
369 extern char *strstr(const char *cs, const char *ct);
372 * This looks horribly ugly, but the compiler can optimize it totally,
373 * as we by now know that both pattern and count is constant..
375 static inline void * __constant_c_and_count_memset(void * s, unsigned long pattern, size_t count)
381 *(unsigned char *)s = pattern;
384 *(unsigned short *)s = pattern;
387 *(unsigned short *)s = pattern;
388 *(2+(unsigned char *)s) = pattern;
391 *(unsigned long *)s = pattern;
395 __asm__ __volatile__( \
398 : "=&c" (d0), "=&D" (d1) \
399 : "a" (pattern),"0" (count/4),"1" ((long) s) \
404 case 0: COMMON(""); return s;
405 case 1: COMMON("\n\tstosb"); return s;
406 case 2: COMMON("\n\tstosw"); return s;
407 default: COMMON("\n\tstosw\n\tstosb"); return s;
414 #define __constant_c_x_memset(s, c, count) \
415 (__builtin_constant_p(count) ? \
416 __constant_c_and_count_memset((s),(c),(count)) : \
417 __constant_c_memset((s),(c),(count)))
419 #define __memset(s, c, count) \
420 (__builtin_constant_p(count) ? \
421 __constant_count_memset((s),(c),(count)) : \
422 __memset_generic((s),(c),(count)))
424 #define __HAVE_ARCH_MEMSET
425 #define memset(s, c, count) \
426 (__builtin_constant_p(c) ? \
427 __constant_c_x_memset((s),(0x01010101UL*(unsigned char)(c)),(count)) : \
428 __memset((s),(c),(count)))
431 * find the first occurrence of byte 'c', or 1 past the area if none
433 #define __HAVE_ARCH_MEMSCAN
434 static inline void * memscan(void * addr, int c, size_t size)
438 __asm__("repnz; scasb\n\t"
442 : "=D" (addr), "=c" (size)
443 : "0" (addr), "1" (size), "a" (c));
447 #endif /* __KERNEL__ */