memmove.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (C) 2004 Joakim Tjernlund
  3. * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. /* These are carefully optimized mem*() functions for PPC written in C.
  8. * Don't muck around with these function without checking the generated
  9. * assmbler code.
  10. * It is possible to optimize these significantly more by using specific
  11. * data cache instructions(mainly dcbz). However that requires knownledge
  12. * about the CPU's cache line size.
  13. *
  14. * BUG ALERT!
  15. * The cache instructions on MPC8xx CPU's are buggy(they don't update
  16. * the DAR register when causing a DTLB Miss/Error) and cannot be
  17. * used on 8xx CPU's without a kernel patch to work around this
  18. * problem.
  19. */
  20. #include <string.h>
  21. void attribute_hidden *__memmove(void *to, const void *from, size_t n)
  22. {
  23. unsigned long rem, chunks, tmp1, tmp2;
  24. unsigned char *tmp_to;
  25. unsigned char *tmp_from = (unsigned char *)from;
  26. if (tmp_from >= (unsigned char *)to)
  27. return __memcpy(to, from, n);
  28. chunks = n / 8;
  29. tmp_from += n;
  30. tmp_to = to + n;
  31. if (!chunks)
  32. goto lessthan8;
  33. rem = (unsigned long )tmp_to % 4;
  34. if (rem)
  35. goto align;
  36. copy_chunks:
  37. do {
  38. /* make gcc to load all data, then store it */
  39. tmp1 = *(unsigned long *)(tmp_from-4);
  40. tmp_from -= 8;
  41. tmp2 = *(unsigned long *)tmp_from;
  42. *(unsigned long *)(tmp_to-4) = tmp1;
  43. tmp_to -= 8;
  44. *(unsigned long *)tmp_to = tmp2;
  45. } while (--chunks);
  46. lessthan8:
  47. n = n % 8;
  48. if (n >= 4) {
  49. *(unsigned long *)(tmp_to-4) = *(unsigned long *)(tmp_from-4);
  50. tmp_from -= 4;
  51. tmp_to -= 4;
  52. n = n-4;
  53. }
  54. if (!n ) return to;
  55. do {
  56. *--tmp_to = *--tmp_from;
  57. } while (--n);
  58. return to;
  59. align:
  60. rem = 4 - rem;
  61. n = n - rem;
  62. do {
  63. *--tmp_to = *--tmp_from;
  64. } while (--rem);
  65. chunks = n / 8;
  66. if (chunks)
  67. goto copy_chunks;
  68. goto lessthan8;
  69. }
  70. strong_alias(__memmove,memmove)