Browse Source

Move malgo-specific code to dedicated source files

Luke Dashjr 11 years ago
parent
commit
289e7c002e
9 changed files with 173 additions and 171 deletions
  1. 3 1
      Makefile.am
  2. 0 1
      findnonce.c
  3. 49 0
      malgo/scrypt.c
  4. 6 0
      malgo/scrypt.h
  5. 111 0
      malgo/sha256d.c
  6. 4 136
      miner.c
  7. 0 19
      scrypt.h
  8. 0 13
      util.c
  9. 0 1
      util.h

+ 3 - 1
Makefile.am

@@ -149,6 +149,8 @@ endif
 
 
 if USE_SHA256D
+bfgminer_SOURCES += malgo/sha256d.c
+
 if USE_OPENCL
 dist_kernels_DATA += \
 	$(top_srcdir)/opencl/diablo.cl  \
@@ -195,7 +197,7 @@ endif # USE_CPUMINING
 endif # USE_SHA256D
 
 if USE_SCRYPT
-bfgminer_SOURCES += scrypt.c scrypt.h
+bfgminer_SOURCES += malgo/scrypt.c malgo/scrypt.h
 dist_doc_DATA += README.scrypt
 
 if USE_OPENCL

+ 0 - 1
findnonce.c

@@ -19,7 +19,6 @@
 
 #include "findnonce.h"
 #include "miner.h"
