sha256_xmm_amd64.asm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. ;/*
  2. ; * Copyright (C) 2011 - Neil Kettle <neil@digit-labs.org>
  3. ; *
  4. ; * This file is part of cpuminer-ng.
  5. ; *
  6. ; * cpuminer-ng is free software: you can redistribute it and/or modify
  7. ; * it under the terms of the GNU General Public License as published by
  8. ; * the Free Software Foundation, either version 3 of the License, or
  9. ; * (at your option) any later version.
  10. ; *
  11. ; * cpuminer-ng is distributed in the hope that it will be useful,
  12. ; * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ; * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ; * GNU General Public License for more details.
  15. ; *
  16. ; * You should have received a copy of the GNU General Public License
  17. ; * along with cpuminer-ng. If not, see <http://www.gnu.org/licenses/>.
  18. ; */
  19. ; %rbp, %rbx, and %r12-%r15 - callee save
  20. ALIGN 32
  21. BITS 64
  22. %define hash rdi
  23. %define hash1 rsi
  24. %define data rdx
  25. %define init rcx
  26. ; 0 = (1024 - 256) (mod (LAB_CALC_UNROLL*LAB_CALC_PARA*16))
  27. %define SHA_CALC_W_PARA 2
  28. %define SHA_CALC_W_UNROLL 8
  29. %define SHA_ROUND_LOOP_UNROLL 16
  30. %ifidn __YASM_OBJFMT__, macho64
  31. extern _sha256_consts_m128i
  32. extern _sha256_init
  33. %else
  34. extern sha256_consts_m128i
  35. extern sha256_init
  36. %endif
  37. %ifidn __YASM_OBJFMT__, macho64
  38. global _sha256_sse2_64_new
  39. %else
  40. global sha256_sse2_64_new
  41. %endif
  42. %define sr1 xmm6
  43. %define sr2 xmm1
  44. %define sr3 xmm2
  45. %define sr4 xmm13
  46. %define rA xmm7
  47. %define rB xmm5
  48. %define rC xmm4
  49. %define rD xmm3
  50. %define rE xmm0
  51. %define rF xmm8
  52. %define rG xmm9
  53. %define rH xmm10
  54. %macro sha_round_blk 0
  55. movdqa sr1, [data+rax] ; T1 = w;
  56. ;movdqa sr1, xmm11
  57. movdqa sr2, rE ; sr2 = rE
  58. pandn sr2, rG ; sr2 = ~rE & rG
  59. movdqa sr3, rF ; sr3 = rF
  60. paddd sr1, rH ; T1 = h + sha256_consts_m128i[i] + w;
  61. movdqa rH, rG ; rH = rG
  62. pand sr3, rE ; sr3 = rE & rF
  63. movdqa rG, rF ; rG = rF
  64. %ifidn __YASM_OBJFMT__, macho64
  65. paddd sr1, [rcx+rax]
  66. %else
  67. paddd sr1, sha256_consts_m128i[rax] ; T1 = sha256_consts_m128i[i] + w;
  68. %endif
  69. pxor sr2, sr3 ; sr2 = (rE & rF) ^ (~rE & rG) = Ch (e, f, g)
  70. movdqa rF, rE ; rF = rE
  71. paddd sr1, sr2 ; T1 = h + Ch (e, f, g) + sha256_consts_m128i[i] + w;
  72. movdqa sr2, rE ; sr2 = rE
  73. psrld rE, 6 ; e >> 6
  74. movdqa sr3, rE ; e >> 6
  75. pslld sr2, 7 ; e << 7
  76. psrld sr3, 5 ; e >> 11
  77. pxor rE, sr2 ; e >> 6 ^ e << 7
  78. pslld sr2, 14 ; e << 21
  79. pxor rE, sr3 ; e >> 6 ^ e << 7 ^ e >> 11
  80. psrld sr3, 14 ; e >> 25
  81. pxor rE, sr2 ; e >> 6 ^ e << 7 ^ e >> 11 ^ e << 21
  82. pslld sr2, 5 ; e << 26
  83. pxor rE, sr3 ; e >> 6 ^ e << 7 ^ e >> 11 ^ e << 21 ^ e >> 25
  84. pxor rE, sr2 ; e >> 6 ^ e << 7 ^ e >> 11 ^ e << 21 ^ e >> 25 ^ e << 26
  85. movdqa sr2, rB ; sr2 = rB
  86. paddd sr1, rE ; sr1 = h + BIGSIGMA1_256(e) + Ch (e, f, g) + sha256_consts_m128i[i] + w;
  87. movdqa rE, rD ; rE = rD
  88. movdqa rD, rC ; rD = rC
  89. paddd rE, sr1 ; rE = rD + T1
  90. movdqa sr3, rC ; sr3 = rC
  91. pand rC, rA ; rC = rC & rA
  92. pand sr3, rB ; sr3 = rB & rC
  93. pand sr2, rA ; sr2 = rB & rA
  94. pxor sr2, rC ; sr2 = (rB & rA) ^ (rC & rA)
  95. movdqa rC, rB ; rC = rB
  96. pxor sr2, sr3 ; sr2 = (rB & rA) ^ (rC & rA) ^ (rB & rC)
  97. movdqa rB, rA ; rB = rA
  98. paddd sr1, sr2 ; sr1 = T1 + (rB & rA) ^ (rC & rA) ^ (rB & rC)
  99. lea rax, [rax+16]
  100. movdqa sr3, rA ; sr3 = rA
  101. psrld rA, 2 ; a >> 2
  102. pslld sr3, 10 ; a << 10
  103. movdqa sr2, rA ; a >> 2
  104. pxor rA, sr3 ; a >> 2 ^ a << 10
  105. psrld sr2, 11 ; a >> 13
  106. pxor rA, sr2 ; a >> 2 ^ a << 10 ^ a >> 13
  107. pslld sr3, 9 ; a << 19
  108. pxor rA, sr3 ; a >> 2 ^ a << 10 ^ a >> 13 ^ a << 19
  109. psrld sr2, 9 ; a >> 21
  110. pxor rA, sr2 ; a >> 2 ^ a << 10 ^ a >> 13 ^ a << 19 ^ a >> 21
  111. pslld sr3, 11 ; a << 30
  112. pxor rA, sr3 ; a >> 2 ^ a << 10 ^ a >> 13 ^ a << 19 ^ a >> 21 ^ a << 30
  113. paddd rA, sr1 ; T1 + BIGSIGMA0_256(a) + Maj(a, b, c);
  114. %endmacro
  115. %macro sha_calc_w_blk 1
  116. movdqa xmm0, [r11-(15-%1)*16] ; xmm0 = W[I-15]
  117. movdqa xmm4, [r11-(15-(%1+1))*16] ; xmm4 = W[I-15+1]
  118. movdqa xmm2, xmm0 ; xmm2 = W[I-15]
  119. movdqa xmm6, xmm4 ; xmm6 = W[I-15+1]
  120. psrld xmm0, 3 ; xmm0 = W[I-15] >> 3
  121. psrld xmm4, 3 ; xmm4 = W[I-15+1] >> 3
  122. movdqa xmm1, xmm0 ; xmm1 = W[I-15] >> 3
  123. movdqa xmm5, xmm4 ; xmm5 = W[I-15+1] >> 3
  124. pslld xmm2, 14 ; xmm2 = W[I-15] << 14
  125. pslld xmm6, 14 ; xmm6 = W[I-15+1] << 14
  126. psrld xmm1, 4 ; xmm1 = W[I-15] >> 7
  127. psrld xmm5, 4 ; xmm5 = W[I-15+1] >> 7
  128. pxor xmm0, xmm1 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7)
  129. pxor xmm4, xmm5 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7)
  130. psrld xmm1, 11 ; xmm1 = W[I-15] >> 18
  131. psrld xmm5, 11 ; xmm5 = W[I-15+1] >> 18
  132. pxor xmm0, xmm2 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14)
  133. pxor xmm4, xmm6 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7) ^ (W[I-15+1] << 14)
  134. pslld xmm2, 11 ; xmm2 = W[I-15] << 25
  135. pslld xmm6, 11 ; xmm6 = W[I-15+1] << 25
  136. pxor xmm0, xmm1 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14) ^ (W[I-15] >> 18)
  137. pxor xmm4, xmm5 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7) ^ (W[I-15+1] << 14) ^ (W[I-15+1] >> 18)
  138. pxor xmm0, xmm2 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14) ^ (W[I-15] >> 18) ^ (W[I-15] << 25)
  139. pxor xmm4, xmm6 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7) ^ (W[I-15+1] << 14) ^ (W[I-15+1] >> 18) ^ (W[I-15+1] << 25)
  140. movdqa xmm3, [r11-(2-%1)*16] ; xmm3 = W[I-2]
  141. movdqa xmm7, [r11-(2-(%1+1))*16] ; xmm7 = W[I-2+1]
  142. paddd xmm0, [r11-(16-%1)*16] ; xmm0 = s0(W[I-15]) + W[I-16]
  143. paddd xmm4, [r11-(16-(%1+1))*16] ; xmm4 = s0(W[I-15+1]) + W[I-16+1]
  144. ;;;;;;;;;;;;;;;;;;
  145. movdqa xmm2, xmm3 ; xmm2 = W[I-2]
  146. movdqa xmm6, xmm7 ; xmm6 = W[I-2+1]
  147. psrld xmm3, 10 ; xmm3 = W[I-2] >> 10
  148. psrld xmm7, 10 ; xmm7 = W[I-2+1] >> 10
  149. movdqa xmm1, xmm3 ; xmm1 = W[I-2] >> 10
  150. movdqa xmm5, xmm7 ; xmm5 = W[I-2+1] >> 10
  151. paddd xmm0, [r11-(7-%1)*16] ; xmm0 = s0(W[I-15]) + W[I-16] + W[I-7]
  152. pslld xmm2, 13 ; xmm2 = W[I-2] << 13
  153. pslld xmm6, 13 ; xmm6 = W[I-2+1] << 13
  154. psrld xmm1, 7 ; xmm1 = W[I-2] >> 17
  155. psrld xmm5, 7 ; xmm5 = W[I-2+1] >> 17
  156. paddd xmm4, [r11-(7-(%1+1))*16] ; xmm4 = s0(W[I-15+1]) + W[I-16+1] + W[I-7+1]
  157. pxor xmm3, xmm1 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17)
  158. pxor xmm7, xmm5 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17)
  159. psrld xmm1, 2 ; xmm1 = W[I-2] >> 19
  160. psrld xmm5, 2 ; xmm5 = W[I-2+1] >> 19
  161. pxor xmm3, xmm2 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13)
  162. pxor xmm7, xmm6 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17) ^ (W[I-2+1] << 13)
  163. pslld xmm2, 2 ; xmm2 = W[I-2] << 15
  164. pslld xmm6, 2 ; xmm6 = W[I-2+1] << 15
  165. pxor xmm3, xmm1 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13) ^ (W[I-2] >> 19)
  166. pxor xmm7, xmm5 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17) ^ (W[I-2+1] << 13) ^ (W[I-2+1] >> 19)
  167. pxor xmm3, xmm2 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13) ^ (W[I-2] >> 19) ^ (W[I-2] << 15)
  168. pxor xmm7, xmm6 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17) ^ (W[I-2+1] << 13) ^ (W[I-2+1] >> 19) ^ (W[I-2+1] << 15)
  169. paddd xmm0, xmm3 ; xmm0 = s0(W[I-15]) + W[I-16] + s1(W[I-2]) + W[I-7]
  170. paddd xmm4, xmm7 ; xmm4 = s0(W[I-15+1]) + W[I-16+1] + s1(W[I-2+1]) + W[I-7+1]
  171. movdqa [r11+(%1*16)], xmm0
  172. movdqa [r11+((%1+1)*16)], xmm4
  173. %endmacro
  174. ; _sha256_sse2_64_new hash(rdi), hash1(rsi), data(rdx), init(rcx),
  175. %ifidn __YASM_OBJFMT__, macho64
  176. _sha256_sse2_64_new:
  177. %else
  178. sha256_sse2_64_new:
  179. %endif
  180. push rbx
  181. %macro SHA_256 0
  182. mov rbx, 64*4 ; rbx is # of SHA-2 rounds
  183. mov rax, 16*4 ; rax is where we expand to
  184. push rbx
  185. lea rbx, qword [data+rbx*4]
  186. lea r11, qword [data+rax*4]
  187. %%SHA_CALC_W:
  188. %assign i 0
  189. %rep SHA_CALC_W_UNROLL
  190. sha_calc_w_blk i
  191. %assign i i+SHA_CALC_W_PARA
  192. %endrep
  193. add r11, SHA_CALC_W_UNROLL*SHA_CALC_W_PARA*16
  194. cmp r11, rbx
  195. jb %%SHA_CALC_W
  196. pop rbx
  197. mov rax, 0
  198. lea rbx, [rbx*4]
  199. movdqa rA, [init]
  200. pshufd rB, rA, 0x55 ; rB == B
  201. pshufd rC, rA, 0xAA ; rC == C
  202. pshufd rD, rA, 0xFF ; rD == D
  203. pshufd rA, rA, 0 ; rA == A
  204. movdqa rE, [init+4*4]
  205. pshufd rF, rE, 0x55 ; rF == F
  206. pshufd rG, rE, 0xAA ; rG == G
  207. pshufd rH, rE, 0xFF ; rH == H
  208. pshufd rE, rE, 0 ; rE == E
  209. %ifidn __YASM_OBJFMT__, macho64
  210. lea rcx, [_sha256_consts_m128i wrt rip]
  211. %endif
  212. %%SHAROUND_LOOP:
  213. %assign i 0
  214. %rep SHA_ROUND_LOOP_UNROLL
  215. sha_round_blk
  216. %assign i i+1
  217. %endrep
  218. cmp rax, rbx
  219. jb %%SHAROUND_LOOP
  220. ; Finished the 64 rounds, calculate hash and save
  221. movdqa sr1, [init]
  222. pshufd sr2, sr1, 0x55
  223. pshufd sr3, sr1, 0xAA
  224. pshufd sr4, sr1, 0xFF
  225. pshufd sr1, sr1, 0
  226. paddd rB, sr2
  227. paddd rC, sr3
  228. paddd rD, sr4
  229. paddd rA, sr1
  230. movdqa sr1, [init+4*4]
  231. pshufd sr2, sr1, 0x55
  232. pshufd sr3, sr1, 0xAA
  233. pshufd sr4, sr1, 0xFF
  234. pshufd sr1, sr1, 0
  235. paddd rF, sr2
  236. paddd rG, sr3
  237. paddd rH, sr4
  238. paddd rE, sr1
  239. %endmacro
  240. SHA_256
  241. movdqa [hash1+0*16], rA
  242. movdqa [hash1+1*16], rB
  243. movdqa [hash1+2*16], rC
  244. movdqa [hash1+3*16], rD
  245. movdqa [hash1+4*16], rE
  246. movdqa [hash1+5*16], rF
  247. movdqa [hash1+6*16], rG
  248. movdqa [hash1+7*16], rH
  249. mov data, hash1
  250. mov init, sha256_init
  251. SHA_256
  252. movdqa [hash+7*16], rH
  253. LAB_RET:
  254. pop rbx
  255. ret
  256. %ifidn __OUTPUT_FORMAT__,elf
  257. section .note.GNU-stack noalloc noexec nowrite progbits
  258. %endif
  259. %ifidn __OUTPUT_FORMAT__,elf64
  260. section .note.GNU-stack noalloc noexec nowrite progbits
  261. %endif