Commit 339ddf4d757e17687d18d59e8ded57a71f186121

Jeff Garzik 2010-11-27T04:31:32

Move sha256_generic into its own .o build output. Const-ify midstate param.

diff --git a/Makefile.am b/Makefile.am
index b3ad6d6..29dda41 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -11,9 +11,7 @@ INCLUDES	= $(PTHREAD_FLAGS) -fno-strict-aliasing $(JANSSON_INCLUDES)
 
 bin_PROGRAMS	= minerd
 
-EXTRA_DIST	= sha256_generic.c
-
-minerd_SOURCES	= cpu-miner.c sha256_4way.c sha256_via.c	\
+minerd_SOURCES	= cpu-miner.c sha256_generic.c sha256_4way.c sha256_via.c \
 		  util.c miner.h compat.h
 minerd_LDFLAGS	= $(PTHREAD_FLAGS)
 minerd_LDADD	= @LIBCURL@ @JANSSON_LIBS@ @PTHREAD_LIBS@
diff --git a/cpu-miner.c b/cpu-miner.c
index 8243718..05766e9 100644
--- a/cpu-miner.c
+++ b/cpu-miner.c
@@ -119,8 +119,6 @@ struct work {
 	unsigned char	hash[32];
 };
 
-#include "sha256_generic.c"
-
 static bool jobj_binary(const json_t *obj, const char *key,
 			void *buf, size_t buflen)
 {
@@ -231,58 +229,6 @@ static void hashmeter(int thr_id, struct timeval *tv_start,
 	       khashes / secs);
 }
 
-static void runhash(void *state, void *input, const void *init)
-{
-	memcpy(state, init, 32);
-	sha256_transform(state, input);
-}
-
-const uint32_t sha256_init_state[8] = {
-	0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
-	0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
-};
-
-/* suspiciously similar to ScanHash* from bitcoin */
-static bool scanhash(unsigned char *midstate, unsigned char *data,
-		     unsigned char *hash1, unsigned char *hash,
-		     unsigned long *hashes_done)
-{
-	uint32_t *hash32 = (uint32_t *) hash;
-	uint32_t *nonce = (uint32_t *)(data + 12);
-	uint32_t n = 0;
-	unsigned long stat_ctr = 0;
-
-	while (1) {
-		n++;
-		*nonce = n;
-
-		runhash(hash1, data, midstate);
-		runhash(hash, hash1, sha256_init_state);
-
-		stat_ctr++;
-
-		if (hash32[7] == 0) {
-			char *hexstr;
-
-			hexstr = bin2hex(hash, 32);
-			fprintf(stderr,
-				"DBG: found zeroes in hash:\n%s\n",
-				hexstr);
-			free(hexstr);
-
-			*hashes_done = stat_ctr;
-			return true;
-		}
-
-		if ((n & 0xffffff) == 0) {
-			if (opt_debug)
-				fprintf(stderr, "DBG: end of nonce range\n");
-			*hashes_done = stat_ctr;
-			return false;
-		}
-	}
-}
-
 static void *miner_thread(void *thr_id_int)
 {
 	int thr_id = (unsigned long) thr_id_int;
@@ -318,8 +264,8 @@ static void *miner_thread(void *thr_id_int)
 		/* scan nonces for a proof-of-work hash */
 		switch (opt_algo) {
 		case ALGO_C:
-			rc = scanhash(work.midstate, work.data + 64,
-				      work.hash1, work.hash, &hashes_done);
+			rc = scanhash_c(work.midstate, work.data + 64,
+				        work.hash1, work.hash, &hashes_done);
 			break;
 
 #ifdef WANT_SSE2_4WAY
diff --git a/miner.h b/miner.h
index ea5379f..cda08ee 100644
--- a/miner.h
+++ b/miner.h
@@ -26,13 +26,18 @@ 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,
+extern unsigned int ScanHash_4WaySSE2(const unsigned char *pmidstate,
 	unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
 	unsigned long *nHashesDone);
 
-extern bool scanhash_via(unsigned char *midstate, const unsigned char *data_in,
+extern bool scanhash_via(const unsigned char *midstate, const unsigned char *data_in,
 	          unsigned char *hash1, unsigned char *hash,
 	          unsigned long *hashes_done);
+
+extern bool scanhash_c(const unsigned char *midstate, unsigned char *data,
+	      unsigned char *hash1, unsigned char *hash,
+	      unsigned long *hashes_done);
+
 extern int
 timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);
 
diff --git a/sha256_4way.c b/sha256_4way.c
index f621136..f91bb0f 100644
--- a/sha256_4way.c
+++ b/sha256_4way.c
@@ -97,7 +97,7 @@ 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 int ScanHash_4WaySSE2(const unsigned char *pmidstate, unsigned char *pdata,
 	unsigned char *phash1, unsigned char *phash, unsigned long *nHashesDone)
 {
     unsigned int *nNonce_p = (unsigned int*)(pdata + 12);
diff --git a/sha256_generic.c b/sha256_generic.c
index d622353..e778113 100644
--- a/sha256_generic.c
+++ b/sha256_generic.c
@@ -19,6 +19,10 @@
  */
 
 #include <stdint.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include "miner.h"
 
 typedef uint32_t u32;
 typedef uint8_t u8;
@@ -221,3 +225,55 @@ static void sha256_transform(u32 *state, const u8 *input)
 #endif
 }
 
+static void runhash(void *state, const void *input, const void *init)
+{
+	memcpy(state, init, 32);
+	sha256_transform(state, input);
+}
+
+const uint32_t sha256_init_state[8] = {
+	0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
+	0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
+};
+
+/* suspiciously similar to ScanHash* from bitcoin */
+bool scanhash_c(const unsigned char *midstate, unsigned char *data,
+	        unsigned char *hash1, unsigned char *hash,
+	        unsigned long *hashes_done)
+{
+	uint32_t *hash32 = (uint32_t *) hash;
+	uint32_t *nonce = (uint32_t *)(data + 12);
+	uint32_t n = 0;
+	unsigned long stat_ctr = 0;
+
+	while (1) {
+		n++;
+		*nonce = n;
+
+		runhash(hash1, data, midstate);
+		runhash(hash, hash1, sha256_init_state);
+
+		stat_ctr++;
+
+		if (hash32[7] == 0) {
+			char *hexstr;
+
+			hexstr = bin2hex(hash, 32);
+			fprintf(stderr,
+				"DBG: found zeroes in hash:\n%s\n",
+				hexstr);
+			free(hexstr);
+
+			*hashes_done = stat_ctr;
+			return true;
+		}
+
+		if ((n & 0xffffff) == 0) {
+			if (opt_debug)
+				fprintf(stderr, "DBG: end of nonce range\n");
+			*hashes_done = stat_ctr;
+			return false;
+		}
+	}
+}
+
diff --git a/sha256_via.c b/sha256_via.c
index 8becfd7..d9abc88 100644
--- a/sha256_via.c
+++ b/sha256_via.c
@@ -28,7 +28,7 @@ static void via_sha256(void *hash, void *buf, unsigned len)
 		     :"memory");
 }
 
-bool scanhash_via(unsigned char *midstate, const unsigned char *data_in,
+bool scanhash_via(const unsigned char *midstate, const unsigned char *data_in,
 	          unsigned char *hash1, unsigned char *hash,
 	          unsigned long *hashes_done)
 {