This repository has been archived by the owner on Apr 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSSE.h
276 lines (230 loc) · 8.17 KB
/
SSE.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
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
/* ******************************************** *\
*
*
*
\* ******************************************** */
/* Including headers */
#pragma once
#include <stdlib.h>
#include <x86intrin.h>
#include <stdint.h>
#ifndef SSE
#define SSE
#endif
#ifndef BITS_PER_REG
#define BITS_PER_REG 128
#endif
/* Defining 0 and 1 */
#define ZERO _mm_setzero_si128()
#define ONES _mm_set1_epi32(-1)
/* Defining macros */
#define REG_SIZE 128
#define CHUNK_SIZE 256
#define AND(a,b) _mm_and_si128(a,b)
#define OR(a,b) _mm_or_si128(a,b)
#define XOR(a,b) _mm_xor_si128(a,b)
#define ANDN(a,b) _mm_andnot_si128(a,b)
#define NOT(a) _mm_xor_si128(ONES,a)
#define ADD(a,b,c) _mm_add_epi##c(a,b)
#define L_SHIFT(a,b,c) _mm_slli_epi##c(a,b)
#define R_SHIFT(a,b,c) _mm_srli_epi##c(a,b)
#define L_ROTATE(a,b,c) \
b == 8 && c == 32 ? \
_mm_shuffle_epi8(a,_mm_set_epi8(14,13,12,15,10,9,8,11,6,5,4,7,2,1,0,3)) : \
b == 16 && c == 32 ? \
_mm_shuffle_epi8(a,_mm_set_epi8(13,12,15,14,9,8,11,10,5,4,7,6,1,0,3,2)) : \
OR(L_SHIFT(a,b,c),R_SHIFT(a,c-b,c))
#define R_ROTATE(a,b,c) \
b == 8 && c == 32 ? \
_mm_shuffle_epi8(a,_mm_set_epi8(12,15,14,13,8,11,10,9,4,7,6,5,0,3,2,1)) : \
b == 16 && c == 32 ? \
_mm_shuffle_epi8(a,_mm_set_epi8(13,12,15,14,9,8,11,10,5,4,7,6,1,0,3,2)) : \
OR(R_SHIFT(a,b,c),L_SHIFT(a,c-b,c))
#define DATATYPE __m128i
#define SET_ALL_ONE() ONES
#define SET_ALL_ZERO() ZERO
/* Note the reverse of the pattern. */
#define PERMUT_16(a,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16) \
_mm_shuffle_epi8(a,_mm_set_epi8(x16,x15,x14,x13,x12,x11,x10,x9,x8,x7,x6,x5,x4,x3,x2,x1))
#define PERMUT_4(a,x1,x2,x3,x4) _mm_shuffle_epi32(a,(x4<<6)|(x3<<4)|(x2<<2)|x1)
#define LIFT_8(x) _mm_set1_epi8(x)
#define LIFT_16(x) _mm_set1_epi16(x)
#define LIFT_32(x) _mm_set1_epi32(x)
#define LIFT_64(x) _mm_set1_epi64x(x)
#define ORTHOGONALIZE(in,out) orthogonalize(in,out)
#define UNORTHOGONALIZE(in,out) unorthogonalize(in,out)
#define ALLOC(size) aligned_alloc(32,size * sizeof(__m128i))
#ifndef NO_RUNTIME
/* Orthogonalization stuffs */
static uint64_t mask_l[6] = {
0xaaaaaaaaaaaaaaaaUL,
0xccccccccccccccccUL,
0xf0f0f0f0f0f0f0f0UL,
0xff00ff00ff00ff00UL,
0xffff0000ffff0000UL,
0xffffffff00000000UL
};
static uint64_t mask_r[6] = {
0x5555555555555555UL,
0x3333333333333333UL,
0x0f0f0f0f0f0f0f0fUL,
0x00ff00ff00ff00ffUL,
0x0000ffff0000ffffUL,
0x00000000ffffffffUL
};
void real_ortho(uint64_t data[]) {
for (int i = 0; i < 6; i ++) {
int n = (1UL << i);
for (int j = 0; j < 64; j += (2 * n))
for (int k = 0; k < n; k ++) {
uint64_t u = data[j + k] & mask_l[i];
uint64_t v = data[j + k] & mask_r[i];
uint64_t x = data[j + n + k] & mask_l[i];
uint64_t y = data[j + n + k] & mask_r[i];
data[j + k] = u | (x >> n);
data[j + n + k] = (v << n) | y;
}
}
}
void real_ortho_128x128(__m128i data[]) {
__m128i mask_l[7] = {
_mm_set1_epi64x(0xaaaaaaaaaaaaaaaaUL),
_mm_set1_epi64x(0xccccccccccccccccUL),
_mm_set1_epi64x(0xf0f0f0f0f0f0f0f0UL),
_mm_set1_epi64x(0xff00ff00ff00ff00UL),
_mm_set1_epi64x(0xffff0000ffff0000UL),
_mm_set1_epi64x(0xffffffff00000000UL),
_mm_set_epi64x(0x0000000000000000UL,0xffffffffffffffffUL),
};
__m128i mask_r[7] = {
_mm_set1_epi64x(0x5555555555555555UL),
_mm_set1_epi64x(0x3333333333333333UL),
_mm_set1_epi64x(0x0f0f0f0f0f0f0f0fUL),
_mm_set1_epi64x(0x00ff00ff00ff00ffUL),
_mm_set1_epi64x(0x0000ffff0000ffffUL),
_mm_set1_epi64x(0x00000000ffffffffUL),
_mm_set_epi64x(0xffffffffffffffffUL,0x0000000000000000UL),
};
for (int i = 0; i < 7; i ++) {
int n = (1UL << i);
for (int j = 0; j < 128; j += (2 * n))
for (int k = 0; k < n; k ++) {
__m128i u = _mm_and_si128(data[j + k], mask_l[i]);
__m128i v = _mm_and_si128(data[j + k], mask_r[i]);
__m128i x = _mm_and_si128(data[j + n + k], mask_l[i]);
__m128i y = _mm_and_si128(data[j + n + k], mask_r[i]);
if (i <= 5) {
data[j + k] = _mm_or_si128(u, _mm_srli_epi64(x, n));
data[j + n + k] = _mm_or_si128(_mm_slli_epi64(v, n), y);
} else {
/* Note the "inversion" of srli and slli. */
data[j + k] = _mm_or_si128(u, _mm_slli_si128(x, 8));
data[j + n + k] = _mm_or_si128(_mm_srli_si128(v, 8), y);
}
}
}
}
void real_ortho_128x128_blend(__m128i data[]) {
__m128i mask_l[7] = {
_mm_set1_epi64x(0xaaaaaaaaaaaaaaaaUL),
_mm_set1_epi64x(0xccccccccccccccccUL),
_mm_set1_epi64x(0xf0f0f0f0f0f0f0f0UL),
_mm_set1_epi64x(0xff00ff00ff00ff00UL),
_mm_set1_epi64x(0xffff0000ffff0000UL),
_mm_set1_epi64x(0xffffffff00000000UL),
_mm_set_epi64x(0UL,-1UL),
};
__m128i mask_r[7] = {
_mm_set1_epi64x(0x5555555555555555UL),
_mm_set1_epi64x(0x3333333333333333UL),
_mm_set1_epi64x(0x0f0f0f0f0f0f0f0fUL),
_mm_set1_epi64x(0x00ff00ff00ff00ffUL),
_mm_set1_epi64x(0x0000ffff0000ffffUL),
_mm_set1_epi64x(0x00000000ffffffffUL),
_mm_set_epi64x(-1UL,0UL),
};
for (int i = 0; i < 7; i ++) {
int n = (1UL << i);
for (int j = 0; j < 128; j += (2 * n))
for (int k = 0; k < n; k ++) {
if (i <= 3) {
__m128i u = _mm_and_si128(data[j + k], mask_l[i]);
__m128i v = _mm_and_si128(data[j + k], mask_r[i]);
__m128i x = _mm_and_si128(data[j + n + k], mask_l[i]);
__m128i y = _mm_and_si128(data[j + n + k], mask_r[i]);
data[j + k] = _mm_or_si128(u, _mm_srli_epi64(x, n));
data[j + n + k] = _mm_or_si128(_mm_slli_epi64(v, n), y);
} else if (i == 4) {
__m128i u = data[j + k];
__m128i v = data[j + k];
__m128i x = data[j + n + k];
__m128i y = data[j + n + k];
data[j + k] = _mm_blend_epi16(u,_mm_srli_epi64(x, n), 0b01010101);
data[j + n + k] = _mm_blend_epi16(_mm_slli_epi64(v, n), y, 0b01010101);
} else if (i == 5) {
__m128i u = data[j + k];
__m128i v = data[j + k];
__m128i x = data[j + n + k];
__m128i y = data[j + n + k];
data[j + k] = _mm_blend_epi16(u,_mm_srli_epi64(x, n), 0b00110011);
data[j + n + k] = _mm_blend_epi16(_mm_slli_epi64(v, n), y, 0b00110011);
} else {
__m128i u = data[j + k];
__m128i v = data[j + k];
__m128i x = data[j + n + k];
__m128i y = data[j + n + k];
/* Note the "inversion" of srli and slli. */
data[j + k] = _mm_blend_epi16(u,_mm_slli_si128(x,8), 0b11110000);
data[j + n + k] = _mm_blend_epi16(_mm_srli_si128(v, 8), y, 0b11110000);
}
}
}
}
#ifdef ORTHO
void orthogonalize_128x64(uint64_t* data, __m128i* out) {
real_ortho(data);
real_ortho(&(data[64]));
for (int i = 0; i < 64; i++)
out[i] = _mm_set_epi64x(data[i], data[64+i]);
}
void unorthogonalize_64x128(__m128i *in, uint64_t* data) {
for (int i = 0; i < 64; i++) {
uint64_t tmp[2];
_mm_store_si128 ((__m128i*)tmp, in[i]);
data[i] = tmp[1];
data[64+i] = tmp[0];
}
real_ortho(data);
real_ortho(&(data[64]));
}
void orthogonalize_128x128(uint64_t* data, __m128i* out) {
for (int i = 0; i < 128; i++)
out[i] = _mm_set_epi64x(data[i], data[128+i]);
real_ortho_128x128(out);
}
void unorthogonalize_128x128(__m128i *in, uint64_t* data) {
real_ortho_128x128(in);
for (int i = 0; i < 128; i++) {
uint64_t tmp[2];
_mm_store_si128 ((__m128i*)tmp, in[i]);
data[i] = tmp[1];
data[128+i] = tmp[0];
}
}
void orthogonalize(uint64_t* data, __m128i* out) {
orthogonalize_128x128(data,out);
}
void unorthogonalize(__m128i *in, uint64_t* data) {
unorthogonalize_128x128(in,data);
}
#else
void orthogonalize(uint64_t *in, __m128i *out) {
for (int i = 0; i < 128; i++)
out[i] = _mm_set_epi64x (in[i*2], in[i*2+1]);
}
void unorthogonalize(__m128i *in, uint64_t *out) {
for (int i = 0; i < 128; i++)
_mm_store_si128 ((__m128i*)&(out[i*2]), in[i]);
}
#endif /* ORTHO */
#endif /* NO_RUNTIME */