From 3df545cfc56c1af4ae4068f74f0e4f0662f8e8ea Mon Sep 17 00:00:00 2001 From: XMRig Date: Mon, 12 Jun 2017 16:19:07 +0300 Subject: [PATCH] Initial MSVC support. --- CMakeLists.txt | 34 +- src/3rdparty/align.h | 33 + src/3rdparty/getopt/getopt.h | 653 ++++++++++++++++++ src/3rdparty/jansson/jansson_private_config.h | 28 +- src/App.cpp | 2 +- src/Console.cpp | 12 +- src/Console.h | 4 +- src/Cpu_stub.cpp | 15 +- src/Mem.h | 5 +- src/Mem_win.cpp | 6 +- src/Options.cpp | 12 +- src/Summary.cpp | 4 +- src/crypto/CryptoNight.h | 9 +- src/crypto/CryptoNight_p.h | 21 +- src/net/Job.h | 7 +- src/net/Url.cpp | 7 +- src/workers/Handle.cpp | 4 +- src/workers/Handle.h | 6 +- src/workers/SingleWorker.cpp | 4 +- src/workers/Workers.cpp | 66 +- src/workers/Workers.h | 9 +- 21 files changed, 845 insertions(+), 96 deletions(-) create mode 100644 src/3rdparty/align.h create mode 100644 src/3rdparty/getopt/getopt.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 753ee492..ed96a8c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ option(WITH_LIBCPUID "Use Libcpuid" ON) option(WITH_AEON "CryptoNight-Lite support" ON) set(HEADERS + src/3rdparty/align.h src/App.h src/Console.h src/Cpu.h @@ -19,12 +20,12 @@ set(HEADERS src/net/Url.h src/Options.h src/Summary.h + src/version.h src/workers/Handle.h src/workers/SingleWorker.h src/workers/Telemetry.h src/workers/Worker.h src/workers/Workers.h - src/version.h ) set(HEADERS_CRYPTO @@ -86,7 +87,6 @@ else() set(EXTRA_LIBS pthread) endif() -add_definitions(/D_GNU_SOURCE) add_definitions(/DUNICODE) #add_definitions(/DAPP_DEBUG) @@ -98,17 +98,31 @@ if ("${CMAKE_BUILD_TYPE}" STREQUAL "") set(CMAKE_BUILD_TYPE Release) endif() -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wall -fno-exceptions") -set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -fno-exceptions -fno-rtti") -set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -s -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") -#set(CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -gdwarf-2") -#set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fprofile-generate") -#set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fprofile-use -fprofile-correction") +# https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html +if (CMAKE_C_COMPILER_ID STREQUAL GNU) + + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes -Wall -fno-exceptions") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") + + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -Wall -fno-exceptions -fno-rtti") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -s -funroll-loops -fvariable-expansion-in-unroller -ftree-loop-if-convert-stores -fmerge-all-constants -fbranch-target-load-optimize2") -if (WIN32) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static") + + add_definitions(/D_GNU_SOURCE) + + #set(CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -gdwarf-2") + +elseif (CMAKE_C_COMPILER_ID STREQUAL MSVC) + + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MT") + +elseif (CMAKE_C_COMPILER_ID MATCHES "Clang") + + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Ofast -funroll-loops -fvariable-expansion-in-unroller -fmerge-all-constants") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast -funroll-loops -fvariable-expansion-in-unroller -fmerge-all-constants") + endif() if (WITH_LIBCPUID) diff --git a/src/3rdparty/align.h b/src/3rdparty/align.h new file mode 100644 index 00000000..b61179b9 --- /dev/null +++ b/src/3rdparty/align.h @@ -0,0 +1,33 @@ +/* XMRig + * Copyright 2010 Jeff Garzik + * Copyright 2012-2014 pooler + * Copyright 2014 Lucas Jones + * Copyright 2014-2016 Wolf9466 + * Copyright 2016 Jay D Dee + * Copyright 2016-2017 XMRig + * + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __ALIGN_H__ +#define __ALIGN_H__ + +#ifdef _MSC_VER +# define VAR_ALIGN(x, decl) __declspec(align(x)) decl +#else +# define VAR_ALIGN(x, decl) decl __attribute__ ((aligned(x))) +#endif + +#endif /* __ALIGN_H__ */ diff --git a/src/3rdparty/getopt/getopt.h b/src/3rdparty/getopt/getopt.h new file mode 100644 index 00000000..0cb88895 --- /dev/null +++ b/src/3rdparty/getopt/getopt.h @@ -0,0 +1,653 @@ +#ifndef __GETOPT_H__ +/** + * DISCLAIMER + * This file is part of the mingw-w64 runtime package. + * + * The mingw-w64 runtime package and its code is distributed in the hope that it + * will be useful but WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESSED OR + * IMPLIED ARE HEREBY DISCLAIMED. This includes but is not limited to + * warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + /* + * Copyright (c) 2002 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Sponsored in part by the Defense Advanced Research Projects + * Agency (DARPA) and Air Force Research Laboratory, Air Force + * Materiel Command, USAF, under agreement number F39502-99-1-0512. + */ +/*- + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dieter Baron and Thomas Klausner. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma warning(disable:4996); + +#define __GETOPT_H__ + +/* All the headers include this file. */ +#include +#include +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define REPLACE_GETOPT /* use this getopt as the system getopt(3) */ + +#ifdef REPLACE_GETOPT +int opterr = 1; /* if error message should be printed */ +int optind = 1; /* index into parent argv vector */ +int optopt = '?'; /* character checked for validity */ +#undef optreset /* see getopt.h */ +#define optreset __mingw_optreset +int optreset; /* reset getopt */ +char *optarg; /* argument associated with option */ +#endif + +//extern int optind; /* index of first non-option in argv */ +//extern int optopt; /* single option character, as parsed */ +//extern int opterr; /* flag to enable built-in diagnostics... */ +// /* (user may set to zero, to suppress) */ +// +//extern char *optarg; /* pointer to argument of current option */ + +#define PRINT_ERROR ((opterr) && (*options != ':')) + +#define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */ +#define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */ +#define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */ + +/* return values */ +#define BADCH (int)'?' +#define BADARG ((*options == ':') ? (int)':' : (int)'?') +#define INORDER (int)1 + +#ifndef __CYGWIN__ +#define __progname __argv[0] +#else +extern char __declspec(dllimport) *__progname; +#endif + +#ifdef __CYGWIN__ +static char EMSG[] = ""; +#else +#define EMSG "" +#endif + +static int getopt_internal(int, char * const *, const char *, + const struct option *, int *, int); +static int parse_long_options(char * const *, const char *, + const struct option *, int *, int); +static int gcd(int, int); +static void permute_args(int, int, int, char * const *); + +static char *place = EMSG; /* option letter processing */ + +/* XXX: set optreset to 1 rather than these two */ +static int nonopt_start = -1; /* first non option argument (for permute) */ +static int nonopt_end = -1; /* first option after non options (for permute) */ + +/* Error messages */ +static const char recargchar[] = "option requires an argument -- %c"; +static const char recargstring[] = "option requires an argument -- %s"; +static const char ambig[] = "ambiguous option -- %.*s"; +static const char noarg[] = "option doesn't take an argument -- %.*s"; +static const char illoptchar[] = "unknown option -- %c"; +static const char illoptstring[] = "unknown option -- %s"; + +static void +_vwarnx(const char *fmt,va_list ap) +{ + (void)fprintf(stderr,"%s: ",__progname); + if (fmt != NULL) + (void)vfprintf(stderr,fmt,ap); + (void)fprintf(stderr,"\n"); +} + +static void +warnx(const char *fmt,...) +{ + va_list ap; + va_start(ap,fmt); + _vwarnx(fmt,ap); + va_end(ap); +} + +/* + * Compute the greatest common divisor of a and b. + */ +static int +gcd(int a, int b) +{ + int c; + + c = a % b; + while (c != 0) { + a = b; + b = c; + c = a % b; + } + + return (b); +} + +/* + * Exchange the block from nonopt_start to nonopt_end with the block + * from nonopt_end to opt_end (keeping the same order of arguments + * in each block). + */ +static void +permute_args(int panonopt_start, int panonopt_end, int opt_end, + char * const *nargv) +{ + int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; + char *swap; + + /* + * compute lengths of blocks and number and size of cycles + */ + nnonopts = panonopt_end - panonopt_start; + nopts = opt_end - panonopt_end; + ncycle = gcd(nnonopts, nopts); + cyclelen = (opt_end - panonopt_start) / ncycle; + + for (i = 0; i < ncycle; i++) { + cstart = panonopt_end+i; + pos = cstart; + for (j = 0; j < cyclelen; j++) { + if (pos >= panonopt_end) + pos -= nnonopts; + else + pos += nopts; + swap = nargv[pos]; + /* LINTED const cast */ + ((char **) nargv)[pos] = nargv[cstart]; + /* LINTED const cast */ + ((char **)nargv)[cstart] = swap; + } + } +} + +#ifdef REPLACE_GETOPT +/* + * getopt -- + * Parse argc/argv argument vector. + * + * [eventually this will replace the BSD getopt] + */ +int +getopt(int nargc, char * const *nargv, const char *options) +{ + + /* + * We don't pass FLAG_PERMUTE to getopt_internal() since + * the BSD getopt(3) (unlike GNU) has never done this. + * + * Furthermore, since many privileged programs call getopt() + * before dropping privileges it makes sense to keep things + * as simple (and bug-free) as possible. + */ + return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); +} +#endif /* REPLACE_GETOPT */ + +//extern int getopt(int nargc, char * const *nargv, const char *options); + +#ifdef _BSD_SOURCE +/* + * BSD adds the non-standard `optreset' feature, for reinitialisation + * of `getopt' parsing. We support this feature, for applications which + * proclaim their BSD heritage, before including this header; however, + * to maintain portability, developers are advised to avoid it. + */ +# define optreset __mingw_optreset +extern int optreset; +#endif +#ifdef __cplusplus +} +#endif +/* + * POSIX requires the `getopt' API to be specified in `unistd.h'; + * thus, `unistd.h' includes this header. However, we do not want + * to expose the `getopt_long' or `getopt_long_only' APIs, when + * included in this manner. Thus, close the standard __GETOPT_H__ + * declarations block, and open an additional __GETOPT_LONG_H__ + * specific block, only when *not* __UNISTD_H_SOURCED__, in which + * to declare the extended API. + */ +#endif /* !defined(__GETOPT_H__) */ + +#if !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__) +#define __GETOPT_LONG_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +struct option /* specification for a long form option... */ +{ + const char *name; /* option name, without leading hyphens */ + int has_arg; /* does it take an argument? */ + int *flag; /* where to save its status, or NULL */ + int val; /* its associated status value */ +}; + +enum /* permitted values for its `has_arg' field... */ +{ + no_argument = 0, /* option never takes an argument */ + required_argument, /* option always requires an argument */ + optional_argument /* option may take an argument */ +}; + +/* + * parse_long_options -- + * Parse long options in argc/argv argument vector. + * Returns -1 if short_too is set and the option does not match long_options. + */ +static int +parse_long_options(char * const *nargv, const char *options, + const struct option *long_options, int *idx, int short_too) +{ + char *current_argv, *has_equal; + size_t current_argv_len; + int i, ambiguous, match; + +#define IDENTICAL_INTERPRETATION(_x, _y) \ + (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \ + long_options[(_x)].flag == long_options[(_y)].flag && \ + long_options[(_x)].val == long_options[(_y)].val) + + current_argv = place; + match = -1; + ambiguous = 0; + + optind++; + + if ((has_equal = strchr(current_argv, '=')) != NULL) { + /* argument found (--option=arg) */ + current_argv_len = has_equal - current_argv; + has_equal++; + } else + current_argv_len = strlen(current_argv); + + for (i = 0; long_options[i].name; i++) { + /* find matching long option */ + if (strncmp(current_argv, long_options[i].name, + current_argv_len)) + continue; + + if (strlen(long_options[i].name) == current_argv_len) { + /* exact match */ + match = i; + ambiguous = 0; + break; + } + /* + * If this is a known short option, don't allow + * a partial match of a single character. + */ + if (short_too && current_argv_len == 1) + continue; + + if (match == -1) /* partial match */ + match = i; + else if (!IDENTICAL_INTERPRETATION(i, match)) + ambiguous = 1; + } + if (ambiguous) { + /* ambiguous abbreviation */ + if (PRINT_ERROR) + warnx(ambig, (int)current_argv_len, + current_argv); + optopt = 0; + return (BADCH); + } + if (match != -1) { /* option found */ + if (long_options[match].has_arg == no_argument + && has_equal) { + if (PRINT_ERROR) + warnx(noarg, (int)current_argv_len, + current_argv); + /* + * XXX: GNU sets optopt to val regardless of flag + */ + if (long_options[match].flag == NULL) + optopt = long_options[match].val; + else + optopt = 0; + return (BADARG); + } + if (long_options[match].has_arg == required_argument || + long_options[match].has_arg == optional_argument) { + if (has_equal) + optarg = has_equal; + else if (long_options[match].has_arg == + required_argument) { + /* + * optional argument doesn't use next nargv + */ + optarg = nargv[optind++]; + } + } + if ((long_options[match].has_arg == required_argument) + && (optarg == NULL)) { + /* + * Missing argument; leading ':' indicates no error + * should be generated. + */ + if (PRINT_ERROR) + warnx(recargstring, + current_argv); + /* + * XXX: GNU sets optopt to val regardless of flag + */ + if (long_options[match].flag == NULL) + optopt = long_options[match].val; + else + optopt = 0; + --optind; + return (BADARG); + } + } else { /* unknown option */ + if (short_too) { + --optind; + return (-1); + } + if (PRINT_ERROR) + warnx(illoptstring, current_argv); + optopt = 0; + return (BADCH); + } + if (idx) + *idx = match; + if (long_options[match].flag) { + *long_options[match].flag = long_options[match].val; + return (0); + } else + return (long_options[match].val); +#undef IDENTICAL_INTERPRETATION +} + +/* + * getopt_internal -- + * Parse argc/argv argument vector. Called by user level routines. + */ +static int +getopt_internal(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx, int flags) +{ + char *oli; /* option letter list index */ + int optchar, short_too; + static int posixly_correct = -1; + + if (options == NULL) + return (-1); + + /* + * XXX Some GNU programs (like cvs) set optind to 0 instead of + * XXX using optreset. Work around this braindamage. + */ + if (optind == 0) + optind = optreset = 1; + + /* + * Disable GNU extensions if POSIXLY_CORRECT is set or options + * string begins with a '+'. + * + * CV, 2009-12-14: Check POSIXLY_CORRECT anew if optind == 0 or + * optreset != 0 for GNU compatibility. + */ + if (posixly_correct == -1 || optreset != 0) + posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); + if (*options == '-') + flags |= FLAG_ALLARGS; + else if (posixly_correct || *options == '+') + flags &= ~FLAG_PERMUTE; + if (*options == '+' || *options == '-') + options++; + + optarg = NULL; + if (optreset) + nonopt_start = nonopt_end = -1; +start: + if (optreset || !*place) { /* update scanning pointer */ + optreset = 0; + if (optind >= nargc) { /* end of argument vector */ + place = EMSG; + if (nonopt_end != -1) { + /* do permutation, if we have to */ + permute_args(nonopt_start, nonopt_end, + optind, nargv); + optind -= nonopt_end - nonopt_start; + } + else if (nonopt_start != -1) { + /* + * If we skipped non-options, set optind + * to the first of them. + */ + optind = nonopt_start; + } + nonopt_start = nonopt_end = -1; + return (-1); + } + if (*(place = nargv[optind]) != '-' || + (place[1] == '\0' && strchr(options, '-') == NULL)) { + place = EMSG; /* found non-option */ + if (flags & FLAG_ALLARGS) { + /* + * GNU extension: + * return non-option as argument to option 1 + */ + optarg = nargv[optind++]; + return (INORDER); + } + if (!(flags & FLAG_PERMUTE)) { + /* + * If no permutation wanted, stop parsing + * at first non-option. + */ + return (-1); + } + /* do permutation */ + if (nonopt_start == -1) + nonopt_start = optind; + else if (nonopt_end != -1) { + permute_args(nonopt_start, nonopt_end, + optind, nargv); + nonopt_start = optind - + (nonopt_end - nonopt_start); + nonopt_end = -1; + } + optind++; + /* process next argument */ + goto start; + } + if (nonopt_start != -1 && nonopt_end == -1) + nonopt_end = optind; + + /* + * If we have "-" do nothing, if "--" we are done. + */ + if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { + optind++; + place = EMSG; + /* + * We found an option (--), so if we skipped + * non-options, we have to permute. + */ + if (nonopt_end != -1) { + permute_args(nonopt_start, nonopt_end, + optind, nargv); + optind -= nonopt_end - nonopt_start; + } + nonopt_start = nonopt_end = -1; + return (-1); + } + } + + /* + * Check long options if: + * 1) we were passed some + * 2) the arg is not just "-" + * 3) either the arg starts with -- we are getopt_long_only() + */ + if (long_options != NULL && place != nargv[optind] && + (*place == '-' || (flags & FLAG_LONGONLY))) { + short_too = 0; + if (*place == '-') + place++; /* --foo long option */ + else if (*place != ':' && strchr(options, *place) != NULL) + short_too = 1; /* could be short option too */ + + optchar = parse_long_options(nargv, options, long_options, + idx, short_too); + if (optchar != -1) { + place = EMSG; + return (optchar); + } + } + + if ((optchar = (int)*place++) == (int)':' || + (optchar == (int)'-' && *place != '\0') || + (oli = (char*)strchr(options, optchar)) == NULL) { + /* + * If the user specified "-" and '-' isn't listed in + * options, return -1 (non-option) as per POSIX. + * Otherwise, it is an unknown option character (or ':'). + */ + if (optchar == (int)'-' && *place == '\0') + return (-1); + if (!*place) + ++optind; + if (PRINT_ERROR) + warnx(illoptchar, optchar); + optopt = optchar; + return (BADCH); + } + if (long_options != NULL && optchar == 'W' && oli[1] == ';') { + /* -W long-option */ + if (*place) /* no space */ + /* NOTHING */; + else if (++optind >= nargc) { /* no arg */ + place = EMSG; + if (PRINT_ERROR) + warnx(recargchar, optchar); + optopt = optchar; + return (BADARG); + } else /* white space */ + place = nargv[optind]; + optchar = parse_long_options(nargv, options, long_options, + idx, 0); + place = EMSG; + return (optchar); + } + if (*++oli != ':') { /* doesn't take argument */ + if (!*place) + ++optind; + } else { /* takes (optional) argument */ + optarg = NULL; + if (*place) /* no white space */ + optarg = place; + else if (oli[1] != ':') { /* arg not optional */ + if (++optind >= nargc) { /* no arg */ + place = EMSG; + if (PRINT_ERROR) + warnx(recargchar, optchar); + optopt = optchar; + return (BADARG); + } else + optarg = nargv[optind]; + } + place = EMSG; + ++optind; + } + /* dump back option letter */ + return (optchar); +} + +/* + * getopt_long -- + * Parse argc/argv argument vector. + */ +int +getopt_long(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx) +{ + + return (getopt_internal(nargc, nargv, options, long_options, idx, + FLAG_PERMUTE)); +} + +/* + * getopt_long_only -- + * Parse argc/argv argument vector. + */ +int +getopt_long_only(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx) +{ + + return (getopt_internal(nargc, nargv, options, long_options, idx, + FLAG_PERMUTE|FLAG_LONGONLY)); +} + +//extern int getopt_long(int nargc, char * const *nargv, const char *options, +// const struct option *long_options, int *idx); +//extern int getopt_long_only(int nargc, char * const *nargv, const char *options, +// const struct option *long_options, int *idx); +/* + * Previous MinGW implementation had... + */ +#ifndef HAVE_DECL_GETOPT +/* + * ...for the long form API only; keep this for compatibility. + */ +# define HAVE_DECL_GETOPT 1 +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__) */ diff --git a/src/3rdparty/jansson/jansson_private_config.h b/src/3rdparty/jansson/jansson_private_config.h index 2b17f182..671993d9 100644 --- a/src/3rdparty/jansson/jansson_private_config.h +++ b/src/3rdparty/jansson/jansson_private_config.h @@ -2,7 +2,9 @@ /* jansson_private_config.h.in. Generated from configure.ac by autoheader. */ /* Define to 1 if gcc's __atomic builtins are available */ -#define HAVE_ATOMIC_BUILTINS 1 +#ifndef _MSC_VER +# define HAVE_ATOMIC_BUILTINS 1 +#endif /* Define to 1 if you have the `close' function. */ #define HAVE_CLOSE 1 @@ -20,7 +22,9 @@ #define HAVE_GETPID 1 /* Define to 1 if you have the `gettimeofday' function. */ -#define HAVE_GETTIMEOFDAY 1 +#ifndef _MSC_VER +# define HAVE_GETTIMEOFDAY 1 +#endif /* Define to 1 if you have the header file. */ #define HAVE_INTTYPES_H 1 @@ -44,10 +48,14 @@ #define HAVE_READ 1 /* Define to 1 if you have the header file. */ -#define HAVE_SCHED_H 1 +#ifndef _MSC_VER +# define HAVE_SCHED_H 1 +#endif /* Define to 1 if you have the `sched_yield' function. */ -#define HAVE_SCHED_YIELD 1 +#ifndef _MSC_VER +# define HAVE_SCHED_YIELD 1 +#endif /* Define to 1 if you have the header file. */ #define HAVE_STDINT_H 1 @@ -68,19 +76,25 @@ #define HAVE_SYNC_BUILTINS 1 /* Define to 1 if you have the header file. */ -#define HAVE_SYS_PARAM_H 1 +#ifndef _MSC_VER +# define HAVE_SYS_PARAM_H 1 +#endif /* Define to 1 if you have the header file. */ #define HAVE_SYS_STAT_H 1 /* Define to 1 if you have the header file. */ -#define HAVE_SYS_TIME_H 1 +#ifndef _MSC_VER +# define HAVE_SYS_TIME_H 1 +#endif /* Define to 1 if you have the header file. */ #define HAVE_SYS_TYPES_H 1 /* Define to 1 if you have the header file. */ -#define HAVE_UNISTD_H 1 +#ifndef _MSC_VER +# define HAVE_UNISTD_H 1 +#endif /* Define to 1 if the system has the type 'unsigned long long int'. */ #define HAVE_UNSIGNED_LONG_LONG_INT 1 diff --git a/src/App.cpp b/src/App.cpp index 4078f4b7..b897be8a 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -60,7 +60,7 @@ App::~App() } -App::exec() +int App::exec() { if (!m_options->isReady()) { return 0; diff --git a/src/Console.cpp b/src/Console.cpp index fd4ff4a5..b3b7ed3e 100644 --- a/src/Console.cpp +++ b/src/Console.cpp @@ -22,8 +22,10 @@ */ +#include #include + #ifdef WIN32 # include # include "3rdparty/winansi.h" @@ -97,12 +99,12 @@ void Console::message(Console::Level level, const char* fmt, ...) m_colors ? kCL_N : "" ); - pthread_mutex_lock(&m_mutex); + uv_mutex_lock(&m_mutex); vfprintf(stdout, buf, ap); fflush(stdout); - pthread_mutex_unlock(&m_mutex); + uv_mutex_unlock(&m_mutex); va_end(ap); } @@ -121,12 +123,12 @@ void Console::text(const char* fmt, ...) m_colors ? kCL_N : "" ); - pthread_mutex_lock(&m_mutex); + uv_mutex_lock(&m_mutex); vfprintf(stdout, buf, ap); fflush(stdout); - pthread_mutex_unlock(&m_mutex); + uv_mutex_unlock(&m_mutex); va_end(ap); } @@ -135,5 +137,5 @@ void Console::text(const char* fmt, ...) Console::Console() : m_colors(true) { - pthread_mutex_init(&m_mutex, nullptr); + uv_mutex_init(&m_mutex); } diff --git a/src/Console.h b/src/Console.h index edd5ae8c..73e04706 100644 --- a/src/Console.h +++ b/src/Console.h @@ -25,7 +25,7 @@ #define __CONSOLE_H__ -#include +#include class Console @@ -61,7 +61,7 @@ private: static Console *m_self; bool m_colors; - pthread_mutex_t m_mutex; + uv_mutex_t m_mutex; }; diff --git a/src/Cpu_stub.cpp b/src/Cpu_stub.cpp index a04bce26..4bf0ba08 100644 --- a/src/Cpu_stub.cpp +++ b/src/Cpu_stub.cpp @@ -22,7 +22,12 @@ */ -#include +#ifdef _MSC_VER +# include +#else +# include +#endif + #include @@ -45,7 +50,7 @@ static inline void cpuid(int level, int output[4]) { int a, b, c, d; - __cpuid_count(level, 0, a, b, c, d); + //__cpuid_count(level, 0, a, b, c, d); output[0] = a; output[1] = b; @@ -73,7 +78,8 @@ static inline bool has_aes_ni() int cpu_info[4] = { 0 }; cpuid(PROCESSOR_INFO, cpu_info); - return cpu_info[ECX_Reg] & bit_AES; + return false; + //return cpu_info[ECX_Reg] & bit_AES; } @@ -81,7 +87,8 @@ static inline bool has_bmi2() { int cpu_info[4] = { 0 }; cpuid(EXTENDED_FEATURES, cpu_info); - return cpu_info[EBX_Reg] & bit_BMI2; + return false; + //return cpu_info[EBX_Reg] & bit_BMI2; } diff --git a/src/Mem.h b/src/Mem.h index 9a438a17..89635e33 100644 --- a/src/Mem.h +++ b/src/Mem.h @@ -28,6 +28,9 @@ #include +#include "align.h" + + struct cryptonight_ctx; @@ -55,7 +58,7 @@ private: static int m_flags; static int m_threads; static size_t m_offset; - static uint8_t *m_memory __attribute__((aligned(16))); + VAR_ALIGN(16, static uint8_t *m_memory); # ifndef XMRIG_NO_AEON static cryptonight_ctx *createLite(int threadId); diff --git a/src/Mem_win.cpp b/src/Mem_win.cpp index f9a7ab3d..c5e606f7 100644 --- a/src/Mem_win.cpp +++ b/src/Mem_win.cpp @@ -26,8 +26,12 @@ #include #include #include -#include +#ifdef __GNUC__ +# include +#else +# include +#endif #include "Console.h" #include "crypto/CryptoNight.h" diff --git a/src/Options.cpp b/src/Options.cpp index a2702a65..9ba9acf7 100644 --- a/src/Options.cpp +++ b/src/Options.cpp @@ -24,8 +24,12 @@ #include #include -#include +#ifdef _MSC_VER +#include "getopt/getopt.h" +#else +#include +#endif #include "Console.h" #include "Cpu.h" @@ -387,7 +391,7 @@ void Options::showUsage(int status) const void Options::showVersion() { - printf(APP_NAME " " APP_VERSION "\n built on " __DATE__ + /* printf(APP_NAME " " APP_VERSION "\n built on " __DATE__) #ifdef __GNUC__ " with GCC"); @@ -404,7 +408,7 @@ void Options::showVersion() #ifdef __AES__ " AES-NI" #endif - "\n"); + "\n");*/ printf("\nlibuv/%s\n", uv_version_string()); printf("libjansson/%s\n", JANSSON_VERSION); @@ -438,7 +442,7 @@ bool Options::setAlgo(const char *algo) bool Options::setUserpass(const char *userpass) { - char *p = strchr(userpass, ':'); + const char *p = strchr(userpass, ':'); if (!p) { showUsage(1); return false; diff --git a/src/Summary.cpp b/src/Summary.cpp index 502747c9..7d5f920f 100644 --- a/src/Summary.cpp +++ b/src/Summary.cpp @@ -35,7 +35,7 @@ static void print_versions() { - char *buf = static_cast(alloca(16)); + char buf[16]; # ifdef __GNUC__ snprintf(buf, 16, " gcc/%d.%d.%d", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__); @@ -87,7 +87,7 @@ static void print_cpu() static void print_threads() { - char *buf = static_cast(alloca(32)); + char buf[32]; if (Options::i()->affinity() != -1L) { snprintf(buf, 32, ", affinity=0x%llX", Options::i()->affinity()); } diff --git a/src/crypto/CryptoNight.h b/src/crypto/CryptoNight.h index fbb2f975..2ed5dfb2 100644 --- a/src/crypto/CryptoNight.h +++ b/src/crypto/CryptoNight.h @@ -28,14 +28,17 @@ #include +#include "align.h" + + #define MEMORY 2097152 /* 2 MiB */ #define MEMORY_LITE 1048576 /* 1 MiB */ struct cryptonight_ctx { - uint8_t state0[200] __attribute__((aligned(16))); - uint8_t state1[200] __attribute__((aligned(16))); - uint8_t* memory __attribute__((aligned(16))); + VAR_ALIGN(16, uint8_t state0[200]); + VAR_ALIGN(16, uint8_t state1[200]); + VAR_ALIGN(16, uint8_t* memory); }; diff --git a/src/crypto/CryptoNight_p.h b/src/crypto/CryptoNight_p.h index ed24a510..b44b6616 100644 --- a/src/crypto/CryptoNight_p.h +++ b/src/crypto/CryptoNight_p.h @@ -25,7 +25,12 @@ #define __CRYPTONIGHT_P_H__ -#include +#ifdef __GNUC__ +# include +#else +# include +# define __restrict__ __restrict +#endif #include "crypto/CryptoNight.h" @@ -68,15 +73,19 @@ void (* const extra_hashes[4])(const void *, size_t, char *) = {do_blake_hash, d -#if defined(__x86_64__) +#if defined(__x86_64__) || defined(_WIN64) # define EXTRACT64(X) _mm_cvtsi128_si64(X) +# ifdef __GNUC__ static inline uint64_t __umul128(uint64_t a, uint64_t b, uint64_t* hi) { unsigned __int128 r = (unsigned __int128) a * (unsigned __int128) b; *hi = r >> 64; return (uint64_t) r; } +# else + #define __umul128 _umul128 +# endif #elif defined(__i386__) # define HI32(X) \ _mm_srli_si128((X), 4) @@ -224,7 +233,7 @@ static inline void cn_explode_scratchpad(const __m128i *input, __m128i *output) xin6 = _mm_load_si128(input + 10); xin7 = _mm_load_si128(input + 11); - for (size_t i = 0; __builtin_expect(i < MEM / sizeof(__m128i), 1); i += 8) { + for (size_t i = 0; i < MEM / sizeof(__m128i); i += 8) { aes_round(k0, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); aes_round(k1, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); aes_round(k2, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7); @@ -265,7 +274,7 @@ static inline void cn_implode_scratchpad(const __m128i *input, __m128i *output) xout6 = _mm_load_si128(output + 10); xout7 = _mm_load_si128(output + 11); - for (size_t i = 0; __builtin_expect(i < MEM / sizeof(__m128i), 1); i += 8) + for (size_t i = 0; i < MEM / sizeof(__m128i); i += 8) { xout0 = _mm_xor_si128(_mm_load_si128(input + i + 0), xout0); xout1 = _mm_xor_si128(_mm_load_si128(input + i + 1), xout1); @@ -315,7 +324,7 @@ inline void cryptonight_hash(const void *__restrict__ input, size_t size, void * uint64_t idx0 = h0[0] ^ h0[4]; - for (size_t i = 0; __builtin_expect(i < ITERATIONS, 1); i++) { + for (size_t i = 0; i < ITERATIONS; i++) { __m128i cx; cx = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); cx = _mm_aesenc_si128(cx, _mm_set_epi64x(ah0, al0)); @@ -372,7 +381,7 @@ inline void cryptonight_double_hash(const void *__restrict__ input, size_t size, uint64_t idx0 = h0[0] ^ h0[4]; uint64_t idx1 = h1[0] ^ h1[4]; - for (size_t i = 0; __builtin_expect(i < ITERATIONS, 1); i++) { + for (size_t i = 0; i < ITERATIONS; i++) { __m128i cx0 = _mm_load_si128((__m128i *) &l0[idx0 & MASK]); __m128i cx1 = _mm_load_si128((__m128i *) &l1[idx1 & MASK]); diff --git a/src/net/Job.h b/src/net/Job.h index 4f08e84c..9848f128 100644 --- a/src/net/Job.h +++ b/src/net/Job.h @@ -28,6 +28,9 @@ #include +#include "align.h" + + class Job { public: @@ -52,8 +55,8 @@ public: private: int m_poolId; - char m_id[64] __attribute__((aligned(16))); - uint8_t m_blob[84] __attribute__((aligned(16))); // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. + VAR_ALIGN(16, char m_id[64]); + VAR_ALIGN(16, uint8_t m_blob[84]); // Max blob size is 84 (75 fixed + 9 variable), aligned to 96. https://github.com/xmrig/xmrig/issues/1 Thanks fireice-uk. uint32_t m_size; uint64_t m_diff; uint64_t m_target; diff --git a/src/net/Url.cpp b/src/net/Url.cpp index 4e1dfd9f..d8264c20 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -29,6 +29,11 @@ #include "net/Url.h" +#ifdef _MSC_VER +# define strncasecmp(x,y,z) _strnicmp(x,y,z) +#endif + + Url::Url() : m_host(nullptr), m_port(3333) @@ -66,7 +71,7 @@ Url::Url(const char *url) : return; } - char *port = strchr(base, ':'); + const char *port = strchr(base, ':'); if (!port) { m_host = strdup(base); return; diff --git a/src/workers/Handle.cpp b/src/workers/Handle.cpp index 76fea588..c97d10b1 100644 --- a/src/workers/Handle.cpp +++ b/src/workers/Handle.cpp @@ -35,7 +35,7 @@ Handle::Handle(int threadId, int threads, int64_t affinity, bool nicehash) : } -void Handle::start(void *(*callback) (void *)) +void Handle::start(void (*callback) (void *)) { - pthread_create(&m_thread, nullptr, callback, this); + uv_thread_create(&m_thread, callback, this); } diff --git a/src/workers/Handle.h b/src/workers/Handle.h index 6b2b1d37..96a512e3 100644 --- a/src/workers/Handle.h +++ b/src/workers/Handle.h @@ -25,8 +25,8 @@ #define __HANDLE_H__ -#include #include +#include class IWorker; @@ -36,7 +36,7 @@ class Handle { public: Handle(int threadId, int threads, int64_t affinity, bool nicehash); - void start(void *(*callback) (void *)); + void start(void (*callback) (void *)); inline bool nicehash() const { return m_nicehash; } inline int threadId() const { return m_threadId; } @@ -51,7 +51,7 @@ private: int m_threads; int64_t m_affinity; IWorker *m_worker; - pthread_t m_thread; + uv_thread_t m_thread; }; diff --git a/src/workers/SingleWorker.cpp b/src/workers/SingleWorker.cpp index 263f0859..3969e883 100644 --- a/src/workers/SingleWorker.cpp +++ b/src/workers/SingleWorker.cpp @@ -22,11 +22,9 @@ */ -#include #include -#include "Console.h" #include "crypto/CryptoNight.h" #include "workers/SingleWorker.h" #include "workers/Workers.h" @@ -62,7 +60,7 @@ void SingleWorker::start() Workers::submit(m_result); } - sched_yield(); +// sched_yield(); } consumeJob(); diff --git a/src/workers/Workers.cpp b/src/workers/Workers.cpp index 832d7da0..b3673445 100644 --- a/src/workers/Workers.cpp +++ b/src/workers/Workers.cpp @@ -34,8 +34,6 @@ IJobResultListener *Workers::m_listener = nullptr; Job Workers::m_job; -pthread_mutex_t Workers::m_mutex; -pthread_rwlock_t Workers::m_rwlock; std::atomic Workers::m_paused; std::atomic Workers::m_sequence; std::list Workers::m_queue; @@ -43,14 +41,16 @@ std::vector Workers::m_workers; Telemetry *Workers::m_telemetry = nullptr; uint64_t Workers::m_ticks = 0; uv_async_t Workers::m_async; +uv_mutex_t Workers::m_mutex; +uv_rwlock_t Workers::m_rwlock; uv_timer_t Workers::m_timer; Job Workers::job() { - pthread_rwlock_rdlock(&m_rwlock); + uv_rwlock_rdlock(&m_rwlock); Job job = m_job; - pthread_rwlock_unlock(&m_rwlock); + uv_rwlock_rdunlock(&m_rwlock); return std::move(job); } @@ -58,9 +58,9 @@ Job Workers::job() void Workers::setJob(const Job &job) { - pthread_rwlock_wrlock(&m_rwlock); + uv_rwlock_wrlock(&m_rwlock); m_job = job; - pthread_rwlock_unlock(&m_rwlock); + uv_rwlock_wrunlock(&m_rwlock); m_sequence++; m_paused = 0; @@ -71,15 +71,15 @@ void Workers::start(int threads, int64_t affinity, bool nicehash) { m_telemetry = new Telemetry(threads); - pthread_mutex_init(&m_mutex, nullptr); - pthread_rwlock_init(&m_rwlock, nullptr); + uv_mutex_init(&m_mutex); + uv_rwlock_init(&m_rwlock); m_sequence = 0; m_paused = 1; uv_async_init(uv_default_loop(), &m_async, Workers::onResult); uv_timer_init(uv_default_loop(), &m_timer); - uv_timer_start(&m_timer, Workers::onPerfTick, 500, 500); + uv_timer_start(&m_timer, Workers::onTick, 500, 500); for (int i = 0; i < threads; ++i) { Handle *handle = new Handle(i, threads, affinity, nicehash); @@ -91,26 +91,43 @@ void Workers::start(int threads, int64_t affinity, bool nicehash) void Workers::submit(const JobResult &result) { - pthread_mutex_lock(&m_mutex); + uv_mutex_lock(&m_mutex); m_queue.push_back(result); - pthread_mutex_unlock(&m_mutex); + uv_mutex_unlock(&m_mutex); uv_async_send(&m_async); } -void *Workers::onReady(void *arg) +void Workers::onReady(void *arg) { auto handle = static_cast(arg); IWorker *worker = new SingleWorker(handle); worker->start(); - - return nullptr; } -void Workers::onPerfTick(uv_timer_t *handle) +void Workers::onResult(uv_async_t *handle) +{ + std::list results; + + uv_mutex_lock(&m_mutex); + while (!m_queue.empty()) { + results.push_back(std::move(m_queue.front())); + m_queue.pop_front(); + } + uv_mutex_unlock(&m_mutex); + + for (auto result : results) { + m_listener->onJobResult(result); + } + + results.clear(); +} + + +void Workers::onTick(uv_timer_t *handle) { for (Handle *handle : m_workers) { m_telemetry->add(handle->threadId(), handle->worker()->hashCount(), handle->worker()->timestamp()); @@ -137,22 +154,3 @@ void Workers::onPerfTick(uv_timer_t *handle) } } } - - -void Workers::onResult(uv_async_t *handle) -{ - std::list results; - - pthread_mutex_lock(&m_mutex); - while (!m_queue.empty()) { - results.push_back(std::move(m_queue.front())); - m_queue.pop_front(); - } - pthread_mutex_unlock(&m_mutex); - - for (auto result : results) { - m_listener->onJobResult(result); - } - - results.clear(); -} diff --git a/src/workers/Workers.h b/src/workers/Workers.h index f7a0e43a..5e398109 100644 --- a/src/workers/Workers.h +++ b/src/workers/Workers.h @@ -27,7 +27,6 @@ #include #include -#include #include #include @@ -55,14 +54,12 @@ public: static inline void setListener(IJobResultListener *listener) { m_listener = listener; } private: - static void *onReady(void *arg); - static void onPerfTick(uv_timer_t *handle); + static void onReady(void *arg); static void onResult(uv_async_t *handle); + static void onTick(uv_timer_t *handle); static IJobResultListener *m_listener; static Job m_job; - static pthread_mutex_t m_mutex; - static pthread_rwlock_t m_rwlock; static std::atomic m_paused; static std::atomic m_sequence; static std::list m_queue; @@ -70,6 +67,8 @@ private: static Telemetry *m_telemetry; static uint64_t m_ticks; static uv_async_t m_async; + static uv_mutex_t m_mutex; + static uv_rwlock_t m_rwlock; static uv_timer_t m_timer; };