memmove.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file COPYING in the main directory of this archive
  4. * for more details.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/string.h>
  8. void *memmove(void *dest, const void *src, size_t n)
  9. {
  10. void *xdest = dest;
  11. size_t temp;
  12. if (!n)
  13. return xdest;
  14. if (dest < src) {
  15. if ((long)dest & 1) {
  16. char *cdest = dest;
  17. const char *csrc = src;
  18. *cdest++ = *csrc++;
  19. dest = cdest;
  20. src = csrc;
  21. n--;
  22. }
  23. if (n > 2 && (long)dest & 2) {
  24. short *sdest = dest;
  25. const short *ssrc = src;
  26. *sdest++ = *ssrc++;
  27. dest = sdest;
  28. src = ssrc;
  29. n -= 2;
  30. }
  31. temp = n >> 2;
  32. if (temp) {
  33. long *ldest = dest;
  34. const long *lsrc = src;
  35. temp--;
  36. do
  37. *ldest++ = *lsrc++;
  38. while (temp--);
  39. dest = ldest;
  40. src = lsrc;
  41. }
  42. if (n & 2) {
  43. short *sdest = dest;
  44. const short *ssrc = src;
  45. *sdest++ = *ssrc++;
  46. dest = sdest;
  47. src = ssrc;
  48. }
  49. if (n & 1) {
  50. char *cdest = dest;
  51. const char *csrc = src;
  52. *cdest = *csrc;
  53. }
  54. } else {
  55. dest = (char *)dest + n;
  56. src = (const char *)src + n;
  57. if ((long)dest & 1) {
  58. char *cdest = dest;
  59. const char *csrc = src;
  60. *--cdest = *--csrc;
  61. dest = cdest;
  62. src = csrc;
  63. n--;
  64. }
  65. if (n > 2 && (long)dest & 2) {
  66. short *sdest = dest;
  67. const short *ssrc = src;
  68. *--sdest = *--ssrc;
  69. dest = sdest;
  70. src = ssrc;
  71. n -= 2;
  72. }
  73. temp = n >> 2;
  74. if (temp) {
  75. long *ldest = dest;
  76. const long *lsrc = src;
  77. temp--;
  78. do
  79. *--ldest = *--lsrc;
  80. while (temp--);
  81. dest = ldest;
  82. src = lsrc;
  83. }
  84. if (n & 2) {
  85. short *sdest = dest;
  86. const short *ssrc = src;
  87. *--sdest = *--ssrc;
  88. dest = sdest;
  89. src = ssrc;
  90. }
  91. if (n & 1) {
  92. char *cdest = dest;
  93. const char *csrc = src;
  94. *--cdest = *--csrc;
  95. }
  96. }
  97. return xdest;
  98. }
  99. EXPORT_SYMBOL(memmove);