string_32.h 6.7 KB

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