memcpy.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 *__memcpy(void *to, const void *from, size_t n)
  22. /* PPC can do pre increment and load/store, but not post increment and load/store.
  23. Therefore use *++ptr instead of *ptr++. */
  24. {
  25. unsigned long rem, chunks, tmp1, tmp2;
  26. unsigned char *tmp_to;
  27. unsigned char *tmp_from = (unsigned char *)from;
  28. chunks = n / 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. 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. tmp_from += 3;
  56. tmp_to += 3;
  57. do {
  58. *++tmp_to = *++tmp_from;
  59. } while (--n);
  60. return to;
  61. align:
  62. rem = 4 - rem;
  63. n = n - rem;
  64. do {
  65. *(tmp_to+4) = *(tmp_from+4);
  66. ++tmp_from;
  67. ++tmp_to;
  68. } while (--rem);
  69. chunks = n / 8;
  70. if (chunks)
  71. goto copy_chunks;
  72. goto lessthan8;
  73. }
  74. strong_alias(__memcpy,memcpy)