-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathm128_compat.h
241 lines (231 loc) · 6.06 KB
/
m128_compat.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
/*
* A thin compatibility layer to SSE's __m128i data format
* and associated instructions to support GHASH & the full algo.
*/
#ifndef __M128_COMPAT_H__
#define __M128_COMPAT_H__
#include "new_instructions_support_b.h"
#include <stdio.h>
/* ouch */
typedef struct {
uint64_t l;
uint64_t h;
} __m128i;
//#define _mm_loadu_si128(a) (*(const __m128i*)a)
static inline __m128i _mm_loadu_si128(const __m128i *ptr) {
__m128i r;
r.l = ((const uint64_t*)ptr)[0];
r.h = ((const uint64_t*)ptr)[1];
return r;
}
//#define _mm_storeu_si128(x,a) (*(__m128i*)x)=a
static inline void _mm_storeu_si128(__m128i *ptr, const __m128i data) {
((uint64_t*)ptr)[0] = data.l;
((uint64_t*)ptr)[1] = data.h;
}
static inline void _mm_store_si128(__m128i *ptr, const __m128i data) {
((uint64_t*)ptr)[0] = data.l;
((uint64_t*)ptr)[1] = data.h;
}
static inline void _mm_storel_epi64 (__m128i *ptr, const __m128i data) {
((uint64_t*)ptr)[0] = data.l;
}
static inline __m128i _mm_clmulepi64_si128(const __m128i a, const __m128i b, const int x) {
__m128i r;
switch (x) {
case 0x00:
r.l = _rv64_clmul(a.l, b.l);
r.h = _rv64_clmulh(a.l, b.l);
break;
case 0x01:
r.l = _rv64_clmul(a.l, b.h);
r.h = _rv64_clmulh(a.l, b.h);
break;
case 0x10:
r.l = _rv64_clmul(a.h, b.l);
r.h = _rv64_clmulh(a.h, b.l);
break;
case 0x11:
r.l = _rv64_clmul(a.h, b.h);
r.h = _rv64_clmulh(a.h, b.h);
break;
}
return r;
}
/*
static inline __m128i (const __m128i a, const __m128i b) {
__m128i r;
return r;
}
*/
static inline __m128i _mm_xor_si128(const __m128i a, const __m128i b) {
__m128i r;
r.l = a.l ^ b.l;
r.h = a.h ^ b.h;
return r;
}
static inline __m128i _mm_or_si128(const __m128i a, const __m128i b) {
__m128i r;
r.l = a.l | b.l;
r.h = a.h | b.h;
return r;
}
static inline __m128i _mm_and_si128(const __m128i a, const __m128i b) {
__m128i r;
r.l = a.l & b.l;
r.h = a.h & b.h;
return r;
}
static inline __m128i _mm_slli_si128(const __m128i a, const int b) {
__m128i r;
switch (b) {
case 4:
r.l = a.l << 32;
r.h = a.h << 32 | a.l >> 32;
break;
case 8:
r.l = 0;
r.h = a.l;
break;
case 12:
r.l = 0;
r.h = a.l << 32;
break;
default:
fprintf(stderr, "%s: %d unimplemented\n", __PRETTY_FUNCTION__, b);
break;
}
return r;
}
static inline __m128i _mm_srli_si128(const __m128i a, const int b) {
__m128i r;
switch (b) {
case 1:
r.l = a.l >> 8 | a.h << 56;
r.h = a.h >> 8;
break;
case 4:
r.l = a.l >> 32 | a.h << 32;
r.h = a.h >> 32;
break;
case 8:
r.l = a.h;
r.h = 0;
break;
case 12:
r.l = a.h >> 32;
r.h = 0;
break;
default:
fprintf(stderr, "%s: %d unimplemented\n", __PRETTY_FUNCTION__, b);
break;
}
return r;
}
static inline __m128i _mm_srli_epi32(const __m128i a, const int b) {
__m128i r;
r.l = ((a.l & 0x00000000FFFFFFFFull) >> b) | (((a.l & 0xFFFFFFFF00000000ull) >> b) & 0xFFFFFFFF00000000ull);
r.h = ((a.h & 0x00000000FFFFFFFFull) >> b) | (((a.h & 0xFFFFFFFF00000000ull) >> b) & 0xFFFFFFFF00000000ull);
return r;
}
static inline __m128i _mm_slli_epi32(const __m128i a, const int b) {
__m128i r;
r.l = (((a.l & 0x00000000FFFFFFFFull) << b) & 0x00000000FFFFFFFFull) | ((a.l & 0xFFFFFFFF00000000ull) << b);
r.h = (((a.h & 0x00000000FFFFFFFFull) << b) & 0x00000000FFFFFFFFull) | ((a.h & 0xFFFFFFFF00000000ull) << b);
return r;
}
/* static inline __m128i _mm_srai_epi32(const __m128i a, const int b) { */
/* __m128i r; */
/* r.l = (((int32_t)(a.l & 0x00000000FFFFFFFFull)) >> b) | ((((int32_t)(a.l & 0xFFFFFFFF00000000ull)) >> b) & 0xFFFFFFFF00000000ull); */
/* r.h = (((int32_t)(a.h & 0x00000000FFFFFFFFull)) >> b) | ((((int32_t)(a.h & 0xFFFFFFFF00000000ull)) >> b) & 0xFFFFFFFF00000000ull); */
/* return r; */
/* } */
static inline __m128i _mm_insert_epi64(const __m128i a, const uint64_t x, const int b) {
__m128i r;
if (b == 0) {
r.l = x;
r.h = a.h;
} else {
r.l = a.l;
r.h = x;
}
return r;
}
static inline __m128i _mm_setzero_si128(void) {
__m128i r;
r.l = 0;
r.h = 0;
return r;
}
static inline __m128i _mm_set1_epi32(const uint32_t x) {
__m128i r;
r.l = x | ((uint64_t)x) << 32;
r.h = x | ((uint64_t)x) << 32;
return r;
}
static inline __m128i _mm_set_epi32 (uint32_t e3, uint32_t e2, uint32_t e1, uint32_t e0) {
__m128i r;
r.l = (uint64_t)e0 | ((uint64_t)e1) << 32;
r.h = (uint64_t)e2 | ((uint64_t)e3) << 32;
return r;
}
/* non-intel stuff, used to replace some common use cases */
static inline uint64_t bytereverse64(const uint64_t a) {
uint64_t r;
r = (uint32_t)_rv32_grev((a>>32), 24) | (((uint64_t)_rv32_grev((a&0xFFFFFFFF), 24))<<32);
return r;
}
static inline __m128i bytereverse128(const __m128i a) {
__m128i r;
r.l = bytereverse64(a.h);
r.h = bytereverse64(a.l);
return r;
}
static inline uint64_t bitreverse64(const uint64_t a) {
uint64_t r;
r = (uint32_t)_rv32_grev((a&0xFFFFFFFF), 7) | (((uint64_t)_rv32_grev((a>>32), 7))<<32);
return r;
}
static inline __m128i bitreverse128(const __m128i a) {
__m128i r;
r.l = bitreverse64(a.l);
r.h = bitreverse64(a.h);
return r;
}
static inline uint64_t wordreverse64(const uint64_t a) {
uint64_t r;
r = (a>>32)|(a<<32);
return r;
}
static inline __m128i wordreverse128(const __m128i a) {
__m128i r;
r.l = wordreverse64(a.h);
r.h = wordreverse64(a.l);
return r;
}
static inline __m128i doublewordreverse128(const __m128i a) {
__m128i r;
r.l = a.h;
r.h = a.l;
return r;
}
static inline __m128i wordrotate1l128(const __m128i a) {
__m128i r;
/* i.e. epi32 _MM_SHUFFLE(2,1,0,3) */
r.l = (a.h >> 32) | (a.l << 32);
r.h = (a.l >> 32) | (a.h << 32);
return r;
}
static inline __m128i halfwordandzero(const uint16_t a) {
__m128i r;
r.l = a;
r.h = 0;
return r;
}
static inline __m128i wordsign128(const __m128i a) {
__m128i r;
r.l = (a.l & 0x0000000080000000ull ? 0x00000000FFFFFFFFull : 0) | (a.l & 0x8000000000000000ull ? 0xFFFFFFFF00000000ull : 0);
r.h = (a.h & 0x0000000080000000ull ? 0x00000000FFFFFFFFull : 0) | (a.h & 0x8000000000000000ull ? 0xFFFFFFFF00000000ull : 0);
return r;
}
#endif // __M128_COMPAT_H__