miner.h 6.6 KB

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