scanhash micro-optimizations * don't bother returning nonce, we only need success/fail boolean * don't needlessly read nonce pointer data, for each loop
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
diff --git a/cpu-miner.c b/cpu-miner.c
index a2c9569..7685fdb 100644
--- a/cpu-miner.c
+++ b/cpu-miner.c
@@ -334,16 +334,15 @@ static const uint32_t init_state[8] = {
};
/* suspiciously similar to ScanHash* from bitcoin */
-static uint32_t scanhash(unsigned char *midstate, unsigned char *data,
- unsigned char *hash1, unsigned char *hash)
+static bool scanhash(unsigned char *midstate, unsigned char *data,
+ unsigned char *hash1, unsigned char *hash)
{
uint32_t *hash32 = (uint32_t *) hash;
uint32_t *nonce = (uint32_t *)(data + 12);
- uint32_t n;
+ uint32_t n = 0;
unsigned long stat_ctr = 0;
while (1) {
- n = *nonce;
n++;
*nonce = n;
@@ -359,7 +358,7 @@ static uint32_t scanhash(unsigned char *midstate, unsigned char *data,
hexstr);
free(hexstr);
- return n;
+ return true;
}
stat_ctr++;
@@ -373,7 +372,7 @@ static uint32_t scanhash(unsigned char *midstate, unsigned char *data,
if (opt_debug)
fprintf(stderr, "DBG: end of nonce range\n");
- return 0;
+ return false;
}
}
}
@@ -426,9 +425,8 @@ static void *miner_thread(void *dummy)
"{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
while (1) {
- json_t *val;
struct work work __attribute__((aligned(128)));
- uint32_t nonce;
+ json_t *val;
bool rc;
/* obtain new work from bitcoin */
@@ -448,11 +446,11 @@ static void *miner_thread(void *dummy)
json_decref(val);
/* scan nonces for a proof-of-work hash */
- nonce = scanhash(work.midstate, work.data + 64,
- work.hash1, work.hash);
+ rc = scanhash(work.midstate, work.data + 64,
+ work.hash1, work.hash);
/* if nonce found, submit work */
- if (nonce)
+ if (rc)
submit_work(&work);
}