forked from ethereum-mining/ethminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonData.cpp
201 lines (167 loc) · 5.92 KB
/
CommonData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
This file is part of ethminer.
ethminer 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.
ethminer 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 ethminer. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib>
#include "CommonData.h"
#include "Exceptions.h"
using namespace std;
using namespace dev;
int dev::fromHex(char _i, WhenError _throw)
{
if (_i >= '0' && _i <= '9')
return _i - '0';
if (_i >= 'a' && _i <= 'f')
return _i - 'a' + 10;
if (_i >= 'A' && _i <= 'F')
return _i - 'A' + 10;
if (_throw == WhenError::Throw)
BOOST_THROW_EXCEPTION(BadHexCharacter() << errinfo_invalidSymbol(_i));
else
return -1;
}
bytes dev::fromHex(std::string const& _s, WhenError _throw)
{
unsigned s = (_s[0] == '0' && _s[1] == 'x') ? 2 : 0;
std::vector<uint8_t> ret;
ret.reserve((_s.size() - s + 1) / 2);
if (_s.size() % 2)
{
int h = fromHex(_s[s++], WhenError::DontThrow);
if (h != -1)
ret.push_back(h);
else if (_throw == WhenError::Throw)
BOOST_THROW_EXCEPTION(BadHexCharacter());
else
return bytes();
}
for (unsigned i = s; i < _s.size(); i += 2)
{
int h = fromHex(_s[i], WhenError::DontThrow);
int l = fromHex(_s[i + 1], WhenError::DontThrow);
if (h != -1 && l != -1)
ret.push_back((byte)(h * 16 + l));
else if (_throw == WhenError::Throw)
BOOST_THROW_EXCEPTION(BadHexCharacter());
else
return bytes();
}
return ret;
}
bool dev::setenv(const char name[], const char value[], bool override)
{
#if _WIN32
if (!override && std::getenv(name) != nullptr)
return true;
return ::_putenv_s(name, value) == 0;
#else
return ::setenv(name, value, override ? 1 : 0) == 0;
#endif
}
std::string dev::getTargetFromDiff(double diff, HexPrefix _prefix)
{
using namespace boost::multiprecision;
using BigInteger = boost::multiprecision::cpp_int;
static BigInteger base("0x00000000ffff0000000000000000000000000000000000000000000000000000");
BigInteger product;
if (diff == 0)
{
product = BigInteger("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
}
else
{
diff = 1 / diff;
BigInteger idiff(diff);
product = base * idiff;
std::string sdiff = boost::lexical_cast<std::string>(diff);
size_t ldiff = sdiff.length();
size_t offset = sdiff.find(".");
if (offset != std::string::npos)
{
// Number of decimal places
size_t precision = (ldiff - 1) - offset;
// Effective sequence of decimal places
string decimals = sdiff.substr(offset + 1);
// Strip leading zeroes. If a string begins with
// 0 or 0x boost parser considers it hex
decimals = decimals.erase(0, decimals.find_first_not_of('0'));
// Build up the divisor as string - just in case
// parser does some implicit conversion with 10^precision
string decimalDivisor = "1";
decimalDivisor.resize(precision + 1, '0');
// This is the multiplier for the decimal part
BigInteger multiplier(decimals);
// This is the divisor for the decimal part
BigInteger divisor(decimalDivisor);
BigInteger decimalproduct;
decimalproduct = base * multiplier;
decimalproduct /= divisor;
// Add the computed decimal part
// to product
product += decimalproduct;
}
}
// Normalize to 64 chars hex with "0x" prefix
stringstream ss;
ss << (_prefix == HexPrefix::Add ? "0x" : "") << setw(64) << setfill('0') << std::hex
<< product;
string target = ss.str();
boost::algorithm::to_lower(target);
return target;
}
double dev::getHashesToTarget(string _target)
{
using namespace boost::multiprecision;
using BigInteger = boost::multiprecision::cpp_int;
static BigInteger dividend(
"0xffff000000000000000000000000000000000000000000000000000000000000");
BigInteger divisor(_target);
return double(dividend / divisor);
}
std::string dev::getScaledSize(double _value, double _divisor, int _precision, string _sizes[],
size_t _numsizes, ScaleSuffix _suffix)
{
double _newvalue = _value;
size_t i = 0;
while (_newvalue > _divisor && i <= (_numsizes - 1))
{
_newvalue /= _divisor;
i++;
}
std::stringstream _ret;
_ret << fixed << setprecision(_precision) << _newvalue;
if (_suffix == ScaleSuffix::Add)
_ret << " " << _sizes[i];
return _ret.str();
}
std::string dev::getFormattedHashes(double _hr, ScaleSuffix _suffix, int _precision)
{
static string suffixes[] = {"h", "Kh", "Mh", "Gh"};
return dev::getScaledSize(_hr, 1000.0, _precision, suffixes, 4, _suffix);
}
std::string dev::getFormattedMemory(double _mem, ScaleSuffix _suffix, int _precision)
{
static string suffixes[] = {"B", "KB", "MB", "GB"};
return dev::getScaledSize(_mem, 1024.0, _precision, suffixes, 4, _suffix);
}
std::string dev::padLeft(std::string _value, size_t _length, char _fillChar)
{
if (_length > _value.size())
_value.insert(0, (_length - _value.size()), _fillChar);
return _value;
}
std::string dev::padRight(std::string _value, size_t _length, char _fillChar)
{
if (_length > _value.size())
_value.resize(_length, _fillChar);
return _value;
}