memset.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /* Copyright (C) 2013-2015 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library. If not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #ifdef ANDROID_CHANGES
  15. # include "machine/asm.h"
  16. # include "machine/regdef.h"
  17. # define PREFETCH_STORE_HINT PREFETCH_HINT_PREPAREFORSTORE
  18. #elif _LIBC
  19. # include <sysdep.h>
  20. # include <sys/regdef.h>
  21. # include <sys/asm.h>
  22. # define PREFETCH_STORE_HINT PREFETCH_HINT_PREPAREFORSTORE
  23. #elif defined _COMPILING_NEWLIB
  24. # include "machine/asm.h"
  25. # include "machine/regdef.h"
  26. # define PREFETCH_STORE_HINT PREFETCH_HINT_PREPAREFORSTORE
  27. #else
  28. # include <sys/regdef.h>
  29. # include <sys/asm.h>
  30. #endif
  31. /* Check to see if the MIPS architecture we are compiling for supports
  32. prefetching. */
  33. #if (__mips == 4) || (__mips == 5) || (__mips == 32) || (__mips == 64)
  34. # ifdef __UCLIBC_USE_MIPS_PREFETCH__
  35. # define USE_PREFETCH
  36. # endif
  37. #endif
  38. #if defined(_MIPS_SIM) && ((_MIPS_SIM == _ABI64) || (_MIPS_SIM == _ABIN32))
  39. # ifndef DISABLE_DOUBLE
  40. # define USE_DOUBLE
  41. # endif
  42. #endif
  43. #ifndef USE_DOUBLE
  44. # ifndef DISABLE_DOUBLE_ALIGN
  45. # define DOUBLE_ALIGN
  46. # endif
  47. #endif
  48. /* Some asm.h files do not have the L macro definition. */
  49. #ifndef L
  50. # if _MIPS_SIM == _ABIO32
  51. # define L(label) $L ## label
  52. # else
  53. # define L(label) .L ## label
  54. # endif
  55. #endif
  56. /* Some asm.h files do not have the PTR_ADDIU macro definition. */
  57. #ifndef PTR_ADDIU
  58. # ifdef USE_DOUBLE
  59. # define PTR_ADDIU daddiu
  60. # else
  61. # define PTR_ADDIU addiu
  62. # endif
  63. #endif
  64. /* New R6 instructions that may not be in asm.h. */
  65. #ifndef PTR_LSA
  66. # if _MIPS_SIM == _ABI64
  67. # define PTR_LSA dlsa
  68. # else
  69. # define PTR_LSA lsa
  70. # endif
  71. #endif
  72. /* Using PREFETCH_HINT_PREPAREFORSTORE instead of PREFETCH_STORE
  73. or PREFETCH_STORE_STREAMED offers a large performance advantage
  74. but PREPAREFORSTORE has some special restrictions to consider.
  75. Prefetch with the 'prepare for store' hint does not copy a memory
  76. location into the cache, it just allocates a cache line and zeros
  77. it out. This means that if you do not write to the entire cache
  78. line before writing it out to memory some data will get zero'ed out
  79. when the cache line is written back to memory and data will be lost.
  80. There are ifdef'ed sections of this memcpy to make sure that it does not
  81. do prefetches on cache lines that are not going to be completely written.
  82. This code is only needed and only used when PREFETCH_STORE_HINT is set to
  83. PREFETCH_HINT_PREPAREFORSTORE. This code assumes that cache lines are
  84. less than MAX_PREFETCH_SIZE bytes and if the cache line is larger it will
  85. not work correctly. */
  86. #ifdef USE_PREFETCH
  87. # define PREFETCH_HINT_STORE 1
  88. # define PREFETCH_HINT_STORE_STREAMED 5
  89. # define PREFETCH_HINT_STORE_RETAINED 7
  90. # define PREFETCH_HINT_PREPAREFORSTORE 30
  91. /* If we have not picked out what hints to use at this point use the
  92. standard load and store prefetch hints. */
  93. # ifndef PREFETCH_STORE_HINT
  94. # define PREFETCH_STORE_HINT PREFETCH_HINT_STORE
  95. # endif
  96. /* We double everything when USE_DOUBLE is true so we do 2 prefetches to
  97. get 64 bytes in that case. The assumption is that each individual
  98. prefetch brings in 32 bytes. */
  99. # ifdef USE_DOUBLE
  100. # define PREFETCH_CHUNK 64
  101. # define PREFETCH_FOR_STORE(chunk, reg) \
  102. pref PREFETCH_STORE_HINT, (chunk)*64(reg); \
  103. pref PREFETCH_STORE_HINT, ((chunk)*64)+32(reg)
  104. # else
  105. # define PREFETCH_CHUNK 32
  106. # define PREFETCH_FOR_STORE(chunk, reg) \
  107. pref PREFETCH_STORE_HINT, (chunk)*32(reg)
  108. # endif
  109. /* MAX_PREFETCH_SIZE is the maximum size of a prefetch, it must not be less
  110. than PREFETCH_CHUNK, the assumed size of each prefetch. If the real size
  111. of a prefetch is greater than MAX_PREFETCH_SIZE and the PREPAREFORSTORE
  112. hint is used, the code will not work correctly. If PREPAREFORSTORE is not
  113. used than MAX_PREFETCH_SIZE does not matter. */
  114. # define MAX_PREFETCH_SIZE 128
  115. /* PREFETCH_LIMIT is set based on the fact that we never use an offset greater
  116. than 5 on a STORE prefetch and that a single prefetch can never be larger
  117. than MAX_PREFETCH_SIZE. We add the extra 32 when USE_DOUBLE is set because
  118. we actually do two prefetches in that case, one 32 bytes after the other. */
  119. # ifdef USE_DOUBLE
  120. # define PREFETCH_LIMIT (5 * PREFETCH_CHUNK) + 32 + MAX_PREFETCH_SIZE
  121. # else
  122. # define PREFETCH_LIMIT (5 * PREFETCH_CHUNK) + MAX_PREFETCH_SIZE
  123. # endif
  124. # if (PREFETCH_STORE_HINT == PREFETCH_HINT_PREPAREFORSTORE) \
  125. && ((PREFETCH_CHUNK * 4) < MAX_PREFETCH_SIZE)
  126. /* We cannot handle this because the initial prefetches may fetch bytes that
  127. are before the buffer being copied. We start copies with an offset
  128. of 4 so avoid this situation when using PREPAREFORSTORE. */
  129. # error "PREFETCH_CHUNK is too large and/or MAX_PREFETCH_SIZE is too small."
  130. # endif
  131. #else /* USE_PREFETCH not defined */
  132. # define PREFETCH_FOR_STORE(offset, reg)
  133. #endif
  134. #if __mips_isa_rev > 5
  135. # if (PREFETCH_STORE_HINT == PREFETCH_HINT_PREPAREFORSTORE)
  136. # undef PREFETCH_STORE_HINT
  137. # define PREFETCH_STORE_HINT PREFETCH_HINT_STORE_STREAMED
  138. # endif
  139. # define R6_CODE
  140. #endif
  141. /* Allow the routine to be named something else if desired. */
  142. #ifndef MEMSET_NAME
  143. # define MEMSET_NAME memset
  144. #endif
  145. /* We load/store 64 bits at a time when USE_DOUBLE is true.
  146. The C_ prefix stands for CHUNK and is used to avoid macro name
  147. conflicts with system header files. */
  148. #ifdef USE_DOUBLE
  149. # define C_ST sd
  150. # ifdef __MIPSEB
  151. # define C_STHI sdl /* high part is left in big-endian */
  152. # else
  153. # define C_STHI sdr /* high part is right in little-endian */
  154. # endif
  155. #else
  156. # define C_ST sw
  157. # ifdef __MIPSEB
  158. # define C_STHI swl /* high part is left in big-endian */
  159. # else
  160. # define C_STHI swr /* high part is right in little-endian */
  161. # endif
  162. #endif
  163. /* Bookkeeping values for 32 vs. 64 bit mode. */
  164. #ifdef USE_DOUBLE
  165. # define NSIZE 8
  166. # define NSIZEMASK 0x3f
  167. # define NSIZEDMASK 0x7f
  168. #else
  169. # define NSIZE 4
  170. # define NSIZEMASK 0x1f
  171. # define NSIZEDMASK 0x3f
  172. #endif
  173. #define UNIT(unit) ((unit)*NSIZE)
  174. #define UNITM1(unit) (((unit)*NSIZE)-1)
  175. #ifdef ANDROID_CHANGES
  176. LEAF(MEMSET_NAME,0)
  177. #else
  178. LEAF(MEMSET_NAME)
  179. #endif
  180. .set nomips16
  181. .set noreorder
  182. /* If the size is less than 2*NSIZE (8 or 16), go to L(lastb). Regardless of
  183. size, copy dst pointer to v0 for the return value. */
  184. slti t2,a2,(2 * NSIZE)
  185. bne t2,zero,L(lastb)
  186. move v0,a0
  187. /* If memset value is not zero, we copy it to all the bytes in a 32 or 64
  188. bit word. */
  189. beq a1,zero,L(set0) /* If memset value is zero no smear */
  190. PTR_SUBU a3,zero,a0
  191. nop
  192. /* smear byte into 32 or 64 bit word */
  193. #if ((__mips == 64) || (__mips == 32)) && (__mips_isa_rev >= 2)
  194. # ifdef USE_DOUBLE
  195. dins a1, a1, 8, 8 /* Replicate fill byte into half-word. */
  196. dins a1, a1, 16, 16 /* Replicate fill byte into word. */
  197. dins a1, a1, 32, 32 /* Replicate fill byte into dbl word. */
  198. # else
  199. ins a1, a1, 8, 8 /* Replicate fill byte into half-word. */
  200. ins a1, a1, 16, 16 /* Replicate fill byte into word. */
  201. # endif
  202. #else
  203. # ifdef USE_DOUBLE
  204. and a1,0xff
  205. dsll t2,a1,8
  206. or a1,t2
  207. dsll t2,a1,16
  208. or a1,t2
  209. dsll t2,a1,32
  210. or a1,t2
  211. # else
  212. and a1,0xff
  213. sll t2,a1,8
  214. or a1,t2
  215. sll t2,a1,16
  216. or a1,t2
  217. # endif
  218. #endif
  219. /* If the destination address is not aligned do a partial store to get it
  220. aligned. If it is already aligned just jump to L(aligned). */
  221. L(set0):
  222. #ifndef R6_CODE
  223. andi t2,a3,(NSIZE-1) /* word-unaligned address? */
  224. beq t2,zero,L(aligned) /* t2 is the unalignment count */
  225. PTR_SUBU a2,a2,t2
  226. C_STHI a1,0(a0)
  227. PTR_ADDU a0,a0,t2
  228. #else /* R6_CODE */
  229. andi t2,a0,(NSIZE-1)
  230. lapc t9,L(atable)
  231. PTR_LSA t9,t2,t9,2
  232. jrc t9
  233. L(atable):
  234. bc L(aligned)
  235. # ifdef USE_DOUBLE
  236. bc L(lb7)
  237. bc L(lb6)
  238. bc L(lb5)
  239. bc L(lb4)
  240. # endif
  241. bc L(lb3)
  242. bc L(lb2)
  243. bc L(lb1)
  244. L(lb7):
  245. sb a1,6(a0)
  246. L(lb6):
  247. sb a1,5(a0)
  248. L(lb5):
  249. sb a1,4(a0)
  250. L(lb4):
  251. sb a1,3(a0)
  252. L(lb3):
  253. sb a1,2(a0)
  254. L(lb2):
  255. sb a1,1(a0)
  256. L(lb1):
  257. sb a1,0(a0)
  258. li t9,NSIZE
  259. subu t2,t9,t2
  260. PTR_SUBU a2,a2,t2
  261. PTR_ADDU a0,a0,t2
  262. #endif /* R6_CODE */
  263. L(aligned):
  264. /* If USE_DOUBLE is not set we may still want to align the data on a 16
  265. byte boundry instead of an 8 byte boundry to maximize the opportunity
  266. of proAptiv chips to do memory bonding (combining two sequential 4
  267. byte stores into one 8 byte store). We know there are at least 4 bytes
  268. left to store or we would have jumped to L(lastb) earlier in the code. */
  269. #ifdef DOUBLE_ALIGN
  270. andi t2,a3,4
  271. beq t2,zero,L(double_aligned)
  272. PTR_SUBU a2,a2,t2
  273. sw a1,0(a0)
  274. PTR_ADDU a0,a0,t2
  275. L(double_aligned):
  276. #endif
  277. /* Now the destination is aligned to (word or double word) aligned address
  278. Set a2 to count how many bytes we have to copy after all the 64/128 byte
  279. chunks are copied and a3 to the dest pointer after all the 64/128 byte
  280. chunks have been copied. We will loop, incrementing a0 until it equals
  281. a3. */
  282. andi t8,a2,NSIZEDMASK /* any whole 64-byte/128-byte chunks? */
  283. beq a2,t8,L(chkw) /* if a2==t8, no 64-byte/128-byte chunks */
  284. PTR_SUBU a3,a2,t8 /* subtract from a2 the reminder */
  285. PTR_ADDU a3,a0,a3 /* Now a3 is the final dst after loop */
  286. /* When in the loop we may prefetch with the 'prepare to store' hint,
  287. in this case the a0+x should not be past the "t0-32" address. This
  288. means: for x=128 the last "safe" a0 address is "t0-160". Alternatively,
  289. for x=64 the last "safe" a0 address is "t0-96" In the current version we
  290. will use "prefetch hint,128(a0)", so "t0-160" is the limit. */
  291. #if defined(USE_PREFETCH) \
  292. && (PREFETCH_STORE_HINT == PREFETCH_HINT_PREPAREFORSTORE)
  293. PTR_ADDU t0,a0,a2 /* t0 is the "past the end" address */
  294. PTR_SUBU t9,t0,PREFETCH_LIMIT /* t9 is the "last safe pref" address */
  295. #endif
  296. #if defined(USE_PREFETCH) \
  297. && (PREFETCH_STORE_HINT != PREFETCH_HINT_PREPAREFORSTORE)
  298. PREFETCH_FOR_STORE (1, a0)
  299. PREFETCH_FOR_STORE (2, a0)
  300. PREFETCH_FOR_STORE (3, a0)
  301. #endif
  302. L(loop16w):
  303. #if defined(USE_PREFETCH) \
  304. && (PREFETCH_STORE_HINT == PREFETCH_HINT_PREPAREFORSTORE)
  305. sltu v1,t9,a0 /* If a0 > t9 don't use next prefetch */
  306. bgtz v1,L(skip_pref)
  307. nop
  308. #endif
  309. #ifdef R6_CODE
  310. PREFETCH_FOR_STORE (2, a0)
  311. #else
  312. PREFETCH_FOR_STORE (4, a0)
  313. PREFETCH_FOR_STORE (5, a0)
  314. #endif
  315. L(skip_pref):
  316. C_ST a1,UNIT(0)(a0)
  317. C_ST a1,UNIT(1)(a0)
  318. C_ST a1,UNIT(2)(a0)
  319. C_ST a1,UNIT(3)(a0)
  320. C_ST a1,UNIT(4)(a0)
  321. C_ST a1,UNIT(5)(a0)
  322. C_ST a1,UNIT(6)(a0)
  323. C_ST a1,UNIT(7)(a0)
  324. C_ST a1,UNIT(8)(a0)
  325. C_ST a1,UNIT(9)(a0)
  326. C_ST a1,UNIT(10)(a0)
  327. C_ST a1,UNIT(11)(a0)
  328. C_ST a1,UNIT(12)(a0)
  329. C_ST a1,UNIT(13)(a0)
  330. C_ST a1,UNIT(14)(a0)
  331. C_ST a1,UNIT(15)(a0)
  332. PTR_ADDIU a0,a0,UNIT(16) /* adding 64/128 to dest */
  333. bne a0,a3,L(loop16w)
  334. nop
  335. move a2,t8
  336. /* Here we have dest word-aligned but less than 64-bytes or 128 bytes to go.
  337. Check for a 32(64) byte chunk and copy if if there is one. Otherwise
  338. jump down to L(chk1w) to handle the tail end of the copy. */
  339. L(chkw):
  340. andi t8,a2,NSIZEMASK /* is there a 32-byte/64-byte chunk. */
  341. /* the t8 is the reminder count past 32-bytes */
  342. beq a2,t8,L(chk1w)/* when a2==t8, no 32-byte chunk */
  343. nop
  344. C_ST a1,UNIT(0)(a0)
  345. C_ST a1,UNIT(1)(a0)
  346. C_ST a1,UNIT(2)(a0)
  347. C_ST a1,UNIT(3)(a0)
  348. C_ST a1,UNIT(4)(a0)
  349. C_ST a1,UNIT(5)(a0)
  350. C_ST a1,UNIT(6)(a0)
  351. C_ST a1,UNIT(7)(a0)
  352. PTR_ADDIU a0,a0,UNIT(8)
  353. /* Here we have less than 32(64) bytes to set. Set up for a loop to
  354. copy one word (or double word) at a time. Set a2 to count how many
  355. bytes we have to copy after all the word (or double word) chunks are
  356. copied and a3 to the dest pointer after all the (d)word chunks have
  357. been copied. We will loop, incrementing a0 until a0 equals a3. */
  358. L(chk1w):
  359. andi a2,t8,(NSIZE-1) /* a2 is the reminder past one (d)word chunks */
  360. beq a2,t8,L(lastb)
  361. PTR_SUBU a3,t8,a2 /* a3 is count of bytes in one (d)word chunks */
  362. PTR_ADDU a3,a0,a3 /* a3 is the dst address after loop */
  363. /* copying in words (4-byte or 8 byte chunks) */
  364. L(wordCopy_loop):
  365. PTR_ADDIU a0,a0,UNIT(1)
  366. bne a0,a3,L(wordCopy_loop)
  367. C_ST a1,UNIT(-1)(a0)
  368. /* Copy the last 8 (or 16) bytes */
  369. L(lastb):
  370. blez a2,L(leave)
  371. PTR_ADDU a3,a0,a2 /* a3 is the last dst address */
  372. L(lastbloop):
  373. PTR_ADDIU a0,a0,1
  374. bne a0,a3,L(lastbloop)
  375. sb a1,-1(a0)
  376. L(leave):
  377. j ra
  378. nop
  379. .set at
  380. .set reorder
  381. END(MEMSET_NAME)
  382. #ifndef ANDROID_CHANGES
  383. # ifdef _LIBC
  384. # ifdef __UCLIBC__
  385. libc_hidden_def(MEMSET_NAME)
  386. # else
  387. libc_hidden_builtin_def (MEMSET_NAME)
  388. # endif
  389. # endif
  390. #endif