-#include "scrypt.h"
 
 #ifdef USE_SHA256D
 const uint32_t SHA256_K[64] = {

+ 49 - 0
scrypt.c → malgo/scrypt.c

@@ -32,11 +32,14 @@
 #include "config.h"
 #include "miner.h"
 
+#include <math.h>
 #include <stdlib.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <string.h>
 
+#include <uthash.h>
+
 typedef struct SHA256Context {
 	uint32_t state[8];
 	uint32_t buf[16];
@@ -546,3 +549,49 @@ bool scanhash_scrypt(struct thr_info * const thr, struct work * const work,
 	free(scratchbuf);
 	return ret;
 }
+
+#ifdef USE_OPENCL
+static
+float opencl_oclthreads_to_intensity_scrypt(const unsigned long oclthreads)
+{
+	return log2(oclthreads);
+}
+
+static
+unsigned long opencl_intensity_to_oclthreads_scrypt(float intensity)
+{
+	return pow(2, intensity);
+}
+
+static
+char *opencl_get_default_kernel_file_scrypt(const struct mining_algorithm * const malgo, struct cgpu_info * const cgpu, struct _clState * const clState)
+{
+	return strdup("scrypt");
+}
+#endif
+
+struct mining_algorithm malgo_scrypt = {
+	.name = "scrypt",
+	.aliases = "scrypt",
+	
+	.algo = POW_SCRYPT,
+	.ui_skip_hash_bytes = 2,
+	.reasonable_low_nonce_diff = 1./0x10000,
+	
+	.hash_data_f = scrypt_hash_data,
+	
+#ifdef USE_OPENCL
+	.opencl_oclthreads_to_intensity = opencl_oclthreads_to_intensity_scrypt,
+	.opencl_intensity_to_oclthreads = opencl_intensity_to_oclthreads_scrypt,
+	.opencl_min_oclthreads =      0x100,  // intensity   8
+	.opencl_max_oclthreads = 0x20000000,  // intensity  31
+	.opencl_get_default_kernel_file = opencl_get_default_kernel_file_scrypt,
+#endif
+};
+
+static
+__attribute__((constructor))
+void init_scrypt(void)
+{
+	LL_APPEND(mining_algorithms, (&malgo_scrypt));
+}

+ 6 - 0
malgo/scrypt.h

@@ -0,0 +1,6 @@
+#ifndef SCRYPT_H
+#define SCRYPT_H
+
+extern void test_scrypt(void);
+
+#endif /* SCRYPT_H */

+ 111 - 0
malgo/sha256d.c

@@ -0,0 +1,111 @@
+/*
+ * Copyright 2012-2014 Luke Dashjr
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+#include "config.h"
+
+#include <math.h>
+#include <string.h>
+
+#include <uthash.h>
+
+#include "logging.h"
+#include "miner.h"
+#include "ocl.h"
+#include "util.h"
+
+static
+void hash_data(void *out_hash, const void *data)
+{
+	unsigned char blkheader[80];
+	
+	// data is past the first SHA256 step (padding and interpreting as big endian on a little endian platform), so we need to flip each 32-bit chunk around to get the original input block header
+	swap32yes(blkheader, data, 80 / 4);
+	
+	// double-SHA256 to get the block hash
+	gen_hash(blkheader, out_hash, 80);
+}
+
+#ifdef USE_OPENCL
+float opencl_oclthreads_to_intensity_sha256d(const unsigned long oclthreads)
+{
+	return log2f(oclthreads) - 15.;
+}
+
+unsigned long opencl_intensity_to_oclthreads_sha256d(float intensity)
+{
+	return powf(2, intensity + 15);
+}
+
+static
+char *opencl_get_default_kernel_file_sha256d(const struct mining_algorithm * const malgo, struct cgpu_info * const cgpu, struct _clState * const clState)
+{
+	const char * const vbuff = clState->platform_ver_str;
+	
+	if (clState->is_mesa)
+	{
+		applog(LOG_INFO, "Selecting phatk kernel for Mesa");
+		return strdup("phatk");
+	}
+	
+	/* Detect all 2.6 SDKs not with Tahiti and use diablo kernel */
+	if (!strstr(cgpu->name, "Tahiti") &&
+	   (strstr(vbuff, "844.4") ||  // Linux 64 bit ATI 2.6 SDK
+	    strstr(vbuff, "851.4") ||  // Windows 64 bit ""
+	    strstr(vbuff, "831.4") ||
+	    strstr(vbuff, "898.1") ||  // 12.2 driver SDK
+	    strstr(vbuff, "923.1") ||  // 12.4
+	    strstr(vbuff, "938.2") ||  // SDK 2.7
+	    strstr(vbuff, "1113.2")))  // SDK 2.8
+	{
+		applog(LOG_INFO, "Selecting diablo kernel");
+		return strdup("diablo");
+	}
+	
+	/* Detect all 7970s, older ATI and NVIDIA and use poclbm */
+	if (strstr(cgpu->name, "Tahiti") || !clState->hasBitAlign)
+	{
+		applog(LOG_INFO, "Selecting poclbm kernel");
+		return strdup("poclbm");
+	}
+	
+	/* Use phatk for the rest R5xxx R6xxx */
+	{
+		applog(LOG_INFO, "Selecting phatk kernel");
+		return strdup("phatk");
+	}
+}
+#endif  /* USE_OPENCL */
+
+struct mining_algorithm malgo_sha256d = {
+	.name = "SHA256d",
+	.aliases = "SHA256d|SHA256|SHA2",
+	
+	.algo = POW_SHA256D,
+	.ui_skip_hash_bytes = 4,
+	.worktime_skip_prevblk_u32 = 1,
+	.reasonable_low_nonce_diff = 1.,
+	
+	.hash_data_f = hash_data,
+	
+#ifdef USE_OPENCL
+	.opencl_nodefault = true,
+	.opencl_oclthreads_to_intensity = opencl_oclthreads_to_intensity_sha256d,
+	.opencl_intensity_to_oclthreads = opencl_intensity_to_oclthreads_sha256d,
+	.opencl_min_oclthreads =       0x20,  // intensity -10
+	.opencl_max_oclthreads = 0x20000000,  // intensity  14
+	.opencl_get_default_kernel_file = opencl_get_default_kernel_file_sha256d,
+#endif
+};
+
+static
+__attribute__((constructor))
+void init_sha256d(void)
+{
+    LL_APPEND(mining_algorithms, (&malgo_sha256d));
+}

+ 4 - 136
miner.c

@@ -80,7 +80,6 @@
 #include "adl.h"
 #include "driver-cpu.h"
 #include "driver-opencl.h"
-#include "scrypt.h"
 #include "util.h"
 
 #ifdef USE_AVALON
@@ -98,7 +97,7 @@
 #endif
 
 #ifdef USE_SCRYPT
-#include "scrypt.h"
+#include "malgo/scrypt.h"
 #endif
 
 #include "version.h"
@@ -1033,128 +1032,8 @@ struct mining_algorithm *mining_algorithm_by_alias(const char * const alias)
 	return NULL;
 }
 
