memset.S 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* Optimized version of the standard memset() function.
  2. Copyright (c) 2002 Hewlett-Packard Co/CERN
  3. Sverre Jarp <Sverre.Jarp@cern.ch>
  4. Return: dest
  5. Inputs:
  6. in0: dest
  7. in1: value
  8. in2: count
  9. The algorithm is fairly straightforward: set byte by byte until we
  10. we get to a 16B-aligned address, then loop on 128 B chunks using an
  11. early store as prefetching, then loop on 32B chucks, then clear remaining
  12. words, finally clear remaining bytes.
  13. Since a stf.spill f0 can store 16B in one go, we use this instruction
  14. to get peak speed when value = 0. */
  15. #include <asm/asmmacro.h>
  16. #include <asm/export.h>
  17. #undef ret
  18. #define dest in0
  19. #define value in1
  20. #define cnt in2
  21. #define tmp r31
  22. #define save_lc r30
  23. #define ptr0 r29
  24. #define ptr1 r28
  25. #define ptr2 r27
  26. #define ptr3 r26
  27. #define ptr9 r24
  28. #define loopcnt r23
  29. #define linecnt r22
  30. #define bytecnt r21
  31. #define fvalue f6
  32. // This routine uses only scratch predicate registers (p6 - p15)
  33. #define p_scr p6 // default register for same-cycle branches
  34. #define p_nz p7
  35. #define p_zr p8
  36. #define p_unalgn p9
  37. #define p_y p11
  38. #define p_n p12
  39. #define p_yy p13
  40. #define p_nn p14
  41. #define MIN1 15
  42. #define MIN1P1HALF 8
  43. #define LINE_SIZE 128
  44. #define LSIZE_SH 7 // shift amount
  45. #define PREF_AHEAD 8
  46. GLOBAL_ENTRY(memset)
  47. { .mmi
  48. .prologue
  49. alloc tmp = ar.pfs, 3, 0, 0, 0
  50. lfetch.nt1 [dest] //
  51. .save ar.lc, save_lc
  52. mov.i save_lc = ar.lc
  53. .body
  54. } { .mmi
  55. mov ret0 = dest // return value
  56. cmp.ne p_nz, p_zr = value, r0 // use stf.spill if value is zero
  57. cmp.eq p_scr, p0 = cnt, r0
  58. ;; }
  59. { .mmi
  60. and ptr2 = -(MIN1+1), dest // aligned address
  61. and tmp = MIN1, dest // prepare to check for correct alignment
  62. tbit.nz p_y, p_n = dest, 0 // Do we have an odd address? (M_B_U)
  63. } { .mib
  64. mov ptr1 = dest
  65. mux1 value = value, @brcst // create 8 identical bytes in word
  66. (p_scr) br.ret.dpnt.many rp // return immediately if count = 0
  67. ;; }
  68. { .mib
  69. cmp.ne p_unalgn, p0 = tmp, r0 //
  70. } { .mib
  71. sub bytecnt = (MIN1+1), tmp // NB: # of bytes to move is 1 higher than loopcnt
  72. cmp.gt p_scr, p0 = 16, cnt // is it a minimalistic task?
  73. (p_scr) br.cond.dptk.many .move_bytes_unaligned // go move just a few (M_B_U)
  74. ;; }
  75. { .mmi
  76. (p_unalgn) add ptr1 = (MIN1+1), ptr2 // after alignment
  77. (p_unalgn) add ptr2 = MIN1P1HALF, ptr2 // after alignment
  78. (p_unalgn) tbit.nz.unc p_y, p_n = bytecnt, 3 // should we do a st8 ?
  79. ;; }
  80. { .mib
  81. (p_y) add cnt = -8, cnt //
  82. (p_unalgn) tbit.nz.unc p_yy, p_nn = bytecnt, 2 // should we do a st4 ?
  83. } { .mib
  84. (p_y) st8 [ptr2] = value,-4 //
  85. (p_n) add ptr2 = 4, ptr2 //
  86. ;; }
  87. { .mib
  88. (p_yy) add cnt = -4, cnt //
  89. (p_unalgn) tbit.nz.unc p_y, p_n = bytecnt, 1 // should we do a st2 ?
  90. } { .mib
  91. (p_yy) st4 [ptr2] = value,-2 //
  92. (p_nn) add ptr2 = 2, ptr2 //
  93. ;; }
  94. { .mmi
  95. mov tmp = LINE_SIZE+1 // for compare
  96. (p_y) add cnt = -2, cnt //
  97. (p_unalgn) tbit.nz.unc p_yy, p_nn = bytecnt, 0 // should we do a st1 ?
  98. } { .mmi
  99. setf.sig fvalue=value // transfer value to FLP side
  100. (p_y) st2 [ptr2] = value,-1 //
  101. (p_n) add ptr2 = 1, ptr2 //
  102. ;; }
  103. { .mmi
  104. (p_yy) st1 [ptr2] = value //
  105. cmp.gt p_scr, p0 = tmp, cnt // is it a minimalistic task?
  106. } { .mbb
  107. (p_yy) add cnt = -1, cnt //
  108. (p_scr) br.cond.dpnt.many .fraction_of_line // go move just a few
  109. ;; }
  110. { .mib
  111. nop.m 0
  112. shr.u linecnt = cnt, LSIZE_SH
  113. (p_zr) br.cond.dptk.many .l1b // Jump to use stf.spill
  114. ;; }
  115. TEXT_ALIGN(32) // --------------------- // L1A: store ahead into cache lines; fill later
  116. { .mmi
  117. and tmp = -(LINE_SIZE), cnt // compute end of range
  118. mov ptr9 = ptr1 // used for prefetching
  119. and cnt = (LINE_SIZE-1), cnt // remainder
  120. } { .mmi
  121. mov loopcnt = PREF_AHEAD-1 // default prefetch loop
  122. cmp.gt p_scr, p0 = PREF_AHEAD, linecnt // check against actual value
  123. ;; }
  124. { .mmi
  125. (p_scr) add loopcnt = -1, linecnt //
  126. add ptr2 = 8, ptr1 // start of stores (beyond prefetch stores)
  127. add ptr1 = tmp, ptr1 // first address beyond total range
  128. ;; }
  129. { .mmi
  130. add tmp = -1, linecnt // next loop count
  131. mov.i ar.lc = loopcnt //
  132. ;; }
  133. .pref_l1a:
  134. { .mib
  135. stf8 [ptr9] = fvalue, 128 // Do stores one cache line apart
  136. nop.i 0
  137. br.cloop.dptk.few .pref_l1a
  138. ;; }
  139. { .mmi
  140. add ptr0 = 16, ptr2 // Two stores in parallel
  141. mov.i ar.lc = tmp //
  142. ;; }
  143. .l1ax:
  144. { .mmi
  145. stf8 [ptr2] = fvalue, 8
  146. stf8 [ptr0] = fvalue, 8
  147. ;; }
  148. { .mmi
  149. stf8 [ptr2] = fvalue, 24
  150. stf8 [ptr0] = fvalue, 24
  151. ;; }
  152. { .mmi
  153. stf8 [ptr2] = fvalue, 8
  154. stf8 [ptr0] = fvalue, 8
  155. ;; }
  156. { .mmi
  157. stf8 [ptr2] = fvalue, 24
  158. stf8 [ptr0] = fvalue, 24
  159. ;; }
  160. { .mmi
  161. stf8 [ptr2] = fvalue, 8
  162. stf8 [ptr0] = fvalue, 8
  163. ;; }
  164. { .mmi
  165. stf8 [ptr2] = fvalue, 24
  166. stf8 [ptr0] = fvalue, 24
  167. ;; }
  168. { .mmi
  169. stf8 [ptr2] = fvalue, 8
  170. stf8 [ptr0] = fvalue, 32
  171. cmp.lt p_scr, p0 = ptr9, ptr1 // do we need more prefetching?
  172. ;; }
  173. { .mmb
  174. stf8 [ptr2] = fvalue, 24
  175. (p_scr) stf8 [ptr9] = fvalue, 128
  176. br.cloop.dptk.few .l1ax
  177. ;; }
  178. { .mbb
  179. cmp.le p_scr, p0 = 8, cnt // just a few bytes left ?
  180. (p_scr) br.cond.dpnt.many .fraction_of_line // Branch no. 2
  181. br.cond.dpnt.many .move_bytes_from_alignment // Branch no. 3
  182. ;; }
  183. TEXT_ALIGN(32)
  184. .l1b: // ------------------------------------ // L1B: store ahead into cache lines; fill later
  185. { .mmi
  186. and tmp = -(LINE_SIZE), cnt // compute end of range
  187. mov ptr9 = ptr1 // used for prefetching
  188. and cnt = (LINE_SIZE-1), cnt // remainder
  189. } { .mmi
  190. mov loopcnt = PREF_AHEAD-1 // default prefetch loop
  191. cmp.gt p_scr, p0 = PREF_AHEAD, linecnt // check against actual value
  192. ;; }
  193. { .mmi
  194. (p_scr) add loopcnt = -1, linecnt
  195. add ptr2 = 16, ptr1 // start of stores (beyond prefetch stores)
  196. add ptr1 = tmp, ptr1 // first address beyond total range
  197. ;; }
  198. { .mmi
  199. add tmp = -1, linecnt // next loop count
  200. mov.i ar.lc = loopcnt
  201. ;; }
  202. .pref_l1b:
  203. { .mib
  204. stf.spill [ptr9] = f0, 128 // Do stores one cache line apart
  205. nop.i 0
  206. br.cloop.dptk.few .pref_l1b
  207. ;; }
  208. { .mmi
  209. add ptr0 = 16, ptr2 // Two stores in parallel
  210. mov.i ar.lc = tmp
  211. ;; }
  212. .l1bx:
  213. { .mmi
  214. stf.spill [ptr2] = f0, 32
  215. stf.spill [ptr0] = f0, 32
  216. ;; }
  217. { .mmi
  218. stf.spill [ptr2] = f0, 32
  219. stf.spill [ptr0] = f0, 32
  220. ;; }
  221. { .mmi
  222. stf.spill [ptr2] = f0, 32
  223. stf.spill [ptr0] = f0, 64
  224. cmp.lt p_scr, p0 = ptr9, ptr1 // do we need more prefetching?
  225. ;; }
  226. { .mmb
  227. stf.spill [ptr2] = f0, 32
  228. (p_scr) stf.spill [ptr9] = f0, 128
  229. br.cloop.dptk.few .l1bx
  230. ;; }
  231. { .mib
  232. cmp.gt p_scr, p0 = 8, cnt // just a few bytes left ?
  233. (p_scr) br.cond.dpnt.many .move_bytes_from_alignment //
  234. ;; }
  235. .fraction_of_line:
  236. { .mib
  237. add ptr2 = 16, ptr1
  238. shr.u loopcnt = cnt, 5 // loopcnt = cnt / 32
  239. ;; }
  240. { .mib
  241. cmp.eq p_scr, p0 = loopcnt, r0
  242. add loopcnt = -1, loopcnt
  243. (p_scr) br.cond.dpnt.many .store_words
  244. ;; }
  245. { .mib
  246. and cnt = 0x1f, cnt // compute the remaining cnt
  247. mov.i ar.lc = loopcnt
  248. ;; }
  249. TEXT_ALIGN(32)
  250. .l2: // ------------------------------------ // L2A: store 32B in 2 cycles
  251. { .mmb
  252. stf8 [ptr1] = fvalue, 8
  253. stf8 [ptr2] = fvalue, 8
  254. ;; } { .mmb
  255. stf8 [ptr1] = fvalue, 24
  256. stf8 [ptr2] = fvalue, 24
  257. br.cloop.dptk.many .l2
  258. ;; }
  259. .store_words:
  260. { .mib
  261. cmp.gt p_scr, p0 = 8, cnt // just a few bytes left ?
  262. (p_scr) br.cond.dpnt.many .move_bytes_from_alignment // Branch
  263. ;; }
  264. { .mmi
  265. stf8 [ptr1] = fvalue, 8 // store
  266. cmp.le p_y, p_n = 16, cnt
  267. add cnt = -8, cnt // subtract
  268. ;; }
  269. { .mmi
  270. (p_y) stf8 [ptr1] = fvalue, 8 // store
  271. (p_y) cmp.le.unc p_yy, p_nn = 16, cnt
  272. (p_y) add cnt = -8, cnt // subtract
  273. ;; }
  274. { .mmi // store
  275. (p_yy) stf8 [ptr1] = fvalue, 8
  276. (p_yy) add cnt = -8, cnt // subtract
  277. ;; }
  278. .move_bytes_from_alignment:
  279. { .mib
  280. cmp.eq p_scr, p0 = cnt, r0
  281. tbit.nz.unc p_y, p0 = cnt, 2 // should we terminate with a st4 ?
  282. (p_scr) br.cond.dpnt.few .restore_and_exit
  283. ;; }
  284. { .mib
  285. (p_y) st4 [ptr1] = value,4
  286. tbit.nz.unc p_yy, p0 = cnt, 1 // should we terminate with a st2 ?
  287. ;; }
  288. { .mib
  289. (p_yy) st2 [ptr1] = value,2
  290. tbit.nz.unc p_y, p0 = cnt, 0 // should we terminate with a st1 ?
  291. ;; }
  292. { .mib
  293. (p_y) st1 [ptr1] = value
  294. ;; }
  295. .restore_and_exit:
  296. { .mib
  297. nop.m 0
  298. mov.i ar.lc = save_lc
  299. br.ret.sptk.many rp
  300. ;; }
  301. .move_bytes_unaligned:
  302. { .mmi
  303. .pred.rel "mutex",p_y, p_n
  304. .pred.rel "mutex",p_yy, p_nn
  305. (p_n) cmp.le p_yy, p_nn = 4, cnt
  306. (p_y) cmp.le p_yy, p_nn = 5, cnt
  307. (p_n) add ptr2 = 2, ptr1
  308. } { .mmi
  309. (p_y) add ptr2 = 3, ptr1
  310. (p_y) st1 [ptr1] = value, 1 // fill 1 (odd-aligned) byte [15, 14 (or less) left]
  311. (p_y) add cnt = -1, cnt
  312. ;; }
  313. { .mmi
  314. (p_yy) cmp.le.unc p_y, p0 = 8, cnt
  315. add ptr3 = ptr1, cnt // prepare last store
  316. mov.i ar.lc = save_lc
  317. } { .mmi
  318. (p_yy) st2 [ptr1] = value, 4 // fill 2 (aligned) bytes
  319. (p_yy) st2 [ptr2] = value, 4 // fill 2 (aligned) bytes [11, 10 (o less) left]
  320. (p_yy) add cnt = -4, cnt
  321. ;; }
  322. { .mmi
  323. (p_y) cmp.le.unc p_yy, p0 = 8, cnt
  324. add ptr3 = -1, ptr3 // last store
  325. tbit.nz p_scr, p0 = cnt, 1 // will there be a st2 at the end ?
  326. } { .mmi
  327. (p_y) st2 [ptr1] = value, 4 // fill 2 (aligned) bytes
  328. (p_y) st2 [ptr2] = value, 4 // fill 2 (aligned) bytes [7, 6 (or less) left]
  329. (p_y) add cnt = -4, cnt
  330. ;; }
  331. { .mmi
  332. (p_yy) st2 [ptr1] = value, 4 // fill 2 (aligned) bytes
  333. (p_yy) st2 [ptr2] = value, 4 // fill 2 (aligned) bytes [3, 2 (or less) left]
  334. tbit.nz p_y, p0 = cnt, 0 // will there be a st1 at the end ?
  335. } { .mmi
  336. (p_yy) add cnt = -4, cnt
  337. ;; }
  338. { .mmb
  339. (p_scr) st2 [ptr1] = value // fill 2 (aligned) bytes
  340. (p_y) st1 [ptr3] = value // fill last byte (using ptr3)
  341. br.ret.sptk.many rp
  342. }
  343. END(memset)
  344. EXPORT_SYMBOL(memset)