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

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