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

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