phatk110711.cl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. // 2011-07-11: further modified by Diapolo and still public-domain
  4. // -ck version to be compatible with cgminer
  5. #define VECTORSX
  6. #ifdef VECTORS4
  7. typedef uint4 u;
  8. #elif defined VECTORS2
  9. typedef uint2 u;
  10. #else
  11. typedef uint u;
  12. #endif
  13. __constant uint K[64] = {
  14. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  15. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  16. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  17. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  18. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  19. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  20. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  21. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  22. };
  23. // H[6] = 0x08909ae5U + 0xb0edbdd0 + K[0] == 0xfc08884d
  24. // H[7] = -0x5be0cd19 - (0x90befffa) K[60] == -0xec9fcd13
  25. __constant uint H[8] = {
  26. 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0xfc08884d, 0xec9fcd13
  27. };
  28. // L = 0xa54ff53a + 0xb0edbdd0 + K[0] == 0x198c7e2a2
  29. __constant ulong L = 0x198c7e2a2;
  30. #define BFI_INTX
  31. #define BITALIGNX
  32. #ifdef BITALIGN
  33. #pragma OPENCL EXTENSION cl_amd_media_ops : enable
  34. #define rot(x, y) amd_bitalign(x, x, (u)(32 - y))
  35. #else
  36. #define rot(x, y) rotate(x, (u)y)
  37. #endif
  38. #ifdef BFI_INT
  39. #define Ch(x, y, z) amd_bytealign(x, y, z)
  40. #else
  41. #define Ch(x, y, z) bitselect(z, y, x)
  42. #endif
  43. // Ma now uses the Ch function, if BFI_INT is enabled, the optimized Ch version is used
  44. #define Ma(x, y, z) Ch((z ^ x), y, x)
  45. // Various intermediate calculations for each SHA round
  46. #define s0(n) (rot(Vals[(128 - n) % 8], 30) ^ rot(Vals[(128 - n) % 8], 19) ^ rot(Vals[(128 - n) % 8], 10))
  47. #define s1(n) (rot(Vals[(132 - n) % 8], 26) ^ rot(Vals[(132 - n) % 8], 21) ^ rot(Vals[(132 - n) % 8], 7))
  48. #define ch(n) (Ch(Vals[(132 - n) % 8], Vals[(133 - n) % 8], Vals[(134 - n) % 8]))
  49. #define ma(n) (Ma(Vals[(129 - n) % 8], Vals[(130 - n) % 8], Vals[(128 - n) % 8]))
  50. #define t1(n) (K[n % 64] + Vals[(135 - n) % 8] + W[n] + s1(n) + ch(n))
  51. // intermediate W calculations
  52. #define P1(x) (rot(W[x - 2], 15) ^ rot(W[x - 2], 13) ^ (W[x - 2] >> 10U))
  53. #define P2(x) (rot(W[x - 15], 25) ^ rot(W[x - 15], 14) ^ (W[x - 15] >> 3U))
  54. #define P3(x) W[x - 7]
  55. #define P4(x) W[x - 16]
  56. // full W calculation
  57. #define W(x) (W[x] = P4(x) + P3(x) + P2(x) + P1(x))
  58. // SHA round without W calc
  59. #define sharound(n) { Vals[(131 - n) % 8] += t1(n); Vals[(135 - n) % 8] = t1(n) + s0(n) + ma(n); }
  60. __kernel void search( const uint state0, const uint state1, const uint state2, const uint state3,
  61. const uint state4, const uint state5, const uint state6, const uint state7,
  62. const uint B1, const uint C1, const uint D1,
  63. const uint F1, const uint G1, const uint H1,
  64. const uint base,
  65. const uint W2,
  66. const uint W16, const uint W17,
  67. const uint PreVal4, const uint T1,
  68. __global uint * output)
  69. {
  70. u W[124];
  71. u Vals[8];
  72. uint it = get_local_id(0);
  73. Vals[1] = B1;
  74. Vals[2] = C1;
  75. Vals[5] = F1;
  76. Vals[6] = G1;
  77. W[2] = W2;
  78. #ifdef VECTORS4
  79. Vals[4] = (W[3] = base + (get_global_id(0) << 2) + (uint4)(0, 1, 2, 3)) + PreVal4;
  80. #elif defined VECTORS2
  81. Vals[4] = (W[3] = base + (get_global_id(0) << 1) + (uint2)(0, 1)) + PreVal4;
  82. #else
  83. Vals[4] = (W[3] = base + get_global_id(0)) + PreVal4;
  84. #endif
  85. // used in: P2(19) == 285220864 (0x11002000), P4(20)
  86. W[4] = 0x80000000U;
  87. // P1(x) is 0 for x == 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
  88. // P2(x) is 0 for x == 20, 21, 22, 23, 24, 25, 26, 27, 28, 29
  89. // P3(x) is 0 for x == 12, 13, 14, 15, 16, 17, 18, 19, 20, 21
  90. // P4(x) is 0 for x == 21, 22, 23, 24, 25, 26, 27, 28, 29, 30
  91. // W[x] in sharound(x) is 0 for x == 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  92. W[14] = W[13] = W[12] = W[11] = W[10] = W[9] = W[8] = W[7] = W[6] = W[5] = 0x00000000U;
  93. // used in: P2(30) == 10485845 (0xA00055), P3(22), P4(31)
  94. // K[15] + W[15] == 0xc19bf174 + 0x00000280U = 0xc19bf3f4
  95. W[15] = 0x00000280U;
  96. W[16] = W16;
  97. W[17] = W17;
  98. // removed P3(18) from add because it is == 0
  99. W[18] = P1(18) + P4(18) + P2(18);
  100. // removed P3(19) from add because it is == 0
  101. W[19] = (u)0x11002000 + P1(19) + P4(19);
  102. // removed P2(20), P3(20) from add because it is == 0
  103. W[20] = P1(20) + P4(20);
  104. W[21] = P1(21);
  105. W[22] = P1(22) + P3(22);
  106. W[23] = P1(23) + P3(23);
  107. W[24] = P1(24) + P3(24);
  108. W[25] = P1(25) + P3(25);
  109. W[26] = P1(26) + P3(26);
  110. W[27] = P1(27) + P3(27);
  111. W[28] = P1(28) + P3(28);
  112. W[29] = P1(29) + P3(29);
  113. W[30] = (u)0xA00055 + P1(30) + P3(30);
  114. // Round 3
  115. Vals[0] = state0 + Vals[4];
  116. Vals[4] += T1;
  117. // Round 4
  118. // K[4] + W[4] == 0x3956c25b + 0x80000000U = 0xb956c25b
  119. Vals[7] = (Vals[3] = (u)0xb956c25b + D1 + s1(4) + ch(4)) + H1;
  120. Vals[3] += s0(4) + ma(4);
  121. // Round 5
  122. Vals[2] = K[5] + C1 + s1(5) + ch(5) + s0(5) + ma(5);
  123. Vals[6] = K[5] + C1 + G1 + s1(5) + ch(5);
  124. sharound(6);
  125. sharound(7);
  126. sharound(8);
  127. sharound(9);
  128. sharound(10);
  129. sharound(11);
  130. sharound(12);
  131. sharound(13);
  132. sharound(14);
  133. sharound(15);
  134. sharound(16);
  135. sharound(17);
  136. sharound(18);
  137. sharound(19);
  138. sharound(20);
  139. sharound(21);
  140. sharound(22);
  141. sharound(23);
  142. sharound(24);
  143. sharound(25);
  144. sharound(26);
  145. sharound(27);
  146. sharound(28);
  147. sharound(29);
  148. sharound(30);
  149. W(31);
  150. sharound(31);
  151. W(32);
  152. sharound(32);
  153. W(33);
  154. sharound(33);
  155. W(34);
  156. sharound(34);
  157. W(35);
  158. sharound(35);
  159. W(36);
  160. sharound(36);
  161. W(37);
  162. sharound(37);
  163. W(38);
  164. sharound(38);
  165. W(39);
  166. sharound(39);
  167. W(40);
  168. sharound(40);
  169. W(41);
  170. sharound(41);
  171. W(42);
  172. sharound(42);
  173. W(43);
  174. sharound(43);
  175. W(44);
  176. sharound(44);
  177. W(45);
  178. sharound(45);
  179. W(46);
  180. sharound(46);
  181. W(47);
  182. sharound(47);
  183. W(48);
  184. sharound(48);
  185. W(49);
  186. sharound(49);
  187. W(50);
  188. sharound(50);
  189. W(51);
  190. sharound(51);
  191. W(52);
  192. sharound(52);
  193. W(53);
  194. sharound(53);
  195. W(54);
  196. sharound(54);
  197. W(55);
  198. sharound(55);
  199. W(56);
  200. sharound(56);
  201. W(57);
  202. sharound(57);
  203. W(58);
  204. sharound(58);
  205. W(59);
  206. sharound(59);
  207. W(60);
  208. sharound(60);
  209. W(61);
  210. sharound(61);
  211. W(62);
  212. sharound(62);
  213. W(63);
  214. sharound(63);
  215. W[64] = state0 + Vals[0];
  216. W[65] = state1 + Vals[1];
  217. W[66] = state2 + Vals[2];
  218. W[67] = state3 + Vals[3];
  219. W[68] = state4 + Vals[4];
  220. W[69] = state5 + Vals[5];
  221. W[70] = state6 + Vals[6];
  222. W[71] = state7 + Vals[7];
  223. // used in: P2(87) = 285220864 (0x11002000), P4(88)
  224. // K[72] + W[72] ==
  225. W[72] = 0x80000000U;
  226. // P1(x) is 0 for x == 75, 76, 77, 78, 79, 80
  227. // P2(x) is 0 for x == 88, 89, 90, 91, 92, 93
  228. // P3(x) is 0 for x == 80, 81, 82, 83, 84, 85
  229. // P4(x) is 0 for x == 89, 90, 91, 92, 93, 94
  230. // W[x] in sharound(x) is 0 for x == 73, 74, 75, 76, 77, 78
  231. W[78] = W[77] = W[76] = W[75] = W[74] = W[73] = 0x00000000U;
  232. // used in: P1(81) = 10485760 (0xA00000), P2(94) = 4194338 (0x400022), P3(86), P4(95)
  233. // K[79] + W[79] ==
  234. W[79] = 0x00000100U;
  235. Vals[0] = H[0];
  236. Vals[1] = H[1];
  237. Vals[2] = H[2];
  238. Vals[3] = (u)L + W[64];
  239. Vals[4] = H[3];
  240. Vals[5] = H[4];
  241. Vals[6] = H[5];
  242. Vals[7] = H[6] + W[64];
  243. sharound(65);
  244. sharound(66);
  245. sharound(67);
  246. sharound(68);
  247. sharound(69);
  248. sharound(70);
  249. sharound(71);
  250. sharound(72);
  251. sharound(73);
  252. sharound(74);
  253. sharound(75);
  254. sharound(76);
  255. sharound(77);
  256. sharound(78);
  257. sharound(79);
  258. // removed P1(80), P3(80) from add because it is == 0
  259. W[80] = P2(80) + P4(80);
  260. W[81] = (u)0xA00000 + P4(81) + P2(81);
  261. W[82] = P4(82) + P2(82) + P1(82);
  262. W[83] = P4(83) + P2(83) + P1(83);
  263. W[84] = P4(84) + P2(84) + P1(84);
  264. W[85] = P4(85) + P2(85) + P1(85);
  265. W(86);
  266. sharound(80);
  267. sharound(81);
  268. sharound(82);
  269. sharound(83);
  270. sharound(84);
  271. sharound(85);
  272. sharound(86);
  273. W[87] = (u)0x11002000 + P4(87) + P3(87) + P1(87);
  274. sharound(87);
  275. W[88] = P4(88) + P3(88) + P1(88);
  276. sharound(88);
  277. W[89] = P3(89) + P1(89);
  278. sharound(89);
  279. W[90] = P3(90) + P1(90);
  280. sharound(90);
  281. W[91] = P3(91) + P1(91);
  282. sharound(91);
  283. W[92] = P3(92) + P1(92);
  284. sharound(92);
  285. // removed P2(93), P4(93) from add because it is == 0
  286. W[93] = P3(93) + P1(93);
  287. sharound(93);
  288. // removed P4(94) from add because it is == 0
  289. W[94] = (u)0x400022 + P3(94) + P1(94);
  290. sharound(94);
  291. W(95);
  292. sharound(95);
  293. W(96);
  294. sharound(96);
  295. W(97);
  296. sharound(97);
  297. W(98);
  298. sharound(98);
  299. W(99);
  300. sharound(99);
  301. W(100);
  302. sharound(100);
  303. W(101);
  304. sharound(101);
  305. W(102);
  306. sharound(102);
  307. W(103);
  308. sharound(103);
  309. W(104);
  310. sharound(104);
  311. W(105);
  312. sharound(105);
  313. W(106);
  314. sharound(106);
  315. W(107);
  316. sharound(107);
  317. W(108);
  318. sharound(108);
  319. W(109);
  320. sharound(109);
  321. W(110);
  322. sharound(110);
  323. W(111);
  324. sharound(111);
  325. W(112);
  326. sharound(112);
  327. W(113);
  328. sharound(113);
  329. W(114);
  330. sharound(114);
  331. W(115);
  332. sharound(115);
  333. W(116);
  334. sharound(116);
  335. W(117);
  336. sharound(117);
  337. W(118);
  338. sharound(118);
  339. W(119);
  340. sharound(119);
  341. W(120);
  342. sharound(120);
  343. W(121);
  344. sharound(121);
  345. W(122);
  346. sharound(122);
  347. W(123);
  348. sharound(123);
  349. // Round 124
  350. Vals[7] += Vals[3] + P4(124) + P3(124) + P2(124) + P1(124) + s1(124) + ch(124);
  351. #define MAXBUFFERS (4 * 512)
  352. #if defined(VECTORS4) || defined(VECTORS2)
  353. if (Vals[7].x == -H[7])
  354. {
  355. // Unlikely event there is something here already !
  356. if (output[it]) {
  357. for (it = 0; it < MAXBUFFERS; it++) {
  358. if (!output[it])
  359. break;
  360. }
  361. }
  362. output[it] = W[3].x;
  363. output[MAXBUFFERS] = 1;
  364. }
  365. if (Vals[7].y == -H[7])
  366. {
  367. it += 512;
  368. if (output[it]) {
  369. for (it = 0; it < MAXBUFFERS; it++) {
  370. if (!output[it])
  371. break;
  372. }
  373. }
  374. output[it] = W[3].y;
  375. output[MAXBUFFERS] = 1;
  376. }
  377. #ifdef VECTORS4
  378. if (Vals[7].z == -H[7])
  379. {
  380. it += 1024;
  381. if (output[it]) {
  382. for (it = 0; it < MAXBUFFERS; it++) {
  383. if (!output[it])
  384. break;
  385. }
  386. }
  387. output[it] = W[3].z;
  388. output[MAXBUFFERS] = 1;
  389. }
  390. if (Vals[7].w == -H[7])
  391. {
  392. it += 1536;
  393. if (output[it]) {
  394. for (it = 0; it < MAXBUFFERS; it++) {
  395. if (!output[it])
  396. break;
  397. }
  398. }
  399. output[it] = W[3].w;
  400. output[MAXBUFFERS] = 1;
  401. }
  402. #endif
  403. #else
  404. if (Vals[7] == -H[7])
  405. {
  406. if (output[it]) {
  407. for (it = 0; it < MAXBUFFERS; it++) {
  408. if (!output[it])
  409. break;
  410. }
  411. }
  412. output[it] = W[3];
  413. output[MAXBUFFERS] = 1;
  414. }
  415. #endif
  416. }