-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrotate.hh
143 lines (134 loc) · 2.8 KB
/
rotate.hh
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
/*
SIMD element wise horizontal rotation
Copyright 2019 Ahmet Inan <[email protected]>
*/
#pragma once
#include "simd.hh"
namespace CODE {
template <typename TYPE, int WIDTH>
class Rotate
{
static const int SIZE = TYPE::SIZE;
static_assert(WIDTH <= SIZE, "width must be smaller or equal to SIMD size");
public:
TYPE operator()(TYPE a, int s)
{
if (s < 0)
s += WIDTH;
int t = WIDTH - s;
TYPE ret;
for (int n = 0; n < s; ++n)
ret.v[n] = a.v[n+t];
for (int n = 0; n < t; ++n)
ret.v[n+s] = a.v[n];
return ret;
}
};
#ifdef __ARM_NEON
template <int WIDTH>
class Rotate<SIMD<int8_t, 16>, WIDTH>
{
static const int SIZE = 16;
static_assert(WIDTH <= SIZE, "width must be smaller or equal to SIMD size");
typedef SIMD<int8_t, SIZE> TYPE;
TYPE rot[WIDTH];
public:
Rotate()
{
for (int i = 0; i < WIDTH; ++i) {
rot[i] = vdup<TYPE>(0x80);
for (int j = 0; j < WIDTH; ++j)
rot[i].v[j] = (j - i + WIDTH) % WIDTH;
}
}
TYPE operator()(TYPE a, int s)
{
if (s < 0)
s += WIDTH;
TYPE ret;
#ifdef __aarch64__
ret.m = vqtbl1q_s8(a.m, vunsigned(rot[s]).m);
#else
int8x8x2_t b { vget_low_s8(a.m), vget_high_s8(a.m) };
int8x8_t c = vtbl2_s8(b, vget_low_s8(rot[s].m));
int8x8_t d = vtbl2_s8(b, vget_high_s8(rot[s].m));
ret.m = vcombine_s8(c, d);
#endif
return ret;
}
};
#endif
#ifdef __AVX2__
template <int WIDTH>
class Rotate<SIMD<int8_t, 32>, WIDTH>
{
static const int SIZE = 32;
static_assert(WIDTH <= SIZE, "width must be smaller or equal to SIMD size");
typedef SIMD<int8_t, SIZE> TYPE;
TYPE rot0[WIDTH], rot1[WIDTH];
public:
Rotate()
{
for (int i = 0; i < WIDTH; ++i) {
rot0[i] = vdup<TYPE>(0x80);
rot1[i] = vdup<TYPE>(0x80);
for (int j = 0; j < WIDTH; ++j) {
int pos = (j - i + WIDTH) % WIDTH;
if (j < 16) {
if (pos < 16) {
rot0[i].v[j] = pos;
} else {
rot1[i].v[j+16] = pos-16;
}
} else {
if (pos < 16) {
rot1[i].v[j-16] = pos;
} else {
rot0[i].v[j] = pos-16;
}
}
}
}
}
TYPE operator()(TYPE a, int s)
{
if (s < 0)
s += WIDTH;
__m256i b = _mm256_shuffle_epi8(a.m, rot0[s].m);
__m256i c = _mm256_shuffle_epi8(a.m, rot1[s].m);
__m256i d = _mm256_permute2x128_si256(c, c, 1);
TYPE ret;
ret.m = _mm256_or_si256(b, d);
return ret;
}
};
#else
#ifdef __SSE4_1__
template <int WIDTH>
class Rotate<SIMD<int8_t, 16>, WIDTH>
{
static const int SIZE = 16;
static_assert(WIDTH <= SIZE, "width must be smaller or equal to SIMD size");
typedef SIMD<int8_t, SIZE> TYPE;
TYPE rot[WIDTH];
public:
Rotate()
{
for (int i = 0; i < WIDTH; ++i) {
rot[i] = vdup<TYPE>(0x80);
for (int j = 0; j < WIDTH; ++j)
rot[i].v[j] = (j - i + WIDTH) % WIDTH;
}
}
TYPE operator()(TYPE a, int s)
{
if (s < 0)
s += WIDTH;
TYPE ret;
ret.m = _mm_shuffle_epi8(a.m, rot[s].m);
return ret;
}
};
#endif
#endif
}