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

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