memset.c 1.7 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. * 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. static inline int expand_byte_word(int c){
  22. /* this does:
  23. c = c << 8 | c;
  24. c = c << 16 | c ;
  25. */
  26. asm("rlwimi %0,%0,8,16,23\n"
  27. "\trlwimi %0,%0,16,0,15\n"
  28. : "=r" (c) : "0" (c));
  29. return c;
  30. }
  31. void attribute_hidden *__memset(void *to, int c, size_t n)
  32. {
  33. unsigned long rem, chunks;
  34. unsigned char *tmp_to;
  35. chunks = n / 8;
  36. tmp_to = to - 4;
  37. c = expand_byte_word(c);
  38. if (!chunks)
  39. goto lessthan8;
  40. rem = (unsigned long )tmp_to % 4;
  41. if (rem)
  42. goto align;
  43. copy_chunks:
  44. do {
  45. *(unsigned long *)(tmp_to+4) = c;
  46. tmp_to += 4;
  47. *(unsigned long *)(tmp_to+4) = c;
  48. tmp_to += 4;
  49. } while (--chunks);
  50. lessthan8:
  51. n = n % 8;
  52. if (n >= 4) {
  53. *(unsigned long *)(tmp_to+4) = c;
  54. tmp_to += 4;
  55. n = n-4;
  56. }
  57. if (!n ) return to;
  58. tmp_to += 3;
  59. do {
  60. *++tmp_to = c;
  61. } while (--n);
  62. return to;
  63. align:
  64. rem = 4 - rem;
  65. n = n-rem;
  66. do {
  67. *(tmp_to+4) = c;
  68. ++tmp_to;
  69. } while (--rem);
  70. chunks = n / 8;
  71. if (chunks)
  72. goto copy_chunks;
  73. goto lessthan8;
  74. }
  75. strong_alias(__memset,memset)