string.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. "movl %4,%%ecx\n\t"
  189. "andl $3,%%ecx\n\t"
  190. #if 1 /* want to pay 2 byte penalty for a chance to skip microcoded rep? */
  191. "jz 1f\n\t"
  192. #endif
  193. "rep ; movsb\n\t"
  194. "1:"
  195. : "=&c" (d0), "=&D" (d1), "=&S" (d2)
  196. : "0" (n/4), "g" (n), "1" ((long) to), "2" ((long) from)
  197. : "memory");
  198. return (to);
  199. }
  200. /*
  201. * This looks 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. long esi, edi;
  207. if (!n) return to;
  208. #if 1 /* want to do small copies with non-string ops? */
  209. switch (n) {
  210. case 1: *(char*)to = *(char*)from; return to;
  211. case 2: *(short*)to = *(short*)from; return to;
  212. case 4: *(int*)to = *(int*)from; return to;
  213. #if 1 /* including those doable with two moves? */
  214. case 3: *(short*)to = *(short*)from;
  215. *((char*)to+2) = *((char*)from+2); return to;
  216. case 5: *(int*)to = *(int*)from;
  217. *((char*)to+4) = *((char*)from+4); return to;
  218. case 6: *(int*)to = *(int*)from;
  219. *((short*)to+2) = *((short*)from+2); return to;
  220. case 8: *(int*)to = *(int*)from;
  221. *((int*)to+1) = *((int*)from+1); return to;
  222. #endif
  223. }
  224. #endif
  225. esi = (long) from;
  226. edi = (long) to;
  227. if (n >= 5*4) {
  228. /* large block: use rep prefix */
  229. int ecx;
  230. __asm__ __volatile__(
  231. "rep ; movsl"
  232. : "=&c" (ecx), "=&D" (edi), "=&S" (esi)
  233. : "0" (n/4), "1" (edi),"2" (esi)
  234. : "memory"
  235. );
  236. } else {
  237. /* small block: don't clobber ecx + smaller code */
  238. if (n >= 4*4) __asm__ __volatile__("movsl"
  239. :"=&D"(edi),"=&S"(esi):"0"(edi),"1"(esi):"memory");
  240. if (n >= 3*4) __asm__ __volatile__("movsl"
  241. :"=&D"(edi),"=&S"(esi):"0"(edi),"1"(esi):"memory");
  242. if (n >= 2*4) __asm__ __volatile__("movsl"
  243. :"=&D"(edi),"=&S"(esi):"0"(edi),"1"(esi):"memory");
  244. if (n >= 1*4) __asm__ __volatile__("movsl"
  245. :"=&D"(edi),"=&S"(esi):"0"(edi),"1"(esi):"memory");
  246. }
  247. switch (n % 4) {
  248. /* tail */
  249. case 0: return to;
  250. case 1: __asm__ __volatile__("movsb"
  251. :"=&D"(edi),"=&S"(esi):"0"(edi),"1"(esi):"memory");
  252. return to;
  253. case 2: __asm__ __volatile__("movsw"
  254. :"=&D"(edi),"=&S"(esi):"0"(edi),"1"(esi):"memory");
  255. return to;
  256. default: __asm__ __volatile__("movsw\n\tmovsb"
  257. :"=&D"(edi),"=&S"(esi):"0"(edi),"1"(esi):"memory");
  258. return to;
  259. }
  260. }
  261. #define __HAVE_ARCH_MEMCPY
  262. #ifdef CONFIG_X86_USE_3DNOW
  263. #include <asm/mmx.h>
  264. /*
  265. * This CPU favours 3DNow strongly (eg AMD Athlon)
  266. */
  267. static inline void * __constant_memcpy3d(void * to, const void * from, size_t len)
  268. {
  269. if (len < 512)
  270. return __constant_memcpy(to, from, len);
  271. return _mmx_memcpy(to, from, len);
  272. }
  273. static __inline__ void *__memcpy3d(void *to, const void *from, size_t len)
  274. {
  275. if (len < 512)
  276. return __memcpy(to, from, len);
  277. return _mmx_memcpy(to, from, len);
  278. }
  279. #define memcpy(t, f, n) \
  280. (__builtin_constant_p(n) ? \
  281. __constant_memcpy3d((t),(f),(n)) : \
  282. __memcpy3d((t),(f),(n)))
  283. #else
  284. /*
  285. * No 3D Now!
  286. */
  287. #define memcpy(t, f, n) \
  288. (__builtin_constant_p(n) ? \
  289. __constant_memcpy((t),(f),(n)) : \
  290. __memcpy((t),(f),(n)))
  291. #endif
  292. #define __HAVE_ARCH_MEMMOVE
  293. void *memmove(void * dest,const void * src, size_t n);
  294. #define memcmp __builtin_memcmp
  295. #define __HAVE_ARCH_MEMCHR
  296. static inline void * memchr(const void * cs,int c,size_t count)
  297. {
  298. int d0;
  299. register void * __res;
  300. if (!count)
  301. return NULL;
  302. __asm__ __volatile__(
  303. "repne\n\t"
  304. "scasb\n\t"
  305. "je 1f\n\t"
  306. "movl $1,%0\n"
  307. "1:\tdecl %0"
  308. :"=D" (__res), "=&c" (d0) : "a" (c),"0" (cs),"1" (count));
  309. return __res;
  310. }
  311. static inline void * __memset_generic(void * s, char c,size_t count)
  312. {
  313. int d0, d1;
  314. __asm__ __volatile__(
  315. "rep\n\t"
  316. "stosb"
  317. : "=&c" (d0), "=&D" (d1)
  318. :"a" (c),"1" (s),"0" (count)
  319. :"memory");
  320. return s;
  321. }
  322. /* we might want to write optimized versions of these later */
  323. #define __constant_count_memset(s,c,count) __memset_generic((s),(c),(count))
  324. /*
  325. * memset(x,0,y) is a reasonably common thing to do, so we want to fill
  326. * things 32 bits at a time even when we don't know the size of the
  327. * area at compile-time..
  328. */
  329. static inline void * __constant_c_memset(void * s, unsigned long c, size_t count)
  330. {
  331. int d0, d1;
  332. __asm__ __volatile__(
  333. "rep ; stosl\n\t"
  334. "testb $2,%b3\n\t"
  335. "je 1f\n\t"
  336. "stosw\n"
  337. "1:\ttestb $1,%b3\n\t"
  338. "je 2f\n\t"
  339. "stosb\n"
  340. "2:"
  341. : "=&c" (d0), "=&D" (d1)
  342. :"a" (c), "q" (count), "0" (count/4), "1" ((long) s)
  343. :"memory");
  344. return (s);
  345. }
  346. /* Added by Gertjan van Wingerde to make minix and sysv module work */
  347. #define __HAVE_ARCH_STRNLEN
  348. static inline size_t strnlen(const char * s, size_t count)
  349. {
  350. int d0;
  351. register int __res;
  352. __asm__ __volatile__(
  353. "movl %2,%0\n\t"
  354. "jmp 2f\n"
  355. "1:\tcmpb $0,(%0)\n\t"
  356. "je 3f\n\t"
  357. "incl %0\n"
  358. "2:\tdecl %1\n\t"
  359. "cmpl $-1,%1\n\t"
  360. "jne 1b\n"
  361. "3:\tsubl %2,%0"
  362. :"=a" (__res), "=&d" (d0)
  363. :"c" (s),"1" (count));
  364. return __res;
  365. }
  366. /* end of additional stuff */
  367. #define __HAVE_ARCH_STRSTR
  368. extern char *strstr(const char *cs, const char *ct);
  369. /*
  370. * This looks horribly ugly, but the compiler can optimize it totally,
  371. * as we by now know that both pattern and count is constant..
  372. */
  373. static inline void * __constant_c_and_count_memset(void * s, unsigned long pattern, size_t count)
  374. {
  375. switch (count) {
  376. case 0:
  377. return s;
  378. case 1:
  379. *(unsigned char *)s = pattern;
  380. return s;
  381. case 2:
  382. *(unsigned short *)s = pattern;
  383. return s;
  384. case 3:
  385. *(unsigned short *)s = pattern;
  386. *(2+(unsigned char *)s) = pattern;
  387. return s;
  388. case 4:
  389. *(unsigned long *)s = pattern;
  390. return s;
  391. }
  392. #define COMMON(x) \
  393. __asm__ __volatile__( \
  394. "rep ; stosl" \
  395. x \
  396. : "=&c" (d0), "=&D" (d1) \
  397. : "a" (pattern),"0" (count/4),"1" ((long) s) \
  398. : "memory")
  399. {
  400. int d0, d1;
  401. switch (count % 4) {
  402. case 0: COMMON(""); return s;
  403. case 1: COMMON("\n\tstosb"); return s;
  404. case 2: COMMON("\n\tstosw"); return s;
  405. default: COMMON("\n\tstosw\n\tstosb"); return s;
  406. }
  407. }
  408. #undef COMMON
  409. }
  410. #define __constant_c_x_memset(s, c, count) \
  411. (__builtin_constant_p(count) ? \
  412. __constant_c_and_count_memset((s),(c),(count)) : \
  413. __constant_c_memset((s),(c),(count)))
  414. #define __memset(s, c, count) \
  415. (__builtin_constant_p(count) ? \
  416. __constant_count_memset((s),(c),(count)) : \
  417. __memset_generic((s),(c),(count)))
  418. #define __HAVE_ARCH_MEMSET
  419. #define memset(s, c, count) \
  420. (__builtin_constant_p(c) ? \
  421. __constant_c_x_memset((s),(0x01010101UL*(unsigned char)(c)),(count)) : \
  422. __memset((s),(c),(count)))
  423. /*
  424. * find the first occurrence of byte 'c', or 1 past the area if none
  425. */
  426. #define __HAVE_ARCH_MEMSCAN
  427. static inline void * memscan(void * addr, int c, size_t size)
  428. {
  429. if (!size)
  430. return addr;
  431. __asm__("repnz; scasb\n\t"
  432. "jnz 1f\n\t"
  433. "dec %%edi\n"
  434. "1:"
  435. : "=D" (addr), "=c" (size)
  436. : "0" (addr), "1" (size), "a" (c));
  437. return addr;
  438. }
  439. #endif /* __KERNEL__ */
  440. #endif