forked from krypdkat/qubicbob
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm256i.h
More file actions
329 lines (282 loc) · 9.39 KB
/
m256i.h
File metadata and controls
329 lines (282 loc) · 9.39 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#pragma once
#include <string>
#include <immintrin.h>
#include <stdint.h>
#include "K12AndKeyUtil.h"
// Used for all kinds of IDs, including in QPI and contracts.
// Existing interface and behavior should never be changed! (However, it may be extended.)
union m256i
{
// access for loops and compatibility with __m256i
int8_t m256i_i8[32];
int16_t m256i_i16[16];
int32_t m256i_i32[8];
int64_t m256i_i64[4];
uint8_t m256i_u8[32];
uint16_t m256i_u16[16];
uint32_t m256i_u32[8];
uint64_t m256i_u64[4];
// interface for QPI (no [] allowed)
struct
{
uint64_t _0, _1, _2, _3;
} u64;
struct
{
int64_t _0, _1, _2, _3;
} i64;
struct
{
uint32_t _0, _1, _2, _3, _4, _5, _6, _7;
} u32;
struct
{
int32_t _0, _1, _2, _3, _4, _5, _6, _7;
} i32;
struct
{
uint16_t _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
} u16;
struct
{
int16_t _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
} i16;
struct
{
uint8_t _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
uint8_t _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31;
} u8;
struct
{
int8_t _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15;
int8_t _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31;
} i8;
m256i() = default;
m256i(unsigned long long ull0, unsigned long long ull1, unsigned long long ull2, unsigned long long ull3)
{
_mm256_storeu_si256(reinterpret_cast<__m256i*>(this),
_mm256_set_epi64x(ull3, ull2, ull1, ull0));
}
m256i(const unsigned char value[32])
{
_mm256_storeu_si256(reinterpret_cast<__m256i*>(this),
_mm256_loadu_si256(reinterpret_cast<const __m256i*>(value)));
}
m256i(const __m256i& value)
{
_mm256_storeu_si256(reinterpret_cast<__m256i*>(this), value);
}
m256i(const m256i& value)
{
assign(value);
}
m256i(const volatile m256i& value)
{
assign(value);
}
m256i(m256i&& other) noexcept
{
assign(other);
}
m256i& operator=(const m256i& other)
{
assign(other);
return *this;
}
volatile m256i& operator=(const m256i& other) volatile
{
assign(other);
return *this;
}
m256i& operator=(const volatile m256i& other)
{
assign(other);
return *this;
}
volatile m256i& operator=(const volatile m256i& other) volatile
{
assign(other);
return *this;
}
m256i& operator=(m256i&& other) noexcept
{
assign(other);
return *this;
}
void assign(const m256i& value) noexcept
{
// supports self-assignment
_mm256_storeu_si256(reinterpret_cast<__m256i*>(this),
_mm256_lddqu_si256(reinterpret_cast<const __m256i*>(&value)));
}
void assign(const m256i& value) volatile noexcept
{
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(const_cast<m256i*>(this)), // Cast away volatile from 'this'
_mm256_lddqu_si256(reinterpret_cast<const __m256i*>(&value))
);
}
void assign(const volatile m256i& value) noexcept
{
// supports self-assignment
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(this),
_mm256_lddqu_si256(reinterpret_cast<const __m256i*>(const_cast<const m256i*>(&value))) // Cast away volatile from 'value'
);
}
void assign(volatile const m256i& value) volatile noexcept
{
// supports self-assignment
_mm256_storeu_si256(
reinterpret_cast<__m256i*>(const_cast<m256i*>(this)), // Cast away volatile from 'this'
_mm256_lddqu_si256(reinterpret_cast<const __m256i*>(const_cast<const m256i*>(&value))) // Cast away volatile from 'value'
);
}
// Safely retrieves the content of the m256i union as an __m256i value.
__m256i getIntrinsicValue() const noexcept
{
return _mm256_loadu_si256(reinterpret_cast<const __m256i*>(this));
}
__m256i getIntrinsicValue() const volatile noexcept
{
return _mm256_loadu_si256(
reinterpret_cast<const __m256i*>( // Step 2: Reinterpret type
const_cast<const m256i*>(this) // Step 1: Cast away volatile
)
);
}
// Safely sets the content of the m256i union from an __m256i value.
void setIntrinsicValue(const __m256i& newValue) noexcept
{
_mm256_storeu_si256(reinterpret_cast<__m256i*>(this), newValue);
}
void setRandomValue()
{
_rdrand64_step(reinterpret_cast<unsigned long long*>(&m256i_u64[0]));
_rdrand64_step(reinterpret_cast<unsigned long long*>(&m256i_u64[1]));
_rdrand64_step(reinterpret_cast<unsigned long long*>(&m256i_u64[2]));
_rdrand64_step(reinterpret_cast<unsigned long long*>(&m256i_u64[3]));
}
static m256i randomValue()
{
m256i ret;
ret.setRandomValue();
return ret;
}
inline static m256i zero()
{
return _mm256_setzero_si256();
}
std::string toHex() const {
static const char hex[] = "0123456789abcdef";
std::string result;
result.reserve(64); // Pre-allocate space for efficiency
for (int i = 0; i < 32; ++i) {
uint8_t byte = m256i_u8[i];
result.push_back(hex[byte >> 4]); // High nibble
result.push_back(hex[byte & 0x0F]); // Low nibble
}
return result;
}
std::string toQubicHash() const {
char hash[64] = {0};
getIdentityFromPublicKey(m256i_u8, hash, true);
return std::string(hash);
};
std::string toQubicHashUpperCase() const {
char hash[64] = {0};
getIdentityFromPublicKey(m256i_u8, hash, false);
return std::string(hash);
};
};
static_assert(sizeof(m256i) == 32, "m256 has unexpected size!");
static inline const __m256i& __m256i_convert(const __m256i& a)
{
return a;
}
static inline __m256i __m256i_convert(const m256i& a)
{
return a.getIntrinsicValue();
}
static inline __m256i __m256i_convert(volatile const m256i& a)
{
return a.getIntrinsicValue();
}
static inline __m256i __m256i_convert(const unsigned char a[32])
{
return _mm256_loadu_si256(reinterpret_cast<const __m256i*>(a));
}
/*
static inline bool EQUAL(const __m256i& a, const __m256i& b)
{
return _mm256_movemask_epi8(_mm256_cmpeq_epi64(a, b)) == 0xFFFFFFFF;
}
*/
#if 0
// Enable this for more flexibility regarding comparisons of m256 variants, but these general == and != operators sometimes lead to very misleading error messages.
template <typename TA, typename TB>
static inline bool operator==(const TA& a, const TB& b)
{
return static_cast<uint64_t>_mm256_movemask_epi8(_mm256_cmpeq_epi64(__m256i_convert(a), __m256i_convert(b))) == 0xFFFFFFFF;
}
template <typename TA, typename TB>
static inline bool operator!=(const TA& a, const TB& b)
{
return static_cast<uint64_t>_mm256_movemask_epi8(_mm256_cmpeq_epi64(__m256i_convert(a), __m256i_convert(b))) != 0xFFFFFFFF;
}
#else
static inline bool operator==(const m256i& a, const m256i& b)
{
return _mm256_movemask_epi8(_mm256_cmpeq_epi64(__m256i_convert(a), __m256i_convert(b))) == (int)0xFFFFFFFFU;
}
static inline bool operator!=(const m256i& a, const m256i& b)
{
return _mm256_movemask_epi8(_mm256_cmpeq_epi64(__m256i_convert(a), __m256i_convert(b))) != (int)0xFFFFFFFFU;
}
static inline bool operator==(const m256i& a, volatile const m256i& b)
{
return _mm256_movemask_epi8(_mm256_cmpeq_epi64(__m256i_convert(a), __m256i_convert(b))) == (int)0xFFFFFFFFU;
}
static inline bool operator!=(const m256i& a, volatile const m256i& b)
{
return _mm256_movemask_epi8(_mm256_cmpeq_epi64(__m256i_convert(a), __m256i_convert(b))) != (int)0xFFFFFFFFU;
}
static inline bool operator==(volatile const m256i& a, const m256i& b)
{
return _mm256_movemask_epi8(_mm256_cmpeq_epi64(__m256i_convert(a), __m256i_convert(b))) == (int)0xFFFFFFFFU;
}
static inline bool operator!=(volatile const m256i& a, const m256i& b)
{
return _mm256_movemask_epi8(_mm256_cmpeq_epi64(__m256i_convert(a), __m256i_convert(b))) != (int)0xFFFFFFFFU;
}
static inline bool operator==(volatile const m256i& a, volatile const m256i& b)
{
return _mm256_movemask_epi8(_mm256_cmpeq_epi64(__m256i_convert(a), __m256i_convert(b))) == (int)0xFFFFFFFFU;
}
static inline bool operator!=(volatile const m256i& a, volatile const m256i& b)
{
return _mm256_movemask_epi8(_mm256_cmpeq_epi64(__m256i_convert(a), __m256i_convert(b))) != (int)0xFFFFFFFFU;
}
#endif
static inline bool operator<(const m256i& a, const m256i& b)
{
// probably this can be done more efficiently, but it is only used in the testing code for now
for (int i = 0; i < 4; ++i)
{
if (a.m256i_u64[i] < b.m256i_u64[i])
return true;
if (a.m256i_u64[i] > b.m256i_u64[i])
return false;
}
return false;
}
static inline bool isZero(const __m256i& a)
{
return _mm256_testz_si256(a, a) == 1;
}
template <typename T>
static inline bool isZero(const T& a)
{
const __m256i& ac = __m256i_convert(a);
return _mm256_testz_si256(ac, ac) == 1;
}