libc-string_i386.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (C) 2008 Denys Vlasenko <vda.linux@googlemail.com>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball
  5. */
  6. #if !defined _STRING_H
  7. #error "Never use <libc-string_i386.h> directly; include <string.h> instead"
  8. #endif
  9. #ifndef _LIBC_STRING_i386_H
  10. #define _LIBC_STRING_i386_H 1
  11. static __always_inline
  12. void *inlined_memset_const_c_count4(void *s, unsigned eax, unsigned count)
  13. {
  14. int ecx, edi;
  15. if (count == 0)
  16. return s;
  17. /* Very small (2 stores or less) are best done with direct
  18. * mov <const>,<mem> instructions (they do not clobber registers) */
  19. if (count == 1) {
  20. *(char *)(s + 0) = eax;
  21. return s;
  22. }
  23. eax *= 0x01010101; /* done at compile time */
  24. if (count == 2) {
  25. *(short *)(s + 0) = eax;
  26. return s;
  27. }
  28. if (count == 3) {
  29. *(short *)(s + 0) = eax;
  30. *(char *) (s + 2) = eax;
  31. return s;
  32. }
  33. if (count == 1*4 + 0) {
  34. *(int *)(s + 0) = eax;
  35. return s;
  36. }
  37. if (count == 1*4 + 1) {
  38. *(int *) (s + 0) = eax;
  39. *(char *)(s + 4) = eax;
  40. return s;
  41. }
  42. if (count == 1*4 + 2) {
  43. *(int *) (s + 0) = eax;
  44. *(short *)(s + 4) = eax;
  45. return s;
  46. }
  47. /* Small string stores: don't clobber ecx
  48. * (clobbers only eax and edi) */
  49. #define small_store(arg) { \
  50. __asm__ __volatile__( \
  51. arg \
  52. : "=&D" (edi) \
  53. : "a" (eax), "0" (s) \
  54. : "memory" \
  55. ); \
  56. return s; \
  57. }
  58. if (count == 1*4 + 3) small_store("stosl; stosw; stosb");
  59. if (count == 2*4 + 0) {
  60. ((int *)s)[0] = eax;
  61. ((int *)s)[1] = eax;
  62. return s;
  63. }
  64. if (count == 2*4 + 1) small_store("stosl; stosl; stosb");
  65. if (count == 2*4 + 2) small_store("stosl; stosl; stosw");
  66. if (count == 2*4 + 3) small_store("stosl; stosl; stosw; stosb");
  67. if (count == 3*4 + 0) small_store("stosl; stosl; stosl");
  68. if (count == 3*4 + 1) small_store("stosl; stosl; stosl; stosb");
  69. if (count == 3*4 + 2) small_store("stosl; stosl; stosl; stosw");
  70. if (count == 3*4 + 3) small_store("stosl; stosl; stosl; stosw; stosb");
  71. if (count == 4*4 + 0) small_store("stosl; stosl; stosl; stosl");
  72. if (count == 4*4 + 1) small_store("stosl; stosl; stosl; stosl; stosb");
  73. /* going over 7 bytes is suboptimal */
  74. /* stosw is 2-byte insn, so this one takes 6 bytes: */
  75. if (count == 4*4 + 2) small_store("stosl; stosl; stosl; stosl; stosw");
  76. /* 7 bytes */
  77. if (count == 4*4 + 3) small_store("stosl; stosl; stosl; stosl; stosw; stosb");
  78. /* 5 bytes */
  79. if (count == 5*4 + 0) small_store("stosl; stosl; stosl; stosl; stosl");
  80. /* 6 bytes */
  81. if (count == 5*4 + 1) small_store("stosl; stosl; stosl; stosl; stosl; stosb");
  82. /* 7 bytes */
  83. if (count == 5*4 + 2) small_store("stosl; stosl; stosl; stosl; stosl; stosw");
  84. /* 8 bytes, but oh well... */
  85. if (count == 5*4 + 3) small_store("stosl; stosl; stosl; stosl; stosl; stosw; stosb");
  86. /* 6 bytes */
  87. if (count == 6*4 + 0) small_store("stosl; stosl; stosl; stosl; stosl; stosl");
  88. /* the rest would be 7+ bytes and is handled below instead */
  89. #undef small_store
  90. /* Not small, but multiple-of-4 store.
  91. * "mov <const>,%ecx; rep; stosl" sequence is 7 bytes */
  92. __asm__ __volatile__(
  93. " rep; stosl\n"
  94. : "=&c" (ecx), "=&D" (edi)
  95. : "a" (eax), "0" (count / 4), "1" (s)
  96. : "memory"
  97. );
  98. return s;
  99. }
  100. #if 1 /* -51 bytes on shared i386 build with gcc 4.3.0 */
  101. #define memset(s, c, count) ( \
  102. ( !(__builtin_constant_p(c) && __builtin_constant_p(count)) \
  103. || ((count) > (6*4 + 0) && ((count) % 4) != 0) \
  104. ) \
  105. ? memset((s), (c), (count)) \
  106. : inlined_memset_const_c_count4((s), (c), (count)) \
  107. )
  108. #endif
  109. static __always_inline
  110. void *inlined_mempcpy_const_count4(void *d, const void *s, unsigned count)
  111. {
  112. int ecx;
  113. char *esi, *edi;
  114. if (count == 0)
  115. return d;
  116. if (count == 1) {
  117. *(char *)d = *(char *)s;
  118. return d + 1;
  119. }
  120. if (count == 2) {
  121. *(short *)d = *(short *)s;
  122. return d + 2;
  123. }
  124. /* Small string moves: don't clobber ecx
  125. * (clobbers only esi and edi) */
  126. #define small_move(arg) { \
  127. __asm__ __volatile__( \
  128. arg \
  129. : "=&S" (esi), "=&D" (edi) \
  130. : "0" (s), "1" (d) \
  131. : "memory" \
  132. ); \
  133. return edi; \
  134. }
  135. if (count == 3) small_move("movsw; movsb");
  136. if (count == 1*4 + 0) {
  137. *(int *)d = *(int *)s;
  138. return d + 4;
  139. }
  140. if (count == 1*4 + 1) small_move("movsl; movsb");
  141. if (count == 1*4 + 2) small_move("movsl; movsw");
  142. if (count == 1*4 + 3) small_move("movsl; movsw; movsb");
  143. if (count == 2*4 + 0) small_move("movsl; movsl");
  144. if (count == 2*4 + 1) small_move("movsl; movsl; movsb");
  145. if (count == 2*4 + 2) small_move("movsl; movsl; movsw");
  146. if (count == 2*4 + 3) small_move("movsl; movsl; movsw; movsb");
  147. if (count == 3*4 + 0) small_move("movsl; movsl; movsl");
  148. if (count == 3*4 + 1) small_move("movsl; movsl; movsl; movsb");
  149. if (count == 3*4 + 2) small_move("movsl; movsl; movsl; movsw");
  150. if (count == 3*4 + 3) small_move("movsl; movsl; movsl; movsw; movsb");
  151. if (count == 4*4 + 0) small_move("movsl; movsl; movsl; movsl");
  152. if (count == 4*4 + 1) small_move("movsl; movsl; movsl; movsl; movsb");
  153. /* going over 7 bytes is suboptimal */
  154. /* movsw is 2-byte insn, so this one takes 6 bytes: */
  155. if (count == 4*4 + 2) small_move("movsl; movsl; movsl; movsl; movsw");
  156. /* 7 bytes */
  157. if (count == 4*4 + 3) small_move("movsl; movsl; movsl; movsl; movsw; movsb");
  158. /* 5 bytes */
  159. if (count == 5*4 + 0) small_move("movsl; movsl; movsl; movsl; movsl");
  160. /* 6 bytes */
  161. if (count == 5*4 + 1) small_move("movsl; movsl; movsl; movsl; movsl; movsb");
  162. /* 7 bytes */
  163. if (count == 5*4 + 2) small_move("movsl; movsl; movsl; movsl; movsl; movsw");
  164. /* 8 bytes, but oh well... */
  165. if (count == 5*4 + 3) small_move("movsl; movsl; movsl; movsl; movsl; movsw; movsb");
  166. /* 6 bytes */
  167. if (count == 6*4 + 0) small_move("movsl; movsl; movsl; movsl; movsl; movsl");
  168. /* the rest would be 7+ bytes and is handled below instead */
  169. #undef small_move
  170. /* Not small, but multiple-of-4 move.
  171. * "mov <const>,%ecx; rep; movsl" sequence is 7 bytes */
  172. __asm__ __volatile__(
  173. " rep; movsl\n"
  174. : "=&c" (ecx), "=&S" (esi), "=&D" (edi)
  175. : "0" (count / 4), "1" (s), "2" (d)
  176. : "memory"
  177. );
  178. return edi;
  179. }
  180. static __always_inline
  181. void *inlined_memcpy_const_count4(void *d, const void *s, unsigned count)
  182. {
  183. inlined_mempcpy_const_count4(d, s, count);
  184. return d;
  185. }
  186. #if 1 /* +34 bytes on shared i386 build with gcc 4.3.0 */
  187. #define mempcpy(d, s, count) ( \
  188. ( !(__builtin_constant_p(count)) \
  189. || ((count) > (6*4 + 0) && ((count) % 4) != 0) \
  190. ) \
  191. ? mempcpy((d), (s), (count)) \
  192. : inlined_mempcpy_const_count4((d), (s), (count)) \
  193. )
  194. #define memcpy(d, s, count) ( \
  195. ( !(__builtin_constant_p(count)) \
  196. || ((count) > (6*4 + 0) && ((count) % 4) != 0) \
  197. ) \
  198. ? memcpy((d), (s), (count)) \
  199. : inlined_memcpy_const_count4((d), (s), (count)) \
  200. )
  201. #endif
  202. static __always_inline
  203. size_t inlined_strlen(const char *s)
  204. {
  205. int edi;
  206. int ecx;
  207. __asm__ __volatile__(
  208. " repne; scasb\n"
  209. /* " notl %0\n" */
  210. /* " decl %0\n" */
  211. : "=c" (ecx), "=&D" (edi)
  212. : "1" (s), "a" (0), "0" (0xffffffffu)
  213. /* : no clobbers */
  214. );
  215. return -ecx - 1;
  216. }
  217. #if 0 /* +1108 bytes on shared i386 build with gcc 4.3.0 */
  218. #define strlen(s) inlined_strlen(s)
  219. #endif
  220. static __always_inline
  221. char *inlined_stpcpy(char *dest, const char *src)
  222. {
  223. char *esi, *edi;
  224. int eax;
  225. __asm__ __volatile__(
  226. "1: lodsb\n"
  227. " stosb\n"
  228. " testb %%al, %%al\n"
  229. " jnz 1b\n"
  230. : "=&S" (esi), "=&D" (edi), "=&a" (eax)
  231. : "0" (src), "1" (dest)
  232. : "memory"
  233. );
  234. return edi - 1;
  235. }
  236. static __always_inline
  237. char *inlined_strcpy(char *dest, const char *src)
  238. {
  239. inlined_stpcpy(dest, src);
  240. return dest;
  241. }
  242. #if 0 /* +562 bytes on shared i386 build with gcc 4.3.0 */
  243. #define stpcpy(dest, src) inlined_stpcpy(dest, src)
  244. #define strcpy(dest, src) inlined_strcpy(dest, src)
  245. #endif
  246. static __always_inline
  247. void *inlined_memchr(const void *s, int c, size_t count)
  248. {
  249. void *edi;
  250. int ecx;
  251. /* Unfortunately, c gets loaded to %eax (wide insn), not %al */
  252. __asm__ __volatile__(
  253. " jecxz 1f\n"
  254. " repne; scasb\n"
  255. " leal -1(%%edi), %%edi\n"
  256. " je 2f\n"
  257. "1:\n"
  258. " xorl %%edi, %%edi\n"
  259. "2:\n"
  260. : "=&D" (edi), "=&c" (ecx)
  261. : "a" (c), "0" (s), "1" (count)
  262. /* : no clobbers */
  263. );
  264. return edi;
  265. }
  266. static __always_inline
  267. void *inlined_memchr_const_c(const void *s, int c, size_t count)
  268. {
  269. void *edi;
  270. int ecx, eax;
  271. __asm__ __volatile__(
  272. " jecxz 1f\n"
  273. " movb %4, %%al\n" /* const c to %%al */
  274. " repne; scasb\n"
  275. " leal -1(%%edi), %%edi\n"
  276. " je 2f\n"
  277. "1:\n"
  278. " xorl %%edi, %%edi\n"
  279. "2:\n"
  280. : "=&D" (edi), "=&c" (ecx), "=&a" (eax)
  281. : "0" (s), "i" (c), "1" (count)
  282. /* : no clobbers */
  283. );
  284. return edi;
  285. }
  286. #if 1 /* +2 bytes on shared i386 build with gcc 4.3.0 */
  287. #define memchr(s, c, count) ( \
  288. __builtin_constant_p(c) \
  289. ? inlined_memchr_const_c(s, (c) & 0xff, count) \
  290. : inlined_memchr(s, c, count) \
  291. )
  292. #endif
  293. #endif /* _LIBC_STRING_i386_H */