memset.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* A memset for CRIS.
  2. Copyright (C) 1999-2005 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. /* Note the multiple occurrence of the expression "12*4", including the
  28. asm. It is hard to get it into the asm in a good way. Thus better to
  29. expose the problem everywhere: no macro. */
  30. /* Assuming one cycle per dword written or read (ok, not really true; the
  31. world is not ideal), and one cycle per instruction, then 43+3*(n/48-1)
  32. <= 24+24*(n/48-1) so n >= 45.7; n >= 0.9; we win on the first full
  33. 48-byte block to set. */
  34. #define MEMSET_BY_BLOCK_THRESHOLD (1 * 48)
  35. /* No name ambiguities in this file. */
  36. __asm__ (".syntax no_register_prefix");
  37. void *memset(void *pdst, int c, unsigned int plen)
  38. {
  39. /* Now we want the parameters in special registers. Make sure the
  40. compiler does something usable with this. */
  41. register char *return_dst __asm__ ("r10") = pdst;
  42. register int n __asm__ ("r12") = plen;
  43. register int lc __asm__ ("r11") = c;
  44. /* Most apps use memset sanely. Memsetting about 3..4 bytes or less get
  45. penalized here compared to the generic implementation. */
  46. /* This is fragile performancewise at best. Check with newer GCC
  47. releases, if they compile cascaded "x |= x << 8" to sane code. */
  48. __asm__("movu.b %0,r13 \n\
  49. lslq 8,r13 \n\
  50. move.b %0,r13 \n\
  51. move.d r13,%0 \n\
  52. lslq 16,r13 \n\
  53. or.d r13,%0"
  54. : "=r" (lc) /* Inputs. */
  55. : "0" (lc) /* Outputs. */
  56. : "r13"); /* Trash. */
  57. {
  58. register char *dst __asm__ ("r13") = pdst;
  59. if (((unsigned long) pdst & 3) != 0
  60. /* Oops! n = 0 must be a valid call, regardless of alignment. */
  61. && n >= 3)
  62. {
  63. if ((unsigned long) dst & 1)
  64. {
  65. *dst = (char) lc;
  66. n--;
  67. dst++;
  68. }
  69. if ((unsigned long) dst & 2)
  70. {
  71. *(short *) dst = lc;
  72. n -= 2;
  73. dst += 2;
  74. }
  75. }
  76. /* Decide which setting method to use. */
  77. if (n >= MEMSET_BY_BLOCK_THRESHOLD)
  78. {
  79. /* It is not optimal to tell the compiler about clobbering any
  80. registers; that will move the saving/restoring of those registers
  81. to the function prologue/epilogue, and make non-block sizes
  82. suboptimal. */
  83. __asm__ volatile
  84. ("\
  85. ;; GCC does promise correct register allocations, but let's \n\
  86. ;; make sure it keeps its promises. \n\
  87. .ifnc %0-%1-%4,$r13-$r12-$r11 \n\
  88. .error \"GCC reg alloc bug: %0-%1-%4 != $r13-$r12-$r11\" \n\
  89. .endif \n\
  90. \n\
  91. ;; Save the registers we'll clobber in the movem process \n\
  92. ;; on the stack. Don't mention them to gcc, it will only be \n\
  93. ;; upset. \n\
  94. subq 11*4,sp \n\
  95. movem r10,[sp] \n\
  96. \n\
  97. move.d r11,r0 \n\
  98. move.d r11,r1 \n\
  99. move.d r11,r2 \n\
  100. move.d r11,r3 \n\
  101. move.d r11,r4 \n\
  102. move.d r11,r5 \n\
  103. move.d r11,r6 \n\
  104. move.d r11,r7 \n\
  105. move.d r11,r8 \n\
  106. move.d r11,r9 \n\
  107. move.d r11,r10 \n\
  108. \n\
  109. ;; Now we've got this: \n\
  110. ;; r13 - dst \n\
  111. ;; r12 - n \n\
  112. \n\
  113. ;; Update n for the first loop \n\
  114. subq 12*4,r12 \n\
  115. 0: \n\
  116. "
  117. #ifdef __arch_common_v10_v32
  118. /* Cater to branch offset difference between v32 and v10. We
  119. assume the branch below has an 8-bit offset. */
  120. " setf\n"
  121. #endif
  122. " subq 12*4,r12 \n\
  123. bge 0b \n\
  124. movem r11,[r13+] \n\
  125. \n\
  126. ;; Compensate for last loop underflowing n. \n\
  127. addq 12*4,r12 \n\
  128. \n\
  129. ;; Restore registers from stack. \n\
  130. movem [sp+],r10"
  131. /* Outputs. */
  132. : "=r" (dst), "=r" (n)
  133. /* Inputs. */
  134. : "0" (dst), "1" (n), "r" (lc));
  135. }
  136. /* An ad-hoc unroll, used for 4*12-1..16 bytes. */
  137. while (n >= 16)
  138. {
  139. *(long *) dst = lc; dst += 4;
  140. *(long *) dst = lc; dst += 4;
  141. *(long *) dst = lc; dst += 4;
  142. *(long *) dst = lc; dst += 4;
  143. n -= 16;
  144. }
  145. switch (n)
  146. {
  147. case 0:
  148. break;
  149. case 1:
  150. *dst = (char) lc;
  151. break;
  152. case 2:
  153. *(short *) dst = (short) lc;
  154. break;
  155. case 3:
  156. *(short *) dst = (short) lc; dst += 2;
  157. *dst = (char) lc;
  158. break;
  159. case 4:
  160. *(long *) dst = lc;
  161. break;
  162. case 5:
  163. *(long *) dst = lc; dst += 4;
  164. *dst = (char) lc;
  165. break;
  166. case 6:
  167. *(long *) dst = lc; dst += 4;
  168. *(short *) dst = (short) lc;
  169. break;
  170. case 7:
  171. *(long *) dst = lc; dst += 4;
  172. *(short *) dst = (short) lc; dst += 2;
  173. *dst = (char) lc;
  174. break;
  175. case 8:
  176. *(long *) dst = lc; dst += 4;
  177. *(long *) dst = lc;
  178. break;
  179. case 9:
  180. *(long *) dst = lc; dst += 4;
  181. *(long *) dst = lc; dst += 4;
  182. *dst = (char) lc;
  183. break;
  184. case 10:
  185. *(long *) dst = lc; dst += 4;
  186. *(long *) dst = lc; dst += 4;
  187. *(short *) dst = (short) lc;
  188. break;
  189. case 11:
  190. *(long *) dst = lc; dst += 4;
  191. *(long *) dst = lc; dst += 4;
  192. *(short *) dst = (short) lc; dst += 2;
  193. *dst = (char) lc;
  194. break;
  195. case 12:
  196. *(long *) dst = lc; dst += 4;
  197. *(long *) dst = lc; dst += 4;
  198. *(long *) dst = lc;
  199. break;
  200. case 13:
  201. *(long *) dst = lc; dst += 4;
  202. *(long *) dst = lc; dst += 4;
  203. *(long *) dst = lc; dst += 4;
  204. *dst = (char) lc;
  205. break;
  206. case 14:
  207. *(long *) dst = lc; dst += 4;
  208. *(long *) dst = lc; dst += 4;
  209. *(long *) dst = lc; dst += 4;
  210. *(short *) dst = (short) lc;
  211. break;
  212. case 15:
  213. *(long *) dst = lc; dst += 4;
  214. *(long *) dst = lc; dst += 4;
  215. *(long *) dst = lc; dst += 4;
  216. *(short *) dst = (short) lc; dst += 2;
  217. *dst = (char) lc;
  218. break;
  219. }
  220. }
  221. return return_dst;
  222. }