Skip to content

Commit

Permalink
Separate 100% CPU mining subroutine as standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
joelao95 committed Oct 14, 2015
1 parent d908fd0 commit 6c0fc04
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 7 deletions.
10 changes: 6 additions & 4 deletions cpu-miner.c
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ static void *miner_thread(void *userdata)

hashes_done = 0;
gettimeofday(&ttest_start, NULL);
scanhash_m7m_hash(thr_id, work.data, work.target, work.data[19] + 1000, &hashes_done, false, false, cpu_dec_time);
scanhash_m7m_hash(thr_id, work.data, work.target, work.data[19] + 1000, &hashes_done);
gettimeofday(&ttest_end, NULL);
timeval_subtract(&tdiff, &ttest_end, &ttest_start);
hashrate_t = hashes_done/((double)(tdiff.tv_sec) + (double)(tdiff.tv_usec)*1.e-6); // h/s
Expand All @@ -1227,7 +1227,7 @@ static void *miner_thread(void *userdata)
cpu_dec_time.tv_nsec = (int64_t)dt_m%(int64_t)1e9;
hashes_done = 0;
gettimeofday(&ttest_start, NULL);
scanhash_m7m_hash(thr_id, work.data, work.target, work.data[19] + 100, &hashes_done, false, true, cpu_dec_time);
scanhash_m7m_hash_t(thr_id, work.data, work.target, work.data[19] + 100, &hashes_done, cpu_dec_time);
gettimeofday(&ttest_end, NULL);
timeval_subtract(&tdiff, &ttest_end, &ttest_start);
hashrate_t2 = hashes_done/((double)(tdiff.tv_sec) + (double)(tdiff.tv_usec)*1.e-6); // h/s
Expand Down Expand Up @@ -1261,8 +1261,10 @@ static void *miner_thread(void *userdata)
break;

case ALGO_M7M:
rc = scanhash_m7m_hash(thr_id, work.data, work.target,
max_nonce, &hashes_done, false, fcpu_dec, cpu_dec_time);
rc = ( fcpu_dec ? scanhash_m7m_hash_t(thr_id, work.data, work.target,
max_nonce, &hashes_done, cpu_dec_time) :
scanhash_m7m_hash(thr_id, work.data, work.target,
max_nonce, &hashes_done) );
break;

default:
Expand Down
208 changes: 206 additions & 2 deletions m7mhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ uint32_t sw2_(int nnounce)
#define SW_DIVS 5
#define M7_MIDSTATE_LEN 76
int scanhash_m7m_hash(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
uint64_t max_nonce, unsigned long *hashes_done, bool fhash_test, bool fcpu_dec, struct timespec cpu_dec_time)
uint64_t max_nonce, unsigned long *hashes_done)
{
uint32_t data[32] __attribute__((aligned(128)));
uint32_t *data_p64 = data + (M7_MIDSTATE_LEN / sizeof(data[0]));
Expand Down Expand Up @@ -284,7 +284,211 @@ int scanhash_m7m_hash(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
sph_sha256 (&ctx_final_sha256, bdata, bytes);
sph_sha256_close(&ctx_final_sha256, (void*)(hash));
}
if (fcpu_dec && n%10 == 0) nanosleep(&cpu_dec_time, NULL);

const unsigned char *hash_ = (const unsigned char *)hash;
const unsigned char *target_ = (const unsigned char *)ptarget;
for (i = 31; i >= 0; i--) {
if (hash_[i] != target_[i]) {
rc = hash_[i] < target_[i];
break;
}
}
if (unlikely(rc)) {
if (opt_debug) {
bin2hex(hash_str, (unsigned char *)hash, 32);
bin2hex(target_str, (unsigned char *)ptarget, 32);
bin2hex(data_str, (unsigned char *)data, 80);
applog(LOG_DEBUG, "DEBUG: [%d thread] Found share!\ndata %s\nhash %s\ntarget %s", thr_id,
data_str,
hash_str,
target_str);
}
pdata[19] = data[19];
goto out;
}
} while (n < max_nonce && !work_restart[thr_id].restart);
pdata[19] = n;
out:
mpf_set_prec_raw(magifpi, prec0);
mpf_set_prec_raw(magifpi0, prec0);
mpf_set_prec_raw(mptmp, prec0);
mpf_set_prec_raw(mpt1, prec0);
mpf_set_prec_raw(mpt2, prec0);
mpf_clear(magifpi);
mpf_clear(magifpi0);
mpf_clear(mpten);
mpf_clear(mptmp);
mpf_clear(mpt1);
mpf_clear(mpt2);
mpz_clears(magipi, magisw, product, bns0, bns1, NULL);

