miner.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #ifndef __MINER_H__
  2. #define __MINER_H__
  3. #include "cpuminer-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 thr_info {
  99. int id;
  100. pthread_t pth;
  101. struct thread_q *q;
  102. };
  103. static inline uint32_t swab32(uint32_t v)
  104. {
  105. #ifdef WANT_BUILTIN_BSWAP
  106. return __builtin_bswap32(v);
  107. #else
  108. return bswap_32(v);
  109. #endif
  110. }
  111. static inline void swap256(void *dest_p, const void *src_p)
  112. {
  113. uint32_t *dest = dest_p;
  114. const uint32_t *src = src_p;
  115. dest[0] = src[7];
  116. dest[1] = src[6];
  117. dest[2] = src[5];
  118. dest[3] = src[4];
  119. dest[4] = src[3];
  120. dest[5] = src[2];
  121. dest[6] = src[1];
  122. dest[7] = src[0];
  123. }
  124. extern bool opt_debug;
  125. extern bool opt_protocol;
  126. extern const uint32_t sha256_init_state[];
  127. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  128. const char *rpc_req, bool, bool);
  129. extern char *bin2hex(const unsigned char *p, size_t len);
  130. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  131. extern unsigned int ScanHash_4WaySSE2(int, const unsigned char *pmidstate,
  132. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  133. const unsigned char *ptarget,
  134. uint32_t max_nonce, unsigned long *nHashesDone);
  135. extern unsigned int scanhash_sse2_amd64(int, const unsigned char *pmidstate,
  136. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  137. const unsigned char *ptarget,
  138. uint32_t max_nonce, unsigned long *nHashesDone);
  139. extern bool scanhash_via(int, unsigned char *data_inout,
  140. const unsigned char *target,
  141. uint32_t max_nonce, unsigned long *hashes_done);
  142. extern bool scanhash_c(int, const unsigned char *midstate, unsigned char *data,
  143. unsigned char *hash1, unsigned char *hash,
  144. const unsigned char *target,
  145. uint32_t max_nonce, unsigned long *hashes_done);
  146. extern bool scanhash_cryptopp(int, const unsigned char *midstate,unsigned char *data,
  147. unsigned char *hash1, unsigned char *hash,
  148. const unsigned char *target,
  149. uint32_t max_nonce, unsigned long *hashes_done);
  150. extern bool scanhash_asm32(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 int scanhash_sse2_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  155. unsigned char *phash1, unsigned char *phash,
  156. const unsigned char *ptarget,
  157. uint32_t max_nonce, unsigned long *nHashesDone);
  158. extern int
  159. timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);
  160. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  161. extern int opt_scantime;
  162. extern bool want_longpoll;
  163. extern bool have_longpoll;
  164. struct thread_q;
  165. struct work_restart {
  166. volatile unsigned long restart;
  167. char padding[128 - sizeof(unsigned long)];
  168. };
  169. extern pthread_mutex_t time_lock;
  170. extern bool use_syslog;
  171. extern struct thr_info *thr_info;
  172. extern int longpoll_thr_id;
  173. extern struct work_restart *work_restart;
  174. typedef struct {
  175. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  176. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  177. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  178. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  179. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  180. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  181. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  182. cl_uint W16; cl_uint W17; cl_uint W2;
  183. cl_uint PreVal4; cl_uint T1;
  184. } dev_blk_ctx;
  185. struct work {
  186. unsigned char data[128];
  187. unsigned char hash1[64];
  188. unsigned char midstate[32];
  189. unsigned char target[32];
  190. unsigned char hash[32];
  191. uint32_t output[1];
  192. uint32_t res_nonce;
  193. uint32_t valid;
  194. dev_blk_ctx blk;
  195. };
  196. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  197. extern void applog(int prio, const char *fmt, ...);
  198. extern struct thread_q *tq_new(void);
  199. extern void tq_free(struct thread_q *tq);
  200. extern bool tq_push(struct thread_q *tq, void *data);
  201. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  202. extern void tq_freeze(struct thread_q *tq);
  203. extern void tq_thaw(struct thread_q *tq);
  204. #endif /* __MINER_H__ */