Skip to content

Commit

Permalink
Check for AES-NI at launch
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasjones committed Jul 4, 2014
1 parent c5c01d8 commit 2d5d5f8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
16 changes: 16 additions & 0 deletions cpu-miner.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ static char *rpc2_blob = NULL;
static int rpc2_bloblen = 0;
static uint32_t rpc2_target = 0;
static char *rpc2_job_id = NULL;
bool aes_ni_supported = false;

pthread_mutex_t applog_lock;
static pthread_mutex_t stats_lock;
Expand Down Expand Up @@ -1758,6 +1759,19 @@ static void signal_handler(int sig) {
}
#endif

static inline int cpuid(int code, uint32_t where[4]) {
asm volatile("cpuid":"=a"(*where),"=b"(*(where+1)),
"=c"(*(where+2)),"=d"(*(where+3)):"a"(code));
return (int)where[0];
}

static bool has_aes_ni()
{
uint32_t cpu_info[4];
cpuid(1, cpu_info);
return cpu_info[2] & (1 << 25);
}

int main(int argc, char *argv[]) {
struct thr_info *thr;
long flags;
Expand All @@ -1775,7 +1789,9 @@ int main(int argc, char *argv[]) {
init_blakehash_contexts();
} else if(opt_algo == ALGO_CRYPTONIGHT) {
jsonrpc_2 = true;
aes_ni_supported = has_aes_ni();
applog(LOG_INFO, "Using JSON-RPC 2.0");
applog(LOG_INFO, "CPU Supports AES-NI: %s", aes_ni_supported ? "YES" : "NO");
}


Expand Down
16 changes: 1 addition & 15 deletions cryptonight.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,6 @@ extern int aesb_single_round(const uint8_t *in, uint8_t*out, const uint8_t *expa
extern int aesb_pseudo_round_mut(uint8_t *val, uint8_t *expandedKey);
extern int fast_aesb_pseudo_round_mut(uint8_t *val, uint8_t *expandedKey);

static inline int cpuid(int code, uint32_t where[4]) {
asm volatile("cpuid":"=a"(*where),"=b"(*(where+1)),
"=c"(*(where+2)),"=d"(*(where+3)):"a"(code));
return (int)where[0];
}

static bool has_aes_ni()
{
uint32_t cpu_info[4];
cpuid(1, cpu_info);
return cpu_info[2] & (1 << 25);
}

static void (* const extra_hashes[4])(const void *, size_t, char *) = {
do_blake_hash, do_groestl_hash, do_jh_hash, do_skein_hash
};
Expand Down Expand Up @@ -270,7 +257,6 @@ void cryptonight_hash_ctx_aes_ni(void* output, const void* input, size_t len, st

int scanhash_cryptonight(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
uint32_t max_nonce, unsigned long *hashes_done) {
bool aes_ni = has_aes_ni();
uint32_t *nonceptr = (uint32_t*) (((char*)pdata) + 39);
uint32_t n = *nonceptr - 1;
const uint32_t first_nonce = n + 1;
Expand All @@ -279,7 +265,7 @@ int scanhash_cryptonight(int thr_id, uint32_t *pdata, const uint32_t *ptarget,

struct cryptonight_ctx *ctx = (struct cryptonight_ctx*)malloc(sizeof(struct cryptonight_ctx));

if (aes_ni) {
if (aes_ni_supported) {
do {
*nonceptr = ++n;
cryptonight_hash_ctx_aes_ni(hash, pdata, 76, ctx);
Expand Down
1 change: 1 addition & 0 deletions miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ extern int longpoll_thr_id;
extern int stratum_thr_id;
extern struct work_restart *work_restart;
extern bool jsonrpc_2;
extern bool aes_ni_supported;

#define JSON_RPC_LONGPOLL (1 << 0)
#define JSON_RPC_QUIET_404 (1 << 1)
Expand Down

0 comments on commit 2d5d5f8

Please sign in to comment.