Browse Source

Merge commit '8da59e5' into bfgminer

Luke Dashjr 13 years ago
parent
commit
2f22090b38
1 changed files with 20 additions and 63 deletions
  1. 20 63
      miner.c

+ 20 - 63
miner.c

@@ -2546,21 +2546,13 @@ share_result(json_t *val, json_t *res, json_t *err, const struct work *work,
 
 
 static const uint64_t diffone = 0xFFFF000000000000ull;
 static const uint64_t diffone = 0xFFFF000000000000ull;
 
 
+static double target_diff(const unsigned char *target);
+
 static uint64_t share_diff(const struct work *work)
 static uint64_t share_diff(const struct work *work)
 {
 {
-	uint64_t *data64, d64;
-	char rhash[32];
 	uint64_t ret;
 	uint64_t ret;
 
 
-	swab256(rhash, work->hash);
-	if (opt_scrypt)
-		data64 = (uint64_t *)(rhash + 2);
-	else
-		data64 = (uint64_t *)(rhash + 4);
-	d64 = be64toh(*data64);
-	if (unlikely(!d64))
-		d64 = 1;
-	ret = diffone / d64;
+	ret = target_diff(work->hash);
 	mutex_lock(&control_lock);
 	mutex_lock(&control_lock);
 	if (ret > best_diff) {
 	if (ret > best_diff) {
 		best_diff = ret;
 		best_diff = ret;
@@ -2770,6 +2762,17 @@ static inline struct pool *select_pool(bool lagging)
 
 
 static double DIFFEXACTONE = 26959946667150639794667015087019630673637144422540572481103610249215.0;
 static double DIFFEXACTONE = 26959946667150639794667015087019630673637144422540572481103610249215.0;
 
 
+static double target_diff(const unsigned char *target)
+{
+	double targ = 0;
+	signed int i;
+
+	for (i = 31; i >= 0; --i)
+		targ = (targ * 0x100) + target[i];
+
+	return DIFFEXACTONE / (targ ?: 1);
+}
+
 /*
 /*
  * Calculate the work share difficulty
  * Calculate the work share difficulty
  */
  */
@@ -2778,26 +2781,8 @@ static void calc_diff(struct work *work, int known)
 	struct cgminer_pool_stats *pool_stats = &(work->pool->cgminer_pool_stats);
 	struct cgminer_pool_stats *pool_stats = &(work->pool->cgminer_pool_stats);
 	double difficulty;
 	double difficulty;
 
 
-	if (opt_scrypt) {
-		uint64_t *data64, d64;
-		char rtarget[32];
-
-		swab256(rtarget, work->target);
-		data64 = (uint64_t *)(rtarget + 2);
-		d64 = be64toh(*data64);
-		if (unlikely(!d64))
-			d64 = 1;
-		work->work_difficulty = diffone / d64;
-	} else if (!known) {
-		double targ = 0;
-		int i;
-
-		for (i = 31; i >= 0; i--) {
-			targ *= 256;
-			targ += work->target[i];
-		}
-
-		work->work_difficulty = DIFFEXACTONE / (targ ? : DIFFEXACTONE);
+	if (!known) {
+		work->work_difficulty = target_diff(work->target);
 	} else
 	} else
 		work->work_difficulty = known;
 		work->work_difficulty = known;
 	difficulty = work->work_difficulty;
 	difficulty = work->work_difficulty;
@@ -4218,40 +4203,12 @@ static int block_sort(struct block *blocka, struct block *blockb)
 
 
 static void set_blockdiff(const struct work *work)
 static void set_blockdiff(const struct work *work)
 {
 {
-	uint64_t *data64, d64, diff64;
-	uint32_t diffhash[8];
-	uint32_t difficulty;
-	uint32_t diffbytes;
-	uint32_t diffvalue;
-	char rhash[32];
-	int diffshift;
-
-	difficulty = swab32(*((uint32_t *)(work->data + 72)));
-
-	diffbytes = ((difficulty >> 24) & 0xff) - 3;
-	diffvalue = difficulty & 0x00ffffff;
-
-	diffshift = (diffbytes % 4) * 8;
-	if (diffshift == 0) {
-		diffshift = 32;
-		diffbytes--;
-	}
-
-	memset(diffhash, 0, 32);
-	diffhash[(diffbytes >> 2) + 1] = diffvalue >> (32 - diffshift);
-	diffhash[diffbytes >> 2] = diffvalue << diffshift;
-
-	swab256(rhash, diffhash);
+	unsigned char target[32];
+	uint64_t diff64;
 
 
-	if (opt_scrypt)
-		data64 = (uint64_t *)(rhash + 2);
-	else
-		data64 = (uint64_t *)(rhash + 4);
-	d64 = be64toh(*data64);
-	if (unlikely(!d64))
-		d64 = 1;
+	real_block_target(target, work->data);
+	diff64 = target_diff(target);
 
 
-	diff64 = diffone / d64;
 	suffix_string(diff64, block_diff, 0);
 	suffix_string(diff64, block_diff, 0);
 }
 }