root/include/linux/string.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. strcpy
  2. strncpy
  3. strcat
  4. strncat
  5. strcmp
  6. strncmp
  7. strchr
  8. strrchr
  9. strspn
  10. strcspn
  11. strpbrk
  12. strstr
  13. strlen
  14. strtok
  15. memcpy
  16. memmove
  17. memcmp
  18. memchr
  19. memset

   1 #ifndef _LINUX_STRING_H_
   2 #define _LINUX_STRING_H_
   3 
   4 #include <linux/types.h>        /* for size_t */
   5 
   6 #ifndef NULL
   7 #define NULL ((void *) 0)
   8 #endif
   9 
  10 /*
  11  * This string-include defines all string functions as inline
  12  * functions. Use gcc. It also assumes ds=es=data space, this should be
  13  * normal. Most of the string-functions are rather heavily hand-optimized,
  14  * see especially strtok,strstr,str[c]spn. They should work, but are not
  15  * very easy to understand. Everything is done entirely within the register
  16  * set, making the functions fast and clean. String instructions have been
  17  * used through-out, making for "slightly" unclear code :-)
  18  *
  19  *              Copyright (C) 1991, 1992 Linus Torvalds
  20  */
  21  
  22 extern inline char * strcpy(char * dest,const char *src)
     /* [previous][next][first][last][top][bottom][index][help] */
  23 {
  24 __asm__("cld\n"
  25         "1:\tlodsb\n\t"
  26         "stosb\n\t"
  27         "testb %%al,%%al\n\t"
  28         "jne 1b"
  29         ::"S" (src),"D" (dest):"si","di","ax","memory");
  30 return dest;
  31 }
  32 
  33 extern inline char * strncpy(char * dest,const char *src,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
  34 {
  35 __asm__("cld\n"
  36         "1:\tdecl %2\n\t"
  37         "js 2f\n\t"
  38         "lodsb\n\t"
  39         "stosb\n\t"
  40         "testb %%al,%%al\n\t"
  41         "jne 1b\n\t"
  42         "rep\n\t"
  43         "stosb\n"
  44         "2:"
  45         ::"S" (src),"D" (dest),"c" (count):"si","di","ax","cx","memory");
  46 return dest;
  47 }
  48 
  49 extern inline char * strcat(char * dest,const char * src)
     /* [previous][next][first][last][top][bottom][index][help] */
  50 {
  51 __asm__("cld\n\t"
  52         "repne\n\t"
  53         "scasb\n\t"
  54         "decl %1\n"
  55         "1:\tlodsb\n\t"
  56         "stosb\n\t"
  57         "testb %%al,%%al\n\t"
  58         "jne 1b"
  59         ::"S" (src),"D" (dest),"a" (0),"c" (0xffffffff):"si","di","ax","cx");
  60 return dest;
  61 }
  62 
  63 extern inline char * strncat(char * dest,const char * src,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
  64 {
  65 __asm__("cld\n\t"
  66         "repne\n\t"
  67         "scasb\n\t"
  68         "decl %1\n\t"
  69         "movl %4,%3\n"
  70         "1:\tdecl %3\n\t"
  71         "js 2f\n\t"
  72         "lodsb\n\t"
  73         "stosb\n\t"
  74         "testb %%al,%%al\n\t"
  75         "jne 1b\n"
  76         "2:\txorl %2,%2\n\t"
  77         "stosb"
  78         ::"S" (src),"D" (dest),"a" (0),"c" (0xffffffff),"g" (count)
  79         :"si","di","ax","cx","memory");
  80 return dest;
  81 }
  82 
  83 extern inline int strcmp(const char * cs,const char * ct)
     /* [previous][next][first][last][top][bottom][index][help] */
  84 {
  85 register int __res __asm__("ax");
  86 __asm__("cld\n"
  87         "1:\tlodsb\n\t"
  88         "scasb\n\t"
  89         "jne 2f\n\t"
  90         "testb %%al,%%al\n\t"
  91         "jne 1b\n\t"
  92         "xorl %%eax,%%eax\n\t"
  93         "jmp 3f\n"
  94         "2:\tmovl $1,%%eax\n\t"
  95         "jb 3f\n\t"
  96         "negl %%eax\n"
  97         "3:"
  98         :"=a" (__res):"D" (cs),"S" (ct):"si","di");
  99 return __res;
 100 }
 101 
 102 extern inline int strncmp(const char * cs,const char * ct,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 103 {
 104 register int __res __asm__("ax");
 105 __asm__("cld\n"
 106         "1:\tdecl %3\n\t"
 107         "js 2f\n\t"
 108         "lodsb\n\t"
 109         "scasb\n\t"
 110         "jne 3f\n\t"
 111         "testb %%al,%%al\n\t"
 112         "jne 1b\n"
 113         "2:\txorl %%eax,%%eax\n\t"
 114         "jmp 4f\n"
 115         "3:\tmovl $1,%%eax\n\t"
 116         "jb 4f\n\t"
 117         "negl %%eax\n"
 118         "4:"
 119         :"=a" (__res):"D" (cs),"S" (ct),"c" (count):"si","di","cx");
 120 return __res;
 121 }
 122 
 123 extern inline char * strchr(const char * s,char c)
     /* [previous][next][first][last][top][bottom][index][help] */
 124 {
 125 register char * __res __asm__("ax");
 126 __asm__("cld\n\t"
 127         "movb %%al,%%ah\n"
 128         "1:\tlodsb\n\t"
 129         "cmpb %%ah,%%al\n\t"
 130         "je 2f\n\t"
 131         "testb %%al,%%al\n\t"
 132         "jne 1b\n\t"
 133         "movl $1,%1\n"
 134         "2:\tmovl %1,%0\n\t"
 135         "decl %0"
 136         :"=a" (__res):"S" (s),"0" (c):"si");
 137 return __res;
 138 }
 139 
 140 extern inline char * strrchr(const char * s,char c)
     /* [previous][next][first][last][top][bottom][index][help] */
 141 {
 142 register char * __res __asm__("dx");
 143 __asm__("cld\n\t"
 144         "movb %%al,%%ah\n"
 145         "1:\tlodsb\n\t"
 146         "cmpb %%ah,%%al\n\t"
 147         "jne 2f\n\t"
 148         "movl %%esi,%0\n\t"
 149         "decl %0\n"
 150         "2:\ttestb %%al,%%al\n\t"
 151         "jne 1b"
 152         :"=d" (__res):"0" (0),"S" (s),"a" (c):"ax","si");
 153 return __res;
 154 }
 155 
 156 extern inline size_t strspn(const char * cs, const char * ct)
     /* [previous][next][first][last][top][bottom][index][help] */
 157 {
 158 register char * __res __asm__("si");
 159 __asm__("cld\n\t"
 160         "movl %4,%%edi\n\t"
 161         "repne\n\t"
 162         "scasb\n\t"
 163         "notl %%ecx\n\t"
 164         "decl %%ecx\n\t"
 165         "movl %%ecx,%%edx\n"
 166         "1:\tlodsb\n\t"
 167         "testb %%al,%%al\n\t"
 168         "je 2f\n\t"
 169         "movl %4,%%edi\n\t"
 170         "movl %%edx,%%ecx\n\t"
 171         "repne\n\t"
 172         "scasb\n\t"
 173         "je 1b\n"
 174         "2:\tdecl %0"
 175         :"=S" (__res):"a" (0),"c" (0xffffffff),"0" (cs),"g" (ct)
 176         :"ax","cx","dx","di");
 177 return __res-cs;
 178 }
 179 
 180 extern inline size_t strcspn(const char * cs, const char * ct)
     /* [previous][next][first][last][top][bottom][index][help] */
 181 {
 182 register char * __res __asm__("si");
 183 __asm__("cld\n\t"
 184         "movl %4,%%edi\n\t"
 185         "repne\n\t"
 186         "scasb\n\t"
 187         "notl %%ecx\n\t"
 188         "decl %%ecx\n\t"
 189         "movl %%ecx,%%edx\n"
 190         "1:\tlodsb\n\t"
 191         "testb %%al,%%al\n\t"
 192         "je 2f\n\t"
 193         "movl %4,%%edi\n\t"
 194         "movl %%edx,%%ecx\n\t"
 195         "repne\n\t"
 196         "scasb\n\t"
 197         "jne 1b\n"
 198         "2:\tdecl %0"
 199         :"=S" (__res):"a" (0),"c" (0xffffffff),"0" (cs),"g" (ct)
 200         :"ax","cx","dx","di");
 201 return __res-cs;
 202 }
 203 
 204 extern inline char * strpbrk(const char * cs,const char * ct)
     /* [previous][next][first][last][top][bottom][index][help] */
 205 {
 206 register char * __res __asm__("si");
 207 __asm__("cld\n\t"
 208         "movl %4,%%edi\n\t"
 209         "repne\n\t"
 210         "scasb\n\t"
 211         "notl %%ecx\n\t"
 212         "decl %%ecx\n\t"
 213         "movl %%ecx,%%edx\n"
 214         "1:\tlodsb\n\t"
 215         "testb %%al,%%al\n\t"
 216         "je 2f\n\t"
 217         "movl %4,%%edi\n\t"
 218         "movl %%edx,%%ecx\n\t"
 219         "repne\n\t"
 220         "scasb\n\t"
 221         "jne 1b\n\t"
 222         "decl %0\n\t"
 223         "jmp 3f\n"
 224         "2:\txorl %0,%0\n"
 225         "3:"
 226         :"=S" (__res):"a" (0),"c" (0xffffffff),"0" (cs),"g" (ct)
 227         :"ax","cx","dx","di");
 228 return __res;
 229 }
 230 
 231 extern inline char * strstr(const char * cs,const char * ct)
     /* [previous][next][first][last][top][bottom][index][help] */
 232 {
 233 register char * __res __asm__("ax");
 234 __asm__("cld\n\t" \
 235         "movl %4,%%edi\n\t"
 236         "repne\n\t"
 237         "scasb\n\t"
 238         "notl %%ecx\n\t"
 239         "decl %%ecx\n\t"        /* NOTE! This also sets Z if searchstring='' */
 240         "movl %%ecx,%%edx\n"
 241         "1:\tmovl %4,%%edi\n\t"
 242         "movl %%esi,%%eax\n\t"
 243         "movl %%edx,%%ecx\n\t"
 244         "repe\n\t"
 245         "cmpsb\n\t"
 246         "je 2f\n\t"             /* also works for empty string, see above */
 247         "xchgl %%eax,%%esi\n\t"
 248         "incl %%esi\n\t"
 249         "cmpb $0,-1(%%eax)\n\t"
 250         "jne 1b\n\t"
 251         "xorl %%eax,%%eax\n\t"
 252         "2:"
 253         :"=a" (__res):"0" (0),"c" (0xffffffff),"S" (cs),"g" (ct)
 254         :"cx","dx","di","si");
 255 return __res;
 256 }
 257 
 258 extern inline size_t strlen(const char * s)
     /* [previous][next][first][last][top][bottom][index][help] */
 259 {
 260 register int __res __asm__("cx");
 261 __asm__("cld\n\t"
 262         "repne\n\t"
 263         "scasb\n\t"
 264         "notl %0\n\t"
 265         "decl %0"
 266         :"=c" (__res):"D" (s),"a" (0),"0" (0xffffffff):"di");
 267 return __res;
 268 }
 269 
 270 extern char * ___strtok;
 271 
 272 extern inline char * strtok(char * s,const char * ct)
     /* [previous][next][first][last][top][bottom][index][help] */
 273 {
 274 register char * __res;
 275 __asm__("testl %1,%1\n\t"
 276         "jne 1f\n\t"
 277         "testl %0,%0\n\t"
 278         "je 8f\n\t"
 279         "movl %0,%1\n"
 280         "1:\txorl %0,%0\n\t"
 281         "movl $-1,%%ecx\n\t"
 282         "xorl %%eax,%%eax\n\t"
 283         "cld\n\t"
 284         "movl %4,%%edi\n\t"
 285         "repne\n\t"
 286         "scasb\n\t"
 287         "notl %%ecx\n\t"
 288         "decl %%ecx\n\t"
 289         "je 7f\n\t"                     /* empty delimeter-string */
 290         "movl %%ecx,%%edx\n"
 291         "2:\tlodsb\n\t"
 292         "testb %%al,%%al\n\t"
 293         "je 7f\n\t"
 294         "movl %4,%%edi\n\t"
 295         "movl %%edx,%%ecx\n\t"
 296         "repne\n\t"
 297         "scasb\n\t"
 298         "je 2b\n\t"
 299         "decl %1\n\t"
 300         "cmpb $0,(%1)\n\t"
 301         "je 7f\n\t"
 302         "movl %1,%0\n"
 303         "3:\tlodsb\n\t"
 304         "testb %%al,%%al\n\t"
 305         "je 5f\n\t"
 306         "movl %4,%%edi\n\t"
 307         "movl %%edx,%%ecx\n\t"
 308         "repne\n\t"
 309         "scasb\n\t"
 310         "jne 3b\n\t"
 311         "decl %1\n\t"
 312         "cmpb $0,(%1)\n\t"
 313         "je 5f\n\t"
 314         "movb $0,(%1)\n\t"
 315         "incl %1\n\t"
 316         "jmp 6f\n"
 317         "5:\txorl %1,%1\n"
 318         "6:\tcmpb $0,(%0)\n\t"
 319         "jne 7f\n\t"
 320         "xorl %0,%0\n"
 321         "7:\ttestl %0,%0\n\t"
 322         "jne 8f\n\t"
 323         "movl %0,%1\n"
 324         "8:"
 325         :"=b" (__res),"=S" (___strtok)
 326         :"0" (___strtok),"1" (s),"g" (ct)
 327         :"ax","cx","dx","di","memory");
 328 return __res;
 329 }
 330 
 331 extern inline void * memcpy(void * to, const void * from, size_t n)
     /* [previous][next][first][last][top][bottom][index][help] */
 332 {
 333 __asm__("cld\n\t"
 334         "movl %%edx, %%ecx\n\t"
 335         "shrl $2,%%ecx\n\t"
 336         "rep ; movsl\n\t"
 337         "testb $1,%%dl\n\t"
 338         "je 1f\n\t"
 339         "movsb\n"
 340         "1:\ttestb $2,%%dl\n\t"
 341         "je 2f\n\t"
 342         "movsw\n"
 343         "2:\n"
 344         ::"d" (n),"D" ((long) to),"S" ((long) from)
 345         : "cx","di","si","memory");
 346 return (to);
 347 }
 348 
 349 extern inline void * memmove(void * dest,const void * src, size_t n)
     /* [previous][next][first][last][top][bottom][index][help] */
 350 {
 351 if (dest<src)
 352 __asm__("cld\n\t"
 353         "rep\n\t"
 354         "movsb"
 355         ::"c" (n),"S" (src),"D" (dest)
 356         :"cx","si","di");
 357 else
 358 __asm__("std\n\t"
 359         "rep\n\t"
 360         "movsb\n\t"
 361         "cld"
 362         ::"c" (n),"S" (src+n-1),"D" (dest+n-1)
 363         :"cx","si","di","memory");
 364 return dest;
 365 }
 366 
 367 extern inline int memcmp(const void * cs,const void * ct,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 368 {
 369 register int __res __asm__("ax");
 370 __asm__("cld\n\t"
 371         "repe\n\t"
 372         "cmpsb\n\t"
 373         "je 1f\n\t"
 374         "movl $1,%%eax\n\t"
 375         "jb 1f\n\t"
 376         "negl %%eax\n"
 377         "1:"
 378         :"=a" (__res):"0" (0),"D" (cs),"S" (ct),"c" (count)
 379         :"si","di","cx");
 380 return __res;
 381 }
 382 
 383 extern inline void * memchr(const void * cs,char c,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 384 {
 385 register void * __res __asm__("di");
 386 if (!count)
 387         return NULL;
 388 __asm__("cld\n\t"
 389         "repne\n\t"
 390         "scasb\n\t"
 391         "je 1f\n\t"
 392         "movl $1,%0\n"
 393         "1:\tdecl %0"
 394         :"=D" (__res):"a" (c),"D" (cs),"c" (count)
 395         :"cx");
 396 return __res;
 397 }
 398 
 399 extern inline void * memset(void * s,char c,size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 400 {
 401 __asm__("cld\n\t"
 402         "rep\n\t"
 403         "stosb"
 404         ::"a" (c),"D" (s),"c" (count)
 405         :"cx","di","memory");
 406 return s;
 407 }
 408 
 409 #endif

/* [previous][next][first][last][top][bottom][index][help] */