string_32.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Most of the string-functions are rather heavily hand-optimized,
  3. * see especially strsep,strstr,str[c]spn. They should work, but are not
  4. * very easy to understand. Everything is done entirely within the register
  5. * set, making the functions fast and clean. String instructions have been
  6. * used through-out, making for "slightly" unclear code :-)
  7. *
  8. * AK: On P4 and K7 using non string instruction implementations might be faster
  9. * for large memory blocks. But most of them are unlikely to be used on large
  10. * strings.
  11. */
  12. #include <linux/string.h>
  13. #include <linux/module.h>
  14. #ifdef __HAVE_ARCH_STRCPY
  15. char *strcpy(char *dest, const char *src)
  16. {
  17. int d0, d1, d2;
  18. asm volatile("1:\tlodsb\n\t"
  19. "stosb\n\t"
  20. "testb %%al,%%al\n\t"
  21. "jne 1b"
  22. : "=&S" (d0), "=&D" (d1), "=&a" (d2)
  23. : "0" (src), "1" (dest) : "memory");
  24. return dest;
  25. }
  26. EXPORT_SYMBOL(strcpy);
  27. #endif
  28. #ifdef __HAVE_ARCH_STRNCPY
  29. char *strncpy(char *dest, const char *src, size_t count)
  30. {
  31. int d0, d1, d2, d3;
  32. asm volatile("1:\tdecl %2\n\t"
  33. "js 2f\n\t"
  34. "lodsb\n\t"
  35. "stosb\n\t"
  36. "testb %%al,%%al\n\t"
  37. "jne 1b\n\t"
  38. "rep\n\t"
  39. "stosb\n"
  40. "2:"
  41. : "=&S" (d0), "=&D" (d1), "=&c" (d2), "=&a" (d3)
  42. : "0" (src), "1" (dest), "2" (count) : "memory");
  43. return dest;
  44. }
  45. EXPORT_SYMBOL(strncpy);
  46. #endif
  47. #ifdef __HAVE_ARCH_STRCAT
  48. char *strcat(char *dest, const char *src)
  49. {
  50. int d0, d1, d2, d3;
  51. asm volatile("repne\n\t"
  52. "scasb\n\t"
  53. "decl %1\n"
  54. "1:\tlodsb\n\t"
  55. "stosb\n\t"
  56. "testb %%al,%%al\n\t"
  57. "jne 1b"
  58. : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
  59. : "0" (src), "1" (dest), "2" (0), "3" (0xffffffffu) : "memory");
  60. return dest;
  61. }
  62. EXPORT_SYMBOL(strcat);
  63. #endif
  64. #ifdef __HAVE_ARCH_STRNCAT
  65. char *strncat(char *dest, const char *src, size_t count)
  66. {
  67. int d0, d1, d2, d3;
  68. asm volatile("repne\n\t"
  69. "scasb\n\t"
  70. "decl %1\n\t"
  71. "movl %8,%3\n"
  72. "1:\tdecl %3\n\t"
  73. "js 2f\n\t"
  74. "lodsb\n\t"
  75. "stosb\n\t"
  76. "testb %%al,%%al\n\t"
  77. "jne 1b\n"
  78. "2:\txorl %2,%2\n\t"
  79. "stosb"
  80. : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
  81. : "0" (src), "1" (dest), "2" (0), "3" (0xffffffffu), "g" (count)
  82. : "memory");
  83. return dest;
  84. }
  85. EXPORT_SYMBOL(strncat);
  86. #endif
  87. #ifdef __HAVE_ARCH_STRCMP
  88. int strcmp(const char *cs, const char *ct)
  89. {
  90. int d0, d1;
  91. int res;
  92. asm volatile("1:\tlodsb\n\t"
  93. "scasb\n\t"
  94. "jne 2f\n\t"
  95. "testb %%al,%%al\n\t"
  96. "jne 1b\n\t"
  97. "xorl %%eax,%%eax\n\t"
  98. "jmp 3f\n"
  99. "2:\tsbbl %%eax,%%eax\n\t"
  100. "orb $1,%%al\n"
  101. "3:"
  102. : "=a" (res), "=&S" (d0), "=&D" (d1)
  103. : "1" (cs), "2" (ct)
  104. : "memory");
  105. return res;
  106. }
  107. EXPORT_SYMBOL(strcmp);
  108. #endif
  109. #ifdef __HAVE_ARCH_STRNCMP
  110. int strncmp(const char *cs, const char *ct, size_t count)
  111. {
  112. int res;
  113. int d0, d1, d2;
  114. asm volatile("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:\tsbbl %%eax,%%eax\n\t"
  124. "orb $1,%%al\n"
  125. "4:"
  126. : "=a" (res), "=&S" (d0), "=&D" (d1), "=&c" (d2)
  127. : "1" (cs), "2" (ct), "3" (count)
  128. : "memory");
  129. return res;
  130. }
  131. EXPORT_SYMBOL(strncmp);
  132. #endif
  133. #ifdef __HAVE_ARCH_STRCHR
  134. char *strchr(const char *s, int c)
  135. {
  136. int d0;
  137. char *res;
  138. asm volatile("movb %%al,%%ah\n"
  139. "1:\tlodsb\n\t"
  140. "cmpb %%ah,%%al\n\t"
  141. "je 2f\n\t"
  142. "testb %%al,%%al\n\t"
  143. "jne 1b\n\t"
  144. "movl $1,%1\n"
  145. "2:\tmovl %1,%0\n\t"
  146. "decl %0"
  147. : "=a" (res), "=&S" (d0)
  148. : "1" (s), "0" (c)
  149. : "memory");
  150. return res;
  151. }
  152. EXPORT_SYMBOL(strchr);
  153. #endif
  154. #ifdef __HAVE_ARCH_STRLEN
  155. size_t strlen(const char *s)
  156. {
  157. int d0;
  158. size_t res;
  159. asm volatile("repne\n\t"
  160. "scasb"
  161. : "=c" (res), "=&D" (d0)
  162. : "1" (s), "a" (0), "0" (0xffffffffu)
  163. : "memory");
  164. return ~res - 1;
  165. }
  166. EXPORT_SYMBOL(strlen);
  167. #endif
  168. #ifdef __HAVE_ARCH_MEMCHR
  169. void *memchr(const void *cs, int c, size_t count)
  170. {
  171. int d0;
  172. void *res;
  173. if (!count)
  174. return NULL;
  175. asm volatile("repne\n\t"
  176. "scasb\n\t"
  177. "je 1f\n\t"
  178. "movl $1,%0\n"
  179. "1:\tdecl %0"
  180. : "=D" (res), "=&c" (d0)
  181. : "a" (c), "0" (cs), "1" (count)
  182. : "memory");
  183. return res;
  184. }
  185. EXPORT_SYMBOL(memchr);
  186. #endif
  187. #ifdef __HAVE_ARCH_MEMSCAN
  188. void *memscan(void *addr, int c, size_t size)
  189. {
  190. if (!size)
  191. return addr;
  192. asm volatile("repnz; scasb\n\t"
  193. "jnz 1f\n\t"
  194. "dec %%edi\n"
  195. "1:"
  196. : "=D" (addr), "=c" (size)
  197. : "0" (addr), "1" (size), "a" (c)
  198. : "memory");
  199. return addr;
  200. }
  201. EXPORT_SYMBOL(memscan);
  202. #endif
  203. #ifdef __HAVE_ARCH_STRNLEN
  204. size_t strnlen(const char *s, size_t count)
  205. {
  206. int d0;
  207. int res;
  208. asm volatile("movl %2,%0\n\t"
  209. "jmp 2f\n"
  210. "1:\tcmpb $0,(%0)\n\t"
  211. "je 3f\n\t"
  212. "incl %0\n"
  213. "2:\tdecl %1\n\t"
  214. "cmpl $-1,%1\n\t"
  215. "jne 1b\n"
  216. "3:\tsubl %2,%0"
  217. : "=a" (res), "=&d" (d0)
  218. : "c" (s), "1" (count)
  219. : "memory");
  220. return res;
  221. }
  222. EXPORT_SYMBOL(strnlen);
  223. #endif