xxhash_arm64.s 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //go:build !appengine && gc && !purego
  2. // +build !appengine
  3. // +build gc
  4. // +build !purego
  5. #include "textflag.h"
  6. // Registers:
  7. #define digest R1
  8. #define h R2 // return value
  9. #define p R3 // input pointer
  10. #define n R4 // input length
  11. #define nblocks R5 // n / 32
  12. #define prime1 R7
  13. #define prime2 R8
  14. #define prime3 R9
  15. #define prime4 R10
  16. #define prime5 R11
  17. #define v1 R12
  18. #define v2 R13
  19. #define v3 R14
  20. #define v4 R15
  21. #define x1 R20
  22. #define x2 R21
  23. #define x3 R22
  24. #define x4 R23
  25. #define round(acc, x) \
  26. MADD prime2, acc, x, acc \
  27. ROR $64-31, acc \
  28. MUL prime1, acc
  29. // round0 performs the operation x = round(0, x).
  30. #define round0(x) \
  31. MUL prime2, x \
  32. ROR $64-31, x \
  33. MUL prime1, x
  34. #define mergeRound(acc, x) \
  35. round0(x) \
  36. EOR x, acc \
  37. MADD acc, prime4, prime1, acc
  38. // blockLoop processes as many 32-byte blocks as possible,
  39. // updating v1, v2, v3, and v4. It assumes that n >= 32.
  40. #define blockLoop() \
  41. LSR $5, n, nblocks \
  42. PCALIGN $16 \
  43. loop: \
  44. LDP.P 16(p), (x1, x2) \
  45. LDP.P 16(p), (x3, x4) \
  46. round(v1, x1) \
  47. round(v2, x2) \
  48. round(v3, x3) \
  49. round(v4, x4) \
  50. SUB $1, nblocks \
  51. CBNZ nblocks, loop
  52. // func Sum64(b []byte) uint64
  53. TEXT ·Sum64(SB), NOSPLIT|NOFRAME, $0-32
  54. LDP b_base+0(FP), (p, n)
  55. LDP ·primes+0(SB), (prime1, prime2)
  56. LDP ·primes+16(SB), (prime3, prime4)
  57. MOVD ·primes+32(SB), prime5
  58. CMP $32, n
  59. CSEL LT, prime5, ZR, h // if n < 32 { h = prime5 } else { h = 0 }
  60. BLT afterLoop
  61. ADD prime1, prime2, v1
  62. MOVD prime2, v2
  63. MOVD $0, v3
  64. NEG prime1, v4
  65. blockLoop()
  66. ROR $64-1, v1, x1
  67. ROR $64-7, v2, x2
  68. ADD x1, x2
  69. ROR $64-12, v3, x3
  70. ROR $64-18, v4, x4
  71. ADD x3, x4
  72. ADD x2, x4, h
  73. mergeRound(h, v1)
  74. mergeRound(h, v2)
  75. mergeRound(h, v3)
  76. mergeRound(h, v4)
  77. afterLoop:
  78. ADD n, h
  79. TBZ $4, n, try8
  80. LDP.P 16(p), (x1, x2)
  81. round0(x1)
  82. // NOTE: here and below, sequencing the EOR after the ROR (using a
  83. // rotated register) is worth a small but measurable speedup for small
  84. // inputs.
  85. ROR $64-27, h
  86. EOR x1 @> 64-27, h, h
  87. MADD h, prime4, prime1, h
  88. round0(x2)
  89. ROR $64-27, h
  90. EOR x2 @> 64-27, h, h
  91. MADD h, prime4, prime1, h
  92. try8:
  93. TBZ $3, n, try4
  94. MOVD.P 8(p), x1
  95. round0(x1)
  96. ROR $64-27, h
  97. EOR x1 @> 64-27, h, h
  98. MADD h, prime4, prime1, h
  99. try4:
  100. TBZ $2, n, try2
  101. MOVWU.P 4(p), x2
  102. MUL prime1, x2
  103. ROR $64-23, h
  104. EOR x2 @> 64-23, h, h
  105. MADD h, prime3, prime2, h
  106. try2:
  107. TBZ $1, n, try1
  108. MOVHU.P 2(p), x3
  109. AND $255, x3, x1
  110. LSR $8, x3, x2
  111. MUL prime5, x1
  112. ROR $64-11, h
  113. EOR x1 @> 64-11, h, h
  114. MUL prime1, h
  115. MUL prime5, x2
  116. ROR $64-11, h
  117. EOR x2 @> 64-11, h, h
  118. MUL prime1, h
  119. try1:
  120. TBZ $0, n, finalize
  121. MOVBU (p), x4
  122. MUL prime5, x4
  123. ROR $64-11, h
  124. EOR x4 @> 64-11, h, h
  125. MUL prime1, h
  126. finalize:
  127. EOR h >> 33, h
  128. MUL prime2, h
  129. EOR h >> 29, h
  130. MUL prime3, h
  131. EOR h >> 32, h
  132. MOVD h, ret+24(FP)
  133. RET
  134. // func writeBlocks(d *Digest, b []byte) int
  135. TEXT ·writeBlocks(SB), NOSPLIT|NOFRAME, $0-40
  136. LDP ·primes+0(SB), (prime1, prime2)
  137. // Load state. Assume v[1-4] are stored contiguously.
  138. MOVD d+0(FP), digest
  139. LDP 0(digest), (v1, v2)
  140. LDP 16(digest), (v3, v4)
  141. LDP b_base+8(FP), (p, n)
  142. blockLoop()
  143. // Store updated state.
  144. STP (v1, v2), 0(digest)
  145. STP (v3, v4), 16(digest)
  146. BIC $31, n
  147. MOVD n, ret+32(FP)
  148. RET