From 33dc35707e8a768412ef9a5c1e2056baf9d2c28a Mon Sep 17 00:00:00 2001 From: Jojo Date: Sun, 20 Sep 2026 20:32:26 +0200 Subject: [PATCH 1/2] Use __builtin_bswap32 for BYTESWAP32 and reduce swap/unswap in sha256. --- src/mining/miner_sha256.cpp | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/src/mining/miner_sha256.cpp b/src/mining/miner_sha256.cpp index e8bbfc9..9774dce 100644 --- a/src/mining/miner_sha256.cpp +++ b/src/mining/miner_sha256.cpp @@ -11,7 +11,7 @@ #include #include "miner_sha256.h" -#define BYTESWAP32(z) ((uint32_t)((z&0xFF)<<24|((z>>8)&0xFF)<<16|((z>>16)&0xFF)<<8|((z>>24)&0xFF))) +#define BYTESWAP32(z) __builtin_bswap32(z) #define RROT(v, s) ((v)>>(s) | (v)<<(32-(s))) #define R1_a(i) (w[i] = w[i-16] + (RROT(w[i-15],7) ^ (RROT(w[i-15],18) ^ (w[i-15] >> 3))) + ((RROT(w[i-2],17) ^ RROT(w[i-2],19) ^ (w[i-2] >> 10)))) @@ -376,26 +376,14 @@ bool miner_sha256_header(sha256_hash_t *midpoint, sha256_hash_t *ctx, block_head CM(1, 2, 3, 4, 5, 6, 7, 0, 63); // First hash complete - byte-swap for second hash input - tmp.hash[0] = BYTESWAP32(WA[0] + midpoint->hash[0]); - tmp.hash[1] = BYTESWAP32(WA[1] + midpoint->hash[1]); - tmp.hash[2] = BYTESWAP32(WA[2] + midpoint->hash[2]); - tmp.hash[3] = BYTESWAP32(WA[3] + midpoint->hash[3]); - tmp.hash[4] = BYTESWAP32(WA[4] + midpoint->hash[4]); - tmp.hash[5] = BYTESWAP32(WA[5] + midpoint->hash[5]); - tmp.hash[6] = BYTESWAP32(WA[6] + midpoint->hash[6]); - tmp.hash[7] = BYTESWAP32(WA[7] + midpoint->hash[7]); - - // Copy first hash into working area for double hash - data = (uint8_t *)tmp.hash; - - w[0] = GET_DATA(data, 0); - w[1] = GET_DATA(data, 4); - w[2] = GET_DATA(data, 8); - w[3] = GET_DATA(data, 12); - w[4] = GET_DATA(data, 16); - w[5] = GET_DATA(data, 20); - w[6] = GET_DATA(data, 24); - w[7] = GET_DATA(data, 28); + w[0] = WA[0] + midpoint->hash[0]; + w[1] = WA[1] + midpoint->hash[1]; + w[2] = WA[2] + midpoint->hash[2]; + w[3] = WA[3] + midpoint->hash[3]; + w[4] = WA[4] + midpoint->hash[4]; + w[5] = WA[5] + midpoint->hash[5]; + w[6] = WA[6] + midpoint->hash[6]; + w[7] = WA[7] + midpoint->hash[7]; w[8] = 0x80000000; w[9] = w[10] = w[11] = w[12] = w[13] = w[14] = 0; From 66ec6572268b439d37e3bac65290c67ceeb4a4ec Mon Sep 17 00:00:00 2001 From: joheirba <61690590+joheirba@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:48:57 +0200 Subject: [PATCH 2/2] Refactor getDifficulty to use hardware float instead of double Some ESP have signle precision hardware floating point units, having enough precision for the function getDifficulty. This function is calles for every "fast checked" candididate, which is quite often. Using single precision floating points reduced the function from approx 12000 cpu cycles to around 800 cycles. --- src/mining/miner.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/mining/miner.cpp b/src/mining/miner.cpp index ea9014f..fc01023 100644 --- a/src/mining/miner.cpp +++ b/src/mining/miner.cpp @@ -264,16 +264,21 @@ static void createCoinbaseHash(uint8_t *hash, const stratum_job_t *job) { // Difficulty Calculation // ============================================================ -static double getDifficulty(sha256_hash_t *ctx) { - static const double maxTarget = 26959535291011309493156476344723991336010898738574164086137773096960.0; - double hashValue = 0.0; - for (int i = 0, j = 31; i < 32; i++, j--) { - hashValue = hashValue * 256 + ctx->bytes[j]; - } - double difficulty = maxTarget / hashValue; - if (isnan(difficulty) || isinf(difficulty)) { - difficulty = 0.0; +static float getDifficulty(sha256_hash_t *ctx){ + static const float maxTarget = ((float) 0xFFFF0000UL) ; + + int j=7; + for (; j >= 3; j--) if(ctx->hash[j] != 0) break ; + + float hashValue = ((float) (ctx->hash[j--])) + + ((float) (ctx->hash[j--])) / (float) 0x100000000; + float mantissa = maxTarget / hashValue; + int exp = (4 - j) * 32; + if (isnan(mantissa) || isinf(mantissa)) { + mantissa = 0.0; } + float difficulty = __builtin_ldexpf(mantissa, exp) ; + return difficulty; }