bzero.S 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* Optimized version of the standard bzero() function.
  2. This file is part of the GNU C Library.
  3. Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
  4. Contributed by Dan Pop for Itanium <Dan.Pop@cern.ch>.
  5. Rewritten for McKinley by Sverre Jarp, HP Labs/CERN <Sverre.Jarp@cern.ch>
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, see
  16. <http://www.gnu.org/licenses/>. */
  17. /* Return: dest
  18. Inputs:
  19. in0: dest
  20. in1: count
  21. The algorithm is fairly straightforward: set byte by byte until we
  22. we get to a 16B-aligned address, then loop on 128 B chunks using an
  23. early store as prefetching, then loop on 32B chucks, then clear remaining
  24. words, finally clear remaining bytes.
  25. Since a stf.spill f0 can store 16B in one go, we use this instruction
  26. to get peak speed. */
  27. #include <sysdep.h>
  28. #ifdef __UCLIBC_SUSV3_LEGACY__
  29. #undef ret
  30. #define dest in0
  31. #define cnt in1
  32. #define tmp r31
  33. #define save_lc r30
  34. #define ptr0 r29
  35. #define ptr1 r28
  36. #define ptr2 r27
  37. #define ptr3 r26
  38. #define ptr9 r24
  39. #define loopcnt r23
  40. #define linecnt r22
  41. #define bytecnt r21
  42. /* This routine uses only scratch predicate registers (p6 - p15) */
  43. #define p_scr p6 /* default register for same-cycle branches */
  44. #define p_unalgn p9
  45. #define p_y p11
  46. #define p_n p12
  47. #define p_yy p13
  48. #define p_nn p14
  49. #define movi0 mov
  50. #define MIN1 15
  51. #define MIN1P1HALF 8
  52. #define LINE_SIZE 128
  53. #define LSIZE_SH 7 /* shift amount */
  54. #define PREF_AHEAD 8
  55. #define USE_FLP
  56. #if defined(USE_INT)
  57. #define store st8
  58. #define myval r0
  59. #elif defined(USE_FLP)
  60. #define store stf8
  61. #define myval f0
  62. #endif
  63. .align 64
  64. ENTRY(bzero)
  65. { .mmi
  66. .prologue
  67. alloc tmp = ar.pfs, 2, 0, 0, 0
  68. lfetch.nt1 [dest]
  69. .save ar.lc, save_lc
  70. movi0 save_lc = ar.lc
  71. } { .mmi
  72. .body
  73. mov ret0 = dest /* return value */
  74. nop.m 0
  75. cmp.eq p_scr, p0 = cnt, r0
  76. ;; }
  77. { .mmi
  78. and ptr2 = -(MIN1+1), dest /* aligned address */
  79. and tmp = MIN1, dest /* prepare to check for alignment */
  80. tbit.nz p_y, p_n = dest, 0 /* Do we have an odd address? (M_B_U) */
  81. } { .mib
  82. mov ptr1 = dest
  83. nop.i 0
  84. (p_scr) br.ret.dpnt.many rp /* return immediately if count = 0 */
  85. ;; }
  86. { .mib
  87. cmp.ne p_unalgn, p0 = tmp, r0
  88. } { .mib /* NB: # of bytes to move is 1 */
  89. sub bytecnt = (MIN1+1), tmp /* higher than loopcnt */
  90. cmp.gt p_scr, p0 = 16, cnt /* is it a minimalistic task? */
  91. (p_scr) br.cond.dptk.many .move_bytes_unaligned /* go move just a few (M_B_U) */
  92. ;; }
  93. { .mmi
  94. (p_unalgn) add ptr1 = (MIN1+1), ptr2 /* after alignment */
  95. (p_unalgn) add ptr2 = MIN1P1HALF, ptr2 /* after alignment */
  96. (p_unalgn) tbit.nz.unc p_y, p_n = bytecnt, 3 /* should we do a st8 ? */
  97. ;; }
  98. { .mib
  99. (p_y) add cnt = -8, cnt
  100. (p_unalgn) tbit.nz.unc p_yy, p_nn = bytecnt, 2 /* should we do a st4 ? */
  101. } { .mib
  102. (p_y) st8 [ptr2] = r0,-4
  103. (p_n) add ptr2 = 4, ptr2
  104. ;; }
  105. { .mib
  106. (p_yy) add cnt = -4, cnt
  107. (p_unalgn) tbit.nz.unc p_y, p_n = bytecnt, 1 /* should we do a st2 ? */
  108. } { .mib
  109. (p_yy) st4 [ptr2] = r0,-2
  110. (p_nn) add ptr2 = 2, ptr2
  111. ;; }
  112. { .mmi
  113. mov tmp = LINE_SIZE+1 /* for compare */
  114. (p_y) add cnt = -2, cnt
  115. (p_unalgn) tbit.nz.unc p_yy, p_nn = bytecnt, 0 /* should we do a st1 ? */
  116. } { .mmi
  117. nop.m 0
  118. (p_y) st2 [ptr2] = r0,-1
  119. (p_n) add ptr2 = 1, ptr2
  120. ;; }
  121. { .mmi
  122. (p_yy) st1 [ptr2] = r0
  123. cmp.gt p_scr, p0 = tmp, cnt /* is it a minimalistic task? */
  124. } { .mbb
  125. (p_yy) add cnt = -1, cnt
  126. (p_scr) br.cond.dpnt.many .fraction_of_line /* go move just a few */
  127. ;; }
  128. { .mib
  129. nop.m 0
  130. shr.u linecnt = cnt, LSIZE_SH
  131. nop.b 0
  132. ;; }
  133. .align 32
  134. .l1b: /* ------------------ L1B: store ahead into cache lines; fill later */
  135. { .mmi
  136. and tmp = -(LINE_SIZE), cnt /* compute end of range */
  137. mov ptr9 = ptr1 /* used for prefetching */
  138. and cnt = (LINE_SIZE-1), cnt /* remainder */
  139. } { .mmi
  140. mov loopcnt = PREF_AHEAD-1 /* default prefetch loop */
  141. cmp.gt p_scr, p0 = PREF_AHEAD, linecnt /* check against actual value */
  142. ;; }
  143. { .mmi
  144. (p_scr) add loopcnt = -1, linecnt
  145. add ptr2 = 16, ptr1 /* start of stores (beyond prefetch stores) */
  146. add ptr1 = tmp, ptr1 /* first address beyond total range */
  147. ;; }
  148. { .mmi
  149. add tmp = -1, linecnt /* next loop count */
  150. movi0 ar.lc = loopcnt
  151. ;; }
  152. .pref_l1b:
  153. { .mib
  154. stf.spill [ptr9] = f0, 128 /* Do stores one cache line apart */
  155. nop.i 0
  156. br.cloop.dptk.few .pref_l1b
  157. ;; }
  158. { .mmi
  159. add ptr0 = 16, ptr2 /* Two stores in parallel */
  160. movi0 ar.lc = tmp
  161. ;; }
  162. .l1bx:
  163. { .mmi
  164. stf.spill [ptr2] = f0, 32
  165. stf.spill [ptr0] = f0, 32
  166. ;; }
  167. { .mmi
  168. stf.spill [ptr2] = f0, 32
  169. stf.spill [ptr0] = f0, 32
  170. ;; }
  171. { .mmi
  172. stf.spill [ptr2] = f0, 32
  173. stf.spill [ptr0] = f0, 64
  174. cmp.lt p_scr, p0 = ptr9, ptr1 /* do we need more prefetching? */
  175. ;; }
  176. { .mmb
  177. stf.spill [ptr2] = f0, 32
  178. (p_scr) stf.spill [ptr9] = f0, 128
  179. br.cloop.dptk.few .l1bx
  180. ;; }
  181. { .mib
  182. cmp.gt p_scr, p0 = 8, cnt /* just a few bytes left ? */
  183. (p_scr) br.cond.dpnt.many .move_bytes_from_alignment
  184. ;; }
  185. .fraction_of_line:
  186. { .mib
  187. add ptr2 = 16, ptr1
  188. shr.u loopcnt = cnt, 5 /* loopcnt = cnt / 32 */
  189. ;; }
  190. { .mib
  191. cmp.eq p_scr, p0 = loopcnt, r0
  192. add loopcnt = -1, loopcnt
  193. (p_scr) br.cond.dpnt.many .store_words
  194. ;; }
  195. { .mib
  196. and cnt = 0x1f, cnt /* compute the remaining cnt */
  197. movi0 ar.lc = loopcnt
  198. ;; }
  199. .align 32
  200. .l2: /* ----------------------------- L2A: store 32B in 2 cycles */
  201. { .mmb
  202. store [ptr1] = myval, 8
  203. store [ptr2] = myval, 8
  204. ;; } { .mmb
  205. store [ptr1] = myval, 24
  206. store [ptr2] = myval, 24
  207. br.cloop.dptk.many .l2
  208. ;; }
  209. .store_words:
  210. { .mib
  211. cmp.gt p_scr, p0 = 8, cnt /* just a few bytes left ? */
  212. (p_scr) br.cond.dpnt.many .move_bytes_from_alignment /* Branch */
  213. ;; }
  214. { .mmi
  215. store [ptr1] = myval, 8 /* store */
  216. cmp.le p_y, p_n = 16, cnt /* */
  217. add cnt = -8, cnt /* subtract */
  218. ;; }
  219. { .mmi
  220. (p_y) store [ptr1] = myval, 8 /* store */
  221. (p_y) cmp.le.unc p_yy, p_nn = 16, cnt
  222. (p_y) add cnt = -8, cnt /* subtract */
  223. ;; }
  224. { .mmi /* store */
  225. (p_yy) store [ptr1] = myval, 8
  226. (p_yy) add cnt = -8, cnt /* subtract */
  227. ;; }
  228. .move_bytes_from_alignment:
  229. { .mib
  230. cmp.eq p_scr, p0 = cnt, r0
  231. tbit.nz.unc p_y, p0 = cnt, 2 /* should we terminate with a st4 ? */
  232. (p_scr) br.cond.dpnt.few .restore_and_exit
  233. ;; }
  234. { .mib
  235. (p_y) st4 [ptr1] = r0,4
  236. tbit.nz.unc p_yy, p0 = cnt, 1 /* should we terminate with a st2 ? */
  237. ;; }
  238. { .mib
  239. (p_yy) st2 [ptr1] = r0,2
  240. tbit.nz.unc p_y, p0 = cnt, 0 /* should we terminate with a st1 ? */
  241. ;; }
  242. { .mib
  243. (p_y) st1 [ptr1] = r0
  244. ;; }
  245. .restore_and_exit:
  246. { .mib
  247. nop.m 0
  248. movi0 ar.lc = save_lc
  249. br.ret.sptk.many rp
  250. ;; }
  251. .move_bytes_unaligned:
  252. { .mmi
  253. .pred.rel "mutex",p_y, p_n
  254. .pred.rel "mutex",p_yy, p_nn
  255. (p_n) cmp.le p_yy, p_nn = 4, cnt
  256. (p_y) cmp.le p_yy, p_nn = 5, cnt
  257. (p_n) add ptr2 = 2, ptr1
  258. } { .mmi
  259. (p_y) add ptr2 = 3, ptr1
  260. (p_y) st1 [ptr1] = r0, 1 /* fill 1 (odd-aligned) byte */
  261. (p_y) add cnt = -1, cnt /* [15, 14 (or less) left] */
  262. ;; }
  263. { .mmi
  264. (p_yy) cmp.le.unc p_y, p0 = 8, cnt
  265. add ptr3 = ptr1, cnt /* prepare last store */
  266. movi0 ar.lc = save_lc
  267. } { .mmi
  268. (p_yy) st2 [ptr1] = r0, 4 /* fill 2 (aligned) bytes */
  269. (p_yy) st2 [ptr2] = r0, 4 /* fill 2 (aligned) bytes */
  270. (p_yy) add cnt = -4, cnt /* [11, 10 (o less) left] */
  271. ;; }
  272. { .mmi
  273. (p_y) cmp.le.unc p_yy, p0 = 8, cnt
  274. add ptr3 = -1, ptr3 /* last store */
  275. tbit.nz p_scr, p0 = cnt, 1 /* will there be a st2 at the end ? */
  276. } { .mmi
  277. (p_y) st2 [ptr1] = r0, 4 /* fill 2 (aligned) bytes */
  278. (p_y) st2 [ptr2] = r0, 4 /* fill 2 (aligned) bytes */
  279. (p_y) add cnt = -4, cnt /* [7, 6 (or less) left] */
  280. ;; }
  281. { .mmi
  282. (p_yy) st2 [ptr1] = r0, 4 /* fill 2 (aligned) bytes */
  283. (p_yy) st2 [ptr2] = r0, 4 /* fill 2 (aligned) bytes */
  284. /* [3, 2 (or less) left] */
  285. tbit.nz p_y, p0 = cnt, 0 /* will there be a st1 at the end ? */
  286. } { .mmi
  287. (p_yy) add cnt = -4, cnt
  288. ;; }
  289. { .mmb
  290. (p_scr) st2 [ptr1] = r0 /* fill 2 (aligned) bytes */
  291. (p_y) st1 [ptr3] = r0 /* fill last byte (using ptr3) */
  292. br.ret.sptk.many rp
  293. ;; }
  294. END(bzero)
  295. #endif