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.

x86_string.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. /** @file
  24. *
  25. * Optimised string operations
  26. *
  27. */
  28. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  29. #include <string.h>
  30. /**
  31. * Copy memory area
  32. *
  33. * @v dest Destination address
  34. * @v src Source address
  35. * @v len Length
  36. * @ret dest Destination address
  37. */
  38. void * __attribute__ (( noinline )) __memcpy ( void *dest, const void *src,
  39. size_t len ) {
  40. void *edi = dest;
  41. const void *esi = src;
  42. int discard_ecx;
  43. /* We often do large dword-aligned and dword-length block
  44. * moves. Using movsl rather than movsb speeds these up by
  45. * around 32%.
  46. */
  47. __asm__ __volatile__ ( "rep movsl"
  48. : "=&D" ( edi ), "=&S" ( esi ),
  49. "=&c" ( discard_ecx )
  50. : "0" ( edi ), "1" ( esi ), "2" ( len >> 2 )
  51. : "memory" );
  52. __asm__ __volatile__ ( "rep movsb"
  53. : "=&D" ( edi ), "=&S" ( esi ),
  54. "=&c" ( discard_ecx )
  55. : "0" ( edi ), "1" ( esi ), "2" ( len & 3 )
  56. : "memory" );
  57. return dest;
  58. }
  59. /**
  60. * Copy memory area backwards
  61. *
  62. * @v dest Destination address
  63. * @v src Source address
  64. * @v len Length
  65. * @ret dest Destination address
  66. */
  67. void * __attribute__ (( noinline )) __memcpy_reverse ( void *dest,
  68. const void *src,
  69. size_t len ) {
  70. void *edi = ( dest + len - 1 );
  71. const void *esi = ( src + len - 1 );
  72. int discard_ecx;
  73. /* Assume memmove() is not performance-critical, and perform a
  74. * bytewise copy for simplicity.
  75. */
  76. __asm__ __volatile__ ( "std\n\t"
  77. "rep movsb\n\t"
  78. "cld\n\t"
  79. : "=&D" ( edi ), "=&S" ( esi ),
  80. "=&c" ( discard_ecx )
  81. : "0" ( edi ), "1" ( esi ),
  82. "2" ( len )
  83. : "memory" );
  84. return dest;
  85. }
  86. /**
  87. * Copy (possibly overlapping) memory area
  88. *
  89. * @v dest Destination address
  90. * @v src Source address
  91. * @v len Length
  92. * @ret dest Destination address
  93. */
  94. void * __memmove ( void *dest, const void *src, size_t len ) {
  95. if ( dest <= src ) {
  96. return __memcpy ( dest, src, len );
  97. } else {
  98. return __memcpy_reverse ( dest, src, len );
  99. }
  100. }