memcpy.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * assembler 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. /* PPC can do pre increment and load/store, but not post increment and
  22. load/store. Therefore use *++ptr instead of *ptr++. */
  23. void *memcpy(void *to, const void *from, size_t len)
  24. {
  25. unsigned long rem, chunks, tmp1, tmp2;
  26. unsigned char *tmp_to;
  27. unsigned char *tmp_from = (unsigned char *)from;
  28. chunks = len / 8;
  29. tmp_from -= 4;
  30. tmp_to = to - 4;
  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. len = len % 8;
  48. if (len >= 4) {
  49. tmp_from += 4;
  50. tmp_to += 4;
  51. *(unsigned long *)(tmp_to) = *(unsigned long *)(tmp_from);
  52. len -= 4;
  53. }
  54. if (!len)
  55. return to;
  56. tmp_from += 3;
  57. tmp_to += 3;
  58. do {
  59. *++tmp_to = *++tmp_from;
  60. } while (--len);
  61. return to;
  62. align:
  63. /* ???: Do we really need to generate the carry flag here? If not, then:
  64. rem -= 4; */
  65. rem = 4 - rem;
  66. len -= rem;
  67. do {
  68. *(tmp_to+4) = *(tmp_from+4);
  69. ++tmp_from;
  70. ++tmp_to;
  71. } while (--rem);
  72. chunks = len / 8;
  73. if (chunks)
  74. goto copy_chunks;
  75. goto lessthan8;
  76. }
  77. libc_hidden_def(memcpy)