home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / elf / d-link.old / i386 / string.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-20  |  9.3 KB  |  440 lines

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