memset.S 9.1 KB

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