memmove.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  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
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/string.h>
  16. #include <linux/module.h>
  17. void *memmove(void *dest, const void *src, size_t n)
  18. {
  19. if ((const char *)src >= (char *)dest + n
  20. || (char *)dest >= (const char *)src + n) {
  21. /* We found no overlap, so let memcpy do all the heavy
  22. * lifting (prefetching, etc.)
  23. */
  24. return memcpy(dest, src, n);
  25. }
  26. if (n != 0) {
  27. const uint8_t *in;
  28. uint8_t x;
  29. uint8_t *out;
  30. int stride;
  31. if (src < dest) {
  32. /* copy backwards */
  33. in = (const uint8_t *)src + n - 1;
  34. out = (uint8_t *)dest + n - 1;
  35. stride = -1;
  36. } else {
  37. /* copy forwards */
  38. in = (const uint8_t *)src;
  39. out = (uint8_t *)dest;
  40. stride = 1;
  41. }
  42. /* Manually software-pipeline this loop. */
  43. x = *in;
  44. in += stride;
  45. while (--n != 0) {
  46. *out = x;
  47. out += stride;
  48. x = *in;
  49. in += stride;
  50. }
  51. *out = x;
  52. }
  53. return dest;
  54. }
  55. EXPORT_SYMBOL(memmove);