Browse Source

Implement shared swap32(yes|tole|tobe) function to handle endian flipping 32-bit chunks in blocks

Luke Dashjr 13 years ago
parent
commit
b3d86f5b3d
5 changed files with 24 additions and 31 deletions
  1. 5 15
      cgminer.c
  2. 1 3
      driver-ztex.c
  3. 14 0
      miner.h
  4. 1 3
      sha256_cryptopp.c
  5. 3 10
      sha256_via.c

+ 5 - 15
cgminer.c

@@ -1249,19 +1249,13 @@ static void calc_midstate(struct work *work)
 		unsigned char c[64];
 		uint32_t i[16];
 	} 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_starts( &ctx, 0 );
 	sha2_update( &ctx, data.c, 64 );
 	memcpy(work->midstate, ctx.state, sizeof(work->midstate));
-#if __BYTE_ORDER != __LITTLE_ENDIAN
-	int i;
-	for (i = 0; i < 8; i++)
-		(((uint32_t*) (work->midstate))[i]) = htole32(((uint32_t*) (work->midstate))[i]);
-#endif
+	swap32tole(work->midstate, work->midstate, 8);
 }
 
 static bool work_decode(const json_t *val, struct work *work)
@@ -1656,8 +1650,7 @@ bool regeneratehash(const struct work *work)
 	int diffshift = 0;
 	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(hash1, 32, (unsigned char *)(work->hash), false);
@@ -4018,16 +4011,13 @@ bool hashtest(const struct work *work)
 	unsigned char hash1[32];
 	unsigned char hash2[32];
 	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(hash1, 32, hash2, false);
 
-	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);
 

+ 1 - 3
driver-ztex.c

@@ -157,7 +157,6 @@ static bool ztex_checkNonce(struct libztex_device *ztex,
 	unsigned char hash1[32];
 	unsigned char hash2[32];
 	uint32_t *hash2_32 = (uint32_t *)hash2;
-	int i;
 
 	hdata->nonce = le32toh(hdata->nonce);
 	hdata->hash7 = le32toh(hdata->hash7);
@@ -167,8 +166,7 @@ static bool ztex_checkNonce(struct libztex_device *ztex,
 	work->data[64 + 12 + 2] = (hdata->nonce >> 16) & 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(hash1, 32, hash2, false);

+ 14 - 0
miner.h

@@ -491,6 +491,20 @@ static inline void swap256(void *dest_p, const void *src_p)
 	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, ...);
 
 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)
 {
 	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);
 }

+ 3 - 10
sha256_via.c

@@ -33,15 +33,13 @@ bool scanhash_via(struct thr_info*thr, const unsigned char *pmidstate,
 	uint32_t *hash32 = (uint32_t *) tmp_hash;
 	uint32_t *nonce = (uint32_t *)(data + 64 + 12);
 	unsigned long stat_ctr = 0;
-	int i;
 
 	/* bitcoin gives us big endian input, but via wants LE,
 	 * so we reverse the swapping bitcoin has already done (extra work)
 	 * in order to permit the hardware to swap everything
 	 * 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) {
 		n++;
@@ -51,9 +49,7 @@ bool scanhash_via(struct thr_info*thr, const unsigned char *pmidstate,
 		memcpy(tmp_hash1, sha256_init_state, 32);
 		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 */
 		memcpy(tmp_hash, sha256_init_state, 32);
@@ -65,10 +61,7 @@ bool scanhash_via(struct thr_info*thr, const unsigned char *pmidstate,
 			/* swap nonce'd data back into original storage area;
 			 * 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;
 			return true;