1 #ifndef _I386_STRING_H_
2 #define _I386_STRING_H_
6 * On a 486 or Pentium, we are better off not using the
7 * byte string operations. But on a 386 or a PPro the
8 * byte string ops are faster than doing it by hand
9 * (MUCH faster on a Pentium).
13 * This string-include defines all string functions as inline
14 * functions. Use gcc. It also assumes ds=es=data space, this should be
15 * normal. Most of the string-functions are rather heavily hand-optimized,
16 * see especially strsep,strstr,str[c]spn. They should work, but are not
17 * very easy to understand. Everything is done entirely within the register
18 * set, making the functions fast and clean. String instructions have been
19 * used through-out, making for "slightly" unclear code :-)
21 * NO Copyright (C) 1991, 1992 Linus Torvalds,
22 * consider these trivial functions to be PD.
25 /* AK: in fact I bet it would be better to move this stuff all out of line.
28 #define __HAVE_ARCH_STRCPY
29 static inline char * strcpy(char * dest
,const char *src
)
37 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
)
38 :"0" (src
),"1" (dest
) : "memory");
42 #define __HAVE_ARCH_STRNCPY
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");
61 #define __HAVE_ARCH_STRCAT
62 static inline char * strcat(char * dest
,const char * src
)
73 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
74 : "0" (src
), "1" (dest
), "2" (0), "3" (0xffffffffu
):"memory");
78 #define __HAVE_ARCH_STRNCAT
79 static inline char * strncat(char * dest
,const char * src
,size_t count
)
95 : "=&S" (d0
), "=&D" (d1
), "=&a" (d2
), "=&c" (d3
)
96 : "0" (src
),"1" (dest
),"2" (0),"3" (0xffffffffu
), "g" (count
)
101 #define __HAVE_ARCH_STRCMP
102 static inline int strcmp(const char * cs
,const char * ct
)
106 __asm__
__volatile__(
110 "testb %%al,%%al\n\t"
112 "xorl %%eax,%%eax\n\t"
114 "2:\tsbbl %%eax,%%eax\n\t"
117 :"=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
)
147 #define __HAVE_ARCH_STRCHR
148 static inline char * strchr(const char * s
, int c
)
151 register char * __res
;
152 __asm__
__volatile__(
157 "testb %%al,%%al\n\t"
162 :"=a" (__res
), "=&S" (d0
)
168 #define __HAVE_ARCH_STRRCHR
169 static inline char * strrchr(const char * s
, int c
)
172 register char * __res
;
173 __asm__
__volatile__(
178 "leal -1(%%esi),%0\n"
179 "2:\ttestb %%al,%%al\n\t"
181 :"=g" (__res
), "=&S" (d0
), "=&a" (d1
)
182 :"0" (0),"1" (s
),"2" (c
)
187 #define __HAVE_ARCH_STRLEN
188 static inline size_t strlen(const char * s
)
192 __asm__
__volatile__(
197 :"=c" (__res
), "=&D" (d0
)
198 :"1" (s
),"a" (0), "0" (0xffffffffu
)
203 static __always_inline
void * __memcpy(void * to
, const void * from
, size_t n
)
206 __asm__
__volatile__(
210 #if 1 /* want to pay 2 byte penalty for a chance to skip microcoded rep? */
215 : "=&c" (d0
), "=&D" (d1
), "=&S" (d2
)
216 : "0" (n
/4), "g" (n
), "1" ((long) to
), "2" ((long) from
)
222 * This looks ugly, but the compiler can optimize it totally,
223 * as the count is constant.
225 static __always_inline
void * __constant_memcpy(void * to
, const void * from
, size_t n
)
229 #if 1 /* want to do small copies with non-string ops? */
231 case 1: *(char*)to
= *(char*)from
; return to
;
232 case 2: *(short*)to
= *(short*)from
; return to
;
233 case 4: *(int*)to
= *(int*)from
; return to
;
234 #if 1 /* including those doable with two moves? */
235 case 3: *(short*)to
= *(short*)from
;
236 *((char*)to
+2) = *((char*)from
+2); return to
;
237 case 5: *(int*)to
= *(int*)from
;
238 *((char*)to
+4) = *((char*)from
+4); return to
;
239 case 6: *(int*)to
= *(int*)from
;
240 *((short*)to
+2) = *((short*)from
+2); return to
;
241 case 8: *(int*)to
= *(int*)from
;
242 *((int*)to
+1) = *((int*)from
+1); return to
;
249 /* large block: use rep prefix */
251 __asm__
__volatile__(
253 : "=&c" (ecx
), "=&D" (edi
), "=&S" (esi
)
254 : "0" (n
/4), "1" (edi
),"2" (esi
)
258 /* small block: don't clobber ecx + smaller code */
259 if (n
>= 4*4) __asm__
__volatile__("movsl"
260 :"=&D"(edi
),"=&S"(esi
):"0"(edi
),"1"(esi
):"memory");
261 if (n
>= 3*4) __asm__
__volatile__("movsl"
262 :"=&D"(edi
),"=&S"(esi
):"0"(edi
),"1"(esi
):"memory");
263 if (n
>= 2*4) __asm__
__volatile__("movsl"
264 :"=&D"(edi
),"=&S"(esi
):"0"(edi
),"1"(esi
):"memory");
265 if (n
>= 1*4) __asm__
__volatile__("movsl"
266 :"=&D"(edi
),"=&S"(esi
):"0"(edi
),"1"(esi
):"memory");
271 case 1: __asm__
__volatile__("movsb"
272 :"=&D"(edi
),"=&S"(esi
):"0"(edi
),"1"(esi
):"memory");
274 case 2: __asm__
__volatile__("movsw"
275 :"=&D"(edi
),"=&S"(esi
):"0"(edi
),"1"(esi
):"memory");
277 default: __asm__
__volatile__("movsw\n\tmovsb"
278 :"=&D"(edi
),"=&S"(esi
):"0"(edi
),"1"(esi
):"memory");
283 #define __HAVE_ARCH_MEMCPY
285 #ifdef CONFIG_X86_USE_3DNOW
290 * This CPU favours 3DNow strongly (eg AMD Athlon)
293 static inline void * __constant_memcpy3d(void * to
, const void * from
, size_t len
)
296 return __constant_memcpy(to
, from
, len
);
297 return _mmx_memcpy(to
, from
, len
);
300 static __inline__
void *__memcpy3d(void *to
, const void *from
, size_t len
)
303 return __memcpy(to
, from
, len
);
304 return _mmx_memcpy(to
, from
, len
);
307 #define memcpy(t, f, n) \
308 (__builtin_constant_p(n) ? \
309 __constant_memcpy3d((t),(f),(n)) : \
310 __memcpy3d((t),(f),(n)))
318 #define memcpy(t, f, n) \
319 (__builtin_constant_p(n) ? \
320 __constant_memcpy((t),(f),(n)) : \
321 __memcpy((t),(f),(n)))
325 #define __HAVE_ARCH_MEMMOVE
326 void *memmove(void * dest
,const void * src
, size_t n
);
328 #define memcmp __builtin_memcmp
330 #define __HAVE_ARCH_MEMCHR
331 static inline void * memchr(const void * cs
,int c
,size_t count
)
334 register void * __res
;
337 __asm__
__volatile__(
343 :"=D" (__res
), "=&c" (d0
)
344 :"a" (c
),"0" (cs
),"1" (count
)
349 static inline void * __memset_generic(void * s
, char c
,size_t count
)
352 __asm__
__volatile__(
355 : "=&c" (d0
), "=&D" (d1
)
356 :"a" (c
),"1" (s
),"0" (count
)
361 /* we might want to write optimized versions of these later */
362 #define __constant_count_memset(s,c,count) __memset_generic((s),(c),(count))
365 * memset(x,0,y) is a reasonably common thing to do, so we want to fill
366 * things 32 bits at a time even when we don't know the size of the
367 * area at compile-time..
369 static __always_inline
void * __constant_c_memset(void * s
, unsigned long c
, size_t count
)
372 __asm__
__volatile__(
377 "1:\ttestb $1,%b3\n\t"
381 :"=&c" (d0
), "=&D" (d1
)
382 :"a" (c
), "q" (count
), "0" (count
/4), "1" ((long) s
)
387 /* Added by Gertjan van Wingerde to make minix and sysv module work */
388 #define __HAVE_ARCH_STRNLEN
389 static inline size_t strnlen(const char * s
, size_t count
)
393 __asm__
__volatile__(
396 "1:\tcmpb $0,(%0)\n\t"
403 :"=a" (__res
), "=&d" (d0
)
408 /* end of additional stuff */
410 #define __HAVE_ARCH_STRSTR
412 extern char *strstr(const char *cs
, const char *ct
);
415 * This looks horribly ugly, but the compiler can optimize it totally,
416 * as we by now know that both pattern and count is constant..
418 static __always_inline
void * __constant_c_and_count_memset(void * s
, unsigned long pattern
, size_t count
)
424 *(unsigned char *)s
= pattern
;
427 *(unsigned short *)s
= pattern
;
430 *(unsigned short *)s
= pattern
;
431 *(2+(unsigned char *)s
) = pattern
;
434 *(unsigned long *)s
= pattern
;
438 __asm__ __volatile__( \
441 : "=&c" (d0), "=&D" (d1) \
442 : "a" (pattern),"0" (count/4),"1" ((long) s) \
447 case 0: COMMON(""); return s
;
448 case 1: COMMON("\n\tstosb"); return s
;
449 case 2: COMMON("\n\tstosw"); return s
;
450 default: COMMON("\n\tstosw\n\tstosb"); return s
;
457 #define __constant_c_x_memset(s, c, count) \
458 (__builtin_constant_p(count) ? \
459 __constant_c_and_count_memset((s),(c),(count)) : \
460 __constant_c_memset((s),(c),(count)))
462 #define __memset(s, c, count) \
463 (__builtin_constant_p(count) ? \
464 __constant_count_memset((s),(c),(count)) : \
465 __memset_generic((s),(c),(count)))
467 #define __HAVE_ARCH_MEMSET
468 #define memset(s, c, count) \
469 (__builtin_constant_p(c) ? \
470 __constant_c_x_memset((s),(0x01010101UL*(unsigned char)(c)),(count)) : \
471 __memset((s),(c),(count)))
474 * find the first occurrence of byte 'c', or 1 past the area if none
476 #define __HAVE_ARCH_MEMSCAN
477 static inline void * memscan(void * addr
, int c
, size_t size
)
481 __asm__("repnz; scasb\n\t"
485 : "=D" (addr
), "=c" (size
)
486 : "0" (addr
), "1" (size
), "a" (c
)
491 #endif /* __KERNEL__ */