-
-#if defined(USE_SHA256D) && defined(USE_OPENCL)
-static
-float opencl_oclthreads_to_intensity_sha256d(const unsigned long oclthreads)
-{
-	return log2f(oclthreads) - 15.;
-}
-
-static
-unsigned long opencl_intensity_to_oclthreads_sha256d(float intensity)
-{
-	return powf(2, intensity + 15);
-}
-#endif
-
-#ifdef USE_SHA256D
-#ifdef USE_OPENCL
-
-#include "ocl.h"
-
-static
-char *opencl_get_default_kernel_file_sha256d(const struct mining_algorithm * const malgo, struct cgpu_info * const cgpu, struct _clState * const clState)
-{
-	const char * const vbuff = clState->platform_ver_str;
-	
-	if (clState->is_mesa)
-	{
-		applog(LOG_INFO, "Selecting phatk kernel for Mesa");
-		return strdup("phatk");
-	}
-	
-	/* Detect all 2.6 SDKs not with Tahiti and use diablo kernel */
-	if (!strstr(cgpu->name, "Tahiti") &&
-	   (strstr(vbuff, "844.4") ||  // Linux 64 bit ATI 2.6 SDK
-	    strstr(vbuff, "851.4") ||  // Windows 64 bit ""
-	    strstr(vbuff, "831.4") ||
-	    strstr(vbuff, "898.1") ||  // 12.2 driver SDK
-	    strstr(vbuff, "923.1") ||  // 12.4
-	    strstr(vbuff, "938.2") ||  // SDK 2.7
-	    strstr(vbuff, "1113.2")))  // SDK 2.8
-	{
-		applog(LOG_INFO, "Selecting diablo kernel");
-		return strdup("diablo");
-	}
-	
-	/* Detect all 7970s, older ATI and NVIDIA and use poclbm */
-	if (strstr(cgpu->name, "Tahiti") || !clState->hasBitAlign)
-	{
-		applog(LOG_INFO, "Selecting poclbm kernel");
-		return strdup("poclbm");
-	}
-	
-	/* Use phatk for the rest R5xxx R6xxx */
-	{
-		applog(LOG_INFO, "Selecting phatk kernel");
-		return strdup("phatk");
-	}
-}
-#endif  /* USE_OPENCL */
-
-static struct mining_algorithm malgo_sha256d = {
-	.name = "SHA256d",
-	.aliases = "SHA256d|SHA256|SHA2",
-	
-	.algo = POW_SHA256D,
-	.ui_skip_hash_bytes = 4,
-	.worktime_skip_prevblk_u32 = 1,
-	.reasonable_low_nonce_diff = 1.,
-	
-	.hash_data_f = hash_data,
-	
-#ifdef USE_OPENCL
-	.opencl_nodefault = true,
-	.opencl_oclthreads_to_intensity = opencl_oclthreads_to_intensity_sha256d,
-	.opencl_intensity_to_oclthreads = opencl_intensity_to_oclthreads_sha256d,
-	.opencl_min_oclthreads =       0x20,  // intensity -10
-	.opencl_max_oclthreads = 0x20000000,  // intensity  14
-	.opencl_get_default_kernel_file = opencl_get_default_kernel_file_sha256d,
-#endif
-};
-#endif
-
-
 #ifdef USE_SCRYPT
