Browse Source

Use correct macros for endian handling code

Luke Dashjr 13 years ago
parent
commit
91fb4e5177
6 changed files with 26 additions and 44 deletions
  1. 8 18
      cgminer.c
  2. 1 3
      driver-bitforce.c
  3. 1 3
      driver-icarus.c
  4. 7 18
      driver-ztex.c
  5. 4 0
      miner.h
  6. 5 2
      uthash.h

+ 8 - 18
cgminer.c

@@ -15,6 +15,7 @@
 #include <curses.h>
 #endif
 
+#include <endian.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -1256,10 +1257,10 @@ static void calc_midstate(struct work *work)
 	sha2_starts( &ctx, 0 );
 	sha2_update( &ctx, data.c, 64 );
 	memcpy(work->midstate, ctx.state, sizeof(work->midstate));
-#if defined(__BIG_ENDIAN__) || defined(MIPSEB)
+#if __BYTE_ORDER != __LITTLE_ENDIAN
 	int i;
 	for (i = 0; i < 8; i++)
-		(((uint32_t*) (work->midstate))[i]) = swab32(((uint32_t*) (work->midstate))[i]);
+		(((uint32_t*) (work->midstate))[i]) = htole32(((uint32_t*) (work->midstate))[i]);
 #endif
 }
 
@@ -1661,7 +1662,7 @@ bool regeneratehash(const struct work *work)
 	sha2(swap, 80, hash1, 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;
 	diffvalue = difficulty & 0x00ffffff;
@@ -1677,19 +1678,17 @@ bool regeneratehash(const struct work *work)
 	diffcmp[diffbytes >> 2] = diffvalue << diffshift;
 
 	for (i = 7; i >= 0; i--) {
-		if (hash32[i] > diffcmp[i])
+		uint32_t hash32i = be32toh(hash32[i]);
+		if (hash32i > diffcmp[i])
 			return false;
-		if (hash32[i] < diffcmp[i])
+		if (hash32i < diffcmp[i])
 			return true;
 	}
 
 	// https://en.bitcoin.it/wiki/Block says: "numerically below"
 	// 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;
-	if (hash32[0] == diffcmp[0])
-		return true;
-	else
-		return false;
+	return true;
 }
 
 static void enable_pool(struct pool *pool)
@@ -1727,12 +1726,6 @@ static bool submit_upstream_work(const struct work *work, CURL *curl)
 	uint32_t *hash32;
 	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 */
 	hexstr = bin2hex(work->data, sizeof(work->data));
 	if (unlikely(!hexstr)) {
@@ -2317,8 +2310,6 @@ static bool stale_work(struct work *work, bool share)
 
 static void check_solve(struct work *work)
 {
-#ifndef MIPSEB
-	/* This segfaults on openwrt */
 	work->block = regeneratehash(work);
 	if (unlikely(work->block)) {
 		work->pool->solved++;
@@ -2326,7 +2317,6 @@ static void check_solve(struct work *work)
 		work->mandatory = true;
 		applog(LOG_NOTICE, "Found block for pool %d!", work->pool);
 	}
-#endif
 }
 
 static void *submit_work_thread(void *userdata)

+ 1 - 3
driver-bitforce.c

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

+ 1 - 3
driver-icarus.c

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

+ 7 - 18
driver-ztex.c

@@ -23,6 +23,7 @@
  *   You should have received a copy of the GNU General Public License
  *   along with this program; if not, see http://www.gnu.org/licenses/.
 **/
+
 #include <unistd.h>
 #include <sha2.h>
 #include "miner.h"
@@ -158,10 +159,8 @@ static bool ztex_checkNonce(struct libztex_device *ztex,
 	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 + 1] = (hdata->nonce >> 8) & 0xff;
@@ -173,11 +172,7 @@ static bool ztex_checkNonce(struct libztex_device *ztex,
 	
 	sha2(swap, 80, hash1, 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;
 		applog(LOG_DEBUG, "%s: checkNonce failed for %0.8X", ztex->repr, hdata->nonce);
 		return false;
@@ -275,9 +270,7 @@ static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
  
 		for (i = 0; i < ztex->numNonces; i++) {
 			nonce = hdata[i].nonce;
-#if defined(__BIGENDIAN__) || defined(MIPSEB)
-			nonce = swab32(nonce);
-#endif
+			nonce = le32toh(nonce);
 			if (nonce > noncecnt)
 				noncecnt = nonce;
 			if (((nonce & 0x7fffffff) >> 4) < ((lastnonce[i] & 0x7fffffff) >> 4)) {
@@ -285,9 +278,7 @@ static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
 				overflow = true;
 			} else
 				lastnonce[i] = nonce;
-#if !(defined(__BIGENDIAN__) || defined(MIPSEB))
-			nonce = swab32(nonce);
-#endif
+			nonce = htole32(nonce);
 			if (!ztex_checkNonce(ztex, work, &hdata[i])) {
 				thr->cgpu->hw_errors++;
 				continue;
@@ -307,9 +298,7 @@ static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
 						backlog[backlog_p++] = nonce;
 						if (backlog_p >= backlog_max)
 							backlog_p = 0;
-#if defined(__BIGENDIAN__) || defined(MIPSEB)
-						nonce = swab32(nonce);
-#endif
+						nonce = le32toh(nonce);
 						work->blk.nonce = 0xffffffff;
 						rv = submit_nonce(thr, work, nonce);
 						applog(LOG_DEBUG, "%s: submitted %0.8x %d", ztex->repr, nonce, rv);

+ 4 - 0
miner.h

@@ -130,9 +130,13 @@ void *alloca (size_t);
 # if __BYTE_ORDER == __LITTLE_ENDIAN
 #  define be32toh(x) bswap_32(x)
 #  define htobe32(x) bswap_32(x)
+#  define le32toh(x) (x)
+#  define htole32(x) (x)
 # elif __BYTE_ORDER == __BIG_ENDIAN
 #  define be32toh(x) (x)
 #  define htobe32(x) (x)
+#  define le32toh(x) bswap_32(x)
+#  define htole32(x) bswap_32(x)
 #else
 #error UNKNOWN BYTE ORDER
 #endif

+ 5 - 2
uthash.h

@@ -24,6 +24,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #ifndef UTHASH_H
 #define UTHASH_H
 
+#include <endian.h>
 #include <string.h>   /* memcmp,strlen */
 #include <stddef.h>   /* ptrdiff_t */
 #include <stdlib.h>   /* exit() */
@@ -500,14 +501,16 @@ do {
 #define MUR_PLUS2_ALIGNED(p) (((unsigned long)p & 0x3) == 2)
 #define MUR_PLUS3_ALIGNED(p) (((unsigned long)p & 0x3) == 3)
 #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_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))
-#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_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))
+#else
+#error "Port me to your endian, please :)"
 #endif
 #define MUR_GETBLOCK(p,i) (MUR_PLUS0_ALIGNED(p) ? ((p)[i]) :           \
                             (MUR_PLUS1_ALIGNED(p) ? MUR_THREE_ONE(p) : \