memcmp.S 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (C) 2013 ARM Ltd.
  3. * Copyright (C) 2013 Linaro.
  4. *
  5. * This code is based on glibc cortex strings work originally authored by Linaro
  6. * and re-licensed under GPLv2 for the Linux kernel. The original code can
  7. * be found @
  8. *
  9. * http://bazaar.launchpad.net/~linaro-toolchain-dev/cortex-strings/trunk/
  10. * files/head:/src/aarch64/
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include <linux/linkage.h>
  25. #include <asm/assembler.h>
  26. /*
  27. * compare memory areas(when two memory areas' offset are different,
  28. * alignment handled by the hardware)
  29. *
  30. * Parameters:
  31. * x0 - const memory area 1 pointer
  32. * x1 - const memory area 2 pointer
  33. * x2 - the maximal compare byte length
  34. * Returns:
  35. * x0 - a compare result, maybe less than, equal to, or greater than ZERO
  36. */
  37. /* Parameters and result. */
  38. src1 .req x0
  39. src2 .req x1
  40. limit .req x2
  41. result .req x0
  42. /* Internal variables. */
  43. data1 .req x3
  44. data1w .req w3
  45. data2 .req x4
  46. data2w .req w4
  47. has_nul .req x5
  48. diff .req x6
  49. endloop .req x7
  50. tmp1 .req x8
  51. tmp2 .req x9
  52. tmp3 .req x10
  53. pos .req x11
  54. limit_wd .req x12
  55. mask .req x13
  56. ENTRY(memcmp)
  57. cbz limit, .Lret0
  58. eor tmp1, src1, src2
  59. tst tmp1, #7
  60. b.ne .Lmisaligned8
  61. ands tmp1, src1, #7
  62. b.ne .Lmutual_align
  63. sub limit_wd, limit, #1 /* limit != 0, so no underflow. */
  64. lsr limit_wd, limit_wd, #3 /* Convert to Dwords. */
  65. /*
  66. * The input source addresses are at alignment boundary.
  67. * Directly compare eight bytes each time.
  68. */
  69. .Lloop_aligned:
  70. ldr data1, [src1], #8
  71. ldr data2, [src2], #8
  72. .Lstart_realigned:
  73. subs limit_wd, limit_wd, #1
  74. eor diff, data1, data2 /* Non-zero if differences found. */
  75. csinv endloop, diff, xzr, cs /* Last Dword or differences. */
  76. cbz endloop, .Lloop_aligned
  77. /* Not reached the limit, must have found a diff. */
  78. tbz limit_wd, #63, .Lnot_limit
  79. /* Limit % 8 == 0 => the diff is in the last 8 bytes. */
  80. ands limit, limit, #7
  81. b.eq .Lnot_limit
  82. /*
  83. * The remained bytes less than 8. It is needed to extract valid data
  84. * from last eight bytes of the intended memory range.
  85. */
  86. lsl limit, limit, #3 /* bytes-> bits. */
  87. mov mask, #~0
  88. CPU_BE( lsr mask, mask, limit )
  89. CPU_LE( lsl mask, mask, limit )
  90. bic data1, data1, mask
  91. bic data2, data2, mask
  92. orr diff, diff, mask
  93. b .Lnot_limit
  94. .Lmutual_align:
  95. /*
  96. * Sources are mutually aligned, but are not currently at an
  97. * alignment boundary. Round down the addresses and then mask off
  98. * the bytes that precede the start point.
  99. */
  100. bic src1, src1, #7
  101. bic src2, src2, #7
  102. ldr data1, [src1], #8
  103. ldr data2, [src2], #8
  104. /*
  105. * We can not add limit with alignment offset(tmp1) here. Since the
  106. * addition probably make the limit overflown.
  107. */
  108. sub limit_wd, limit, #1/*limit != 0, so no underflow.*/
  109. and tmp3, limit_wd, #7
  110. lsr limit_wd, limit_wd, #3
  111. add tmp3, tmp3, tmp1
  112. add limit_wd, limit_wd, tmp3, lsr #3
  113. add limit, limit, tmp1/* Adjust the limit for the extra. */
  114. lsl tmp1, tmp1, #3/* Bytes beyond alignment -> bits.*/
  115. neg tmp1, tmp1/* Bits to alignment -64. */
  116. mov tmp2, #~0
  117. /*mask off the non-intended bytes before the start address.*/
  118. CPU_BE( lsl tmp2, tmp2, tmp1 )/*Big-endian.Early bytes are at MSB*/
  119. /* Little-endian. Early bytes are at LSB. */
  120. CPU_LE( lsr tmp2, tmp2, tmp1 )
  121. orr data1, data1, tmp2
  122. orr data2, data2, tmp2
  123. b .Lstart_realigned
  124. /*src1 and src2 have different alignment offset.*/
  125. .Lmisaligned8:
  126. cmp limit, #8
  127. b.lo .Ltiny8proc /*limit < 8: compare byte by byte*/
  128. and tmp1, src1, #7
  129. neg tmp1, tmp1
  130. add tmp1, tmp1, #8/*valid length in the first 8 bytes of src1*/
  131. and tmp2, src2, #7
  132. neg tmp2, tmp2
  133. add tmp2, tmp2, #8/*valid length in the first 8 bytes of src2*/
  134. subs tmp3, tmp1, tmp2
  135. csel pos, tmp1, tmp2, hi /*Choose the maximum.*/
  136. sub limit, limit, pos
  137. /*compare the proceeding bytes in the first 8 byte segment.*/
  138. .Ltinycmp:
  139. ldrb data1w, [src1], #1
  140. ldrb data2w, [src2], #1
  141. subs pos, pos, #1
  142. ccmp data1w, data2w, #0, ne /* NZCV = 0b0000. */
  143. b.eq .Ltinycmp
  144. cbnz pos, 1f /*diff occurred before the last byte.*/
  145. cmp data1w, data2w
  146. b.eq .Lstart_align
  147. 1:
  148. sub result, data1, data2
  149. ret
  150. .Lstart_align:
  151. lsr limit_wd, limit, #3
  152. cbz limit_wd, .Lremain8
  153. ands xzr, src1, #7
  154. b.eq .Lrecal_offset
  155. /*process more leading bytes to make src1 aligned...*/
  156. add src1, src1, tmp3 /*backwards src1 to alignment boundary*/
  157. add src2, src2, tmp3
  158. sub limit, limit, tmp3
  159. lsr limit_wd, limit, #3
  160. cbz limit_wd, .Lremain8
  161. /*load 8 bytes from aligned SRC1..*/
  162. ldr data1, [src1], #8
  163. ldr data2, [src2], #8
  164. subs limit_wd, limit_wd, #1
  165. eor diff, data1, data2 /*Non-zero if differences found.*/
  166. csinv endloop, diff, xzr, ne
  167. cbnz endloop, .Lunequal_proc
  168. /*How far is the current SRC2 from the alignment boundary...*/
  169. and tmp3, tmp3, #7
  170. .Lrecal_offset:/*src1 is aligned now..*/
  171. neg pos, tmp3
  172. .Lloopcmp_proc:
  173. /*
  174. * Divide the eight bytes into two parts. First,backwards the src2
  175. * to an alignment boundary,load eight bytes and compare from
  176. * the SRC2 alignment boundary. If all 8 bytes are equal,then start
  177. * the second part's comparison. Otherwise finish the comparison.
  178. * This special handle can garantee all the accesses are in the
  179. * thread/task space in avoid to overrange access.
  180. */
  181. ldr data1, [src1,pos]
  182. ldr data2, [src2,pos]
  183. eor diff, data1, data2 /* Non-zero if differences found. */
  184. cbnz diff, .Lnot_limit
  185. /*The second part process*/
  186. ldr data1, [src1], #8
  187. ldr data2, [src2], #8
  188. eor diff, data1, data2 /* Non-zero if differences found. */
  189. subs limit_wd, limit_wd, #1
  190. csinv endloop, diff, xzr, ne/*if limit_wd is 0,will finish the cmp*/
  191. cbz endloop, .Lloopcmp_proc
  192. .Lunequal_proc:
  193. cbz diff, .Lremain8
  194. /* There is difference occurred in the latest comparison. */
  195. .Lnot_limit:
  196. /*
  197. * For little endian,reverse the low significant equal bits into MSB,then
  198. * following CLZ can find how many equal bits exist.
  199. */
  200. CPU_LE( rev diff, diff )
  201. CPU_LE( rev data1, data1 )
  202. CPU_LE( rev data2, data2 )
  203. /*
  204. * The MS-non-zero bit of DIFF marks either the first bit
  205. * that is different, or the end of the significant data.
  206. * Shifting left now will bring the critical information into the
  207. * top bits.
  208. */
  209. clz pos, diff
  210. lsl data1, data1, pos
  211. lsl data2, data2, pos
  212. /*
  213. * We need to zero-extend (char is unsigned) the value and then
  214. * perform a signed subtraction.
  215. */
  216. lsr data1, data1, #56
  217. sub result, data1, data2, lsr #56
  218. ret
  219. .Lremain8:
  220. /* Limit % 8 == 0 =>. all data are equal.*/
  221. ands limit, limit, #7
  222. b.eq .Lret0
  223. .Ltiny8proc:
  224. ldrb data1w, [src1], #1
  225. ldrb data2w, [src2], #1
  226. subs limit, limit, #1
  227. ccmp data1w, data2w, #0, ne /* NZCV = 0b0000. */
  228. b.eq .Ltiny8proc
  229. sub result, data1, data2
  230. ret
  231. .Lret0:
  232. mov result, #0
  233. ret
  234. ENDPIPROC(memcmp)