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. libc_hidden_proto(memmove)
  22. void *memmove(void *to, const void *from, size_t n)
  23. {
  24. unsigned long rem, chunks, tmp1, tmp2;
  25. unsigned char *tmp_to;
  26. unsigned char *tmp_from = (unsigned char *)from;
  27. if (tmp_from >= (unsigned char *)to)
  28. return __memcpy(to, from, n);
  29. chunks = n / 8;
  30. tmp_from += n;
  31. tmp_to = to + n;
  32. if (!chunks)
  33. goto lessthan8;
  34. rem = (unsigned long )tmp_to % 4;
  35. if (rem)
  36. goto align;
  37. copy_chunks:
  38. do {
  39. /* make gcc to load all data, then store it */
  40. tmp1 = *(unsigned long *)(tmp_from-4);
  41. tmp_from -= 8;
  42. tmp2 = *(unsigned long *)tmp_from;
  43. *(unsigned long *)(tmp_to-4) = tmp1;
  44. tmp_to -= 8;
  45. *(unsigned long *)tmp_to = tmp2;
  46. } while (--chunks);
  47. lessthan8:
  48. n = n % 8;
  49. if (n >= 4) {
  50. *(unsigned long *)(tmp_to-4) = *(unsigned long *)(tmp_from-4);
  51. tmp_from -= 4;
  52. tmp_to -= 4;
  53. n = n-4;
  54. }
  55. if (!n ) return to;
  56. do {
  57. *--tmp_to = *--tmp_from;
  58. } while (--n);
  59. return to;
  60. align:
  61. rem = 4 - rem;
  62. n = n - rem;
  63. do {
  64. *--tmp_to = *--tmp_from;
  65. } while (--rem);
  66. chunks = n / 8;
  67. if (chunks)
  68. goto copy_chunks;
  69. goto lessthan8;
  70. }
  71. libc_hidden_def(memmove)