diff --git a/Makefile.am b/Makefile.am index a7b09fa..2c149d0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -13,7 +13,7 @@ bin_PROGRAMS = minerd EXTRA_DIST = sha256_generic.c -minerd_SOURCES = util.c cpu-miner.c miner.h compat.h +minerd_SOURCES = cpu-miner.c sha256_4way.c util.c miner.h compat.h minerd_LDFLAGS = $(PTHREAD_FLAGS) minerd_LDADD = @LIBCURL@ @JANSSON_LIBS@ @PTHREAD_LIBS@ diff --git a/README b/README index 40dafb1..01455c1 100644 --- a/README +++ b/README @@ -15,7 +15,7 @@ Basic *nix build instructions: Basic WIN32 build instructions (on Fedora 13; requires mingw32): ./autogen.sh - mingw32-configure + MINGW32_CFLAGS="-O2 -Wall -msse2 -g" mingw32-configure make ./mknsis.sh diff --git a/cpu-miner.c b/cpu-miner.c index 45e4e55..052c3fe 100644 --- a/cpu-miner.c +++ b/cpu-miner.c @@ -36,10 +36,16 @@ enum { STAT_CTR_INTERVAL = 10000000, }; +enum sha256_algos { + ALGO_C, + ALGO_4WAY +}; + static bool opt_debug; bool opt_protocol = false; static bool program_running = true; static const bool opt_time = true; +static enum sha256_algos opt_algo = ALGO_C; static int opt_n_threads = 1; static pthread_mutex_t stats_mutex = PTHREAD_MUTEX_INITIALIZER; static uint64_t hash_ctr; @@ -56,6 +62,14 @@ static struct option_help options_help[] = { { "help", "(-h) Display this help text" }, + { "algo", + "(-a) Specify sha256 implementation:\n" + "\tc\t\tLinux kernel sha256, implemented in C" +#ifdef __SSE__ + "\n\t4way\t\ttcatm's 4-way SSE2 implementation" +#endif + }, + { "debug", "(-D) Enable debug output" }, @@ -76,6 +90,7 @@ static struct option_help options_help[] = { static struct option options[] = { { "help", 0, NULL, 'h' }, + { "algo", 1, NULL, 'a' }, { "debug", 0, NULL, 'D' }, { "protocol-dump", 0, NULL, 'P' }, { "threads", 1, NULL, 't' }, @@ -276,8 +291,19 @@ static void *miner_thread(void *dummy) json_decref(val); /* scan nonces for a proof-of-work hash */ - rc = scanhash(work.midstate, work.data + 64, - work.hash1, work.hash); + if (opt_algo == ALGO_C) + rc = scanhash(work.midstate, work.data + 64, + work.hash1, work.hash); +#ifdef __SSE__ + else { + unsigned int hashesdone = 0; + unsigned int rc4 = + ScanHash_4WaySSE2(work.midstate, work.data + 64, + work.hash1, work.hash, + &hashesdone); + rc = (rc4 == -1) ? false : true; + } +#endif /* if nonce found, submit work */ if (rc) @@ -307,6 +333,16 @@ static void parse_arg (int key, char *arg) int v; switch(key) { + case 'a': + if (!strcmp(arg, "c")) + opt_algo = ALGO_C; +#ifdef __SSE__ + else if (!strcmp(arg, "4way")) + opt_algo = ALGO_4WAY; +#endif + else + show_usage(); + break; case 'D': opt_debug = true; break; @@ -343,7 +379,7 @@ static void parse_cmdline(int argc, char *argv[]) int key; while (1) { - key = getopt_long(argc, argv, "DPt:h?", options, NULL); + key = getopt_long(argc, argv, "a:DPt:h?", options, NULL); if (key < 0) break; diff --git a/miner.h b/miner.h index dc84c1c..b4cc939 100644 --- a/miner.h +++ b/miner.h @@ -14,4 +14,8 @@ extern json_t *json_rpc_call(const char *url, const char *userpass, extern char *bin2hex(unsigned char *p, size_t len); extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len); +extern unsigned int ScanHash_4WaySSE2(unsigned char *pmidstate, + unsigned char *pdata, unsigned char *phash1, unsigned char *phash, + unsigned int *nHashesDone); + #endif /* __MINER_H__ */ diff --git a/sha256_4way.c b/sha256_4way.c new file mode 100644 index 0000000..56132ff --- /dev/null +++ b/sha256_4way.c @@ -0,0 +1,469 @@ +// Copyright (c) 2010 Satoshi Nakamoto +// Distributed under the MIT/X11 software license, see the accompanying +// file license.txt or http://www.opensource.org/licenses/mit-license.php. + +// tcatm's 4-way 128-bit SSE2 SHA-256 + +#ifdef __SSE__ + +#include +#include + +#include +#include +#include + +#define NPAR 32 + +static void DoubleBlockSHA256(const void* pin, void* pout, const void* pinit, unsigned int hash[8][NPAR], const void* init2); + +static const unsigned int sha256_consts[] = { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, /* 0 */ + 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, /* 8 */ + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, /* 16 */ + 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, /* 24 */ + 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, /* 32 */ + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, /* 40 */ + 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, /* 48 */ + 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, /* 56 */ + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 +}; + + +static inline __m128i Ch(const __m128i b, const __m128i c, const __m128i d) { + return (b & c) ^ (~b & d); +} + +static inline __m128i Maj(const __m128i b, const __m128i c, const __m128i d) { + return (b & c) ^ (b & d) ^ (c & d); +} + +static inline __m128i ROTR(__m128i x, const int n) { + return _mm_srli_epi32(x, n) | _mm_slli_epi32(x, 32 - n); +} + +static inline __m128i SHR(__m128i x, const int n) { + return _mm_srli_epi32(x, n); +} + +/* SHA256 Functions */ +#define BIGSIGMA0_256(x) (ROTR((x), 2) ^ ROTR((x), 13) ^ ROTR((x), 22)) +#define BIGSIGMA1_256(x) (ROTR((x), 6) ^ ROTR((x), 11) ^ ROTR((x), 25)) +#define SIGMA0_256(x) (ROTR((x), 7) ^ ROTR((x), 18) ^ SHR((x), 3)) +#define SIGMA1_256(x) (ROTR((x), 17) ^ ROTR((x), 19) ^ SHR((x), 10)) + +static inline unsigned int store32(const __m128i x, int i) { + union { unsigned int ret[4]; __m128i x; } box; + box.x = x; + return box.ret[i]; +} + +static inline void store_epi32(const __m128i x, unsigned int *x0, unsigned int *x1, unsigned int *x2, unsigned int *x3) { + union { unsigned int ret[4]; __m128i x; } box; + box.x = x; + *x0 = box.ret[3]; *x1 = box.ret[2]; *x2 = box.ret[1]; *x3 = box.ret[0]; +} + +#define add4(x0, x1, x2, x3) _mm_add_epi32(_mm_add_epi32(_mm_add_epi32(x0, x1), x2), x3) +#define add5(x0, x1, x2, x3, x4) _mm_add_epi32(add4(x0, x1, x2, x3), x4) + +#define SHA256ROUND(a, b, c, d, e, f, g, h, i, w) \ + T1 = add5(h, BIGSIGMA1_256(e), Ch(e, f, g), _mm_set1_epi32(sha256_consts[i]), w); \ +d = _mm_add_epi32(d, T1); \ +h = _mm_add_epi32(T1, _mm_add_epi32(BIGSIGMA0_256(a), Maj(a, b, c))); + +static inline void dumpreg(__m128i x, char *msg) { + union { unsigned int ret[4]; __m128i x; } box; + box.x = x ; + printf("%s %08x %08x %08x %08x\n", msg, box.ret[0], box.ret[1], box.ret[2], box.ret[3]); +} + +#if 1 +#define dumpstate(i) printf("%s: %08x %08x %08x %08x %08x %08x %08x %08x %08x\n", \ + __func__, store32(w0, i), store32(a, i), store32(b, i), store32(c, i), store32(d, i), store32(e, i), store32(f, i), store32(g, i), store32(h, i)); +#else +#define dumpstate() +#endif + +static const unsigned int pSHA256InitState[8] = +{0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; + + +unsigned int ScanHash_4WaySSE2(unsigned char *pmidstate, unsigned char *pdata, + unsigned char *phash1, unsigned char *phash, unsigned int *nHashesDone) +{ + unsigned int *nNonce_p = (unsigned int*)(pdata + 12); + unsigned int nonce = 0; + + for (;;) + { + unsigned int thash[9][NPAR] __attribute__((aligned(128))); + int j; + + nonce += NPAR; + *nNonce_p = nonce; + + DoubleBlockSHA256(pdata, phash1, pmidstate, thash, pSHA256InitState); + + for (j = 0; j < NPAR; j++) + { + if (thash[7][j] == 0) + { + int i; + + for (i = 0; i < 32/4; i++) + ((unsigned int*)phash)[i] = thash[i][j]; + return nonce + j; + } + } + + if ((nonce & 0xffff) == 0) + { + *nHashesDone = 0xffff+1; + return -1; + } + } +} + + +static void DoubleBlockSHA256(const void* pin, void* pad, const void *pre, unsigned int thash[9][NPAR], const void *init) +{ + unsigned int* In = (unsigned int*)pin; + unsigned int* Pad = (unsigned int*)pad; + unsigned int* hPre = (unsigned int*)pre; + unsigned int* hInit = (unsigned int*)init; + unsigned int /* i, j, */ k; + + /* vectors used in calculation */ + __m128i w0, w1, w2, w3, w4, w5, w6, w7; + __m128i w8, w9, w10, w11, w12, w13, w14, w15; + __m128i T1; + __m128i a, b, c, d, e, f, g, h; + __m128i nonce; + + /* nonce offset for vector */ + __m128i offset = _mm_set_epi32(0x00000003, 0x00000002, 0x00000001, 0x00000000); + + + for(k = 0; k