Advertisement
Guest User

Untitled

a guest
Jun 30th, 2011
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.45 KB | None | 0 0
  1. // This file is taken and modified from the public-domain poclbm project, and
  2. // we have therefore decided to keep it public-domain in Phoenix.
  3.  
  4. #ifdef VECTORS
  5. typedef uint2 u;
  6. #else
  7. typedef uint u;
  8. #endif
  9.  
  10. __constant uint K[64] = {
  11. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  12. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  13. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  14. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  15. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  16. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  17. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  18. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  19. };
  20.  
  21. __constant uint H[8] = {
  22. 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
  23. };
  24.  
  25. #ifdef BITALIGN
  26. #pragma OPENCL EXTENSION cl_amd_media_ops : enable
  27. #define rotD(x, y) amd_bitalign(x, x, (u)(32-y))
  28. #define rotS(val,nBits) (((val)<<(u)(nBits))|((val)>>(u)(32-nBits)))
  29. #define rot(x,y) rotD(x,y)
  30. #define rotW(idx,nbits) ( (((idx>=4)&&(idx<=15))||((idx>=64+8)&&(idx<=64+15))) ? (rotS(W[(idx)],(nbits))) : (rotD(W[(idx)],(nbits))) )
  31. #else
  32. #define rot(x, y) rotate(x, (u)y)
  33. #define rotW(idx,nbits) rot(W[(idx])],(nbits))
  34. #endif
  35.  
  36.  
  37.  
  38.  
  39. // This part is not from the stock poclbm kernel. It's part of an optimization
  40. // added in the Phoenix Miner.
  41.  
  42. // Some AMD devices have the BFI_INT opcode, which behaves exactly like the
  43. // SHA-256 Ch function, but provides it in exactly one instruction. If
  44. // detected, use it for Ch. Otherwise, construct Ch out of simpler logical
  45. // primitives.
  46.  
  47. #ifdef BFI_INT
  48. // Well, slight problem... It turns out BFI_INT isn't actually exposed to
  49. // OpenCL (or CAL IL for that matter) in any way. However, there is
  50. // a similar instruction, BYTE_ALIGN_INT, which is exposed to OpenCL via
  51. // amd_bytealign, takes the same inputs, and provides the same output.
  52. // We can use that as a placeholder for BFI_INT and have the application
  53. // patch it after compilation.
  54.  
  55. // This is the BFI_INT function
  56. #define Ch(x, y, z) amd_bytealign(x, y, z)
  57.  
  58. // Ma can also be implemented in terms of BFI_INT...
  59. //#define Ma(x, y, z) amd_bytealign((y), (x | z), (z & x))
  60. #define Ma(x, y, z) amd_bytealign( ((z)^(x)), (y), (x) )
  61. #else
  62. #define Ch(x, y, z) (z ^ (x & (y ^ z)))
  63. #define Ma(x, y, z) ((x & z) | (y & (x | z)))
  64. #endif
  65.  
  66. //Various intermediate calculations for each SHA round
  67. #define s0(n) (rot(Vals[(0 + 128 - (n)) % 8], 30)^rot(Vals[(0 + 128 - (n)) % 8], 19)^rot(Vals[(0 + 128 - (n)) % 8], 10))
  68. #define s1(n) (rot(Vals[(4 + 128 - (n)) % 8], 26)^rot(Vals[(4 + 128 - (n)) % 8], 21)^rot(Vals[(4 + 128 - (n)) % 8], 7))
  69. #define ch(n) (Ch(Vals[(4 + 128 - (n)) % 8],Vals[(5 + 128 - (n)) % 8],Vals[(6 + 128 - (n)) % 8]))
  70. #define maj(n) (Ma(Vals[(1 + 128 - (n)) % 8],Vals[(2 + 128 - (n)) % 8],Vals[(0 + 128 - (n)) % 8]))
  71. #define t1(n) (Vals[(7 + 128 - (n)) % 8] + K[(n) % 64]+ W[(n)] + ch(n) + s1(n))
  72. #define t1W(n) (Vals[(7 + 128 - (n)) % 8] + K[(n) % 64]+ w(n) + ch(n) + s1(n))
  73. #define t2(n) (s0(n) + maj(n))
  74.  
  75. //W calculation used for SHA round
  76. #define w(n) (W[n] = P1(n) + P2(n) + P3(n) + P4(n))
  77.  
  78. //Full W calculation
  79. #define R(x) (W[x] = (rotW(x-2,15)^rotW(x-2,13)^((W[x-2])>>10U)) + W[x-7] + (rotW(x-15,25)^rotW(x-15,14)^((W[x-15])>>3U)) + W[x-16])
  80.  
  81. //Partial W calculations (used for the begining where only some values are nonzero)
  82. #define r0(x) ((rot(x,25)^rot(x,14)^((x)>>3U)))
  83. #define r1(x) ((rot(x],15)^rot(x,13)^((x)>>10U)))
  84. #define R0(n) ((rotW((n),25)^rotW((n),14)^((W[(n)])>>3U)))
  85. #define R1(n) ((rotW((n),15)^rotW((n),13)^((W[(n)])>>10U)))
  86. #define P1(x) R1(x-2)
  87. #define P2(x) R0(x-15)
  88. #define P3(x) W[x-7]
  89. #define P4(x) W[x-16]
  90.  
  91. //SHA round with built in W calc
  92. #define sharound2(n) { Vals[(3 + 128 - (n)) % 8] += t1W(n); Vals[(7 + 128 - (n)) % 8] = t1W(n) + t2(n); }
  93. //SHA round without W calc
  94. #define sharound(n) {t1 = t1(n); Vals[(3 + 128 - (n)) % 8] += t1(n); Vals[(7 + 128 - (n)) % 8] = t1(n) + t2(n); }
  95.  
  96. //Partial SHA calculations (used for begining and end)
  97. #define partround(n) {Vals[(7 + 128 - n) % 8]=(Vals[(7 + 128 - n) % 8]+W[n]); Vals[(3 + 128 - n) % 8]+=Vals[(7 + 128 - n) % 8]; Vals[(7 + 128 - n) % 8]+=t1;}
  98.  
  99. __kernel
  100.  
  101. void search( const uint state0, const uint state1, const uint state2, const uint state3,
  102. const uint state4, const uint state5, const uint state6, const uint state7,
  103. const uint B1, const uint C1, const uint D1,
  104. const uint F1, const uint G1, const uint H1,
  105. const uint base,
  106. const uint W2,
  107. const uint W16, const uint W17,
  108. const uint PreVal4, const uint T1,
  109. __global uint * output)
  110. {
  111.  
  112. u W[128];
  113. u Vals[8];
  114. u t1 = T1;
  115.  
  116. Vals[0]=state0;
  117. Vals[1]=B1;
  118. Vals[2]=C1;
  119. Vals[3]=D1;
  120. Vals[4]=PreVal4;
  121. Vals[5]=F1;
  122. Vals[6]=G1;
  123. Vals[7]=H1;
  124.  
  125. W[2] = W2;
  126. W[4]=0x80000000U;
  127. W[5]=0x00000000U;
  128. W[6]=0x00000000U;
  129. W[7]=0x00000000U;
  130. W[8]=0x00000000U;
  131. W[9]=0x00000000U;
  132. W[10]=0x00000000U;
  133. W[11]=0x00000000U;
  134. W[12]=0x00000000U;
  135. W[13]=0x00000000U;
  136. W[14]=0x00000000U;
  137. W[15]=0x00000280U;
  138. W[16] = W16;
  139. W[17] = W17;
  140.  
  141. W[19] = P1(19) + P2(19) + P3(19);
  142. W[18] = P1(18) + P3(18) + P4(18);
  143. W[20] = P2(20) + P3(20) + P4(20);
  144.  
  145. #ifdef VECTORS
  146. W[3] = ((base + get_global_id(0))<<1) + (uint2)(0, 1);
  147. #else
  148. W[3] = base + get_global_id(0);
  149. #endif
  150.  
  151. //the order of the W calcs and Rounds is like this because the compiler needs help finding how to order the instructions
  152. W[31] = P2(31) + P4(31);
  153. W[18] += P2(18);
  154. partround(3);
  155. W[19] += P4(19);
  156. sharound(4);
  157. W[20] += P1(20);
  158. sharound(5);
  159. W[32] = P2(32) + P4(32);
  160. W[21] = P1(21);
  161. sharound(6);
  162. W[22] = P3(22) + P1(22);
  163. W[23] = P3(23) + P1(23);
  164. sharound(7);
  165. W[24] = P1(24) + P3(24);
  166. sharound(8);
  167. W[25] = P1(25) + P3(25);
  168. sharound(9);
  169. W[26] = P1(26) + P3(26);
  170. W[27] = P1(27) + P3(27);
  171. sharound(10);
  172. sharound(11);
  173. W[28] = P1(28) + P3(28);
  174. sharound(12);
  175. W[29] = P1(29) + P3(29);
  176. W[30] = P1(30) + P2(30) + P3(30);
  177. sharound(13);
  178. sharound(14);
  179. W[31] += (P1(31) + P3(31));
  180. sharound(15);
  181. sharound(16);
  182. W[32] += (P1(32) + P3(32));
  183. sharound(17);
  184. sharound(18);
  185. sharound(19);
  186. sharound(20);
  187. sharound(21);
  188. sharound(22);
  189. sharound(23);
  190. sharound(24);
  191. sharound(25);
  192. sharound(26);
  193. sharound(27);
  194. sharound(28);
  195. sharound(29);
  196. sharound(30);
  197. sharound(31);
  198. sharound(32);
  199. sharound2(33);
  200. sharound2(34);
  201. sharound2(35);
  202. sharound2(36);
  203. sharound2(37);
  204. sharound2(38);
  205. sharound2(39);
  206. sharound2(40);
  207. sharound2(41);
  208. sharound2(42);
  209. sharound2(43);
  210. sharound2(44);
  211. sharound2(45);
  212. sharound2(46);
  213. //for some reason, this is faster than using all sharound2...
  214. R(47);
  215. sharound(47);
  216. R(48);
  217. sharound(48);
  218. R(49);
  219. sharound(49);
  220. R(50);
  221. sharound(50);
  222. R(51);
  223. sharound(51);
  224. R(52);
  225. sharound(52);
  226. R(53);
  227. sharound(53);
  228. R(54);
  229. sharound(54);
  230. R(55);
  231. sharound(55);
  232. R(56);
  233. sharound(56);
  234. R(57);
  235. sharound(57);
  236. R(58);
  237. sharound(58);
  238. R(59);
  239. sharound(59);
  240. R(60);
  241. sharound(60);
  242. R(61);
  243. sharound(61);
  244. sharound2(62);
  245. sharound2(63);
  246.  
  247. W[64]=state0+Vals[0];
  248. W[65]=state1+Vals[1];
  249. W[66]=state2+Vals[2];
  250. W[67]=state3+Vals[3];
  251. W[68]=state4+Vals[4];
  252. W[69]=state5+Vals[5];
  253. W[70]=state6+Vals[6];
  254. W[71]=state7+Vals[7];
  255.  
  256. W[64 + 8]=0x80000000U;
  257. W[64 + 9]=0x00000000U;
  258. W[64 + 10]=0x00000000U;
  259. W[64 + 11]=0x00000000U;
  260. W[64 + 12]=0x00000000U;
  261. W[64 + 13]=0x00000000U;
  262. W[64 + 14]=0x00000000U;
  263. W[64 + 15]=0x00000100U;
  264.  
  265. Vals[0]=H[0];
  266. Vals[1]=H[1];
  267. Vals[2]=H[2];
  268. Vals[3]=H[3];
  269. Vals[4]=H[4];
  270. Vals[5]=H[5];
  271. Vals[6]=H[6];
  272. Vals[7]=H[7];
  273.  
  274. Vals[7] = 0xb0edbdd0 + K[0] + W[64] + 0x08909ae5U;
  275. Vals[3] = 0xa54ff53a + 0xb0edbdd0 + K[0] + W[64];
  276.  
  277. R(64 + 16);
  278.  
  279. sharound(64 + 1);
  280. sharound(64 + 2);
  281. W[64 + 17] = P1(64 + 17) + P2(64 + 17) + P4(64 + 17);
  282. W[64 + 18] = P1(64 + 18) + P2(64 + 18) + P4(64 + 18);
  283. sharound(64 + 3);
  284. W[64 + 19] = P1(64 + 19) + P2(64 + 19) + P4(64 + 19);
  285. sharound(64 + 4);
  286. W[64 + 20] = P1(64 + 20) + P2(64 + 20) + P4(64 + 20);
  287. sharound(64 + 5);
  288. W[64 + 21] = P1(64 + 21) + P2(64 + 21) + P4(64 + 21);
  289. sharound(64 + 6);
  290. R(64 + 22);
  291. sharound(64 + 7);
  292. sharound(64 + 8);
  293. R(64 + 23);
  294. W[64 + 24] = P1(64 + 24) + P3(64 + 24) + P4(64 + 24);
  295. sharound(64 + 9);
  296. sharound(64 + 10);
  297. W[64 + 25] = P1(64 + 25) + P3(64 + 25);
  298. W[64 + 26] = P1(64 + 26) + P3(64 + 26);
  299. sharound(64 + 11);
  300. sharound(64 + 12);
  301. W[64 + 27] = P1(64 + 27) + P3(64 + 27);
  302. W[64 + 28] = P1(64 + 28) + P3(64 + 28);
  303. sharound(64 + 13);
  304. sharound(64 + 14);
  305. sharound(64 + 15);
  306. sharound(64 + 16);
  307. sharound(64 + 17);
  308. sharound(64 + 18);
  309. sharound(64 + 19);
  310. sharound(64 + 20);
  311. sharound(64 + 21);
  312. sharound(64 + 22);
  313. sharound(64 + 23);
  314. sharound(64 + 24);
  315. sharound(64 + 25);
  316. sharound(64 + 26);
  317. sharound(64 + 27);
  318. sharound(64 + 28);
  319. sharound2(64 + 29);
  320. sharound2(64 + 30);
  321. sharound2(64 + 31);
  322. sharound2(64 + 32);
  323. sharound2(64 + 33);
  324. sharound2(64 + 34);
  325. sharound2(64 + 35);
  326. sharound2(64 + 36);
  327. sharound2(64 + 37);
  328. sharound2(64 + 38);
  329. sharound2(64 + 39);
  330. sharound2(64 + 40);
  331. sharound2(64 + 41);
  332. sharound2(64 + 42);
  333. sharound2(64 + 43);
  334. sharound2(64 + 44);
  335. sharound2(64 + 45);
  336. sharound2(64 + 46);
  337. sharound2(64 + 47);
  338. sharound2(64 + 48);
  339. sharound2(64 + 49);
  340. R(64 + 50);
  341. sharound(64 + 50);
  342. R(64 + 51);
  343. sharound(64 + 51);
  344. R(64 + 52);
  345. sharound(64 + 52);
  346. R(64 + 53);
  347. sharound(64 + 53);
  348. R(64 + 54);
  349. sharound(64 + 54);
  350. R(64 + 55);
  351. sharound(64 + 55);
  352. sharound2(64 + 56);
  353. sharound2(64 + 57);
  354. sharound2(64 + 58);
  355. sharound2(64 + 59);
  356.  
  357. //Faster to write it this way...
  358. Vals[3] += K[60] +s1(124) + ch(124);
  359. R(64+60);
  360. partround(64 + 60);
  361. Vals[7] += H[7];
  362.  
  363. #ifdef VECTORS
  364. if (Vals[7].x == 0)
  365. {
  366. uint nonce = W[3].x;
  367. //Faster to shift the nonce by 4 probably due to 32 bit addressing and does not add more collisions
  368. output[OUTPUT_SIZE] = output[(nonce >> 2) & OUTPUT_MASK] = nonce;
  369.  
  370. }
  371. if (Vals[7].y == 0)
  372. {
  373. uint nonce = W[3].y;
  374. output[OUTPUT_SIZE] = output[(nonce >> 2) & OUTPUT_MASK] = nonce;
  375. }
  376. #else
  377. if (Vals[7] == 0)
  378. {
  379. uint nonce = W[3];
  380. output[OUTPUT_SIZE] = output[(nonce >> 2) & OUTPUT_MASK] = nonce;
  381. }
  382. #endif
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement