sha256_sse4_amd64.asm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. ;; SHA-256 for X86-64 for Linux, based off of:
  2. ; (c) Ufasoft 2011 http://ufasoft.com mailto:support@ufasoft.com
  3. ; Version 2011
  4. ; This software is Public Domain
  5. ; Significant re-write/optimisation and reordering by,
  6. ; Neil Kettle <mu-b@digit-labs.org>
  7. ; ~18% performance improvement
  8. ; SHA-256 CPU SSE cruncher for Bitcoin Miner
  9. ALIGN 32
  10. BITS 64
  11. %ifidn __OUTPUT_FORMAT__,win64
  12. %define hash rcx
  13. %define data rdx
  14. %define init r8
  15. %define temp r9
  16. %else
  17. %define hash rdi
  18. %define data rsi
  19. %define init rdx
  20. %define temp rcx
  21. %endif
  22. ; 0 = (1024 - 256) (mod (LAB_CALC_UNROLL*LAB_CALC_PARA*16))
  23. %define LAB_CALC_PARA 2
  24. %define LAB_CALC_UNROLL 8
  25. %define LAB_LOOP_UNROLL 8
  26. extern g_4sha256_k
  27. global CalcSha256_x64_sse4
  28. ; CalcSha256 hash(rdi), data(rsi), init(rdx)
  29. ; CalcSha256 hash(rcx), data(rdx), init(r8)
  30. CalcSha256_x64_sse4:
  31. push rbx
  32. %ifidn __OUTPUT_FORMAT__,win64
  33. sub rsp, 16 * 6
  34. movdqa [rsp + 16*0], xmm6
  35. movdqa [rsp + 16*1], xmm7
  36. movdqa [rsp + 16*2], xmm8
  37. movdqa [rsp + 16*3], xmm9
  38. movdqa [rsp + 16*4], xmm10
  39. movdqa [rsp + 16*5], xmm11
  40. %endif
  41. LAB_NEXT_NONCE:
  42. mov temp, 64*4 ; 256 - temp is # of SHA-2 rounds
  43. mov rax, 16*4 ; 64 - rax is where we expand to
  44. LAB_SHA:
  45. push temp
  46. lea temp, qword [data+temp*4] ; + 1024
  47. lea r11, qword [data+rax*4] ; + 256
  48. LAB_CALC:
  49. %macro lab_calc_blk 1
  50. movntdqa xmm0, [r11-(15-%1)*16] ; xmm0 = W[I-15]
  51. movdqa xmm2, xmm0 ; xmm2 = W[I-15]
  52. movntdqa xmm4, [r11-(15-(%1+1))*16] ; xmm4 = W[I-15+1]
  53. movdqa xmm6, xmm4 ; xmm6 = W[I-15+1]
  54. psrld xmm0, 3 ; xmm0 = W[I-15] >> 3
  55. movdqa xmm1, xmm0 ; xmm1 = W[I-15] >> 3
  56. pslld xmm2, 14 ; xmm2 = W[I-15] << 14
  57. psrld xmm4, 3 ; xmm4 = W[I-15+1] >> 3
  58. movdqa xmm5, xmm4 ; xmm5 = W[I-15+1] >> 3
  59. psrld xmm5, 4 ; xmm5 = W[I-15+1] >> 7
  60. pxor xmm4, xmm5 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7)
  61. pslld xmm6, 14 ; xmm6 = W[I-15+1] << 14
  62. psrld xmm1, 4 ; xmm1 = W[I-15] >> 7
  63. pxor xmm0, xmm1 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7)
  64. pxor xmm0, xmm2 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14)
  65. psrld xmm1, 11 ; xmm1 = W[I-15] >> 18
  66. psrld xmm5, 11 ; xmm5 = W[I-15+1] >> 18
  67. pxor xmm4, xmm6 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7) ^ (W[I-15+1] << 14)
  68. pxor xmm4, xmm5 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7) ^ (W[I-15+1] << 14) ^ (W[I-15+1] >> 18)
  69. pslld xmm2, 11 ; xmm2 = W[I-15] << 25
  70. pslld xmm6, 11 ; xmm6 = W[I-15+1] << 25
  71. 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)
  72. pxor xmm0, xmm1 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14) ^ (W[I-15] >> 18)
  73. pxor xmm0, xmm2 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14) ^ (W[I-15] >> 18) ^ (W[I-15] << 25)
  74. paddd xmm0, [r11-(16-%1)*16] ; xmm0 = s0(W[I-15]) + W[I-16]
  75. paddd xmm4, [r11-(16-(%1+1))*16] ; xmm4 = s0(W[I-15+1]) + W[I-16+1]
  76. movntdqa xmm3, [r11-(2-%1)*16] ; xmm3 = W[I-2]
  77. movntdqa xmm7, [r11-(2-(%1+1))*16] ; xmm7 = W[I-2+1]
  78. ;;;;;;;;;;;;;;;;;;
  79. movdqa xmm2, xmm3 ; xmm2 = W[I-2]
  80. psrld xmm3, 10 ; xmm3 = W[I-2] >> 10
  81. movdqa xmm1, xmm3 ; xmm1 = W[I-2] >> 10
  82. movdqa xmm6, xmm7 ; xmm6 = W[I-2+1]
  83. psrld xmm7, 10 ; xmm7 = W[I-2+1] >> 10
  84. movdqa xmm5, xmm7 ; xmm5 = W[I-2+1] >> 10
  85. paddd xmm0, [r11-(7-%1)*16] ; xmm0 = s0(W[I-15]) + W[I-16] + W[I-7]
  86. paddd xmm4, [r11-(7-(%1+1))*16] ; xmm4 = s0(W[I-15+1]) + W[I-16+1] + W[I-7+1]
  87. pslld xmm2, 13 ; xmm2 = W[I-2] << 13
  88. pslld xmm6, 13 ; xmm6 = W[I-2+1] << 13
  89. psrld xmm1, 7 ; xmm1 = W[I-2] >> 17
  90. psrld xmm5, 7 ; xmm5 = W[I-2+1] >> 17
  91. pxor xmm3, xmm1 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17)
  92. psrld xmm1, 2 ; xmm1 = W[I-2] >> 19
  93. pxor xmm3, xmm2 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13)
  94. pslld xmm2, 2 ; xmm2 = W[I-2] << 15
  95. pxor xmm7, xmm5 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17)
  96. psrld xmm5, 2 ; xmm5 = W[I-2+1] >> 19
  97. pxor xmm7, xmm6 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17) ^ (W[I-2+1] << 13)
  98. pslld xmm6, 2 ; xmm6 = W[I-2+1] << 15
  99. pxor xmm3, xmm1 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13) ^ (W[I-2] >> 19)
  100. pxor xmm3, xmm2 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13) ^ (W[I-2] >> 19) ^ (W[I-2] << 15)
  101. paddd xmm0, xmm3 ; xmm0 = s0(W[I-15]) + W[I-16] + s1(W[I-2]) + W[I-7]
  102. pxor xmm7, xmm5 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17) ^ (W[I-2+1] << 13) ^ (W[I-2+1] >> 19)
  103. 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)
  104. paddd xmm4, xmm7 ; xmm4 = s0(W[I-15+1]) + W[I-16+1] + s1(W[I-2+1]) + W[I-7+1]
  105. movdqa [r11+(%1*16)], xmm0
  106. movdqa [r11+((%1+1)*16)], xmm4
  107. %endmacro
  108. %assign i 0
  109. %rep LAB_CALC_UNROLL
  110. lab_calc_blk i
  111. %assign i i+LAB_CALC_PARA
  112. %endrep
  113. add r11, LAB_CALC_UNROLL*LAB_CALC_PARA*16
  114. cmp r11, temp
  115. jb LAB_CALC
  116. pop temp
  117. mov rax, 0
  118. ; Load the init values of the message into the hash.
  119. movntdqa xmm7, [init]
  120. pshufd xmm5, xmm7, 0x55 ; xmm5 == b
  121. pshufd xmm4, xmm7, 0xAA ; xmm4 == c
  122. pshufd xmm3, xmm7, 0xFF ; xmm3 == d
  123. pshufd xmm7, xmm7, 0 ; xmm7 == a
  124. movntdqa xmm0, [init+4*4]
  125. pshufd xmm8, xmm0, 0x55 ; xmm8 == f
  126. pshufd xmm9, xmm0, 0xAA ; xmm9 == g
  127. pshufd xmm10, xmm0, 0xFF ; xmm10 == h
  128. pshufd xmm0, xmm0, 0 ; xmm0 == e
  129. LAB_LOOP:
  130. ;; T t1 = h + (Rotr32(e, 6) ^ Rotr32(e, 11) ^ Rotr32(e, 25)) + ((e & f) ^ AndNot(e, g)) + Expand32<T>(g_sha256_k[j]) + w[j]
  131. %macro lab_loop_blk 0
  132. movntdqa xmm6, [data+rax*4]
  133. paddd xmm6, g_4sha256_k[rax*4]
  134. add rax, 4
  135. paddd xmm6, xmm10 ; +h
  136. movdqa xmm1, xmm0
  137. movdqa xmm2, xmm9
  138. pandn xmm1, xmm2 ; ~e & g
  139. movdqa xmm10, xmm2 ; h = g
  140. movdqa xmm2, xmm8 ; f
  141. movdqa xmm9, xmm2 ; g = f
  142. pand xmm2, xmm0 ; e & f
  143. pxor xmm1, xmm2 ; (e & f) ^ (~e & g)
  144. movdqa xmm8, xmm0 ; f = e
  145. paddd xmm6, xmm1 ; Ch + h + w[i] + k[i]
  146. movdqa xmm1, xmm0
  147. psrld xmm0, 6
  148. movdqa xmm2, xmm0
  149. pslld xmm1, 7
  150. psrld xmm2, 5
  151. pxor xmm0, xmm1
  152. pxor xmm0, xmm2
  153. pslld xmm1, 14
  154. psrld xmm2, 14
  155. pxor xmm0, xmm1
  156. pxor xmm0, xmm2
  157. pslld xmm1, 5
  158. pxor xmm0, xmm1 ; Rotr32(e, 6) ^ Rotr32(e, 11) ^ Rotr32(e, 25)
  159. paddd xmm6, xmm0 ; xmm6 = t1
  160. movdqa xmm0, xmm3 ; d
  161. paddd xmm0, xmm6 ; e = d+t1
  162. movdqa xmm1, xmm5 ; =b
  163. movdqa xmm3, xmm4 ; d = c
  164. movdqa xmm2, xmm4 ; c
  165. pand xmm2, xmm5 ; b & c
  166. pand xmm4, xmm7 ; a & c
  167. pand xmm1, xmm7 ; a & b
  168. pxor xmm1, xmm4
  169. movdqa xmm4, xmm5 ; c = b
  170. movdqa xmm5, xmm7 ; b = a
  171. pxor xmm1, xmm2 ; (a & c) ^ (a & d) ^ (c & d)
  172. paddd xmm6, xmm1 ; t1 + ((a & c) ^ (a & d) ^ (c & d))
  173. movdqa xmm2, xmm7
  174. psrld xmm7, 2
  175. movdqa xmm1, xmm7
  176. pslld xmm2, 10
  177. psrld xmm1, 11
  178. pxor xmm7, xmm2
  179. pxor xmm7, xmm1
  180. pslld xmm2, 9
  181. psrld xmm1, 9
  182. pxor xmm7, xmm2
  183. pxor xmm7, xmm1
  184. pslld xmm2, 11
  185. pxor xmm7, xmm2
  186. paddd xmm7, xmm6 ; a = t1 + (Rotr32(a, 2) ^ Rotr32(a, 13) ^ Rotr32(a, 22)) + ((a & c) ^ (a & d) ^ (c & d));
  187. %endmacro
  188. %assign i 0
  189. %rep LAB_LOOP_UNROLL
  190. lab_loop_blk
  191. %assign i i+1
  192. %endrep
  193. cmp rax, temp
  194. jb LAB_LOOP
  195. ; Finished the 64 rounds, calculate hash and save
  196. movntdqa xmm1, [init]
  197. pshufd xmm2, xmm1, 0x55
  198. paddd xmm5, xmm2
  199. pshufd xmm6, xmm1, 0xAA
  200. paddd xmm4, xmm6
  201. pshufd xmm11, xmm1, 0xFF
  202. paddd xmm3, xmm11
  203. pshufd xmm1, xmm1, 0
  204. paddd xmm7, xmm1
  205. movntdqa xmm1, [init+4*4]
  206. pshufd xmm2, xmm1, 0x55
  207. paddd xmm8, xmm2
  208. pshufd xmm6, xmm1, 0xAA
  209. paddd xmm9, xmm6
  210. pshufd xmm11, xmm1, 0xFF
  211. paddd xmm10, xmm11
  212. pshufd xmm1, xmm1, 0
  213. paddd xmm0, xmm1
  214. movdqa [hash+0*16], xmm7
  215. movdqa [hash+1*16], xmm5
  216. movdqa [hash+2*16], xmm4
  217. movdqa [hash+3*16], xmm3
  218. movdqa [hash+4*16], xmm0
  219. movdqa [hash+5*16], xmm8
  220. movdqa [hash+6*16], xmm9
  221. movdqa [hash+7*16], xmm10
  222. LAB_RET:
  223. %ifidn __OUTPUT_FORMAT__,win64
  224. movdqa xmm6, [rsp + 16*0]
  225. movdqa xmm7, [rsp + 16*1]
  226. movdqa xmm8, [rsp + 16*2]
  227. movdqa xmm9, [rsp + 16*3]
  228. movdqa xmm10, [rsp + 16*4]
  229. movdqa xmm11, [rsp + 16*5]
  230. add rsp, 16 * 6
  231. %endif
  232. pop rbx
  233. ret
  234. %ifidn __OUTPUT_FORMAT__,elf
  235. section .note.GNU-stack noalloc noexec nowrite progbits
  236. %endif
  237. %ifidn __OUTPUT_FORMAT__,elf64
  238. section .note.GNU-stack noalloc noexec nowrite progbits
  239. %endif