memcmp.S 6.5 KB

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