Browse Source

Merge branch 'bigendian' into bfgminer

Conflicts:
	miner.c
Luke Dashjr 13 years ago
parent
commit
a8d26db4c5
8 changed files with 48 additions and 73 deletions
  1. 1 3
      driver-bitforce.c
  2. 1 3
      driver-icarus.c
  3. 8 21
      driver-ztex.c
  4. 11 31
      miner.c
  5. 18 0
      miner.h
  6. 1 3
      sha256_cryptopp.c
  7. 3 10
      sha256_via.c
  8. 5 2
      uthash.h

+ 1 - 3
driver-bitforce.c

@@ -545,9 +545,7 @@ static int64_t bitforce_get_result(struct thr_info *thr, struct work *work)
 
 
 	while (1) {
 	while (1) {
 		hex2bin((void*)&nonce, pnoncebuf, 4);
 		hex2bin((void*)&nonce, pnoncebuf, 4);
-#ifndef __BIG_ENDIAN__
-		nonce = swab32(nonce);
-#endif
+		nonce = be32toh(nonce);
 		if (unlikely(bitforce->nonce_range && (nonce >= work->blk.nonce ||
 		if (unlikely(bitforce->nonce_range && (nonce >= work->blk.nonce ||
 			(work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
 			(work->blk.nonce > 0 && nonce < work->blk.nonce - bitforce->nonces - 1)))) {
 				applog(LOG_WARNING, "BFL%i: Disabling broken nonce range support", bitforce->device_id);
 				applog(LOG_WARNING, "BFL%i: Disabling broken nonce range support", bitforce->device_id);

+ 1 - 3
driver-icarus.c

@@ -672,9 +672,7 @@ static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
 		return estimate_hashes;
 		return estimate_hashes;
 	}
 	}
 
 
-#if !defined (__BIG_ENDIAN__) && !defined(MIPSEB)
-	nonce = swab32(nonce);
-#endif
+	nonce = be32toh(nonce);
 
 
 	submit_nonce(thr, &state->last_work, nonce);
 	submit_nonce(thr, &state->last_work, nonce);
 	memcpy(&state->last_work, work, sizeof(state->last_work));
 	memcpy(&state->last_work, work, sizeof(state->last_work));

+ 8 - 21
driver-ztex.c

@@ -23,6 +23,7 @@
  *   You should have received a copy of the GNU General Public License
  *   You should have received a copy of the GNU General Public License
  *   along with this program; if not, see http://www.gnu.org/licenses/.
  *   along with this program; if not, see http://www.gnu.org/licenses/.
 **/
 **/
+
 #include <unistd.h>
 #include <unistd.h>
 #include <sha2.h>
 #include <sha2.h>
 #include "miner.h"
 #include "miner.h"
@@ -156,28 +157,20 @@ static bool ztex_checkNonce(struct libztex_device *ztex,
 	unsigned char hash1[32];
 	unsigned char hash1[32];
 	unsigned char hash2[32];
 	unsigned char hash2[32];
 	uint32_t *hash2_32 = (uint32_t *)hash2;
 	uint32_t *hash2_32 = (uint32_t *)hash2;
-	int i;
 
 
-#if defined(__BIGENDIAN__) || defined(MIPSEB)
-	hdata->nonce = swab32(hdata->nonce);
-	hdata->hash7 = swab32(hdata->hash7);
-#endif
+	hdata->nonce = le32toh(hdata->nonce);
+	hdata->hash7 = le32toh(hdata->hash7);
 
 
 	work->data[64 + 12 + 0] = (hdata->nonce >> 0) & 0xff;
 	work->data[64 + 12 + 0] = (hdata->nonce >> 0) & 0xff;
 	work->data[64 + 12 + 1] = (hdata->nonce >> 8) & 0xff;
 	work->data[64 + 12 + 1] = (hdata->nonce >> 8) & 0xff;
 	work->data[64 + 12 + 2] = (hdata->nonce >> 16) & 0xff;
 	work->data[64 + 12 + 2] = (hdata->nonce >> 16) & 0xff;
 	work->data[64 + 12 + 3] = (hdata->nonce >> 24) & 0xff;
 	work->data[64 + 12 + 3] = (hdata->nonce >> 24) & 0xff;
 
 
-	for (i = 0; i < 80 / 4; i++)
-		swap32[i] = swab32(data32[i]);
+	swap32yes(swap32, data32, 80 / 4);
 	
 	
 	sha2(swap, 80, hash1, false);
 	sha2(swap, 80, hash1, false);
 	sha2(hash1, 32, hash2, false);
 	sha2(hash1, 32, hash2, false);
-#if defined(__BIGENDIAN__) || defined(MIPSEB)
-	if (hash2_32[7] != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
-#else
-	if (swab32(hash2_32[7]) != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
-#endif
+	if (htobe32(hash2_32[7]) != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
 		ztex->errorCount[ztex->freqM] += 1.0 / ztex->numNonces;
 		ztex->errorCount[ztex->freqM] += 1.0 / ztex->numNonces;
 		applog(LOG_DEBUG, "%s: checkNonce failed for %0.8X", ztex->repr, hdata->nonce);
 		applog(LOG_DEBUG, "%s: checkNonce failed for %0.8X", ztex->repr, hdata->nonce);
 		return false;
 		return false;
@@ -275,9 +268,7 @@ static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
  
  
 		for (i = 0; i < ztex->numNonces; i++) {
 		for (i = 0; i < ztex->numNonces; i++) {
 			nonce = hdata[i].nonce;
 			nonce = hdata[i].nonce;
-#if defined(__BIGENDIAN__) || defined(MIPSEB)
-			nonce = swab32(nonce);
-#endif
+			nonce = le32toh(nonce);
 			if (nonce > noncecnt)
 			if (nonce > noncecnt)
 				noncecnt = nonce;
 				noncecnt = nonce;
 			if (((nonce & 0x7fffffff) >> 4) < ((lastnonce[i] & 0x7fffffff) >> 4)) {
 			if (((nonce & 0x7fffffff) >> 4) < ((lastnonce[i] & 0x7fffffff) >> 4)) {
@@ -285,9 +276,7 @@ static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
 				overflow = true;
 				overflow = true;
 			} else
 			} else
 				lastnonce[i] = nonce;
 				lastnonce[i] = nonce;
-#if !(defined(__BIGENDIAN__) || defined(MIPSEB))
-			nonce = swab32(nonce);
-#endif
+			nonce = htole32(nonce);
 			if (!ztex_checkNonce(ztex, work, &hdata[i])) {
 			if (!ztex_checkNonce(ztex, work, &hdata[i])) {
 				thr->cgpu->hw_errors++;
 				thr->cgpu->hw_errors++;
 				continue;
 				continue;
@@ -307,9 +296,7 @@ static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
 						backlog[backlog_p++] = nonce;
 						backlog[backlog_p++] = nonce;
 						if (backlog_p >= backlog_max)
 						if (backlog_p >= backlog_max)
 							backlog_p = 0;
 							backlog_p = 0;
-#if defined(__BIGENDIAN__) || defined(MIPSEB)
-						nonce = swab32(nonce);
-#endif
+						nonce = le32toh(nonce);
 						work->blk.nonce = 0xffffffff;
 						work->blk.nonce = 0xffffffff;
 						rv = submit_nonce(thr, work, nonce);
 						rv = submit_nonce(thr, work, nonce);
 						applog(LOG_DEBUG, "%s: submitted %0.8x %d", ztex->repr, nonce, rv);
 						applog(LOG_DEBUG, "%s: submitted %0.8x %d", ztex->repr, nonce, rv);

+ 11 - 31
miner.c

@@ -15,6 +15,7 @@
 #include <curses.h>
 #include <curses.h>
 #endif
 #endif
 
 
+#include <endian.h>
 #include <stdio.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdlib.h>
 #include <string.h>
 #include <string.h>
@@ -1287,19 +1288,13 @@ static void calc_midstate(struct work *work)
 		unsigned char c[64];
 		unsigned char c[64];
 		uint32_t i[16];
 		uint32_t i[16];
 	} data;
 	} data;
-	int swapcounter;
 
 
-	for (swapcounter = 0; swapcounter < 16; swapcounter++)
-		data.i[swapcounter] = swab32(((uint32_t*) (work->data))[swapcounter]);
+	swap32yes(&data.i[0], work->data, 16);
 	sha2_context ctx;
 	sha2_context ctx;
 	sha2_starts( &ctx, 0 );
 	sha2_starts( &ctx, 0 );
 	sha2_update( &ctx, data.c, 64 );
 	sha2_update( &ctx, data.c, 64 );
 	memcpy(work->midstate, ctx.state, sizeof(work->midstate));
 	memcpy(work->midstate, ctx.state, sizeof(work->midstate));
-#if defined(__BIG_ENDIAN__) || defined(MIPSEB)
-	int i;
-	for (i = 0; i < 8; i++)
-		(((uint32_t*) (work->midstate))[i]) = swab32(((uint32_t*) (work->midstate))[i]);
-#endif
+	swap32tole(work->midstate, work->midstate, 8);
 }
 }
 
 
 static bool work_decode(const json_t *val, struct work *work)
 static bool work_decode(const json_t *val, struct work *work)
@@ -1821,13 +1816,12 @@ bool regeneratehash(const struct work *work)
 	int diffshift = 0;
 	int diffshift = 0;
 	int i;
 	int i;
 
 
-	for (i = 0; i < 80 / 4; i++)
-		swap32[i] = swab32(data32[i]);
+	swap32yes(swap32, data32, 80 / 4);
 
 
 	sha2(swap, 80, hash1, false);
 	sha2(swap, 80, hash1, false);
 	sha2(hash1, 32, (unsigned char *)(work->hash), false);
 	sha2(hash1, 32, (unsigned char *)(work->hash), false);
 
 
-	difficulty = swab32(*((uint32_t *)(work->data + 72)));
+	difficulty = be32toh(*((uint32_t *)(work->data + 72)));
 
 
 	diffbytes = ((difficulty >> 24) & 0xff) - 3;
 	diffbytes = ((difficulty >> 24) & 0xff) - 3;
 	diffvalue = difficulty & 0x00ffffff;
 	diffvalue = difficulty & 0x00ffffff;
@@ -1843,19 +1837,17 @@ bool regeneratehash(const struct work *work)
 	diffcmp[diffbytes >> 2] = diffvalue << diffshift;
 	diffcmp[diffbytes >> 2] = diffvalue << diffshift;
 
 
 	for (i = 7; i >= 0; i--) {
 	for (i = 7; i >= 0; i--) {
-		if (hash32[i] > diffcmp[i])
+		uint32_t hash32i = be32toh(hash32[i]);
+		if (hash32i > diffcmp[i])
 			return false;
 			return false;
-		if (hash32[i] < diffcmp[i])
+		if (hash32i < diffcmp[i])
 			return true;
 			return true;
 	}
 	}
 
 
 	// https://en.bitcoin.it/wiki/Block says: "numerically below"
 	// https://en.bitcoin.it/wiki/Block says: "numerically below"
 	// https://en.bitcoin.it/wiki/Target says: "lower than or equal to"
 	// https://en.bitcoin.it/wiki/Target says: "lower than or equal to"
 	// code in bitcoind 0.3.24 main.cpp CheckWork() says: if (hash > hashTarget) return false;
 	// code in bitcoind 0.3.24 main.cpp CheckWork() says: if (hash > hashTarget) return false;
-	if (hash32[0] == diffcmp[0])
-		return true;
-	else
-		return false;
+	return true;
 }
 }
 
 
 static void enable_pool(struct pool *pool)
 static void enable_pool(struct pool *pool)
@@ -1893,12 +1885,6 @@ static bool submit_upstream_work(const struct work *work, CURL *curl)
 	uint32_t *hash32;
 	uint32_t *hash32;
 	char hashshow[64+1] = "";
 	char hashshow[64+1] = "";
 
 
-#ifdef __BIG_ENDIAN__
-        int swapcounter = 0;
-        for (swapcounter = 0; swapcounter < 32; swapcounter++)
-            (((uint32_t*) (work->data))[swapcounter]) = swab32(((uint32_t*) (work->data))[swapcounter]);
-#endif
-
 	/* build hex string */
 	/* build hex string */
 	hexstr = bin2hex(work->data, sizeof(work->data));
 	hexstr = bin2hex(work->data, sizeof(work->data));
 	if (unlikely(!hexstr)) {
 	if (unlikely(!hexstr)) {
@@ -2510,8 +2496,6 @@ static bool stale_work(struct work *work, bool share)
 
 
 static void check_solve(struct work *work)
 static void check_solve(struct work *work)
 {
 {
-#ifndef MIPSEB
-	/* This segfaults on openwrt */
 	work->block = regeneratehash(work);
 	work->block = regeneratehash(work);
 	if (unlikely(work->block)) {
 	if (unlikely(work->block)) {
 		work->pool->solved++;
 		work->pool->solved++;
@@ -2519,7 +2503,6 @@ static void check_solve(struct work *work)
 		work->mandatory = true;
 		work->mandatory = true;
 		applog(LOG_NOTICE, "Found block for pool %d!", work->pool->pool_no);
 		applog(LOG_NOTICE, "Found block for pool %d!", work->pool->pool_no);
 	}
 	}
-#endif
 }
 }
 
 
 static void submit_discard_share(struct work *work)
 static void submit_discard_share(struct work *work)
@@ -4348,10 +4331,8 @@ bool hashtest(const struct work *work, bool checktarget)
 	unsigned char hash1[32];
 	unsigned char hash1[32];
 	unsigned char hash2[32];
 	unsigned char hash2[32];
 	uint32_t *hash2_32 = (uint32_t *)hash2;
 	uint32_t *hash2_32 = (uint32_t *)hash2;
-	int i;
 
 
-	for (i = 0; i < 80 / 4; i++)
-		swap32[i] = swab32(data32[i]);
+	swap32yes(swap32, data32, 80 / 4);
 
 
 	sha2(swap, 80, hash1, false);
 	sha2(swap, 80, hash1, false);
 	sha2(hash1, 32, hash2, false);
 	sha2(hash1, 32, hash2, false);
@@ -4359,8 +4340,7 @@ bool hashtest(const struct work *work, bool checktarget)
 	if (!checktarget)
 	if (!checktarget)
 		return hash2_32[7] == 0;
 		return hash2_32[7] == 0;
 
 
-	for (i = 0; i < 32 / 4; i++)
-		hash2_32[i] = swab32(hash2_32[i]);
+	swap32yes(hash2_32, hash2_32, 32 / 4);
 
 
 	memcpy((void*)work->hash, hash2, 32);
 	memcpy((void*)work->hash, hash2, 32);
 
 

+ 18 - 0
miner.h

@@ -126,9 +126,13 @@ void *alloca (size_t);
 # if __BYTE_ORDER == __LITTLE_ENDIAN
 # if __BYTE_ORDER == __LITTLE_ENDIAN
 #  define be32toh(x) bswap_32(x)
 #  define be32toh(x) bswap_32(x)
 #  define htobe32(x) bswap_32(x)
 #  define htobe32(x) bswap_32(x)
+#  define le32toh(x) (x)
+#  define htole32(x) (x)
 # elif __BYTE_ORDER == __BIG_ENDIAN
 # elif __BYTE_ORDER == __BIG_ENDIAN
 #  define be32toh(x) (x)
 #  define be32toh(x) (x)
 #  define htobe32(x) (x)
 #  define htobe32(x) (x)
+#  define le32toh(x) bswap_32(x)
+#  define htole32(x) bswap_32(x)
 #else
 #else
 #error UNKNOWN BYTE ORDER
 #error UNKNOWN BYTE ORDER
 #endif
 #endif
@@ -495,6 +499,20 @@ static inline void swap256(void *dest_p, const void *src_p)
 	dest[7] = src[0];
 	dest[7] = src[0];
 }
 }
 
 
+static inline void swap32yes(void*out, const void*in, size_t sz) {
+	size_t swapcounter = 0;
+	for (swapcounter = 0; swapcounter < sz; ++swapcounter)
+		(((uint32_t*)out)[swapcounter]) = swab32(((uint32_t*)in)[swapcounter]);
+}
+
+#ifdef __BIG_ENDIAN__
+#  define swap32tobe(out, in, sz)  (void)0
+#  define swap32tole(out, in, sz)  swap32yes(out, in, sz)
+#else
+#  define swap32tobe(out, in, sz)  swap32yes(out, in, sz)
+#  define swap32tole(out, in, sz)  (void)0
+#endif
+
 extern void quit(int status, const char *format, ...);
 extern void quit(int status, const char *format, ...);
 
 
 static inline void mutex_lock(pthread_mutex_t *lock)
 static inline void mutex_lock(pthread_mutex_t *lock)

+ 1 - 3
sha256_cryptopp.c

@@ -560,10 +560,8 @@ static inline bool HasSSE2(void) { return false; }
 static void SHA256_Transform32(word32 *state, const word32 *data)
 static void SHA256_Transform32(word32 *state, const word32 *data)
 {
 {
 	word32 W[16];
 	word32 W[16];
-	int i;
 
 
-	for (i = 0; i < 16; i++)
-		W[i] = swab32(((word32 *)(data))[i]);
+	swap32yes(W, data, 16);
 
 
 	X86_SHA256_HashBlocks(state, W, 16 * 4);
 	X86_SHA256_HashBlocks(state, W, 16 * 4);
 }
 }

+ 3 - 10
sha256_via.c

@@ -33,15 +33,13 @@ bool scanhash_via(struct thr_info*thr, __maybe_unused const unsigned char *pmids
 	uint32_t *hash32 = (uint32_t *) tmp_hash;
 	uint32_t *hash32 = (uint32_t *) tmp_hash;
 	uint32_t *nonce = (uint32_t *)(data + 64 + 12);
 	uint32_t *nonce = (uint32_t *)(data + 64 + 12);
 	unsigned long stat_ctr = 0;
 	unsigned long stat_ctr = 0;
-	int i;
 
 
 	/* bitcoin gives us big endian input, but via wants LE,
 	/* bitcoin gives us big endian input, but via wants LE,
 	 * so we reverse the swapping bitcoin has already done (extra work)
 	 * so we reverse the swapping bitcoin has already done (extra work)
 	 * in order to permit the hardware to swap everything
 	 * in order to permit the hardware to swap everything
 	 * back to BE again (extra work).
 	 * back to BE again (extra work).
 	 */
 	 */
-	for (i = 0; i < 128/4; i++)
-		data32[i] = swab32(((uint32_t *)data_inout)[i]);
+	swap32yes(data32, data_inout, 128/4);
 
 
 	while (1) {
 	while (1) {
 		n++;
 		n++;
@@ -51,9 +49,7 @@ bool scanhash_via(struct thr_info*thr, __maybe_unused const unsigned char *pmids
 		memcpy(tmp_hash1, sha256_init_state, 32);
 		memcpy(tmp_hash1, sha256_init_state, 32);
 		via_sha256(tmp_hash1, data, 80);	/* or maybe 128? */
 		via_sha256(tmp_hash1, data, 80);	/* or maybe 128? */
 
 
-		for (i = 0; i < 32/4; i++)
-			((uint32_t *)tmp_hash1)[i] =
-				swab32(((uint32_t *)tmp_hash1)[i]);
+		swap32yes(tmp_hash1, tmp_hash1, 32/4);
 
 
 		/* second SHA256 transform */
 		/* second SHA256 transform */
 		memcpy(tmp_hash, sha256_init_state, 32);
 		memcpy(tmp_hash, sha256_init_state, 32);
@@ -65,10 +61,7 @@ bool scanhash_via(struct thr_info*thr, __maybe_unused const unsigned char *pmids
 			/* swap nonce'd data back into original storage area;
 			/* swap nonce'd data back into original storage area;
 			 * TODO: only swap back the nonce, rather than all data
 			 * TODO: only swap back the nonce, rather than all data
 			 */
 			 */
-			for (i = 0; i < 128/4; i++) {
-				uint32_t *dout32 = (uint32_t *) data_inout;
-				dout32[i] = swab32(data32[i]);
-			}
+			swap32yes(data_inout, data32, 128/4);
 
 
 			*last_nonce = n;
 			*last_nonce = n;
 			return true;
 			return true;

+ 5 - 2
uthash.h

@@ -24,6 +24,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #ifndef UTHASH_H
 #ifndef UTHASH_H
 #define UTHASH_H
 #define UTHASH_H
 
 
+#include <endian.h>
 #include <string.h>   /* memcmp,strlen */
 #include <string.h>   /* memcmp,strlen */
 #include <stddef.h>   /* ptrdiff_t */
 #include <stddef.h>   /* ptrdiff_t */
 #include <stdlib.h>   /* exit() */
 #include <stdlib.h>   /* exit() */
@@ -500,14 +501,16 @@ do {
 #define MUR_PLUS2_ALIGNED(p) (((unsigned long)p & 0x3) == 2)
 #define MUR_PLUS2_ALIGNED(p) (((unsigned long)p & 0x3) == 2)
 #define MUR_PLUS3_ALIGNED(p) (((unsigned long)p & 0x3) == 3)
 #define MUR_PLUS3_ALIGNED(p) (((unsigned long)p & 0x3) == 3)
 #define WP(p) ((uint32_t*)((unsigned long)(p) & ~3UL))
 #define WP(p) ((uint32_t*)((unsigned long)(p) & ~3UL))
-#if (defined(__BIG_ENDIAN__) || defined(SPARC) || defined(__ppc__) || defined(__ppc64__))
+#if __BYTE_ORDER == __BIG_ENDIAN
 #define MUR_THREE_ONE(p) ((((*WP(p))&0x00ffffff) << 8) | (((*(WP(p)+1))&0xff000000) >> 24))
 #define MUR_THREE_ONE(p) ((((*WP(p))&0x00ffffff) << 8) | (((*(WP(p)+1))&0xff000000) >> 24))
 #define MUR_TWO_TWO(p)   ((((*WP(p))&0x0000ffff) <<16) | (((*(WP(p)+1))&0xffff0000) >> 16))
 #define MUR_TWO_TWO(p)   ((((*WP(p))&0x0000ffff) <<16) | (((*(WP(p)+1))&0xffff0000) >> 16))
 #define MUR_ONE_THREE(p) ((((*WP(p))&0x000000ff) <<24) | (((*(WP(p)+1))&0xffffff00) >>  8))
 #define MUR_ONE_THREE(p) ((((*WP(p))&0x000000ff) <<24) | (((*(WP(p)+1))&0xffffff00) >>  8))
-#else /* assume little endian non-intel */
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
 #define MUR_THREE_ONE(p) ((((*WP(p))&0xffffff00) >> 8) | (((*(WP(p)+1))&0x000000ff) << 24))
 #define MUR_THREE_ONE(p) ((((*WP(p))&0xffffff00) >> 8) | (((*(WP(p)+1))&0x000000ff) << 24))
 #define MUR_TWO_TWO(p)   ((((*WP(p))&0xffff0000) >>16) | (((*(WP(p)+1))&0x0000ffff) << 16))
 #define MUR_TWO_TWO(p)   ((((*WP(p))&0xffff0000) >>16) | (((*(WP(p)+1))&0x0000ffff) << 16))
 #define MUR_ONE_THREE(p) ((((*WP(p))&0xff000000) >>24) | (((*(WP(p)+1))&0x00ffffff) <<  8))
 #define MUR_ONE_THREE(p) ((((*WP(p))&0xff000000) >>24) | (((*(WP(p)+1))&0x00ffffff) <<  8))
+#else
+#error "Port me to your endian, please :)"
 #endif
 #endif
 #define MUR_GETBLOCK(p,i) (MUR_PLUS0_ALIGNED(p) ? ((p)[i]) :           \
 #define MUR_GETBLOCK(p,i) (MUR_PLUS0_ALIGNED(p) ? ((p)[i]) :           \
                             (MUR_PLUS1_ALIGNED(p) ? MUR_THREE_ONE(p) : \
                             (MUR_PLUS1_ALIGNED(p) ? MUR_THREE_ONE(p) : \