phatk.cl 11 KB

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