memset.c 1.8 KB

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