phatk.cl 11 KB

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