sha256_xmm_amd64.asm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. %ifidn __OUTPUT_FORMAT__,win64
  23. %define hash rcx
  24. %define hash1 rdx
  25. %define data r8
  26. %define init r9
  27. %else
  28. %define hash rdi
  29. %define hash1 rsi
  30. %define data rdx
  31. %define init rcx
  32. %endif
  33. ; 0 = (1024 - 256) (mod (LAB_CALC_UNROLL*LAB_CALC_PARA*16))
  34. %define SHA_CALC_W_PARA 2
  35. %define SHA_CALC_W_UNROLL 8
  36. %define SHA_ROUND_LOOP_UNROLL 16
  37. extern sha256_consts_m128i
  38. extern sha256_init
  39. global sha256_sse2_64_new
  40. %define sr1 xmm6
  41. %define sr2 xmm1
  42. %define sr3 xmm2
  43. %define sr4 xmm13
  44. %define rA xmm7
  45. %define rB xmm5
  46. %define rC xmm4
  47. %define rD xmm3
  48. %define rE xmm0
  49. %define rF xmm8
  50. %define rG xmm9
  51. %define rH xmm10
  52. %macro sha_round_blk 0
  53. movdqa sr1, [data+rax] ; T1 = w;
  54. ;movdqa sr1, xmm11
  55. movdqa sr2, rE ; sr2 = rE
  56. pandn sr2, rG ; sr2 = ~rE & rG
  57. movdqa sr3, rF ; sr3 = rF
  58. paddd sr1, rH ; T1 = h + sha256_consts_m128i[i] + w;
  59. movdqa rH, rG ; rH = rG
  60. pand sr3, rE ; sr3 = rE & rF
  61. movdqa rG, rF ; rG = rF
  62. %ifidn __YASM_OBJFMT__, macho64
  63. paddd sr1, [rcx+rax]
  64. %else
  65. paddd sr1, sha256_consts_m128i[rax] ; T1 = sha256_consts_m128i[i] + w;
  66. %endif
  67. pxor sr2, sr3 ; sr2 = (rE & rF) ^ (~rE & rG) = Ch (e, f, g)
  68. movdqa rF, rE ; rF = rE
  69. paddd sr1, sr2 ; T1 = h + Ch (e, f, g) + sha256_consts_m128i[i] + w;
  70. movdqa sr2, rE ; sr2 = rE
  71. psrld rE, 6 ; e >> 6
  72. movdqa sr3, rE ; e >> 6
  73. pslld sr2, 7 ; e << 7
  74. psrld sr3, 5 ; e >> 11
  75. pxor rE, sr2 ; e >> 6 ^ e << 7
  76. pslld sr2, 14 ; e << 21
  77. pxor rE, sr3 ; e >> 6 ^ e << 7 ^ e >> 11
  78. psrld sr3, 14 ; e >> 25
  79. pxor rE, sr2 ; e >> 6 ^ e << 7 ^ e >> 11 ^ e << 21
  80. pslld sr2, 5 ; e << 26
  81. pxor rE, sr3 ; e >> 6 ^ e << 7 ^ e >> 11 ^ e << 21 ^ e >> 25
  82. pxor rE, sr2 ; e >> 6 ^ e << 7 ^ e >> 11 ^ e << 21 ^ e >> 25 ^ e << 26
  83. movdqa sr2, rB ; sr2 = rB
  84. paddd sr1, rE ; sr1 = h + BIGSIGMA1_256(e) + Ch (e, f, g) + sha256_consts_m128i[i] + w;
  85. movdqa rE, rD ; rE = rD
  86. movdqa rD, rC ; rD = rC
  87. paddd rE, sr1 ; rE = rD + T1
  88. movdqa sr3, rC ; sr3 = rC
  89. pand rC, rA ; rC = rC & rA
  90. pand sr3, rB ; sr3 = rB & rC
  91. pand sr2, rA ; sr2 = rB & rA
  92. pxor sr2, rC ; sr2 = (rB & rA) ^ (rC & rA)
  93. movdqa rC, rB ; rC = rB
  94. pxor sr2, sr3 ; sr2 = (rB & rA) ^ (rC & rA) ^ (rB & rC)
  95. movdqa rB, rA ; rB = rA
  96. paddd sr1, sr2 ; sr1 = T1 + (rB & rA) ^ (rC & rA) ^ (rB & rC)
  97. lea rax, [rax+16]
  98. movdqa sr3, rA ; sr3 = rA
  99. psrld rA, 2 ; a >> 2
  100. pslld sr3, 10 ; a << 10
  101. movdqa sr2, rA ; a >> 2
  102. pxor rA, sr3 ; a >> 2 ^ a << 10
  103. psrld sr2, 11 ; a >> 13
  104. pxor rA, sr2 ; a >> 2 ^ a << 10 ^ a >> 13
  105. pslld sr3, 9 ; a << 19
  106. pxor rA, sr3 ; a >> 2 ^ a << 10 ^ a >> 13 ^ a << 19
  107. psrld sr2, 9 ; a >> 21
  108. pxor rA, sr2 ; a >> 2 ^ a << 10 ^ a >> 13 ^ a << 19 ^ a >> 21
  109. pslld sr3, 11 ; a << 30
  110. pxor rA, sr3 ; a >> 2 ^ a << 10 ^ a >> 13 ^ a << 19 ^ a >> 21 ^ a << 30
  111. paddd rA, sr1 ; T1 + BIGSIGMA0_256(a) + Maj(a, b, c);
  112. %endmacro
  113. %macro sha_calc_w_blk 1
  114. movdqa xmm0, [r11-(15-%1)*16] ; xmm0 = W[I-15]
  115. movdqa xmm4, [r11-(15-(%1+1))*16] ; xmm4 = W[I-15+1]
  116. movdqa xmm2, xmm0 ; xmm2 = W[I-15]
  117. movdqa xmm6, xmm4 ; xmm6 = W[I-15+1]
  118. psrld xmm0, 3 ; xmm0 = W[I-15] >> 3
  119. psrld xmm4, 3 ; xmm4 = W[I-15+1] >> 3
  120. movdqa xmm1, xmm0 ; xmm1 = W[I-15] >> 3
  121. movdqa xmm5, xmm4 ; xmm5 = W[I-15+1] >> 3
  122. pslld xmm2, 14 ; xmm2 = W[I-15] << 14
  123. pslld xmm6, 14 ; xmm6 = W[I-15+1] << 14
  124. psrld xmm1, 4 ; xmm1 = W[I-15] >> 7
  125. psrld xmm5, 4 ; xmm5 = W[I-15+1] >> 7
  126. pxor xmm0, xmm1 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7)
  127. pxor xmm4, xmm5 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7)
  128. psrld xmm1, 11 ; xmm1 = W[I-15] >> 18
  129. psrld xmm5, 11 ; xmm5 = W[I-15+1] >> 18
  130. pxor xmm0, xmm2 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14)
  131. pxor xmm4, xmm6 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7) ^ (W[I-15+1] << 14)
  132. pslld xmm2, 11 ; xmm2 = W[I-15] << 25
  133. pslld xmm6, 11 ; xmm6 = W[I-15+1] << 25
  134. pxor xmm0, xmm1 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14) ^ (W[I-15] >> 18)
  135. pxor xmm4, xmm5 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7) ^ (W[I-15+1] << 14) ^ (W[I-15+1] >> 18)
  136. pxor xmm0, xmm2 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14) ^ (W[I-15] >> 18) ^ (W[I-15] << 25)
  137. 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)
  138. movdqa xmm3, [r11-(2-%1)*16] ; xmm3 = W[I-2]
  139. movdqa xmm7, [r11-(2-(%1+1))*16] ; xmm7 = W[I-2+1]
  140. paddd xmm0, [r11-(16-%1)*16] ; xmm0 = s0(W[I-15]) + W[I-16]
  141. paddd xmm4, [r11-(16-(%1+1))*16] ; xmm4 = s0(W[I-15+1]) + W[I-16+1]
  142. ;;;;;;;;;;;;;;;;;;
  143. movdqa xmm2, xmm3 ; xmm2 = W[I-2]
  144. movdqa xmm6, xmm7 ; xmm6 = W[I-2+1]
  145. psrld xmm3, 10 ; xmm3 = W[I-2] >> 10
  146. psrld xmm7, 10 ; xmm7 = W[I-2+1] >> 10
  147. movdqa xmm1, xmm3 ; xmm1 = W[I-2] >> 10
  148. movdqa xmm5, xmm7 ; xmm5 = W[I-2+1] >> 10
  149. paddd xmm0, [r11-(7-%1)*16] ; xmm0 = s0(W[I-15]) + W[I-16] + W[I-7]
  150. pslld xmm2, 13 ; xmm2 = W[I-2] << 13
  151. pslld xmm6, 13 ; xmm6 = W[I-2+1] << 13
  152. psrld xmm1, 7 ; xmm1 = W[I-2] >> 17
  153. psrld xmm5, 7 ; xmm5 = W[I-2+1] >> 17
  154. paddd xmm4, [r11-(7-(%1+1))*16] ; xmm4 = s0(W[I-15+1]) + W[I-16+1] + W[I-7+1]
  155. pxor xmm3, xmm1 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17)
  156. pxor xmm7, xmm5 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17)
  157. psrld xmm1, 2 ; xmm1 = W[I-2] >> 19
  158. psrld xmm5, 2 ; xmm5 = W[I-2+1] >> 19
  159. pxor xmm3, xmm2 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13)
  160. pxor xmm7, xmm6 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17) ^ (W[I-2+1] << 13)
  161. pslld xmm2, 2 ; xmm2 = W[I-2] << 15
  162. pslld xmm6, 2 ; xmm6 = W[I-2+1] << 15
  163. pxor xmm3, xmm1 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13) ^ (W[I-2] >> 19)
  164. pxor xmm7, xmm5 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17) ^ (W[I-2+1] << 13) ^ (W[I-2+1] >> 19)
  165. pxor xmm3, xmm2 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13) ^ (W[I-2] >> 19) ^ (W[I-2] << 15)
  166. 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)
  167. paddd xmm0, xmm3 ; xmm0 = s0(W[I-15]) + W[I-16] + s1(W[I-2]) + W[I-7]
  168. paddd xmm4, xmm7 ; xmm4 = s0(W[I-15+1]) + W[I-16+1] + s1(W[I-2+1]) + W[I-7+1]
  169. movdqa [r11+(%1*16)], xmm0
  170. movdqa [r11+((%1+1)*16)], xmm4
  171. %endmacro
  172. ; _sha256_sse2_64_new hash(rdi), hash1(rsi), data(rdx), init(rcx),
  173. sha256_sse2_64_new:
  174. push rbx
  175. %ifidn __OUTPUT_FORMAT__,win64
  176. sub rsp, 16 * 6
  177. movdqa [rsp + 16*0], xmm6
  178. movdqa [rsp + 16*1], xmm7
  179. movdqa [rsp + 16*2], xmm8
  180. movdqa [rsp + 16*3], xmm9
  181. movdqa [rsp + 16*4], xmm10
  182. movdqa [rsp + 16*5], xmm13
  183. %endif
  184. %macro SHA_256 0
  185. mov rbx, 64*4 ; rbx is # of SHA-2 rounds
  186. mov rax, 16*4 ; rax is where we expand to
  187. push rbx
  188. lea rbx, qword [data+rbx*4]
  189. lea r11, qword [data+rax*4]
  190. %%SHA_CALC_W:
  191. %assign i 0
  192. %rep SHA_CALC_W_UNROLL
  193. sha_calc_w_blk i
  194. %assign i i+SHA_CALC_W_PARA
  195. %endrep
  196. add r11, SHA_CALC_W_UNROLL*SHA_CALC_W_PARA*16
  197. cmp r11, rbx
  198. jb %%SHA_CALC_W
  199. pop rbx
  200. mov rax, 0
  201. lea rbx, [rbx*4]
  202. movdqa rA, [init]
  203. pshufd rB, rA, 0x55 ; rB == B
  204. pshufd rC, rA, 0xAA ; rC == C
  205. pshufd rD, rA, 0xFF ; rD == D
  206. pshufd rA, rA, 0 ; rA == A
  207. movdqa rE, [init+4*4]
  208. pshufd rF, rE, 0x55 ; rF == F
  209. pshufd rG, rE, 0xAA ; rG == G
  210. pshufd rH, rE, 0xFF ; rH == H
  211. pshufd rE, rE, 0 ; rE == E
  212. %ifidn __YASM_OBJFMT__, macho64
  213. lea rcx, [sha256_consts_m128i wrt rip]
  214. %endif
  215. %%SHAROUND_LOOP:
  216. %assign i 0
  217. %rep SHA_ROUND_LOOP_UNROLL
  218. sha_round_blk
  219. %assign i i+1
  220. %endrep
  221. cmp rax, rbx
  222. jb %%SHAROUND_LOOP
  223. ; Finished the 64 rounds, calculate hash and save
  224. movdqa sr1, [init]
  225. pshufd sr2, sr1, 0x55
  226. pshufd sr3, sr1, 0xAA
  227. pshufd sr4, sr1, 0xFF
  228. pshufd sr1, sr1, 0
  229. paddd rB, sr2
  230. paddd rC, sr3
  231. paddd rD, sr4
  232. paddd rA, sr1
  233. movdqa sr1, [init+4*4]
  234. pshufd sr2, sr1, 0x55
  235. pshufd sr3, sr1, 0xAA
  236. pshufd sr4, sr1, 0xFF
  237. pshufd sr1, sr1, 0
  238. paddd rF, sr2
  239. paddd rG, sr3
  240. paddd rH, sr4
  241. paddd rE, sr1
  242. %endmacro
  243. SHA_256
  244. movdqa [hash1+0*16], rA
  245. movdqa [hash1+1*16], rB
  246. movdqa [hash1+2*16], rC
  247. movdqa [hash1+3*16], rD
  248. movdqa [hash1+4*16], rE
  249. movdqa [hash1+5*16], rF
  250. movdqa [hash1+6*16], rG
  251. movdqa [hash1+7*16], rH
  252. mov data, hash1
  253. mov init, qword sha256_init
  254. SHA_256
  255. movdqa [hash+7*16], rH
  256. LAB_RET:
  257. %ifidn __OUTPUT_FORMAT__,win64
  258. movdqa xmm6, [rsp + 16*0]
  259. movdqa xmm7, [rsp + 16*1]
  260. movdqa xmm8, [rsp + 16*2]
  261. movdqa xmm9, [rsp + 16*3]
  262. movdqa xmm10, [rsp + 16*4]
  263. movdqa xmm13, [rsp + 16*5]
  264. add rsp, 16 * 6
  265. %endif
  266. pop rbx
  267. ret
  268. %ifidn __OUTPUT_FORMAT__,elf
  269. section .note.GNU-stack noalloc noexec nowrite progbits
  270. %endif
  271. %ifidn __OUTPUT_FORMAT__,elf64
  272. section .note.GNU-stack noalloc noexec nowrite progbits
  273. %endif