blob: 1e3a87afc7bb36c584d3efffc68351225fefd46c [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
10namespace SK_OPTS_NS {
11
12 inline void interpret_skvm(const skvm::InterpreterInstruction insts[], const int ninsts,
13 const int nregs, const int loop,
14 const int strides[], const int nargs,
15 int n, void* args[]) {
16 using namespace skvm;
17
18 // We'll operate in SIMT style, knocking off K-size chunks from n while possible.
19 // We noticed quad-pumping is slower than single-pumping and both were slower than double.
Mike Klein51d35ed2020-04-24 08:16:22 -050020 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2
Mike Kleinec370972020-03-05 10:15:35 -060021 constexpr int K = 16;
22 #else
23 constexpr int K = 8;
24 #endif
25 using I32 = skvx::Vec<K, int>;
26 using F32 = skvx::Vec<K, float>;
27 using U32 = skvx::Vec<K, uint32_t>;
28 using U16 = skvx::Vec<K, uint16_t>;
29 using U8 = skvx::Vec<K, uint8_t>;
30
Mike Kleinec370972020-03-05 10:15:35 -060031 union Slot {
32 F32 f32;
33 I32 i32;
34 U32 u32;
Mike Kleinec370972020-03-05 10:15:35 -060035 };
36
37 Slot few_regs[16];
38 std::unique_ptr<char[]> many_regs;
39
Mike Klein4284f752020-07-10 15:16:17 -050040 Slot* r = few_regs;
Mike Kleinec370972020-03-05 10:15:35 -060041
42 if (nregs > (int)SK_ARRAY_COUNT(few_regs)) {
43 // Annoyingly we can't trust that malloc() or new will work with Slot because
44 // the skvx::Vec types may have alignment greater than what they provide.
45 // We'll overallocate one extra register so we can align manually.
46 many_regs.reset(new char[ sizeof(Slot) * (nregs + 1) ]);
47
48 uintptr_t addr = (uintptr_t)many_regs.get();
49 addr += alignof(Slot) -
50 (addr & (alignof(Slot) - 1));
51 SkASSERT((addr & (alignof(Slot) - 1)) == 0);
Mike Klein4284f752020-07-10 15:16:17 -050052 r = (Slot*)addr;
Mike Kleinec370972020-03-05 10:15:35 -060053 }
54
55
Mike Kleinec370972020-03-05 10:15:35 -060056 // Step each argument pointer ahead by its stride a number of times.
57 auto step_args = [&](int times) {
58 for (int i = 0; i < nargs; i++) {
59 args[i] = (void*)( (char*)args[i] + times * strides[i] );
60 }
61 };
62
63 int start = 0,
64 stride;
65 for ( ; n > 0; start = loop, n -= stride, step_args(stride)) {
66 stride = n >= K ? K : 1;
67
68 for (int i = start; i < ninsts; i++) {
69 InterpreterInstruction inst = insts[i];
70
71 // d = op(x,y/imm,z/imm)
72 Reg d = inst.d,
73 x = inst.x,
74 y = inst.y,
75 z = inst.z;
76 int immy = inst.immy,
77 immz = inst.immz;
78
79 // Ops that interact with memory need to know whether we're stride=1 or K,
80 // but all non-memory ops can run the same code no matter the stride.
81 switch (2*(int)inst.op + (stride == K ? 1 : 0)) {
82 default: SkUNREACHABLE;
83
84 #define STRIDE_1(op) case 2*(int)op
85 #define STRIDE_K(op) case 2*(int)op + 1
Mike Klein4284f752020-07-10 15:16:17 -050086 STRIDE_1(Op::store8 ): memcpy(args[immy], &r[x].i32, 1); break;
87 STRIDE_1(Op::store16): memcpy(args[immy], &r[x].i32, 2); break;
88 STRIDE_1(Op::store32): memcpy(args[immy], &r[x].i32, 4); break;
Mike Kleinec370972020-03-05 10:15:35 -060089
Mike Klein4284f752020-07-10 15:16:17 -050090 STRIDE_K(Op::store8 ): skvx::cast<uint8_t> (r[x].i32).store(args[immy]); break;
91 STRIDE_K(Op::store16): skvx::cast<uint16_t>(r[x].i32).store(args[immy]); break;
92 STRIDE_K(Op::store32): (r[x].i32).store(args[immy]); break;
Mike Kleinec370972020-03-05 10:15:35 -060093
Mike Klein4284f752020-07-10 15:16:17 -050094 STRIDE_1(Op::load8 ): r[d].i32 = 0; memcpy(&r[d].i32, args[immy], 1); break;
95 STRIDE_1(Op::load16): r[d].i32 = 0; memcpy(&r[d].i32, args[immy], 2); break;
96 STRIDE_1(Op::load32): r[d].i32 = 0; memcpy(&r[d].i32, args[immy], 4); break;
Mike Kleinec370972020-03-05 10:15:35 -060097
Mike Klein4284f752020-07-10 15:16:17 -050098 STRIDE_K(Op::load8 ): r[d].i32= skvx::cast<int>(U8 ::Load(args[immy])); break;
99 STRIDE_K(Op::load16): r[d].i32= skvx::cast<int>(U16::Load(args[immy])); break;
100 STRIDE_K(Op::load32): r[d].i32= I32::Load(args[immy]) ; break;
Mike Kleinec370972020-03-05 10:15:35 -0600101
102 // The pointer we base our gather on is loaded indirectly from a uniform:
Mike Klein4284f752020-07-10 15:16:17 -0500103 // - args[immy] is the uniform holding our gather base pointer somewhere;
104 // - (const uint8_t*)args[immy] + immz points to the gather base pointer;
Mike Kleinec370972020-03-05 10:15:35 -0600105 // - memcpy() loads the gather base and into a pointer of the right type.
106 // After all that we have an ordinary (uniform) pointer `ptr` to load from,
Mike Klein4284f752020-07-10 15:16:17 -0500107 // and we then gather from it using the varying indices in r[x].
Mike Kleinec370972020-03-05 10:15:35 -0600108 STRIDE_1(Op::gather8):
109 for (int i = 0; i < K; i++) {
110 const uint8_t* ptr;
Mike Klein4284f752020-07-10 15:16:17 -0500111 memcpy(&ptr, (const uint8_t*)args[immy] + immz, sizeof(ptr));
112 r[d].i32[i] = (i==0) ? ptr[ r[x].i32[i] ] : 0;
Mike Kleinec370972020-03-05 10:15:35 -0600113 } break;
114 STRIDE_1(Op::gather16):
115 for (int i = 0; i < K; i++) {
116 const uint16_t* ptr;
Mike Klein4284f752020-07-10 15:16:17 -0500117 memcpy(&ptr, (const uint8_t*)args[immy] + immz, sizeof(ptr));
118 r[d].i32[i] = (i==0) ? ptr[ r[x].i32[i] ] : 0;
Mike Kleinec370972020-03-05 10:15:35 -0600119 } break;
120 STRIDE_1(Op::gather32):
121 for (int i = 0; i < K; i++) {
122 const int* ptr;
Mike Klein4284f752020-07-10 15:16:17 -0500123 memcpy(&ptr, (const uint8_t*)args[immy] + immz, sizeof(ptr));
124 r[d].i32[i] = (i==0) ? ptr[ r[x].i32[i] ] : 0;
Mike Kleinec370972020-03-05 10:15:35 -0600125 } break;
126
127 STRIDE_K(Op::gather8):
128 for (int i = 0; i < K; i++) {
129 const uint8_t* ptr;
Mike Klein4284f752020-07-10 15:16:17 -0500130 memcpy(&ptr, (const uint8_t*)args[immy] + immz, sizeof(ptr));
131 r[d].i32[i] = ptr[ r[x].i32[i] ];
Mike Kleinec370972020-03-05 10:15:35 -0600132 } break;
133 STRIDE_K(Op::gather16):
134 for (int i = 0; i < K; i++) {
135 const uint16_t* ptr;
Mike Klein4284f752020-07-10 15:16:17 -0500136 memcpy(&ptr, (const uint8_t*)args[immy] + immz, sizeof(ptr));
137 r[d].i32[i] = ptr[ r[x].i32[i] ];
Mike Kleinec370972020-03-05 10:15:35 -0600138 } break;
139 STRIDE_K(Op::gather32):
140 for (int i = 0; i < K; i++) {
141 const int* ptr;
Mike Klein4284f752020-07-10 15:16:17 -0500142 memcpy(&ptr, (const uint8_t*)args[immy] + immz, sizeof(ptr));
143 r[d].i32[i] = ptr[ r[x].i32[i] ];
Mike Kleinec370972020-03-05 10:15:35 -0600144 } break;
145
146 #undef STRIDE_1
147 #undef STRIDE_K
148
149 // Ops that don't interact with memory should never care about the stride.
150 #define CASE(op) case 2*(int)op: /*fallthrough*/ case 2*(int)op+1
151
152 CASE(Op::assert_true):
153 #ifdef SK_DEBUG
Mike Klein4284f752020-07-10 15:16:17 -0500154 if (!all(r[x].i32)) {
Mike Kleinec370972020-03-05 10:15:35 -0600155 SkDebugf("inst %d, register %d\n", i, y);
156 for (int i = 0; i < K; i++) {
Mike Klein4284f752020-07-10 15:16:17 -0500157 SkDebugf("\t%2d: %08x (%g)\n", i, r[y].i32[i], r[y].f32[i]);
Mike Kleinec370972020-03-05 10:15:35 -0600158 }
159 }
Mike Klein4284f752020-07-10 15:16:17 -0500160 SkASSERT(all(r[x].i32));
Mike Kleinec370972020-03-05 10:15:35 -0600161 #endif
162 break;
163
164 CASE(Op::index): {
165 const int iota[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,
166 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
167 static_assert(K <= SK_ARRAY_COUNT(iota), "");
168
Mike Klein4284f752020-07-10 15:16:17 -0500169 r[d].i32 = n - I32::Load(iota);
Mike Kleinec370972020-03-05 10:15:35 -0600170 } break;
171
172 CASE(Op::uniform8):
Mike Klein4284f752020-07-10 15:16:17 -0500173 r[d].i32 = *(const uint8_t* )( (const char*)args[immy] + immz );
Mike Kleinec370972020-03-05 10:15:35 -0600174 break;
175 CASE(Op::uniform16):
Mike Klein4284f752020-07-10 15:16:17 -0500176 r[d].i32 = *(const uint16_t*)( (const char*)args[immy] + immz );
Mike Kleinec370972020-03-05 10:15:35 -0600177 break;
178 CASE(Op::uniform32):
Mike Klein4284f752020-07-10 15:16:17 -0500179 r[d].i32 = *(const int* )( (const char*)args[immy] + immz );
Mike Kleinec370972020-03-05 10:15:35 -0600180 break;
181
Mike Klein4284f752020-07-10 15:16:17 -0500182 CASE(Op::splat): r[d].i32 = immy; break;
Mike Kleinec370972020-03-05 10:15:35 -0600183
Mike Klein4284f752020-07-10 15:16:17 -0500184 CASE(Op::add_f32): r[d].f32 = r[x].f32 + r[y].f32; break;
185 CASE(Op::sub_f32): r[d].f32 = r[x].f32 - r[y].f32; break;
186 CASE(Op::mul_f32): r[d].f32 = r[x].f32 * r[y].f32; break;
187 CASE(Op::div_f32): r[d].f32 = r[x].f32 / r[y].f32; break;
188 CASE(Op::min_f32): r[d].f32 = min(r[x].f32, r[y].f32); break;
189 CASE(Op::max_f32): r[d].f32 = max(r[x].f32, r[y].f32); break;
Mike Kleinec370972020-03-05 10:15:35 -0600190
Mike Klein4284f752020-07-10 15:16:17 -0500191 CASE(Op::fma_f32): r[d].f32 = fma( r[x].f32, r[y].f32, r[z].f32); break;
192 CASE(Op::fms_f32): r[d].f32 = fma( r[x].f32, r[y].f32, -r[z].f32); break;
193 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 -0600194
Mike Klein4284f752020-07-10 15:16:17 -0500195 CASE(Op::sqrt_f32): r[d].f32 = sqrt(r[x].f32); break;
Mike Kleinec370972020-03-05 10:15:35 -0600196
Mike Klein4284f752020-07-10 15:16:17 -0500197 CASE(Op::add_i32): r[d].i32 = r[x].i32 + r[y].i32; break;
198 CASE(Op::sub_i32): r[d].i32 = r[x].i32 - r[y].i32; break;
199 CASE(Op::mul_i32): r[d].i32 = r[x].i32 * r[y].i32; break;
Mike Kleinec370972020-03-05 10:15:35 -0600200
Mike Klein4284f752020-07-10 15:16:17 -0500201 CASE(Op::shl_i32): r[d].i32 = r[x].i32 << immy; break;
202 CASE(Op::sra_i32): r[d].i32 = r[x].i32 >> immy; break;
203 CASE(Op::shr_i32): r[d].u32 = r[x].u32 >> immy; break;
Mike Kleinec370972020-03-05 10:15:35 -0600204
Mike Klein4284f752020-07-10 15:16:17 -0500205 CASE(Op:: eq_f32): r[d].i32 = r[x].f32 == r[y].f32; break;
206 CASE(Op::neq_f32): r[d].i32 = r[x].f32 != r[y].f32; break;
207 CASE(Op:: gt_f32): r[d].i32 = r[x].f32 > r[y].f32; break;
208 CASE(Op::gte_f32): r[d].i32 = r[x].f32 >= r[y].f32; break;
Mike Kleinec370972020-03-05 10:15:35 -0600209
Mike Klein4284f752020-07-10 15:16:17 -0500210 CASE(Op:: eq_i32): r[d].i32 = r[x].i32 == r[y].i32; break;
211 CASE(Op:: gt_i32): r[d].i32 = r[x].i32 > r[y].i32; break;
Mike Kleinec370972020-03-05 10:15:35 -0600212
Mike Klein4284f752020-07-10 15:16:17 -0500213 CASE(Op::bit_and ): r[d].i32 = r[x].i32 & r[y].i32; break;
214 CASE(Op::bit_or ): r[d].i32 = r[x].i32 | r[y].i32; break;
215 CASE(Op::bit_xor ): r[d].i32 = r[x].i32 ^ r[y].i32; break;
216 CASE(Op::bit_clear): r[d].i32 = r[x].i32 & ~r[y].i32; break;
Mike Kleinec370972020-03-05 10:15:35 -0600217
Mike Klein4284f752020-07-10 15:16:17 -0500218 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 -0600219 break;
220
Mike Klein4284f752020-07-10 15:16:17 -0500221 CASE(Op::pack): r[d].u32 = r[x].u32 | (r[y].u32 << immz); break;
Mike Kleinec370972020-03-05 10:15:35 -0600222
Mike Klein4284f752020-07-10 15:16:17 -0500223 CASE(Op::ceil): r[d].f32 = skvx::ceil(r[x].f32) ; break;
224 CASE(Op::floor): r[d].f32 = skvx::floor(r[x].f32) ; break;
225 CASE(Op::to_f32): r[d].f32 = skvx::cast<float>( r[x].i32 ); break;
226 CASE(Op::trunc): r[d].i32 = skvx::cast<int> ( r[x].f32 ); break;
227 CASE(Op::round): r[d].i32 = skvx::cast<int> (skvx::lrint(r[x].f32)); break;
Mike Kleinec370972020-03-05 10:15:35 -0600228 #undef CASE
229 }
230 }
231 }
232 }
233
234}
235
236#endif//SkVM_opts_DEFINED