blob: f74481e8d357a8c609b065eb665aed2c8daccd11 [file] [log] [blame]
Mike Kleinec370972020-03-05 10:15:35 -06001// Copyright 2020 Google LLC.
Mike Kleina67d1ae2020-03-09 17:36:00 -05002// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Mike Kleinec370972020-03-05 10:15:35 -06003
4#ifndef SkVM_opts_DEFINED
5#define SkVM_opts_DEFINED
6
7#include "include/private/SkVx.h"
8#include "src/core/SkVM.h"
9
Mike Klein6d94b652020-09-16 11:37:03 -050010// Ideally this is (x*y + 0x2000)>>14,
11// but to let use vpmulhrsw we'll approximate that as ((x*y + 0x4000)>>15)<<1.
12template <int N>
13static inline skvx::Vec<N,int16_t> mul_q14(const skvx::Vec<N,int16_t>& x,
14 const skvx::Vec<N,int16_t>& y) {
15#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2
16 if constexpr (N == 16) {
17 return skvx::bit_pun<skvx::Vec<N,int16_t>>(_mm256_mulhrs_epi16(skvx::bit_pun<__m256i>(x),
18 skvx::bit_pun<__m256i>(y)))
19 << 1;
20 }
21#endif
22#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
23 if constexpr (N == 8) {
24 return skvx::bit_pun<skvx::Vec<N,int16_t>>(_mm_mulhrs_epi16(skvx::bit_pun<__m128i>(x),
25 skvx::bit_pun<__m128i>(y)))
26 << 1;
27 }
28#endif
29 // TODO: NEON specialization with vqrdmulh.s16?
30
31 // Try to recurse onto the specializations above.
32 if constexpr (N > 8) {
33 return join(mul_q14(x.lo, y.lo),
34 mul_q14(x.hi, y.hi));
35 }
36 return skvx::cast<int16_t>((skvx::cast<int>(x) *
37 skvx::cast<int>(y) + 0x4000)>>15 ) <<1;
38}
39
Mike Kleinec370972020-03-05 10:15:35 -060040namespace SK_OPTS_NS {
41
42 inline void interpret_skvm(const skvm::InterpreterInstruction insts[], const int ninsts,
43 const int nregs, const int loop,
44 const int strides[], const int nargs,
45 int n, void* args[]) {
46 using namespace skvm;
47
48 // We'll operate in SIMT style, knocking off K-size chunks from n while possible.
49 // We noticed quad-pumping is slower than single-pumping and both were slower than double.
Mike Klein51d35ed2020-04-24 08:16:22 -050050 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2
Mike Kleinec370972020-03-05 10:15:35 -060051 constexpr int K = 16;
52 #else
53 constexpr int K = 8;
54 #endif
55 using I32 = skvx::Vec<K, int>;
56 using F32 = skvx::Vec<K, float>;
Mike Klein6732da02020-07-16 13:03:18 -050057 using U64 = skvx::Vec<K, uint64_t>;
Mike Kleinec370972020-03-05 10:15:35 -060058 using U32 = skvx::Vec<K, uint32_t>;
59 using U16 = skvx::Vec<K, uint16_t>;
60 using U8 = skvx::Vec<K, uint8_t>;
61
Mike Klein9791e502020-09-15 12:43:38 -050062 using I16x2 = skvx::Vec<2*K, int16_t>;
63 using U16x2 = skvx::Vec<2*K, uint16_t>;
64
Mike Kleinec370972020-03-05 10:15:35 -060065 union Slot {
66 F32 f32;
67 I32 i32;
68 U32 u32;
Mike Klein9791e502020-09-15 12:43:38 -050069 I16x2 i16x2;
70 U16x2 u16x2;
Mike Kleinec370972020-03-05 10:15:35 -060071 };
72
73 Slot few_regs[16];
74 std::unique_ptr<char[]> many_regs;
75
Mike Klein4284f752020-07-10 15:16:17 -050076 Slot* r = few_regs;
Mike Kleinec370972020-03-05 10:15:35 -060077
78 if (nregs > (int)SK_ARRAY_COUNT(few_regs)) {
79 // Annoyingly we can't trust that malloc() or new will work with Slot because
80 // the skvx::Vec types may have alignment greater than what they provide.
81 // We'll overallocate one extra register so we can align manually.
82 many_regs.reset(new char[ sizeof(Slot) * (nregs + 1) ]);
83
84 uintptr_t addr = (uintptr_t)many_regs.get();
85 addr += alignof(Slot) -
86 (addr & (alignof(Slot) - 1));
87 SkASSERT((addr & (alignof(Slot) - 1)) == 0);
Mike Klein4284f752020-07-10 15:16:17 -050088 r = (Slot*)addr;
Mike Kleinec370972020-03-05 10:15:35 -060089 }
90
91
Mike Kleinec370972020-03-05 10:15:35 -060092 // Step each argument pointer ahead by its stride a number of times.
93 auto step_args = [&](int times) {
94 for (int i = 0; i < nargs; i++) {
95 args[i] = (void*)( (char*)args[i] + times * strides[i] );
96 }
97 };
98
99 int start = 0,
100 stride;
101 for ( ; n > 0; start = loop, n -= stride, step_args(stride)) {
102 stride = n >= K ? K : 1;
103
104 for (int i = start; i < ninsts; i++) {
105 InterpreterInstruction inst = insts[i];
106
107 // d = op(x,y/imm,z/imm)
108 Reg d = inst.d,
109 x = inst.x,
110 y = inst.y,
111 z = inst.z;
112 int immy = inst.immy,
113 immz = inst.immz;
114
115 // Ops that interact with memory need to know whether we're stride=1 or K,
116 // but all non-memory ops can run the same code no matter the stride.
117 switch (2*(int)inst.op + (stride == K ? 1 : 0)) {
118 default: SkUNREACHABLE;
119
120 #define STRIDE_1(op) case 2*(int)op
121 #define STRIDE_K(op) case 2*(int)op + 1
Mike Klein4284f752020-07-10 15:16:17 -0500122 STRIDE_1(Op::store8 ): memcpy(args[immy], &r[x].i32, 1); break;
123 STRIDE_1(Op::store16): memcpy(args[immy], &r[x].i32, 2); break;
124 STRIDE_1(Op::store32): memcpy(args[immy], &r[x].i32, 4); break;
Mike Klein6732da02020-07-16 13:03:18 -0500125 STRIDE_1(Op::store64): memcpy((char*)args[immz]+0, &r[x].i32, 4);
126 memcpy((char*)args[immz]+4, &r[y].i32, 4); break;
Mike Kleinec370972020-03-05 10:15:35 -0600127
Mike Klein4284f752020-07-10 15:16:17 -0500128 STRIDE_K(Op::store8 ): skvx::cast<uint8_t> (r[x].i32).store(args[immy]); break;
129 STRIDE_K(Op::store16): skvx::cast<uint16_t>(r[x].i32).store(args[immy]); break;
130 STRIDE_K(Op::store32): (r[x].i32).store(args[immy]); break;
Mike Klein6732da02020-07-16 13:03:18 -0500131 STRIDE_K(Op::store64): (skvx::cast<uint64_t>(r[x].u32) << 0 |
132 skvx::cast<uint64_t>(r[y].u32) << 32).store(args[immz]);
133 break;
Mike Kleinec370972020-03-05 10:15:35 -0600134
Mike Klein4284f752020-07-10 15:16:17 -0500135 STRIDE_1(Op::load8 ): r[d].i32 = 0; memcpy(&r[d].i32, args[immy], 1); break;
136 STRIDE_1(Op::load16): r[d].i32 = 0; memcpy(&r[d].i32, args[immy], 2); break;
137 STRIDE_1(Op::load32): r[d].i32 = 0; memcpy(&r[d].i32, args[immy], 4); break;
Mike Klein31367892020-07-30 08:19:12 -0500138 STRIDE_1(Op::load64):
139 r[d].i32 = 0; memcpy(&r[d].i32, (char*)args[immy] + 4*immz, 4); break;
Mike Kleinec370972020-03-05 10:15:35 -0600140
Mike Klein4284f752020-07-10 15:16:17 -0500141 STRIDE_K(Op::load8 ): r[d].i32= skvx::cast<int>(U8 ::Load(args[immy])); break;
142 STRIDE_K(Op::load16): r[d].i32= skvx::cast<int>(U16::Load(args[immy])); break;
143 STRIDE_K(Op::load32): r[d].i32= I32::Load(args[immy]) ; break;
Mike Klein31367892020-07-30 08:19:12 -0500144 STRIDE_K(Op::load64):
145 // Low 32 bits if immz=0, or high 32 bits if immz=1.
146 r[d].i32 = skvx::cast<int>(U64::Load(args[immy]) >> (32*immz)); break;
Mike Kleinec370972020-03-05 10:15:35 -0600147
148 // The pointer we base our gather on is loaded indirectly from a uniform:
Mike Klein4284f752020-07-10 15:16:17 -0500149 // - args[immy] is the uniform holding our gather base pointer somewhere;
150 // - (const uint8_t*)args[immy] + immz points to the gather base pointer;
Mike Kleinec370972020-03-05 10:15:35 -0600151 // - memcpy() loads the gather base and into a pointer of the right type.
152 // After all that we have an ordinary (uniform) pointer `ptr` to load from,
Mike Klein4284f752020-07-10 15:16:17 -0500153 // and we then gather from it using the varying indices in r[x].
Mike Kleinec370972020-03-05 10:15:35 -0600154 STRIDE_1(Op::gather8):
155 for (int i = 0; i < K; i++) {
156 const uint8_t* ptr;
Mike Klein4284f752020-07-10 15:16:17 -0500157 memcpy(&ptr, (const uint8_t*)args[immy] + immz, sizeof(ptr));
158 r[d].i32[i] = (i==0) ? ptr[ r[x].i32[i] ] : 0;
Mike Kleinec370972020-03-05 10:15:35 -0600159 } break;
160 STRIDE_1(Op::gather16):
161 for (int i = 0; i < K; i++) {
162 const uint16_t* ptr;
Mike Klein4284f752020-07-10 15:16:17 -0500163 memcpy(&ptr, (const uint8_t*)args[immy] + immz, sizeof(ptr));
164 r[d].i32[i] = (i==0) ? ptr[ r[x].i32[i] ] : 0;
Mike Kleinec370972020-03-05 10:15:35 -0600165 } break;
166 STRIDE_1(Op::gather32):
167 for (int i = 0; i < K; i++) {
168 const int* ptr;
Mike Klein4284f752020-07-10 15:16:17 -0500169 memcpy(&ptr, (const uint8_t*)args[immy] + immz, sizeof(ptr));
170 r[d].i32[i] = (i==0) ? ptr[ r[x].i32[i] ] : 0;
Mike Kleinec370972020-03-05 10:15:35 -0600171 } break;
172
173 STRIDE_K(Op::gather8):
174 for (int i = 0; i < K; i++) {
175 const uint8_t* ptr;
Mike Klein4284f752020-07-10 15:16:17 -0500176 memcpy(&ptr, (const uint8_t*)args[immy] + immz, sizeof(ptr));
177 r[d].i32[i] = ptr[ r[x].i32[i] ];
Mike Kleinec370972020-03-05 10:15:35 -0600178 } break;
179 STRIDE_K(Op::gather16):
180 for (int i = 0; i < K; i++) {
181 const uint16_t* ptr;
Mike Klein4284f752020-07-10 15:16:17 -0500182 memcpy(&ptr, (const uint8_t*)args[immy] + immz, sizeof(ptr));
183 r[d].i32[i] = ptr[ r[x].i32[i] ];
Mike Kleinec370972020-03-05 10:15:35 -0600184 } break;
185 STRIDE_K(Op::gather32):
186 for (int i = 0; i < K; i++) {
187 const int* ptr;
Mike Klein4284f752020-07-10 15:16:17 -0500188 memcpy(&ptr, (const uint8_t*)args[immy] + immz, sizeof(ptr));
189 r[d].i32[i] = ptr[ r[x].i32[i] ];
Mike Kleinec370972020-03-05 10:15:35 -0600190 } break;
191
192 #undef STRIDE_1
193 #undef STRIDE_K
194
195 // Ops that don't interact with memory should never care about the stride.
196 #define CASE(op) case 2*(int)op: /*fallthrough*/ case 2*(int)op+1
197
Mike Klein89b3c1f2020-07-29 16:45:05 -0500198 // These 128-bit ops are implemented serially for simplicity.
Mike Klein31367892020-07-30 08:19:12 -0500199 CASE(Op::store128): {
200 int ptr = immz>>1,
201 lane = immz&1;
Mike Klein89b3c1f2020-07-29 16:45:05 -0500202 U64 src = (skvx::cast<uint64_t>(r[x].u32) << 0 |
203 skvx::cast<uint64_t>(r[y].u32) << 32);
204 for (int i = 0; i < stride; i++) {
Mike Klein31367892020-07-30 08:19:12 -0500205 memcpy((char*)args[ptr] + 16*i + 8*lane, &src[i], 8);
Mike Klein89b3c1f2020-07-29 16:45:05 -0500206 }
207 } break;
208
Mike Klein31367892020-07-30 08:19:12 -0500209 CASE(Op::load128):
Mike Klein89b3c1f2020-07-29 16:45:05 -0500210 r[d].i32 = 0;
211 for (int i = 0; i < stride; i++) {
212 memcpy(&r[d].i32[i], (const char*)args[immy] + 16*i+ 4*immz, 4);
213 } break;
214
Mike Kleinec370972020-03-05 10:15:35 -0600215 CASE(Op::assert_true):
216 #ifdef SK_DEBUG
Mike Klein4284f752020-07-10 15:16:17 -0500217 if (!all(r[x].i32)) {
Mike Kleinec370972020-03-05 10:15:35 -0600218 SkDebugf("inst %d, register %d\n", i, y);
219 for (int i = 0; i < K; i++) {
Mike Klein4284f752020-07-10 15:16:17 -0500220 SkDebugf("\t%2d: %08x (%g)\n", i, r[y].i32[i], r[y].f32[i]);
Mike Kleinec370972020-03-05 10:15:35 -0600221 }
Mike Klein51a7f952020-09-16 16:00:33 -0500222 SkASSERT(false);
Mike Kleinec370972020-03-05 10:15:35 -0600223 }
Mike Kleinec370972020-03-05 10:15:35 -0600224 #endif
225 break;
226
227 CASE(Op::index): {
228 const int iota[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,
229 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
230 static_assert(K <= SK_ARRAY_COUNT(iota), "");
231
Mike Klein4284f752020-07-10 15:16:17 -0500232 r[d].i32 = n - I32::Load(iota);
Mike Kleinec370972020-03-05 10:15:35 -0600233 } break;
234
235 CASE(Op::uniform8):
Mike Klein4284f752020-07-10 15:16:17 -0500236 r[d].i32 = *(const uint8_t* )( (const char*)args[immy] + immz );
Mike Kleinec370972020-03-05 10:15:35 -0600237 break;
238 CASE(Op::uniform16):
Mike Klein4284f752020-07-10 15:16:17 -0500239 r[d].i32 = *(const uint16_t*)( (const char*)args[immy] + immz );
Mike Kleinec370972020-03-05 10:15:35 -0600240 break;
241 CASE(Op::uniform32):
Mike Klein4284f752020-07-10 15:16:17 -0500242 r[d].i32 = *(const int* )( (const char*)args[immy] + immz );
Mike Kleinec370972020-03-05 10:15:35 -0600243 break;
244
Mike Klein4284f752020-07-10 15:16:17 -0500245 CASE(Op::splat): r[d].i32 = immy; break;
Mike Kleinec370972020-03-05 10:15:35 -0600246
Mike Klein4284f752020-07-10 15:16:17 -0500247 CASE(Op::add_f32): r[d].f32 = r[x].f32 + r[y].f32; break;
248 CASE(Op::sub_f32): r[d].f32 = r[x].f32 - r[y].f32; break;
249 CASE(Op::mul_f32): r[d].f32 = r[x].f32 * r[y].f32; break;
250 CASE(Op::div_f32): r[d].f32 = r[x].f32 / r[y].f32; break;
251 CASE(Op::min_f32): r[d].f32 = min(r[x].f32, r[y].f32); break;
252 CASE(Op::max_f32): r[d].f32 = max(r[x].f32, r[y].f32); break;
Mike Kleinec370972020-03-05 10:15:35 -0600253
Mike Klein4284f752020-07-10 15:16:17 -0500254 CASE(Op::fma_f32): r[d].f32 = fma( r[x].f32, r[y].f32, r[z].f32); break;
255 CASE(Op::fms_f32): r[d].f32 = fma( r[x].f32, r[y].f32, -r[z].f32); break;
256 CASE(Op::fnma_f32): r[d].f32 = fma(-r[x].f32, r[y].f32, r[z].f32); break;
Mike Kleinec370972020-03-05 10:15:35 -0600257
Mike Klein4284f752020-07-10 15:16:17 -0500258 CASE(Op::sqrt_f32): r[d].f32 = sqrt(r[x].f32); break;
Mike Kleinec370972020-03-05 10:15:35 -0600259
Mike Klein4284f752020-07-10 15:16:17 -0500260 CASE(Op::add_i32): r[d].i32 = r[x].i32 + r[y].i32; break;
261 CASE(Op::sub_i32): r[d].i32 = r[x].i32 - r[y].i32; break;
262 CASE(Op::mul_i32): r[d].i32 = r[x].i32 * r[y].i32; break;
Mike Kleinec370972020-03-05 10:15:35 -0600263
Mike Klein4284f752020-07-10 15:16:17 -0500264 CASE(Op::shl_i32): r[d].i32 = r[x].i32 << immy; break;
265 CASE(Op::sra_i32): r[d].i32 = r[x].i32 >> immy; break;
266 CASE(Op::shr_i32): r[d].u32 = r[x].u32 >> immy; break;
Mike Kleinec370972020-03-05 10:15:35 -0600267
Mike Klein4284f752020-07-10 15:16:17 -0500268 CASE(Op:: eq_f32): r[d].i32 = r[x].f32 == r[y].f32; break;
269 CASE(Op::neq_f32): r[d].i32 = r[x].f32 != r[y].f32; break;
270 CASE(Op:: gt_f32): r[d].i32 = r[x].f32 > r[y].f32; break;
271 CASE(Op::gte_f32): r[d].i32 = r[x].f32 >= r[y].f32; break;
Mike Kleinec370972020-03-05 10:15:35 -0600272
Mike Klein4284f752020-07-10 15:16:17 -0500273 CASE(Op:: eq_i32): r[d].i32 = r[x].i32 == r[y].i32; break;
274 CASE(Op:: gt_i32): r[d].i32 = r[x].i32 > r[y].i32; break;
Mike Kleinec370972020-03-05 10:15:35 -0600275
Mike Klein4284f752020-07-10 15:16:17 -0500276 CASE(Op::bit_and ): r[d].i32 = r[x].i32 & r[y].i32; break;
277 CASE(Op::bit_or ): r[d].i32 = r[x].i32 | r[y].i32; break;
278 CASE(Op::bit_xor ): r[d].i32 = r[x].i32 ^ r[y].i32; break;
279 CASE(Op::bit_clear): r[d].i32 = r[x].i32 & ~r[y].i32; break;
Mike Kleinec370972020-03-05 10:15:35 -0600280
Mike Klein4284f752020-07-10 15:16:17 -0500281 CASE(Op::select): r[d].i32 = skvx::if_then_else(r[x].i32, r[y].i32, r[z].i32);
Mike Kleinec370972020-03-05 10:15:35 -0600282 break;
283
Mike Klein4284f752020-07-10 15:16:17 -0500284 CASE(Op::pack): r[d].u32 = r[x].u32 | (r[y].u32 << immz); break;
Mike Kleinec370972020-03-05 10:15:35 -0600285
Mike Klein4284f752020-07-10 15:16:17 -0500286 CASE(Op::ceil): r[d].f32 = skvx::ceil(r[x].f32) ; break;
287 CASE(Op::floor): r[d].f32 = skvx::floor(r[x].f32) ; break;
288 CASE(Op::to_f32): r[d].f32 = skvx::cast<float>( r[x].i32 ); break;
289 CASE(Op::trunc): r[d].i32 = skvx::cast<int> ( r[x].f32 ); break;
290 CASE(Op::round): r[d].i32 = skvx::cast<int> (skvx::lrint(r[x].f32)); break;
Mike Klein4d680cd2020-07-15 09:58:51 -0500291
292 CASE(Op::to_half):
293 r[d].i32 = skvx::cast<int>(skvx::to_half(r[x].f32));
294 break;
295 CASE(Op::from_half):
296 r[d].f32 = skvx::from_half(skvx::cast<uint16_t>(r[x].i32));
297 break;
Mike Klein98c512c2020-09-15 10:00:27 -0500298
Mike Klein9791e502020-09-15 12:43:38 -0500299 CASE(Op::add_q14x2): r[d].i16x2 = r[x].i16x2 + r[y].i16x2; break;
300 CASE(Op::sub_q14x2): r[d].i16x2 = r[x].i16x2 - r[y].i16x2; break;
Mike Klein6d94b652020-09-16 11:37:03 -0500301 CASE(Op::mul_q14x2): r[d].i16x2 = mul_q14(r[x].i16x2, r[y].i16x2); break;
Mike Klein9791e502020-09-15 12:43:38 -0500302
303 CASE(Op::shl_q14x2): r[d].i16x2 = r[x].i16x2 << immy; break;
304 CASE(Op::sra_q14x2): r[d].i16x2 = r[x].i16x2 >> immy; break;
305 CASE(Op::shr_q14x2): r[d].u16x2 = r[x].u16x2 >> immy; break;
306
307 CASE(Op::eq_q14x2): r[d].i16x2 = r[x].i16x2 == r[y].i16x2; break;
308 CASE(Op::gt_q14x2): r[d].i16x2 = r[x].i16x2 > r[y].i16x2; break;
309
310 CASE(Op:: min_q14x2): r[d].i16x2 = min(r[x].i16x2, r[y].i16x2); break;
311 CASE(Op:: max_q14x2): r[d].i16x2 = max(r[x].i16x2, r[y].i16x2); break;
312 CASE(Op::umin_q14x2): r[d].u16x2 = min(r[x].u16x2, r[y].u16x2); break;
313
Mike Klein7b1620f2020-09-16 10:18:47 -0500314 // Happily, Clang can see through this one and generates perfect code
315 // using vpavgw without any help from us!
Mike Klein98c512c2020-09-15 10:00:27 -0500316 CASE(Op::uavg_q14x2):
Mike Klein9791e502020-09-15 12:43:38 -0500317 r[d].u16x2 = skvx::cast<uint16_t>( (skvx::cast<int>(r[x].u16x2) +
318 skvx::cast<int>(r[y].u16x2) + 1)>>1 );
319 break;
Mike Kleinec370972020-03-05 10:15:35 -0600320 #undef CASE
321 }
322 }
323 }
324 }
325
John Stilesa6841be2020-08-06 14:11:56 -0400326} // namespace SK_OPTS_NS
Mike Kleinec370972020-03-05 10:15:35 -0600327
328#endif//SkVM_opts_DEFINED