bzero.S 7.9 KB

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