string_32.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #ifndef _I386_STRING_H_
  2. #define _I386_STRING_H_
  3. #ifdef __KERNEL__
  4. /* Let gcc decide wether to inline or use the out of line functions */
  5. #define __HAVE_ARCH_STRCPY
  6. extern char *strcpy(char *dest, const char *src);
  7. #define __HAVE_ARCH_STRNCPY
  8. extern char *strncpy(char *dest, const char *src, size_t count);
  9. #define __HAVE_ARCH_STRCAT
  10. extern char *strcat(char *dest, const char *src);
  11. #define __HAVE_ARCH_STRNCAT
  12. extern char *strncat(char *dest, const char *src, size_t count);
  13. #define __HAVE_ARCH_STRCMP
  14. extern int strcmp(const char *cs, const char *ct);
  15. #define __HAVE_ARCH_STRNCMP
  16. extern int strncmp(const char *cs, const char *ct, size_t count);
  17. #define __HAVE_ARCH_STRCHR
  18. extern char *strchr(const char *s, int c);
  19. #define __HAVE_ARCH_STRLEN
  20. extern size_t strlen(const char *s);
  21. static __always_inline void * __memcpy(void * to, const void * from, size_t n)
  22. {
  23. int d0, d1, d2;
  24. __asm__ __volatile__(
  25. "rep ; movsl\n\t"
  26. "movl %4,%%ecx\n\t"
  27. "andl $3,%%ecx\n\t"
  28. "jz 1f\n\t"
  29. "rep ; movsb\n\t"
  30. "1:"
  31. : "=&c" (d0), "=&D" (d1), "=&S" (d2)
  32. : "0" (n/4), "g" (n), "1" ((long) to), "2" ((long) from)
  33. : "memory");
  34. return (to);
  35. }
  36. /*
  37. * This looks ugly, but the compiler can optimize it totally,
  38. * as the count is constant.
  39. */
  40. static __always_inline void * __constant_memcpy(void * to, const void * from, size_t n)
  41. {
  42. long esi, edi;
  43. if (!n) return to;
  44. #if 1 /* want to do small copies with non-string ops? */
  45. switch (n) {
  46. case 1: *(char*)to = *(char*)from; return to;
  47. case 2: *(short*)to = *(short*)from; return to;
  48. case 4: *(int*)to = *(int*)from; return to;
  49. #if 1 /* including those doable with two moves? */
  50. case 3: *(short*)to = *(short*)from;
  51. *((char*)to+2) = *((char*)from+2); return to;
  52. case 5: *(int*)to = *(int*)from;
  53. *((char*)to+4) = *((char*)from+4); return to;
  54. case 6: *(int*)to = *(int*)from;
  55. *((short*)to+2) = *((short*)from+2); return to;
  56. case 8: *(int*)to = *(int*)from;
  57. *((int*)to+1) = *((int*)from+1); return to;
  58. #endif
  59. }
  60. #endif
  61. esi = (long) from;
  62. edi = (long) to;
  63. if (n >= 5*4) {
  64. /* large block: use rep prefix */
  65. int ecx;
  66. __asm__ __volatile__(
  67. "rep ; movsl"
  68. : "=&c" (ecx), "=&D" (edi), "=&S" (esi)
  69. : "0" (n/4), "1" (edi),"2" (esi)
  70. : "memory"
  71. );
  72. } else {
  73. /* small block: don't clobber ecx + smaller code */
  74. if (n >= 4*4) __asm__ __volatile__("movsl"
  75. :"=&D"(edi),"=&S"(esi):"0"(edi),"1"(esi):"memory");
  76. if (n >= 3*4) __asm__ __volatile__("movsl"
  77. :"=&D"(edi),"=&S"(esi):"0"(edi),"1"(esi):"memory");
  78. if (n >= 2*4) __asm__ __volatile__("movsl"
  79. :"=&D"(edi),"=&S"(esi):"0"(edi),"1"(esi):"memory");
  80. if (n >= 1*4) __asm__ __volatile__("movsl"
  81. :"=&D"(edi),"=&S"(esi):"0"(edi),"1"(esi):"memory");
  82. }
  83. switch (n % 4) {
  84. /* tail */
  85. case 0: return to;
  86. case 1: __asm__ __volatile__("movsb"
  87. :"=&D"(edi),"=&S"(esi):"0"(edi),"1"(esi):"memory");
  88. return to;
  89. case 2: __asm__ __volatile__("movsw"
  90. :"=&D"(edi),"=&S"(esi):"0"(edi),"1"(esi):"memory");
  91. return to;
  92. default: __asm__ __volatile__("movsw\n\tmovsb"
  93. :"=&D"(edi),"=&S"(esi):"0"(edi),"1"(esi):"memory");
  94. return to;
  95. }
  96. }
  97. #define __HAVE_ARCH_MEMCPY
  98. #ifdef CONFIG_X86_USE_3DNOW
  99. #include <asm/mmx.h>
  100. /*
  101. * This CPU favours 3DNow strongly (eg AMD Athlon)
  102. */
  103. static inline void * __constant_memcpy3d(void * to, const void * from, size_t len)
  104. {
  105. if (len < 512)
  106. return __constant_memcpy(to, from, len);
  107. return _mmx_memcpy(to, from, len);
  108. }
  109. static __inline__ void *__memcpy3d(void *to, const void *from, size_t len)
  110. {
  111. if (len < 512)
  112. return __memcpy(to, from, len);
  113. return _mmx_memcpy(to, from, len);
  114. }
  115. #define memcpy(t, f, n) \
  116. (__builtin_constant_p(n) ? \
  117. __constant_memcpy3d((t),(f),(n)) : \
  118. __memcpy3d((t),(f),(n)))
  119. #else
  120. /*
  121. * No 3D Now!
  122. */
  123. #define memcpy(t, f, n) \
  124. (__builtin_constant_p(n) ? \
  125. __constant_memcpy((t),(f),(n)) : \
  126. __memcpy((t),(f),(n)))
  127. #endif
  128. #define __HAVE_ARCH_MEMMOVE
  129. void *memmove(void * dest,const void * src, size_t n);
  130. #define memcmp __builtin_memcmp
  131. #define __HAVE_ARCH_MEMCHR
  132. extern void *memchr(const void * cs,int c,size_t count);
  133. static inline void * __memset_generic(void * s, char c,size_t count)
  134. {
  135. int d0, d1;
  136. __asm__ __volatile__(
  137. "rep\n\t"
  138. "stosb"
  139. : "=&c" (d0), "=&D" (d1)
  140. :"a" (c),"1" (s),"0" (count)
  141. :"memory");
  142. return s;
  143. }
  144. /* we might want to write optimized versions of these later */
  145. #define __constant_count_memset(s,c,count) __memset_generic((s),(c),(count))
  146. /*
  147. * memset(x,0,y) is a reasonably common thing to do, so we want to fill
  148. * things 32 bits at a time even when we don't know the size of the
  149. * area at compile-time..
  150. */
  151. static __always_inline void * __constant_c_memset(void * s, unsigned long c, size_t count)
  152. {
  153. int d0, d1;
  154. __asm__ __volatile__(
  155. "rep ; stosl\n\t"
  156. "testb $2,%b3\n\t"
  157. "je 1f\n\t"
  158. "stosw\n"
  159. "1:\ttestb $1,%b3\n\t"
  160. "je 2f\n\t"
  161. "stosb\n"
  162. "2:"
  163. :"=&c" (d0), "=&D" (d1)
  164. :"a" (c), "q" (count), "0" (count/4), "1" ((long) s)
  165. :"memory");
  166. return (s);
  167. }
  168. /* Added by Gertjan van Wingerde to make minix and sysv module work */
  169. #define __HAVE_ARCH_STRNLEN
  170. extern size_t strnlen(const char * s, size_t count);
  171. /* end of additional stuff */
  172. #define __HAVE_ARCH_STRSTR
  173. extern char *strstr(const char *cs, const char *ct);
  174. /*
  175. * This looks horribly ugly, but the compiler can optimize it totally,
  176. * as we by now know that both pattern and count is constant..
  177. */
  178. static __always_inline void * __constant_c_and_count_memset(void * s, unsigned long pattern, size_t count)
  179. {
  180. switch (count) {
  181. case 0:
  182. return s;
  183. case 1:
  184. *(unsigned char *)s = pattern;
  185. return s;
  186. case 2:
  187. *(unsigned short *)s = pattern;
  188. return s;
  189. case 3:
  190. *(unsigned short *)s = pattern;
  191. *(2+(unsigned char *)s) = pattern;
  192. return s;
  193. case 4:
  194. *(unsigned long *)s = pattern;
  195. return s;
  196. }
  197. #define COMMON(x) \
  198. __asm__ __volatile__( \
  199. "rep ; stosl" \
  200. x \
  201. : "=&c" (d0), "=&D" (d1) \
  202. : "a" (pattern),"0" (count/4),"1" ((long) s) \
  203. : "memory")
  204. {
  205. int d0, d1;
  206. switch (count % 4) {
  207. case 0: COMMON(""); return s;
  208. case 1: COMMON("\n\tstosb"); return s;
  209. case 2: COMMON("\n\tstosw"); return s;
  210. default: COMMON("\n\tstosw\n\tstosb"); return s;
  211. }
  212. }
  213. #undef COMMON
  214. }
  215. #define __constant_c_x_memset(s, c, count) \
  216. (__builtin_constant_p(count) ? \
  217. __constant_c_and_count_memset((s),(c),(count)) : \
  218. __constant_c_memset((s),(c),(count)))
  219. #define __memset(s, c, count) \
  220. (__builtin_constant_p(count) ? \
  221. __constant_count_memset((s),(c),(count)) : \
  222. __memset_generic((s),(c),(count)))
  223. #define __HAVE_ARCH_MEMSET
  224. #define memset(s, c, count) \
  225. (__builtin_constant_p(c) ? \
  226. __constant_c_x_memset((s),(0x01010101UL*(unsigned char)(c)),(count)) : \
  227. __memset((s),(c),(count)))
  228. /*
  229. * find the first occurrence of byte 'c', or 1 past the area if none
  230. */
  231. #define __HAVE_ARCH_MEMSCAN
  232. extern void *memscan(void * addr, int c, size_t size);
  233. #endif /* __KERNEL__ */
  234. #endif