memmove.c 1.9 KB

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