memset.S 10 KB

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