*hashes_done = n - first_nonce + 1;
return rc;
}

int scanhash_m7m_hash_t(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
uint64_t max_nonce, unsigned long *hashes_done, struct timespec cpu_dec_time)
{
uint32_t data[32] __attribute__((aligned(128)));
uint32_t *data_p64 = data + (M7_MIDSTATE_LEN / sizeof(data[0]));
uint32_t hash[8] __attribute__((aligned(32)));
uint8_t bhash[7][64] __attribute__((aligned(32)));
uint32_t n = pdata[19] - 1;
uint32_t usw_, mpzscale;
const uint32_t first_nonce = pdata[19];
char data_str[161], hash_str[65], target_str[65];
//uint8_t *bdata = 0;
uint8_t bdata[8192];
int rc = 0, i, digits;
int bytes, nnNonce2;
size_t p = sizeof(unsigned long), a = 64/p, b = 32/p;

memcpy(data, pdata, 80);

sph_sha256_context ctx_final_sha256;

sph_sha256_context ctx_sha256;
sph_sha512_context ctx_sha512;
sph_keccak512_context ctx_keccak;
sph_whirlpool_context ctx_whirlpool;
sph_haval256_5_context ctx_haval;
sph_tiger_context ctx_tiger;
sph_ripemd160_context ctx_ripemd;

sph_sha256_init(&ctx_final_sha256);

sph_sha256_init(&ctx_sha256);
sph_sha256 (&ctx_sha256, data, M7_MIDSTATE_LEN);

sph_sha512_init(&ctx_sha512);
sph_sha512 (&ctx_sha512, data, M7_MIDSTATE_LEN);

sph_keccak512_init(&ctx_keccak);
sph_keccak512 (&ctx_keccak, data, M7_MIDSTATE_LEN);

sph_whirlpool_init(&ctx_whirlpool);
sph_whirlpool (&ctx_whirlpool, data, M7_MIDSTATE_LEN);

sph_haval256_5_init(&ctx_haval);
sph_haval256_5 (&ctx_haval, data, M7_MIDSTATE_LEN);

sph_tiger_init(&ctx_tiger);
sph_tiger (&ctx_tiger, data, M7_MIDSTATE_LEN);

sph_ripemd160_init(&ctx_ripemd);
sph_ripemd160 (&ctx_ripemd, data, M7_MIDSTATE_LEN);

sph_sha256_context ctx2_sha256;
sph_sha512_context ctx2_sha512;
sph_keccak512_context ctx2_keccak;
sph_whirlpool_context ctx2_whirlpool;
sph_haval256_5_context ctx2_haval;
sph_tiger_context ctx2_tiger;
sph_ripemd160_context ctx2_ripemd;

mpz_t magipi, magisw, product, bns0, bns1;
mpf_t magifpi, magifpi0, mpt1, mpt2, mptmp, mpten;

mpz_inits(magipi, magisw, bns0, bns1, NULL);
mpz_init2(product, 512);

mp_bitcnt_t prec0 = (long int)((int)((sqrt((double)(INT_MAX))*(1.+EPS))/9000+75)*BITS_PER_DIGIT+16);
mpf_set_default_prec(prec0);

mpf_init(magifpi);
mpf_init(magifpi0);
mpf_init(mpt1);
mpf_init(mpt2);
mpf_init(mptmp);
mpf_init_set_ui(mpten, 10);
mpf_set_str(mpt2, "0.8e3b1a9b359805c2e54c6415037f2e336893b6457f7754f6b4ae045eb6c5f2bedb26a114030846be7", 16);
mpf_set_str(magifpi0, "0.b7bfc6837e20bdb22653f1fc419f6bc33ca80eb65b7b0246f7f3b65689560aea1a2f2fd95f254d68c", 16);

do {
data[19] = ++n;
memset(bhash, 0, 7 * 64);

ctx2_sha256 = ctx_sha256;
sph_sha256 (&ctx2_sha256, data_p64, 80 - M7_MIDSTATE_LEN);
sph_sha256_close(&ctx2_sha256, (void*)(bhash[0]));

ctx2_sha512 = ctx_sha512;
sph_sha512 (&ctx2_sha512, data_p64, 80 - M7_MIDSTATE_LEN);
sph_sha512_close(&ctx2_sha512, (void*)(bhash[1]));

ctx2_keccak = ctx_keccak;
sph_keccak512 (&ctx2_keccak, data_p64, 80 - M7_MIDSTATE_LEN);
sph_keccak512_close(&ctx2_keccak, (void*)(bhash[2]));

ctx2_whirlpool = ctx_whirlpool;
sph_whirlpool (&ctx2_whirlpool, data_p64, 80 - M7_MIDSTATE_LEN);
sph_whirlpool_close(&ctx2_whirlpool, (void*)(bhash[3]));

ctx2_haval = ctx_haval;
sph_haval256_5 (&ctx2_haval, data_p64, 80 - M7_MIDSTATE_LEN);
sph_haval256_5_close(&ctx2_haval, (void*)(bhash[4]));

ctx2_tiger = ctx_tiger;
sph_tiger (&ctx2_tiger, data_p64, 80 - M7_MIDSTATE_LEN);
sph_tiger_close(&ctx2_tiger, (void*)(bhash[5]));

ctx2_ripemd = ctx_ripemd;
sph_ripemd160 (&ctx2_ripemd, data_p64, 80 - M7_MIDSTATE_LEN);
sph_ripemd160_close(&ctx2_ripemd, (void*)(bhash[6]));

mpz_import(bns0, a, -1, p, -1, 0, bhash[0]);
mpz_set(bns1, bns0);
mpz_set(product, bns0);
for(int i=1; i < 7; i++){
mpz_import(bns0, a, -1, p, -1, 0, bhash[i]);
mpz_add(bns1, bns1, bns0);
mpz_mul(product, product, bns0);
}
mpz_mul(product, product, bns1);

mpz_mul(product, product, product);
bytes = mpz_sizeinbase(product, 256);
mpz_export((void *)bdata, NULL, -1, 1, 0, 0, product);

sph_sha256 (&ctx_final_sha256, bdata, bytes);
sph_sha256_close(&ctx_final_sha256, (void*)(hash));

digits=(int)((sqrt((double)(n/2))*(1.+EPS))/9000+75);
mp_bitcnt_t prec = (long int)(digits*BITS_PER_DIGIT+16);
mpf_set_prec_raw(magifpi, prec);
mpf_set_prec_raw(mptmp, prec);
mpf_set_prec_raw(mpt1, prec);
mpf_set_prec_raw(mpt2, prec);

usw_ = sw2_(n/2);
mpzscale = 1;
mpz_set_ui(magisw, usw_);

for(i = 0; i < 5; i++)
{
mpf_set_d(mpt1, 0.25*mpzscale);
mpf_sub(mpt1, mpt1, mpt2);
mpf_abs(mpt1, mpt1);
mpf_div(magifpi, magifpi0, mpt1);
mpf_pow_ui(mptmp, mpten, digits >> 1);
mpf_mul(magifpi, magifpi, mptmp);
mpz_set_f(magipi, magifpi);
mpz_add(magipi,magipi,magisw);
mpz_add(product,product,magipi);

mpz_import(bns0, b, -1, p, -1, 0, (void*)(hash));
mpz_add(bns1, bns1, bns0);
mpz_mul(product,product,bns1);
mpz_cdiv_q (product, product, bns0);

bytes = mpz_sizeinbase(product, 256);
mpzscale=bytes;
mpz_export(bdata, NULL, -1, 1, 0, 0, product);

sph_sha256 (&ctx_final_sha256, bdata, bytes);
sph_sha256_close(&ctx_final_sha256, (void*)(hash));
}
if (n%10 == 0) nanosleep(&cpu_dec_time, NULL);
const unsigned char *hash_ = (const unsigned char *)hash;
const unsigned char *target_ = (const unsigned char *)ptarget;
for (i = 31; i >= 0; i--) {
Expand Down
5 changes: 4 additions & 1 deletion miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ extern int scanhash_scrypt(int thr_id, uint32_t *pdata,
unsigned char *scratchbuf, const uint32_t *ptarget,
uint32_t max_nonce, unsigned long *hashes_done, int N);

int scanhash_m7m_hash_t(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
uint64_t max_nonce, unsigned long *hashes_done, struct timespec cpu_dec_time);

int scanhash_m7m_hash(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
uint64_t max_nonce, unsigned long *hashes_done, bool fhash_test, bool fcpu_dec, struct timespec cpu_dec_time);
uint64_t max_nonce, unsigned long *hashes_done);

struct thr_info {
int id;
Expand Down

0 comments on commit 6c0fc04

Please sign in to comment.