Mark hash success as unlikely, using __builtin_expect() intrinsic
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 69 70 71 72 73 74 75 76 77 78 79 80
diff --git a/miner.h b/miner.h
index b396380..1c73114 100644
--- a/miner.h
+++ b/miner.h
@@ -21,6 +21,14 @@
#include <byteswap.h>
#endif
+#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
+#undef unlikely
+#define unlikely(expr) (__builtin_expect((expr), 0))
+#else
+#undef unlikely
+#define unlikely(expr) (expr)
+#endif
+
#if defined(__i386__)
#define WANT_CRYPTOPP_ASM32
#endif
diff --git a/sha256_4way.c b/sha256_4way.c
index fe9642b..ea6634a 100644
--- a/sha256_4way.c
+++ b/sha256_4way.c
@@ -118,7 +118,7 @@ unsigned int ScanHash_4WaySSE2(const unsigned char *pmidstate, unsigned char *pd
for (j = 0; j < NPAR; j++)
{
- if (thash[7][j] == 0)
+ if (unlikely(thash[7][j] == 0))
{
int i;
diff --git a/sha256_cryptopp.c b/sha256_cryptopp.c
index eed3a52..a954a85 100644
--- a/sha256_cryptopp.c
+++ b/sha256_cryptopp.c
@@ -110,7 +110,7 @@ bool scanhash_cryptopp(const unsigned char *midstate, unsigned char *data,
stat_ctr++;
- if ((hash32[7] == 0) && fulltest(hash, target)) {
+ if (unlikely((hash32[7] == 0) && fulltest(hash, target))) {
*hashes_done = stat_ctr;
return true;
}
@@ -592,7 +592,7 @@ bool scanhash_asm32(const unsigned char *midstate, unsigned char *data,
stat_ctr++;
- if ((hash32[7] == 0) && fulltest(hash, target)) {
+ if (unlikely((hash32[7] == 0) && fulltest(hash, target))) {
fulltest(hash, target);
*hashes_done = stat_ctr;
diff --git a/sha256_generic.c b/sha256_generic.c
index e63b8a3..46cc0c2 100644
--- a/sha256_generic.c
+++ b/sha256_generic.c
@@ -256,7 +256,7 @@ bool scanhash_c(const unsigned char *midstate, unsigned char *data,
stat_ctr++;
- if ((hash32[7] == 0) && fulltest(hash, target)) {
+ if (unlikely((hash32[7] == 0) && fulltest(hash, target))) {
*hashes_done = stat_ctr;
return true;
}
diff --git a/sha256_via.c b/sha256_via.c
index 39f86a5..186007e 100644
--- a/sha256_via.c
+++ b/sha256_via.c
@@ -57,7 +57,7 @@ bool scanhash_via(unsigned char *data_inout,
stat_ctr++;
- if ((hash32[7] == 0) && fulltest(tmp_hash, target)) {
+ if (unlikely((hash32[7] == 0) && fulltest(tmp_hash, target))) {
/* swap nonce'd data back into original storage area;
* TODO: only swap back the nonce, rather than all data
*/