string.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. #ifndef _I386_STRING_H_
  2. #define _I386_STRING_H_
  3. #ifdef __KERNEL__
  4. #include <linux/config.h>
  5. /*
  6. * On a 486 or Pentium, we are better off not using the
  7. * byte string operations. But on a 386 or a PPro the
  8. * byte string ops are faster than doing it by hand
  9. * (MUCH faster on a Pentium).
  10. */
  11. /*
  12. * This string-include defines all string functions as inline
  13. * functions. Use gcc. It also assumes ds=es=data space, this should be
  14. * normal. Most of the string-functions are rather heavily hand-optimized,
  15. * see especially strsep,strstr,str[c]spn. They should work, but are not
  16. * very easy to understand. Everything is done entirely within the register
  17. * set, making the functions fast and clean. String instructions have been
  18. * used through-out, making for "slightly" unclear code :-)
  19. *
  20. * NO Copyright (C) 1991, 1992 Linus Torvalds,
  21. * consider these trivial functions to be PD.
  22. */
  23. /* AK: in fact I bet it would be better to move this stuff all out of line.
  24. */
  25. #define __HAVE_ARCH_STRCPY
  26. static inline char * strcpy(char * dest,const char *src)
  27. {
  28. int d0, d1, d2;
  29. __asm__ __volatile__(
  30. "1:\tlodsb\n\t"
  31. "stosb\n\t"
  32. "testb %%al,%%al\n\t"
  33. "jne 1b"
  34. : "=&S" (d0), "=&D" (d1), "=&a" (d2)
  35. :"0" (src),"1" (dest) : "memory");
  36. return dest;
  37. }
  38. #define __HAVE_ARCH_STRNCPY
  39. static inline char * strncpy(char * dest,const char *src,size_t count)
  40. {
  41. int d0, d1, d2, d3;
  42. __asm__ __volatile__(
  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. : "=&S" (d0), "=&D" (d1), "=&c" (d2), "=&a" (d3)
  53. :"0" (src),"1" (dest),"2" (count) : "memory");
  54. return dest;
  55. }
  56. #define __HAVE_ARCH_STRCAT
  57. static inline char * strcat(char * dest,const char * src)
  58. {
  59. int d0, d1, d2, d3;
  60. __asm__ __volatile__(
  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. : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
  69. : "0" (src), "1" (dest), "2" (0), "3" (0xffffffffu):"memory");
  70. return dest;
  71. }
  72. #define __HAVE_ARCH_STRNCAT
  73. static inline char * strncat(char * dest,const char * src,size_t count)
  74. {
  75. int d0, d1, d2, d3;
  76. __asm__ __volatile__(
  77. "repne\n\t"
  78. "scasb\n\t"
  79. "decl %1\n\t"
  80. "movl %8,%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. : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
  90. : "0" (src),"1" (dest),"2" (0),"3" (0xffffffffu), "g" (count)
  91. : "memory");
  92. return dest;
  93. }
  94. #define __HAVE_ARCH_STRCMP
  95. static inline int strcmp(const char * cs,const char * ct)
  96. {
  97. int d0, d1;
  98. register int __res;
  99. __asm__ __volatile__(
  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,%%al\n"
  109. "3:"
  110. :"=a" (__res), "=&S" (d0), "=&D" (d1)
  111. :"1" (cs),"2" (ct));
  112. return __res;
  113. }
  114. #define __HAVE_ARCH_STRNCMP
  115. static inline int strncmp(const char * cs,const char * ct,size_t count)
  116. {
  117. register int __res;
  118. int d0, d1, d2;
  119. __asm__ __volatile__(
  120. "1:\tdecl %3\n\t"
  121. "js 2f\n\t"
  122. "lodsb\n\t"
  123. "scasb\n\t"
  124. "jne 3f\n\t"
  125. "testb %%al,%%al\n\t"
  126. "jne 1b\n"
  127. "2:\txorl %%eax,%%eax\n\t"
  128. "jmp 4f\n"
  129. "3:\tsbbl %%eax,%%eax\n\t"
  130. "orb $1,%%al\n"
  131. "4:"
  132. :"=a" (__res), "=&S" (d0), "=&D" (d1), "=&c" (d2)
  133. :"1" (cs),"2" (ct),"3" (count));
  134. return __res;
  135. }
  136. #define __HAVE_ARCH_STRCHR
  137. static inline char * strchr(const char * s, int c)
  138. {
  139. int d0;
  140. register char * __res;
  141. __asm__ __volatile__(
  142. "movb %%al,%%ah\n"
  143. "1:\tlodsb\n\t"
  144. "cmpb %%ah,%%al\n\t"
  145. "je 2f\n\t"
  146. "testb %%al,%%al\n\t"
  147. "jne 1b\n\t"
  148. "movl $1,%1\n"
  149. "2:\tmovl %1,%0\n\t"
  150. "decl %0"
  151. :"=a" (__res), "=&S" (d0) : "1" (s),"0" (c));
  152. return __res;
  153. }
  154. #define __HAVE_ARCH_STRRCHR
  155. static inline char * strrchr(const char * s, int c)
  156. {
  157. int d0, d1;
  158. register char * __res;
  159. __asm__ __volatile__(
  160. "movb %%al,%%ah\n"
  161. "1:\tlodsb\n\t"
  162. "cmpb %%ah,%%al\n\t"
  163. "jne 2f\n\t"
  164. "leal -1(%%esi),%0\n"
  165. "2:\ttestb %%al,%%al\n\t"
  166. "jne 1b"
  167. :"=g" (__res), "=&S" (d0), "=&a" (d1) :"0" (0),"1" (s),"2" (c));
  168. return __res;
  169. }
  170. #define __HAVE_ARCH_STRLEN
  171. static inline size_t strlen(const char * s)
  172. {
  173. int d0;
  174. register int __res;
  175. __asm__ __volatile__(
  176. "repne\n\t"
  177. "scasb\n\t"
  178. "notl %0\n\t"
  179. "decl %0"
  180. :"=c" (__res), "=&D" (d0) :"1" (s),"a" (0), "0" (0xffffffffu));
  181. return __res;
  182. }
  183. static inline void * __memcpy(void * to, const void * from, size_t n)
  184. {
  185. int d0, d1, d2;
  186. __asm__ __volatile__(
  187. "rep ; movsl\n\t"
  188. "testb $2,%b4\n\t"
  189. "je 1f\n\t"
  190. "movsw\n"
  191. "1:\ttestb $1,%b4\n\t"
  192. "je 2f\n\t"
  193. "movsb\n"
  194. "2:"
  195. : "=&c" (d0), "=&D" (d1), "=&S" (d2)
  196. :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
  197. : "memory");
  198. return (to);
  199. }
  200. /*
  201. * This looks horribly ugly, but the compiler can optimize it totally,
  202. * as the count is constant.
  203. */
  204. static inline void * __constant_memcpy(void * to, const void * from, size_t n)
  205. {
  206. if (n <= 128)
  207. return __builtin_memcpy(to, from, n);
  208. #define COMMON(x) \
  209. __asm__ __volatile__( \
  210. "rep ; movsl" \
  211. x \
  212. : "=&c" (d0), "=&D" (d1), "=&S" (d2) \
  213. : "0" (n/4),"1" ((long) to),"2" ((long) from) \
  214. : "memory");
  215. {
  216. int d0, d1, d2;
  217. switch (n % 4) {
  218. case 0: COMMON(""); return to;
  219. case 1: COMMON("\n\tmovsb"); return to;
  220. case 2: COMMON("\n\tmovsw"); return to;
  221. default: COMMON("\n\tmovsw\n\tmovsb"); return to;
  222. }
  223. }
  224. #undef COMMON
  225. }
  226. #define __HAVE_ARCH_MEMCPY
  227. #ifdef CONFIG_X86_USE_3DNOW
  228. #include <asm/mmx.h>
  229. /*
  230. * This CPU favours 3DNow strongly (eg AMD Athlon)
  231. */
  232. static inline void * __constant_memcpy3d(void * to, const void * from, size_t len)
  233. {
  234. if (len < 512)
  235. return __constant_memcpy(to, from, len);
  236. return _mmx_memcpy(to, from, len);
  237. }
  238. static __inline__ void *__memcpy3d(void *to, const void *from, size_t len)
  239. {
  240. if (len < 512)
  241. return __memcpy(to, from, len);
  242. return _mmx_memcpy(to, from, len);
  243. }
  244. #define memcpy(t, f, n) \
  245. (__builtin_constant_p(n) ? \
  246. __constant_memcpy3d((t),(f),(n)) : \
  247. __memcpy3d((t),(f),(n)))
  248. #else
  249. /*
  250. * No 3D Now!
  251. */
  252. #define memcpy(t, f, n) \
  253. (__builtin_constant_p(n) ? \
  254. __constant_memcpy((t),(f),(n)) : \
  255. __memcpy((t),(f),(n)))
  256. #endif
  257. #define __HAVE_ARCH_MEMMOVE
  258. void *memmove(void * dest,const void * src, size_t n);
  259. #define memcmp __builtin_memcmp
  260. #define __HAVE_ARCH_MEMCHR
  261. static inline void * memchr(const void * cs,int c,size_t count)
  262. {
  263. int d0;
  264. register void * __res;
  265. if (!count)
  266. return NULL;
  267. __asm__ __volatile__(
  268. "repne\n\t"
  269. "scasb\n\t"
  270. "je 1f\n\t"
  271. "movl $1,%0\n"
  272. "1:\tdecl %0"
  273. :"=D" (__res), "=&c" (d0) : "a" (c),"0" (cs),"1" (count));
  274. return __res;
  275. }
  276. static inline void * __memset_generic(void * s, char c,size_t count)
  277. {
  278. int d0, d1;
  279. __asm__ __volatile__(
  280. "rep\n\t"
  281. "stosb"
  282. : "=&c" (d0), "=&D" (d1)
  283. :"a" (c),"1" (s),"0" (count)
  284. :"memory");
  285. return s;
  286. }
  287. /* we might want to write optimized versions of these later */
  288. #define __constant_count_memset(s,c,count) __memset_generic((s),(c),(count))
  289. /*
  290. * memset(x,0,y) is a reasonably common thing to do, so we want to fill
  291. * things 32 bits at a time even when we don't know the size of the
  292. * area at compile-time..
  293. */
  294. static inline void * __constant_c_memset(void * s, unsigned long c, size_t count)
  295. {
  296. int d0, d1;
  297. __asm__ __volatile__(
  298. "rep ; stosl\n\t"
  299. "testb $2,%b3\n\t"
  300. "je 1f\n\t"
  301. "stosw\n"
  302. "1:\ttestb $1,%b3\n\t"
  303. "je 2f\n\t"
  304. "stosb\n"
  305. "2:"
  306. : "=&c" (d0), "=&D" (d1)
  307. :"a" (c), "q" (count), "0" (count/4), "1" ((long) s)
  308. :"memory");
  309. return (s);
  310. }
  311. /* Added by Gertjan van Wingerde to make minix and sysv module work */
  312. #define __HAVE_ARCH_STRNLEN
  313. static inline size_t strnlen(const char * s, size_t count)
  314. {
  315. int d0;
  316. register int __res;
  317. __asm__ __volatile__(
  318. "movl %2,%0\n\t"
  319. "jmp 2f\n"
  320. "1:\tcmpb $0,(%0)\n\t"
  321. "je 3f\n\t"
  322. "incl %0\n"
  323. "2:\tdecl %1\n\t"
  324. "cmpl $-1,%1\n\t"
  325. "jne 1b\n"
  326. "3:\tsubl %2,%0"
  327. :"=a" (__res), "=&d" (d0)
  328. :"c" (s),"1" (count));
  329. return __res;
  330. }
  331. /* end of additional stuff */
  332. #define __HAVE_ARCH_STRSTR
  333. extern char *strstr(const char *cs, const char *ct);
  334. /*
  335. * This looks horribly ugly, but the compiler can optimize it totally,
  336. * as we by now know that both pattern and count is constant..
  337. */
  338. static inline void * __constant_c_and_count_memset(void * s, unsigned long pattern, size_t count)
  339. {
  340. switch (count) {
  341. case 0:
  342. return s;
  343. case 1:
  344. *(unsigned char *)s = pattern;
  345. return s;
  346. case 2:
  347. *(unsigned short *)s = pattern;
  348. return s;
  349. case 3:
  350. *(unsigned short *)s = pattern;
  351. *(2+(unsigned char *)s) = pattern;
  352. return s;
  353. case 4:
  354. *(unsigned long *)s = pattern;
  355. return s;
  356. }
  357. #define COMMON(x) \
  358. __asm__ __volatile__( \
  359. "rep ; stosl" \
  360. x \
  361. : "=&c" (d0), "=&D" (d1) \
  362. : "a" (pattern),"0" (count/4),"1" ((long) s) \
  363. : "memory")
  364. {
  365. int d0, d1;
  366. switch (count % 4) {
  367. case 0: COMMON(""); return s;
  368. case 1: COMMON("\n\tstosb"); return s;
  369. case 2: COMMON("\n\tstosw"); return s;
  370. default: COMMON("\n\tstosw\n\tstosb"); return s;
  371. }
  372. }
  373. #undef COMMON
  374. }
  375. #define __constant_c_x_memset(s, c, count) \
  376. (__builtin_constant_p(count) ? \
  377. __constant_c_and_count_memset((s),(c),(count)) : \
  378. __constant_c_memset((s),(c),(count)))
  379. #define __memset(s, c, count) \
  380. (__builtin_constant_p(count) ? \
  381. __constant_count_memset((s),(c),(count)) : \
  382. __memset_generic((s),(c),(count)))
  383. #define __HAVE_ARCH_MEMSET
  384. #define memset(s, c, count) \
  385. (__builtin_constant_p(c) ? \
  386. __constant_c_x_memset((s),(0x01010101UL*(unsigned char)(c)),(count)) : \
  387. __memset((s),(c),(count)))
  388. /*
  389. * find the first occurrence of byte 'c', or 1 past the area if none
  390. */
  391. #define __HAVE_ARCH_MEMSCAN
  392. static inline void * memscan(void * addr, int c, size_t size)
  393. {
  394. if (!size)
  395. return addr;
  396. __asm__("repnz; scasb\n\t"
  397. "jnz 1f\n\t"
  398. "dec %%edi\n"
  399. "1:"
  400. : "=D" (addr), "=c" (size)
  401. : "0" (addr), "1" (size), "a" (c));
  402. return addr;
  403. }
  404. #endif /* __KERNEL__ */
  405. #endif