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