string.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU Library General Public License as published by the Free
  4. * Software Foundation; either version 2 of the License, or (at your option) any
  5. * later version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more
  10. * details.
  11. */
  12. /* These are carefully optimized mem*() functions for PPC written in C.
  13. * Don't muck around with these function without checking the generated
  14. * assmbler code.
  15. * It is possible to optimize these significantly more by using specific
  16. * data cache instructions(mainly dcbz). However that requires knownledge
  17. * about the CPU's cache line size.
  18. *
  19. * BUG ALERT!
  20. * The cache instructions on MPC8xx CPU's are buggy(they don't update
  21. * the DAR register when causing a DTLB Miss/Error) and cannot be
  22. * used on 8xx CPU's without a kernel patch to work around this
  23. * problem.
  24. *
  25. * Copyright (C) 2004 Joakim Tjernlund
  26. */
  27. #define _STDIO_UTILITY
  28. #define _GNU_SOURCE
  29. #include <string.h>
  30. #include <locale.h> /* for __LOCALE_C_ONLY */
  31. #ifdef L_memcpy
  32. void *memcpy(void *to, const void *from, size_t n)
  33. /* PPC can do pre increment and load/store, but not post increment and load/store.
  34. Therefore use *++ptr instead of *ptr++. */
  35. {
  36. unsigned long rem, chunks, tmp1, tmp2;
  37. void *tmp_to;
  38. chunks = n / 8;
  39. from -= 4;
  40. tmp_to = to - 4;
  41. if (!chunks)
  42. goto lessthan8;
  43. rem = (unsigned long )tmp_to % 4;
  44. if (rem)
  45. goto align;
  46. copy_chunks:
  47. do {
  48. /* make gcc to load all data, then store it */
  49. tmp1 = *(unsigned long *)(from+4);
  50. from += 8;
  51. tmp2 = *(unsigned long *)from;
  52. *(unsigned long *)(tmp_to+4) = tmp1;
  53. tmp_to += 8;
  54. *(unsigned long *)tmp_to = tmp2;
  55. } while (--chunks);
  56. lessthan8:
  57. n = n % 8;
  58. if (n >= 4) {
  59. *++(unsigned long *)tmp_to = *++(unsigned long *)from;
  60. n = n-4;
  61. }
  62. if (!n ) return to;
  63. from += 3;
  64. tmp_to += 3;
  65. do {
  66. *++(unsigned char *)tmp_to = *++(unsigned char *)from;
  67. } while (--n);
  68. return to;
  69. align:
  70. rem = 4 - rem;
  71. n = n-rem;
  72. do {
  73. *(unsigned char *)(tmp_to+4) = *(unsigned char *)(from+4);
  74. ++from;
  75. ++tmp_to;
  76. } while (--rem);
  77. chunks = n / 8;
  78. if (chunks)
  79. goto copy_chunks;
  80. goto lessthan8;
  81. }
  82. #endif
  83. #ifdef L_memmove
  84. void *memmove(void *to, const void *from, size_t n)
  85. {
  86. unsigned long rem, chunks, tmp1, tmp2;
  87. void *tmp_to;
  88. if (from >= to)
  89. return memcpy(to, from, n);
  90. chunks = n / 8;
  91. from += n;
  92. tmp_to = to + n;
  93. if (!chunks)
  94. goto lessthan8;
  95. rem = (unsigned long )tmp_to % 4;
  96. if (rem)
  97. goto align;
  98. copy_chunks:
  99. do {
  100. /* make gcc to load all data, then store it */
  101. tmp1 = *(unsigned long *)(from-4);
  102. from -= 8;
  103. tmp2 = *(unsigned long *)from;
  104. *(unsigned long *)(tmp_to-4) = tmp1;
  105. tmp_to -= 8;
  106. *(unsigned long *)tmp_to = tmp2;
  107. } while (--chunks);
  108. lessthan8:
  109. n = n % 8;
  110. if (n >= 4) {
  111. *--(unsigned long *)tmp_to = *--(unsigned long *)from;
  112. n = n-4;
  113. }
  114. if (!n ) return to;
  115. do {
  116. *--(unsigned char *)tmp_to = *--(unsigned char *)from;
  117. } while (--n);
  118. return to;
  119. align:
  120. rem = 4 - rem;
  121. n = n-rem;
  122. do {
  123. *--(unsigned char *)tmp_to = *--(unsigned char *)from;
  124. } while (--rem);
  125. chunks = n / 8;
  126. if (chunks)
  127. goto copy_chunks;
  128. goto lessthan8;
  129. }
  130. #endif
  131. #ifdef L_memset
  132. static inline int expand_byte_word(int c){
  133. /* this does:
  134. c = c << 8 | c;
  135. c = c << 16 | c ;
  136. */
  137. asm("rlwimi %0,%0,8,16,23\n"
  138. "\trlwimi %0,%0,16,0,15\n"
  139. : "=r" (c) : "0" (c));
  140. return c;
  141. }
  142. void *memset(void *to, int c, size_t n)
  143. {
  144. unsigned long rem, chunks;
  145. void *tmp_to;
  146. chunks = n / 8;
  147. tmp_to = to - 4;
  148. c = expand_byte_word(c);
  149. if (!chunks)
  150. goto lessthan8;
  151. rem = (unsigned long )tmp_to % 4;
  152. if (rem)
  153. goto align;
  154. copy_chunks:
  155. do {
  156. *++(unsigned long *)tmp_to = c;
  157. *++(unsigned long *)tmp_to = c;
  158. } while (--chunks);
  159. lessthan8:
  160. n = n % 8;
  161. if (n >= 4) {
  162. *++(unsigned long *)tmp_to = c;
  163. n = n-4;
  164. }
  165. if (!n ) return to;
  166. tmp_to += 3;
  167. do {
  168. *++(unsigned char *)tmp_to = c;
  169. } while (--n);
  170. return to;
  171. align:
  172. rem = 4 - rem;
  173. n = n-rem;
  174. do {
  175. *(unsigned char *)(tmp_to+4) = c;
  176. ++tmp_to;
  177. } while (--rem);
  178. chunks = n / 8;
  179. if (chunks)
  180. goto copy_chunks;
  181. goto lessthan8;
  182. }
  183. #endif
  184. #ifdef L_bzero
  185. void bzero(void *s, size_t n)
  186. {
  187. (void)memset(s, 0, n);
  188. }
  189. #endif