-
Notifications
You must be signed in to change notification settings - Fork 2
/
polar_helper.hh
230 lines (223 loc) · 4.09 KB
/
polar_helper.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
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
/*
SIMD wrapper used by polar encoder and decoder
Copyright 2020 Ahmet Inan <xdsopl@gmail.com>
*/
#pragma once
template <typename TYPE>
struct PolarHelper
{
static TYPE one()
{
return 1;
}
static TYPE zero()
{
return 0;
}
static TYPE signum(TYPE v)
{
return (v > 0) - (v < 0);
}
static TYPE decide(TYPE v)
{
return (v >= 0) - (v < 0);
}
template <typename IN>
static TYPE quant(IN in)
{
return in;
}
static TYPE qabs(TYPE a)
{
return std::abs(a);
}
static TYPE qmin(TYPE a, TYPE b)
{
return std::min(a, b);
}
static TYPE qadd(TYPE a, TYPE b)
{
return a + b;
}
static TYPE qmul(TYPE a, TYPE b)
{
return a * b;
}
static TYPE prod(TYPE a, TYPE b)
{
return signum(a) * signum(b) * qmin(qabs(a), qabs(b));
}
static TYPE madd(TYPE a, TYPE b, TYPE c)
{
return a * b + c;
}
static TYPE flip(TYPE a, TYPE b, TYPE c, TYPE d)
{
return c == d ? qmul(a, b) : a;
}
};
template <typename VALUE, int WIDTH>
struct PolarHelper<SIMD<VALUE, WIDTH>>
{
typedef SIMD<VALUE, WIDTH> TYPE;
static TYPE one()
{
return vdup<TYPE>(1);
}
static TYPE zero()
{
return vzero<TYPE>();
}
static TYPE signum(TYPE a)
{
return vsignum(a);
}
static TYPE decide(TYPE a)
{
return vcopysign(one(), a);
}
static TYPE qabs(TYPE a)
{
return vabs(a);
}
static TYPE qmin(TYPE a, TYPE b)
{
return vmin(a, b);
}
static TYPE qadd(TYPE a, TYPE b)
{
return vadd(a, b);
}
static TYPE qmul(TYPE a, TYPE b)
{
return vmul(a, b);
}
static TYPE prod(TYPE a, TYPE b)
{
return vmul(vmul(vsignum(a), vsignum(b)), vmin(vabs(a), vabs(b)));
}
static TYPE madd(TYPE a, TYPE b, TYPE c)
{
return vadd(vmul(a, b), c);
}
static TYPE flip(TYPE a, TYPE b, TYPE c, TYPE d)
{
return vreinterpret<TYPE>(vbsl(vceq(c, d), vmask(qmul(a, b)), vmask(a)));
}
};
template <int WIDTH>
struct PolarHelper<SIMD<int8_t, WIDTH>>
{
typedef SIMD<int8_t, WIDTH> TYPE;
static TYPE one()
{
return vdup<TYPE>(1);
}
static TYPE zero()
{
return vzero<TYPE>();
}
static TYPE signum(TYPE a)
{
return vsignum(a);
}
static TYPE decide(TYPE a)
{
return vreinterpret<TYPE>(vorr(vmask(one()), vcltz(a)));
}
static TYPE qabs(TYPE a)
{
return vqabs(a);
}
static TYPE qmin(TYPE a, TYPE b)
{
return vmin(a, b);
}
static TYPE qadd(TYPE a, TYPE b)
{
return vqadd(a, b);
}
static TYPE qmul(TYPE a, TYPE b)
{
#ifdef __ARM_NEON__
return vmul(a, b);
#else
return vsign(a, b);
#endif
}
static TYPE prod(TYPE a, TYPE b)
{
#ifdef __ARM_NEON__
return vmul(vmul(vsignum(a), vsignum(b)), vmin(vqabs(a), vqabs(b)));
#else
return vsign(vmin(vqabs(a), vqabs(b)), vsign(vsignum(a), b));
#endif
}
static TYPE madd(TYPE a, TYPE b, TYPE c)
{
#ifdef __ARM_NEON__
return vqadd(vmul(a, vmax(b, vdup<TYPE>(-127))), c);
#else
return vqadd(vsign(vmax(b, vdup<TYPE>(-127)), a), c);
#endif
}
static TYPE flip(TYPE a, TYPE b, TYPE c, TYPE d)
{
return vreinterpret<TYPE>(vbsl(vceq(c, d), vmask(qmul(a, b)), vmask(a)));
}
};
template <>
struct PolarHelper<int8_t>
{
static int8_t one()
{
return 1;
}
static int8_t zero()
{
return 0;
}
static int8_t signum(int8_t v)
{
return (v > 0) - (v < 0);
}
static int8_t decide(int8_t v)
{
return (v >= 0) - (v < 0);
}
template <typename IN>
static int8_t quant(IN in)
{
return std::min<IN>(std::max<IN>(std::nearbyint(in), -128), 127);
}
static int8_t qabs(int8_t a)
{
return std::abs(std::max<int8_t>(a, -127));
}
static int8_t qmin(int8_t a, int8_t b)
{
return std::min(a, b);
}
static int8_t qadd(int8_t a, int8_t b)
{
return std::min<int16_t>(std::max<int16_t>(int16_t(a) + int16_t(b), -128), 127);
}
static int8_t qmul(int8_t a, int8_t b)
{
// return std::min<int16_t>(std::max<int16_t>(int16_t(a) * int16_t(b), -128), 127);
// only used for hard decision values anyway
return a * b;
}
static int8_t prod(int8_t a, int8_t b)
{
return signum(a) * signum(b) * qmin(qabs(a), qabs(b));
}
static int8_t madd(int8_t a, int8_t b, int8_t c)
{
return std::min<int16_t>(std::max<int16_t>(int16_t(a) * int16_t(b) + int16_t(c), -128), 127);
}
static int8_t flip(int8_t a, int8_t b, int8_t c, int8_t d)
{
return c == d ? qmul(a, b) : a;
}
};