miner.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #ifndef __MINER_H__
  2. #define __MINER_H__
  3. #include "config.h"
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <sys/time.h>
  7. #include <pthread.h>
  8. #include <jansson.h>
  9. #include <curl/curl.h>
  10. #ifdef __APPLE_CC__
  11. #include <OpenCL/opencl.h>
  12. #else
  13. #include <CL/cl.h>
  14. #endif
  15. #ifdef STDC_HEADERS
  16. # include <stdlib.h>
  17. # include <stddef.h>
  18. #else
  19. # ifdef HAVE_STDLIB_H
  20. # include <stdlib.h>
  21. # endif
  22. #endif
  23. #ifdef HAVE_ALLOCA_H
  24. # include <alloca.h>
  25. #elif defined __GNUC__
  26. # define alloca __builtin_alloca
  27. #elif defined _AIX
  28. # define alloca __alloca
  29. #elif defined _MSC_VER
  30. # include <malloc.h>
  31. # define alloca _alloca
  32. #else
  33. # ifndef HAVE_ALLOCA
  34. # ifdef __cplusplus
  35. extern "C"
  36. # endif
  37. void *alloca (size_t);
  38. # endif
  39. #endif
  40. #ifdef __SSE2__
  41. #define WANT_SSE2_4WAY 1
  42. #endif
  43. #if defined(__i386__) || defined(__x86_64__)
  44. #define WANT_VIA_PADLOCK 1
  45. #endif
  46. #if defined(__x86_64__) && defined(__SSE2__) && defined(HAS_YASM)
  47. #define WANT_X8664_SSE2 1
  48. #endif
  49. #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  50. #define WANT_BUILTIN_BSWAP
  51. #else
  52. #if HAVE_BYTESWAP_H
  53. #include <byteswap.h>
  54. #elif defined(USE_SYS_ENDIAN_H)
  55. #include <sys/endian.h>
  56. #elif defined(__APPLE__)
  57. #include <libkern/OSByteOrder.h>
  58. #define bswap_16 OSSwapInt16
  59. #define bswap_32 OSSwapInt32
  60. #define bswap_64 OSSwapInt64
  61. #else
  62. #define bswap_16(value) \
  63. ((((value) & 0xff) << 8) | ((value) >> 8))
  64. #define bswap_32(value) \
  65. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  66. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  67. #define bswap_64(value) \
  68. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  69. << 32) | \
  70. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  71. #endif
  72. #endif /* !defined(__GLXBYTEORDER_H__) */
  73. #ifdef HAVE_SYSLOG_H
  74. #include <syslog.h>
  75. #else
  76. enum {
  77. LOG_ERR,
  78. LOG_WARNING,
  79. LOG_INFO,
  80. LOG_DEBUG,
  81. };
  82. #endif
  83. #undef unlikely
  84. #undef likely
  85. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  86. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  87. #define likely(expr) (__builtin_expect(!!(expr), 1))
  88. #else
  89. #define unlikely(expr) (expr)
  90. #define likely(expr) (expr)
  91. #endif
  92. #if defined(__i386__)
  93. #define WANT_CRYPTOPP_ASM32
  94. #endif
  95. #ifndef ARRAY_SIZE
  96. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  97. #endif
  98. struct cgpu_info {
  99. int is_gpu;
  100. int cpu_gpu;
  101. int accepted;
  102. int rejected;
  103. int hw_errors;
  104. };
  105. struct thr_info {
  106. int id;
  107. pthread_t pth;
  108. struct thread_q *q;
  109. struct cgpu_info *cgpu;
  110. };
  111. static inline uint32_t swab32(uint32_t v)
  112. {
  113. #ifdef WANT_BUILTIN_BSWAP
  114. return __builtin_bswap32(v);
  115. #else
  116. return bswap_32(v);
  117. #endif
  118. }
  119. static inline void swap256(void *dest_p, const void *src_p)
  120. {
  121. uint32_t *dest = dest_p;
  122. const uint32_t *src = src_p;
  123. dest[0] = src[7];
  124. dest[1] = src[6];
  125. dest[2] = src[5];
  126. dest[3] = src[4];
  127. dest[4] = src[3];
  128. dest[5] = src[2];
  129. dest[6] = src[1];
  130. dest[7] = src[0];
  131. }
  132. extern bool opt_debug;
  133. extern bool opt_protocol;
  134. extern const uint32_t sha256_init_state[];
  135. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  136. const char *rpc_req, bool, bool);
  137. extern char *bin2hex(const unsigned char *p, size_t len);
  138. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  139. extern unsigned int ScanHash_4WaySSE2(int, const unsigned char *pmidstate,
  140. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  141. const unsigned char *ptarget,
  142. uint32_t max_nonce, unsigned long *nHashesDone);
  143. extern unsigned int scanhash_sse2_amd64(int, const unsigned char *pmidstate,
  144. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  145. const unsigned char *ptarget,
  146. uint32_t max_nonce, unsigned long *nHashesDone);
  147. extern bool scanhash_via(int, unsigned char *data_inout,
  148. const unsigned char *target,
  149. uint32_t max_nonce, unsigned long *hashes_done);
  150. extern bool scanhash_c(int, const unsigned char *midstate, unsigned char *data,
  151. unsigned char *hash1, unsigned char *hash,
  152. const unsigned char *target,
  153. uint32_t max_nonce, unsigned long *hashes_done);
  154. extern bool scanhash_cryptopp(int, const unsigned char *midstate,unsigned char *data,
  155. unsigned char *hash1, unsigned char *hash,
  156. const unsigned char *target,
  157. uint32_t max_nonce, unsigned long *hashes_done);
  158. extern bool scanhash_asm32(int, const unsigned char *midstate,unsigned char *data,
  159. unsigned char *hash1, unsigned char *hash,
  160. const unsigned char *target,
  161. uint32_t max_nonce, unsigned long *hashes_done);
  162. extern int scanhash_sse2_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  163. unsigned char *phash1, unsigned char *phash,
  164. const unsigned char *ptarget,
  165. uint32_t max_nonce, unsigned long *nHashesDone);
  166. extern int
  167. timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);
  168. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  169. extern int opt_scantime;
  170. extern bool want_longpoll;
  171. extern bool have_longpoll;
  172. struct thread_q;
  173. struct work_restart {
  174. volatile unsigned long restart;
  175. char padding[128 - sizeof(unsigned long)];
  176. };
  177. extern pthread_mutex_t time_lock;
  178. extern int hw_errors;
  179. extern bool use_syslog;
  180. extern struct thr_info *thr_info;
  181. extern int longpoll_thr_id;
  182. extern struct work_restart *work_restart;
  183. typedef struct {
  184. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  185. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  186. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  187. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  188. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  189. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  190. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  191. cl_uint W16; cl_uint W17; cl_uint W2;
  192. cl_uint PreVal4; cl_uint T1;
  193. } dev_blk_ctx;
  194. struct work {
  195. unsigned char data[128];
  196. unsigned char hash1[64];
  197. unsigned char midstate[32];
  198. unsigned char target[32];
  199. unsigned char hash[32];
  200. uint32_t output[1];
  201. uint32_t res_nonce;
  202. uint32_t valid;
  203. dev_blk_ctx blk;
  204. int thr_id;
  205. };
  206. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  207. extern void applog(int prio, const char *fmt, ...);
  208. extern struct thread_q *tq_new(void);
  209. extern void tq_free(struct thread_q *tq);
  210. extern bool tq_push(struct thread_q *tq, void *data);
  211. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  212. extern void tq_freeze(struct thread_q *tq);
  213. extern void tq_thaw(struct thread_q *tq);
  214. #endif /* __MINER_H__ */