memcmp_64.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Author: Anton Blanchard <[email protected]>
  4. * Copyright 2015 IBM Corporation.
  5. */
  6. #include <asm/ppc_asm.h>
  7. #include <asm/export.h>
  8. #include <asm/ppc-opcode.h>
  9. #define off8 r6
  10. #define off16 r7
  11. #define off24 r8
  12. #define rA r9
  13. #define rB r10
  14. #define rC r11
  15. #define rD r27
  16. #define rE r28
  17. #define rF r29
  18. #define rG r30
  19. #define rH r31
  20. #ifdef __LITTLE_ENDIAN__
  21. #define LH lhbrx
  22. #define LW lwbrx
  23. #define LD ldbrx
  24. #define LVS lvsr
  25. #define VPERM(_VRT,_VRA,_VRB,_VRC) \
  26. vperm _VRT,_VRB,_VRA,_VRC
  27. #else
  28. #define LH lhzx
  29. #define LW lwzx
  30. #define LD ldx
  31. #define LVS lvsl
  32. #define VPERM(_VRT,_VRA,_VRB,_VRC) \
  33. vperm _VRT,_VRA,_VRB,_VRC
  34. #endif
  35. #define VMX_THRESH 4096
  36. #define ENTER_VMX_OPS \
  37. mflr r0; \
  38. std r3,-STACKFRAMESIZE+STK_REG(R31)(r1); \
  39. std r4,-STACKFRAMESIZE+STK_REG(R30)(r1); \
  40. std r5,-STACKFRAMESIZE+STK_REG(R29)(r1); \
  41. std r0,16(r1); \
  42. stdu r1,-STACKFRAMESIZE(r1); \
  43. bl enter_vmx_ops; \
  44. cmpwi cr1,r3,0; \
  45. ld r0,STACKFRAMESIZE+16(r1); \
  46. ld r3,STK_REG(R31)(r1); \
  47. ld r4,STK_REG(R30)(r1); \
  48. ld r5,STK_REG(R29)(r1); \
  49. addi r1,r1,STACKFRAMESIZE; \
  50. mtlr r0
  51. #define EXIT_VMX_OPS \
  52. mflr r0; \
  53. std r3,-STACKFRAMESIZE+STK_REG(R31)(r1); \
  54. std r4,-STACKFRAMESIZE+STK_REG(R30)(r1); \
  55. std r5,-STACKFRAMESIZE+STK_REG(R29)(r1); \
  56. std r0,16(r1); \
  57. stdu r1,-STACKFRAMESIZE(r1); \
  58. bl exit_vmx_ops; \
  59. ld r0,STACKFRAMESIZE+16(r1); \
  60. ld r3,STK_REG(R31)(r1); \
  61. ld r4,STK_REG(R30)(r1); \
  62. ld r5,STK_REG(R29)(r1); \
  63. addi r1,r1,STACKFRAMESIZE; \
  64. mtlr r0
  65. /*
  66. * LD_VSR_CROSS16B load the 2nd 16 bytes for _vaddr which is unaligned with
  67. * 16 bytes boundary and permute the result with the 1st 16 bytes.
  68. * | y y y y y y y y y y y y y 0 1 2 | 3 4 5 6 7 8 9 a b c d e f z z z |
  69. * ^ ^ ^
  70. * 0xbbbb10 0xbbbb20 0xbbb30
  71. * ^
  72. * _vaddr
  73. *
  74. *
  75. * _vmask is the mask generated by LVS
  76. * _v1st_qw is the 1st aligned QW of current addr which is already loaded.
  77. * for example: 0xyyyyyyyyyyyyy012 for big endian
  78. * _v2nd_qw is the 2nd aligned QW of cur _vaddr to be loaded.
  79. * for example: 0x3456789abcdefzzz for big endian
  80. * The permute result is saved in _v_res.
  81. * for example: 0x0123456789abcdef for big endian.
  82. */
  83. #define LD_VSR_CROSS16B(_vaddr,_vmask,_v1st_qw,_v2nd_qw,_v_res) \
  84. lvx _v2nd_qw,_vaddr,off16; \
  85. VPERM(_v_res,_v1st_qw,_v2nd_qw,_vmask)
  86. /*
  87. * There are 2 categories for memcmp:
  88. * 1) src/dst has the same offset to the 8 bytes boundary. The handlers
  89. * are named like .Lsameoffset_xxxx
  90. * 2) src/dst has different offset to the 8 bytes boundary. The handlers
  91. * are named like .Ldiffoffset_xxxx
  92. */
  93. _GLOBAL_TOC(memcmp)
  94. cmpdi cr1,r5,0
  95. /* Use the short loop if the src/dst addresses are not
  96. * with the same offset of 8 bytes align boundary.
  97. */
  98. xor r6,r3,r4
  99. andi. r6,r6,7
  100. /* Fall back to short loop if compare at aligned addrs
  101. * with less than 8 bytes.
  102. */
  103. cmpdi cr6,r5,7
  104. beq cr1,.Lzero
  105. bgt cr6,.Lno_short
  106. .Lshort:
  107. mtctr r5
  108. 1: lbz rA,0(r3)
  109. lbz rB,0(r4)
  110. subf. rC,rB,rA
  111. bne .Lnon_zero
  112. bdz .Lzero
  113. lbz rA,1(r3)
  114. lbz rB,1(r4)
  115. subf. rC,rB,rA
  116. bne .Lnon_zero
  117. bdz .Lzero
  118. lbz rA,2(r3)
  119. lbz rB,2(r4)
  120. subf. rC,rB,rA
  121. bne .Lnon_zero
  122. bdz .Lzero
  123. lbz rA,3(r3)
  124. lbz rB,3(r4)
  125. subf. rC,rB,rA
  126. bne .Lnon_zero
  127. addi r3,r3,4
  128. addi r4,r4,4
  129. bdnz 1b
  130. .Lzero:
  131. li r3,0
  132. blr
  133. .Lno_short:
  134. dcbt 0,r3
  135. dcbt 0,r4
  136. bne .Ldiffoffset_8bytes_make_align_start
  137. .Lsameoffset_8bytes_make_align_start:
  138. /* attempt to compare bytes not aligned with 8 bytes so that
  139. * rest comparison can run based on 8 bytes alignment.
  140. */
  141. andi. r6,r3,7
  142. /* Try to compare the first double word which is not 8 bytes aligned:
  143. * load the first double word at (src & ~7UL) and shift left appropriate
  144. * bits before comparision.
  145. */
  146. rlwinm r6,r3,3,26,28
  147. beq .Lsameoffset_8bytes_aligned
  148. clrrdi r3,r3,3
  149. clrrdi r4,r4,3
  150. LD rA,0,r3
  151. LD rB,0,r4
  152. sld rA,rA,r6
  153. sld rB,rB,r6
  154. cmpld cr0,rA,rB
  155. srwi r6,r6,3
  156. bne cr0,.LcmpAB_lightweight
  157. subfic r6,r6,8
  158. subf. r5,r6,r5
  159. addi r3,r3,8
  160. addi r4,r4,8
  161. beq .Lzero
  162. .Lsameoffset_8bytes_aligned:
  163. /* now we are aligned with 8 bytes.
  164. * Use .Llong loop if left cmp bytes are equal or greater than 32B.
  165. */
  166. cmpdi cr6,r5,31
  167. bgt cr6,.Llong
  168. .Lcmp_lt32bytes:
  169. /* compare 1 ~ 31 bytes, at least r3 addr is 8 bytes aligned now */
  170. cmpdi cr5,r5,7
  171. srdi r0,r5,3
  172. ble cr5,.Lcmp_rest_lt8bytes
  173. /* handle 8 ~ 31 bytes */
  174. clrldi r5,r5,61
  175. mtctr r0
  176. 2:
  177. LD rA,0,r3
  178. LD rB,0,r4
  179. cmpld cr0,rA,rB
  180. addi r3,r3,8
  181. addi r4,r4,8
  182. bne cr0,.LcmpAB_lightweight
  183. bdnz 2b
  184. cmpwi r5,0
  185. beq .Lzero
  186. .Lcmp_rest_lt8bytes:
  187. /*
  188. * Here we have less than 8 bytes to compare. At least s1 is aligned to
  189. * 8 bytes, but s2 may not be. We must make sure s2 + 7 doesn't cross a
  190. * page boundary, otherwise we might read past the end of the buffer and
  191. * trigger a page fault. We use 4K as the conservative minimum page
  192. * size. If we detect that case we go to the byte-by-byte loop.
  193. *
  194. * Otherwise the next double word is loaded from s1 and s2, and shifted
  195. * right to compare the appropriate bits.
  196. */
  197. clrldi r6,r4,(64-12) // r6 = r4 & 0xfff
  198. cmpdi r6,0xff8
  199. bgt .Lshort
  200. subfic r6,r5,8
  201. slwi r6,r6,3
  202. LD rA,0,r3
  203. LD rB,0,r4
  204. srd rA,rA,r6
  205. srd rB,rB,r6
  206. cmpld cr0,rA,rB
  207. bne cr0,.LcmpAB_lightweight
  208. b .Lzero
  209. .Lnon_zero:
  210. mr r3,rC
  211. blr
  212. .Llong:
  213. #ifdef CONFIG_ALTIVEC
  214. BEGIN_FTR_SECTION
  215. /* Try to use vmx loop if length is equal or greater than 4K */
  216. cmpldi cr6,r5,VMX_THRESH
  217. bge cr6,.Lsameoffset_vmx_cmp
  218. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  219. .Llong_novmx_cmp:
  220. #endif
  221. /* At least s1 addr is aligned with 8 bytes */
  222. li off8,8
  223. li off16,16
  224. li off24,24
  225. std r31,-8(r1)
  226. std r30,-16(r1)
  227. std r29,-24(r1)
  228. std r28,-32(r1)
  229. std r27,-40(r1)
  230. srdi r0,r5,5
  231. mtctr r0
  232. andi. r5,r5,31
  233. LD rA,0,r3
  234. LD rB,0,r4
  235. LD rC,off8,r3
  236. LD rD,off8,r4
  237. LD rE,off16,r3
  238. LD rF,off16,r4
  239. LD rG,off24,r3
  240. LD rH,off24,r4
  241. cmpld cr0,rA,rB
  242. addi r3,r3,32
  243. addi r4,r4,32
  244. bdz .Lfirst32
  245. LD rA,0,r3
  246. LD rB,0,r4
  247. cmpld cr1,rC,rD
  248. LD rC,off8,r3
  249. LD rD,off8,r4
  250. cmpld cr6,rE,rF
  251. LD rE,off16,r3
  252. LD rF,off16,r4
  253. cmpld cr7,rG,rH
  254. bne cr0,.LcmpAB
  255. LD rG,off24,r3
  256. LD rH,off24,r4
  257. cmpld cr0,rA,rB
  258. bne cr1,.LcmpCD
  259. addi r3,r3,32
  260. addi r4,r4,32
  261. bdz .Lsecond32
  262. .balign 16
  263. 1: LD rA,0,r3
  264. LD rB,0,r4
  265. cmpld cr1,rC,rD
  266. bne cr6,.LcmpEF
  267. LD rC,off8,r3
  268. LD rD,off8,r4
  269. cmpld cr6,rE,rF
  270. bne cr7,.LcmpGH
  271. LD rE,off16,r3
  272. LD rF,off16,r4
  273. cmpld cr7,rG,rH
  274. bne cr0,.LcmpAB
  275. LD rG,off24,r3
  276. LD rH,off24,r4
  277. cmpld cr0,rA,rB
  278. bne cr1,.LcmpCD
  279. addi r3,r3,32
  280. addi r4,r4,32
  281. bdnz 1b
  282. .Lsecond32:
  283. cmpld cr1,rC,rD
  284. bne cr6,.LcmpEF
  285. cmpld cr6,rE,rF
  286. bne cr7,.LcmpGH
  287. cmpld cr7,rG,rH
  288. bne cr0,.LcmpAB
  289. bne cr1,.LcmpCD
  290. bne cr6,.LcmpEF
  291. bne cr7,.LcmpGH
  292. .Ltail:
  293. ld r31,-8(r1)
  294. ld r30,-16(r1)
  295. ld r29,-24(r1)
  296. ld r28,-32(r1)
  297. ld r27,-40(r1)
  298. cmpdi r5,0
  299. beq .Lzero
  300. b .Lshort
  301. .Lfirst32:
  302. cmpld cr1,rC,rD
  303. cmpld cr6,rE,rF
  304. cmpld cr7,rG,rH
  305. bne cr0,.LcmpAB
  306. bne cr1,.LcmpCD
  307. bne cr6,.LcmpEF
  308. bne cr7,.LcmpGH
  309. b .Ltail
  310. .LcmpAB:
  311. li r3,1
  312. bgt cr0,.Lout
  313. li r3,-1
  314. b .Lout
  315. .LcmpCD:
  316. li r3,1
  317. bgt cr1,.Lout
  318. li r3,-1
  319. b .Lout
  320. .LcmpEF:
  321. li r3,1
  322. bgt cr6,.Lout
  323. li r3,-1
  324. b .Lout
  325. .LcmpGH:
  326. li r3,1
  327. bgt cr7,.Lout
  328. li r3,-1
  329. .Lout:
  330. ld r31,-8(r1)
  331. ld r30,-16(r1)
  332. ld r29,-24(r1)
  333. ld r28,-32(r1)
  334. ld r27,-40(r1)
  335. blr
  336. .LcmpAB_lightweight: /* skip NV GPRS restore */
  337. li r3,1
  338. bgtlr
  339. li r3,-1
  340. blr
  341. #ifdef CONFIG_ALTIVEC
  342. .Lsameoffset_vmx_cmp:
  343. /* Enter with src/dst addrs has the same offset with 8 bytes
  344. * align boundary.
  345. *
  346. * There is an optimization based on following fact: memcmp()
  347. * prones to fail early at the first 32 bytes.
  348. * Before applying VMX instructions which will lead to 32x128bits
  349. * VMX regs load/restore penalty, we compare the first 32 bytes
  350. * so that we can catch the ~80% fail cases.
  351. */
  352. li r0,4
  353. mtctr r0
  354. .Lsameoffset_prechk_32B_loop:
  355. LD rA,0,r3
  356. LD rB,0,r4
  357. cmpld cr0,rA,rB
  358. addi r3,r3,8
  359. addi r4,r4,8
  360. bne cr0,.LcmpAB_lightweight
  361. addi r5,r5,-8
  362. bdnz .Lsameoffset_prechk_32B_loop
  363. ENTER_VMX_OPS
  364. beq cr1,.Llong_novmx_cmp
  365. 3:
  366. /* need to check whether r4 has the same offset with r3
  367. * for 16 bytes boundary.
  368. */
  369. xor r0,r3,r4
  370. andi. r0,r0,0xf
  371. bne .Ldiffoffset_vmx_cmp_start
  372. /* len is no less than 4KB. Need to align with 16 bytes further.
  373. */
  374. andi. rA,r3,8
  375. LD rA,0,r3
  376. beq 4f
  377. LD rB,0,r4
  378. cmpld cr0,rA,rB
  379. addi r3,r3,8
  380. addi r4,r4,8
  381. addi r5,r5,-8
  382. beq cr0,4f
  383. /* save and restore cr0 */
  384. mfocrf r5,128
  385. EXIT_VMX_OPS
  386. mtocrf 128,r5
  387. b .LcmpAB_lightweight
  388. 4:
  389. /* compare 32 bytes for each loop */
  390. srdi r0,r5,5
  391. mtctr r0
  392. clrldi r5,r5,59
  393. li off16,16
  394. .balign 16
  395. 5:
  396. lvx v0,0,r3
  397. lvx v1,0,r4
  398. VCMPEQUD_RC(v0,v0,v1)
  399. bnl cr6,7f
  400. lvx v0,off16,r3
  401. lvx v1,off16,r4
  402. VCMPEQUD_RC(v0,v0,v1)
  403. bnl cr6,6f
  404. addi r3,r3,32
  405. addi r4,r4,32
  406. bdnz 5b
  407. EXIT_VMX_OPS
  408. cmpdi r5,0
  409. beq .Lzero
  410. b .Lcmp_lt32bytes
  411. 6:
  412. addi r3,r3,16
  413. addi r4,r4,16
  414. 7:
  415. /* diff the last 16 bytes */
  416. EXIT_VMX_OPS
  417. LD rA,0,r3
  418. LD rB,0,r4
  419. cmpld cr0,rA,rB
  420. li off8,8
  421. bne cr0,.LcmpAB_lightweight
  422. LD rA,off8,r3
  423. LD rB,off8,r4
  424. cmpld cr0,rA,rB
  425. bne cr0,.LcmpAB_lightweight
  426. b .Lzero
  427. #endif
  428. .Ldiffoffset_8bytes_make_align_start:
  429. /* now try to align s1 with 8 bytes */
  430. rlwinm r6,r3,3,26,28
  431. beq .Ldiffoffset_align_s1_8bytes
  432. clrrdi r3,r3,3
  433. LD rA,0,r3
  434. LD rB,0,r4 /* unaligned load */
  435. sld rA,rA,r6
  436. srd rA,rA,r6
  437. srd rB,rB,r6
  438. cmpld cr0,rA,rB
  439. srwi r6,r6,3
  440. bne cr0,.LcmpAB_lightweight
  441. subfic r6,r6,8
  442. subf. r5,r6,r5
  443. addi r3,r3,8
  444. add r4,r4,r6
  445. beq .Lzero
  446. .Ldiffoffset_align_s1_8bytes:
  447. /* now s1 is aligned with 8 bytes. */
  448. #ifdef CONFIG_ALTIVEC
  449. BEGIN_FTR_SECTION
  450. /* only do vmx ops when the size equal or greater than 4K bytes */
  451. cmpdi cr5,r5,VMX_THRESH
  452. bge cr5,.Ldiffoffset_vmx_cmp
  453. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  454. .Ldiffoffset_novmx_cmp:
  455. #endif
  456. cmpdi cr5,r5,31
  457. ble cr5,.Lcmp_lt32bytes
  458. #ifdef CONFIG_ALTIVEC
  459. b .Llong_novmx_cmp
  460. #else
  461. b .Llong
  462. #endif
  463. #ifdef CONFIG_ALTIVEC
  464. .Ldiffoffset_vmx_cmp:
  465. /* perform a 32 bytes pre-checking before
  466. * enable VMX operations.
  467. */
  468. li r0,4
  469. mtctr r0
  470. .Ldiffoffset_prechk_32B_loop:
  471. LD rA,0,r3
  472. LD rB,0,r4
  473. cmpld cr0,rA,rB
  474. addi r3,r3,8
  475. addi r4,r4,8
  476. bne cr0,.LcmpAB_lightweight
  477. addi r5,r5,-8
  478. bdnz .Ldiffoffset_prechk_32B_loop
  479. ENTER_VMX_OPS
  480. beq cr1,.Ldiffoffset_novmx_cmp
  481. .Ldiffoffset_vmx_cmp_start:
  482. /* Firstly try to align r3 with 16 bytes */
  483. andi. r6,r3,0xf
  484. li off16,16
  485. beq .Ldiffoffset_vmx_s1_16bytes_align
  486. LVS v3,0,r3
  487. LVS v4,0,r4
  488. lvx v5,0,r3
  489. lvx v6,0,r4
  490. LD_VSR_CROSS16B(r3,v3,v5,v7,v9)
  491. LD_VSR_CROSS16B(r4,v4,v6,v8,v10)
  492. VCMPEQUB_RC(v7,v9,v10)
  493. bnl cr6,.Ldiffoffset_vmx_diff_found
  494. subfic r6,r6,16
  495. subf r5,r6,r5
  496. add r3,r3,r6
  497. add r4,r4,r6
  498. .Ldiffoffset_vmx_s1_16bytes_align:
  499. /* now s1 is aligned with 16 bytes */
  500. lvx v6,0,r4
  501. LVS v4,0,r4
  502. srdi r6,r5,5 /* loop for 32 bytes each */
  503. clrldi r5,r5,59
  504. mtctr r6
  505. .balign 16
  506. .Ldiffoffset_vmx_32bytesloop:
  507. /* the first qw of r4 was saved in v6 */
  508. lvx v9,0,r3
  509. LD_VSR_CROSS16B(r4,v4,v6,v8,v10)
  510. VCMPEQUB_RC(v7,v9,v10)
  511. vor v6,v8,v8
  512. bnl cr6,.Ldiffoffset_vmx_diff_found
  513. addi r3,r3,16
  514. addi r4,r4,16
  515. lvx v9,0,r3
  516. LD_VSR_CROSS16B(r4,v4,v6,v8,v10)
  517. VCMPEQUB_RC(v7,v9,v10)
  518. vor v6,v8,v8
  519. bnl cr6,.Ldiffoffset_vmx_diff_found
  520. addi r3,r3,16
  521. addi r4,r4,16
  522. bdnz .Ldiffoffset_vmx_32bytesloop
  523. EXIT_VMX_OPS
  524. cmpdi r5,0
  525. beq .Lzero
  526. b .Lcmp_lt32bytes
  527. .Ldiffoffset_vmx_diff_found:
  528. EXIT_VMX_OPS
  529. /* anyway, the diff will appear in next 16 bytes */
  530. li r5,16
  531. b .Lcmp_lt32bytes
  532. #endif
  533. EXPORT_SYMBOL(memcmp)