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

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