Move sha256_generic into its own .o build output. Const-ify midstate param.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
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)
{