string.h 11 KB

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