memset.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* A memset for CRIS.
  2. Copyright (C) 1999-2008 Axis Communications.
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Neither the name of Axis Communications nor the names of its
  10. contributors may be used to endorse or promote products derived
  11. from this software without specific prior written permission.
  12. THIS SOFTWARE IS PROVIDED BY AXIS COMMUNICATIONS AND ITS CONTRIBUTORS
  13. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AXIS
  16. COMMUNICATIONS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  17. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  21. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  22. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE. */
  24. /* FIXME: This file should really only be used for reference, as the
  25. result is somewhat depending on gcc generating what we expect rather
  26. than what we describe. An assembly file should be used instead. */
  27. #include <string.h>
  28. /* Note the multiple occurrence of the expression "12*4", including the
  29. asm. It is hard to get it into the asm in a good way. Thus better to
  30. expose the problem everywhere: no macro. */
  31. /* Assuming one cycle per dword written or read (ok, not really true; the
  32. world is not ideal), and one cycle per instruction, then 43+3*(n/48-1)
  33. <= 24+24*(n/48-1) so n >= 45.7; n >= 0.9; we win on the first full
  34. 48-byte block to set. */
  35. #define MEMSET_BY_BLOCK_THRESHOLD (1 * 48)
  36. /* No name ambiguities in this file. */
  37. __asm__ (".syntax no_register_prefix");
  38. void *memset(void *pdst, int c, unsigned int plen)
  39. {
  40. /* Now we want the parameters in special registers. Make sure the
  41. compiler does something usable with this. */
  42. register char *return_dst __asm__ ("r10") = pdst;
  43. register int n __asm__ ("r12") = plen;
  44. register int lc __asm__ ("r11") = c;
  45. /* Most apps use memset sanely. Memsetting about 3..4 bytes or less get
  46. penalized here compared to the generic implementation. */
  47. /* This is fragile performancewise at best. Check with newer GCC
  48. releases, if they compile cascaded "x |= x << 8" to sane code. */
  49. __asm__("movu.b %0,r13 \n\
  50. lslq 8,r13 \n\
  51. move.b %0,r13 \n\
  52. move.d r13,%0 \n\
  53. lslq 16,r13 \n\
  54. or.d r13,%0"
  55. : "=r" (lc) /* Inputs. */
  56. : "0" (lc) /* Outputs. */
  57. : "r13"); /* Trash. */
  58. {
  59. register char *dst __asm__ ("r13") = pdst;
  60. if (((unsigned long) pdst & 3) != 0
  61. /* Oops! n = 0 must be a valid call, regardless of alignment. */
  62. && n >= 3)
  63. {
  64. if ((unsigned long) dst & 1)
  65. {
  66. *dst = (char) lc;
  67. n--;
  68. dst++;
  69. }
  70. if ((unsigned long) dst & 2)
  71. {
  72. *(short *) dst = lc;
  73. n -= 2;
  74. dst += 2;
  75. }
  76. }
  77. /* Decide which setting method to use. */
  78. if (n >= MEMSET_BY_BLOCK_THRESHOLD)
  79. {
  80. /* It is not optimal to tell the compiler about clobbering any
  81. registers; that will move the saving/restoring of those registers
  82. to the function prologue/epilogue, and make non-block sizes
  83. suboptimal. */
  84. __asm__ __volatile__
  85. ("\
  86. ;; GCC does promise correct register allocations, but let's \n\
  87. ;; make sure it keeps its promises. \n\
  88. .ifnc %0-%1-%4,$r13-$r12-$r11 \n\
  89. .error \"GCC reg alloc bug: %0-%1-%4 != $r13-$r12-$r11\" \n\
  90. .endif \n\
  91. \n\
  92. ;; Save the registers we'll clobber in the movem process \n\
  93. ;; on the stack. Don't mention them to gcc, it will only be \n\
  94. ;; upset. \n\
  95. subq 11*4,sp \n\
  96. movem r10,[sp] \n\
  97. \n\
  98. move.d r11,r0 \n\
  99. move.d r11,r1 \n\
  100. move.d r11,r2 \n\
  101. move.d r11,r3 \n\
  102. move.d r11,r4 \n\
  103. move.d r11,r5 \n\
  104. move.d r11,r6 \n\
  105. move.d r11,r7 \n\
  106. move.d r11,r8 \n\
  107. move.d r11,r9 \n\
  108. move.d r11,r10 \n\
  109. \n\
  110. ;; Now we've got this: \n\
  111. ;; r13 - dst \n\
  112. ;; r12 - n \n\
  113. \n\
  114. ;; Update n for the first loop \n\
  115. subq 12*4,r12 \n\
  116. 0: \n\
  117. "
  118. #ifdef __arch_common_v10_v32
  119. /* Cater to branch offset difference between v32 and v10. We
  120. assume the branch below has an 8-bit offset. */
  121. " setf\n"
  122. #endif
  123. " subq 12*4,r12 \n\
  124. bge 0b \n\
  125. movem r11,[r13+] \n\
  126. \n\
  127. ;; Compensate for last loop underflowing n. \n\
  128. addq 12*4,r12 \n\
  129. \n\
  130. ;; Restore registers from stack. \n\
  131. movem [sp+],r10"
  132. /* Outputs. */
  133. : "=r" (dst), "=r" (n)
  134. /* Inputs. */
  135. : "0" (dst), "1" (n), "r" (lc));
  136. }
  137. /* An ad-hoc unroll, used for 4*12-1..16 bytes. */
  138. while (n >= 16)
  139. {
  140. *(long *) dst = lc; dst += 4;
  141. *(long *) dst = lc; dst += 4;
  142. *(long *) dst = lc; dst += 4;
  143. *(long *) dst = lc; dst += 4;
  144. n -= 16;
  145. }
  146. switch (n)
  147. {
  148. case 0:
  149. break;
  150. case 1:
  151. *dst = (char) lc;
  152. break;
  153. case 2:
  154. *(short *) dst = (short) lc;
  155. break;
  156. case 3:
  157. *(short *) dst = (short) lc; dst += 2;
  158. *dst = (char) lc;
  159. break;
  160. case 4:
  161. *(long *) dst = lc;
  162. break;
  163. case 5:
  164. *(long *) dst = lc; dst += 4;
  165. *dst = (char) lc;
  166. break;
  167. case 6:
  168. *(long *) dst = lc; dst += 4;
  169. *(short *) dst = (short) lc;
  170. break;
  171. case 7:
  172. *(long *) dst = lc; dst += 4;
  173. *(short *) dst = (short) lc; dst += 2;
  174. *dst = (char) lc;
  175. break;
  176. case 8:
  177. *(long *) dst = lc; dst += 4;
  178. *(long *) dst = lc;
  179. break;
  180. case 9:
  181. *(long *) dst = lc; dst += 4;
  182. *(long *) dst = lc; dst += 4;
  183. *dst = (char) lc;
  184. break;
  185. case 10:
  186. *(long *) dst = lc; dst += 4;
  187. *(long *) dst = lc; dst += 4;
  188. *(short *) dst = (short) lc;
  189. break;
  190. case 11:
  191. *(long *) dst = lc; dst += 4;
  192. *(long *) dst = lc; dst += 4;
  193. *(short *) dst = (short) lc; dst += 2;
  194. *dst = (char) lc;
  195. break;
  196. case 12:
  197. *(long *) dst = lc; dst += 4;
  198. *(long *) dst = lc; dst += 4;
  199. *(long *) dst = lc;
  200. break;
  201. case 13:
  202. *(long *) dst = lc; dst += 4;
  203. *(long *) dst = lc; dst += 4;
  204. *(long *) dst = lc; dst += 4;
  205. *dst = (char) lc;
  206. break;
  207. case 14:
  208. *(long *) dst = lc; dst += 4;
  209. *(long *) dst = lc; dst += 4;
  210. *(long *) dst = lc; dst += 4;
  211. *(short *) dst = (short) lc;
  212. break;
  213. case 15:
  214. *(long *) dst = lc; dst += 4;
  215. *(long *) dst = lc; dst += 4;
  216. *(long *) dst = lc; dst += 4;
  217. *(short *) dst = (short) lc; dst += 2;
  218. *dst = (char) lc;
  219. break;
  220. }
  221. }
  222. return return_dst;
  223. }
  224. libc_hidden_def(memset)