-#ifdef USE_OPENCL
-static
-float opencl_oclthreads_to_intensity_scrypt(const unsigned long oclthreads)
-{
-	return log2(oclthreads);
-}
-
-static
-unsigned long opencl_intensity_to_oclthreads_scrypt(float intensity)
-{
-	return pow(2, intensity);
-}
-
-static
-char *opencl_get_default_kernel_file_scrypt(const struct mining_algorithm * const malgo, struct cgpu_info * const cgpu, struct _clState * const clState)
-{
-	return strdup("scrypt");
-}
-#endif
-
-static struct mining_algorithm malgo_scrypt = {
-	.name = "scrypt",
-	.aliases = "scrypt",
-	
-	.algo = POW_SCRYPT,
-	.ui_skip_hash_bytes = 2,
-	.reasonable_low_nonce_diff = 1./0x10000,
-	
-	.hash_data_f = scrypt_hash_data,
-	
-#ifdef USE_OPENCL
-	.opencl_oclthreads_to_intensity = opencl_oclthreads_to_intensity_scrypt,
-	.opencl_intensity_to_oclthreads = opencl_intensity_to_oclthreads_scrypt,
-	.opencl_min_oclthreads =      0x100,  // intensity   8
-	.opencl_max_oclthreads = 0x20000000,  // intensity  31
-	.opencl_get_default_kernel_file = opencl_get_default_kernel_file_scrypt,
-#endif
-};
+extern struct mining_algorithm malgo_scrypt;
 
 static
 const char *set_malgo_scrypt()
@@ -1162,21 +1041,8 @@ const char *set_malgo_scrypt()
 	goal_set_malgo(get_mining_goal("default"), &malgo_scrypt);
 	return NULL;
 }
-
 #endif
 
-static
-__attribute__((constructor))
-void init_mining_goals(struct mining_goal_info * const goal, const struct mining_algorithm * const malgo)
-{
-#ifdef USE_SHA256D
-	LL_APPEND(mining_algorithms, (&malgo_sha256d));
-#endif
-#ifdef USE_SCRYPT
-	LL_APPEND(mining_algorithms, (&malgo_scrypt));
-#endif
-}
-
 static
 int mining_goals_name_cmp(const struct mining_goal_info * const a, const struct mining_goal_info * const b)
 {
@@ -1197,6 +1063,8 @@ void blkchain_init_block(struct blockchain_info * const blkchain)
 	blkchain->currentblk = dummy_block;
 }
 
+extern struct mining_algorithm malgo_sha256d;
+
 struct mining_goal_info *get_mining_goal(const char * const name)
 {
 	static unsigned next_goal_id;

+ 0 - 19
scrypt.h

@@ -1,19 +0,0 @@
-#ifndef SCRYPT_H
-#define SCRYPT_H
-
-#include <stdint.h>
-
-#include "miner.h"
-
-#ifdef USE_SCRYPT
-extern void test_scrypt(void);
-extern void scrypt_hash_data(void *digest, const void *data);
-extern void scrypt_regenhash(struct work *work);
-
-#else /* USE_SCRYPT */
-static inline void scrypt_regenhash(__maybe_unused struct work *work)
-{
-}
-#endif /* USE_SCRYPT */
-
-#endif /* SCRYPT_H */

+ 0 - 13
util.c

@@ -790,19 +790,6 @@ char *ucs2_to_utf8_dup(uint16_t * const in, size_t sz)
 	return out;
 }
 
-#ifdef USE_SHA256D
-void hash_data(void *out_hash, const void *data)
-{
-	unsigned char blkheader[80];
-	
-	// data is past the first SHA256 step (padding and interpreting as big endian on a little endian platform), so we need to flip each 32-bit chunk around to get the original input block header
-	swap32yes(blkheader, data, 80 / 4);
-	
-	// double-SHA256 to get the block hash
-	gen_hash(blkheader, out_hash, 80);
-}
-#endif
-
 // Example output: 0000000000000000000000000000000000000000000000000000ffff00000000 (bdiff 1)
 void real_block_target(unsigned char *target, const unsigned char *data)
 {

+ 0 - 1
util.h

@@ -194,7 +194,6 @@ extern char *ucs2_to_utf8_dup(uint16_t *in, size_t sz);
 }while(0)
 
 extern void gen_hash(unsigned char *data, unsigned char *hash, int len);
-extern void hash_data(void *digest, const void *data);
 extern void real_block_target(unsigned char *target, const unsigned char *data);
 extern bool hash_target_check(const unsigned char *hash, const unsigned char *target);
 extern bool hash_target_check_v(const unsigned char *hash, const unsigned char *target);