You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

string.h 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #ifndef ETHERBOOT_BITS_STRING_H
  2. #define ETHERBOOT_BITS_STRING_H
  3. /*
  4. * Taken from Linux /usr/include/asm/string.h
  5. * All except memcpy, memmove, memset and memcmp removed.
  6. *
  7. * Non-standard memswap() function added because it saves quite a bit
  8. * of code (mbrown@fensystems.co.uk).
  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 strtok,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. #define __HAVE_ARCH_MEMCPY
  23. extern void * __memcpy ( void *dest, const void *src, size_t len );
  24. #if 0
  25. static inline __attribute__ (( always_inline )) void *
  26. __memcpy ( void *dest, const void *src, size_t len ) {
  27. int d0, d1, d2;
  28. __asm__ __volatile__ ( "rep ; movsb"
  29. : "=&c" ( d0 ), "=&S" ( d1 ), "=&D" ( d2 )
  30. : "0" ( len ), "1" ( src ), "2" ( dest )
  31. : "memory" );
  32. return dest;
  33. }
  34. #endif
  35. static inline __attribute__ (( always_inline )) void *
  36. __constant_memcpy ( void *dest, const void *src, size_t len ) {
  37. union {
  38. uint32_t u32[2];
  39. uint16_t u16[4];
  40. uint8_t u8[8];
  41. } __attribute__ (( __may_alias__ )) *dest_u = dest;
  42. const union {
  43. uint32_t u32[2];
  44. uint16_t u16[4];
  45. uint8_t u8[8];
  46. } __attribute__ (( __may_alias__ )) *src_u = src;
  47. const void *esi;
  48. void *edi;
  49. switch ( len ) {
  50. case 0 : /* 0 bytes */
  51. return dest;
  52. /*
  53. * Single-register moves; these are always better than a
  54. * string operation. We can clobber an arbitrary two
  55. * registers (data, source, dest can re-use source register)
  56. * instead of being restricted to esi and edi. There's also a
  57. * much greater potential for optimising with nearby code.
  58. *
  59. */
  60. case 1 : /* 4 bytes */
  61. dest_u->u8[0] = src_u->u8[0];
  62. return dest;
  63. case 2 : /* 6 bytes */
  64. dest_u->u16[0] = src_u->u16[0];
  65. return dest;
  66. case 4 : /* 4 bytes */
  67. dest_u->u32[0] = src_u->u32[0];
  68. return dest;
  69. /*
  70. * Double-register moves; these are probably still a win.
  71. *
  72. */
  73. case 3 : /* 12 bytes */
  74. dest_u->u16[0] = src_u->u16[0];
  75. dest_u->u8[2] = src_u->u8[2];
  76. return dest;
  77. case 5 : /* 10 bytes */
  78. dest_u->u32[0] = src_u->u32[0];
  79. dest_u->u8[4] = src_u->u8[4];
  80. return dest;
  81. case 6 : /* 12 bytes */
  82. dest_u->u32[0] = src_u->u32[0];
  83. dest_u->u16[2] = src_u->u16[2];
  84. return dest;
  85. case 8 : /* 10 bytes */
  86. dest_u->u32[0] = src_u->u32[0];
  87. dest_u->u32[1] = src_u->u32[1];
  88. return dest;
  89. }
  90. /* Even if we have to load up esi and edi ready for a string
  91. * operation, we can sometimes save space by using multiple
  92. * single-byte "movs" operations instead of loading up ecx and
  93. * using "rep movsb".
  94. *
  95. * "load ecx, rep movsb" is 7 bytes, plus an average of 1 byte
  96. * to allow for saving/restoring ecx 50% of the time.
  97. *
  98. * "movsl" and "movsb" are 1 byte each, "movsw" is two bytes.
  99. * (In 16-bit mode, "movsl" is 2 bytes and "movsw" is 1 byte,
  100. * but "movsl" moves twice as much data, so it balances out).
  101. *
  102. * The cutoff point therefore occurs around 26 bytes; the byte
  103. * requirements for each method are:
  104. *
  105. * len 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  106. * #bytes (ecx) 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
  107. * #bytes (no ecx) 4 5 6 7 5 6 7 8 6 7 8 9 7 8 9 10
  108. */
  109. esi = src;
  110. edi = dest;
  111. if ( len >= 26 )
  112. return __memcpy ( dest, src, len );
  113. if ( len >= 6*4 )
  114. __asm__ __volatile__ ( "movsl" : "=&D" ( edi ), "=&S" ( esi )
  115. : "0" ( edi ), "1" ( esi ) : "memory" );
  116. if ( len >= 5*4 )
  117. __asm__ __volatile__ ( "movsl" : "=&D" ( edi ), "=&S" ( esi )
  118. : "0" ( edi ), "1" ( esi ) : "memory" );
  119. if ( len >= 4*4 )
  120. __asm__ __volatile__ ( "movsl" : "=&D" ( edi ), "=&S" ( esi )
  121. : "0" ( edi ), "1" ( esi ) : "memory" );
  122. if ( len >= 3*4 )
  123. __asm__ __volatile__ ( "movsl" : "=&D" ( edi ), "=&S" ( esi )
  124. : "0" ( edi ), "1" ( esi ) : "memory" );
  125. if ( len >= 2*4 )
  126. __asm__ __volatile__ ( "movsl" : "=&D" ( edi ), "=&S" ( esi )
  127. : "0" ( edi ), "1" ( esi ) : "memory" );
  128. if ( len >= 1*4 )
  129. __asm__ __volatile__ ( "movsl" : "=&D" ( edi ), "=&S" ( esi )
  130. : "0" ( edi ), "1" ( esi ) : "memory" );
  131. if ( ( len % 4 ) >= 2 )
  132. __asm__ __volatile__ ( "movsw" : "=&D" ( edi ), "=&S" ( esi )
  133. : "0" ( edi ), "1" ( esi ) : "memory" );
  134. if ( ( len % 2 ) >= 1 )
  135. __asm__ __volatile__ ( "movsb" : "=&D" ( edi ), "=&S" ( esi )
  136. : "0" ( edi ), "1" ( esi ) : "memory" );
  137. return dest;
  138. }
  139. #define memcpy( dest, src, len ) \
  140. ( __builtin_constant_p ( (len) ) ? \
  141. __constant_memcpy ( (dest), (src), (len) ) : \
  142. __memcpy ( (dest), (src), (len) ) )
  143. #define __HAVE_ARCH_MEMMOVE
  144. static inline void * memmove(void * dest,const void * src, size_t n)
  145. {
  146. int d0, d1, d2;
  147. if (dest<src)
  148. __asm__ __volatile__(
  149. "cld\n\t"
  150. "rep\n\t"
  151. "movsb"
  152. : "=&c" (d0), "=&S" (d1), "=&D" (d2)
  153. :"0" (n),"1" (src),"2" (dest)
  154. : "memory");
  155. else
  156. __asm__ __volatile__(
  157. "std\n\t"
  158. "rep\n\t"
  159. "movsb\n\t"
  160. "cld"
  161. : "=&c" (d0), "=&S" (d1), "=&D" (d2)
  162. :"0" (n),
  163. "1" (n-1+(const char *)src),
  164. "2" (n-1+(char *)dest)
  165. :"memory");
  166. return dest;
  167. }
  168. #define __HAVE_ARCH_MEMSET
  169. static inline void * memset(void *s, int c,size_t count)
  170. {
  171. int d0, d1;
  172. __asm__ __volatile__(
  173. "cld\n\t"
  174. "rep\n\t"
  175. "stosb"
  176. : "=&c" (d0), "=&D" (d1)
  177. :"a" (c),"1" (s),"0" (count)
  178. :"memory");
  179. return s;
  180. }
  181. #define __HAVE_ARCH_MEMSWAP
  182. static inline void * memswap(void *dest, void *src, size_t n)
  183. {
  184. int d0, d1, d2, d3;
  185. __asm__ __volatile__(
  186. "\n1:\t"
  187. "movb (%%edi),%%al\n\t"
  188. "xchgb (%%esi),%%al\n\t"
  189. "incl %%esi\n\t"
  190. "stosb\n\t"
  191. "loop 1b"
  192. : "=&c" (d0), "=&S" (d1), "=&D" (d2), "=&a" (d3)
  193. : "0" (n), "1" (src), "2" (dest)
  194. : "memory" );
  195. return dest;
  196. }
  197. #define __HAVE_ARCH_STRNCMP
  198. static inline int strncmp(const char * cs,const char * ct,size_t count)
  199. {
  200. register int __res;
  201. int d0, d1, d2;
  202. __asm__ __volatile__(
  203. "1:\tdecl %3\n\t"
  204. "js 2f\n\t"
  205. "lodsb\n\t"
  206. "scasb\n\t"
  207. "jne 3f\n\t"
  208. "testb %%al,%%al\n\t"
  209. "jne 1b\n"
  210. "2:\txorl %%eax,%%eax\n\t"
  211. "jmp 4f\n"
  212. "3:\tsbbl %%eax,%%eax\n\t"
  213. "orb $1,%%al\n"
  214. "4:"
  215. :"=a" (__res), "=&S" (d0), "=&D" (d1), "=&c" (d2)
  216. :"1" (cs),"2" (ct),"3" (count));
  217. return __res;
  218. }
  219. #define __HAVE_ARCH_STRLEN
  220. static inline size_t strlen(const char * s)
  221. {
  222. int d0;
  223. register int __res;
  224. __asm__ __volatile__(
  225. "repne\n\t"
  226. "scasb\n\t"
  227. "notl %0\n\t"
  228. "decl %0"
  229. :"=c" (__res), "=&D" (d0) :"1" (s),"a" (0), "0" (0xffffffff));
  230. return __res;
  231. }
  232. #endif /* ETHERBOOT_BITS_STRING_H */