-
Notifications
You must be signed in to change notification settings - Fork 19
/
util.h
134 lines (103 loc) · 3.63 KB
/
util.h
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
#ifndef COINSHIELD_UTIL_H
#define COINSHIELD_UTIL_H
#include <boost/date_time/posix_time/posix_time.hpp>
#include "hash/templates.h"
#include "bignum.h"
#if (defined _WIN32 || defined WIN32) && !defined __MINGW32__
#include <conio.h>
#include <time.h>
typedef int pid_t;
#define aligned_alloc(a, s) malloc(s)
#define gmtime_r(now, tm_time) _gmtime64_s(tm_time, now)
typedef unsigned long int pthread_t;
#elif !defined __MINGW32__
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/resource.h>
#endif
#include <string>
#include <vector>
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/filesystem.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <gmp.h>
#define loop for(;;)
#define BEGIN(a) ((char*)&(a))
#define END(a) ((char*)&((&(a))[1]))
#define Sleep(a) boost::this_thread::sleep(boost::posix_time::milliseconds(a))
#define LOCK(a) boost::lock_guard<boost::mutex> lock(a)
int GetTotalCores();
/** Convert an mpz into 64 bit unsigned integer. Used in replacement of mpz_get_ui. **/
uint64_t mpz2uint64(mpz_t n);
/** Convert a Bignum into Mpz integer for GMP. **/
int bignum2mpz(const BIGNUM *bn, mpz_t g);
/** Convert a 32 bit Unsigned Integer to Byte Vector using Bitwise Shifts. **/
inline std::vector<unsigned char> uint2bytes(unsigned int UINT)
{
std::vector<unsigned char> BYTES(4, 0);
BYTES[0] = UINT >> 24;
BYTES[1] = UINT >> 16;
BYTES[2] = UINT >> 8;
BYTES[3] = UINT;
return BYTES;
}
/** Convert a byte stream into unsigned integer 32 bit. **/
inline unsigned int bytes2uint(std::vector<unsigned char> BYTES, int nOffset = 0) { return (BYTES[0 + nOffset] << 24) + (BYTES[1 + nOffset] << 16) + (BYTES[2 + nOffset] << 8) + BYTES[3 + nOffset]; }
/** Convert a 64 bit Unsigned Integer to Byte Vector using Bitwise Shifts. **/
inline std::vector<unsigned char> uint2bytes64(uint64 UINT)
{
std::vector<unsigned char> INTS[2];
INTS[0] = uint2bytes((unsigned int) UINT);
INTS[1] = uint2bytes((unsigned int) (UINT >> 32));
std::vector<unsigned char> BYTES;
BYTES.insert(BYTES.end(), INTS[0].begin(), INTS[0].end());
BYTES.insert(BYTES.end(), INTS[1].begin(), INTS[1].end());
return BYTES;
}
/** Convert a byte Vector into unsigned integer 64 bit. **/
inline uint64 bytes2uint64(std::vector<unsigned char> BYTES) { return (bytes2uint(BYTES) | ((uint64)bytes2uint(BYTES, 4) << 32)); }
/** Convert Standard String into Byte Vector. **/
inline std::vector<unsigned char> string2bytes(std::string STRING)
{
std::vector<unsigned char> BYTES(STRING.begin(), STRING.end());
return BYTES;
}
/** Convert Byte Vector into Standard String. **/
inline std::string bytes2string(std::vector<unsigned char> BYTES)
{
std::string STRING(BYTES.begin(), BYTES.end());
return STRING;
}
inline std::vector<unsigned char> double2bytes(double DOUBLE)
{
union {
double DOUBLE;
uint64 UINT64;
} u;
u.DOUBLE = DOUBLE;
return uint2bytes64(u.UINT64);
}
inline double bytes2double(std::vector<unsigned char> BYTES)
{
uint64 n64 = bytes2uint64(BYTES);
union {
double DOUBLE;
uint64 UINT64;
} u;
u.UINT64 = n64;
return u.DOUBLE;
}
inline int64 GetTimeMicros()
{
return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))).total_microseconds();
}
void SetCurrentThreadPriority(int priority = INT32_MAX);
void SetThreadPriority(pthread_t threadID, int priority);
#endif