Debian Codegraph @

src/opts/SkRasterPipeline_opts.h

raw
1 /*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkRasterPipeline_opts_DEFINED
9 #define SkRasterPipeline_opts_DEFINED
10
11 #include "include/core/SkTypes.h"
12 #include "include/private/base/SkMalloc.h"
13 #include "include/private/base/SkSpan_impl.h"
14 #include "include/private/base/SkTemplates.h"
15 #include "modules/skcms/skcms.h"
16 #include "src/base/SkUtils.h" // unaligned_{load,store}
17 #include "src/core/SkRasterPipeline.h"
18 #include "src/core/SkRasterPipelineContextUtils.h"
19 #include "src/shaders/SkPerlinNoiseShaderType.h"
20 #include "src/sksl/tracing/SkSLTraceHook.h"
21
22 #include <cstdint>
23 #include <type_traits>
24
25 // Every function in this file should be marked static and inline using SI.
26 #if defined(__clang__) || defined(__GNUC__)
27 #define SI __attribute__((always_inline)) static inline
28 #else
29 #define SI static inline
30 #endif
31
32 #if defined(__clang__)
33 #define SK_UNROLL _Pragma("unroll")
34 #else
35 #define SK_UNROLL
36 #endif
37
38 // Why does RasterPipeline have its own SIMD wrapper and is not using SkVx? SkVx is designed
39 // for keeping things simple, e.g. so you can put vectors in classes. SkVx has a very simple,
40 // predictable, memory layout - they are equivalent to a struct with an array of n values.
41 // Unfortunately, because of that, they will not pass in registers. A core design principle of
42 // SkRP is to have the 8 parameters passed into a stage be actual hardware registers (for
43 // optimal performance).
44 #if defined(__clang__)
45 template <int N, typename T> using Vec = T __attribute__((ext_vector_type(N)));
46 #elif defined(__GNUC__)
47 // Unfortunately, GCC does not allow us to omit the struct. This will not compile:
48 // template <int N, typename T> using Vec = T __attribute__((vector_size(N*sizeof(T))));
49 template <int N, typename T> struct VecHelper {
50 typedef T __attribute__((vector_size(N * sizeof(T)))) V;
51 };
52 template <int N, typename T> using Vec = typename VecHelper<N, T>::V;
53 #endif
54
55 template <typename Dst, typename Src>
56 SI Dst widen_cast(const Src& src) {
57 static_assert(sizeof(Dst) > sizeof(Src));
58 static_assert(std::is_trivially_copyable<Dst>::value);
59 static_assert(std::is_trivially_copyable<Src>::value);
60 Dst dst;
61 memcpy(&dst, &src, sizeof(Src));
62 return dst;
63 }
64
65 struct Ctx {
66 SkRasterPipelineStage* fStage;
67
68 template <typename T>
69 operator T*() {
70 return (T*)fStage->ctx;
71 }
72 };
73
74 using NoCtx = const void*;
75
76 #if defined(SKRP_CPU_SCALAR) || defined(SKRP_CPU_NEON) || defined(SKRP_CPU_HSW) || \
77 defined(SKRP_CPU_SKX) || defined(SKRP_CPU_AVX) || defined(SKRP_CPU_SSE41) || \
78 defined(SKRP_CPU_SSE2)
79 // Honor the existing setting
80 #elif !defined(__clang__) && !defined(__GNUC__)
81 #define SKRP_CPU_SCALAR
82 #elif defined(SK_ARM_HAS_NEON)
83 #define SKRP_CPU_NEON
84 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SKX
85 #define SKRP_CPU_SKX
86 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2
87 #define SKRP_CPU_HSW
88 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX
89 #define SKRP_CPU_AVX
90 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
91 #define SKRP_CPU_SSE41
92 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
93 #define SKRP_CPU_SSE2
94 #elif SK_CPU_LSX_LEVEL >= SK_CPU_LSX_LEVEL_LASX
95 #define SKRP_CPU_LASX
96 #elif SK_CPU_LSX_LEVEL >= SK_CPU_LSX_LEVEL_LSX
97 #define SKRP_CPU_LSX
98 #else
99 #define SKRP_CPU_SCALAR
100 #endif
101
102 #if defined(SKRP_CPU_SCALAR)
103 #include <math.h>
104 #elif defined(SKRP_CPU_NEON)
105 #include <arm_neon.h>
106 #elif defined(SKRP_CPU_LASX)
107 #include <lasxintrin.h>
108 #include <lsxintrin.h>
109 #elif defined(SKRP_CPU_LSX)
110 #include <lsxintrin.h>
111 #else
112 #include <immintrin.h>
113 #endif
114
115 // Notes:
116 // * rcp_fast and rcp_precise both produce a reciprocal, but rcp_fast is an estimate with at least
117 // 12 bits of precision while rcp_precise should be accurate for float size. For ARM rcp_precise
118 // requires 2 Newton-Raphson refinement steps because its estimate has 8 bit precision, and for
119 // Intel this requires one additional step because its estimate has 12 bit precision.
120 //
121 // * Don't call rcp_approx or rsqrt_approx directly; only use rcp_fast and rsqrt.
122
123 namespace SK_OPTS_NS {
124 #if defined(SKRP_CPU_SCALAR)
125 // This path should lead to portable scalar code.
126 using F = float ;
127 using I32 = int32_t;
128 using U64 = uint64_t;
129 using U32 = uint32_t;
130 using U16 = uint16_t;
131 using U8 = uint8_t ;
132
133 SI F min(F a, F b) { return fminf(a,b); }
134 SI I32 min(I32 a, I32 b) { return a < b ? a : b; }
135 SI U32 min(U32 a, U32 b) { return a < b ? a : b; }
136 SI F max(F a, F b) { return fmaxf(a,b); }
137 SI I32 max(I32 a, I32 b) { return a > b ? a : b; }
138 SI U32 max(U32 a, U32 b) { return a > b ? a : b; }
139
140 SI F mad(F f, F m, F a) { return a+f*m; }
141 SI F nmad(F f, F m, F a) { return a-f*m; }
142 SI F abs_ (F v) { return fabsf(v); }
143 SI I32 abs_ (I32 v) { return v < 0 ? -v : v; }
144 SI F floor_(F v) { return floorf(v); }
145 SI F ceil_(F v) { return ceilf(v); }
146 SI F rcp_approx(F v) { return 1.0f / v; } // use rcp_fast instead
147 SI F rsqrt_approx(F v) { return 1.0f / sqrtf(v); }
148 SI F sqrt_ (F v) { return sqrtf(v); }
149 SI F rcp_precise (F v) { return 1.0f / v; }
150
151 SI I32 iround(F v) { return (I32)(v + 0.5f); }
152 SI U32 round(F v) { return (U32)(v + 0.5f); }
153 SI U16 pack(U32 v) { return (U16)v; }
154 SI U8 pack(U16 v) { return (U8)v; }
155
156 SI F if_then_else(I32 c, F t, F e) { return c ? t : e; }
157 SI I32 if_then_else(I32 c, I32 t, I32 e) { return c ? t : e; }
158
159 SI bool any(I32 c) { return c != 0; }
160 SI bool all(I32 c) { return c != 0; }
161
162 template <typename T>
163 SI T gather(const T* p, U32 ix) { return p[ix]; }
164 template <typename T>
165 SI T gather_unaligned(const T* p, U32 ix) { return gather<T>(p, ix); }
166
167 SI void scatter_masked(I32 src, int* dst, U32 ix, I32 mask) {
168 dst[ix] = mask ? src : dst[ix];
169 }
170
171 SI void load2(const uint16_t* ptr, U16* r, U16* g) {
172 *r = ptr[0];
173 *g = ptr[1];
174 }
175 SI void store2(uint16_t* ptr, U16 r, U16 g) {
176 ptr[0] = r;
177 ptr[1] = g;
178 }
179 SI void load4(const uint16_t* ptr, U16* r, U16* g, U16* b, U16* a) {
180 *r = ptr[0];
181 *g = ptr[1];
182 *b = ptr[2];
183 *a = ptr[3];
184 }
185 SI void store4(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
186 ptr[0] = r;
187 ptr[1] = g;
188 ptr[2] = b;
189 ptr[3] = a;
190 }
191
192 SI void load4(const float* ptr, F* r, F* g, F* b, F* a) {
193 *r = ptr[0];
194 *g = ptr[1];
195 *b = ptr[2];
196 *a = ptr[3];
197 }
198 SI void store4(float* ptr, F r, F g, F b, F a) {
199 ptr[0] = r;
200 ptr[1] = g;
201 ptr[2] = b;
202 ptr[3] = a;
203 }
204
205 #elif defined(SKRP_CPU_NEON)
206 template <typename T> using V = Vec<4, T>;
207 using F = V<float >;
208 using I32 = V< int32_t>;
209 using U64 = V<uint64_t>;
210 using U32 = V<uint32_t>;
211 using U16 = V<uint16_t>;
212 using U8 = V<uint8_t >;
213
214 // We polyfill a few routines that Clang doesn't build into ext_vector_types.
215 SI F min(F a, F b) { return vminq_f32(a,b); }
216 SI I32 min(I32 a, I32 b) { return vminq_s32(a,b); }
217 SI U32 min(U32 a, U32 b) { return vminq_u32(a,b); }
218 SI F max(F a, F b) { return vmaxq_f32(a,b); }
219 SI I32 max(I32 a, I32 b) { return vmaxq_s32(a,b); }
220 SI U32 max(U32 a, U32 b) { return vmaxq_u32(a,b); }
221
222 SI F abs_ (F v) { return vabsq_f32(v); }
223 SI I32 abs_ (I32 v) { return vabsq_s32(v); }
224 SI F rcp_approx(F v) { auto e = vrecpeq_f32(v); return vrecpsq_f32 (v,e ) * e; }
225 SI F rcp_precise(F v) { auto e = rcp_approx(v); return vrecpsq_f32 (v,e ) * e; }
226 SI F rsqrt_approx(F v) { auto e = vrsqrteq_f32(v); return vrsqrtsq_f32(v,e*e) * e; }
227
228 SI U16 pack(U32 v) { return __builtin_convertvector(v, U16); }
229 SI U8 pack(U16 v) { return __builtin_convertvector(v, U8); }
230
231 SI F if_then_else(I32 c, F t, F e) { return vbslq_f32((U32)c,t,e); }
232 SI I32 if_then_else(I32 c, I32 t, I32 e) { return vbslq_s32((U32)c,t,e); }
233
234 #if defined(SK_CPU_ARM64)
235 SI bool any(I32 c) { return vmaxvq_u32((U32)c) != 0; }
236 SI bool all(I32 c) { return vminvq_u32((U32)c) != 0; }
237
238 SI F mad(F f, F m, F a) { return vfmaq_f32(a,f,m); }
239 SI F nmad(F f, F m, F a) { return vfmsq_f32(a,f,m); }
240 SI F floor_(F v) { return vrndmq_f32(v); }
241 SI F ceil_(F v) { return vrndpq_f32(v); }
242 SI F sqrt_(F v) { return vsqrtq_f32(v); }
243 SI I32 iround(F v) { return vcvtnq_s32_f32(v); }
244 SI U32 round(F v) { return vcvtnq_u32_f32(v); }
245 #else
246 SI bool any(I32 c) { return c[0] | c[1] | c[2] | c[3]; }
247 SI bool all(I32 c) { return c[0] & c[1] & c[2] & c[3]; }
248
249 SI F mad(F f, F m, F a) { return vmlaq_f32(a,f,m); }
250 SI F nmad(F f, F m, F a) { return vmlsq_f32(a,f,m); }
251
252 SI F floor_(F v) {
253 F roundtrip = vcvtq_f32_s32(vcvtq_s32_f32(v));
254 return roundtrip - if_then_else(roundtrip > v, F() + 1, F());
255 }
256
257 SI F ceil_(F v) {
258 F roundtrip = vcvtq_f32_s32(vcvtq_s32_f32(v));
259 return roundtrip + if_then_else(roundtrip < v, F() + 1, F());
260 }
261
262 SI F sqrt_(F v) {
263 auto e = vrsqrteq_f32(v); // Estimate and two refinement steps for e = rsqrt(v).
264 e *= vrsqrtsq_f32(v,e*e);
265 e *= vrsqrtsq_f32(v,e*e);
266 return v*e; // sqrt(v) == v*rsqrt(v).
267 }
268
269 SI I32 iround(F v) {
270 return vcvtq_s32_f32(v + 0.5f);
271 }
272
273 SI U32 round(F v) {
274 return vcvtq_u32_f32(v + 0.5f);
275 }
276 #endif
277
278 template <typename T>
279 SI V<T> gather(const T* ptr, U32 ix) {
280 // The compiler assumes ptr is aligned, which caused crashes on some
281 // arm32 chips because a register was marked as "aligned to 32 bits"
282 // incorrectly. skbug.com/409859319
283 SkASSERTF(reinterpret_cast<uintptr_t>(ptr) % alignof(T) == 0,
284 "Should use gather_unaligned");
285 return V<T>{ptr[ix[0]], ptr[ix[1]], ptr[ix[2]], ptr[ix[3]]};
286 }
287 template <typename T>
288 SI V<T> gather_unaligned(const T* ptr, U32 ix) {
289 // This tells the compiler ptr might not be aligned appropriately, so
290 // it generates better assembly.
291 typedef T __attribute__ ((aligned (1))) unaligned_ptr;
292 const unaligned_ptr* uptr = static_cast<const unaligned_ptr*>(ptr);
293 return V<T>{uptr[ix[0]], uptr[ix[1]], uptr[ix[2]], uptr[ix[3]]};
294 }
295
296 SI void scatter_masked(I32 src, int* dst, U32 ix, I32 mask) {
297 I32 before = gather(dst, ix);
298 I32 after = if_then_else(mask, src, before);
299 dst[ix[0]] = after[0];
300 dst[ix[1]] = after[1];
301 dst[ix[2]] = after[2];
302 dst[ix[3]] = after[3];
303 }
304 SI void load2(const uint16_t* ptr, U16* r, U16* g) {
305 uint16x4x2_t rg = vld2_u16(ptr);
306 *r = rg.val[0];
307 *g = rg.val[1];
308 }
309 SI void store2(uint16_t* ptr, U16 r, U16 g) {
310 vst2_u16(ptr, (uint16x4x2_t{{r,g}}));
311 }
312 SI void load4(const uint16_t* ptr, U16* r, U16* g, U16* b, U16* a) {
313 uint16x4x4_t rgba = vld4_u16(ptr);
314 *r = rgba.val[0];
315 *g = rgba.val[1];
316 *b = rgba.val[2];
317 *a = rgba.val[3];
318 }
319
320 SI void store4(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
321 vst4_u16(ptr, (uint16x4x4_t{{r,g,b,a}}));
322 }
323 SI void load4(const float* ptr, F* r, F* g, F* b, F* a) {
324 float32x4x4_t rgba = vld4q_f32(ptr);
325 *r = rgba.val[0];
326 *g = rgba.val[1];
327 *b = rgba.val[2];
328 *a = rgba.val[3];
329 }
330 SI void store4(float* ptr, F r, F g, F b, F a) {
331 vst4q_f32(ptr, (float32x4x4_t{{r,g,b,a}}));
332 }
333
334 #elif defined(SKRP_CPU_SKX)
335 template <typename T> using V = Vec<16, T>;
336 using F = V<float >;
337 using I32 = V< int32_t>;
338 using U64 = V<uint64_t>;
339 using U32 = V<uint32_t>;
340 using U16 = V<uint16_t>;
341 using U8 = V<uint8_t >;
342
343 SI F mad(F f, F m, F a) { return _mm512_fmadd_ps(f, m, a); }
344 SI F nmad(F f, F m, F a) { return _mm512_fnmadd_ps(f, m, a); }
345 SI F min(F a, F b) { return _mm512_min_ps(a,b); }
346 SI I32 min(I32 a, I32 b) { return (I32)_mm512_min_epi32((__m512i)a,(__m512i)b); }
347 SI U32 min(U32 a, U32 b) { return (U32)_mm512_min_epu32((__m512i)a,(__m512i)b); }
348 SI F max(F a, F b) { return _mm512_max_ps(a,b); }
349 SI I32 max(I32 a, I32 b) { return (I32)_mm512_max_epi32((__m512i)a,(__m512i)b); }
350 SI U32 max(U32 a, U32 b) { return (U32)_mm512_max_epu32((__m512i)a,(__m512i)b); }
351 SI F abs_ (F v) { return _mm512_and_ps(v, _mm512_sub_ps(_mm512_setzero(), v)); }
352 SI I32 abs_ (I32 v) { return (I32)_mm512_abs_epi32((__m512i)v); }
353 SI F floor_(F v) { return _mm512_floor_ps(v); }
354 SI F ceil_(F v) { return _mm512_ceil_ps(v); }
355 SI F rcp_approx(F v) { return _mm512_rcp14_ps (v); }
356 SI F rsqrt_approx (F v) { return _mm512_rsqrt14_ps(v); }
357 SI F sqrt_ (F v) { return _mm512_sqrt_ps (v); }
358 SI F rcp_precise (F v) {
359 F e = rcp_approx(v);
360 return _mm512_fnmadd_ps(v, e, _mm512_set1_ps(2.0f)) * e;
361 }
362 SI I32 iround(F v) { return (I32)_mm512_cvtps_epi32(v); }
363 SI U32 round(F v) { return (U32)_mm512_cvtps_epi32(v); }
364 SI U16 pack(U32 v) {
365 __m256i rst = _mm256_packus_epi32(_mm512_castsi512_si256((__m512i)v),
366 _mm512_extracti64x4_epi64((__m512i)v, 1));
367 return (U16)_mm256_permutex_epi64(rst, 216);
368 }
369 SI U8 pack(U16 v) {
370 __m256i rst = _mm256_packus_epi16((__m256i)v, (__m256i)v);
371 return (U8)_mm256_castsi256_si128(_mm256_permute4x64_epi64(rst, 8));
372 }
373 SI F if_then_else(I32 c, F t, F e) {
374 __m512i mask = _mm512_set1_epi32(0x80000000);
375 __m512i aa = _mm512_and_si512((__m512i)c, mask);
376 return _mm512_mask_blend_ps(_mm512_test_epi32_mask(aa, aa),e,t);
377 }
378 SI I32 if_then_else(I32 c, I32 t, I32 e) {
379 __m512i mask = _mm512_set1_epi32(0x80000000);
380 __m512i aa = _mm512_and_si512((__m512i)c, mask);
381 return (I32)_mm512_mask_blend_epi32(_mm512_test_epi32_mask(aa, aa),(__m512i)e,(__m512i)t);
382 }
383 SI bool any(I32 c) {
384 __mmask16 mask32 = _mm512_test_epi32_mask((__m512i)c, (__m512i)c);
385 return mask32 != 0;
386 }
387 SI bool all(I32 c) {
388 __mmask16 mask32 = _mm512_test_epi32_mask((__m512i)c, (__m512i)c);
389 return mask32 == 0xffff;
390 }
391 template <typename T>
392 SI V<T> gather(const T* p, U32 ix) {
393 return V<T>{ p[ix[ 0]], p[ix[ 1]], p[ix[ 2]], p[ix[ 3]],
394 p[ix[ 4]], p[ix[ 5]], p[ix[ 6]], p[ix[ 7]],
395 p[ix[ 8]], p[ix[ 9]], p[ix[10]], p[ix[11]],
396 p[ix[12]], p[ix[13]], p[ix[14]], p[ix[15]] };
397 }
398 SI F gather(const float* p, U32 ix) { return _mm512_i32gather_ps((__m512i)ix, p, 4); }
399 SI U32 gather(const uint32_t* p, U32 ix) {
400 return (U32)_mm512_i32gather_epi32((__m512i)ix, p, 4); }
401 SI U64 gather(const uint64_t* p, U32 ix) {
402 __m512i parts[] = {
403 _mm512_i32gather_epi64(_mm512_castsi512_si256((__m512i)ix), p, 8),
404 _mm512_i32gather_epi64(_mm512_extracti32x8_epi32((__m512i)ix, 1), p, 8),
405 };
406 return sk_bit_cast<U64>(parts);
407 }
408 template <typename T>
409 SI V<T> gather_unaligned(const T* p, U32 ix) {
410 return gather(p, ix);
411 }
412
413 template <typename V, typename S>
414 SI void scatter_masked(V src, S* dst, U32 ix, I32 mask) {
415 V before = gather(dst, ix);
416 V after = if_then_else(mask, src, before);
417 dst[ix[0]] = after[0];
418 dst[ix[1]] = after[1];
419 dst[ix[2]] = after[2];
420 dst[ix[3]] = after[3];
421 dst[ix[4]] = after[4];
422 dst[ix[5]] = after[5];
423 dst[ix[6]] = after[6];
424 dst[ix[7]] = after[7];
425 dst[ix[8]] = after[8];
426 dst[ix[9]] = after[9];
427 dst[ix[10]] = after[10];
428 dst[ix[11]] = after[11];
429 dst[ix[12]] = after[12];
430 dst[ix[13]] = after[13];
431 dst[ix[14]] = after[14];
432 dst[ix[15]] = after[15];
433 }
434
435 SI void load2(const uint16_t* ptr, U16* r, U16* g) {
436 __m256i _01234567 = _mm256_loadu_si256(((const __m256i*)ptr) + 0);
437 __m256i _89abcdef = _mm256_loadu_si256(((const __m256i*)ptr) + 1);
438
439 *r = (U16)_mm256_permute4x64_epi64(_mm256_packs_epi32(_mm256_srai_epi32(_mm256_slli_epi32
440 (_01234567, 16), 16), _mm256_srai_epi32(_mm256_slli_epi32(_89abcdef, 16), 16)), 216);
441 *g = (U16)_mm256_permute4x64_epi64(_mm256_packs_epi32(_mm256_srai_epi32(_01234567, 16),
442 _mm256_srai_epi32(_89abcdef, 16)), 216);
443 }
444 SI void store2(uint16_t* ptr, U16 r, U16 g) {
445 __m256i _01234567 = _mm256_unpacklo_epi16((__m256i)r, (__m256i)g);
446 __m256i _89abcdef = _mm256_unpackhi_epi16((__m256i)r, (__m256i)g);
447 __m512i combinedVector = _mm512_inserti64x4(_mm512_castsi256_si512(_01234567),
448 _89abcdef, 1);
449 __m512i aa = _mm512_permutexvar_epi64(_mm512_setr_epi64(0,1,4,5,2,3,6,7), combinedVector);
450 _01234567 = _mm512_castsi512_si256(aa);
451 _89abcdef = _mm512_extracti64x4_epi64(aa, 1);
452
453 _mm256_storeu_si256((__m256i*)ptr + 0, _01234567);
454 _mm256_storeu_si256((__m256i*)ptr + 1, _89abcdef);
455 }
456
457 SI void load4(const uint16_t* ptr, U16* r, U16* g, U16* b, U16* a) {
458 __m256i _0123 = _mm256_loadu_si256((const __m256i*)ptr),
459 _4567 = _mm256_loadu_si256(((const __m256i*)ptr) + 1),
460 _89ab = _mm256_loadu_si256(((const __m256i*)ptr) + 2),
461 _cdef = _mm256_loadu_si256(((const __m256i*)ptr) + 3);
462
463 auto a0 = _mm256_unpacklo_epi16(_0123, _4567),
464 a1 = _mm256_unpackhi_epi16(_0123, _4567),
465 b0 = _mm256_unpacklo_epi16(a0, a1),
466 b1 = _mm256_unpackhi_epi16(a0, a1),
467 a2 = _mm256_unpacklo_epi16(_89ab, _cdef),
468 a3 = _mm256_unpackhi_epi16(_89ab, _cdef),
469 b2 = _mm256_unpacklo_epi16(a2, a3),
470 b3 = _mm256_unpackhi_epi16(a2, a3),
471 rr = _mm256_unpacklo_epi64(b0, b2),
472 gg = _mm256_unpackhi_epi64(b0, b2),
473 bb = _mm256_unpacklo_epi64(b1, b3),
474 aa = _mm256_unpackhi_epi64(b1, b3);
475
476 *r = (U16)_mm256_permutexvar_epi32(_mm256_setr_epi32(0,4,1,5,2,6,3,7), rr);
477 *g = (U16)_mm256_permutexvar_epi32(_mm256_setr_epi32(0,4,1,5,2,6,3,7), gg);
478 *b = (U16)_mm256_permutexvar_epi32(_mm256_setr_epi32(0,4,1,5,2,6,3,7), bb);
479 *a = (U16)_mm256_permutexvar_epi32(_mm256_setr_epi32(0,4,1,5,2,6,3,7), aa);
480 }
481 SI void store4(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
482 auto rg012389ab = _mm256_unpacklo_epi16((__m256i)r, (__m256i)g),
483 rg4567cdef = _mm256_unpackhi_epi16((__m256i)r, (__m256i)g),
484 ba012389ab = _mm256_unpacklo_epi16((__m256i)b, (__m256i)a),
485 ba4567cdef = _mm256_unpackhi_epi16((__m256i)b, (__m256i)a);
486
487 auto _0189 = _mm256_unpacklo_epi32(rg012389ab, ba012389ab),
488 _23ab = _mm256_unpackhi_epi32(rg012389ab, ba012389ab),
489 _45cd = _mm256_unpacklo_epi32(rg4567cdef, ba4567cdef),
490 _67ef = _mm256_unpackhi_epi32(rg4567cdef, ba4567cdef);
491
492 auto _ab23 = _mm256_permutex_epi64(_23ab, 78),
493 _0123 = _mm256_blend_epi32(_0189, _ab23, 0xf0),
494 _89ab = _mm256_permutex_epi64(_mm256_blend_epi32(_0189, _ab23, 0x0f), 78),
495 _ef67 = _mm256_permutex_epi64(_67ef, 78),
496 _4567 = _mm256_blend_epi32(_45cd, _ef67, 0xf0),
497 _cdef = _mm256_permutex_epi64(_mm256_blend_epi32(_45cd, _ef67, 0x0f), 78);
498
499 _mm256_storeu_si256((__m256i*)ptr, _0123);
500 _mm256_storeu_si256((__m256i*)ptr + 1, _4567);
501 _mm256_storeu_si256((__m256i*)ptr + 2, _89ab);
502 _mm256_storeu_si256((__m256i*)ptr + 3, _cdef);
503 }
504
505 SI void load4(const float* ptr, F* r, F* g, F* b, F* a) {
506 F _048c, _159d, _26ae, _37bf;
507
508 _048c = _mm512_castps128_ps512(_mm_loadu_ps(ptr) );
509 _048c = _mm512_insertf32x4(_048c, _mm_loadu_ps(ptr+16), 1);
510 _048c = _mm512_insertf32x4(_048c, _mm_loadu_ps(ptr+32), 2);
511 _048c = _mm512_insertf32x4(_048c, _mm_loadu_ps(ptr+48), 3);
512 _159d = _mm512_castps128_ps512(_mm_loadu_ps(ptr+4) );
513 _159d = _mm512_insertf32x4(_159d, _mm_loadu_ps(ptr+20), 1);
514 _159d = _mm512_insertf32x4(_159d, _mm_loadu_ps(ptr+36), 2);
515 _159d = _mm512_insertf32x4(_159d, _mm_loadu_ps(ptr+52), 3);
516 _26ae = _mm512_castps128_ps512(_mm_loadu_ps(ptr+8) );
517 _26ae = _mm512_insertf32x4(_26ae, _mm_loadu_ps(ptr+24), 1);
518 _26ae = _mm512_insertf32x4(_26ae, _mm_loadu_ps(ptr+40), 2);
519 _26ae = _mm512_insertf32x4(_26ae, _mm_loadu_ps(ptr+56), 3);
520 _37bf = _mm512_castps128_ps512(_mm_loadu_ps(ptr+12) );
521 _37bf = _mm512_insertf32x4(_37bf, _mm_loadu_ps(ptr+28), 1);
522 _37bf = _mm512_insertf32x4(_37bf, _mm_loadu_ps(ptr+44), 2);
523 _37bf = _mm512_insertf32x4(_37bf, _mm_loadu_ps(ptr+60), 3);
524
525 F rg02468acf = _mm512_unpacklo_ps(_048c, _26ae),
526 ba02468acf = _mm512_unpackhi_ps(_048c, _26ae),
527 rg13579bde = _mm512_unpacklo_ps(_159d, _37bf),
528 ba13579bde = _mm512_unpackhi_ps(_159d, _37bf);
529
530 *r = (F)_mm512_unpacklo_ps(rg02468acf, rg13579bde);
531 *g = (F)_mm512_unpackhi_ps(rg02468acf, rg13579bde);
532 *b = (F)_mm512_unpacklo_ps(ba02468acf, ba13579bde);
533 *a = (F)_mm512_unpackhi_ps(ba02468acf, ba13579bde);
534 }
535
536 SI void store4(float* ptr, F r, F g, F b, F a) {
537 F rg014589cd = _mm512_unpacklo_ps(r, g),
538 rg2367abef = _mm512_unpackhi_ps(r, g),
539 ba014589cd = _mm512_unpacklo_ps(b, a),
540 ba2367abef = _mm512_unpackhi_ps(b, a);
541
542 F _048c = (F)_mm512_unpacklo_pd((__m512d)rg014589cd, (__m512d)ba014589cd),
543 _26ae = (F)_mm512_unpacklo_pd((__m512d)rg2367abef, (__m512d)ba2367abef),
544 _159d = (F)_mm512_unpackhi_pd((__m512d)rg014589cd, (__m512d)ba014589cd),
545 _37bf = (F)_mm512_unpackhi_pd((__m512d)rg2367abef, (__m512d)ba2367abef);
546
547 F _ae26 = (F)_mm512_permutexvar_pd(_mm512_setr_epi64(4,5,6,7,0,1,2,3), (__m512d)_26ae),
548 _bf37 = (F)_mm512_permutexvar_pd(_mm512_setr_epi64(4,5,6,7,0,1,2,3), (__m512d)_37bf),
549 _8c04 = (F)_mm512_permutexvar_pd(_mm512_setr_epi64(4,5,6,7,0,1,2,3), (__m512d)_048c),
550 _9d15 = (F)_mm512_permutexvar_pd(_mm512_setr_epi64(4,5,6,7,0,1,2,3), (__m512d)_159d);
551
552 __m512i index = _mm512_setr_epi32(4,5,6,7,0,1,2,3,12,13,14,15,8,9,10,11);
553 F _0426 = (F)_mm512_permutex2var_pd((__m512d)_048c, _mm512_setr_epi64(0,1,2,3,12,13,14,15),
554 (__m512d)_ae26),
555 _1537 = (F)_mm512_permutex2var_pd((__m512d)_159d, _mm512_setr_epi64(0,1,2,3,12,13,14,15),
556 (__m512d)_bf37),
557 _5173 = _mm512_permutexvar_ps(index, _1537),
558 _0123 = (F)_mm512_permutex2var_pd((__m512d)_0426, _mm512_setr_epi64(0,1,10,11,4,5,14,15),
559 (__m512d)_5173);
560
561 F _5476 = (F)_mm512_permutex2var_pd((__m512d)_5173, _mm512_setr_epi64(0,1,10,11,4,5,14,15),
562 (__m512d)_0426),
563 _4567 = _mm512_permutexvar_ps(index, _5476),
564 _8cae = (F)_mm512_permutex2var_pd((__m512d)_8c04, _mm512_setr_epi64(0,1,2,3,12,13,14,15),
565 (__m512d)_26ae),
566 _9dbf = (F)_mm512_permutex2var_pd((__m512d)_9d15, _mm512_setr_epi64(0,1,2,3,12,13,14,15),
567 (__m512d)_37bf),
568 _d9fb = _mm512_permutexvar_ps(index, _9dbf),
569 _89ab = (F)_mm512_permutex2var_pd((__m512d)_8cae, _mm512_setr_epi64(0,1,10,11,4,5,14,15),
570 (__m512d)_d9fb),
571 _dcfe = (F)_mm512_permutex2var_pd((__m512d)_d9fb, _mm512_setr_epi64(0,1,10,11,4,5,14,15),
572 (__m512d)_8cae),
573 _cdef = _mm512_permutexvar_ps(index, _dcfe);
574
575 _mm512_storeu_ps(ptr+0, _0123);
576 _mm512_storeu_ps(ptr+16, _4567);
577 _mm512_storeu_ps(ptr+32, _89ab);
578 _mm512_storeu_ps(ptr+48, _cdef);
579 }
580
581 #elif defined(SKRP_CPU_HSW)
582 // These are __m256 and __m256i, but friendlier and strongly-typed.
583 template <typename T> using V = Vec<8, T>;
584 using F = V<float >;
585 using I32 = V< int32_t>;
586 using U64 = V<uint64_t>;
587 using U32 = V<uint32_t>;
588 using U16 = V<uint16_t>;
589 using U8 = V<uint8_t >;
590
591 SI F mad(F f, F m, F a) { return _mm256_fmadd_ps(f, m, a); }
592 SI F nmad(F f, F m, F a) { return _mm256_fnmadd_ps(f, m, a); }
593
594 SI F min(F a, F b) { return _mm256_min_ps(a,b); }
595 SI I32 min(I32 a, I32 b) { return (I32)_mm256_min_epi32((__m256i)a,(__m256i)b); }
596 SI U32 min(U32 a, U32 b) { return (U32)_mm256_min_epu32((__m256i)a,(__m256i)b); }
597 SI F max(F a, F b) { return _mm256_max_ps(a,b); }
598 SI I32 max(I32 a, I32 b) { return (I32)_mm256_max_epi32((__m256i)a,(__m256i)b); }
599 SI U32 max(U32 a, U32 b) { return (U32)_mm256_max_epu32((__m256i)a,(__m256i)b); }
600
601 SI F abs_ (F v) { return _mm256_and_ps(v, 0-v); }
602 SI I32 abs_ (I32 v) { return (I32)_mm256_abs_epi32((__m256i)v); }
603 SI F floor_(F v) { return _mm256_floor_ps(v); }
604 SI F ceil_(F v) { return _mm256_ceil_ps(v); }
605 SI F rcp_approx(F v) { return _mm256_rcp_ps (v); } // use rcp_fast instead
606 SI F rsqrt_approx(F v) { return _mm256_rsqrt_ps(v); }
607 SI F sqrt_ (F v) { return _mm256_sqrt_ps (v); }
608 SI F rcp_precise (F v) {
609 F e = rcp_approx(v);
610 return _mm256_fnmadd_ps(v, e, _mm256_set1_ps(2.0f)) * e;
611 }
612
613 SI I32 iround(F v) { return (I32)_mm256_cvtps_epi32(v); }
614 SI U32 round(F v) { return (U32)_mm256_cvtps_epi32(v); }
615 SI U16 pack(U32 v) {
616 return (U16)_mm_packus_epi32(_mm256_extractf128_si256((__m256i)v, 0),
617 _mm256_extractf128_si256((__m256i)v, 1));
618 }
619 SI U8 pack(U16 v) {
620 auto r = _mm_packus_epi16((__m128i)v,(__m128i)v);
621 return sk_unaligned_load<U8>(&r);
622 }
623
624 SI F if_then_else(I32 c, F t, F e) { return _mm256_blendv_ps(e, t, (__m256)c); }
625 SI I32 if_then_else(I32 c, I32 t, I32 e) {
626 return (I32)_mm256_blendv_ps((__m256)e, (__m256)t, (__m256)c);
627 }
628
629 // NOTE: This version of 'all' only works with mask values (true == all bits set)
630 SI bool any(I32 c) { return !_mm256_testz_si256((__m256i)c, _mm256_set1_epi32(-1)); }
631 SI bool all(I32 c) { return _mm256_testc_si256((__m256i)c, _mm256_set1_epi32(-1)); }
632
633 template <typename T>
634 SI V<T> gather(const T* p, U32 ix) {
635 return V<T>{ p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]],
636 p[ix[4]], p[ix[5]], p[ix[6]], p[ix[7]], };
637 }
638 SI F gather(const float* p, U32 ix) { return _mm256_i32gather_ps(p, (__m256i)ix, 4); }
639 SI U32 gather(const uint32_t* p, U32 ix) {
640 return (U32)_mm256_i32gather_epi32((const int*)p, (__m256i)ix, 4);
641 }
642 SI U64 gather(const uint64_t* p, U32 ix) {
643 __m256i parts[] = {
644 _mm256_i32gather_epi64(
645 (const long long int*)p, _mm256_extracti128_si256((__m256i)ix, 0), 8),
646 _mm256_i32gather_epi64(
647 (const long long int*)p, _mm256_extracti128_si256((__m256i)ix, 1), 8),
648 };
649 return sk_bit_cast<U64>(parts);
650 }
651 template <typename T>
652 SI V<T> gather_unaligned(const T* p, U32 ix) {
653 return gather(p, ix);
654 }
655
656 SI void scatter_masked(I32 src, int* dst, U32 ix, I32 mask) {
657 I32 before = gather(dst, ix);
658 I32 after = if_then_else(mask, src, before);
659 dst[ix[0]] = after[0];
660 dst[ix[1]] = after[1];
661 dst[ix[2]] = after[2];
662 dst[ix[3]] = after[3];
663 dst[ix[4]] = after[4];
664 dst[ix[5]] = after[5];
665 dst[ix[6]] = after[6];
666 dst[ix[7]] = after[7];
667 }
668
669 SI void load2(const uint16_t* ptr, U16* r, U16* g) {
670 __m128i _0123 = _mm_loadu_si128(((const __m128i*)ptr) + 0),
671 _4567 = _mm_loadu_si128(((const __m128i*)ptr) + 1);
672 *r = (U16)_mm_packs_epi32(_mm_srai_epi32(_mm_slli_epi32(_0123, 16), 16),
673 _mm_srai_epi32(_mm_slli_epi32(_4567, 16), 16));
674 *g = (U16)_mm_packs_epi32(_mm_srai_epi32(_0123, 16),
675 _mm_srai_epi32(_4567, 16));
676 }
677 SI void store2(uint16_t* ptr, U16 r, U16 g) {
678 auto _0123 = _mm_unpacklo_epi16((__m128i)r, (__m128i)g),
679 _4567 = _mm_unpackhi_epi16((__m128i)r, (__m128i)g);
680 _mm_storeu_si128((__m128i*)ptr + 0, _0123);
681 _mm_storeu_si128((__m128i*)ptr + 1, _4567);
682 }
683
684 SI void load4(const uint16_t* ptr, U16* r, U16* g, U16* b, U16* a) {
685 __m128i _01 = _mm_loadu_si128(((const __m128i*)ptr) + 0),
686 _23 = _mm_loadu_si128(((const __m128i*)ptr) + 1),
687 _45 = _mm_loadu_si128(((const __m128i*)ptr) + 2),
688 _67 = _mm_loadu_si128(((const __m128i*)ptr) + 3);
689
690 auto _02 = _mm_unpacklo_epi16(_01, _23), // r0 r2 g0 g2 b0 b2 a0 a2
691 _13 = _mm_unpackhi_epi16(_01, _23), // r1 r3 g1 g3 b1 b3 a1 a3
692 _46 = _mm_unpacklo_epi16(_45, _67),
693 _57 = _mm_unpackhi_epi16(_45, _67);
694
695 auto rg0123 = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3
696 ba0123 = _mm_unpackhi_epi16(_02, _13), // b0 b1 b2 b3 a0 a1 a2 a3
697 rg4567 = _mm_unpacklo_epi16(_46, _57),
698 ba4567 = _mm_unpackhi_epi16(_46, _57);
699
700 *r = (U16)_mm_unpacklo_epi64(rg0123, rg4567);
701 *g = (U16)_mm_unpackhi_epi64(rg0123, rg4567);
702 *b = (U16)_mm_unpacklo_epi64(ba0123, ba4567);
703 *a = (U16)_mm_unpackhi_epi64(ba0123, ba4567);
704 }
705 SI void store4(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
706 auto rg0123 = _mm_unpacklo_epi16((__m128i)r, (__m128i)g), // r0 g0 r1 g1 r2 g2 r3 g3
707 rg4567 = _mm_unpackhi_epi16((__m128i)r, (__m128i)g), // r4 g4 r5 g5 r6 g6 r7 g7
708 ba0123 = _mm_unpacklo_epi16((__m128i)b, (__m128i)a),
709 ba4567 = _mm_unpackhi_epi16((__m128i)b, (__m128i)a);
710
711 auto _01 = _mm_unpacklo_epi32(rg0123, ba0123),
712 _23 = _mm_unpackhi_epi32(rg0123, ba0123),
713 _45 = _mm_unpacklo_epi32(rg4567, ba4567),
714 _67 = _mm_unpackhi_epi32(rg4567, ba4567);
715
716 _mm_storeu_si128((__m128i*)ptr + 0, _01);
717 _mm_storeu_si128((__m128i*)ptr + 1, _23);
718 _mm_storeu_si128((__m128i*)ptr + 2, _45);
719 _mm_storeu_si128((__m128i*)ptr + 3, _67);
720 }
721
722 SI void load4(const float* ptr, F* r, F* g, F* b, F* a) {
723 F _04 = _mm256_castps128_ps256(_mm_loadu_ps(ptr+ 0)),
724 _15 = _mm256_castps128_ps256(_mm_loadu_ps(ptr+ 4)),
725 _26 = _mm256_castps128_ps256(_mm_loadu_ps(ptr+ 8)),
726 _37 = _mm256_castps128_ps256(_mm_loadu_ps(ptr+12));
727 _04 = _mm256_insertf128_ps(_04, _mm_loadu_ps(ptr+16), 1);
728 _15 = _mm256_insertf128_ps(_15, _mm_loadu_ps(ptr+20), 1);
729 _26 = _mm256_insertf128_ps(_26, _mm_loadu_ps(ptr+24), 1);
730 _37 = _mm256_insertf128_ps(_37, _mm_loadu_ps(ptr+28), 1);
731
732 F rg0145 = _mm256_unpacklo_ps(_04,_15), // r0 r1 g0 g1 | r4 r5 g4 g5
733 ba0145 = _mm256_unpackhi_ps(_04,_15),
734 rg2367 = _mm256_unpacklo_ps(_26,_37),
735 ba2367 = _mm256_unpackhi_ps(_26,_37);
736
737 *r = (F)_mm256_unpacklo_pd((__m256d)rg0145, (__m256d)rg2367);
738 *g = (F)_mm256_unpackhi_pd((__m256d)rg0145, (__m256d)rg2367);
739 *b = (F)_mm256_unpacklo_pd((__m256d)ba0145, (__m256d)ba2367);
740 *a = (F)_mm256_unpackhi_pd((__m256d)ba0145, (__m256d)ba2367);
741 }
742 SI void store4(float* ptr, F r, F g, F b, F a) {
743 F rg0145 = _mm256_unpacklo_ps(r, g), // r0 g0 r1 g1 | r4 g4 r5 g5
744 rg2367 = _mm256_unpackhi_ps(r, g), // r2 ... | r6 ...
745 ba0145 = _mm256_unpacklo_ps(b, a), // b0 a0 b1 a1 | b4 a4 b5 a5
746 ba2367 = _mm256_unpackhi_ps(b, a); // b2 ... | b6 ...
747
748 F _04 = (F)_mm256_unpacklo_pd((__m256d)rg0145, (__m256d)ba0145),// r0 g0 b0 a0 | r4 g4 b4 a4
749 _15 = (F)_mm256_unpackhi_pd((__m256d)rg0145, (__m256d)ba0145),// r1 ... | r5 ...
750 _26 = (F)_mm256_unpacklo_pd((__m256d)rg2367, (__m256d)ba2367),// r2 ... | r6 ...
751 _37 = (F)_mm256_unpackhi_pd((__m256d)rg2367, (__m256d)ba2367);// r3 ... | r7 ...
752
753 F _01 = _mm256_permute2f128_ps(_04, _15, 32), // 32 == 0010 0000 == lo, lo
754 _23 = _mm256_permute2f128_ps(_26, _37, 32),
755 _45 = _mm256_permute2f128_ps(_04, _15, 49), // 49 == 0011 0001 == hi, hi
756 _67 = _mm256_permute2f128_ps(_26, _37, 49);
757 _mm256_storeu_ps(ptr+ 0, _01);
758 _mm256_storeu_ps(ptr+ 8, _23);
759 _mm256_storeu_ps(ptr+16, _45);
760 _mm256_storeu_ps(ptr+24, _67);
761 }
762
763 #elif defined(SKRP_CPU_SSE2) || defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
764 template <typename T> using V = Vec<4, T>;
765 using F = V<float >;
766 using I32 = V< int32_t>;
767 using U64 = V<uint64_t>;
768 using U32 = V<uint32_t>;
769 using U16 = V<uint16_t>;
770 using U8 = V<uint8_t >;
771
772 SI F if_then_else(I32 c, F t, F e) {
773 return _mm_or_ps(_mm_and_ps((__m128)c, t), _mm_andnot_ps((__m128)c, e));
774 }
775 SI I32 if_then_else(I32 c, I32 t, I32 e) {
776 return (I32)_mm_or_ps(_mm_and_ps((__m128)c, (__m128)t),
777 _mm_andnot_ps((__m128)c, (__m128)e));
778 }
779
780 SI F min(F a, F b) { return _mm_min_ps(a,b); }
781 SI F max(F a, F b) { return _mm_max_ps(a,b); }
782 #if defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
783 SI I32 min(I32 a, I32 b) { return (I32)_mm_min_epi32((__m128i)a,(__m128i)b); }
784 SI U32 min(U32 a, U32 b) { return (U32)_mm_min_epu32((__m128i)a,(__m128i)b); }
785 SI I32 max(I32 a, I32 b) { return (I32)_mm_max_epi32((__m128i)a,(__m128i)b); }
786 SI U32 max(U32 a, U32 b) { return (U32)_mm_max_epu32((__m128i)a,(__m128i)b); }
787 #else
788 SI I32 min(I32 a, I32 b) { return if_then_else(a < b, a, b); }
789 SI I32 max(I32 a, I32 b) { return if_then_else(a > b, a, b); }
790 SI U32 min(U32 a, U32 b) {
791 return sk_bit_cast<U32>(if_then_else(a < b, sk_bit_cast<I32>(a), sk_bit_cast<I32>(b)));
792 }
793 SI U32 max(U32 a, U32 b) {
794 return sk_bit_cast<U32>(if_then_else(a > b, sk_bit_cast<I32>(a), sk_bit_cast<I32>(b)));
795 }
796 #endif
797
798 SI F mad(F f, F m, F a) { return a+f*m; }
799 SI F nmad(F f, F m, F a) { return a-f*m; }
800 SI F abs_(F v) { return _mm_and_ps(v, 0-v); }
801 #if defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
802 SI I32 abs_(I32 v) { return (I32)_mm_abs_epi32((__m128i)v); }
803 #else
804 SI I32 abs_(I32 v) { return max(v, -v); }
805 #endif
806 SI F rcp_approx(F v) { return _mm_rcp_ps (v); } // use rcp_fast instead
807 SI F rcp_precise (F v) { F e = rcp_approx(v); return e * (2.0f - v * e); }
808 SI F rsqrt_approx(F v) { return _mm_rsqrt_ps(v); }
809 SI F sqrt_(F v) { return _mm_sqrt_ps (v); }
810
811 SI I32 iround(F v) { return (I32)_mm_cvtps_epi32(v); }
812 SI U32 round(F v) { return (U32)_mm_cvtps_epi32(v); }
813
814 SI U16 pack(U32 v) {
815 #if defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
816 auto p = _mm_packus_epi32((__m128i)v,(__m128i)v);
817 #else
818 // Sign extend so that _mm_packs_epi32() does the pack we want.
819 auto p = _mm_srai_epi32(_mm_slli_epi32((__m128i)v, 16), 16);
820 p = _mm_packs_epi32(p,p);
821 #endif
822 return sk_unaligned_load<U16>(&p); // We have two copies. Return (the lower) one.
823 }
824 SI U8 pack(U16 v) {
825 auto r = widen_cast<__m128i>(v);
826 r = _mm_packus_epi16(r,r);
827 return sk_unaligned_load<U8>(&r);
828 }
829
830 // NOTE: This only checks the top bit of each lane, and is incorrect with non-mask values.
831 SI bool any(I32 c) { return _mm_movemask_ps(sk_bit_cast<F>(c)) != 0b0000; }
832 SI bool all(I32 c) { return _mm_movemask_ps(sk_bit_cast<F>(c)) == 0b1111; }
833
834 SI F floor_(F v) {
835 #if defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
836 return _mm_floor_ps(v);
837 #else
838 F roundtrip = _mm_cvtepi32_ps(_mm_cvttps_epi32(v));
839 return roundtrip - if_then_else(roundtrip > v, F() + 1, F() + 0);
840 #endif
841 }
842
843 SI F ceil_(F v) {
844 #if defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
845 return _mm_ceil_ps(v);
846 #else
847 F roundtrip = _mm_cvtepi32_ps(_mm_cvttps_epi32(v));
848 return roundtrip + if_then_else(roundtrip < v, F() + 1, F() + 0);
849 #endif
850 }
851
852 template <typename T>
853 SI V<T> gather(const T* p, U32 ix) {
854 return V<T>{p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]]};
855 }
856 template <typename T>
857 SI V<T> gather_unaligned(const T* p, U32 ix) {
858 return gather(p, ix);
859 }
860 SI void scatter_masked(I32 src, int* dst, U32 ix, I32 mask) {
861 I32 before = gather(dst, ix);
862 I32 after = if_then_else(mask, src, before);
863 dst[ix[0]] = after[0];
864 dst[ix[1]] = after[1];
865 dst[ix[2]] = after[2];
866 dst[ix[3]] = after[3];
867 }
868 SI void load2(const uint16_t* ptr, U16* r, U16* g) {
869 __m128i _01 = _mm_loadu_si128(((const __m128i*)ptr) + 0); // r0 g0 r1 g1 r2 g2 r3 g3
870 auto rg01_23 = _mm_shufflelo_epi16(_01, 0xD8); // r0 r1 g0 g1 r2 g2 r3 g3
871 auto rg = _mm_shufflehi_epi16(rg01_23, 0xD8); // r0 r1 g0 g1 r2 r3 g2 g3
872
873 auto R = _mm_shuffle_epi32(rg, 0x88); // r0 r1 r2 r3 r0 r1 r2 r3
874 auto G = _mm_shuffle_epi32(rg, 0xDD); // g0 g1 g2 g3 g0 g1 g2 g3
875 *r = sk_unaligned_load<U16>(&R);
876 *g = sk_unaligned_load<U16>(&G);
877 }
878 SI void store2(uint16_t* ptr, U16 r, U16 g) {
879 __m128i rg = _mm_unpacklo_epi16(widen_cast<__m128i>(r), widen_cast<__m128i>(g));
880 _mm_storeu_si128((__m128i*)ptr + 0, rg);
881 }
882
883 SI void load4(const uint16_t* ptr, U16* r, U16* g, U16* b, U16* a) {
884 __m128i _01 = _mm_loadu_si128(((const __m128i*)ptr) + 0), // r0 g0 b0 a0 r1 g1 b1 a1
885 _23 = _mm_loadu_si128(((const __m128i*)ptr) + 1); // r2 g2 b2 a2 r3 g3 b3 a3
886
887 auto _02 = _mm_unpacklo_epi16(_01, _23), // r0 r2 g0 g2 b0 b2 a0 a2
888 _13 = _mm_unpackhi_epi16(_01, _23); // r1 r3 g1 g3 b1 b3 a1 a3
889
890 auto rg = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3
891 ba = _mm_unpackhi_epi16(_02, _13); // b0 b1 b2 b3 a0 a1 a2 a3
892
893 *r = sk_unaligned_load<U16>((uint16_t*)&rg + 0);
894 *g = sk_unaligned_load<U16>((uint16_t*)&rg + 4);
895 *b = sk_unaligned_load<U16>((uint16_t*)&ba + 0);
896 *a = sk_unaligned_load<U16>((uint16_t*)&ba + 4);
897 }
898
899 SI void store4(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
900 auto rg = _mm_unpacklo_epi16(widen_cast<__m128i>(r), widen_cast<__m128i>(g)),
901 ba = _mm_unpacklo_epi16(widen_cast<__m128i>(b), widen_cast<__m128i>(a));
902
903 _mm_storeu_si128((__m128i*)ptr + 0, _mm_unpacklo_epi32(rg, ba));
904 _mm_storeu_si128((__m128i*)ptr + 1, _mm_unpackhi_epi32(rg, ba));
905 }
906
907 SI void load4(const float* ptr, F* r, F* g, F* b, F* a) {
908 F _0 = _mm_loadu_ps(ptr + 0),
909 _1 = _mm_loadu_ps(ptr + 4),
910 _2 = _mm_loadu_ps(ptr + 8),
911 _3 = _mm_loadu_ps(ptr +12);
912 _MM_TRANSPOSE4_PS(_0,_1,_2,_3);
913 *r = _0;
914 *g = _1;
915 *b = _2;
916 *a = _3;
917 }
918
919 SI void store4(float* ptr, F r, F g, F b, F a) {
920 _MM_TRANSPOSE4_PS(r,g,b,a);
921 _mm_storeu_ps(ptr + 0, r);
922 _mm_storeu_ps(ptr + 4, g);
923 _mm_storeu_ps(ptr + 8, b);
924 _mm_storeu_ps(ptr +12, a);
925 }
926
927 #elif defined(SKRP_CPU_LASX)
928 // These are __m256 and __m256i, but friendlier and strongly-typed.
929 template <typename T> using V = Vec<8, T>;
930 using F = V<float >;
931 using I32 = V<int32_t>;
932 using U64 = V<uint64_t>;
933 using U32 = V<uint32_t>;
934 using U16 = V<uint16_t>;
935 using U8 = V<uint8_t >;
936
937 SI __m128i emulate_lasx_d_xr2vr_l(__m256i a) {
938 v4i64 tmp = a;
939 v2i64 al = {tmp[0], tmp[1]};
940 return (__m128i)al;
941 }
942
943 SI __m128i emulate_lasx_d_xr2vr_h(__m256i a) {
944 v4i64 tmp = a;
945 v2i64 ah = {tmp[2], tmp[3]};
946 return (__m128i)ah;
947 }
948
949 SI F if_then_else(I32 c, F t, F e) {
950 return sk_bit_cast<Vec<8,float>>(__lasx_xvbitsel_v(sk_bit_cast<__m256i>(e),
951 sk_bit_cast<__m256i>(t),
952 sk_bit_cast<__m256i>(c)));
953 }
954
955 SI I32 if_then_else(I32 c, I32 t, I32 e) {
956 return sk_bit_cast<Vec<8,int32_t>>(__lasx_xvbitsel_v(sk_bit_cast<__m256i>(e),
957 sk_bit_cast<__m256i>(t),
958 sk_bit_cast<__m256i>(c)));
959 }
960
961 SI F min(F a, F b) { return __lasx_xvfmin_s(a,b); }
962 SI F max(F a, F b) { return __lasx_xvfmax_s(a,b); }
963 SI I32 min(I32 a, I32 b) { return __lasx_xvmin_w(a,b); }
964 SI U32 min(U32 a, U32 b) { return __lasx_xvmin_wu(a,b); }
965 SI I32 max(I32 a, I32 b) { return __lasx_xvmax_w(a,b); }
966 SI U32 max(U32 a, U32 b) { return __lasx_xvmax_wu(a,b); }
967
968 SI F mad(F f, F m, F a) { return __lasx_xvfmadd_s(f, m, a); }
969 SI F nmad(F f, F m, F a) { return __lasx_xvfmadd_s(-f, m, a); }
970 SI F abs_ (F v) { return (F)__lasx_xvand_v((I32)v, (I32)(0-v)); }
971 SI I32 abs_(I32 v) { return max(v, -v); }
972 SI F rcp_approx(F v) { return __lasx_xvfrecip_s(v); }
973 SI F rcp_precise (F v) { F e = rcp_approx(v); return e * nmad(v, e, F() + 2.0f); }
974 SI F rsqrt_approx (F v) { return __lasx_xvfrsqrt_s(v); }
975 SI F sqrt_(F v) { return __lasx_xvfsqrt_s(v); }
976
977 SI U32 iround(F v) {
978 F t = F() + 0.5f;
979 return __lasx_xvftintrz_w_s(v + t);
980 }
981
982 SI U32 round(F v) {
983 F t = F() + 0.5f;
984 return __lasx_xvftintrz_w_s(v + t);
985 }
986
987 SI U16 pack(U32 v) {
988 return __lsx_vpickev_h(__lsx_vsat_wu(emulate_lasx_d_xr2vr_h(v), 15),
989 __lsx_vsat_wu(emulate_lasx_d_xr2vr_l(v), 15));
990 }
991
992 SI U8 pack(U16 v) {
993 __m128i tmp = __lsx_vsat_hu(v, 7);
994 auto r = __lsx_vpickev_b(tmp, tmp);
995 return sk_unaligned_load<U8>(&r);
996 }
997
998 SI bool any(I32 c){
999 v8i32 retv = (v8i32)__lasx_xvmskltz_w(__lasx_xvslt_wu(__lasx_xvldi(0), c));
1000 return (retv[0] | retv[4]) != 0b0000;
1001 }
1002
1003 SI bool all(I32 c){
1004 v8i32 retv = (v8i32)__lasx_xvmskltz_w(__lasx_xvslt_wu(__lasx_xvldi(0), c));
1005 return (retv[0] & retv[4]) == 0b1111;
1006 }
1007
1008 SI F floor_(F v) {
1009 return __lasx_xvfrintrm_s(v);
1010 }
1011
1012 SI F ceil_(F v) {
1013 return __lasx_xvfrintrp_s(v);
1014 }
1015
1016 template <typename T>
1017 SI V<T> gather(const T* p, U32 ix) {
1018 return V<T>{ p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]],
1019 p[ix[4]], p[ix[5]], p[ix[6]], p[ix[7]], };
1020 }
1021 template <typename T>
1022 SI V<T> gather_unaligned(const T* p, U32 ix) {
1023 return gather(p, ix);
1024 }
1025
1026 template <typename V, typename S>
1027 SI void scatter_masked(V src, S* dst, U32 ix, I32 mask) {
1028 V before = gather(dst, ix);
1029 V after = if_then_else(mask, src, before);
1030 dst[ix[0]] = after[0];
1031 dst[ix[1]] = after[1];
1032 dst[ix[2]] = after[2];
1033 dst[ix[3]] = after[3];
1034 dst[ix[4]] = after[4];
1035 dst[ix[5]] = after[5];
1036 dst[ix[6]] = after[6];
1037 dst[ix[7]] = after[7];
1038 }
1039
1040 SI void load2(const uint16_t* ptr, U16* r, U16* g) {
1041 U16 _0123 = __lsx_vld(ptr, 0),
1042 _4567 = __lsx_vld(ptr, 16);
1043 *r = __lsx_vpickev_h(__lsx_vsat_w(__lsx_vsrai_w(__lsx_vslli_w(_4567, 16), 16), 15),
1044 __lsx_vsat_w(__lsx_vsrai_w(__lsx_vslli_w(_0123, 16), 16), 15));
1045 *g = __lsx_vpickev_h(__lsx_vsat_w(__lsx_vsrai_w(_4567, 16), 15),
1046 __lsx_vsat_w(__lsx_vsrai_w(_0123, 16), 15));
1047 }
1048 SI void store2(uint16_t* ptr, U16 r, U16 g) {
1049 auto _0123 = __lsx_vilvl_h(g, r),
1050 _4567 = __lsx_vilvh_h(g, r);
1051 __lsx_vst(_0123, ptr, 0);
1052 __lsx_vst(_4567, ptr, 16);
1053 }
1054
1055 SI void load4(const uint16_t* ptr, U16* r, U16* g, U16* b, U16* a) {
1056 __m128i _01 = __lsx_vld(ptr, 0),
1057 _23 = __lsx_vld(ptr, 16),
1058 _45 = __lsx_vld(ptr, 32),
1059 _67 = __lsx_vld(ptr, 48);
1060
1061 auto _02 = __lsx_vilvl_h(_23, _01), // r0 r2 g0 g2 b0 b2 a0 a2
1062 _13 = __lsx_vilvh_h(_23, _01), // r1 r3 g1 g3 b1 b3 a1 a3
1063 _46 = __lsx_vilvl_h(_67, _45),
1064 _57 = __lsx_vilvh_h(_67, _45);
1065
1066 auto rg0123 = __lsx_vilvl_h(_13, _02), // r0 r1 r2 r3 g0 g1 g2 g3
1067 ba0123 = __lsx_vilvh_h(_13, _02), // b0 b1 b2 b3 a0 a1 a2 a3
1068 rg4567 = __lsx_vilvl_h(_57, _46),
1069 ba4567 = __lsx_vilvh_h(_57, _46);
1070
1071 *r = __lsx_vilvl_d(rg4567, rg0123);
1072 *g = __lsx_vilvh_d(rg4567, rg0123);
1073 *b = __lsx_vilvl_d(ba4567, ba0123);
1074 *a = __lsx_vilvh_d(ba4567, ba0123);
1075 }
1076
1077 SI void store4(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
1078 auto rg0123 = __lsx_vilvl_h(g, r), // r0 g0 r1 g1 r2 g2 r3 g3
1079 rg4567 = __lsx_vilvh_h(g, r), // r4 g4 r5 g5 r6 g6 r7 g7
1080 ba0123 = __lsx_vilvl_h(a, b),
1081 ba4567 = __lsx_vilvh_h(a, b);
1082
1083 auto _01 =__lsx_vilvl_w(ba0123, rg0123),
1084 _23 =__lsx_vilvh_w(ba0123, rg0123),
1085 _45 =__lsx_vilvl_w(ba4567, rg4567),
1086 _67 =__lsx_vilvh_w(ba4567, rg4567);
1087
1088 __lsx_vst(_01, ptr, 0);
1089 __lsx_vst(_23, ptr, 16);
1090 __lsx_vst(_45, ptr, 32);
1091 __lsx_vst(_67, ptr, 48);
1092 }
1093
1094 SI void load4(const float* ptr, F* r, F* g, F* b, F* a) {
1095 F _04 = (F)__lasx_xvpermi_q(__lasx_xvld(ptr, 0), __lasx_xvld(ptr, 64), 0x02);
1096 F _15 = (F)__lasx_xvpermi_q(__lasx_xvld(ptr, 16), __lasx_xvld(ptr, 80), 0x02);
1097 F _26 = (F)__lasx_xvpermi_q(__lasx_xvld(ptr, 32), __lasx_xvld(ptr, 96), 0x02);
1098 F _37 = (F)__lasx_xvpermi_q(__lasx_xvld(ptr, 48), __lasx_xvld(ptr, 112), 0x02);
1099
1100 F rg0145 = (F)__lasx_xvilvl_w((__m256i)_15, (__m256i)_04), // r0 r1 g0 g1 | r4 r5 g4 g5
1101 ba0145 = (F)__lasx_xvilvh_w((__m256i)_15, (__m256i)_04),
1102 rg2367 = (F)__lasx_xvilvl_w((__m256i)_37, (__m256i)_26),
1103 ba2367 = (F)__lasx_xvilvh_w((__m256i)_37, (__m256i)_26);
1104
1105 *r = (F)__lasx_xvilvl_d((__m256i)rg2367, (__m256i)rg0145);
1106 *g = (F)__lasx_xvilvh_d((__m256i)rg2367, (__m256i)rg0145);
1107 *b = (F)__lasx_xvilvl_d((__m256i)ba2367, (__m256i)ba0145);
1108 *a = (F)__lasx_xvilvh_d((__m256i)ba2367, (__m256i)ba0145);
1109 }
1110 SI void store4(float* ptr, F r, F g, F b, F a) {
1111 F rg0145 = (F)__lasx_xvilvl_w((__m256i)g, (__m256i)r), // r0 g0 r1 g1 | r4 g4 r5 g5
1112 rg2367 = (F)__lasx_xvilvh_w((__m256i)g, (__m256i)r), // r2 ... | r6 ...
1113 ba0145 = (F)__lasx_xvilvl_w((__m256i)a, (__m256i)b), // b0 a0 b1 a1 | b4 a4 b5 a5
1114 ba2367 = (F)__lasx_xvilvh_w((__m256i)a, (__m256i)b); // b2 ... | b6 ...
1115
1116 F _04 = (F)__lasx_xvilvl_d((__m256i)ba0145, (__m256i)rg0145), // r0 g0 b0 a0 | r4 g4 b4 a4
1117 _15 = (F)__lasx_xvilvh_d((__m256i)ba0145, (__m256i)rg0145), // r1 ... | r5 ...
1118 _26 = (F)__lasx_xvilvl_d((__m256i)ba2367, (__m256i)rg2367), // r2 ... | r6 ...
1119 _37 = (F)__lasx_xvilvh_d((__m256i)ba2367, (__m256i)rg2367); // r3 ... | r7 ...
1120
1121 F _01 = (F)__lasx_xvpermi_q((__m256i)_04, (__m256i)_15, 0x02),
1122 _23 = (F)__lasx_xvpermi_q((__m256i)_26, (__m256i)_37, 0x02),
1123 _45 = (F)__lasx_xvpermi_q((__m256i)_04, (__m256i)_15, 0x13),
1124 _67 = (F)__lasx_xvpermi_q((__m256i)_26, (__m256i)_37, 0x13);
1125 __lasx_xvst(_01, ptr, 0);
1126 __lasx_xvst(_23, ptr, 32);
1127 __lasx_xvst(_45, ptr, 64);
1128 __lasx_xvst(_67, ptr, 96);
1129 }
1130
1131 #elif defined(SKRP_CPU_LSX)
1132 template <typename T> using V = Vec<4, T>;
1133 using F = V<float >;
1134 using I32 = V<int32_t >;
1135 using U64 = V<uint64_t>;
1136 using U32 = V<uint32_t>;
1137 using U16 = V<uint16_t>;
1138 using U8 = V<uint8_t >;
1139
1140 #define _LSX_TRANSPOSE4_S(row0, row1, row2, row3) \
1141 do { \
1142 __m128 __t0 = (__m128)__lsx_vilvl_w ((__m128i)row1, (__m128i)row0); \
1143 __m128 __t1 = (__m128)__lsx_vilvl_w ((__m128i)row3, (__m128i)row2); \
1144 __m128 __t2 = (__m128)__lsx_vilvh_w ((__m128i)row1, (__m128i)row0); \
1145 __m128 __t3 = (__m128)__lsx_vilvh_w ((__m128i)row3, (__m128i)row2); \
1146 (row0) = (__m128)__lsx_vilvl_d ((__m128i)__t1, (__m128i)__t0); \
1147 (row1) = (__m128)__lsx_vilvh_d ((__m128i)__t1, (__m128i)__t0); \
1148 (row2) = (__m128)__lsx_vilvl_d ((__m128i)__t3, (__m128i)__t2); \
1149 (row3) = (__m128)__lsx_vilvh_d ((__m128i)__t3, (__m128i)__t2); \
1150 } while (0)
1151
1152 SI F if_then_else(I32 c, F t, F e) {
1153 return sk_bit_cast<Vec<4,float>>(__lsx_vbitsel_v(sk_bit_cast<__m128i>(e),
1154 sk_bit_cast<__m128i>(t),
1155 sk_bit_cast<__m128i>(c)));
1156 }
1157
1158 SI I32 if_then_else(I32 c, I32 t, I32 e) {
1159 return sk_bit_cast<Vec<4,int32_t>>(__lsx_vbitsel_v(sk_bit_cast<__m128i>(e),
1160 sk_bit_cast<__m128i>(t),
1161 sk_bit_cast<__m128i>(c)));
1162 }
1163
1164 SI F min(F a, F b) { return __lsx_vfmin_s(a,b); }
1165 SI F max(F a, F b) { return __lsx_vfmax_s(a,b); }
1166 SI I32 min(I32 a, I32 b) { return __lsx_vmin_w(a,b); }
1167 SI U32 min(U32 a, U32 b) { return __lsx_vmin_wu(a,b); }
1168 SI I32 max(I32 a, I32 b) { return __lsx_vmax_w(a,b); }
1169 SI U32 max(U32 a, U32 b) { return __lsx_vmax_wu(a,b); }
1170
1171 SI F mad(F f, F m, F a) { return __lsx_vfmadd_s(f, m, a); }
1172 SI F nmad(F f, F m, F a) { return __lsx_vfmadd_s(-f, m, a); }
1173 SI F abs_(F v) { return (F)__lsx_vand_v((I32)v, (I32)(0-v)); }
1174 SI I32 abs_(I32 v) { return max(v, -v); }
1175 SI F rcp_approx (F v) { return __lsx_vfrecip_s(v); }
1176 SI F rcp_precise (F v) { F e = rcp_approx(v); return e * nmad(v, e, F() + 2.0f); }
1177 SI F rsqrt_approx (F v) { return __lsx_vfrsqrt_s(v); }
1178 SI F sqrt_(F v) { return __lsx_vfsqrt_s (v); }
1179
1180 SI U32 iround(F v) {
1181 F t = F() + 0.5f;
1182 return __lsx_vftintrz_w_s(v + t); }
1183
1184 SI U32 round(F v) {
1185 F t = F() + 0.5f;
1186 return __lsx_vftintrz_w_s(v + t); }
1187
1188 SI U16 pack(U32 v) {
1189 __m128i tmp = __lsx_vsat_wu(v, 15);
1190 auto p = __lsx_vpickev_h(tmp, tmp);
1191 return sk_unaligned_load<U16>(&p); // We have two copies. Return (the lower) one.
1192 }
1193
1194 SI U8 pack(U16 v) {
1195 auto r = widen_cast<__m128i>(v);
1196 __m128i tmp = __lsx_vsat_hu(r, 7);
1197 r = __lsx_vpickev_b(tmp, tmp);
1198 return sk_unaligned_load<U8>(&r);
1199 }
1200
1201 SI bool any(I32 c){
1202 v4i32 retv = (v4i32)__lsx_vmskltz_w(__lsx_vslt_wu(__lsx_vldi(0), c));
1203 return retv[0] != 0b0000;
1204 }
1205
1206 SI bool all(I32 c){
1207 v4i32 retv = (v4i32)__lsx_vmskltz_w(__lsx_vslt_wu(__lsx_vldi(0), c));
1208 return retv[0] == 0b1111;
1209 }
1210
1211 SI F floor_(F v) {
1212 return __lsx_vfrintrm_s(v);
1213 }
1214
1215 SI F ceil_(F v) {
1216 return __lsx_vfrintrp_s(v);
1217 }
1218
1219 template <typename T>
1220 SI V<T> gather(const T* p, U32 ix) {
1221 return V<T>{p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]]};
1222 }
1223 // Using 'int*' prevents data from passing through floating-point registers.
1224 SI F gather(const int* p, int ix0, int ix1, int ix2, int ix3) {
1225 F ret = {0.0};
1226 ret = (F)__lsx_vinsgr2vr_w(ret, p[ix0], 0);
1227 ret = (F)__lsx_vinsgr2vr_w(ret, p[ix1], 1);
1228 ret = (F)__lsx_vinsgr2vr_w(ret, p[ix2], 2);
1229 ret = (F)__lsx_vinsgr2vr_w(ret, p[ix3], 3);
1230 return ret;
1231 }
1232 template <typename T>
1233 SI V<T> gather_unaligned(const T* p, U32 ix) {
1234 return gather(p, ix);
1235 }
1236
1237 template <typename V, typename S>
1238 SI void scatter_masked(V src, S* dst, U32 ix, I32 mask) {
1239 V before = gather(dst, ix);
1240 V after = if_then_else(mask, src, before);
1241 dst[ix[0]] = after[0];
1242 dst[ix[1]] = after[1];
1243 dst[ix[2]] = after[2];
1244 dst[ix[3]] = after[3];
1245 }
1246
1247 SI void load2(const uint16_t* ptr, U16* r, U16* g) {
1248 __m128i _01 = __lsx_vld(ptr, 0); // r0 g0 r1 g1 r2 g2 r3 g3
1249 auto rg = __lsx_vshuf4i_h(_01, 0xD8); // r0 r1 g0 g1 r2 r3 g2 g3
1250
1251 auto R = __lsx_vshuf4i_w(rg, 0x88); // r0 r1 r2 r3 r0 r1 r2 r3
1252 auto G = __lsx_vshuf4i_w(rg, 0xDD); // g0 g1 g2 g3 g0 g1 g2 g3
1253 *r = sk_unaligned_load<U16>(&R);
1254 *g = sk_unaligned_load<U16>(&G);
1255 }
1256
1257 SI void store2(uint16_t* ptr, U16 r, U16 g) {
1258 U32 rg = __lsx_vilvl_h(widen_cast<__m128i>(g), widen_cast<__m128i>(r));
1259 __lsx_vst(rg, ptr, 0);
1260 }
1261
1262 SI void load4(const uint16_t* ptr, U16* r, U16* g, U16* b, U16* a) {
1263 __m128i _01 = __lsx_vld(ptr, 0), // r0 g0 b0 a0 r1 g1 b1 a1
1264 _23 = __lsx_vld(ptr, 16); // r2 g2 b2 a2 r3 g3 b3 a3
1265
1266 auto _02 = __lsx_vilvl_h(_23, _01), // r0 r2 g0 g2 b0 b2 a0 a2
1267 _13 = __lsx_vilvh_h(_23, _01); // r1 r3 g1 g3 b1 b3 a1 a3
1268
1269 auto rg = __lsx_vilvl_h(_13, _02), // r0 r1 r2 r3 g0 g1 g2 g3
1270 ba = __lsx_vilvh_h(_13, _02); // b0 b1 b2 b3 a0 a1 a2 a3
1271
1272 *r = sk_unaligned_load<U16>((uint16_t*)&rg + 0);
1273 *g = sk_unaligned_load<U16>((uint16_t*)&rg + 4);
1274 *b = sk_unaligned_load<U16>((uint16_t*)&ba + 0);
1275 *a = sk_unaligned_load<U16>((uint16_t*)&ba + 4);
1276 }
1277
1278 SI void store4(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
1279 auto rg = __lsx_vilvl_h(widen_cast<__m128i>(g), widen_cast<__m128i>(r)),
1280 ba = __lsx_vilvl_h(widen_cast<__m128i>(a), widen_cast<__m128i>(b));
1281
1282 __lsx_vst(__lsx_vilvl_w(ba, rg), ptr, 0);
1283 __lsx_vst(__lsx_vilvh_w(ba, rg), ptr, 16);
1284 }
1285
1286 SI void load4(const float* ptr, F* r, F* g, F* b, F* a) {
1287 F _0 = (F)__lsx_vld(ptr, 0),
1288 _1 = (F)__lsx_vld(ptr, 16),
1289 _2 = (F)__lsx_vld(ptr, 32),
1290 _3 = (F)__lsx_vld(ptr, 48);
1291 _LSX_TRANSPOSE4_S(_0,_1,_2,_3);
1292 *r = _0;
1293 *g = _1;
1294 *b = _2;
1295 *a = _3;
1296 }
1297
1298 SI void store4(float* ptr, F r, F g, F b, F a) {
1299 _LSX_TRANSPOSE4_S(r,g,b,a);
1300 __lsx_vst(r, ptr, 0);
1301 __lsx_vst(g, ptr, 16);
1302 __lsx_vst(b, ptr, 32);
1303 __lsx_vst(a, ptr, 48);
1304 }
1305
1306 #endif
1307
1308 // Helpers to do scalar -> vector promotion on GCC (clang does this automatically)
1309 // We need to subtract (not add) zero to keep float conversion zero-cost. See:
1310 // https://stackoverflow.com/q/48255293
1311 //
1312 // The GCC implementation should be usable everywhere, but Mac clang (only) complains that the
1313 // expressions make these functions not constexpr.
1314 //
1315 // Further: We can't use the subtract-zero version in scalar mode. There, the subtraction will
1316 // really happen (at least at low optimization levels), which can alter the bit pattern of NaNs.
1317 // Because F_() is used when copying uniforms (even integer uniforms), this can corrupt values.
1318 // The vector subtraction of zero doesn't appear to ever alter NaN bit patterns.
1319 #if defined(__clang__) || defined(SKRP_CPU_SCALAR)
1320 SI constexpr F F_(float x) { return x; }
1321 SI constexpr I32 I32_(int32_t x) { return x; }
1322 SI constexpr U32 U32_(uint32_t x) { return x; }
1323 #else
1324 SI constexpr F F_(float x) { return x - F(); }
1325 SI constexpr I32 I32_(int32_t x) { return x + I32(); }
1326 SI constexpr U32 U32_(uint32_t x) { return x + U32(); }
1327 #endif
1328
1329 // Extremely helpful literals:
1330 static constexpr F F0 = F_(0.0f),
1331 F1 = F_(1.0f);
1332
1333 #if !defined(SKRP_CPU_SCALAR)
1334 SI F min(F a, float b) { return min(a, F_(b)); }
1335 SI F min(float a, F b) { return min(F_(a), b); }
1336 SI F max(F a, float b) { return max(a, F_(b)); }
1337 SI F max(float a, F b) { return max(F_(a), b); }
1338
1339 SI F mad(F f, F m, float a) { return mad(f, m, F_(a)); }
1340 SI F mad(F f, float m, F a) { return mad(f, F_(m), a); }
1341 SI F mad(F f, float m, float a) { return mad(f, F_(m), F_(a)); }
1342 SI F mad(float f, F m, F a) { return mad(F_(f), m, a); }
1343 SI F mad(float f, F m, float a) { return mad(F_(f), m, F_(a)); }
1344 SI F mad(float f, float m, F a) { return mad(F_(f), F_(m), a); }
1345
1346 SI F nmad(F f, F m, float a) { return nmad(f, m, F_(a)); }
1347 SI F nmad(F f, float m, F a) { return nmad(f, F_(m), a); }
1348 SI F nmad(F f, float m, float a) { return nmad(f, F_(m), F_(a)); }
1349 SI F nmad(float f, F m, F a) { return nmad(F_(f), m, a); }
1350 SI F nmad(float f, F m, float a) { return nmad(F_(f), m, F_(a)); }
1351 SI F nmad(float f, float m, F a) { return nmad(F_(f), F_(m), a); }
1352 #endif
1353
1354 // We need to be a careful with casts.
1355 // (F)x means cast x to float in the portable path, but bit_cast x to float in the others.
1356 // These named casts and bit_cast() are always what they seem to be.
1357 #if defined(SKRP_CPU_SCALAR)
1358 SI F cast (U32 v) { return (F)v; }
1359 SI F cast64(U64 v) { return (F)v; }
1360 SI U32 trunc_(F v) { return (U32)v; }
1361 SI U32 expand(U16 v) { return (U32)v; }
1362 SI U32 expand(U8 v) { return (U32)v; }
1363 #else
1364 SI F cast (U32 v) { return __builtin_convertvector((I32)v, F); }
1365 SI F cast64(U64 v) { return __builtin_convertvector( v, F); }
1366 SI U32 trunc_(F v) { return (U32)__builtin_convertvector( v, I32); }
1367 SI U32 expand(U16 v) { return __builtin_convertvector( v, U32); }
1368 SI U32 expand(U8 v) { return __builtin_convertvector( v, U32); }
1369 #endif
1370
1371 #if !defined(SKRP_CPU_SCALAR)
1372 SI F if_then_else(I32 c, F t, float e) { return if_then_else(c, t , F_(e)); }
1373 SI F if_then_else(I32 c, float t, F e) { return if_then_else(c, F_(t), e ); }
1374 SI F if_then_else(I32 c, float t, float e) { return if_then_else(c, F_(t), F_(e)); }
1375 #endif
1376
1377 SI F fract(F v) { return v - floor_(v); }
1378
1379 // See http://www.machinedlearnings.com/2011/06/fast-approximate-logarithm-exponential.html
1380 SI F approx_log2(F x) {
1381 // e - 127 is a fair approximation of log2(x) in its own right...
1382 F e = cast(sk_bit_cast<U32>(x)) * (1.0f / (1<<23));
1383
1384 // ... but using the mantissa to refine its error is _much_ better.
1385 F m = sk_bit_cast<F>((sk_bit_cast<U32>(x) & 0x007fffff) | 0x3f000000);
1386
1387 return nmad(m, 1.498030302f, e - 124.225514990f) - 1.725879990f / (0.3520887068f + m);
1388 }
1389
1390 SI F approx_log(F x) {
1391 const float ln2 = 0.69314718f;
1392 return ln2 * approx_log2(x);
1393 }
1394
1395 SI F approx_pow2(F x) {
1396 constexpr float kInfinityBits = 0x7f800000;
1397
1398 F f = fract(x);
1399 F approx = nmad(f, 1.490129070f, x + 121.274057500f);
1400 approx += 27.728023300f / (4.84252568f - f);
1401 approx *= 1.0f * (1<<23);
1402 approx = min(max(approx, F0), F_(kInfinityBits)); // guard against underflow/overflow
1403
1404 return sk_bit_cast<F>(round(approx));
1405 }
1406
1407 SI F approx_exp(F x) {
1408 const float log2_e = 1.4426950408889634074f;
1409 return approx_pow2(log2_e * x);
1410 }
1411
1412 SI F approx_powf(F x, F y) {
1413 return if_then_else((x == 0)|(x == 1), x
1414 , approx_pow2(approx_log2(x) * y));
1415 }
1416 #if !defined(SKRP_CPU_SCALAR)
1417 SI F approx_powf(F x, float y) { return approx_powf(x, F_(y)); }
1418 #endif
1419
1420 SI F from_half(U16 h) {
1421 #if defined(SKRP_CPU_NEON) && defined(SK_CPU_ARM64)
1422 return vcvt_f32_f16((float16x4_t)h);
1423
1424 #elif defined(SKRP_CPU_SKX)
1425 return _mm512_cvtph_ps((__m256i)h);
1426
1427 #elif defined(SKRP_CPU_HSW)
1428 return _mm256_cvtph_ps((__m128i)h);
1429
1430 #else
1431 // Remember, a half is 1-5-10 (sign-exponent-mantissa) with 15 exponent bias.
1432 U32 sem = expand(h),
1433 s = sem & 0x8000,
1434 em = sem ^ s;
1435
1436 // Convert to 1-8-23 float with 127 bias, flushing denorm halfs (including zero) to zero.
1437 auto denorm = (I32)em < 0x0400; // I32 comparison is often quicker, and always safe here.
1438 return if_then_else(denorm, F0
1439 , sk_bit_cast<F>( (s<<16) + (em<<13) + ((127-15)<<23) ));
1440 #endif
1441 }
1442
1443 SI U16 to_half(F f) {
1444 #if defined(SKRP_CPU_NEON) && defined(SK_CPU_ARM64)
1445 return (U16)vcvt_f16_f32(f);
1446
1447 #elif defined(SKRP_CPU_SKX)
1448 return (U16)_mm512_cvtps_ph(f, _MM_FROUND_CUR_DIRECTION);
1449
1450 #elif defined(SKRP_CPU_HSW)
1451 return (U16)_mm256_cvtps_ph(f, _MM_FROUND_CUR_DIRECTION);
1452
1453 #else
1454 // Remember, a float is 1-8-23 (sign-exponent-mantissa) with 127 exponent bias.
1455 U32 sem = sk_bit_cast<U32>(f),
1456 s = sem & 0x80000000,
1457 em = sem ^ s;
1458
1459 // Convert to 1-5-10 half with 15 bias, flushing denorm halfs (including zero) to zero.
1460 auto denorm = (I32)em < 0x38800000; // I32 comparison is often quicker, and always safe here.
1461 return pack((U32)if_then_else(denorm, I32_(0)
1462 , (I32)((s>>16) + (em>>13) - ((127-15)<<10))));
1463 #endif
1464 }
1465
1466 static void patch_memory_contexts(SkSpan<SkRasterPipelineContexts::MemoryCtxPatch> memoryCtxPatches,
1467 const size_t dx,
1468 const size_t dy,
1469 size_t tail) {
1470 for (SkRasterPipelineContexts::MemoryCtxPatch& patch : memoryCtxPatches) {
1471 SkRasterPipelineContexts::MemoryCtx* ctx = patch.info.context;
1472
1473 const ptrdiff_t offset = patch.info.bytesPerPixel * (dy * ctx->stride + dx);
1474 if (patch.info.load) {
1475 void* ctxData = SkTAddOffset<void>(ctx->pixels, offset);
1476 memcpy(patch.scratch, ctxData, patch.info.bytesPerPixel * tail);
1477 }
1478
1479 SkASSERT(patch.backup == nullptr);
1480 void* scratchFakeBase = SkTAddOffset<void>(patch.scratch, -offset);
1481 patch.backup = ctx->pixels;
1482 ctx->pixels = scratchFakeBase;
1483 }
1484 }
1485
1486 static void restore_memory_contexts(
1487 SkSpan<SkRasterPipelineContexts::MemoryCtxPatch> memoryCtxPatches,
1488 const size_t dx,
1489 const size_t dy,
1490 size_t tail) {
1491 for (SkRasterPipelineContexts::MemoryCtxPatch& patch : memoryCtxPatches) {
1492 SkRasterPipelineContexts::MemoryCtx* ctx = patch.info.context;
1493
1494 SkASSERT(patch.backup != nullptr);
1495 ctx->pixels = patch.backup;
1496 patch.backup = nullptr;
1497
1498 const ptrdiff_t offset = patch.info.bytesPerPixel * (dy * ctx->stride + dx);
1499 if (patch.info.store) {
1500 void* ctxData = SkTAddOffset<void>(ctx->pixels, offset);
1501 memcpy(ctxData, patch.scratch, patch.info.bytesPerPixel * tail);
1502 }
1503 }
1504 }
1505
1506 #if defined(SKRP_CPU_SCALAR) || defined(SKRP_CPU_SSE2)
1507 // In scalar and SSE2 mode, we always use precise math so we can have more predictable results.
1508 // Chrome will use the SSE2 implementation when --disable-skia-runtime-opts is set. (b/40042946)
1509 SI F rcp_fast(F v) { return rcp_precise(v); }
1510 SI F rsqrt(F v) { return rcp_precise(sqrt_(v)); }
1511 #else
1512 SI F rcp_fast(F v) { return rcp_approx(v); }
1513 SI F rsqrt(F v) { return rsqrt_approx(v); }
1514 #endif
1515
1516 // Our fundamental vector depth is our pixel stride.
1517 static constexpr size_t N = sizeof(F) / sizeof(float);
1518
1519 // We're finally going to get to what a Stage function looks like!
1520
1521 // Any custom ABI to use for all (non-externally-facing) stage functions?
1522 // Also decide here whether to use narrow (compromise) or wide (ideal) stages.
1523 #if defined(SK_CPU_ARM32) && defined(SKRP_CPU_NEON)
1524 // This lets us pass vectors more efficiently on 32-bit ARM.
1525 // We can still only pass 16 floats, so best as 4x {r,g,b,a}.
1526 #define ABI __attribute__((pcs("aapcs-vfp")))
1527 #define SKRP_NARROW_STAGES 1
1528 #elif defined(_MSC_VER)
1529 // Even if not vectorized, this lets us pass {r,g,b,a} as registers,
1530 // instead of {b,a} on the stack. Narrow stages work best for __vectorcall.
1531 #define ABI __vectorcall
1532 #define SKRP_NARROW_STAGES 1
1533 #elif defined(__x86_64__) || defined(SK_CPU_ARM64) || defined(SK_CPU_LOONGARCH)
1534 // These platforms are ideal for wider stages, and their default ABI is ideal.
1535 #define ABI
1536 #define SKRP_NARROW_STAGES 0
1537 #else
1538 // 32-bit or unknown... shunt them down the narrow path.
1539 // Odds are these have few registers and are better off there.
1540 #define ABI
1541 #define SKRP_NARROW_STAGES 1
1542 #endif
1543
1544 #if SKRP_NARROW_STAGES
1545 struct Params {
1546 size_t dx, dy;
1547 std::byte* base;
1548 F dr,dg,db,da;
1549 };
1550 using Stage = void(ABI*)(Params*, SkRasterPipelineStage* program, F r, F g, F b, F a);
1551 #else
1552 using Stage = void(ABI*)(SkRasterPipelineStage* program, const size_t dx, const size_t dy,
1553 std::byte* base, F,F,F,F, F,F,F,F);
1554 #endif
1555
1556 static void start_pipeline(size_t dx, size_t dy,
1557 size_t xlimit, size_t ylimit,
1558 SkRasterPipelineStage* program,
1559 SkSpan<SkRasterPipelineContexts::MemoryCtxPatch> memoryCtxPatches,
1560 uint8_t* tailPointer) {
1561 uint8_t unreferencedTail;
1562 if (!tailPointer) {
1563 tailPointer = &unreferencedTail;
1564 }
1565 auto start = (Stage)program->fn;
1566 const size_t x0 = dx;
1567 std::byte* const base = nullptr;
1568 for (; dy < ylimit; dy++) {
1569 #if SKRP_NARROW_STAGES
1570 Params params = { x0,dy,base, F0,F0,F0,F0 };
1571 while (params.dx + N <= xlimit) {
1572 start(&params,program, F0,F0,F0,F0);
1573 params.dx += N;
1574 }
1575 if (size_t tail = xlimit - params.dx) {
1576 *tailPointer = tail;
1577 patch_memory_contexts(memoryCtxPatches, params.dx, dy, tail);
1578 start(&params,program, F0,F0,F0,F0);
1579 restore_memory_contexts(memoryCtxPatches, params.dx, dy, tail);
1580 *tailPointer = 0xFF;
1581 }
1582 #else
1583 dx = x0;
1584 while (dx + N <= xlimit) {
1585 start(program,dx,dy,base, F0,F0,F0,F0, F0,F0,F0,F0);
1586 dx += N;
1587 }
1588 if (size_t tail = xlimit - dx) {
1589 *tailPointer = tail;
1590 patch_memory_contexts(memoryCtxPatches, dx, dy, tail);
1591 start(program,dx,dy,base, F0,F0,F0,F0, F0,F0,F0,F0);
1592 restore_memory_contexts(memoryCtxPatches, dx, dy, tail);
1593 *tailPointer = 0xFF;
1594 }
1595 #endif
1596 }
1597 }
1598
1599 #if SK_HAS_MUSTTAIL
1600 #define SKRP_MUSTTAIL [[clang::musttail]]
1601 #else
1602 #define SKRP_MUSTTAIL
1603 #endif
1604
1605 #if SKRP_NARROW_STAGES
1606 #define DECLARE_HIGHP_STAGE(name, ARG, STAGE_RET, INC, OFFSET, MUSTTAIL) \
1607 SI STAGE_RET name##_k(ARG, const size_t dx, const size_t dy, std::byte*& base, \
1608 F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da); \
1609 static void ABI name(Params* params, SkRasterPipelineStage* program, \
1610 F r, F g, F b, F a) { \
1611 OFFSET name##_k(Ctx{program}, params->dx,params->dy,params->base, \
1612 r,g,b,a, params->dr, params->dg, params->db, params->da); \
1613 INC; \
1614 auto fn = (Stage)program->fn; \
1615 MUSTTAIL return fn(params, program, r,g,b,a); \
1616 } \
1617 SI STAGE_RET name##_k(ARG, const size_t dx, const size_t dy, std::byte*& base, \
1618 F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da)
1619 #else
1620 #define DECLARE_HIGHP_STAGE(name, ARG, STAGE_RET, INC, OFFSET, MUSTTAIL) \
1621 SI STAGE_RET name##_k(ARG, const size_t dx, const size_t dy, std::byte*& base, \
1622 F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da); \
1623 static void ABI name(SkRasterPipelineStage* program, const size_t dx, const size_t dy, \
1624 std::byte* base, F r, F g, F b, F a, F dr, F dg, F db, F da) { \
1625 OFFSET name##_k(Ctx{program}, dx,dy,base, r,g,b,a, dr,dg,db,da); \
1626 INC; \
1627 auto fn = (Stage)program->fn; \
1628 MUSTTAIL return fn(program, dx,dy,base, r,g,b,a, dr,dg,db,da); \
1629 } \
1630 SI STAGE_RET name##_k(ARG, const size_t dx, const size_t dy, std::byte*& base, \
1631 F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da)
1632 #endif
1633
1634 // A typical stage returns void, always increments the program counter by 1, and lets the optimizer
1635 // decide whether or not tail-calling is appropriate.
1636 #define HIGHP_STAGE(name, arg) \
1637 DECLARE_HIGHP_STAGE(name, arg, void, ++program, /*no offset*/, /*no musttail*/)
1638
1639 // A tail stage returns void, always increments the program counter by 1, and uses tail-calling.
1640 // Tail-calling is necessary in SkSL-generated programs, which can be thousands of ops long, and
1641 // could overflow the stack (particularly in debug).
1642 #define HIGHP_TAIL_STAGE(name, arg) \
1643 DECLARE_HIGHP_STAGE(name, arg, void, ++program, /*no offset*/, SKRP_MUSTTAIL)
1644
1645 // A branch stage returns an integer, which is added directly to the program counter, and tailcalls.
1646 #define HIGHP_BRANCH_STAGE(name, arg) \
1647 DECLARE_HIGHP_STAGE(name, arg, int, /*no increment*/, program +=, SKRP_MUSTTAIL)
1648
1649 // just_return() is a simple no-op stage that only exists to end the chain,
1650 // returning back up to start_pipeline(), and from there to the caller.
1651 #if SKRP_NARROW_STAGES
1652 static void ABI just_return(Params*, SkRasterPipelineStage*, F,F,F,F) {}
1653 #else
1654 static void ABI just_return(SkRasterPipelineStage*, size_t,size_t, std::byte*,
1655 F,F,F,F, F,F,F,F) {}
1656 #endif
1657
1658 // Note that in release builds, most stages consume no stack (thanks to tail call optimization).
1659 // However: certain builds (especially with non-clang compilers) may fail to optimize tail
1660 // calls, resulting in actual stack frames being generated.
1661 //
1662 // stack_checkpoint() and stack_rewind() are special stages that can be used to manage stack growth.
1663 // If a pipeline contains a stack_checkpoint, followed by any number of stack_rewind (at any point),
1664 // the C++ stack will be reset to the state it was at when the stack_checkpoint was initially hit.
1665 //
1666 // All instances of stack_rewind (as well as the one instance of stack_checkpoint near the start of
1667 // a pipeline) share a single context (of type SkRasterPipelineContexts::RewindCtx). That context holds the
1668 // full state of the mutable registers that are normally passed to the next stage in the program.
1669 //
1670 // stack_rewind is the only stage other than just_return that actually returns (rather than jumping
1671 // to the next stage in the program). Before it does so, it stashes all of the registers in the
1672 // context. This includes the updated `program` pointer. Unlike stages that tail call exactly once,
1673 // stack_checkpoint calls the next stage in the program repeatedly, as long as the `program` in the
1674 // context is overwritten (i.e., as long as a stack_rewind was the reason the pipeline returned,
1675 // rather than a just_return).
1676 //
1677 // Normally, just_return is the only stage that returns, and no other stage does anything after a
1678 // subsequent (called) stage returns, so the stack just unwinds all the way to start_pipeline.
1679 // With stack_checkpoint on the stack, any stack_rewind stages will return all the way up to the
1680 // stack_checkpoint. That grabs the values that would have been passed to the next stage (from the
1681 // context), and continues the linear execution of stages, but has reclaimed all of the stack frames
1682 // pushed before the stack_rewind before doing so.
1683 #if SKRP_NARROW_STAGES
1684 static void ABI stack_checkpoint(Params* params, SkRasterPipelineStage* program,
1685 F r, F g, F b, F a) {
1686 SkRasterPipelineContexts::RewindCtx* ctx = Ctx{program};
1687 while (program) {
1688 auto next = (Stage)(++program)->fn;
1689
1690 ctx->stage = nullptr;
1691 next(params, program, r, g, b, a);
1692 program = ctx->stage;
1693
1694 if (program) {
1695 r = sk_unaligned_load<F>(ctx->r );
1696 g = sk_unaligned_load<F>(ctx->g );
1697 b = sk_unaligned_load<F>(ctx->b );
1698 a = sk_unaligned_load<F>(ctx->a );
1699 params->dr = sk_unaligned_load<F>(ctx->dr);
1700 params->dg = sk_unaligned_load<F>(ctx->dg);
1701 params->db = sk_unaligned_load<F>(ctx->db);
1702 params->da = sk_unaligned_load<F>(ctx->da);
1703 params->base = ctx->base;
1704 }
1705 }
1706 }
1707 static void ABI stack_rewind(Params* params, SkRasterPipelineStage* program,
1708 F r, F g, F b, F a) {
1709 SkRasterPipelineContexts::RewindCtx* ctx = Ctx{program};
1710 sk_unaligned_store(ctx->r , r );
1711 sk_unaligned_store(ctx->g , g );
1712 sk_unaligned_store(ctx->b , b );
1713 sk_unaligned_store(ctx->a , a );
1714 sk_unaligned_store(ctx->dr, params->dr);
1715 sk_unaligned_store(ctx->dg, params->dg);
1716 sk_unaligned_store(ctx->db, params->db);
1717 sk_unaligned_store(ctx->da, params->da);
1718 ctx->base = params->base;
1719 ctx->stage = program;
1720 }
1721 #else
1722 static void ABI stack_checkpoint(SkRasterPipelineStage* program,
1723 const size_t dx, const size_t dy, std::byte* base,
1724 F r, F g, F b, F a, F dr, F dg, F db, F da) {
1725 SkRasterPipelineContexts::RewindCtx* ctx = Ctx{program};
1726 while (program) {
1727 auto next = (Stage)(++program)->fn;
1728
1729 ctx->stage = nullptr;
1730 next(program, dx, dy, base, r, g, b, a, dr, dg, db, da);
1731 program = ctx->stage;
1732
1733 if (program) {
1734 r = sk_unaligned_load<F>(ctx->r );
1735 g = sk_unaligned_load<F>(ctx->g );
1736 b = sk_unaligned_load<F>(ctx->b );
1737 a = sk_unaligned_load<F>(ctx->a );
1738 dr = sk_unaligned_load<F>(ctx->dr);
1739 dg = sk_unaligned_load<F>(ctx->dg);
1740 db = sk_unaligned_load<F>(ctx->db);
1741 da = sk_unaligned_load<F>(ctx->da);
1742 base = ctx->base;
1743 }
1744 }
1745 }
1746 static void ABI stack_rewind(SkRasterPipelineStage* program,
1747 const size_t dx, const size_t dy, std::byte* base,
1748 F r, F g, F b, F a, F dr, F dg, F db, F da) {
1749 SkRasterPipelineContexts::RewindCtx* ctx = Ctx{program};
1750 sk_unaligned_store(ctx->r , r );
1751 sk_unaligned_store(ctx->g , g );
1752 sk_unaligned_store(ctx->b , b );
1753 sk_unaligned_store(ctx->a , a );
1754 sk_unaligned_store(ctx->dr, dr);
1755 sk_unaligned_store(ctx->dg, dg);
1756 sk_unaligned_store(ctx->db, db);
1757 sk_unaligned_store(ctx->da, da);
1758 ctx->base = base;
1759 ctx->stage = program;
1760 }
1761 #endif
1762
1763
1764 // We could start defining normal Stages now. But first, some helper functions.
1765
1766 template <typename V, typename T>
1767 SI V load(const T* src) {
1768 return sk_unaligned_load<V>(src);
1769 }
1770
1771 template <typename V, typename T>
1772 SI void store(T* dst, V v) {
1773 sk_unaligned_store(dst, v);
1774 }
1775
1776 SI F from_byte(U8 b) {
1777 return cast(expand(b)) * (1/255.0f);
1778 }
1779 SI F from_short(U16 s) {
1780 return cast(expand(s)) * (1/65535.0f);
1781 }
1782 SI void from_565(U16 _565, F* r, F* g, F* b) {
1783 U32 wide = expand(_565);
1784 *r = cast(wide & (31<<11)) * (1.0f / (31<<11));
1785 *g = cast(wide & (63<< 5)) * (1.0f / (63<< 5));
1786 *b = cast(wide & (31<< 0)) * (1.0f / (31<< 0));
1787 }
1788 SI void from_4444(U16 _4444, F* r, F* g, F* b, F* a) {
1789 U32 wide = expand(_4444);
1790 *r = cast(wide & (15<<12)) * (1.0f / (15<<12));
1791 *g = cast(wide & (15<< 8)) * (1.0f / (15<< 8));
1792 *b = cast(wide & (15<< 4)) * (1.0f / (15<< 4));
1793 *a = cast(wide & (15<< 0)) * (1.0f / (15<< 0));
1794 }
1795 SI void from_8888(U32 _8888, F* r, F* g, F* b, F* a) {
1796 *r = cast((_8888 ) & 0xff) * (1/255.0f);
1797 *g = cast((_8888 >> 8) & 0xff) * (1/255.0f);
1798 *b = cast((_8888 >> 16) & 0xff) * (1/255.0f);
1799 *a = cast((_8888 >> 24) ) * (1/255.0f);
1800 }
1801 SI void from_88(U16 _88, F* r, F* g) {
1802 U32 wide = expand(_88);
1803 *r = cast((wide ) & 0xff) * (1/255.0f);
1804 *g = cast((wide >> 8) & 0xff) * (1/255.0f);
1805 }
1806 SI void from_1010102(U32 rgba, F* r, F* g, F* b, F* a) {
1807 *r = cast((rgba ) & 0x3ff) * (1/1023.0f);
1808 *g = cast((rgba >> 10) & 0x3ff) * (1/1023.0f);
1809 *b = cast((rgba >> 20) & 0x3ff) * (1/1023.0f);
1810 *a = cast((rgba >> 30) ) * (1/ 3.0f);
1811 }
1812 SI void from_1010102_xr(U32 rgba, F* r, F* g, F* b, F* a) {
1813 // Match https://developer.apple.com/documentation/metal/mtlpixelformat/bgr10_xr?language=objc
1814 // i.e. "float = (xr10_value - 384) / 510.0f", but with the modification that we store 2 bits
1815 // of alpha with a regular unorm encoding.
1816 *r = (cast((rgba ) & 0x3ff) - 384.f) * (1/510.f);
1817 *g = (cast((rgba >> 10) & 0x3ff) - 384.f) * (1/510.f);
1818 *b = (cast((rgba >> 20) & 0x3ff) - 384.f) * (1/510.f);
1819 *a = (cast((rgba >> 30) ) ) * (1/3.f); // A in 1010102_xr is *not* extended range
1820 }
1821 SI void from_10101010_xr(U64 _10x6, F* r, F* g, F* b, F* a) {
1822 // From https://developer.apple.com/documentation/metal/mtlpixelformat/bgra10_xr?language=objc
1823 // the linear transformation is the same as 1010102_xr, except the integer encoding is shifted
1824 // to have 6 low bits of padding.
1825 *r = (cast64((_10x6 >> ( 0+6)) & 0x3ff) - 384.f) * (1/510.f);
1826 *g = (cast64((_10x6 >> (16+6)) & 0x3ff) - 384.f) * (1/510.f);
1827 *b = (cast64((_10x6 >> (32+6)) & 0x3ff) - 384.f) * (1/510.f);
1828 *a = (cast64((_10x6 >> (48+6)) & 0x3ff) - 384.f) * (1/510.f);
1829 }
1830 SI void from_10x6(U64 _10x6, F* r, F* g, F* b, F* a) {
1831 *r = cast64((_10x6 >> ( 0+6)) & 0x3ff) * (1/1023.0f);
1832 *g = cast64((_10x6 >> (16+6)) & 0x3ff) * (1/1023.0f);
1833 *b = cast64((_10x6 >> (32+6)) & 0x3ff) * (1/1023.0f);
1834 *a = cast64((_10x6 >> (48+6)) & 0x3ff) * (1/1023.0f);
1835 }
1836 SI void from_1616(U32 _1616, F* r, F* g) {
1837 *r = cast((_1616 ) & 0xffff) * (1/65535.0f);
1838 *g = cast((_1616 >> 16) & 0xffff) * (1/65535.0f);
1839 }
1840 SI void from_16161616(U64 _16161616, F* r, F* g, F* b, F* a) {
1841 *r = cast64((_16161616 ) & 0xffff) * (1/65535.0f);
1842 *g = cast64((_16161616 >> 16) & 0xffff) * (1/65535.0f);
1843 *b = cast64((_16161616 >> 32) & 0xffff) * (1/65535.0f);
1844 *a = cast64((_16161616 >> 48) & 0xffff) * (1/65535.0f);
1845 }
1846
1847 // Used by load_ and store_ stages to get to the right (dx,dy) starting point of contiguous memory.
1848 template <typename T>
1849 SI T* ptr_at_xy(const SkRasterPipelineContexts::MemoryCtx* ctx, const size_t dx, const size_t dy) {
1850 return (T*)ctx->pixels + dy*ctx->stride + dx;
1851 }
1852
1853 // clamp v to [0,limit).
1854 SI F clamp(F v, F limit) {
1855 F inclusive = sk_bit_cast<F>(sk_bit_cast<U32>(limit) - 1); // Exclusive -> inclusive.
1856 return min(max(0.0f, v), inclusive);
1857 }
1858
1859 // clamp to (0,limit).
1860 SI F clamp_ex(F v, float limit) {
1861 const F inclusiveZ = F_(std::numeric_limits<float>::min()),
1862 inclusiveL = sk_bit_cast<F>( sk_bit_cast<U32>(F_(limit)) - 1 );
1863 return min(max(inclusiveZ, v), inclusiveL);
1864 }
1865
1866 // Polynomial approximation of degree 5 for sin(x * 2 * pi) in the range [-1/4, 1/4]
1867 // Adapted from https://github.com/google/swiftshader/blob/master/docs/Sin-Cos-Optimization.pdf
1868 SI F sin5q_(F x) {
1869 // A * x + B * x^3 + C * x^5
1870 // Exact at x = 0, 1/12, 1/6, 1/4, and their negatives,
1871 // which correspond to x * 2 * pi = 0, pi/6, pi/3, pi/2
1872 constexpr float A = 6.28230858f;
1873 constexpr float B = -41.1693687f;
1874 constexpr float C = 74.4388885f;
1875 F x2 = x * x;
1876 return x * mad(mad(x2, C, B), x2, A);
1877 }
1878
1879 SI F sin_(F x) {
1880 constexpr float one_over_pi2 = 1 / (2 * SK_FloatPI);
1881 x = mad(x, -one_over_pi2, 0.25f);
1882 x = 0.25f - abs_(x - floor_(x + 0.5f));
1883 return sin5q_(x);
1884 }
1885
1886 SI F cos_(F x) {
1887 constexpr float one_over_pi2 = 1 / (2 * SK_FloatPI);
1888 x *= one_over_pi2;
1889 x = 0.25f - abs_(x - floor_(x + 0.5f));
1890 return sin5q_(x);
1891 }
1892
1893 /* "GENERATING ACCURATE VALUES FOR THE TANGENT FUNCTION"
1894 https://mae.ufl.edu/~uhk/ACCURATE-TANGENT.pdf
1895
1896 approx = x + (1/3)x^3 + (2/15)x^5 + (17/315)x^7 + (62/2835)x^9
1897
1898 Some simplifications:
1899 1. tan(x) is periodic, -PI/2 < x < PI/2
1900 2. tan(x) is odd, so tan(-x) = -tan(x)
1901 3. Our polynomial approximation is best near zero, so we use the following identity
1902 tan(x) + tan(y)
1903 tan(x + y) = -----------------
1904 1 - tan(x)*tan(y)
1905 tan(PI/4) = 1
1906
1907 So for x > PI/8, we do the following refactor:
1908 x' = x - PI/4
1909
1910 1 + tan(x')
1911 tan(x) = ------------
1912 1 - tan(x')
1913 */
1914 SI F tan_(F x) {
1915 constexpr float Pi = SK_FloatPI;
1916 // periodic between -pi/2 ... pi/2
1917 // shift to 0...Pi, scale 1/Pi to get into 0...1, then fract, scale-up, shift-back
1918 x = mad(fract(mad(x, 1/Pi, 0.5f)), Pi, -Pi/2);
1919
1920 I32 neg = (x < 0.0f);
1921 x = if_then_else(neg, -x, x);
1922
1923 // minimize total error by shifting if x > pi/8
1924 I32 use_quotient = (x > (Pi/8));
1925 x = if_then_else(use_quotient, x - (Pi/4), x);
1926
1927 // 9th order poly = 4th order(x^2) * x
1928 const float c4 = 62 / 2835.0f;
1929 const float c3 = 17 / 315.0f;
1930 const float c2 = 2 / 15.0f;
1931 const float c1 = 1 / 3.0f;
1932 const float c0 = 1.0f;
1933 F x2 = x * x;
1934 x *= mad(x2, mad(x2, mad(x2, mad(x2, c4, c3), c2), c1), c0);
1935 x = if_then_else(use_quotient, (1+x)/(1-x), x);
1936 x = if_then_else(neg, -x, x);
1937 return x;
1938 }
1939
1940 /* Use 4th order polynomial approximation from https://arachnoid.com/polysolve/
1941 with 129 values of x,atan(x) for x:[0...1]
1942 This only works for 0 <= x <= 1
1943 */
1944 SI F approx_atan_unit(F x) {
1945 // y = 0.14130025741326729 x⁴
1946 // - 0.34312835980675116 x³
1947 // - 0.016172900528248768 x²
1948 // + 1.00376969762003850 x
1949 // - 0.00014758242182738969
1950 const float c4 = 0.14130025741326729f;
1951 const float c3 = -0.34312835980675116f;
1952 const float c2 = -0.016172900528248768f;
1953 const float c1 = 1.0037696976200385f;
1954 const float c0 = -0.00014758242182738969f;
1955 return mad(x, mad(x, mad(x, mad(x, c4, c3), c2), c1), c0);
1956 }
1957
1958 // Use identity atan(x) = pi/2 - atan(1/x) for x > 1
1959 SI F atan_(F x) {
1960 I32 neg = (x < 0.0f);
1961 x = if_then_else(neg, -x, x);
1962 I32 flip = (x > 1.0f);
1963 x = if_then_else(flip, 1/x, x);
1964 x = approx_atan_unit(x);
1965 x = if_then_else(flip, SK_FloatPI/2 - x, x);
1966 x = if_then_else(neg, -x, x);
1967 return x;
1968 }
1969
1970 // Handbook of Mathematical Functions, by Milton Abramowitz and Irene Stegun:
1971 // https://books.google.com/books/content?id=ZboM5tOFWtsC&pg=PA81&img=1&zoom=3&hl=en&bul=1&sig=ACfU3U2M75tG_iGVOS92eQspr14LTq02Nw&ci=0%2C15%2C999%2C1279&edge=0
1972 // http://screen/8YGJxUGFQ49bVX6
1973 SI F asin_(F x) {
1974 I32 neg = (x < 0.0f);
1975 x = if_then_else(neg, -x, x);
1976 const float c3 = -0.0187293f;
1977 const float c2 = 0.0742610f;
1978 const float c1 = -0.2121144f;
1979 const float c0 = 1.5707288f;
1980 F poly = mad(x, mad(x, mad(x, c3, c2), c1), c0);
1981 x = nmad(sqrt_(1 - x), poly, SK_FloatPI/2);
1982 x = if_then_else(neg, -x, x);
1983 return x;
1984 }
1985
1986 SI F acos_(F x) {
1987 return SK_FloatPI/2 - asin_(x);
1988 }
1989
1990 /* Use identity atan(x) = pi/2 - atan(1/x) for x > 1
1991 By swapping y,x to ensure the ratio is <= 1, we can safely call atan_unit()
1992 which avoids a 2nd divide instruction if we had instead called atan().
1993 */
1994 SI F atan2_(F y0, F x0) {
1995 I32 flip = (abs_(y0) > abs_(x0));
1996 F y = if_then_else(flip, x0, y0);
1997 F x = if_then_else(flip, y0, x0);
1998 F arg = y/x;
1999
2000 I32 neg = (arg < 0.0f);
2001 arg = if_then_else(neg, -arg, arg);
2002
2003 F r = approx_atan_unit(arg);
2004 r = if_then_else(flip, SK_FloatPI/2 - r, r);
2005 r = if_then_else(neg, -r, r);
2006
2007 // handle quadrant distinctions
2008 r = if_then_else((y0 >= 0) & (x0 < 0), r + SK_FloatPI, r);
2009 r = if_then_else((y0 < 0) & (x0 <= 0), r - SK_FloatPI, r);
2010 // Note: we don't try to handle 0,0 or infinities
2011 return r;
2012 }
2013
2014 // Used by gather_ stages to calculate the base pointer and a vector of indices to load.
2015 template <typename T>
2016 SI U32 ix_and_ptr(T** ptr, const SkRasterPipelineContexts::GatherCtx* ctx, F x, F y) {
2017 // We use exclusive clamp so that our min value is > 0 because ULP subtraction using U32 would
2018 // produce a NaN if applied to +0.f.
2019 x = clamp_ex(x, ctx->width );
2020 y = clamp_ex(y, ctx->height);
2021 x = sk_bit_cast<F>(sk_bit_cast<U32>(x) - (uint32_t)ctx->roundDownAtInteger);
2022 y = sk_bit_cast<F>(sk_bit_cast<U32>(y) - (uint32_t)ctx->roundDownAtInteger);
2023 *ptr = (const T*)ctx->pixels;
2024 return trunc_(y)*ctx->stride + trunc_(x);
2025 }
2026
2027 // We often have a nominally [0,1] float value we need to scale and convert to an integer,
2028 // whether for a table lookup or to pack back down into bytes for storage. The floating point
2029 // value is mapped to an integer using the equation "v * scale + bias".
2030 //
2031 // In practice, especially when dealing with interesting color spaces, that notionally
2032 // [0,1] float may be out of [0,1] range. Unorms cannot represent that, so we must clamp to
2033 // [0,maxI] after the bias and scale has been applied to `v`. This allows callers that explicitly
2034 // support negative float values (extended range) to still pack to a unorm.
2035 //
2036 // In most cases bias is 0 and the max value equals `scale`, but you can adjust the expected input
2037 // by tweaking `maxI` relative to `scale`.
2038 SI U32 to_unorm(F v, float scale, float bias, int maxI) {
2039 // Any time we use round() we probably want to use to_unorm().
2040 return round(min(max(0.0f, mad(v, scale, bias)), (float) maxI));
2041 }
2042 SI U32 to_unorm(F v, int scale) {
2043 return to_unorm(v, (float) scale, /*bias=*/0.f, /*maxI=*/scale);
2044 }
2045
2046 SI I32 cond_to_mask(I32 cond) {
2047 #if defined(SKRP_CPU_SCALAR)
2048 // In scalar mode, conditions are bools (0 or 1), but we want to store and operate on masks
2049 // (eg, using bitwise operations to select values).
2050 return if_then_else(cond, I32(~0), I32(0));
2051 #else
2052 // In SIMD mode, our various instruction sets already represent conditions as masks.
2053 return cond;
2054 #endif
2055 }
2056
2057 #if defined(SKRP_CPU_SCALAR)
2058 // In scalar mode, `data` only contains a single lane.
2059 SI uint32_t select_lane(uint32_t data, int /*lane*/) { return data; }
2060 SI int32_t select_lane( int32_t data, int /*lane*/) { return data; }
2061 #else
2062 // In SIMD mode, `data` contains a vector of lanes.
2063 SI uint32_t select_lane(U32 data, int lane) { return data[lane]; }
2064 SI int32_t select_lane(I32 data, int lane) { return data[lane]; }
2065 #endif
2066
2067 // Now finally, normal Stages!
2068
2069 HIGHP_STAGE(seed_shader, NoCtx) {
2070 static constexpr float iota[] = {
2071 0.5f, 1.5f, 2.5f, 3.5f, 4.5f, 5.5f, 6.5f, 7.5f,
2072 8.5f, 9.5f,10.5f,11.5f,12.5f,13.5f,14.5f,15.5f,
2073 };
2074 static_assert(std::size(iota) >= SkRasterPipelineContexts::kMaxStride_highp);
2075
2076 // It's important for speed to explicitly cast(dx) and cast(dy),
2077 // which has the effect of splatting them to vectors before converting to floats.
2078 // On Intel this breaks a data dependency on previous loop iterations' registers.
2079 r = cast(U32_(dx)) + sk_unaligned_load<F>(iota);
2080 g = cast(U32_(dy)) + 0.5f;
2081 b = F1; // This is w=1 for matrix multiplies by the device coords.
2082 a = F0;
2083 }
2084
2085 HIGHP_STAGE(dither, const float* rate) {
2086 // Get [(dx,dy), (dx+1,dy), (dx+2,dy), ...] loaded up in integer vectors.
2087 uint32_t iota[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
2088 static_assert(std::size(iota) >= SkRasterPipelineContexts::kMaxStride_highp);
2089
2090 U32 X = U32_(dx) + sk_unaligned_load<U32>(iota),
2091 Y = U32_(dy);
2092
2093 // We're doing 8x8 ordered dithering, see https://en.wikipedia.org/wiki/Ordered_dithering.
2094 // In this case n=8 and we're using the matrix that looks like 1/64 x [ 0 48 12 60 ... ].
2095
2096 // We only need X and X^Y from here on, so it's easier to just think of that as "Y".
2097 Y ^= X;
2098
2099 // We'll mix the bottom 3 bits of each of X and Y to make 6 bits,
2100 // for 2^6 == 64 == 8x8 matrix values. If X=abc and Y=def, we make fcebda.
2101 U32 M = (Y & 1) << 5 | (X & 1) << 4
2102 | (Y & 2) << 2 | (X & 2) << 1
2103 | (Y & 4) >> 1 | (X & 4) >> 2;
2104
2105 // Scale that dither to [0,1), then (-0.5,+0.5), here using 63/128 = 0.4921875 as 0.5-epsilon.
2106 // We want to make sure our dither is less than 0.5 in either direction to keep exact values
2107 // like 0 and 1 unchanged after rounding.
2108 F dither = mad(cast(M), 2/128.0f, -63/128.0f);
2109
2110 r = mad(dither, *rate, r);
2111 g = mad(dither, *rate, g);
2112 b = mad(dither, *rate, b);
2113
2114 r = max(0.0f, min(r, a));
2115 g = max(0.0f, min(g, a));
2116 b = max(0.0f, min(b, a));
2117 }
2118
2119 // load 4 floats from memory, and splat them into r,g,b,a
2120 HIGHP_STAGE(uniform_color, const SkRasterPipelineContexts::UniformColorCtx* c) {
2121 r = F_(c->r);
2122 g = F_(c->g);
2123 b = F_(c->b);
2124 a = F_(c->a);
2125 }
2126 HIGHP_STAGE(unbounded_uniform_color, const SkRasterPipelineContexts::UniformColorCtx* c) {
2127 r = F_(c->r);
2128 g = F_(c->g);
2129 b = F_(c->b);
2130 a = F_(c->a);
2131 }
2132 // load 4 floats from memory, and splat them into dr,dg,db,da
2133 HIGHP_STAGE(uniform_color_dst, const SkRasterPipelineContexts::UniformColorCtx* c) {
2134 dr = F_(c->r);
2135 dg = F_(c->g);
2136 db = F_(c->b);
2137 da = F_(c->a);
2138 }
2139
2140 // splats opaque-black into r,g,b,a
2141 HIGHP_STAGE(black_color, NoCtx) {
2142 r = g = b = F0;
2143 a = F1;
2144 }
2145
2146 HIGHP_STAGE(white_color, NoCtx) {
2147 r = g = b = a = F1;
2148 }
2149
2150 // load registers r,g,b,a from context (mirrors store_src)
2151 HIGHP_STAGE(load_src, const float* ptr) {
2152 r = sk_unaligned_load<F>(ptr + 0*N);
2153 g = sk_unaligned_load<F>(ptr + 1*N);
2154 b = sk_unaligned_load<F>(ptr + 2*N);
2155 a = sk_unaligned_load<F>(ptr + 3*N);
2156 }
2157
2158 // store registers r,g,b,a into context (mirrors load_src)
2159 HIGHP_STAGE(store_src, float* ptr) {
2160 sk_unaligned_store(ptr + 0*N, r);
2161 sk_unaligned_store(ptr + 1*N, g);
2162 sk_unaligned_store(ptr + 2*N, b);
2163 sk_unaligned_store(ptr + 3*N, a);
2164 }
2165 // store registers r,g into context
2166 HIGHP_STAGE(store_src_rg, float* ptr) {
2167 sk_unaligned_store(ptr + 0*N, r);
2168 sk_unaligned_store(ptr + 1*N, g);
2169 }
2170 // load registers r,g from context
2171 HIGHP_STAGE(load_src_rg, float* ptr) {
2172 r = sk_unaligned_load<F>(ptr + 0*N);
2173 g = sk_unaligned_load<F>(ptr + 1*N);
2174 }
2175 // store register a into context
2176 HIGHP_STAGE(store_src_a, float* ptr) {
2177 sk_unaligned_store(ptr, a);
2178 }
2179
2180 // load registers dr,dg,db,da from context (mirrors store_dst)
2181 HIGHP_STAGE(load_dst, const float* ptr) {
2182 dr = sk_unaligned_load<F>(ptr + 0*N);
2183 dg = sk_unaligned_load<F>(ptr + 1*N);
2184 db = sk_unaligned_load<F>(ptr + 2*N);
2185 da = sk_unaligned_load<F>(ptr + 3*N);
2186 }
2187
2188 // store registers dr,dg,db,da into context (mirrors load_dst)
2189 HIGHP_STAGE(store_dst, float* ptr) {
2190 sk_unaligned_store(ptr + 0*N, dr);
2191 sk_unaligned_store(ptr + 1*N, dg);
2192 sk_unaligned_store(ptr + 2*N, db);
2193 sk_unaligned_store(ptr + 3*N, da);
2194 }
2195
2196 // Most blend modes apply the same logic to each channel.
2197 #define BLEND_MODE(name) \
2198 SI F name##_channel(F s, F d, F sa, F da); \
2199 HIGHP_STAGE(name, NoCtx) { \
2200 r = name##_channel(r,dr,a,da); \
2201 g = name##_channel(g,dg,a,da); \
2202 b = name##_channel(b,db,a,da); \
2203 a = name##_channel(a,da,a,da); \
2204 } \
2205 SI F name##_channel(F s, F d, F sa, F da)
2206
2207 SI F inv(F x) { return 1.0f - x; }
2208 SI F two(F x) { return x + x; }
2209
2210 BLEND_MODE(clear) { return F0; }
2211 BLEND_MODE(srcatop) { return mad(s, da, d*inv(sa)); }
2212 BLEND_MODE(dstatop) { return mad(d, sa, s*inv(da)); }
2213 BLEND_MODE(srcin) { return s * da; }
2214 BLEND_MODE(dstin) { return d * sa; }
2215 BLEND_MODE(srcout) { return s * inv(da); }
2216 BLEND_MODE(dstout) { return d * inv(sa); }
2217 BLEND_MODE(srcover) { return mad(d, inv(sa), s); }
2218 BLEND_MODE(dstover) { return mad(s, inv(da), d); }
2219
2220 BLEND_MODE(modulate) { return s*d; }
2221 BLEND_MODE(multiply) { return mad(s, d, mad(s, inv(da), d*inv(sa))); }
2222 BLEND_MODE(plus_) { return min(s + d, 1.0f); } // We can clamp to either 1 or sa.
2223 BLEND_MODE(screen) { return nmad(s, d, s + d); }
2224 BLEND_MODE(xor_) { return mad(s, inv(da), d*inv(sa)); }
2225 #undef BLEND_MODE
2226
2227 // Most other blend modes apply the same logic to colors, and srcover to alpha.
2228 #define BLEND_MODE(name) \
2229 SI F name##_channel(F s, F d, F sa, F da); \
2230 HIGHP_STAGE(name, NoCtx) { \
2231 r = name##_channel(r,dr,a,da); \
2232 g = name##_channel(g,dg,a,da); \
2233 b = name##_channel(b,db,a,da); \
2234 a = mad(da, inv(a), a); \
2235 } \
2236 SI F name##_channel(F s, F d, F sa, F da)
2237
2238 BLEND_MODE(darken) { return s + d - max(s*da, d*sa) ; }
2239 BLEND_MODE(lighten) { return s + d - min(s*da, d*sa) ; }
2240 BLEND_MODE(difference) { return s + d - two(min(s*da, d*sa)); }
2241 BLEND_MODE(exclusion) { return s + d - two(s*d); }
2242
2243 BLEND_MODE(colorburn) {
2244 return if_then_else(d == da, d + s*inv(da),
2245 if_then_else(s == 0, /* s + */ d*inv(sa),
2246 sa*(da - min(da, (da-d)*sa*rcp_fast(s))) + s*inv(da) + d*inv(sa)));
2247 }
2248 BLEND_MODE(colordodge) {
2249 return if_then_else(d == 0, /* d + */ s*inv(da),
2250 if_then_else(s == sa, s + d*inv(sa),
2251 sa*min(da, (d*sa)*rcp_fast(sa - s)) + s*inv(da) + d*inv(sa)));
2252 }
2253 BLEND_MODE(hardlight) {
2254 return s*inv(da) + d*inv(sa)
2255 + if_then_else(two(s) <= sa, two(s*d), sa*da - two((da-d)*(sa-s)));
2256 }
2257 BLEND_MODE(overlay) {
2258 return s*inv(da) + d*inv(sa)
2259 + if_then_else(two(d) <= da, two(s*d), sa*da - two((da-d)*(sa-s)));
2260 }
2261
2262 BLEND_MODE(softlight) {
2263 F m = if_then_else(da > 0, d / da, 0.0f),
2264 s2 = two(s),
2265 m4 = two(two(m));
2266
2267 // The logic forks three ways:
2268 // 1. dark src?
2269 // 2. light src, dark dst?
2270 // 3. light src, light dst?
2271 F darkSrc = d*(sa + (s2 - sa)*(1.0f - m)), // Used in case 1.
2272 darkDst = (m4*m4 + m4)*(m - 1.0f) + 7.0f*m, // Used in case 2.
2273 liteDst = sqrt_(m) - m,
2274 liteSrc = d*sa + da*(s2 - sa) * if_then_else(two(two(d)) <= da, darkDst, liteDst); // 2 or 3?
2275 return s*inv(da) + d*inv(sa) + if_then_else(s2 <= sa, darkSrc, liteSrc); // 1 or (2 or 3)?
2276 }
2277 #undef BLEND_MODE
2278
2279 // We're basing our implemenation of non-separable blend modes on
2280 // https://www.w3.org/TR/compositing-1/#blendingnonseparable.
2281 // and
2282 // https://www.khronos.org/registry/OpenGL/specs/es/3.2/es_spec_3.2.pdf
2283 // They're equivalent, but ES' math has been better simplified.
2284 //
2285 // Anything extra we add beyond that is to make the math work with premul inputs.
2286
2287 SI F sat(F r, F g, F b) { return max(r, max(g,b)) - min(r, min(g,b)); }
2288 SI F lum(F r, F g, F b) { return mad(r, 0.30f, mad(g, 0.59f, b*0.11f)); }
2289
2290 SI void set_sat(F* r, F* g, F* b, F s) {
2291 F mn = min(*r, min(*g,*b)),
2292 mx = max(*r, max(*g,*b)),
2293 sat = mx - mn;
2294
2295 // Map min channel to 0, max channel to s, and scale the middle proportionally.
2296 s = if_then_else(sat == 0.0f, 0.0f, s * rcp_fast(sat));
2297 *r = (*r - mn) * s;
2298 *g = (*g - mn) * s;
2299 *b = (*b - mn) * s;
2300 }
2301 SI void set_lum(F* r, F* g, F* b, F l) {
2302 F diff = l - lum(*r, *g, *b);
2303 *r += diff;
2304 *g += diff;
2305 *b += diff;
2306 }
2307 SI F clip_channel(F c, F l, I32 clip_low, I32 clip_high, F mn_scale, F mx_scale) {
2308 c = if_then_else(clip_low, mad(mn_scale, c - l, l), c);
2309 c = if_then_else(clip_high, mad(mx_scale, c - l, l), c);
2310 c = max(c, 0.0f); // Sometimes without this we may dip just a little negative.
2311 return c;
2312 }
2313 SI void clip_color(F* r, F* g, F* b, F a) {
2314 F mn = min(*r, min(*g, *b)),
2315 mx = max(*r, max(*g, *b)),
2316 l = lum(*r, *g, *b),
2317 mn_scale = ( l) * rcp_fast(l - mn),
2318 mx_scale = (a - l) * rcp_fast(mx - l);
2319 I32 clip_low = cond_to_mask(mn < 0 && l != mn),
2320 clip_high = cond_to_mask(mx > a && l != mx);
2321
2322 *r = clip_channel(*r, l, clip_low, clip_high, mn_scale, mx_scale);
2323 *g = clip_channel(*g, l, clip_low, clip_high, mn_scale, mx_scale);
2324 *b = clip_channel(*b, l, clip_low, clip_high, mn_scale, mx_scale);
2325 }
2326
2327 HIGHP_STAGE(hue, NoCtx) {
2328 F R = r*a,
2329 G = g*a,
2330 B = b*a;
2331
2332 set_sat(&R, &G, &B, sat(dr,dg,db)*a);
2333 set_lum(&R, &G, &B, lum(dr,dg,db)*a);
2334 clip_color(&R,&G,&B, a*da);
2335
2336 r = mad(r, inv(da), mad(dr, inv(a), R));
2337 g = mad(g, inv(da), mad(dg, inv(a), G));
2338 b = mad(b, inv(da), mad(db, inv(a), B));
2339 a = a + nmad(a, da, da);
2340 }
2341 HIGHP_STAGE(saturation, NoCtx) {
2342 F R = dr*a,
2343 G = dg*a,
2344 B = db*a;
2345
2346 set_sat(&R, &G, &B, sat( r, g, b)*da);
2347 set_lum(&R, &G, &B, lum(dr,dg,db)* a); // (This is not redundant.)
2348 clip_color(&R,&G,&B, a*da);
2349
2350 r = mad(r, inv(da), mad(dr, inv(a), R));
2351 g = mad(g, inv(da), mad(dg, inv(a), G));
2352 b = mad(b, inv(da), mad(db, inv(a), B));
2353 a = a + nmad(a, da, da);
2354 }
2355 HIGHP_STAGE(color, NoCtx) {
2356 F R = r*da,
2357 G = g*da,
2358 B = b*da;
2359
2360 set_lum(&R, &G, &B, lum(dr,dg,db)*a);
2361 clip_color(&R,&G,&B, a*da);
2362
2363 r = mad(r, inv(da), mad(dr, inv(a), R));
2364 g = mad(g, inv(da), mad(dg, inv(a), G));
2365 b = mad(b, inv(da), mad(db, inv(a), B));
2366 a = a + nmad(a, da, da);
2367 }
2368 HIGHP_STAGE(luminosity, NoCtx) {
2369 F R = dr*a,
2370 G = dg*a,
2371 B = db*a;
2372
2373 set_lum(&R, &G, &B, lum(r,g,b)*da);
2374 clip_color(&R,&G,&B, a*da);
2375
2376 r = mad(r, inv(da), mad(dr, inv(a), R));
2377 g = mad(g, inv(da), mad(dg, inv(a), G));
2378 b = mad(b, inv(da), mad(db, inv(a), B));
2379 a = a + nmad(a, da, da);
2380 }
2381
2382 HIGHP_STAGE(srcover_rgba_8888, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2383 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
2384
2385 U32 dst = load<U32>(ptr);
2386 dr = cast((dst ) & 0xff);
2387 dg = cast((dst >> 8) & 0xff);
2388 db = cast((dst >> 16) & 0xff);
2389 da = cast((dst >> 24) );
2390 // {dr,dg,db,da} are in [0,255]
2391 // { r, g, b, a} are in [0, 1] (but may be out of gamut)
2392
2393 r = mad(dr, inv(a), r*255.0f);
2394 g = mad(dg, inv(a), g*255.0f);
2395 b = mad(db, inv(a), b*255.0f);
2396 a = mad(da, inv(a), a*255.0f);
2397 // { r, g, b, a} are now in [0,255] (but may be out of gamut)
2398
2399 // to_unorm() clamps back to gamut. Scaling by 1 since we're already 255-based.
2400 dst = to_unorm(r, /*scale=*/1, /*bias=*/0.f, /*maxI=*/255)
2401 | to_unorm(g, /*scale=*/1, /*bias=*/0.f, /*maxI=*/255) << 8
2402 | to_unorm(b, /*scale=*/1, /*bias=*/0.f, /*maxI=*/255) << 16
2403 | to_unorm(a, /*scale=*/1, /*bias=*/0.f, /*maxI=*/255) << 24;
2404 store(ptr, dst);
2405 }
2406
2407 SI F clamp_01_(F v) { return min(max(0.0f, v), 1.0f); }
2408
2409 HIGHP_STAGE(clamp_01, NoCtx) {
2410 r = clamp_01_(r);
2411 g = clamp_01_(g);
2412 b = clamp_01_(b);
2413 a = clamp_01_(a);
2414 }
2415
2416 HIGHP_STAGE(clamp_a_01, NoCtx) {
2417 a = clamp_01_(a);
2418 }
2419
2420 HIGHP_STAGE(clamp_gamut, NoCtx) {
2421 a = min(max(a, 0.0f), 1.0f);
2422 r = min(max(r, 0.0f), a);
2423 g = min(max(g, 0.0f), a);
2424 b = min(max(b, 0.0f), a);
2425 }
2426
2427 HIGHP_STAGE(set_rgb, const float* rgb) {
2428 r = F_(rgb[0]);
2429 g = F_(rgb[1]);
2430 b = F_(rgb[2]);
2431 }
2432
2433 HIGHP_STAGE(unbounded_set_rgb, const float* rgb) {
2434 r = F_(rgb[0]);
2435 g = F_(rgb[1]);
2436 b = F_(rgb[2]);
2437 }
2438
2439 HIGHP_STAGE(swap_rb, NoCtx) {
2440 auto tmp = r;
2441 r = b;
2442 b = tmp;
2443 }
2444 HIGHP_STAGE(swap_rb_dst, NoCtx) {
2445 auto tmp = dr;
2446 dr = db;
2447 db = tmp;
2448 }
2449
2450 HIGHP_STAGE(move_src_dst, NoCtx) {
2451 dr = r;
2452 dg = g;
2453 db = b;
2454 da = a;
2455 }
2456 HIGHP_STAGE(move_dst_src, NoCtx) {
2457 r = dr;
2458 g = dg;
2459 b = db;
2460 a = da;
2461 }
2462 HIGHP_STAGE(swap_src_dst, NoCtx) {
2463 std::swap(r, dr);
2464 std::swap(g, dg);
2465 std::swap(b, db);
2466 std::swap(a, da);
2467 }
2468
2469 HIGHP_STAGE(premul, NoCtx) {
2470 r = r * a;
2471 g = g * a;
2472 b = b * a;
2473 }
2474 HIGHP_STAGE(premul_dst, NoCtx) {
2475 dr = dr * da;
2476 dg = dg * da;
2477 db = db * da;
2478 }
2479 HIGHP_STAGE(unpremul, NoCtx) {
2480 float inf = sk_bit_cast<float>(0x7f800000);
2481 auto scale = if_then_else(1.0f/a < inf, 1.0f/a, 0.0f);
2482 r *= scale;
2483 g *= scale;
2484 b *= scale;
2485 }
2486 HIGHP_STAGE(unpremul_polar, NoCtx) {
2487 float inf = sk_bit_cast<float>(0x7f800000);
2488 auto scale = if_then_else(1.0f/a < inf, 1.0f/a, 0.0f);
2489 g *= scale;
2490 b *= scale;
2491 }
2492
2493 HIGHP_STAGE(force_opaque , NoCtx) { a = F1; }
2494 HIGHP_STAGE(force_opaque_dst, NoCtx) { da = F1; }
2495
2496 HIGHP_STAGE(rgb_to_hsl, NoCtx) {
2497 F mx = max(r, max(g,b)),
2498 mn = min(r, min(g,b)),
2499 d = mx - mn,
2500 d_rcp = 1.0f / d;
2501
2502 F h = (1/6.0f) *
2503 if_then_else(mx == mn, 0.0f,
2504 if_then_else(mx == r, (g-b)*d_rcp + if_then_else(g < b, 6.0f, 0.0f),
2505 if_then_else(mx == g, (b-r)*d_rcp + 2.0f,
2506 (r-g)*d_rcp + 4.0f)));
2507
2508 F l = (mx + mn) * 0.5f;
2509 F s = if_then_else(mx == mn, 0.0f,
2510 d / if_then_else(l > 0.5f, 2.0f-mx-mn, mx+mn));
2511
2512 r = h;
2513 g = s;
2514 b = l;
2515 }
2516 HIGHP_STAGE(hsl_to_rgb, NoCtx) {
2517 // See GrRGBToHSLFilterEffect.fp
2518
2519 F h = r,
2520 s = g,
2521 l = b,
2522 c = (1.0f - abs_(2.0f * l - 1)) * s;
2523
2524 auto hue_to_rgb = [&](F hue) {
2525 F q = clamp_01_(abs_(fract(hue) * 6.0f - 3.0f) - 1.0f);
2526 return (q - 0.5f) * c + l;
2527 };
2528
2529 r = hue_to_rgb(h + 0.0f/3.0f);
2530 g = hue_to_rgb(h + 2.0f/3.0f);
2531 b = hue_to_rgb(h + 1.0f/3.0f);
2532 }
2533
2534 // Color conversion functions used in gradient interpolation, based on
2535 // https://www.w3.org/TR/css-color-4/#color-conversion-code
2536 HIGHP_STAGE(css_lab_to_xyz, NoCtx) {
2537 constexpr float k = 24389 / 27.0f;
2538 constexpr float e = 216 / 24389.0f;
2539
2540 F f[3];
2541 f[1] = (r + 16) * (1 / 116.0f);
2542 f[0] = (g * (1 / 500.0f)) + f[1];
2543 f[2] = f[1] - (b * (1 / 200.0f));
2544
2545 F f_cubed[3] = { f[0]*f[0]*f[0], f[1]*f[1]*f[1], f[2]*f[2]*f[2] };
2546
2547 F xyz[3] = {
2548 if_then_else(f_cubed[0] > e, f_cubed[0], (116 * f[0] - 16) * (1 / k)),
2549 if_then_else(r > k * e, f_cubed[1], r * (1 / k)),
2550 if_then_else(f_cubed[2] > e, f_cubed[2], (116 * f[2] - 16) * (1 / k))
2551 };
2552
2553 constexpr float D50[3] = { 0.3457f / 0.3585f, 1.0f, (1.0f - 0.3457f - 0.3585f) / 0.3585f };
2554 r = xyz[0]*D50[0];
2555 g = xyz[1]*D50[1];
2556 b = xyz[2]*D50[2];
2557 }
2558
2559 HIGHP_STAGE(css_oklab_to_linear_srgb, NoCtx) {
2560 F l_ = r + 0.3963377774f * g + 0.2158037573f * b,
2561 m_ = r - 0.1055613458f * g - 0.0638541728f * b,
2562 s_ = r - 0.0894841775f * g - 1.2914855480f * b;
2563
2564 F l = l_*l_*l_,
2565 m = m_*m_*m_,
2566 s = s_*s_*s_;
2567
2568 r = +4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s;
2569 g = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s;
2570 b = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s;
2571 }
2572
2573 HIGHP_STAGE(css_oklab_gamut_map_to_linear_srgb, NoCtx) {
2574 // TODO(https://crbug.com/1508329): Add support for gamut mapping.
2575 // Return a greyscale value, so that accidental use is obvious.
2576 F l_ = r,
2577 m_ = r,
2578 s_ = r;
2579
2580 F l = l_*l_*l_,
2581 m = m_*m_*m_,
2582 s = s_*s_*s_;
2583
2584 r = +4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s;
2585 g = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s;
2586 b = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s;
2587 }
2588
2589 // Skia stores all polar colors with hue in the first component, so this "LCH -> Lab" transform
2590 // actually takes "HCL". This is also used to do the same polar transform for OkHCL to OkLAB.
2591 // See similar comments & logic in SkGradientBaseShader.cpp.
2592 HIGHP_STAGE(css_hcl_to_lab, NoCtx) {
2593 F H = r,
2594 C = g,
2595 L = b;
2596
2597 F hueRadians = H * (SK_FloatPI / 180);
2598
2599 r = L;
2600 g = C * cos_(hueRadians);
2601 b = C * sin_(hueRadians);
2602 }
2603
2604 SI F mod_(F x, float y) {
2605 return nmad(y, floor_(x * (1 / y)), x);
2606 }
2607
2608 struct RGB { F r, g, b; };
2609
2610 SI RGB css_hsl_to_srgb_(F h, F s, F l) {
2611 h = mod_(h, 360);
2612
2613 s *= 0.01f;
2614 l *= 0.01f;
2615
2616 F k[3] = {
2617 mod_(0 + h * (1 / 30.0f), 12),
2618 mod_(8 + h * (1 / 30.0f), 12),
2619 mod_(4 + h * (1 / 30.0f), 12)
2620 };
2621 F a = s * min(l, 1 - l);
2622 return {
2623 l - a * max(-1.0f, min(min(k[0] - 3.0f, 9.0f - k[0]), 1.0f)),
2624 l - a * max(-1.0f, min(min(k[1] - 3.0f, 9.0f - k[1]), 1.0f)),
2625 l - a * max(-1.0f, min(min(k[2] - 3.0f, 9.0f - k[2]), 1.0f))
2626 };
2627 }
2628
2629 HIGHP_STAGE(css_hsl_to_srgb, NoCtx) {
2630 RGB rgb = css_hsl_to_srgb_(r, g, b);
2631 r = rgb.r;
2632 g = rgb.g;
2633 b = rgb.b;
2634 }
2635
2636 HIGHP_STAGE(css_hwb_to_srgb, NoCtx) {
2637 g *= 0.01f;
2638 b *= 0.01f;
2639
2640 F gray = g / (g + b);
2641
2642 RGB rgb = css_hsl_to_srgb_(r, F_(100.0f), F_(50.0f));
2643 rgb.r = rgb.r * (1 - g - b) + g;
2644 rgb.g = rgb.g * (1 - g - b) + g;
2645 rgb.b = rgb.b * (1 - g - b) + g;
2646
2647 auto isGray = (g + b) >= 1;
2648
2649 r = if_then_else(isGray, gray, rgb.r);
2650 g = if_then_else(isGray, gray, rgb.g);
2651 b = if_then_else(isGray, gray, rgb.b);
2652 }
2653
2654 // Derive alpha's coverage from rgb coverage and the values of src and dst alpha.
2655 SI F alpha_coverage_from_rgb_coverage(F a, F da, F cr, F cg, F cb) {
2656 return if_then_else(a < da, min(cr, min(cg,cb))
2657 , max(cr, max(cg,cb)));
2658 }
2659
2660 HIGHP_STAGE(scale_1_float, const float* c) {
2661 r = r * *c;
2662 g = g * *c;
2663 b = b * *c;
2664 a = a * *c;
2665 }
2666 HIGHP_STAGE(scale_u8, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2667 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
2668
2669 auto scales = load<U8>(ptr);
2670 auto c = from_byte(scales);
2671
2672 r = r * c;
2673 g = g * c;
2674 b = b * c;
2675 a = a * c;
2676 }
2677 HIGHP_STAGE(scale_565, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2678 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
2679
2680 F cr,cg,cb;
2681 from_565(load<U16>(ptr), &cr, &cg, &cb);
2682
2683 F ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
2684
2685 r = r * cr;
2686 g = g * cg;
2687 b = b * cb;
2688 a = a * ca;
2689 }
2690
2691 SI F lerp(F from, F to, F t) {
2692 return mad(to-from, t, from);
2693 }
2694
2695 HIGHP_STAGE(lerp_1_float, const float* c) {
2696 r = lerp(dr, r, F_(*c));
2697 g = lerp(dg, g, F_(*c));
2698 b = lerp(db, b, F_(*c));
2699 a = lerp(da, a, F_(*c));
2700 }
2701 HIGHP_STAGE(scale_native, const float scales[]) {
2702 auto c = sk_unaligned_load<F>(scales);
2703 r = r * c;
2704 g = g * c;
2705 b = b * c;
2706 a = a * c;
2707 }
2708 HIGHP_STAGE(lerp_native, const float scales[]) {
2709 auto c = sk_unaligned_load<F>(scales);
2710 r = lerp(dr, r, c);
2711 g = lerp(dg, g, c);
2712 b = lerp(db, b, c);
2713 a = lerp(da, a, c);
2714 }
2715 HIGHP_STAGE(lerp_u8, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2716 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
2717
2718 auto scales = load<U8>(ptr);
2719 auto c = from_byte(scales);
2720
2721 r = lerp(dr, r, c);
2722 g = lerp(dg, g, c);
2723 b = lerp(db, b, c);
2724 a = lerp(da, a, c);
2725 }
2726 HIGHP_STAGE(lerp_565, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2727 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
2728
2729 F cr,cg,cb;
2730 from_565(load<U16>(ptr), &cr, &cg, &cb);
2731
2732 F ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
2733
2734 r = lerp(dr, r, cr);
2735 g = lerp(dg, g, cg);
2736 b = lerp(db, b, cb);
2737 a = lerp(da, a, ca);
2738 }
2739
2740 HIGHP_STAGE(emboss, const SkRasterPipelineContexts::EmbossCtx* ctx) {
2741 auto mptr = ptr_at_xy<const uint8_t>(&ctx->mul, dx,dy),
2742 aptr = ptr_at_xy<const uint8_t>(&ctx->add, dx,dy);
2743
2744 F mul = from_byte(load<U8>(mptr)),
2745 add = from_byte(load<U8>(aptr));
2746
2747 r = mad(r, mul, add);
2748 g = mad(g, mul, add);
2749 b = mad(b, mul, add);
2750 }
2751
2752 HIGHP_STAGE(byte_tables, const SkRasterPipelineContexts::TablesCtx* tables) {
2753 r = from_byte(gather(tables->r, to_unorm(r, 255)));
2754 g = from_byte(gather(tables->g, to_unorm(g, 255)));
2755 b = from_byte(gather(tables->b, to_unorm(b, 255)));
2756 a = from_byte(gather(tables->a, to_unorm(a, 255)));
2757 }
2758
2759 SI F strip_sign(F x, U32* sign) {
2760 U32 bits = sk_bit_cast<U32>(x);
2761 *sign = bits & 0x80000000;
2762 return sk_bit_cast<F>(bits ^ *sign);
2763 }
2764
2765 SI F apply_sign(F x, U32 sign) {
2766 return sk_bit_cast<F>(sign | sk_bit_cast<U32>(x));
2767 }
2768
2769 HIGHP_STAGE(parametric, const skcms_TransferFunction* ctx) {
2770 auto fn = [&](F v) {
2771 U32 sign;
2772 v = strip_sign(v, &sign);
2773
2774 F r = if_then_else(v <= ctx->d, mad(ctx->c, v, ctx->f)
2775 , approx_powf(mad(ctx->a, v, ctx->b), ctx->g) + ctx->e);
2776 return apply_sign(r, sign);
2777 };
2778 r = fn(r);
2779 g = fn(g);
2780 b = fn(b);
2781 }
2782
2783 HIGHP_STAGE(gamma_, const float* G) {
2784 auto fn = [&](F v) {
2785 U32 sign;
2786 v = strip_sign(v, &sign);
2787 return apply_sign(approx_powf(v, *G), sign);
2788 };
2789 r = fn(r);
2790 g = fn(g);
2791 b = fn(b);
2792 }
2793
2794 HIGHP_STAGE(PQish, const skcms_TransferFunction* ctx) {
2795 auto fn = [&](F v) {
2796 U32 sign;
2797 v = strip_sign(v, &sign);
2798
2799 F r = approx_powf(max(mad(ctx->b, approx_powf(v, ctx->c), ctx->a), 0.0f)
2800 / (mad(ctx->e, approx_powf(v, ctx->c), ctx->d)),
2801 ctx->f);
2802
2803 return apply_sign(r, sign);
2804 };
2805 r = fn(r);
2806 g = fn(g);
2807 b = fn(b);
2808 }
2809
2810 HIGHP_STAGE(HLGish, const skcms_TransferFunction* ctx) {
2811 auto fn = [&](F v) {
2812 U32 sign;
2813 v = strip_sign(v, &sign);
2814
2815 const float R = ctx->a, G = ctx->b,
2816 a = ctx->c, b = ctx->d, c = ctx->e,
2817 K = ctx->f + 1.0f;
2818
2819 F r = if_then_else(v*R <= 1, approx_powf(v*R, G)
2820 , approx_exp((v-c)*a) + b);
2821
2822 return K * apply_sign(r, sign);
2823 };
2824 r = fn(r);
2825 g = fn(g);
2826 b = fn(b);
2827 }
2828
2829 HIGHP_STAGE(HLGinvish, const skcms_TransferFunction* ctx) {
2830 auto fn = [&](F v) {
2831 U32 sign;
2832 v = strip_sign(v, &sign);
2833
2834 const float R = ctx->a, G = ctx->b,
2835 a = ctx->c, b = ctx->d, c = ctx->e,
2836 K = ctx->f + 1.0f;
2837
2838 v /= K;
2839 F r = if_then_else(v <= 1, R * approx_powf(v, G)
2840 , a * approx_log(v - b) + c);
2841
2842 return apply_sign(r, sign);
2843 };
2844 r = fn(r);
2845 g = fn(g);
2846 b = fn(b);
2847 }
2848
2849 HIGHP_STAGE(ootf, const float* ctx) {
2850 F Y = ctx[0] * r + ctx[1] * g + ctx[2] * b;
2851
2852 U32 sign;
2853 Y = strip_sign(Y, &sign);
2854 F Y_to_gamma_minus_one = apply_sign(approx_powf(Y, ctx[3]), sign);
2855 r = r * Y_to_gamma_minus_one;
2856 g = g * Y_to_gamma_minus_one;
2857 b = b * Y_to_gamma_minus_one;
2858 }
2859
2860 HIGHP_STAGE(load_a8, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2861 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
2862
2863 r = g = b = F0;
2864 a = from_byte(load<U8>(ptr));
2865 }
2866 HIGHP_STAGE(load_a8_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2867 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
2868
2869 dr = dg = db = F0;
2870 da = from_byte(load<U8>(ptr));
2871 }
2872 HIGHP_STAGE(gather_a8, const SkRasterPipelineContexts::GatherCtx* ctx) {
2873 const uint8_t* ptr;
2874 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
2875 r = g = b = F0;
2876 a = from_byte(gather(ptr, ix));
2877 }
2878 HIGHP_STAGE(store_a8, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2879 auto ptr = ptr_at_xy<uint8_t>(ctx, dx,dy);
2880
2881 U8 packed = pack(pack(to_unorm(a, 255)));
2882 store(ptr, packed);
2883 }
2884 HIGHP_STAGE(store_r8, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2885 auto ptr = ptr_at_xy<uint8_t>(ctx, dx,dy);
2886
2887 U8 packed = pack(pack(to_unorm(r, 255)));
2888 store(ptr, packed);
2889 }
2890
2891 HIGHP_STAGE(load_565, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2892 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
2893
2894 from_565(load<U16>(ptr), &r,&g,&b);
2895 a = F1;
2896 }
2897 HIGHP_STAGE(load_565_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2898 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
2899
2900 from_565(load<U16>(ptr), &dr,&dg,&db);
2901 da = F1;
2902 }
2903 HIGHP_STAGE(gather_565, const SkRasterPipelineContexts::GatherCtx* ctx) {
2904 const uint16_t* ptr;
2905 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
2906 from_565(gather_unaligned(ptr, ix), &r,&g,&b);
2907 a = F1;
2908 }
2909 HIGHP_STAGE(store_565, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2910 auto ptr = ptr_at_xy<uint16_t>(ctx, dx,dy);
2911
2912 U16 px = pack( to_unorm(r, 31) << 11
2913 | to_unorm(g, 63) << 5
2914 | to_unorm(b, 31) );
2915 store(ptr, px);
2916 }
2917
2918 HIGHP_STAGE(load_4444, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2919 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
2920 from_4444(load<U16>(ptr), &r,&g,&b,&a);
2921 }
2922 HIGHP_STAGE(load_4444_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2923 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
2924 from_4444(load<U16>(ptr), &dr,&dg,&db,&da);
2925 }
2926 HIGHP_STAGE(gather_4444, const SkRasterPipelineContexts::GatherCtx* ctx) {
2927 const uint16_t* ptr;
2928 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
2929 from_4444(gather_unaligned(ptr, ix), &r,&g,&b,&a);
2930 }
2931 HIGHP_STAGE(store_4444, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2932 auto ptr = ptr_at_xy<uint16_t>(ctx, dx,dy);
2933 U16 px = pack( to_unorm(r, 15) << 12
2934 | to_unorm(g, 15) << 8
2935 | to_unorm(b, 15) << 4
2936 | to_unorm(a, 15) );
2937 store(ptr, px);
2938 }
2939
2940 HIGHP_STAGE(load_8888, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2941 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
2942 from_8888(load<U32>(ptr), &r,&g,&b,&a);
2943 }
2944 HIGHP_STAGE(load_8888_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2945 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
2946 from_8888(load<U32>(ptr), &dr,&dg,&db,&da);
2947 }
2948 HIGHP_STAGE(gather_8888, const SkRasterPipelineContexts::GatherCtx* ctx) {
2949 const uint32_t* ptr;
2950 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
2951 from_8888(gather_unaligned(ptr, ix), &r,&g,&b,&a);
2952 }
2953 HIGHP_STAGE(store_8888, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2954 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
2955
2956 U32 px = to_unorm(r, 255)
2957 | to_unorm(g, 255) << 8
2958 | to_unorm(b, 255) << 16
2959 | to_unorm(a, 255) << 24;
2960 store(ptr, px);
2961 }
2962
2963 HIGHP_STAGE(load_rg88, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2964 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx, dy);
2965 from_88(load<U16>(ptr), &r, &g);
2966 b = F0;
2967 a = F1;
2968 }
2969 HIGHP_STAGE(load_rg88_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2970 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx, dy);
2971 from_88(load<U16>(ptr), &dr, &dg);
2972 db = F0;
2973 da = F1;
2974 }
2975 HIGHP_STAGE(gather_rg88, const SkRasterPipelineContexts::GatherCtx* ctx) {
2976 const uint16_t* ptr;
2977 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
2978 from_88(gather_unaligned(ptr, ix), &r, &g);
2979 b = F0;
2980 a = F1;
2981 }
2982 HIGHP_STAGE(store_rg88, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2983 auto ptr = ptr_at_xy<uint16_t>(ctx, dx, dy);
2984 U16 px = pack( to_unorm(r, 255) | to_unorm(g, 255) << 8 );
2985 store(ptr, px);
2986 }
2987
2988 HIGHP_STAGE(load_a16, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2989 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
2990 r = g = b = F0;
2991 a = from_short(load<U16>(ptr));
2992 }
2993 HIGHP_STAGE(load_a16_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
2994 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx, dy);
2995 dr = dg = db = F0;
2996 da = from_short(load<U16>(ptr));
2997 }
2998 HIGHP_STAGE(gather_a16, const SkRasterPipelineContexts::GatherCtx* ctx) {
2999 const uint16_t* ptr;
3000 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
3001 r = g = b = F0;
3002 a = from_short(gather_unaligned(ptr, ix));
3003 }
3004 HIGHP_STAGE(store_a16, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3005 auto ptr = ptr_at_xy<uint16_t>(ctx, dx,dy);
3006
3007 U16 px = pack(to_unorm(a, 65535));
3008 store(ptr, px);
3009 }
3010
3011 HIGHP_STAGE(load_r16, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3012 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx, dy);
3013 g = b = F0;
3014 a = F1;
3015 r = from_short(load<U16>(ptr));
3016 }
3017 HIGHP_STAGE(load_r16_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3018 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx, dy);
3019 dg = db = F0;
3020 da = F1;
3021 dr = from_short(load<U16>(ptr));
3022 }
3023 HIGHP_STAGE(gather_r16, const SkRasterPipelineContexts::GatherCtx* ctx) {
3024 const uint16_t* ptr;
3025 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
3026 g = b = F0;
3027 a = F1;
3028 r = from_short(gather_unaligned(ptr, ix));
3029 }
3030 HIGHP_STAGE(store_r16, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3031 auto ptr = ptr_at_xy<uint16_t>(ctx, dx, dy);
3032
3033 U16 px = pack(to_unorm(r, 65535));
3034 store(ptr, px);
3035 }
3036
3037 HIGHP_STAGE(load_rg1616, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3038 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx, dy);
3039 b = F0;
3040 a = F1;
3041 from_1616(load<U32>(ptr), &r,&g);
3042 }
3043 HIGHP_STAGE(load_rg1616_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3044 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx, dy);
3045 from_1616(load<U32>(ptr), &dr, &dg);
3046 db = F0;
3047 da = F1;
3048 }
3049 HIGHP_STAGE(gather_rg1616, const SkRasterPipelineContexts::GatherCtx* ctx) {
3050 const uint32_t* ptr;
3051 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
3052 from_1616(gather_unaligned(ptr, ix), &r, &g);
3053 b = F0;
3054 a = F1;
3055 }
3056 HIGHP_STAGE(store_rg1616, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3057 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
3058
3059 U32 px = to_unorm(r, 65535)
3060 | to_unorm(g, 65535) << 16;
3061 store(ptr, px);
3062 }
3063
3064 HIGHP_STAGE(load_16161616, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3065 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx, dy);
3066 from_16161616(load<U64>(ptr), &r,&g, &b, &a);
3067 }
3068 HIGHP_STAGE(load_16161616_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3069 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx, dy);
3070 from_16161616(load<U64>(ptr), &dr, &dg, &db, &da);
3071 }
3072 HIGHP_STAGE(gather_16161616, const SkRasterPipelineContexts::GatherCtx* ctx) {
3073 const uint64_t* ptr;
3074 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
3075 from_16161616(gather_unaligned(ptr, ix), &r, &g, &b, &a);
3076 }
3077 HIGHP_STAGE(store_16161616, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3078 auto ptr = ptr_at_xy<uint16_t>(ctx, 4*dx,4*dy);
3079
3080 U16 R = pack(to_unorm(r, 65535)),
3081 G = pack(to_unorm(g, 65535)),
3082 B = pack(to_unorm(b, 65535)),
3083 A = pack(to_unorm(a, 65535));
3084
3085 store4(ptr, R,G,B,A);
3086 }
3087
3088 HIGHP_STAGE(load_10x6, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3089 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx, dy);
3090 from_10x6(load<U64>(ptr), &r,&g, &b, &a);
3091 }
3092 HIGHP_STAGE(load_10x6_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3093 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx, dy);
3094 from_10x6(load<U64>(ptr), &dr, &dg, &db, &da);
3095 }
3096 HIGHP_STAGE(gather_10x6, const SkRasterPipelineContexts::GatherCtx* ctx) {
3097 const uint64_t* ptr;
3098 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
3099 from_10x6(gather_unaligned(ptr, ix), &r, &g, &b, &a);
3100 }
3101 HIGHP_STAGE(store_10x6, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3102 auto ptr = ptr_at_xy<uint16_t>(ctx, 4*dx,4*dy);
3103
3104 U16 R = pack(to_unorm(r, 1023)) << 6,
3105 G = pack(to_unorm(g, 1023)) << 6,
3106 B = pack(to_unorm(b, 1023)) << 6,
3107 A = pack(to_unorm(a, 1023)) << 6;
3108
3109 store4(ptr, R,G,B,A);
3110 }
3111
3112 HIGHP_STAGE(load_1010102, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3113 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
3114 from_1010102(load<U32>(ptr), &r,&g,&b,&a);
3115 }
3116 HIGHP_STAGE(load_1010102_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3117 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
3118 from_1010102(load<U32>(ptr), &dr,&dg,&db,&da);
3119 }
3120 HIGHP_STAGE(load_1010102_xr, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3121 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
3122 from_1010102_xr(load<U32>(ptr), &r,&g,&b,&a);
3123 }
3124 HIGHP_STAGE(load_1010102_xr_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3125 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
3126 from_1010102_xr(load<U32>(ptr), &dr,&dg,&db,&da);
3127 }
3128 HIGHP_STAGE(gather_1010102, const SkRasterPipelineContexts::GatherCtx* ctx) {
3129 const uint32_t* ptr;
3130 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
3131 from_1010102(gather_unaligned(ptr, ix), &r,&g,&b,&a);
3132 }
3133 HIGHP_STAGE(gather_1010102_xr, const SkRasterPipelineContexts::GatherCtx* ctx) {
3134 const uint32_t* ptr;
3135 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
3136 from_1010102_xr(gather_unaligned(ptr, ix), &r,&g,&b,&a);
3137 }
3138 HIGHP_STAGE(gather_10101010_xr, const SkRasterPipelineContexts::GatherCtx* ctx) {
3139 const uint64_t* ptr;
3140 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
3141 from_10101010_xr(gather_unaligned(ptr, ix), &r, &g, &b, &a);
3142 }
3143 HIGHP_STAGE(load_10101010_xr, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3144 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx, dy);
3145 from_10101010_xr(load<U64>(ptr), &r,&g, &b, &a);
3146 }
3147 HIGHP_STAGE(load_10101010_xr_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3148 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx, dy);
3149 from_10101010_xr(load<U64>(ptr), &dr, &dg, &db, &da);
3150 }
3151 HIGHP_STAGE(store_10101010_xr, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3152 auto ptr = ptr_at_xy<uint16_t>(ctx, 4*dx,4*dy);
3153
3154 // This is the inverse of from_10101010_xr, e.g. (v * 510 + 384)
3155 U16 R = pack(to_unorm(r, /*scale=*/510, /*bias=*/384, /*maxI=*/1023)) << 6,
3156 G = pack(to_unorm(g, /*scale=*/510, /*bias=*/384, /*maxI=*/1023)) << 6,
3157 B = pack(to_unorm(b, /*scale=*/510, /*bias=*/384, /*maxI=*/1023)) << 6,
3158 A = pack(to_unorm(a, /*scale=*/510, /*bias=*/384, /*maxI=*/1023)) << 6;
3159
3160 store4(ptr, R,G,B,A);
3161 }
3162 HIGHP_STAGE(store_1010102, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3163 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
3164
3165 U32 px = to_unorm(r, 1023)
3166 | to_unorm(g, 1023) << 10
3167 | to_unorm(b, 1023) << 20
3168 | to_unorm(a, 3) << 30;
3169 store(ptr, px);
3170 }
3171 HIGHP_STAGE(store_1010102_xr, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3172 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
3173
3174 // This is the inverse of from_1010102_xr, e.g. (v * 510 + 384)
3175 U32 px = to_unorm(r, /*scale=*/510, /*bias=*/384, /*maxI=*/1023)
3176 | to_unorm(g, /*scale=*/510, /*bias=*/384, /*maxI=*/1023) << 10
3177 | to_unorm(b, /*scale=*/510, /*bias=*/384, /*maxI=*/1023) << 20
3178 | to_unorm(a, /*scale=*/3) << 30;
3179 store(ptr, px);
3180 }
3181
3182 HIGHP_STAGE(load_f16, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3183 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx,dy);
3184
3185 U16 R,G,B,A;
3186 load4((const uint16_t*)ptr, &R,&G,&B,&A);
3187 r = from_half(R);
3188 g = from_half(G);
3189 b = from_half(B);
3190 a = from_half(A);
3191 }
3192 HIGHP_STAGE(load_f16_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3193 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx,dy);
3194
3195 U16 R,G,B,A;
3196 load4((const uint16_t*)ptr, &R,&G,&B,&A);
3197 dr = from_half(R);
3198 dg = from_half(G);
3199 db = from_half(B);
3200 da = from_half(A);
3201 }
3202 HIGHP_STAGE(gather_f16, const SkRasterPipelineContexts::GatherCtx* ctx) {
3203 const uint64_t* ptr;
3204 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
3205 auto px = gather_unaligned(ptr, ix);
3206
3207 U16 R,G,B,A;
3208 load4((const uint16_t*)&px, &R,&G,&B,&A);
3209 r = from_half(R);
3210 g = from_half(G);
3211 b = from_half(B);
3212 a = from_half(A);
3213 }
3214 HIGHP_STAGE(store_f16, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3215 auto ptr = ptr_at_xy<uint64_t>(ctx, dx,dy);
3216 store4((uint16_t*)ptr, to_half(r)
3217 , to_half(g)
3218 , to_half(b)
3219 , to_half(a));
3220 }
3221
3222 HIGHP_STAGE(load_af16, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3223 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
3224
3225 U16 A = load<U16>((const uint16_t*)ptr);
3226 r = F0;
3227 g = F0;
3228 b = F0;
3229 a = from_half(A);
3230 }
3231 HIGHP_STAGE(load_af16_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3232 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx, dy);
3233
3234 U16 A = load<U16>((const uint16_t*)ptr);
3235 dr = dg = db = F0;
3236 da = from_half(A);
3237 }
3238 HIGHP_STAGE(gather_af16, const SkRasterPipelineContexts::GatherCtx* ctx) {
3239 const uint16_t* ptr;
3240 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
3241 r = g = b = F0;
3242 a = from_half(gather_unaligned(ptr, ix));
3243 }
3244 HIGHP_STAGE(store_af16, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3245 auto ptr = ptr_at_xy<uint16_t>(ctx, dx,dy);
3246 store(ptr, to_half(a));
3247 }
3248
3249 HIGHP_STAGE(load_rgf16, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3250 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx, dy);
3251
3252 U16 R,G;
3253 load2((const uint16_t*)ptr, &R, &G);
3254 r = from_half(R);
3255 g = from_half(G);
3256 b = F0;
3257 a = F1;
3258 }
3259 HIGHP_STAGE(load_rgf16_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3260 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx, dy);
3261
3262 U16 R,G;
3263 load2((const uint16_t*)ptr, &R, &G);
3264 dr = from_half(R);
3265 dg = from_half(G);
3266 db = F0;
3267 da = F1;
3268 }
3269 HIGHP_STAGE(gather_rgf16, const SkRasterPipelineContexts::GatherCtx* ctx) {
3270 const uint32_t* ptr;
3271 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
3272 auto px = gather_unaligned(ptr, ix);
3273
3274 U16 R,G;
3275 load2((const uint16_t*)&px, &R, &G);
3276 r = from_half(R);
3277 g = from_half(G);
3278 b = F0;
3279 a = F1;
3280 }
3281 HIGHP_STAGE(store_rgf16, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3282 auto ptr = ptr_at_xy<uint32_t>(ctx, dx, dy);
3283 store2((uint16_t*)ptr, to_half(r)
3284 , to_half(g));
3285 }
3286
3287 HIGHP_STAGE(load_f32, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3288 auto ptr = ptr_at_xy<const float>(ctx, 4*dx,4*dy);
3289 load4(ptr, &r,&g,&b,&a);
3290 }
3291 HIGHP_STAGE(load_f32_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3292 auto ptr = ptr_at_xy<const float>(ctx, 4*dx,4*dy);
3293 load4(ptr, &dr,&dg,&db,&da);
3294 }
3295 HIGHP_STAGE(gather_f32, const SkRasterPipelineContexts::GatherCtx* ctx) {
3296 const float* ptr;
3297 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
3298 r = gather_unaligned(ptr, 4*ix + 0);
3299 g = gather_unaligned(ptr, 4*ix + 1);
3300 b = gather_unaligned(ptr, 4*ix + 2);
3301 a = gather_unaligned(ptr, 4*ix + 3);
3302 }
3303 HIGHP_STAGE(store_f32, const SkRasterPipelineContexts::MemoryCtx* ctx) {
3304 auto ptr = ptr_at_xy<float>(ctx, 4*dx,4*dy);
3305 store4(ptr, r,g,b,a);
3306 }
3307
3308 SI F exclusive_repeat(F v, const SkRasterPipelineContexts::TileCtx* ctx) {
3309 return v - floor_(v*ctx->invScale)*ctx->scale;
3310 }
3311 SI F exclusive_mirror(F v, const SkRasterPipelineContexts::TileCtx* ctx) {
3312 auto limit = ctx->scale;
3313 auto invLimit = ctx->invScale;
3314
3315 // This is "repeat" over the range 0..2*limit
3316 auto u = v - floor_(v*invLimit*0.5f)*2*limit;
3317 // s will be 0 when moving forward (e.g. [0, limit)) and 1 when moving backward (e.g.
3318 // [limit, 2*limit)).
3319 auto s = floor_(u*invLimit);
3320 // This is the mirror result.
3321 auto m = u - 2*s*(u - limit);
3322 // Apply a bias to m if moving backwards so that we snap consistently at exact integer coords in
3323 // the logical infinite image. This is tested by mirror_tile GM. Note that all values
3324 // that have a non-zero bias applied are > 0.
3325 auto biasInUlps = trunc_(s);
3326 return sk_bit_cast<F>(sk_bit_cast<U32>(m) + ctx->mirrorBiasDir*biasInUlps);
3327 }
3328 // Tile x or y to [0,limit) == [0,limit - 1 ulp] (think, sampling from images).
3329 // The gather stages will hard clamp the output of these stages to [0,limit)...
3330 // we just need to do the basic repeat or mirroring.
3331 HIGHP_STAGE(repeat_x, const SkRasterPipelineContexts::TileCtx* ctx) {
3332 r = exclusive_repeat(r, ctx);
3333 }
3334 HIGHP_STAGE(repeat_y, const SkRasterPipelineContexts::TileCtx* ctx) {
3335 g = exclusive_repeat(g, ctx);
3336 }
3337 HIGHP_STAGE(mirror_x, const SkRasterPipelineContexts::TileCtx* ctx) {
3338 r = exclusive_mirror(r, ctx);
3339 }
3340 HIGHP_STAGE(mirror_y, const SkRasterPipelineContexts::TileCtx* ctx) {
3341 g = exclusive_mirror(g, ctx);
3342 }
3343
3344 HIGHP_STAGE( clamp_x_1, NoCtx) { r = clamp_01_(r); }
3345 HIGHP_STAGE(repeat_x_1, NoCtx) { r = clamp_01_(r - floor_(r)); }
3346 HIGHP_STAGE(mirror_x_1, NoCtx) { r = clamp_01_(abs_( (r-1.0f) - two(floor_((r-1.0f)*0.5f)) - 1.0f )); }
3347
3348 HIGHP_STAGE(clamp_x_and_y, const SkRasterPipelineContexts::CoordClampCtx* ctx) {
3349 r = min(ctx->max_x, max(ctx->min_x, r));
3350 g = min(ctx->max_y, max(ctx->min_y, g));
3351 }
3352
3353 // Decal stores a 32bit mask after checking the coordinate (x and/or y) against its domain:
3354 // mask == 0x00000000 if the coordinate(s) are out of bounds
3355 // mask == 0xFFFFFFFF if the coordinate(s) are in bounds
3356 // After the gather stage, the r,g,b,a values are AND'd with this mask, setting them to 0
3357 // if either of the coordinates were out of bounds.
3358
3359 HIGHP_STAGE(decal_x, SkRasterPipelineContexts::DecalTileCtx* ctx) {
3360 auto w = ctx->limit_x;
3361 auto e = ctx->inclusiveEdge_x;
3362 auto cond = ((0 < r) & (r < w)) | (r == e);
3363 sk_unaligned_store(ctx->mask, cond_to_mask(cond));
3364 }
3365 HIGHP_STAGE(decal_y, SkRasterPipelineContexts::DecalTileCtx* ctx) {
3366 auto h = ctx->limit_y;
3367 auto e = ctx->inclusiveEdge_y;
3368 auto cond = ((0 < g) & (g < h)) | (g == e);
3369 sk_unaligned_store(ctx->mask, cond_to_mask(cond));
3370 }
3371 HIGHP_STAGE(decal_x_and_y, SkRasterPipelineContexts::DecalTileCtx* ctx) {
3372 auto w = ctx->limit_x;
3373 auto h = ctx->limit_y;
3374 auto ex = ctx->inclusiveEdge_x;
3375 auto ey = ctx->inclusiveEdge_y;
3376 auto cond = (((0 < r) & (r < w)) | (r == ex))
3377 & (((0 < g) & (g < h)) | (g == ey));
3378 sk_unaligned_store(ctx->mask, cond_to_mask(cond));
3379 }
3380 HIGHP_STAGE(check_decal_mask, SkRasterPipelineContexts::DecalTileCtx* ctx) {
3381 auto mask = sk_unaligned_load<U32>(ctx->mask);
3382 r = sk_bit_cast<F>(sk_bit_cast<U32>(r) & mask);
3383 g = sk_bit_cast<F>(sk_bit_cast<U32>(g) & mask);
3384 b = sk_bit_cast<F>(sk_bit_cast<U32>(b) & mask);
3385 a = sk_bit_cast<F>(sk_bit_cast<U32>(a) & mask);
3386 }
3387
3388 HIGHP_STAGE(alpha_to_gray, NoCtx) {
3389 r = g = b = a;
3390 a = F1;
3391 }
3392 HIGHP_STAGE(alpha_to_gray_dst, NoCtx) {
3393 dr = dg = db = da;
3394 da = F1;
3395 }
3396 HIGHP_STAGE(alpha_to_red, NoCtx) {
3397 r = a;
3398 a = F1;
3399 }
3400 HIGHP_STAGE(alpha_to_red_dst, NoCtx) {
3401 dr = da;
3402 da = F1;
3403 }
3404
3405 HIGHP_STAGE(bt709_luminance_or_luma_to_alpha, NoCtx) {
3406 a = r*0.2126f + g*0.7152f + b*0.0722f;
3407 r = g = b = F0;
3408 }
3409 HIGHP_STAGE(bt709_luminance_or_luma_to_rgb, NoCtx) {
3410 r = g = b = r*0.2126f + g*0.7152f + b*0.0722f;
3411 }
3412
3413 HIGHP_STAGE(matrix_translate, const float* m) {
3414 r += m[0];
3415 g += m[1];
3416 }
3417 HIGHP_STAGE(matrix_scale_translate, const float* m) {
3418 r = mad(r,m[0], m[2]);
3419 g = mad(g,m[1], m[3]);
3420 }
3421 HIGHP_STAGE(matrix_2x3, const float* m) {
3422 auto R = mad(r,m[0], mad(g,m[1], m[2])),
3423 G = mad(r,m[3], mad(g,m[4], m[5]));
3424 r = R;
3425 g = G;
3426 }
3427 HIGHP_STAGE(matrix_3x3, const float* m) {
3428 auto R = mad(r,m[0], mad(g,m[3], b*m[6])),
3429 G = mad(r,m[1], mad(g,m[4], b*m[7])),
3430 B = mad(r,m[2], mad(g,m[5], b*m[8]));
3431 r = R;
3432 g = G;
3433 b = B;
3434 }
3435 HIGHP_STAGE(matrix_3x4, const float* m) {
3436 auto R = mad(r,m[0], mad(g,m[3], mad(b,m[6], m[ 9]))),
3437 G = mad(r,m[1], mad(g,m[4], mad(b,m[7], m[10]))),
3438 B = mad(r,m[2], mad(g,m[5], mad(b,m[8], m[11])));
3439 r = R;
3440 g = G;
3441 b = B;
3442 }
3443 HIGHP_STAGE(matrix_4x5, const float* m) {
3444 auto R = mad(r,m[ 0], mad(g,m[ 1], mad(b,m[ 2], mad(a,m[ 3], m[ 4])))),
3445 G = mad(r,m[ 5], mad(g,m[ 6], mad(b,m[ 7], mad(a,m[ 8], m[ 9])))),
3446 B = mad(r,m[10], mad(g,m[11], mad(b,m[12], mad(a,m[13], m[14])))),
3447 A = mad(r,m[15], mad(g,m[16], mad(b,m[17], mad(a,m[18], m[19]))));
3448 r = R;
3449 g = G;
3450 b = B;
3451 a = A;
3452 }
3453 HIGHP_STAGE(matrix_4x3, const float* m) {
3454 auto X = r,
3455 Y = g;
3456
3457 r = mad(X, m[0], mad(Y, m[4], m[ 8]));
3458 g = mad(X, m[1], mad(Y, m[5], m[ 9]));
3459 b = mad(X, m[2], mad(Y, m[6], m[10]));
3460 a = mad(X, m[3], mad(Y, m[7], m[11]));
3461 }
3462 HIGHP_STAGE(matrix_perspective, const float* m) {
3463 // N.B. Unlike the other matrix_ stages, this matrix is row-major.
3464 auto R = mad(r,m[0], mad(g,m[1], m[2])),
3465 G = mad(r,m[3], mad(g,m[4], m[5])),
3466 Z = mad(r,m[6], mad(g,m[7], m[8]));
3467 r = R * rcp_precise(Z);
3468 g = G * rcp_precise(Z);
3469 }
3470
3471 SI void gradient_lookup(const SkRasterPipelineContexts::GradientCtx* c, U32 idx, F t,
3472 F* r, F* g, F* b, F* a) {
3473 F fr, br, fg, bg, fb, bb, fa, ba;
3474 #if defined(SKRP_CPU_HSW)
3475 if (c->stopCount <=8) {
3476 fr = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->factors[0]), (__m256i)idx);
3477 br = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->biases[0]), (__m256i)idx);
3478 fg = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->factors[1]), (__m256i)idx);
3479 bg = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->biases[1]), (__m256i)idx);
3480 fb = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->factors[2]), (__m256i)idx);
3481 bb = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->biases[2]), (__m256i)idx);
3482 fa = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->factors[3]), (__m256i)idx);
3483 ba = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->biases[3]), (__m256i)idx);
3484 } else
3485 #elif defined(SKRP_CPU_LASX)
3486 if (c->stopCount <= 8) {
3487 fr = (__m256)__lasx_xvperm_w(__lasx_xvld(c->factors[0], 0), idx);
3488 br = (__m256)__lasx_xvperm_w(__lasx_xvld(c->biases[0], 0), idx);
3489 fg = (__m256)__lasx_xvperm_w(__lasx_xvld(c->factors[1], 0), idx);
3490 bg = (__m256)__lasx_xvperm_w(__lasx_xvld(c->biases[1], 0), idx);
3491 fb = (__m256)__lasx_xvperm_w(__lasx_xvld(c->factors[2], 0), idx);
3492 bb = (__m256)__lasx_xvperm_w(__lasx_xvld(c->biases[2], 0), idx);
3493 fa = (__m256)__lasx_xvperm_w(__lasx_xvld(c->factors[3], 0), idx);
3494 ba = (__m256)__lasx_xvperm_w(__lasx_xvld(c->biases[3], 0), idx);
3495 } else
3496 #elif defined(SKRP_CPU_LSX)
3497 if (c->stopCount <= 4) {
3498 __m128i zero = __lsx_vldi(0);
3499 fr = (__m128)__lsx_vshuf_w(idx, zero, __lsx_vld(c->factors[0], 0));
3500 br = (__m128)__lsx_vshuf_w(idx, zero, __lsx_vld(c->biases[0], 0));
3501 fg = (__m128)__lsx_vshuf_w(idx, zero, __lsx_vld(c->factors[1], 0));
3502 bg = (__m128)__lsx_vshuf_w(idx, zero, __lsx_vld(c->biases[1], 0));
3503 fb = (__m128)__lsx_vshuf_w(idx, zero, __lsx_vld(c->factors[2], 0));
3504 bb = (__m128)__lsx_vshuf_w(idx, zero, __lsx_vld(c->biases[2], 0));
3505 fa = (__m128)__lsx_vshuf_w(idx, zero, __lsx_vld(c->factors[3], 0));
3506 ba = (__m128)__lsx_vshuf_w(idx, zero, __lsx_vld(c->biases[3], 0));
3507 } else
3508 #endif
3509 {
3510 #if defined(SKRP_CPU_LSX)
3511 // This can reduce some vpickve2gr instructions.
3512 int i0 = __lsx_vpickve2gr_w(idx, 0);
3513 int i1 = __lsx_vpickve2gr_w(idx, 1);
3514 int i2 = __lsx_vpickve2gr_w(idx, 2);
3515 int i3 = __lsx_vpickve2gr_w(idx, 3);
3516 fr = gather((int *)c->factors[0], i0, i1, i2, i3);
3517 br = gather((int *)c->biases[0], i0, i1, i2, i3);
3518 fg = gather((int *)c->factors[1], i0, i1, i2, i3);
3519 bg = gather((int *)c->biases[1], i0, i1, i2, i3);
3520 fb = gather((int *)c->factors[2], i0, i1, i2, i3);
3521 bb = gather((int *)c->biases[2], i0, i1, i2, i3);
3522 fa = gather((int *)c->factors[3], i0, i1, i2, i3);
3523 ba = gather((int *)c->biases[3], i0, i1, i2, i3);
3524 #else
3525 fr = gather(c->factors[0], idx);
3526 br = gather(c->biases[0], idx);
3527 fg = gather(c->factors[1], idx);
3528 bg = gather(c->biases[1], idx);
3529 fb = gather(c->factors[2], idx);
3530 bb = gather(c->biases[2], idx);
3531 fa = gather(c->factors[3], idx);
3532 ba = gather(c->biases[3], idx);
3533 #endif
3534 }
3535
3536 *r = mad(t, fr, br);
3537 *g = mad(t, fg, bg);
3538 *b = mad(t, fb, bb);
3539 *a = mad(t, fa, ba);
3540 }
3541
3542 HIGHP_STAGE(evenly_spaced_gradient, const SkRasterPipelineContexts::GradientCtx* c) {
3543 auto t = r;
3544 auto idx = trunc_(t * static_cast<float>(c->stopCount-1));
3545 gradient_lookup(c, idx, t, &r, &g, &b, &a);
3546 }
3547
3548 HIGHP_STAGE(gradient, const SkRasterPipelineContexts::GradientCtx* c) {
3549 auto t = r;
3550 U32 idx = U32_(0);
3551
3552 // N.B. The loop starts at 1 because idx 0 is the color to use before the first stop.
3553 for (size_t i = 1; i < c->stopCount; i++) {
3554 idx += (U32)if_then_else(t >= c->ts[i], I32_(1), I32_(0));
3555 }
3556
3557 gradient_lookup(c, idx, t, &r, &g, &b, &a);
3558 }
3559
3560 HIGHP_STAGE(evenly_spaced_2_stop_gradient,
3561 const SkRasterPipelineContexts::EvenlySpaced2StopGradientCtx* c) {
3562 auto t = r;
3563 r = mad(t, c->factor[0], c->bias[0]);
3564 g = mad(t, c->factor[1], c->bias[1]);
3565 b = mad(t, c->factor[2], c->bias[2]);
3566 a = mad(t, c->factor[3], c->bias[3]);
3567 }
3568
3569 HIGHP_STAGE(xy_to_unit_angle, NoCtx) {
3570 F X = r,
3571 Y = g;
3572 F xabs = abs_(X),
3573 yabs = abs_(Y);
3574
3575 F slope = min(xabs, yabs)/max(xabs, yabs);
3576 F s = slope * slope;
3577
3578 // Use a 7th degree polynomial to approximate atan.
3579 // This was generated using sollya.gforge.inria.fr.
3580 // A float optimized polynomial was generated using the following command.
3581 // P1 = fpminimax((1/(2*Pi))*atan(x),[|1,3,5,7|],[|24...|],[2^(-40),1],relative);
3582 F phi = slope
3583 * (0.15912117063999176025390625f + s
3584 * (-5.185396969318389892578125e-2f + s
3585 * (2.476101927459239959716796875e-2f + s
3586 * (-7.0547382347285747528076171875e-3f))));
3587
3588 phi = if_then_else(xabs < yabs, 1.0f/4.0f - phi, phi);
3589 phi = if_then_else(X < 0.0f , 1.0f/2.0f - phi, phi);
3590 phi = if_then_else(Y < 0.0f , 1.0f - phi , phi);
3591 phi = if_then_else(phi != phi , 0.0f , phi); // Check for NaN.
3592 r = phi;
3593 }
3594
3595 HIGHP_STAGE(xy_to_radius, NoCtx) {
3596 F X2 = r * r,
3597 Y2 = g * g;
3598 r = sqrt_(X2 + Y2);
3599 }
3600
3601 // Please see https://skia.org/dev/design/conical for how our 2pt conical shader works.
3602
3603 HIGHP_STAGE(negate_x, NoCtx) { r = -r; }
3604
3605 HIGHP_STAGE(xy_to_2pt_conical_strip, const SkRasterPipelineContexts::Conical2PtCtx* ctx) {
3606 F x = r, y = g, &t = r;
3607 t = x + sqrt_(ctx->fP0 - y*y); // ctx->fP0 = r0 * r0
3608 }
3609
3610 HIGHP_STAGE(xy_to_2pt_conical_focal_on_circle, NoCtx) {
3611 F x = r, y = g, &t = r;
3612 t = x + y*y / x; // (x^2 + y^2) / x
3613 }
3614
3615 HIGHP_STAGE(xy_to_2pt_conical_well_behaved, const SkRasterPipelineContexts::Conical2PtCtx* ctx) {
3616 F x = r, y = g, &t = r;
3617 t = sqrt_(x*x + y*y) - x * ctx->fP0; // ctx->fP0 = 1/r1
3618 }
3619
3620 HIGHP_STAGE(xy_to_2pt_conical_greater, const SkRasterPipelineContexts::Conical2PtCtx* ctx) {
3621 F x = r, y = g, &t = r;
3622 t = sqrt_(x*x - y*y) - x * ctx->fP0; // ctx->fP0 = 1/r1
3623 }
3624
3625 HIGHP_STAGE(xy_to_2pt_conical_smaller, const SkRasterPipelineContexts::Conical2PtCtx* ctx) {
3626 F x = r, y = g, &t = r;
3627 t = -sqrt_(x*x - y*y) - x * ctx->fP0; // ctx->fP0 = 1/r1
3628 }
3629
3630 HIGHP_STAGE(alter_2pt_conical_compensate_focal,
3631 const SkRasterPipelineContexts::Conical2PtCtx* ctx) {
3632 F& t = r;
3633 t = t + ctx->fP1; // ctx->fP1 = f
3634 }
3635
3636 HIGHP_STAGE(alter_2pt_conical_unswap, NoCtx) {
3637 F& t = r;
3638 t = 1 - t;
3639 }
3640
3641 HIGHP_STAGE(mask_2pt_conical_nan, SkRasterPipelineContexts::Conical2PtCtx* c) {
3642 F& t = r;
3643 auto is_degenerate = (t != t); // NaN
3644 t = if_then_else(is_degenerate, F0, t);
3645 sk_unaligned_store(&c->fMask, cond_to_mask(!is_degenerate));
3646 }
3647
3648 HIGHP_STAGE(mask_2pt_conical_degenerates, SkRasterPipelineContexts::Conical2PtCtx* c) {
3649 F& t = r;
3650 auto is_degenerate = (t <= 0) | (t != t);
3651 t = if_then_else(is_degenerate, F0, t);
3652 sk_unaligned_store(&c->fMask, cond_to_mask(!is_degenerate));
3653 }
3654
3655 HIGHP_STAGE(apply_vector_mask, const uint32_t* ctx) {
3656 const U32 mask = sk_unaligned_load<U32>(ctx);
3657 r = sk_bit_cast<F>(sk_bit_cast<U32>(r) & mask);
3658 g = sk_bit_cast<F>(sk_bit_cast<U32>(g) & mask);
3659 b = sk_bit_cast<F>(sk_bit_cast<U32>(b) & mask);
3660 a = sk_bit_cast<F>(sk_bit_cast<U32>(a) & mask);
3661 }
3662
3663 SI void save_xy(F* r, F* g, SkRasterPipelineContexts::SamplerCtx* c) {
3664 // Whether bilinear or bicubic, all sample points are at the same fractional offset (fx,fy).
3665 // They're either the 4 corners of a logical 1x1 pixel or the 16 corners of a 3x3 grid
3666 // surrounding (x,y) at (0.5,0.5) off-center.
3667 F fx = fract(*r + 0.5f),
3668 fy = fract(*g + 0.5f);
3669
3670 // Samplers will need to load x and fx, or y and fy.
3671 sk_unaligned_store(c->x, *r);
3672 sk_unaligned_store(c->y, *g);
3673 sk_unaligned_store(c->fx, fx);
3674 sk_unaligned_store(c->fy, fy);
3675 }
3676
3677 HIGHP_STAGE(accumulate, const SkRasterPipelineContexts::SamplerCtx* c) {
3678 // Bilinear and bicubic filters are both separable, so we produce independent contributions
3679 // from x and y, multiplying them together here to get each pixel's total scale factor.
3680 auto scale = sk_unaligned_load<F>(c->scalex)
3681 * sk_unaligned_load<F>(c->scaley);
3682 dr = mad(scale, r, dr);
3683 dg = mad(scale, g, dg);
3684 db = mad(scale, b, db);
3685 da = mad(scale, a, da);
3686 }
3687
3688 // In bilinear interpolation, the 4 pixels at +/- 0.5 offsets from the sample pixel center
3689 // are combined in direct proportion to their area overlapping that logical query pixel.
3690 // At positive offsets, the x-axis contribution to that rectangle is fx, or (1-fx) at negative x.
3691 // The y-axis is symmetric.
3692
3693 template <int kScale>
3694 SI void bilinear_x(SkRasterPipelineContexts::SamplerCtx* ctx, F* x) {
3695 *x = sk_unaligned_load<F>(ctx->x) + (kScale * 0.5f);
3696 F fx = sk_unaligned_load<F>(ctx->fx);
3697
3698 F scalex;
3699 if (kScale == -1) { scalex = 1.0f - fx; }
3700 if (kScale == +1) { scalex = fx; }
3701 sk_unaligned_store(ctx->scalex, scalex);
3702 }
3703 template <int kScale>
3704 SI void bilinear_y(SkRasterPipelineContexts::SamplerCtx* ctx, F* y) {
3705 *y = sk_unaligned_load<F>(ctx->y) + (kScale * 0.5f);
3706 F fy = sk_unaligned_load<F>(ctx->fy);
3707
3708 F scaley;
3709 if (kScale == -1) { scaley = 1.0f - fy; }
3710 if (kScale == +1) { scaley = fy; }
3711 sk_unaligned_store(ctx->scaley, scaley);
3712 }
3713
3714 HIGHP_STAGE(bilinear_setup, SkRasterPipelineContexts::SamplerCtx* ctx) {
3715 save_xy(&r, &g, ctx);
3716 // Init for accumulate
3717 dr = dg = db = da = F0;
3718 }
3719
3720 HIGHP_STAGE(bilinear_nx, SkRasterPipelineContexts::SamplerCtx* ctx) { bilinear_x<-1>(ctx, &r); }
3721 HIGHP_STAGE(bilinear_px, SkRasterPipelineContexts::SamplerCtx* ctx) { bilinear_x<+1>(ctx, &r); }
3722 HIGHP_STAGE(bilinear_ny, SkRasterPipelineContexts::SamplerCtx* ctx) { bilinear_y<-1>(ctx, &g); }
3723 HIGHP_STAGE(bilinear_py, SkRasterPipelineContexts::SamplerCtx* ctx) { bilinear_y<+1>(ctx, &g); }
3724
3725 // In bicubic interpolation, the 16 pixels and +/- 0.5 and +/- 1.5 offsets from the sample
3726 // pixel center are combined with a non-uniform cubic filter, with higher values near the center.
3727 //
3728 // This helper computes the total weight along one axis (our bicubic filter is separable), given one
3729 // column of the sampling matrix, and a fractional pixel offset. See SkCubicResampler for details.
3730
3731 SI F bicubic_wts(F t, float A, float B, float C, float D) {
3732 return mad(t, mad(t, mad(t, D, C), B), A);
3733 }
3734
3735 template <int kScale>
3736 SI void bicubic_x(SkRasterPipelineContexts::SamplerCtx* ctx, F* x) {
3737 *x = sk_unaligned_load<F>(ctx->x) + (kScale * 0.5f);
3738
3739 F scalex;
3740 if (kScale == -3) { scalex = sk_unaligned_load<F>(ctx->wx[0]); }
3741 if (kScale == -1) { scalex = sk_unaligned_load<F>(ctx->wx[1]); }
3742 if (kScale == +1) { scalex = sk_unaligned_load<F>(ctx->wx[2]); }
3743 if (kScale == +3) { scalex = sk_unaligned_load<F>(ctx->wx[3]); }
3744 sk_unaligned_store(ctx->scalex, scalex);
3745 }
3746 template <int kScale>
3747 SI void bicubic_y(SkRasterPipelineContexts::SamplerCtx* ctx, F* y) {
3748 *y = sk_unaligned_load<F>(ctx->y) + (kScale * 0.5f);
3749
3750 F scaley;
3751 if (kScale == -3) { scaley = sk_unaligned_load<F>(ctx->wy[0]); }
3752 if (kScale == -1) { scaley = sk_unaligned_load<F>(ctx->wy[1]); }
3753 if (kScale == +1) { scaley = sk_unaligned_load<F>(ctx->wy[2]); }
3754 if (kScale == +3) { scaley = sk_unaligned_load<F>(ctx->wy[3]); }
3755 sk_unaligned_store(ctx->scaley, scaley);
3756 }
3757
3758 HIGHP_STAGE(bicubic_setup, SkRasterPipelineContexts::SamplerCtx* ctx) {
3759 save_xy(&r, &g, ctx);
3760
3761 const float* w = ctx->weights;
3762
3763 F fx = sk_unaligned_load<F>(ctx->fx);
3764 sk_unaligned_store(ctx->wx[0], bicubic_wts(fx, w[0], w[4], w[ 8], w[12]));
3765 sk_unaligned_store(ctx->wx[1], bicubic_wts(fx, w[1], w[5], w[ 9], w[13]));
3766 sk_unaligned_store(ctx->wx[2], bicubic_wts(fx, w[2], w[6], w[10], w[14]));
3767 sk_unaligned_store(ctx->wx[3], bicubic_wts(fx, w[3], w[7], w[11], w[15]));
3768
3769 F fy = sk_unaligned_load<F>(ctx->fy);
3770 sk_unaligned_store(ctx->wy[0], bicubic_wts(fy, w[0], w[4], w[ 8], w[12]));
3771 sk_unaligned_store(ctx->wy[1], bicubic_wts(fy, w[1], w[5], w[ 9], w[13]));
3772 sk_unaligned_store(ctx->wy[2], bicubic_wts(fy, w[2], w[6], w[10], w[14]));
3773 sk_unaligned_store(ctx->wy[3], bicubic_wts(fy, w[3], w[7], w[11], w[15]));
3774
3775 // Init for accumulate
3776 dr = dg = db = da = F0;
3777 }
3778
3779 HIGHP_STAGE(bicubic_n3x, SkRasterPipelineContexts::SamplerCtx* ctx) { bicubic_x<-3>(ctx, &r); }
3780 HIGHP_STAGE(bicubic_n1x, SkRasterPipelineContexts::SamplerCtx* ctx) { bicubic_x<-1>(ctx, &r); }
3781 HIGHP_STAGE(bicubic_p1x, SkRasterPipelineContexts::SamplerCtx* ctx) { bicubic_x<+1>(ctx, &r); }
3782 HIGHP_STAGE(bicubic_p3x, SkRasterPipelineContexts::SamplerCtx* ctx) { bicubic_x<+3>(ctx, &r); }
3783
3784 HIGHP_STAGE(bicubic_n3y, SkRasterPipelineContexts::SamplerCtx* ctx) { bicubic_y<-3>(ctx, &g); }
3785 HIGHP_STAGE(bicubic_n1y, SkRasterPipelineContexts::SamplerCtx* ctx) { bicubic_y<-1>(ctx, &g); }
3786 HIGHP_STAGE(bicubic_p1y, SkRasterPipelineContexts::SamplerCtx* ctx) { bicubic_y<+1>(ctx, &g); }
3787 HIGHP_STAGE(bicubic_p3y, SkRasterPipelineContexts::SamplerCtx* ctx) { bicubic_y<+3>(ctx, &g); }
3788
3789 SI F compute_perlin_vector(U32 sample, F x, F y) {
3790 // We're relying on the packing of uint16s within a uint32, which will vary based on endianness.
3791 #ifdef SK_CPU_BENDIAN
3792 U32 sampleLo = sample >> 16;
3793 U32 sampleHi = sample & 0xFFFF;
3794 #else
3795 U32 sampleLo = sample & 0xFFFF;
3796 U32 sampleHi = sample >> 16;
3797 #endif
3798
3799 // Convert 32-bit sample value into two floats in the [-1..1] range.
3800 F vecX = mad(cast(sampleLo), 2.0f / 65535.0f, -1.0f);
3801 F vecY = mad(cast(sampleHi), 2.0f / 65535.0f, -1.0f);
3802
3803 // Return the dot of the sample and the passed-in vector.
3804 return mad(vecX, x,
3805 vecY * y);
3806 }
3807
3808 HIGHP_STAGE(perlin_noise, SkRasterPipelineContexts::PerlinNoiseCtx* ctx) {
3809 F noiseVecX = (r + 0.5) * ctx->baseFrequencyX;
3810 F noiseVecY = (g + 0.5) * ctx->baseFrequencyY;
3811 r = g = b = a = F0;
3812 F stitchDataX = F_(ctx->stitchDataInX);
3813 F stitchDataY = F_(ctx->stitchDataInY);
3814 F ratio = F1;
3815
3816 for (int octave = 0; octave < ctx->numOctaves; ++octave) {
3817 // Calculate noise coordinates. (Roughly $noise_helper in Graphite)
3818 F floorValX = floor_(noiseVecX);
3819 F floorValY = floor_(noiseVecY);
3820 F ceilValX = floorValX + 1.0f;
3821 F ceilValY = floorValY + 1.0f;
3822 F fractValX = noiseVecX - floorValX;
3823 F fractValY = noiseVecY - floorValY;
3824
3825 if (ctx->stitching) {
3826 // If we are stitching, wrap the coordinates to the stitch position.
3827 floorValX -= sk_bit_cast<F>(cond_to_mask(floorValX >= stitchDataX) &
3828 sk_bit_cast<I32>(stitchDataX));
3829 floorValY -= sk_bit_cast<F>(cond_to_mask(floorValY >= stitchDataY) &
3830 sk_bit_cast<I32>(stitchDataY));
3831 ceilValX -= sk_bit_cast<F>(cond_to_mask(ceilValX >= stitchDataX) &
3832 sk_bit_cast<I32>(stitchDataX));
3833 ceilValY -= sk_bit_cast<F>(cond_to_mask(ceilValY >= stitchDataY) &
3834 sk_bit_cast<I32>(stitchDataY));
3835 }
3836
3837 U32 latticeLookup = (U32)(iround(floorValX)) & 0xFF;
3838 F latticeIdxX = cast(expand(gather(ctx->latticeSelector, latticeLookup)));
3839 latticeLookup = (U32)(iround(ceilValX)) & 0xFF;
3840 F latticeIdxY = cast(expand(gather(ctx->latticeSelector, latticeLookup)));
3841
3842 U32 b00 = (U32)(iround(latticeIdxX + floorValY)) & 0xFF;
3843 U32 b10 = (U32)(iround(latticeIdxY + floorValY)) & 0xFF;
3844 U32 b01 = (U32)(iround(latticeIdxX + ceilValY)) & 0xFF;
3845 U32 b11 = (U32)(iround(latticeIdxY + ceilValY)) & 0xFF;
3846
3847 // Calculate noise colors. (Roughly $noise_function in Graphite)
3848 // Apply Hermite interpolation to the fractional value.
3849 F smoothX = fractValX * fractValX * (3.0f - 2.0f * fractValX);
3850 F smoothY = fractValY * fractValY * (3.0f - 2.0f * fractValY);
3851
3852 F color[4];
3853 const uint32_t* channelNoiseData = reinterpret_cast<const uint32_t*>(ctx->noiseData);
3854 for (int channel = 0; channel < 4; ++channel) {
3855 U32 sample00 = gather(channelNoiseData, b00);
3856 U32 sample10 = gather(channelNoiseData, b10);
3857 U32 sample01 = gather(channelNoiseData, b01);
3858 U32 sample11 = gather(channelNoiseData, b11);
3859 channelNoiseData += 256;
3860
3861 F u = compute_perlin_vector(sample00, fractValX, fractValY);
3862 F v = compute_perlin_vector(sample10, fractValX - 1.0f, fractValY);
3863 F A = lerp(u, v, smoothX);
3864
3865 u = compute_perlin_vector(sample01, fractValX, fractValY - 1.0f);
3866 v = compute_perlin_vector(sample11, fractValX - 1.0f, fractValY - 1.0f);
3867 F B = lerp(u, v, smoothX);
3868
3869 color[channel] = lerp(A, B, smoothY);
3870 }
3871
3872 if (ctx->noiseType != SkPerlinNoiseShaderType::kFractalNoise) {
3873 // For kTurbulence the result is: abs(noise[-1,1])
3874 color[0] = abs_(color[0]);
3875 color[1] = abs_(color[1]);
3876 color[2] = abs_(color[2]);
3877 color[3] = abs_(color[3]);
3878 }
3879
3880 r = mad(color[0], ratio, r);
3881 g = mad(color[1], ratio, g);
3882 b = mad(color[2], ratio, b);
3883 a = mad(color[3], ratio, a);
3884
3885 // Scale inputs for the next round.
3886 noiseVecX *= 2.0f;
3887 noiseVecY *= 2.0f;
3888 stitchDataX *= 2.0f;
3889 stitchDataY *= 2.0f;
3890 ratio *= 0.5f;
3891 }
3892
3893 if (ctx->noiseType == SkPerlinNoiseShaderType::kFractalNoise) {
3894 // For kFractalNoise the result is: noise[-1,1] * 0.5 + 0.5
3895 r = mad(r, 0.5f, 0.5f);
3896 g = mad(g, 0.5f, 0.5f);
3897 b = mad(b, 0.5f, 0.5f);
3898 a = mad(a, 0.5f, 0.5f);
3899 }
3900
3901 r = clamp_01_(r) * a;
3902 g = clamp_01_(g) * a;
3903 b = clamp_01_(b) * a;
3904 a = clamp_01_(a);
3905 }
3906
3907 HIGHP_STAGE(mipmap_linear_init, SkRasterPipelineContexts::MipmapCtx* ctx) {
3908 sk_unaligned_store(ctx->x, r);
3909 sk_unaligned_store(ctx->y, g);
3910 }
3911
3912 HIGHP_STAGE(mipmap_linear_update, SkRasterPipelineContexts::MipmapCtx* ctx) {
3913 sk_unaligned_store(ctx->r, r);
3914 sk_unaligned_store(ctx->g, g);
3915 sk_unaligned_store(ctx->b, b);
3916 sk_unaligned_store(ctx->a, a);
3917
3918 r = sk_unaligned_load<F>(ctx->x) * ctx->scaleX;
3919 g = sk_unaligned_load<F>(ctx->y) * ctx->scaleY;
3920 }
3921
3922 HIGHP_STAGE(mipmap_linear_finish, SkRasterPipelineContexts::MipmapCtx* ctx) {
3923 r = lerp(sk_unaligned_load<F>(ctx->r), r, F_(ctx->lowerWeight));
3924 g = lerp(sk_unaligned_load<F>(ctx->g), g, F_(ctx->lowerWeight));
3925 b = lerp(sk_unaligned_load<F>(ctx->b), b, F_(ctx->lowerWeight));
3926 a = lerp(sk_unaligned_load<F>(ctx->a), a, F_(ctx->lowerWeight));
3927 }
3928
3929 HIGHP_STAGE(callback, SkRasterPipelineContexts::CallbackCtx* c) {
3930 store4(c->rgba, r,g,b,a);
3931 c->fn(c, N);
3932 load4(c->read_from, &r,&g,&b,&a);
3933 }
3934
3935 HIGHP_TAIL_STAGE(set_base_pointer, std::byte* p) {
3936 base = p;
3937 }
3938
3939 // All control flow stages used by SkSL maintain some state in the common registers:
3940 // r: condition mask
3941 // g: loop mask
3942 // b: return mask
3943 // a: execution mask (intersection of all three masks)
3944 // After updating r/g/b, you must invoke update_execution_mask().
3945 #define execution_mask() sk_bit_cast<I32>(a)
3946 #define update_execution_mask() a = sk_bit_cast<F>(sk_bit_cast<I32>(r) & \
3947 sk_bit_cast<I32>(g) & \
3948 sk_bit_cast<I32>(b))
3949
3950 HIGHP_TAIL_STAGE(init_lane_masks, SkRasterPipelineContexts::InitLaneMasksCtx* ctx) {
3951 uint32_t iota[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
3952 static_assert(std::size(iota) >= SkRasterPipelineContexts::kMaxStride_highp);
3953
3954 I32 mask = cond_to_mask(sk_unaligned_load<U32>(iota) < *ctx->tail);
3955 r = g = b = a = sk_bit_cast<F>(mask);
3956 }
3957
3958 HIGHP_TAIL_STAGE(store_device_xy01, F* dst) {
3959 // This is very similar to `seed_shader + store_src`, but b/a are backwards.
3960 // (sk_FragCoord actually puts w=1 in the w slot.)
3961 static constexpr float iota[] = {
3962 0.5f, 1.5f, 2.5f, 3.5f, 4.5f, 5.5f, 6.5f, 7.5f,
3963 8.5f, 9.5f,10.5f,11.5f,12.5f,13.5f,14.5f,15.5f,
3964 };
3965 static_assert(std::size(iota) >= SkRasterPipelineContexts::kMaxStride_highp);
3966
3967 dst[0] = cast(U32_(dx)) + sk_unaligned_load<F>(iota);
3968 dst[1] = cast(U32_(dy)) + 0.5f;
3969 dst[2] = F0;
3970 dst[3] = F1;
3971 }
3972
3973 HIGHP_TAIL_STAGE(exchange_src, F* rgba) {
3974 // Swaps r,g,b,a registers with the values at `rgba`.
3975 F temp[4] = {r, g, b, a};
3976 r = rgba[0];
3977 rgba[0] = temp[0];
3978 g = rgba[1];
3979 rgba[1] = temp[1];
3980 b = rgba[2];
3981 rgba[2] = temp[2];
3982 a = rgba[3];
3983 rgba[3] = temp[3];
3984 }
3985
3986 HIGHP_TAIL_STAGE(load_condition_mask, F* ctx) {
3987 r = sk_unaligned_load<F>(ctx);
3988 update_execution_mask();
3989 }
3990
3991 HIGHP_TAIL_STAGE(store_condition_mask, F* ctx) {
3992 sk_unaligned_store(ctx, r);
3993 }
3994
3995 HIGHP_TAIL_STAGE(merge_condition_mask, I32* ptr) {
3996 // Set the condition-mask to the intersection of two adjacent masks at the pointer.
3997 r = sk_bit_cast<F>(ptr[0] & ptr[1]);
3998 update_execution_mask();
3999 }
4000
4001 HIGHP_TAIL_STAGE(merge_inv_condition_mask, I32* ptr) {
4002 // Set the condition-mask to the intersection of the first mask and the inverse of the second.
4003 r = sk_bit_cast<F>(ptr[0] & ~ptr[1]);
4004 update_execution_mask();
4005 }
4006
4007 HIGHP_TAIL_STAGE(load_loop_mask, F* ctx) {
4008 g = sk_unaligned_load<F>(ctx);
4009 update_execution_mask();
4010 }
4011
4012 HIGHP_TAIL_STAGE(store_loop_mask, F* ctx) {
4013 sk_unaligned_store(ctx, g);
4014 }
4015
4016 HIGHP_TAIL_STAGE(mask_off_loop_mask, NoCtx) {
4017 // We encountered a break statement. If a lane was active, it should be masked off now, and stay
4018 // masked-off until the termination of the loop.
4019 g = sk_bit_cast<F>(sk_bit_cast<I32>(g) & ~execution_mask());
4020 update_execution_mask();
4021 }
4022
4023 HIGHP_TAIL_STAGE(reenable_loop_mask, I32* ptr) {
4024 // Set the loop-mask to the union of the current loop-mask with the mask at the pointer.
4025 g = sk_bit_cast<F>(sk_bit_cast<I32>(g) | ptr[0]);
4026 update_execution_mask();
4027 }
4028
4029 HIGHP_TAIL_STAGE(merge_loop_mask, I32* ptr) {
4030 // Set the loop-mask to the intersection of the current loop-mask with the mask at the pointer.
4031 // (Note: this behavior subtly differs from merge_condition_mask!)
4032 g = sk_bit_cast<F>(sk_bit_cast<I32>(g) & ptr[0]);
4033 update_execution_mask();
4034 }
4035
4036 HIGHP_TAIL_STAGE(continue_op, I32* continueMask) {
4037 // Set any currently-executing lanes in the continue-mask to true.
4038 *continueMask |= execution_mask();
4039
4040 // Disable any currently-executing lanes from the loop mask. (Just like `mask_off_loop_mask`.)
4041 g = sk_bit_cast<F>(sk_bit_cast<I32>(g) & ~execution_mask());
4042 update_execution_mask();
4043 }
4044
4045 HIGHP_TAIL_STAGE(case_op, SkRasterPipelineContexts::CaseOpCtx* packed) {
4046 auto ctx = SkRPCtxUtils::Unpack(packed);
4047
4048 // Check each lane to see if the case value matches the expectation.
4049 I32* actualValue = (I32*)(base + ctx.offset);
4050 I32 caseMatches = cond_to_mask(*actualValue == ctx.expectedValue);
4051
4052 // In lanes where we found a match, enable the loop mask...
4053 g = sk_bit_cast<F>(sk_bit_cast<I32>(g) | caseMatches);
4054 update_execution_mask();
4055
4056 // ... and clear the default-case mask.
4057 I32* defaultMask = actualValue + 1;
4058 *defaultMask &= ~caseMatches;
4059 }
4060
4061 HIGHP_TAIL_STAGE(load_return_mask, F* ctx) {
4062 b = sk_unaligned_load<F>(ctx);
4063 update_execution_mask();
4064 }
4065
4066 HIGHP_TAIL_STAGE(store_return_mask, F* ctx) {
4067 sk_unaligned_store(ctx, b);
4068 }
4069
4070 HIGHP_TAIL_STAGE(mask_off_return_mask, NoCtx) {
4071 // We encountered a return statement. If a lane was active, it should be masked off now, and
4072 // stay masked-off until the end of the function.
4073 b = sk_bit_cast<F>(sk_bit_cast<I32>(b) & ~execution_mask());
4074 update_execution_mask();
4075 }
4076
4077 HIGHP_BRANCH_STAGE(branch_if_all_lanes_active, SkRasterPipelineContexts::BranchIfAllLanesActiveCtx* ctx) {
4078 uint32_t iota[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
4079 static_assert(std::size(iota) >= SkRasterPipelineContexts::kMaxStride_highp);
4080
4081 I32 tailLanes = cond_to_mask(*ctx->tail <= sk_unaligned_load<U32>(iota));
4082 return all(execution_mask() | tailLanes) ? ctx->offset : 1;
4083 }
4084
4085 HIGHP_BRANCH_STAGE(branch_if_any_lanes_active, SkRasterPipelineContexts::BranchCtx* ctx) {
4086 return any(execution_mask()) ? ctx->offset : 1;
4087 }
4088
4089 HIGHP_BRANCH_STAGE(branch_if_no_lanes_active, SkRasterPipelineContexts::BranchCtx* ctx) {
4090 return any(execution_mask()) ? 1 : ctx->offset;
4091 }
4092
4093 HIGHP_BRANCH_STAGE(jump, SkRasterPipelineContexts::BranchCtx* ctx) { return ctx->offset; }
4094
4095 HIGHP_BRANCH_STAGE(branch_if_no_active_lanes_eq, SkRasterPipelineContexts::BranchIfEqualCtx* ctx) {
4096 // Compare each lane against the expected value...
4097 I32 match = cond_to_mask(*(const I32*)ctx->ptr == ctx->value);
4098 // ... but mask off lanes that aren't executing.
4099 match &= execution_mask();
4100 // If any lanes matched, don't take the branch.
4101 return any(match) ? 1 : ctx->offset;
4102 }
4103
4104 HIGHP_TAIL_STAGE(trace_line, SkRasterPipelineContexts::TraceLineCtx* ctx) {
4105 const I32* traceMask = (const I32*)ctx->traceMask;
4106 if (any(execution_mask() & *traceMask)) {
4107 ctx->traceHook->line(ctx->lineNumber);
4108 }
4109 }
4110
4111 HIGHP_TAIL_STAGE(trace_enter, SkRasterPipelineContexts::TraceFuncCtx* ctx) {
4112 const I32* traceMask = (const I32*)ctx->traceMask;
4113 if (any(execution_mask() & *traceMask)) {
4114 ctx->traceHook->enter(ctx->funcIdx);
4115 }
4116 }
4117
4118 HIGHP_TAIL_STAGE(trace_exit, SkRasterPipelineContexts::TraceFuncCtx* ctx) {
4119 const I32* traceMask = (const I32*)ctx->traceMask;
4120 if (any(execution_mask() & *traceMask)) {
4121 ctx->traceHook->exit(ctx->funcIdx);
4122 }
4123 }
4124
4125 HIGHP_TAIL_STAGE(trace_scope, SkRasterPipelineContexts::TraceScopeCtx* ctx) {
4126 // Note that trace_scope intentionally does not incorporate the execution mask. Otherwise, the
4127 // scopes would become unbalanced if the execution mask changed in the middle of a block. The
4128 // caller is responsible for providing a combined trace- and execution-mask.
4129 const I32* traceMask = (const I32*)ctx->traceMask;
4130 if (any(*traceMask)) {
4131 ctx->traceHook->scope(ctx->delta);
4132 }
4133 }
4134
4135 HIGHP_TAIL_STAGE(trace_var, SkRasterPipelineContexts::TraceVarCtx* ctx) {
4136 const I32* traceMask = (const I32*)ctx->traceMask;
4137 I32 mask = execution_mask() & *traceMask;
4138 if (any(mask)) {
4139 for (size_t lane = 0; lane < N; ++lane) {
4140 if (select_lane(mask, lane)) {
4141 const I32* data = (const I32*)ctx->data;
4142 int slotIdx = ctx->slotIdx, numSlots = ctx->numSlots;
4143 if (ctx->indirectOffset) {
4144 // If this was an indirect store, apply the indirect-offset to the data pointer.
4145 uint32_t indirectOffset = select_lane(*(const U32*)ctx->indirectOffset, lane);
4146 indirectOffset = std::min<uint32_t>(indirectOffset, ctx->indirectLimit);
4147 data += indirectOffset;
4148 slotIdx += indirectOffset;
4149 }
4150 while (numSlots--) {
4151 ctx->traceHook->var(slotIdx, select_lane(*data, lane));
4152 ++slotIdx;
4153 ++data;
4154 }
4155 break;
4156 }
4157 }
4158 }
4159 }
4160
4161 HIGHP_TAIL_STAGE(copy_uniform, SkRasterPipelineContexts::UniformCtx* ctx) {
4162 const int* src = ctx->src;
4163 I32* dst = (I32*)ctx->dst;
4164 dst[0] = I32_(src[0]);
4165 }
4166 HIGHP_TAIL_STAGE(copy_2_uniforms, SkRasterPipelineContexts::UniformCtx* ctx) {
4167 const int* src = ctx->src;
4168 I32* dst = (I32*)ctx->dst;
4169 dst[0] = I32_(src[0]);
4170 dst[1] = I32_(src[1]);
4171 }
4172 HIGHP_TAIL_STAGE(copy_3_uniforms, SkRasterPipelineContexts::UniformCtx* ctx) {
4173 const int* src = ctx->src;
4174 I32* dst = (I32*)ctx->dst;
4175 dst[0] = I32_(src[0]);
4176 dst[1] = I32_(src[1]);
4177 dst[2] = I32_(src[2]);
4178 }
4179 HIGHP_TAIL_STAGE(copy_4_uniforms, SkRasterPipelineContexts::UniformCtx* ctx) {
4180 const int* src = ctx->src;
4181 I32* dst = (I32*)ctx->dst;
4182 dst[0] = I32_(src[0]);
4183 dst[1] = I32_(src[1]);
4184 dst[2] = I32_(src[2]);
4185 dst[3] = I32_(src[3]);
4186 }
4187
4188 HIGHP_TAIL_STAGE(copy_constant, SkRasterPipelineContexts::ConstantCtx* packed) {
4189 auto ctx = SkRPCtxUtils::Unpack(packed);
4190 I32* dst = (I32*)(base + ctx.dst);
4191 I32 value = I32_(ctx.value);
4192 dst[0] = value;
4193 }
4194 HIGHP_TAIL_STAGE(splat_2_constants, SkRasterPipelineContexts::ConstantCtx* packed) {
4195 auto ctx = SkRPCtxUtils::Unpack(packed);
4196 I32* dst = (I32*)(base + ctx.dst);
4197 I32 value = I32_(ctx.value);
4198 dst[0] = dst[1] = value;
4199 }
4200 HIGHP_TAIL_STAGE(splat_3_constants, SkRasterPipelineContexts::ConstantCtx* packed) {
4201 auto ctx = SkRPCtxUtils::Unpack(packed);
4202 I32* dst = (I32*)(base + ctx.dst);
4203 I32 value = I32_(ctx.value);
4204 dst[0] = dst[1] = dst[2] = value;
4205 }
4206 HIGHP_TAIL_STAGE(splat_4_constants, SkRasterPipelineContexts::ConstantCtx* packed) {
4207 auto ctx = SkRPCtxUtils::Unpack(packed);
4208 I32* dst = (I32*)(base + ctx.dst);
4209 I32 value = I32_(ctx.value);
4210 dst[0] = dst[1] = dst[2] = dst[3] = value;
4211 }
4212
4213 template <int NumSlots>
4214 SI void copy_n_slots_unmasked_fn(SkRasterPipelineContexts::BinaryOpCtx* packed, std::byte* base) {
4215 auto ctx = SkRPCtxUtils::Unpack(packed);
4216 F* dst = (F*)(base + ctx.dst);
4217 F* src = (F*)(base + ctx.src);
4218 memcpy(dst, src, sizeof(F) * NumSlots);
4219 }
4220
4221 HIGHP_TAIL_STAGE(copy_slot_unmasked, SkRasterPipelineContexts::BinaryOpCtx* packed) {
4222 copy_n_slots_unmasked_fn<1>(packed, base);
4223 }
4224 HIGHP_TAIL_STAGE(copy_2_slots_unmasked, SkRasterPipelineContexts::BinaryOpCtx* packed) {
4225 copy_n_slots_unmasked_fn<2>(packed, base);
4226 }
4227 HIGHP_TAIL_STAGE(copy_3_slots_unmasked, SkRasterPipelineContexts::BinaryOpCtx* packed) {
4228 copy_n_slots_unmasked_fn<3>(packed, base);
4229 }
4230 HIGHP_TAIL_STAGE(copy_4_slots_unmasked, SkRasterPipelineContexts::BinaryOpCtx* packed) {
4231 copy_n_slots_unmasked_fn<4>(packed, base);
4232 }
4233
4234 template <int NumSlots>
4235 SI void copy_n_immutable_unmasked_fn(SkRasterPipelineContexts::BinaryOpCtx* packed,
4236 std::byte* base) {
4237 auto ctx = SkRPCtxUtils::Unpack(packed);
4238
4239 // Load the scalar values.
4240 float* src = (float*)(base + ctx.src);
4241 float values[NumSlots];
4242 SK_UNROLL for (int index = 0; index < NumSlots; ++index) {
4243 values[index] = src[index];
4244 }
4245 // Broadcast the scalars into the destination.
4246 F* dst = (F*)(base + ctx.dst);
4247 SK_UNROLL for (int index = 0; index < NumSlots; ++index) {
4248 dst[index] = F_(values[index]);
4249 }
4250 }
4251
4252 HIGHP_TAIL_STAGE(copy_immutable_unmasked, SkRasterPipelineContexts::BinaryOpCtx* packed) {
4253 copy_n_immutable_unmasked_fn<1>(packed, base);
4254 }
4255 HIGHP_TAIL_STAGE(copy_2_immutables_unmasked, SkRasterPipelineContexts::BinaryOpCtx* packed) {
4256 copy_n_immutable_unmasked_fn<2>(packed, base);
4257 }
4258 HIGHP_TAIL_STAGE(copy_3_immutables_unmasked, SkRasterPipelineContexts::BinaryOpCtx* packed) {
4259 copy_n_immutable_unmasked_fn<3>(packed, base);
4260 }
4261 HIGHP_TAIL_STAGE(copy_4_immutables_unmasked, SkRasterPipelineContexts::BinaryOpCtx* packed) {
4262 copy_n_immutable_unmasked_fn<4>(packed, base);
4263 }
4264
4265 template <int NumSlots>
4266 SI void copy_n_slots_masked_fn(SkRasterPipelineContexts::BinaryOpCtx* packed,
4267 std::byte* base,
4268 I32 mask) {
4269 auto ctx = SkRPCtxUtils::Unpack(packed);
4270 I32* dst = (I32*)(base + ctx.dst);
4271 I32* src = (I32*)(base + ctx.src);
4272 SK_UNROLL for (int count = 0; count < NumSlots; ++count) {
4273 *dst = if_then_else(mask, *src, *dst);
4274 dst += 1;
4275 src += 1;
4276 }
4277 }
4278
4279 HIGHP_TAIL_STAGE(copy_slot_masked, SkRasterPipelineContexts::BinaryOpCtx* packed) {
4280 copy_n_slots_masked_fn<1>(packed, base, execution_mask());
4281 }
4282 HIGHP_TAIL_STAGE(copy_2_slots_masked, SkRasterPipelineContexts::BinaryOpCtx* packed) {
4283 copy_n_slots_masked_fn<2>(packed, base, execution_mask());
4284 }
4285 HIGHP_TAIL_STAGE(copy_3_slots_masked, SkRasterPipelineContexts::BinaryOpCtx* packed) {
4286 copy_n_slots_masked_fn<3>(packed, base, execution_mask());
4287 }
4288 HIGHP_TAIL_STAGE(copy_4_slots_masked, SkRasterPipelineContexts::BinaryOpCtx* packed) {
4289 copy_n_slots_masked_fn<4>(packed, base, execution_mask());
4290 }
4291
4292 template <int LoopCount, typename OffsetType>
4293 SI void shuffle_fn(std::byte* ptr, OffsetType* offsets, int numSlots) {
4294 F scratch[16];
4295 SK_UNROLL for (int count = 0; count < LoopCount; ++count) {
4296 scratch[count] = *(F*)(ptr + offsets[count]);
4297 }
4298 // Surprisingly, this switch generates significantly better code than a memcpy (on x86-64) when
4299 // the number of slots is unknown at compile time, and generates roughly identical code when the
4300 // number of slots is hardcoded. Using a switch allows `scratch` to live in ymm0-ymm15 instead
4301 // of being written out to the stack and then read back in. Also, the intrinsic memcpy assumes
4302 // that `numSlots` could be arbitrarily large, and so it emits more code than we need.
4303 F* dst = (F*)ptr;
4304 switch (numSlots) {
4305 case 16: dst[15] = scratch[15]; [[fallthrough]];
4306 case 15: dst[14] = scratch[14]; [[fallthrough]];
4307 case 14: dst[13] = scratch[13]; [[fallthrough]];
4308 case 13: dst[12] = scratch[12]; [[fallthrough]];
4309 case 12: dst[11] = scratch[11]; [[fallthrough]];
4310 case 11: dst[10] = scratch[10]; [[fallthrough]];
4311 case 10: dst[ 9] = scratch[ 9]; [[fallthrough]];
4312 case 9: dst[ 8] = scratch[ 8]; [[fallthrough]];
4313 case 8: dst[ 7] = scratch[ 7]; [[fallthrough]];
4314 case 7: dst[ 6] = scratch[ 6]; [[fallthrough]];
4315 case 6: dst[ 5] = scratch[ 5]; [[fallthrough]];
4316 case 5: dst[ 4] = scratch[ 4]; [[fallthrough]];
4317 case 4: dst[ 3] = scratch[ 3]; [[fallthrough]];
4318 case 3: dst[ 2] = scratch[ 2]; [[fallthrough]];
4319 case 2: dst[ 1] = scratch[ 1]; [[fallthrough]];
4320 case 1: dst[ 0] = scratch[ 0];
4321 }
4322 }
4323
4324 template <int N>
4325 SI void small_swizzle_fn(SkRasterPipelineContexts::SwizzleCtx* packed, std::byte* base) {
4326 auto ctx = SkRPCtxUtils::Unpack(packed);
4327 shuffle_fn<N>(base + ctx.dst, ctx.offsets, N);
4328 }
4329
4330 HIGHP_TAIL_STAGE(swizzle_1, SkRasterPipelineContexts::SwizzleCtx* packed) {
4331 small_swizzle_fn<1>(packed, base);
4332 }
4333 HIGHP_TAIL_STAGE(swizzle_2, SkRasterPipelineContexts::SwizzleCtx* packed) {
4334 small_swizzle_fn<2>(packed, base);
4335 }
4336 HIGHP_TAIL_STAGE(swizzle_3, SkRasterPipelineContexts::SwizzleCtx* packed) {
4337 small_swizzle_fn<3>(packed, base);
4338 }
4339 HIGHP_TAIL_STAGE(swizzle_4, SkRasterPipelineContexts::SwizzleCtx* packed) {
4340 small_swizzle_fn<4>(packed, base);
4341 }
4342 HIGHP_TAIL_STAGE(shuffle, SkRasterPipelineContexts::ShuffleCtx* ctx) {
4343 shuffle_fn<16>((std::byte*)ctx->ptr, ctx->offsets, ctx->count);
4344 }
4345
4346 template <int NumSlots>
4347 SI void swizzle_copy_masked_fn(I32* dst, const I32* src, uint16_t* offsets, I32 mask) {
4348 std::byte* dstB = (std::byte*)dst;
4349 SK_UNROLL for (int count = 0; count < NumSlots; ++count) {
4350 I32* dstS = (I32*)(dstB + *offsets);
4351 *dstS = if_then_else(mask, *src, *dstS);
4352 offsets += 1;
4353 src += 1;
4354 }
4355 }
4356
4357 HIGHP_TAIL_STAGE(swizzle_copy_slot_masked, SkRasterPipelineContexts::SwizzleCopyCtx* ctx) {
4358 swizzle_copy_masked_fn<1>((I32*)ctx->dst, (const I32*)ctx->src, ctx->offsets, execution_mask());
4359 }
4360 HIGHP_TAIL_STAGE(swizzle_copy_2_slots_masked, SkRasterPipelineContexts::SwizzleCopyCtx* ctx) {
4361 swizzle_copy_masked_fn<2>((I32*)ctx->dst, (const I32*)ctx->src, ctx->offsets, execution_mask());
4362 }
4363 HIGHP_TAIL_STAGE(swizzle_copy_3_slots_masked, SkRasterPipelineContexts::SwizzleCopyCtx* ctx) {
4364 swizzle_copy_masked_fn<3>((I32*)ctx->dst, (const I32*)ctx->src, ctx->offsets, execution_mask());
4365 }
4366 HIGHP_TAIL_STAGE(swizzle_copy_4_slots_masked, SkRasterPipelineContexts::SwizzleCopyCtx* ctx) {
4367 swizzle_copy_masked_fn<4>((I32*)ctx->dst, (const I32*)ctx->src, ctx->offsets, execution_mask());
4368 }
4369
4370 HIGHP_TAIL_STAGE(copy_from_indirect_unmasked, SkRasterPipelineContexts::CopyIndirectCtx* ctx) {
4371 // Clamp the indirect offsets to stay within the limit.
4372 U32 offsets = *(const U32*)ctx->indirectOffset;
4373 offsets = min(offsets, U32_(ctx->indirectLimit));
4374
4375 // Scale up the offsets to account for the N lanes per value.
4376 offsets *= N;
4377
4378 // Adjust the offsets forward so that they fetch from the correct lane.
4379 static constexpr uint32_t iota[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
4380 static_assert(std::size(iota) >= SkRasterPipelineContexts::kMaxStride_highp);
4381 offsets += sk_unaligned_load<U32>(iota);
4382
4383 // Use gather to perform indirect lookups; write the results into `dst`.
4384 const int* src = ctx->src;
4385 I32* dst = (I32*)ctx->dst;
4386 I32* end = dst + ctx->slots;
4387 do {
4388 *dst = gather(src, offsets);
4389 dst += 1;
4390 src += N;
4391 } while (dst != end);
4392 }
4393
4394 HIGHP_TAIL_STAGE(copy_from_indirect_uniform_unmasked,
4395 SkRasterPipelineContexts::CopyIndirectCtx* ctx) {
4396 // Clamp the indirect offsets to stay within the limit.
4397 U32 offsets = *(const U32*)ctx->indirectOffset;
4398 offsets = min(offsets, U32_(ctx->indirectLimit));
4399
4400 // Use gather to perform indirect lookups; write the results into `dst`.
4401 const int* src = ctx->src;
4402 I32* dst = (I32*)ctx->dst;
4403 I32* end = dst + ctx->slots;
4404 do {
4405 *dst = gather(src, offsets);
4406 dst += 1;
4407 src += 1;
4408 } while (dst != end);
4409 }
4410
4411 HIGHP_TAIL_STAGE(copy_to_indirect_masked, SkRasterPipelineContexts::CopyIndirectCtx* ctx) {
4412 // Clamp the indirect offsets to stay within the limit.
4413 U32 offsets = *(const U32*)ctx->indirectOffset;
4414 offsets = min(offsets, U32_(ctx->indirectLimit));
4415
4416 // Scale up the offsets to account for the N lanes per value.
4417 offsets *= N;
4418
4419 // Adjust the offsets forward so that they store into the correct lane.
4420 static constexpr uint32_t iota[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
4421 static_assert(std::size(iota) >= SkRasterPipelineContexts::kMaxStride_highp);
4422 offsets += sk_unaligned_load<U32>(iota);
4423
4424 // Perform indirect, masked writes into `dst`.
4425 const I32* src = (const I32*)ctx->src;
4426 const I32* end = src + ctx->slots;
4427 int* dst = ctx->dst;
4428 I32 mask = execution_mask();
4429 do {
4430 scatter_masked(*src, dst, offsets, mask);
4431 dst += N;
4432 src += 1;
4433 } while (src != end);
4434 }
4435
4436 HIGHP_TAIL_STAGE(swizzle_copy_to_indirect_masked,
4437 SkRasterPipelineContexts::SwizzleCopyIndirectCtx* ctx) {
4438 // Clamp the indirect offsets to stay within the limit.
4439 U32 offsets = *(const U32*)ctx->indirectOffset;
4440 offsets = min(offsets, U32_(ctx->indirectLimit));
4441
4442 // Scale up the offsets to account for the N lanes per value.
4443 offsets *= N;
4444
4445 // Adjust the offsets forward so that they store into the correct lane.
4446 static constexpr uint32_t iota[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
4447 static_assert(std::size(iota) >= SkRasterPipelineContexts::kMaxStride_highp);
4448 offsets += sk_unaligned_load<U32>(iota);
4449
4450 // Perform indirect, masked, swizzled writes into `dst`.
4451 const I32* src = (const I32*)ctx->src;
4452 const I32* end = src + ctx->slots;
4453 std::byte* dstB = (std::byte*)ctx->dst;
4454 const uint16_t* swizzle = ctx->offsets;
4455 I32 mask = execution_mask();
4456 do {
4457 int* dst = (int*)(dstB + *swizzle);
4458 scatter_masked(*src, dst, offsets, mask);
4459 swizzle += 1;
4460 src += 1;
4461 } while (src != end);
4462 }
4463
4464 // Unary operations take a single input, and overwrite it with their output.
4465 // Unlike binary or ternary operations, we provide variations of 1-4 slots, but don't provide
4466 // an arbitrary-width "n-slot" variation; the Builder can chain together longer sequences manually.
4467 template <typename T, void (*ApplyFn)(T*)>
4468 SI void apply_adjacent_unary(T* dst, T* end) {
4469 do {
4470 ApplyFn(dst);
4471 dst += 1;
4472 } while (dst != end);
4473 }
4474
4475 #if defined(SKRP_CPU_SCALAR)
4476 template <typename T>
4477 SI void cast_to_float_from_fn(T* dst) {
4478 *dst = sk_bit_cast<T>((F)*dst);
4479 }
4480 SI void cast_to_int_from_fn(F* dst) {
4481 *dst = sk_bit_cast<F>((I32)*dst);
4482 }
4483 SI void cast_to_uint_from_fn(F* dst) {
4484 *dst = sk_bit_cast<F>((U32)*dst);
4485 }
4486 #else
4487 template <typename T>
4488 SI void cast_to_float_from_fn(T* dst) {
4489 *dst = sk_bit_cast<T>(__builtin_convertvector(*dst, F));
4490 }
4491 SI void cast_to_int_from_fn(F* dst) {
4492 *dst = sk_bit_cast<F>(__builtin_convertvector(*dst, I32));
4493 }
4494 SI void cast_to_uint_from_fn(F* dst) {
4495 *dst = sk_bit_cast<F>(__builtin_convertvector(*dst, U32));
4496 }
4497 #endif
4498
4499 SI void abs_fn(I32* dst) {
4500 *dst = abs_(*dst);
4501 }
4502
4503 SI void floor_fn(F* dst) {
4504 *dst = floor_(*dst);
4505 }
4506
4507 SI void ceil_fn(F* dst) {
4508 *dst = ceil_(*dst);
4509 }
4510
4511 SI void invsqrt_fn(F* dst) {
4512 *dst = rsqrt(*dst);
4513 }
4514
4515 #define DECLARE_UNARY_FLOAT(name) \
4516 HIGHP_TAIL_STAGE(name##_float, F* dst) { apply_adjacent_unary<F, &name##_fn>(dst, dst + 1); } \
4517 HIGHP_TAIL_STAGE(name##_2_floats, F* dst) { apply_adjacent_unary<F, &name##_fn>(dst, dst + 2); } \
4518 HIGHP_TAIL_STAGE(name##_3_floats, F* dst) { apply_adjacent_unary<F, &name##_fn>(dst, dst + 3); } \
4519 HIGHP_TAIL_STAGE(name##_4_floats, F* dst) { apply_adjacent_unary<F, &name##_fn>(dst, dst + 4); }
4520
4521 #define DECLARE_UNARY_INT(name) \
4522 HIGHP_TAIL_STAGE(name##_int, I32* dst) { apply_adjacent_unary<I32, &name##_fn>(dst, dst + 1); } \
4523 HIGHP_TAIL_STAGE(name##_2_ints, I32* dst) { apply_adjacent_unary<I32, &name##_fn>(dst, dst + 2); } \
4524 HIGHP_TAIL_STAGE(name##_3_ints, I32* dst) { apply_adjacent_unary<I32, &name##_fn>(dst, dst + 3); } \
4525 HIGHP_TAIL_STAGE(name##_4_ints, I32* dst) { apply_adjacent_unary<I32, &name##_fn>(dst, dst + 4); }
4526
4527 #define DECLARE_UNARY_UINT(name) \
4528 HIGHP_TAIL_STAGE(name##_uint, U32* dst) { apply_adjacent_unary<U32, &name##_fn>(dst, dst + 1); } \
4529 HIGHP_TAIL_STAGE(name##_2_uints, U32* dst) { apply_adjacent_unary<U32, &name##_fn>(dst, dst + 2); } \
4530 HIGHP_TAIL_STAGE(name##_3_uints, U32* dst) { apply_adjacent_unary<U32, &name##_fn>(dst, dst + 3); } \
4531 HIGHP_TAIL_STAGE(name##_4_uints, U32* dst) { apply_adjacent_unary<U32, &name##_fn>(dst, dst + 4); }
4532
4533 DECLARE_UNARY_INT(cast_to_float_from) DECLARE_UNARY_UINT(cast_to_float_from)
4534 DECLARE_UNARY_FLOAT(cast_to_int_from)
4535 DECLARE_UNARY_FLOAT(cast_to_uint_from)
4536 DECLARE_UNARY_FLOAT(floor)
4537 DECLARE_UNARY_FLOAT(ceil)
4538 DECLARE_UNARY_FLOAT(invsqrt)
4539 DECLARE_UNARY_INT(abs)
4540
4541 #undef DECLARE_UNARY_FLOAT
4542 #undef DECLARE_UNARY_INT
4543 #undef DECLARE_UNARY_UINT
4544
4545 // For complex unary ops, we only provide a 1-slot version to reduce code bloat.
4546 HIGHP_TAIL_STAGE(sin_float, F* dst) { *dst = sin_(*dst); }
4547 HIGHP_TAIL_STAGE(cos_float, F* dst) { *dst = cos_(*dst); }
4548 HIGHP_TAIL_STAGE(tan_float, F* dst) { *dst = tan_(*dst); }
4549 HIGHP_TAIL_STAGE(asin_float, F* dst) { *dst = asin_(*dst); }
4550 HIGHP_TAIL_STAGE(acos_float, F* dst) { *dst = acos_(*dst); }
4551 HIGHP_TAIL_STAGE(atan_float, F* dst) { *dst = atan_(*dst); }
4552 HIGHP_TAIL_STAGE(sqrt_float, F* dst) { *dst = sqrt_(*dst); }
4553 HIGHP_TAIL_STAGE(exp_float, F* dst) { *dst = approx_exp(*dst); }
4554 HIGHP_TAIL_STAGE(exp2_float, F* dst) { *dst = approx_pow2(*dst); }
4555 HIGHP_TAIL_STAGE(log_float, F* dst) { *dst = approx_log(*dst); }
4556 HIGHP_TAIL_STAGE(log2_float, F* dst) { *dst = approx_log2(*dst); }
4557
4558 HIGHP_TAIL_STAGE(inverse_mat2, F* dst) {
4559 F a00 = dst[0], a01 = dst[1],
4560 a10 = dst[2], a11 = dst[3];
4561 F det = nmad(a01, a10, a00 * a11),
4562 invdet = rcp_precise(det);
4563 dst[0] = invdet * a11;
4564 dst[1] = -invdet * a01;
4565 dst[2] = -invdet * a10;
4566 dst[3] = invdet * a00;
4567 }
4568
4569 HIGHP_TAIL_STAGE(inverse_mat3, F* dst) {
4570 F a00 = dst[0], a01 = dst[1], a02 = dst[2],
4571 a10 = dst[3], a11 = dst[4], a12 = dst[5],
4572 a20 = dst[6], a21 = dst[7], a22 = dst[8];
4573 F b01 = nmad(a12, a21, a22 * a11),
4574 b11 = nmad(a22, a10, a12 * a20),
4575 b21 = nmad(a11, a20, a21 * a10);
4576 F det = mad(a00, b01, mad(a01, b11, a02 * b21)),
4577 invdet = rcp_precise(det);
4578 dst[0] = invdet * b01;
4579 dst[1] = invdet * nmad(a22, a01, a02 * a21);
4580 dst[2] = invdet * nmad(a02, a11, a12 * a01);
4581 dst[3] = invdet * b11;
4582 dst[4] = invdet * nmad(a02, a20, a22 * a00);
4583 dst[5] = invdet * nmad(a12, a00, a02 * a10);
4584 dst[6] = invdet * b21;
4585 dst[7] = invdet * nmad(a21, a00, a01 * a20);
4586 dst[8] = invdet * nmad(a01, a10, a11 * a00);
4587 }
4588
4589 HIGHP_TAIL_STAGE(inverse_mat4, F* dst) {
4590 F a00 = dst[0], a01 = dst[1], a02 = dst[2], a03 = dst[3],
4591 a10 = dst[4], a11 = dst[5], a12 = dst[6], a13 = dst[7],
4592 a20 = dst[8], a21 = dst[9], a22 = dst[10], a23 = dst[11],
4593 a30 = dst[12], a31 = dst[13], a32 = dst[14], a33 = dst[15];
4594 F b00 = nmad(a01, a10, a00 * a11),
4595 b01 = nmad(a02, a10, a00 * a12),
4596 b02 = nmad(a03, a10, a00 * a13),
4597 b03 = nmad(a02, a11, a01 * a12),
4598 b04 = nmad(a03, a11, a01 * a13),
4599 b05 = nmad(a03, a12, a02 * a13),
4600 b06 = nmad(a21, a30, a20 * a31),
4601 b07 = nmad(a22, a30, a20 * a32),
4602 b08 = nmad(a23, a30, a20 * a33),
4603 b09 = nmad(a22, a31, a21 * a32),
4604 b10 = nmad(a23, a31, a21 * a33),
4605 b11 = nmad(a23, a32, a22 * a33),
4606 det = mad(b00, b11, b05 * b06) + mad(b02, b09, b03 * b08) - mad(b01, b10, b04 * b07),
4607 invdet = rcp_precise(det);
4608 b00 *= invdet;
4609 b01 *= invdet;
4610 b02 *= invdet;
4611 b03 *= invdet;
4612 b04 *= invdet;
4613 b05 *= invdet;
4614 b06 *= invdet;
4615 b07 *= invdet;
4616 b08 *= invdet;
4617 b09 *= invdet;
4618 b10 *= invdet;
4619 b11 *= invdet;
4620 dst[0] = mad(a13, b09, nmad(a12, b10, a11*b11));
4621 dst[1] = nmad(a03, b09, nmad(a01, b11, a02*b10));
4622 dst[2] = mad(a33, b03, nmad(a32, b04, a31*b05));
4623 dst[3] = nmad(a23, b03, nmad(a21, b05, a22*b04));
4624 dst[4] = nmad(a13, b07, nmad(a10, b11, a12*b08));
4625 dst[5] = mad(a03, b07, nmad(a02, b08, a00*b11));
4626 dst[6] = nmad(a33, b01, nmad(a30, b05, a32*b02));
4627 dst[7] = mad(a23, b01, nmad(a22, b02, a20*b05));
4628 dst[8] = mad(a13, b06, nmad(a11, b08, a10*b10));
4629 dst[9] = nmad(a03, b06, nmad(a00, b10, a01*b08));
4630 dst[10] = mad(a33, b00, nmad(a31, b02, a30*b04));
4631 dst[11] = nmad(a23, b00, nmad(a20, b04, a21*b02));
4632 dst[12] = nmad(a12, b06, nmad(a10, b09, a11*b07));
4633 dst[13] = mad(a02, b06, nmad(a01, b07, a00*b09));
4634 dst[14] = nmad(a32, b00, nmad(a30, b03, a31*b01));
4635 dst[15] = mad(a22, b00, nmad(a21, b01, a20*b03));
4636 }
4637
4638 // Binary operations take two adjacent inputs, and write their output in the first position.
4639 template <typename T, void (*ApplyFn)(T*, T*)>
4640 SI void apply_adjacent_binary(T* dst, T* src) {
4641 T* end = src;
4642 do {
4643 ApplyFn(dst, src);
4644 dst += 1;
4645 src += 1;
4646 } while (dst != end);
4647 }
4648
4649 template <typename T, void (*ApplyFn)(T*, T*)>
4650 SI void apply_adjacent_binary_packed(SkRasterPipelineContexts::BinaryOpCtx* packed,
4651 std::byte* base) {
4652 auto ctx = SkRPCtxUtils::Unpack(packed);
4653 std::byte* dst = base + ctx.dst;
4654 std::byte* src = base + ctx.src;
4655 apply_adjacent_binary<T, ApplyFn>((T*)dst, (T*)src);
4656 }
4657
4658 template <int N, typename V, typename S, void (*ApplyFn)(V*, V*)>
4659 SI void apply_binary_immediate(SkRasterPipelineContexts::ConstantCtx* packed, std::byte* base) {
4660 auto ctx = SkRPCtxUtils::Unpack(packed);
4661 V* dst = (V*)(base + ctx.dst); // get a pointer to the destination
4662 S scalar = sk_bit_cast<S>(ctx.value); // bit-pun the constant value as desired
4663 V src = scalar - V(); // broadcast the constant value into a vector
4664 SK_UNROLL for (int index = 0; index < N; ++index) {
4665 ApplyFn(dst, &src); // perform the operation
4666 dst += 1;
4667 }
4668 }
4669
4670 template <typename T>
4671 SI void add_fn(T* dst, T* src) {
4672 *dst += *src;
4673 }
4674
4675 template <typename T>
4676 SI void sub_fn(T* dst, T* src) {
4677 *dst -= *src;
4678 }
4679
4680 template <typename T>
4681 SI void mul_fn(T* dst, T* src) {
4682 *dst *= *src;
4683 }
4684
4685 SI void div_fn(I32* dst, I32* src) {
4686 I32 divisor = *src;
4687 // Integer division crashes when we divide by 0, but we can divide by -1 to not crash (the
4688 // result will be non-sensical). The mask will be 0xFFFFFFF if true, which happens to be -1.
4689 divisor |= ((I32)cond_to_mask(divisor == 0));
4690 // Dividing by -1 works for all numerators *except* INT_MIN, so we can add -1 once more if
4691 // we are in that case.
4692 divisor += ((I32)cond_to_mask(divisor == -1 && *dst == std::numeric_limits<int32_t>::lowest()));
4693 *dst /= divisor;
4694 }
4695
4696 SI void div_fn(U32* dst, U32* src) {
4697 U32 divisor = *src;
4698 // Integer division crashes when we divide by 0, but we can divide by something else to not
4699 // crash (the result will be non-sensical). The mask will be 0xFFFFFFF if true.
4700 divisor |= ((U32)cond_to_mask(divisor == 0));
4701 *dst /= divisor;
4702 }
4703
4704 SI void div_fn(F* dst, F* src) {
4705 *dst /= *src;
4706 }
4707
4708 SI void bitwise_and_fn(I32* dst, I32* src) {
4709 *dst &= *src;
4710 }
4711
4712 SI void bitwise_or_fn(I32* dst, I32* src) {
4713 *dst |= *src;
4714 }
4715
4716 SI void bitwise_xor_fn(I32* dst, I32* src) {
4717 *dst ^= *src;
4718 }
4719
4720 template <typename T>
4721 SI void max_fn(T* dst, T* src) {
4722 *dst = max(*dst, *src);
4723 }
4724
4725 template <typename T>
4726 SI void min_fn(T* dst, T* src) {
4727 *dst = min(*dst, *src);
4728 }
4729
4730 template <typename T>
4731 SI void cmplt_fn(T* dst, T* src) {
4732 static_assert(sizeof(T) == sizeof(I32));
4733 I32 result = cond_to_mask(*dst < *src);
4734 memcpy(dst, &result, sizeof(I32));
4735 }
4736
4737 template <typename T>
4738 SI void cmple_fn(T* dst, T* src) {
4739 static_assert(sizeof(T) == sizeof(I32));
4740 I32 result = cond_to_mask(*dst <= *src);
4741 memcpy(dst, &result, sizeof(I32));
4742 }
4743
4744 template <typename T>
4745 SI void cmpeq_fn(T* dst, T* src) {
4746 static_assert(sizeof(T) == sizeof(I32));
4747 I32 result = cond_to_mask(*dst == *src);
4748 memcpy(dst, &result, sizeof(I32));
4749 }
4750
4751 template <typename T>
4752 SI void cmpne_fn(T* dst, T* src) {
4753 static_assert(sizeof(T) == sizeof(I32));
4754 I32 result = cond_to_mask(*dst != *src);
4755 memcpy(dst, &result, sizeof(I32));
4756 }
4757
4758 SI void atan2_fn(F* dst, F* src) {
4759 *dst = atan2_(*dst, *src);
4760 }
4761
4762 SI void pow_fn(F* dst, F* src) {
4763 *dst = approx_powf(*dst, *src);
4764 }
4765
4766 SI void mod_fn(F* dst, F* src) {
4767 *dst = nmad(*src, floor_(*dst / *src), *dst);
4768 }
4769
4770 #define DECLARE_N_WAY_BINARY_FLOAT(name) \
4771 HIGHP_TAIL_STAGE(name##_n_floats, SkRasterPipelineContexts::BinaryOpCtx* packed) { \
4772 apply_adjacent_binary_packed<F, &name##_fn>(packed, base); \
4773 }
4774
4775 #define DECLARE_BINARY_FLOAT(name) \
4776 HIGHP_TAIL_STAGE(name##_float, F* dst) { apply_adjacent_binary<F, &name##_fn>(dst, dst + 1); } \
4777 HIGHP_TAIL_STAGE(name##_2_floats, F* dst) { apply_adjacent_binary<F, &name##_fn>(dst, dst + 2); } \
4778 HIGHP_TAIL_STAGE(name##_3_floats, F* dst) { apply_adjacent_binary<F, &name##_fn>(dst, dst + 3); } \
4779 HIGHP_TAIL_STAGE(name##_4_floats, F* dst) { apply_adjacent_binary<F, &name##_fn>(dst, dst + 4); } \
4780 DECLARE_N_WAY_BINARY_FLOAT(name)
4781
4782 #define DECLARE_N_WAY_BINARY_INT(name) \
4783 HIGHP_TAIL_STAGE(name##_n_ints, SkRasterPipelineContexts::BinaryOpCtx* packed) { \
4784 apply_adjacent_binary_packed<I32, &name##_fn>(packed, base); \
4785 }
4786
4787 #define DECLARE_BINARY_INT(name) \
4788 HIGHP_TAIL_STAGE(name##_int, I32* dst) { apply_adjacent_binary<I32, &name##_fn>(dst, dst + 1); } \
4789 HIGHP_TAIL_STAGE(name##_2_ints, I32* dst) { apply_adjacent_binary<I32, &name##_fn>(dst, dst + 2); } \
4790 HIGHP_TAIL_STAGE(name##_3_ints, I32* dst) { apply_adjacent_binary<I32, &name##_fn>(dst, dst + 3); } \
4791 HIGHP_TAIL_STAGE(name##_4_ints, I32* dst) { apply_adjacent_binary<I32, &name##_fn>(dst, dst + 4); } \
4792 DECLARE_N_WAY_BINARY_INT(name)
4793
4794 #define DECLARE_N_WAY_BINARY_UINT(name) \
4795 HIGHP_TAIL_STAGE(name##_n_uints, SkRasterPipelineContexts::BinaryOpCtx* packed) { \
4796 apply_adjacent_binary_packed<U32, &name##_fn>(packed, base); \
4797 }
4798
4799 #define DECLARE_BINARY_UINT(name) \
4800 HIGHP_TAIL_STAGE(name##_uint, U32* dst) { apply_adjacent_binary<U32, &name##_fn>(dst, dst + 1); } \
4801 HIGHP_TAIL_STAGE(name##_2_uints, U32* dst) { apply_adjacent_binary<U32, &name##_fn>(dst, dst + 2); } \
4802 HIGHP_TAIL_STAGE(name##_3_uints, U32* dst) { apply_adjacent_binary<U32, &name##_fn>(dst, dst + 3); } \
4803 HIGHP_TAIL_STAGE(name##_4_uints, U32* dst) { apply_adjacent_binary<U32, &name##_fn>(dst, dst + 4); } \
4804 DECLARE_N_WAY_BINARY_UINT(name)
4805
4806 // Many ops reuse the int stages when performing uint arithmetic, since they're equivalent on a
4807 // two's-complement machine. (Even multiplication is equivalent in the lower 32 bits.)
4808 DECLARE_BINARY_FLOAT(add) DECLARE_BINARY_INT(add)
4809 DECLARE_BINARY_FLOAT(sub) DECLARE_BINARY_INT(sub)
4810 DECLARE_BINARY_FLOAT(mul) DECLARE_BINARY_INT(mul)
4811 DECLARE_BINARY_FLOAT(div) DECLARE_BINARY_INT(div) DECLARE_BINARY_UINT(div)
4812 DECLARE_BINARY_INT(bitwise_and)
4813 DECLARE_BINARY_INT(bitwise_or)
4814 DECLARE_BINARY_INT(bitwise_xor)
4815 DECLARE_BINARY_FLOAT(mod)
4816 DECLARE_BINARY_FLOAT(min) DECLARE_BINARY_INT(min) DECLARE_BINARY_UINT(min)
4817 DECLARE_BINARY_FLOAT(max) DECLARE_BINARY_INT(max) DECLARE_BINARY_UINT(max)
4818 DECLARE_BINARY_FLOAT(cmplt) DECLARE_BINARY_INT(cmplt) DECLARE_BINARY_UINT(cmplt)
4819 DECLARE_BINARY_FLOAT(cmple) DECLARE_BINARY_INT(cmple) DECLARE_BINARY_UINT(cmple)
4820 DECLARE_BINARY_FLOAT(cmpeq) DECLARE_BINARY_INT(cmpeq)
4821 DECLARE_BINARY_FLOAT(cmpne) DECLARE_BINARY_INT(cmpne)
4822
4823 // Sufficiently complex ops only provide an N-way version, to avoid code bloat from the dedicated
4824 // 1-4 slot versions.
4825 DECLARE_N_WAY_BINARY_FLOAT(atan2)
4826 DECLARE_N_WAY_BINARY_FLOAT(pow)
4827
4828 // Some ops have an optimized version when the right-side is an immediate value.
4829 #define DECLARE_IMM_BINARY_FLOAT(name) \
4830 HIGHP_TAIL_STAGE(name##_imm_float, SkRasterPipelineContexts::ConstantCtx* packed) { \
4831 apply_binary_immediate<1, F, float, &name##_fn>(packed, base); \
4832 }
4833 #define DECLARE_IMM_BINARY_INT(name) \
4834 HIGHP_TAIL_STAGE(name##_imm_int, SkRasterPipelineContexts::ConstantCtx* packed) { \
4835 apply_binary_immediate<1, I32, int32_t, &name##_fn>(packed, base); \
4836 }
4837 #define DECLARE_MULTI_IMM_BINARY_INT(name) \
4838 HIGHP_TAIL_STAGE(name##_imm_int, SkRasterPipelineContexts::ConstantCtx* packed) { \
4839 apply_binary_immediate<1, I32, int32_t, &name##_fn>(packed, base); \
4840 } \
4841 HIGHP_TAIL_STAGE(name##_imm_2_ints, SkRasterPipelineContexts::ConstantCtx* packed) { \
4842 apply_binary_immediate<2, I32, int32_t, &name##_fn>(packed, base); \
4843 } \
4844 HIGHP_TAIL_STAGE(name##_imm_3_ints, SkRasterPipelineContexts::ConstantCtx* packed) { \
4845 apply_binary_immediate<3, I32, int32_t, &name##_fn>(packed, base); \
4846 } \
4847 HIGHP_TAIL_STAGE(name##_imm_4_ints, SkRasterPipelineContexts::ConstantCtx* packed) { \
4848 apply_binary_immediate<4, I32, int32_t, &name##_fn>(packed, base); \
4849 }
4850 #define DECLARE_IMM_BINARY_UINT(name) \
4851 HIGHP_TAIL_STAGE(name##_imm_uint, SkRasterPipelineContexts::ConstantCtx* packed) { \
4852 apply_binary_immediate<1, U32, uint32_t, &name##_fn>(packed, base); \
4853 }
4854
4855 DECLARE_IMM_BINARY_FLOAT(add) DECLARE_IMM_BINARY_INT(add)
4856 DECLARE_IMM_BINARY_FLOAT(mul) DECLARE_IMM_BINARY_INT(mul)
4857 DECLARE_MULTI_IMM_BINARY_INT(bitwise_and)
4858 DECLARE_IMM_BINARY_FLOAT(max)
4859 DECLARE_IMM_BINARY_FLOAT(min)
4860 DECLARE_IMM_BINARY_INT(bitwise_xor)
4861 DECLARE_IMM_BINARY_FLOAT(cmplt) DECLARE_IMM_BINARY_INT(cmplt) DECLARE_IMM_BINARY_UINT(cmplt)
4862 DECLARE_IMM_BINARY_FLOAT(cmple) DECLARE_IMM_BINARY_INT(cmple) DECLARE_IMM_BINARY_UINT(cmple)
4863 DECLARE_IMM_BINARY_FLOAT(cmpeq) DECLARE_IMM_BINARY_INT(cmpeq)
4864 DECLARE_IMM_BINARY_FLOAT(cmpne) DECLARE_IMM_BINARY_INT(cmpne)
4865
4866 #undef DECLARE_MULTI_IMM_BINARY_INT
4867 #undef DECLARE_IMM_BINARY_FLOAT
4868 #undef DECLARE_IMM_BINARY_INT
4869 #undef DECLARE_IMM_BINARY_UINT
4870 #undef DECLARE_BINARY_FLOAT
4871 #undef DECLARE_BINARY_INT
4872 #undef DECLARE_BINARY_UINT
4873 #undef DECLARE_N_WAY_BINARY_FLOAT
4874 #undef DECLARE_N_WAY_BINARY_INT
4875 #undef DECLARE_N_WAY_BINARY_UINT
4876
4877 // Dots can be represented with multiply and add ops, but they are so foundational that it's worth
4878 // having dedicated ops.
4879 HIGHP_TAIL_STAGE(dot_2_floats, F* dst) {
4880 dst[0] = mad(dst[0], dst[2],
4881 dst[1] * dst[3]);
4882 }
4883
4884 HIGHP_TAIL_STAGE(dot_3_floats, F* dst) {
4885 dst[0] = mad(dst[0], dst[3],
4886 mad(dst[1], dst[4],
4887 dst[2] * dst[5]));
4888 }
4889
4890 HIGHP_TAIL_STAGE(dot_4_floats, F* dst) {
4891 dst[0] = mad(dst[0], dst[4],
4892 mad(dst[1], dst[5],
4893 mad(dst[2], dst[6],
4894 dst[3] * dst[7])));
4895 }
4896
4897 // MxM, VxM and MxV multiplication all use matrix_multiply. Vectors are treated like a matrix with a
4898 // single column or row.
4899 template <int N>
4900 SI void matrix_multiply(SkRasterPipelineContexts::MatrixMultiplyCtx* packed, std::byte* base) {
4901 auto ctx = SkRPCtxUtils::Unpack(packed);
4902
4903 int outColumns = ctx.rightColumns,
4904 outRows = ctx.leftRows;
4905
4906 SkASSERT(outColumns >= 1);
4907 SkASSERT(outRows >= 1);
4908 SkASSERT(outColumns <= 4);
4909 SkASSERT(outRows <= 4);
4910
4911 SkASSERT(ctx.leftColumns == ctx.rightRows);
4912 SkASSERT(N == ctx.leftColumns); // N should match the result width
4913
4914 #if !defined(SKRP_CPU_SCALAR)
4915 // This prevents Clang from generating early-out checks for zero-sized matrices.
4916 SK_ASSUME(outColumns >= 1);
4917 SK_ASSUME(outRows >= 1);
4918 SK_ASSUME(outColumns <= 4);
4919 SK_ASSUME(outRows <= 4);
4920 #endif
4921
4922 // Get pointers to the adjacent left- and right-matrices.
4923 F* resultMtx = (F*)(base + ctx.dst);
4924 F* leftMtx = &resultMtx[ctx.rightColumns * ctx.leftRows];
4925 F* rightMtx = &leftMtx[N * ctx.leftRows];
4926
4927 // Emit each matrix element.
4928 for (int c = 0; c < outColumns; ++c) {
4929 for (int r = 0; r < outRows; ++r) {
4930 // Dot a vector from leftMtx[*][r] with rightMtx[c][*].
4931 F* leftRow = &leftMtx [r];
4932 F* rightColumn = &rightMtx[c * N];
4933
4934 F element = *leftRow * *rightColumn;
4935 for (int idx = 1; idx < N; ++idx) {
4936 leftRow += outRows;
4937 rightColumn += 1;
4938 element = mad(*leftRow, *rightColumn, element);
4939 }
4940
4941 *resultMtx++ = element;
4942 }
4943 }
4944 }
4945
4946 HIGHP_TAIL_STAGE(matrix_multiply_2, SkRasterPipelineContexts::MatrixMultiplyCtx* packed) {
4947 matrix_multiply<2>(packed, base);
4948 }
4949
4950 HIGHP_TAIL_STAGE(matrix_multiply_3, SkRasterPipelineContexts::MatrixMultiplyCtx* packed) {
4951 matrix_multiply<3>(packed, base);
4952 }
4953
4954 HIGHP_TAIL_STAGE(matrix_multiply_4, SkRasterPipelineContexts::MatrixMultiplyCtx* packed) {
4955 matrix_multiply<4>(packed, base);
4956 }
4957
4958 // Refract always operates on 4-wide incident and normal vectors; for narrower inputs, the code
4959 // generator fills in the input columns with zero, and discards the extra output columns.
4960 HIGHP_TAIL_STAGE(refract_4_floats, F* dst) {
4961 // Algorithm adapted from https://registry.khronos.org/OpenGL-Refpages/gl4/html/refract.xhtml
4962 F *incident = dst + 0;
4963 F *normal = dst + 4;
4964 F eta = dst[8];
4965
4966 F dotNI = mad(normal[0], incident[0],
4967 mad(normal[1], incident[1],
4968 mad(normal[2], incident[2],
4969 normal[3] * incident[3])));
4970
4971 F k = 1.0 - eta * eta * (1.0 - dotNI * dotNI);
4972 F sqrt_k = sqrt_(k);
4973
4974 for (int idx = 0; idx < 4; ++idx) {
4975 dst[idx] = if_then_else(k >= 0,
4976 eta * incident[idx] - (eta * dotNI + sqrt_k) * normal[idx],
4977 0.0);
4978 }
4979 }
4980
4981 // Ternary operations work like binary ops (see immediately above) but take two source inputs.
4982 template <typename T, void (*ApplyFn)(T*, T*, T*)>
4983 SI void apply_adjacent_ternary(T* dst, T* src0, T* src1) {
4984 int count = src0 - dst;
4985 #if !defined(SKRP_CPU_SCALAR)
4986 SK_ASSUME(count >= 1);
4987 #endif
4988
4989 for (int index = 0; index < count; ++index) {
4990 ApplyFn(dst, src0, src1);
4991 dst += 1;
4992 src0 += 1;
4993 src1 += 1;
4994 }
4995 }
4996
4997 template <typename T, void (*ApplyFn)(T*, T*, T*)>
4998 SI void apply_adjacent_ternary_packed(SkRasterPipelineContexts::TernaryOpCtx* packed,
4999 std::byte* base) {
5000 auto ctx = SkRPCtxUtils::Unpack(packed);
5001 std::byte* dst = base + ctx.dst;
5002 std::byte* src0 = dst + ctx.delta;
5003 std::byte* src1 = src0 + ctx.delta;
5004 apply_adjacent_ternary<T, ApplyFn>((T*)dst, (T*)src0, (T*)src1);
5005 }
5006
5007 SI void mix_fn(F* a, F* x, F* y) {
5008 // We reorder the arguments here to match lerp's GLSL-style order (interpolation point last).
5009 *a = lerp(*x, *y, *a);
5010 }
5011
5012 SI void mix_fn(I32* a, I32* x, I32* y) {
5013 // We reorder the arguments here to match if_then_else's expected order (y before x).
5014 *a = if_then_else(*a, *y, *x);
5015 }
5016
5017 SI void smoothstep_fn(F* edge0, F* edge1, F* x) {
5018 F t = clamp_01_((*x - *edge0) / (*edge1 - *edge0));
5019 *edge0 = t * t * (3.0 - 2.0 * t);
5020 }
5021
5022 #define DECLARE_N_WAY_TERNARY_FLOAT(name) \
5023 HIGHP_TAIL_STAGE(name##_n_floats, SkRasterPipelineContexts::TernaryOpCtx* packed) { \
5024 apply_adjacent_ternary_packed<F, &name##_fn>(packed, base); \
5025 }
5026
5027 #define DECLARE_TERNARY_FLOAT(name) \
5028 HIGHP_TAIL_STAGE(name##_float, F* p) { apply_adjacent_ternary<F, &name##_fn>(p, p+1, p+2); } \
5029 HIGHP_TAIL_STAGE(name##_2_floats, F* p) { apply_adjacent_ternary<F, &name##_fn>(p, p+2, p+4); } \
5030 HIGHP_TAIL_STAGE(name##_3_floats, F* p) { apply_adjacent_ternary<F, &name##_fn>(p, p+3, p+6); } \
5031 HIGHP_TAIL_STAGE(name##_4_floats, F* p) { apply_adjacent_ternary<F, &name##_fn>(p, p+4, p+8); } \
5032 DECLARE_N_WAY_TERNARY_FLOAT(name)
5033
5034 #define DECLARE_TERNARY_INT(name) \
5035 HIGHP_TAIL_STAGE(name##_int, I32* p) { \
5036 apply_adjacent_ternary<I32, &name##_fn>(p, p + 1, p + 2); \
5037 } \
5038 HIGHP_TAIL_STAGE(name##_2_ints, I32* p) { \
5039 apply_adjacent_ternary<I32, &name##_fn>(p, p + 2, p + 4); \
5040 } \
5041 HIGHP_TAIL_STAGE(name##_3_ints, I32* p) { \
5042 apply_adjacent_ternary<I32, &name##_fn>(p, p + 3, p + 6); \
5043 } \
5044 HIGHP_TAIL_STAGE(name##_4_ints, I32* p) { \
5045 apply_adjacent_ternary<I32, &name##_fn>(p, p + 4, p + 8); \
5046 } \
5047 HIGHP_TAIL_STAGE(name##_n_ints, SkRasterPipelineContexts::TernaryOpCtx* packed) { \
5048 apply_adjacent_ternary_packed<I32, &name##_fn>(packed, base); \
5049 }
5050
5051 DECLARE_N_WAY_TERNARY_FLOAT(smoothstep)
5052 DECLARE_TERNARY_FLOAT(mix)
5053 DECLARE_TERNARY_INT(mix)
5054
5055 #undef DECLARE_N_WAY_TERNARY_FLOAT
5056 #undef DECLARE_TERNARY_FLOAT
5057 #undef DECLARE_TERNARY_INT
5058
5059 HIGHP_STAGE(gauss_a_to_rgba, NoCtx) {
5060 // x = 1 - x;
5061 // exp(-x * x * 4) - 0.018f;
5062 // ... now approximate with quartic
5063 //
5064 const float c4 = -2.26661229133605957031f;
5065 const float c3 = 2.89795351028442382812f;
5066 const float c2 = 0.21345567703247070312f;
5067 const float c1 = 0.15489584207534790039f;
5068 const float c0 = 0.00030726194381713867f;
5069 a = mad(a, mad(a, mad(a, mad(a, c4, c3), c2), c1), c0);
5070 r = a;
5071 g = a;
5072 b = a;
5073 }
5074
5075 SI void bilerp_clamp_large(const SkRasterPipelineContexts::GatherCtx* ctx,
5076 F* r, F* g, F* b, F* a) {
5077 // (cx,cy) are the center of our sample.
5078 F cx = *r,
5079 cy = *g;
5080
5081 // All sample points are at the same fractional offset (fx,fy).
5082 // They're the 4 corners of a logical 1x1 pixel surrounding (x,y) at (0.5,0.5) offsets.
5083 F fx = fract(cx + 0.5f),
5084 fy = fract(cy + 0.5f);
5085
5086 // We'll accumulate the color of all four samples into {r,g,b,a} directly.
5087 *r = *g = *b = *a = F0;
5088
5089 for (float py = -0.5f; py <= +0.5f; py += 1.0f)
5090 for (float px = -0.5f; px <= +0.5f; px += 1.0f) {
5091 // (x,y) are the coordinates of this sample point.
5092 F x = cx + px,
5093 y = cy + py;
5094
5095 // ix_and_ptr() will clamp to the image's bounds for us.
5096 const uint32_t* ptr;
5097 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
5098
5099 F sr,sg,sb,sa;
5100 from_8888(gather_unaligned(ptr, ix), &sr,&sg,&sb,&sa);
5101
5102 // In bilinear interpolation, the 4 pixels at +/- 0.5 offsets from the sample pixel center
5103 // are combined in direct proportion to their area overlapping that logical query pixel.
5104 // At positive offsets, the x-axis contribution to that rectangle is fx,
5105 // or (1-fx) at negative x. Same deal for y.
5106 F sx = (px > 0) ? fx : 1.0f - fx,
5107 sy = (py > 0) ? fy : 1.0f - fy,
5108 area = sx * sy;
5109
5110 *r += sr * area;
5111 *g += sg * area;
5112 *b += sb * area;
5113 *a += sa * area;
5114 }
5115 }
5116
5117 // A specialized fused image shader for clamp-x, clamp-y, non-sRGB sampling.
5118 HIGHP_STAGE(bilerp_clamp_8888, const SkRasterPipelineContexts::GatherCtx* ctx) {
5119 bilerp_clamp_large(ctx, &r, &g, &b, &a);
5120 }
5121
5122 // A specialized fused image shader for clamp-x, clamp-y, non-sRGB sampling.
5123 // This version exists to allow a shader to force high precision.
5124 HIGHP_STAGE(bilerp_clamp_8888_force_highp, const SkRasterPipelineContexts::GatherCtx* ctx) {
5125 bilerp_clamp_large(ctx, &r, &g, &b, &a);
5126 }
5127
5128 // A specialized fused image shader for clamp-x, clamp-y, non-sRGB sampling.
5129 HIGHP_STAGE(bicubic_clamp_8888, const SkRasterPipelineContexts::GatherCtx* ctx) {
5130 // (cx,cy) are the center of our sample.
5131 F cx = r,
5132 cy = g;
5133
5134 // All sample points are at the same fractional offset (fx,fy).
5135 // They're the 4 corners of a logical 1x1 pixel surrounding (x,y) at (0.5,0.5) offsets.
5136 F fx = fract(cx + 0.5f),
5137 fy = fract(cy + 0.5f);
5138
5139 // We'll accumulate the color of all four samples into {r,g,b,a} directly.
5140 r = g = b = a = F0;
5141
5142 const float* w = ctx->weights;
5143 const F scaley[4] = {bicubic_wts(fy, w[0], w[4], w[ 8], w[12]),
5144 bicubic_wts(fy, w[1], w[5], w[ 9], w[13]),
5145 bicubic_wts(fy, w[2], w[6], w[10], w[14]),
5146 bicubic_wts(fy, w[3], w[7], w[11], w[15])};
5147 const F scalex[4] = {bicubic_wts(fx, w[0], w[4], w[ 8], w[12]),
5148 bicubic_wts(fx, w[1], w[5], w[ 9], w[13]),
5149 bicubic_wts(fx, w[2], w[6], w[10], w[14]),
5150 bicubic_wts(fx, w[3], w[7], w[11], w[15])};
5151
5152 F sample_y = cy - 1.5f;
5153 for (int yy = 0; yy <= 3; ++yy) {
5154 F sample_x = cx - 1.5f;
5155 for (int xx = 0; xx <= 3; ++xx) {
5156 F scale = scalex[xx] * scaley[yy];
5157
5158 // ix_and_ptr() will clamp to the image's bounds for us.
5159 const uint32_t* ptr;
5160 U32 ix = ix_and_ptr(&ptr, ctx, sample_x, sample_y);
5161
5162 F sr,sg,sb,sa;
5163 from_8888(gather_unaligned(ptr, ix), &sr,&sg,&sb,&sa);
5164
5165 r = mad(scale, sr, r);
5166 g = mad(scale, sg, g);
5167 b = mad(scale, sb, b);
5168 a = mad(scale, sa, a);
5169
5170 sample_x += 1;
5171 }
5172 sample_y += 1;
5173 }
5174 }
5175
5176 // ~~~~~~ skgpu::Swizzle stage ~~~~~~ //
5177
5178 HIGHP_STAGE(swizzle, void* ctx) {
5179 auto ir = r, ig = g, ib = b, ia = a;
5180 F* o[] = {&r, &g, &b, &a};
5181 char swiz[4];
5182 memcpy(swiz, &ctx, sizeof(swiz));
5183
5184 for (int i = 0; i < 4; ++i) {
5185 switch (swiz[i]) {
5186 case 'r': *o[i] = ir; break;
5187 case 'g': *o[i] = ig; break;
5188 case 'b': *o[i] = ib; break;
5189 case 'a': *o[i] = ia; break;
5190 case '0': *o[i] = F0; break;
5191 case '1': *o[i] = F1; break;
5192 default: break;
5193 }
5194 }
5195 }
5196
5197 namespace lowp {
5198 #if defined(SKRP_CPU_SCALAR) || defined(SK_ENABLE_OPTIMIZE_SIZE) || \
5199 defined(SK_DISABLE_LOWP_RASTER_PIPELINE)
5200 // We don't bother generating the lowp stages if we are:
5201 // - ... in scalar mode (MSVC, old clang, etc...)
5202 // - ... trying to save code size
5203 // - ... explicitly disabling it. This is currently used by Flutter and Google3.
5204 //
5205 // Having nullptr for every stage will cause SkRasterPipeline to always use the highp stages.
5206 #define M(st) static void (*st)(void) = nullptr;
5207 SK_RASTER_PIPELINE_OPS_LOWP(M)
5208 #undef M
5209 static void (*just_return)(void) = nullptr;
5210
5211 static void start_pipeline(size_t,size_t,size_t,size_t, SkRasterPipelineStage*,
5212 SkSpan<SkRasterPipelineContexts::MemoryCtxPatch>,
5213 uint8_t* tailPointer) {}
5214
5215 #else // We are compiling vector code with Clang... let's make some lowp stages!
5216
5217 #if defined(SKRP_CPU_SKX) || defined(SKRP_CPU_HSW) || defined(SKRP_CPU_LASX)
5218 template <typename T> using V = Vec<16, T>;
5219 #else
5220 template <typename T> using V = Vec<8, T>;
5221 #endif
5222
5223 using U8 = V<uint8_t >;
5224 using U16 = V<uint16_t>;
5225 using I16 = V< int16_t>;
5226 using I32 = V< int32_t>;
5227 using U32 = V<uint32_t>;
5228 using I64 = V< int64_t>;
5229 using U64 = V<uint64_t>;
5230 using F = V<float >;
5231
5232 static constexpr size_t N = sizeof(U16) / sizeof(uint16_t);
5233
5234 // Promotion helpers (for GCC)
5235 #if defined(__clang__)
5236 SI constexpr U16 U16_(uint16_t x) { return x; }
5237 SI constexpr I32 I32_( int32_t x) { return x; }
5238 SI constexpr U32 U32_(uint32_t x) { return x; }
5239 SI constexpr F F_ (float x) { return x; }
5240 #else
5241 SI constexpr U16 U16_(uint16_t x) { return x + U16(); }
5242 SI constexpr I32 I32_( int32_t x) { return x + I32(); }
5243 SI constexpr U32 U32_(uint32_t x) { return x + U32(); }
5244 SI constexpr F F_ (float x) { return x - F (); }
5245 #endif
5246
5247 static constexpr U16 U16_0 = U16_(0),
5248 U16_255 = U16_(255);
5249
5250 // Once again, some platforms benefit from a restricted Stage calling convention,
5251 // but others can pass tons and tons of registers and we're happy to exploit that.
5252 // It's exactly the same decision and implementation strategy as the F stages above.
5253 #if SKRP_NARROW_STAGES
5254 struct Params {
5255 size_t dx, dy;
5256 U16 dr,dg,db,da;
5257 };
5258 using Stage = void (ABI*)(Params*, SkRasterPipelineStage* program, U16 r, U16 g, U16 b, U16 a);
5259 #else
5260 using Stage = void (ABI*)(SkRasterPipelineStage* program,
5261 const size_t dx, const size_t dy,
5262 U16 r, U16 g, U16 b, U16 a,
5263 U16 dr, U16 dg, U16 db, U16 da);
5264 #endif
5265
5266 static void start_pipeline(size_t x0, size_t y0,
5267 size_t xlimit, size_t ylimit,
5268 SkRasterPipelineStage* program,
5269 SkSpan<SkRasterPipelineContexts::MemoryCtxPatch> memoryCtxPatches,
5270 uint8_t* tailPointer) {
5271 uint8_t unreferencedTail;
5272 if (!tailPointer) {
5273 tailPointer = &unreferencedTail;
5274 }
5275 auto start = (Stage)program->fn;
5276 for (size_t dy = y0; dy < ylimit; dy++) {
5277 #if SKRP_NARROW_STAGES
5278 Params params = { x0,dy, U16_0,U16_0,U16_0,U16_0 };
5279 for (; params.dx + N <= xlimit; params.dx += N) {
5280 start(&params, program, U16_0,U16_0,U16_0,U16_0);
5281 }
5282 if (size_t tail = xlimit - params.dx) {
5283 *tailPointer = tail;
5284 patch_memory_contexts(memoryCtxPatches, params.dx, dy, tail);
5285 start(&params, program, U16_0,U16_0,U16_0,U16_0);
5286 restore_memory_contexts(memoryCtxPatches, params.dx, dy, tail);
5287 *tailPointer = 0xFF;
5288 }
5289 #else
5290 size_t dx = x0;
5291 for (; dx + N <= xlimit; dx += N) {
5292 start(program, dx,dy, U16_0,U16_0,U16_0,U16_0, U16_0,U16_0,U16_0,U16_0);
5293 }
5294 if (size_t tail = xlimit - dx) {
5295 *tailPointer = tail;
5296 patch_memory_contexts(memoryCtxPatches, dx, dy, tail);
5297 start(program, dx,dy, U16_0,U16_0,U16_0,U16_0, U16_0,U16_0,U16_0,U16_0);
5298 restore_memory_contexts(memoryCtxPatches, dx, dy, tail);
5299 *tailPointer = 0xFF;
5300 }
5301 #endif
5302 }
5303 }
5304
5305 #if SKRP_NARROW_STAGES
5306 static void ABI just_return(Params*, SkRasterPipelineStage*, U16,U16,U16,U16) {}
5307 #else
5308 static void ABI just_return(SkRasterPipelineStage*, size_t,size_t,
5309 U16,U16,U16,U16, U16,U16,U16,U16) {}
5310 #endif
5311
5312 // All stages use the same function call ABI to chain into each other, but there are three types:
5313 // GG: geometry in, geometry out -- think, a matrix
5314 // GP: geometry in, pixels out. -- think, a memory gather
5315 // PP: pixels in, pixels out. -- think, a blend mode
5316 //
5317 // (Some stages ignore their inputs or produce no logical output. That's perfectly fine.)
5318 //
5319 // These three LOWP_STAGE_ macros let you define each type of stage,
5320 // and will have (x,y) geometry and/or (r,g,b,a, dr,dg,db,da) pixel arguments as appropriate.
5321 //
5322 // Why does the LOWP version have 3 versions of a stage while HIGHP only has 1?
5323 // We don't want to lose precision on the x and y coordinates, so we fuse the rg and ba
5324 // registers before passing them in (and need to know if we have to split that super
5325 // register or not).
5326
5327 #if SKRP_NARROW_STAGES
5328 #define LOWP_STAGE_GG(name, ARG) \
5329 SI void name##_k(ARG, const size_t dx, const size_t dy, F& x, F& y); \
5330 static void ABI name(Params* params, SkRasterPipelineStage* program, \
5331 U16 r, U16 g, U16 b, U16 a) { \
5332 auto x = join<F>(r,g), \
5333 y = join<F>(b,a); \
5334 name##_k(Ctx{program}, params->dx,params->dy, x,y); \
5335 split(x, &r,&g); \
5336 split(y, &b,&a); \
5337 auto fn = (Stage)(++program)->fn; \
5338 fn(params, program, r,g,b,a); \
5339 } \
5340 SI void name##_k(ARG, const size_t dx, const size_t dy, F& x, F& y)
5341
5342 #define LOWP_STAGE_GP(name, ARG) \
5343 SI void name##_k(ARG, const size_t dx, const size_t dy, const F x, const F y, \
5344 U16& r, U16& g, U16& b, U16& a); \
5345 static void ABI name(Params* params, SkRasterPipelineStage* program, \
5346 U16 r, U16 g, U16 b, U16 a) { \
5347 auto x = join<F>(r,g), \
5348 y = join<F>(b,a); \
5349 name##_k(Ctx{program}, params->dx,params->dy, x,y, r,g,b,a); \
5350 auto fn = (Stage)(++program)->fn; \
5351 fn(params, program, r,g,b,a); \
5352 } \
5353 SI void name##_k(ARG, const size_t dx, const size_t dy, const F x, const F y, \
5354 U16& r, U16& g, U16& b, U16& a)
5355
5356 #define LOWP_STAGE_PP(name, ARG) \
5357 SI void name##_k(ARG, const size_t dx, const size_t dy, \
5358 U16& r, U16& g, U16& b, U16& a, \
5359 U16& dr, U16& dg, U16& db, U16& da); \
5360 static void ABI name(Params* params, SkRasterPipelineStage* program, \
5361 U16 r, U16 g, U16 b, U16 a) { \
5362 name##_k(Ctx{program}, params->dx,params->dy, r,g,b,a, \
5363 params->dr,params->dg,params->db,params->da); \
5364 auto fn = (Stage)(++program)->fn; \
5365 fn(params, program, r,g,b,a); \
5366 } \
5367 SI void name##_k(ARG, const size_t dx, const size_t dy, \
5368 U16& r, U16& g, U16& b, U16& a, \
5369 U16& dr, U16& dg, U16& db, U16& da)
5370 #else
5371 #define LOWP_STAGE_GG(name, ARG) \
5372 SI void name##_k(ARG, const size_t dx, const size_t dy, F& x, F& y); \
5373 static void ABI name(SkRasterPipelineStage* program, \
5374 const size_t dx, const size_t dy, \
5375 U16 r, U16 g, U16 b, U16 a, \
5376 U16 dr, U16 dg, U16 db, U16 da) { \
5377 auto x = join<F>(r,g), \
5378 y = join<F>(b,a); \
5379 name##_k(Ctx{program}, dx,dy, x,y); \
5380 split(x, &r,&g); \
5381 split(y, &b,&a); \
5382 auto fn = (Stage)(++program)->fn; \
5383 fn(program, dx,dy, r,g,b,a, dr,dg,db,da); \
5384 } \
5385 SI void name##_k(ARG, const size_t dx, const size_t dy, F& x, F& y)
5386
5387 #define LOWP_STAGE_GP(name, ARG) \
5388 SI void name##_k(ARG, const size_t dx, const size_t dy, const F x, const F y, \
5389 U16& r, U16& g, U16& b, U16& a); \
5390 static void ABI name(SkRasterPipelineStage* program, \
5391 const size_t dx, const size_t dy, \
5392 U16 r, U16 g, U16 b, U16 a, \
5393 U16 dr, U16 dg, U16 db, U16 da) { \
5394 auto x = join<F>(r,g), \
5395 y = join<F>(b,a); \
5396 name##_k(Ctx{program}, dx,dy, x,y, r,g,b,a); \
5397 auto fn = (Stage)(++program)->fn; \
5398 fn(program, dx,dy, r,g,b,a, dr,dg,db,da); \
5399 } \
5400 SI void name##_k(ARG, const size_t dx, const size_t dy, const F x, const F y, \
5401 U16& r, U16& g, U16& b, U16& a)
5402
5403 #define LOWP_STAGE_PP(name, ARG) \
5404 SI void name##_k(ARG, const size_t dx, const size_t dy, \
5405 U16& r, U16& g, U16& b, U16& a, \
5406 U16& dr, U16& dg, U16& db, U16& da); \
5407 static void ABI name(SkRasterPipelineStage* program, \
5408 const size_t dx, const size_t dy, \
5409 U16 r, U16 g, U16 b, U16 a, \
5410 U16 dr, U16 dg, U16 db, U16 da) { \
5411 name##_k(Ctx{program}, dx,dy, r,g,b,a, dr,dg,db,da); \
5412 auto fn = (Stage)(++program)->fn; \
5413 fn(program, dx,dy, r,g,b,a, dr,dg,db,da); \
5414 } \
5415 SI void name##_k(ARG, const size_t dx, const size_t dy, \
5416 U16& r, U16& g, U16& b, U16& a, \
5417 U16& dr, U16& dg, U16& db, U16& da)
5418 #endif
5419
5420 // ~~~~~~ Commonly used helper functions ~~~~~~ //
5421
5422 /**
5423 * Helpers to to properly rounded division (by 255). The ideal answer we want to compute is slow,
5424 * thanks to a division by a non-power of two:
5425 * [1] (v + 127) / 255
5426 *
5427 * There is a two-step process that computes the correct answer for all inputs:
5428 * [2] (v + 128 + ((v + 128) >> 8)) >> 8
5429 *
5430 * There is also a single iteration approximation, but it's wrong (+-1) ~25% of the time:
5431 * [3] (v + 255) >> 8;
5432 *
5433 * We offer two different implementations here, depending on the requirements of the calling stage.
5434 */
5435
5436 /**
5437 * div255 favors speed over accuracy. It uses formula [2] on NEON (where we can compute it as fast
5438 * as [3]), and uses [3] elsewhere.
5439 */
5440 SI U16 div255(U16 v) {
5441 #if defined(SKRP_CPU_NEON)
5442 // With NEON we can compute [2] just as fast as [3], so let's be correct.
5443 // First we compute v + ((v+128)>>8), then one more round of (...+128)>>8 to finish up:
5444 return vrshrq_n_u16(vrsraq_n_u16(v, v, 8), 8);
5445 #else
5446 // Otherwise, use [3], which is never wrong by more than 1:
5447 return (v+255)/256;
5448 #endif
5449 }
5450
5451 /**
5452 * div255_accurate guarantees the right answer on all platforms, at the expense of performance.
5453 */
5454 SI U16 div255_accurate(U16 v) {
5455 #if defined(SKRP_CPU_NEON)
5456 // Our NEON implementation of div255 is already correct for all inputs:
5457 return div255(v);
5458 #else
5459 // This is [2] (the same formulation as NEON), but written without the benefit of intrinsics:
5460 v += 128;
5461 return (v+(v/256))/256;
5462 #endif
5463 }
5464
5465 SI U16 inv(U16 v) { return 255-v; }
5466
5467 SI U16 if_then_else(I16 c, U16 t, U16 e) {
5468 return (t & sk_bit_cast<U16>(c)) | (e & sk_bit_cast<U16>(~c));
5469 }
5470 SI U32 if_then_else(I32 c, U32 t, U32 e) {
5471 return (t & sk_bit_cast<U32>(c)) | (e & sk_bit_cast<U32>(~c));
5472 }
5473
5474 SI U16 max(U16 x, U16 y) { return if_then_else(x < y, y, x); }
5475 SI U16 min(U16 x, U16 y) { return if_then_else(x < y, x, y); }
5476
5477 SI U16 max(U16 a, uint16_t b) { return max( a , U16_(b)); }
5478 SI U16 max(uint16_t a, U16 b) { return max(U16_(a), b ); }
5479 SI U16 min(U16 a, uint16_t b) { return min( a , U16_(b)); }
5480 SI U16 min(uint16_t a, U16 b) { return min(U16_(a), b ); }
5481
5482 SI U16 from_float(float f) { return U16_(f * 255.0f + 0.5f); }
5483
5484 SI U16 lerp(U16 from, U16 to, U16 t) { return div255( from*inv(t) + to*t ); }
5485
5486 template <typename D, typename S>
5487 SI D cast(S src) {
5488 return __builtin_convertvector(src, D);
5489 }
5490
5491 template <typename D, typename S>
5492 SI void split(S v, D* lo, D* hi) {
5493 static_assert(2*sizeof(D) == sizeof(S), "");
5494 memcpy(lo, (const char*)&v + 0*sizeof(D), sizeof(D));
5495 memcpy(hi, (const char*)&v + 1*sizeof(D), sizeof(D));
5496 }
5497 template <typename D, typename S>
5498 SI D join(S lo, S hi) {
5499 static_assert(sizeof(D) == 2*sizeof(S), "");
5500 D v;
5501 memcpy((char*)&v + 0*sizeof(S), &lo, sizeof(S));
5502 memcpy((char*)&v + 1*sizeof(S), &hi, sizeof(S));
5503 return v;
5504 }
5505
5506 SI F if_then_else(I32 c, F t, F e) {
5507 return sk_bit_cast<F>( (sk_bit_cast<I32>(t) & c) | (sk_bit_cast<I32>(e) & ~c) );
5508 }
5509 SI F if_then_else(I32 c, F t, float e) { return if_then_else(c, t , F_(e)); }
5510 SI F if_then_else(I32 c, float t, F e) { return if_then_else(c, F_(t), e ); }
5511
5512 SI F max(F x, F y) { return if_then_else(x < y, y, x); }
5513 SI F min(F x, F y) { return if_then_else(x < y, x, y); }
5514
5515 SI F max(F a, float b) { return max( a , F_(b)); }
5516 SI F max(float a, F b) { return max(F_(a), b ); }
5517 SI F min(F a, float b) { return min( a , F_(b)); }
5518 SI F min(float a, F b) { return min(F_(a), b ); }
5519
5520 SI I32 if_then_else(I32 c, I32 t, I32 e) {
5521 return (t & c) | (e & ~c);
5522 }
5523 SI I32 max(I32 x, I32 y) { return if_then_else(x < y, y, x); }
5524 SI I32 min(I32 x, I32 y) { return if_then_else(x < y, x, y); }
5525
5526 SI I32 max(I32 a, int32_t b) { return max( a , I32_(b)); }
5527 SI I32 max(int32_t a, I32 b) { return max(I32_(a), b ); }
5528 SI I32 min(I32 a, int32_t b) { return min( a , I32_(b)); }
5529 SI I32 min(int32_t a, I32 b) { return min(I32_(a), b ); }
5530
5531 SI F mad(F f, F m, F a) { return a+f*m; }
5532 SI F mad(F f, F m, float a) { return mad( f , m , F_(a)); }
5533 SI F mad(F f, float m, F a) { return mad( f , F_(m), a ); }
5534 SI F mad(F f, float m, float a) { return mad( f , F_(m), F_(a)); }
5535 SI F mad(float f, F m, F a) { return mad(F_(f), m , a ); }
5536 SI F mad(float f, F m, float a) { return mad(F_(f), m , F_(a)); }
5537 SI F mad(float f, float m, F a) { return mad(F_(f), F_(m), a ); }
5538
5539 SI F nmad(F f, F m, F a) { return a-f*m; }
5540 SI F nmad(F f, F m, float a) { return nmad( f , m , F_(a)); }
5541 SI F nmad(F f, float m, F a) { return nmad( f , F_(m), a ); }
5542 SI F nmad(F f, float m, float a) { return nmad( f , F_(m), F_(a)); }
5543 SI F nmad(float f, F m, F a) { return nmad(F_(f), m , a ); }
5544 SI F nmad(float f, F m, float a) { return nmad(F_(f), m , F_(a)); }
5545 SI F nmad(float f, float m, F a) { return nmad(F_(f), F_(m), a ); }
5546
5547 SI U32 trunc_(F x) { return (U32)cast<I32>(x); }
5548
5549 // Use approximate instructions and one Newton-Raphson step to calculate 1/x.
5550 SI F rcp_precise(F x) {
5551 #if defined(SKRP_CPU_SKX)
5552 F e = _mm512_rcp14_ps(x);
5553 return _mm512_fnmadd_ps(x, e, _mm512_set1_ps(2.0f)) * e;
5554 #elif defined(SKRP_CPU_HSW)
5555 __m256 lo,hi;
5556 split(x, &lo,&hi);
5557 return join<F>(SK_OPTS_NS::rcp_precise(lo), SK_OPTS_NS::rcp_precise(hi));
5558 #elif defined(SKRP_CPU_SSE2) || defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
5559 __m128 lo,hi;
5560 split(x, &lo,&hi);
5561 return join<F>(SK_OPTS_NS::rcp_precise(lo), SK_OPTS_NS::rcp_precise(hi));
5562 #elif defined(SKRP_CPU_NEON)
5563 float32x4_t lo,hi;
5564 split(x, &lo,&hi);
5565 return join<F>(SK_OPTS_NS::rcp_precise(lo), SK_OPTS_NS::rcp_precise(hi));
5566 #elif defined(SKRP_CPU_LASX)
5567 __m256 lo,hi;
5568 split(x, &lo,&hi);
5569 return join<F>(__lasx_xvfrecip_s(lo), __lasx_xvfrecip_s(hi));
5570 #elif defined(SKRP_CPU_LSX)
5571 __m128 lo,hi;
5572 split(x, &lo,&hi);
5573 return join<F>(__lsx_vfrecip_s(lo), __lsx_vfrecip_s(hi));
5574 #else
5575 return 1.0f / x;
5576 #endif
5577 }
5578 SI F sqrt_(F x) {
5579 #if defined(SKRP_CPU_SKX)
5580 return _mm512_sqrt_ps(x);
5581 #elif defined(SKRP_CPU_HSW)
5582 __m256 lo,hi;
5583 split(x, &lo,&hi);
5584 return join<F>(_mm256_sqrt_ps(lo), _mm256_sqrt_ps(hi));
5585 #elif defined(SKRP_CPU_SSE2) || defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
5586 __m128 lo,hi;
5587 split(x, &lo,&hi);
5588 return join<F>(_mm_sqrt_ps(lo), _mm_sqrt_ps(hi));
5589 #elif defined(SK_CPU_ARM64)
5590 float32x4_t lo,hi;
5591 split(x, &lo,&hi);
5592 return join<F>(vsqrtq_f32(lo), vsqrtq_f32(hi));
5593 #elif defined(SKRP_CPU_NEON)
5594 auto sqrt = [](float32x4_t v) {
5595 auto est = vrsqrteq_f32(v); // Estimate and two refinement steps for est = rsqrt(v).
5596 est *= vrsqrtsq_f32(v,est*est);
5597 est *= vrsqrtsq_f32(v,est*est);
5598 return v*est; // sqrt(v) == v*rsqrt(v).
5599 };
5600 float32x4_t lo,hi;
5601 split(x, &lo,&hi);
5602 return join<F>(sqrt(lo), sqrt(hi));
5603 #elif defined(SKRP_CPU_LASX)
5604 __m256 lo,hi;
5605 split(x, &lo,&hi);
5606 return join<F>(__lasx_xvfsqrt_s(lo), __lasx_xvfsqrt_s(hi));
5607 #elif defined(SKRP_CPU_LSX)
5608 __m128 lo,hi;
5609 split(x, &lo,&hi);
5610 return join<F>(__lsx_vfsqrt_s(lo), __lsx_vfsqrt_s(hi));
5611 #else
5612 return F{
5613 sqrtf(x[0]), sqrtf(x[1]), sqrtf(x[2]), sqrtf(x[3]),
5614 sqrtf(x[4]), sqrtf(x[5]), sqrtf(x[6]), sqrtf(x[7]),
5615 };
5616 #endif
5617 }
5618
5619 SI F floor_(F x) {
5620 #if defined(SK_CPU_ARM64)
5621 float32x4_t lo,hi;
5622 split(x, &lo,&hi);
5623 return join<F>(vrndmq_f32(lo), vrndmq_f32(hi));
5624 #elif defined(SKRP_CPU_SKX)
5625 return _mm512_floor_ps(x);
5626 #elif defined(SKRP_CPU_HSW)
5627 __m256 lo,hi;
5628 split(x, &lo,&hi);
5629 return join<F>(_mm256_floor_ps(lo), _mm256_floor_ps(hi));
5630 #elif defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
5631 __m128 lo,hi;
5632 split(x, &lo,&hi);
5633 return join<F>(_mm_floor_ps(lo), _mm_floor_ps(hi));
5634 #elif defined(SKRP_CPU_LASX)
5635 __m256 lo,hi;
5636 split(x, &lo,&hi);
5637 return join<F>(__lasx_xvfrintrm_s(lo), __lasx_xvfrintrm_s(hi));
5638 #elif defined(SKRP_CPU_LSX)
5639 __m128 lo,hi;
5640 split(x, &lo,&hi);
5641 return join<F>(__lsx_vfrintrm_s(lo), __lsx_vfrintrm_s(hi));
5642 #else
5643 F roundtrip = cast<F>(cast<I32>(x));
5644 return roundtrip - if_then_else(roundtrip > x, F_(1), F_(0));
5645 #endif
5646 }
5647
5648 // scaled_mult interprets a and b as number on [-1, 1) which are numbers in Q15 format. Functionally
5649 // this multiply is:
5650 // (2 * a * b + (1 << 15)) >> 16
5651 // The result is a number on [-1, 1).
5652 // Note: on neon this is a saturating multiply while the others are not.
5653 SI I16 scaled_mult(I16 a, I16 b) {
5654 #if defined(SKRP_CPU_SKX)
5655 return (I16)_mm256_mulhrs_epi16((__m256i)a, (__m256i)b);
5656 #elif defined(SKRP_CPU_HSW)
5657 return (I16)_mm256_mulhrs_epi16((__m256i)a, (__m256i)b);
5658 #elif defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
5659 return (I16)_mm_mulhrs_epi16((__m128i)a, (__m128i)b);
5660 #elif defined(SK_CPU_ARM64)
5661 return vqrdmulhq_s16(a, b);
5662 #elif defined(SKRP_CPU_NEON)
5663 return vqrdmulhq_s16(a, b);
5664 #elif defined(SKRP_CPU_LASX)
5665 I16 res = __lasx_xvmuh_h(a, b);
5666 return __lasx_xvslli_h(res, 1);
5667 #elif defined(SKRP_CPU_LSX)
5668 I16 res = __lsx_vmuh_h(a, b);
5669 return __lsx_vslli_h(res, 1);
5670 #else
5671 const I32 roundingTerm = I32_(1 << 14);
5672 return cast<I16>((cast<I32>(a) * cast<I32>(b) + roundingTerm) >> 15);
5673 #endif
5674 }
5675
5676 // This sum is to support lerp where the result will always be a positive number. In general,
5677 // a sum like this would require an additional bit, but because we know the range of the result
5678 // we know that the extra bit will always be zero.
5679 SI U16 constrained_add(I16 a, U16 b) {
5680 #if defined(SK_DEBUG)
5681 for (size_t i = 0; i < N; i++) {
5682 // Ensure that a + b is on the interval [0, UINT16_MAX]
5683 int ia = a[i],
5684 ib = b[i];
5685 // Use 65535 here because fuchsia's compiler evaluates UINT16_MAX - ib, which is
5686 // 65536U - ib, as an uint32_t instead of an int32_t. This was forcing ia to be
5687 // interpreted as an uint32_t.
5688 SkASSERT(-ib <= ia && ia <= 65535 - ib);
5689 }
5690 #endif
5691 return b + sk_bit_cast<U16>(a);
5692 }
5693
5694 SI F fract(F x) { return x - floor_(x); }
5695 SI F abs_(F x) { return sk_bit_cast<F>( sk_bit_cast<I32>(x) & 0x7fffffff ); }
5696
5697 // ~~~~~~ Basic / misc. stages ~~~~~~ //
5698
5699 LOWP_STAGE_GG(seed_shader, NoCtx) {
5700 #if defined(SKRP_CPU_LSX)
5701 __m128 val1 = {0.5f, 1.5f, 2.5f, 3.5f};
5702 __m128 val2 = {4.5f, 5.5f, 6.5f, 7.5f};
5703 __m128 val3 = {0.5f, 0.5f, 0.5f, 0.5f};
5704
5705 __m128i v_d = __lsx_vreplgr2vr_w(dx);
5706
5707 __m128 f_d = __lsx_vffint_s_w(v_d);
5708 val1 = __lsx_vfadd_s(val1, f_d);
5709 val2 = __lsx_vfadd_s(val2, f_d);
5710 x = join<F>(val1, val2);
5711
5712 v_d = __lsx_vreplgr2vr_w(dy);
5713 f_d = __lsx_vffint_s_w(v_d);
5714 val3 = __lsx_vfadd_s(val3, f_d);
5715 y = join<F>(val3, val3);
5716 #else
5717 static constexpr float iota[] = {
5718 0.5f, 1.5f, 2.5f, 3.5f, 4.5f, 5.5f, 6.5f, 7.5f,
5719 8.5f, 9.5f,10.5f,11.5f,12.5f,13.5f,14.5f,15.5f,
5720 };
5721 static_assert(std::size(iota) >= SkRasterPipelineContexts::kMaxStride);
5722
5723 x = cast<F>(I32_(dx)) + sk_unaligned_load<F>(iota);
5724 y = cast<F>(I32_(dy)) + 0.5f;
5725 #endif
5726 }
5727
5728 LOWP_STAGE_GG(matrix_translate, const float* m) {
5729 x += m[0];
5730 y += m[1];
5731 }
5732 LOWP_STAGE_GG(matrix_scale_translate, const float* m) {
5733 x = mad(x,m[0], m[2]);
5734 y = mad(y,m[1], m[3]);
5735 }
5736 LOWP_STAGE_GG(matrix_2x3, const float* m) {
5737 auto X = mad(x,m[0], mad(y,m[1], m[2])),
5738 Y = mad(x,m[3], mad(y,m[4], m[5]));
5739 x = X;
5740 y = Y;
5741 }
5742 LOWP_STAGE_GG(matrix_perspective, const float* m) {
5743 // N.B. Unlike the other matrix_ stages, this matrix is row-major.
5744 auto X = mad(x,m[0], mad(y,m[1], m[2])),
5745 Y = mad(x,m[3], mad(y,m[4], m[5])),
5746 Z = mad(x,m[6], mad(y,m[7], m[8]));
5747 x = X * rcp_precise(Z);
5748 y = Y * rcp_precise(Z);
5749 }
5750
5751 LOWP_STAGE_PP(uniform_color, const SkRasterPipelineContexts::UniformColorCtx* c) {
5752 r = U16_(c->rgba[0]);
5753 g = U16_(c->rgba[1]);
5754 b = U16_(c->rgba[2]);
5755 a = U16_(c->rgba[3]);
5756 }
5757 LOWP_STAGE_PP(uniform_color_dst, const SkRasterPipelineContexts::UniformColorCtx* c) {
5758 dr = U16_(c->rgba[0]);
5759 dg = U16_(c->rgba[1]);
5760 db = U16_(c->rgba[2]);
5761 da = U16_(c->rgba[3]);
5762 }
5763 LOWP_STAGE_PP(black_color, NoCtx) { r = g = b = U16_0; a = U16_255; }
5764 LOWP_STAGE_PP(white_color, NoCtx) { r = g = b = U16_255; a = U16_255; }
5765
5766 LOWP_STAGE_PP(set_rgb, const float rgb[3]) {
5767 r = from_float(rgb[0]);
5768 g = from_float(rgb[1]);
5769 b = from_float(rgb[2]);
5770 }
5771
5772 // No need to clamp against 0 here (values are unsigned)
5773 LOWP_STAGE_PP(clamp_01, NoCtx) {
5774 r = min(r, 255);
5775 g = min(g, 255);
5776 b = min(b, 255);
5777 a = min(a, 255);
5778 }
5779
5780 LOWP_STAGE_PP(clamp_a_01, NoCtx) {
5781 a = min(a, 255);
5782 }
5783
5784 LOWP_STAGE_PP(clamp_gamut, NoCtx) {
5785 a = min(a, 255);
5786 r = min(r, a);
5787 g = min(g, a);
5788 b = min(b, a);
5789 }
5790
5791 LOWP_STAGE_PP(premul, NoCtx) {
5792 r = div255_accurate(r * a);
5793 g = div255_accurate(g * a);
5794 b = div255_accurate(b * a);
5795 }
5796 LOWP_STAGE_PP(premul_dst, NoCtx) {
5797 dr = div255_accurate(dr * da);
5798 dg = div255_accurate(dg * da);
5799 db = div255_accurate(db * da);
5800 }
5801
5802 LOWP_STAGE_PP(force_opaque , NoCtx) { a = U16_255; }
5803 LOWP_STAGE_PP(force_opaque_dst, NoCtx) { da = U16_255; }
5804
5805 LOWP_STAGE_PP(swap_rb, NoCtx) {
5806 auto tmp = r;
5807 r = b;
5808 b = tmp;
5809 }
5810 LOWP_STAGE_PP(swap_rb_dst, NoCtx) {
5811 auto tmp = dr;
5812 dr = db;
5813 db = tmp;
5814 }
5815
5816 LOWP_STAGE_PP(move_src_dst, NoCtx) {
5817 dr = r;
5818 dg = g;
5819 db = b;
5820 da = a;
5821 }
5822
5823 LOWP_STAGE_PP(move_dst_src, NoCtx) {
5824 r = dr;
5825 g = dg;
5826 b = db;
5827 a = da;
5828 }
5829
5830 LOWP_STAGE_PP(swap_src_dst, NoCtx) {
5831 std::swap(r, dr);
5832 std::swap(g, dg);
5833 std::swap(b, db);
5834 std::swap(a, da);
5835 }
5836
5837 // ~~~~~~ Blend modes ~~~~~~ //
5838
5839 // The same logic applied to all 4 channels.
5840 #define BLEND_MODE(name) \
5841 SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da); \
5842 LOWP_STAGE_PP(name, NoCtx) { \
5843 r = name##_channel(r,dr,a,da); \
5844 g = name##_channel(g,dg,a,da); \
5845 b = name##_channel(b,db,a,da); \
5846 a = name##_channel(a,da,a,da); \
5847 } \
5848 SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da)
5849
5850 #if defined(SK_USE_INACCURATE_DIV255_IN_BLEND)
5851 BLEND_MODE(clear) { return U16_0; }
5852 BLEND_MODE(srcatop) { return div255( s*da + d*inv(sa) ); }
5853 BLEND_MODE(dstatop) { return div255( d*sa + s*inv(da) ); }
5854 BLEND_MODE(srcin) { return div255( s*da ); }
5855 BLEND_MODE(dstin) { return div255( d*sa ); }
5856 BLEND_MODE(srcout) { return div255( s*inv(da) ); }
5857 BLEND_MODE(dstout) { return div255( d*inv(sa) ); }
5858 BLEND_MODE(srcover) { return s + div255( d*inv(sa) ); }
5859 BLEND_MODE(dstover) { return d + div255( s*inv(da) ); }
5860 BLEND_MODE(modulate) { return div255( s*d ); }
5861 BLEND_MODE(multiply) { return div255( s*inv(da) + d*inv(sa) + s*d ); }
5862 BLEND_MODE(plus_) { return min(s+d, 255); }
5863 BLEND_MODE(screen) { return s + d - div255( s*d ); }
5864 BLEND_MODE(xor_) { return div255( s*inv(da) + d*inv(sa) ); }
5865 #else
5866 BLEND_MODE(clear) { return U16_0; }
5867 BLEND_MODE(srcatop) { return div255( s*da + d*inv(sa) ); }
5868 BLEND_MODE(dstatop) { return div255( d*sa + s*inv(da) ); }
5869 BLEND_MODE(srcin) { return div255_accurate( s*da ); }
5870 BLEND_MODE(dstin) { return div255_accurate( d*sa ); }
5871 BLEND_MODE(srcout) { return div255_accurate( s*inv(da) ); }
5872 BLEND_MODE(dstout) { return div255_accurate( d*inv(sa) ); }
5873 BLEND_MODE(srcover) { return s + div255_accurate( d*inv(sa) ); }
5874 BLEND_MODE(dstover) { return d + div255_accurate( s*inv(da) ); }
5875 BLEND_MODE(modulate) { return div255_accurate( s*d ); }
5876 BLEND_MODE(multiply) { return div255( s*inv(da) + d*inv(sa) + s*d ); }
5877 BLEND_MODE(plus_) { return min(s+d, 255); }
5878 BLEND_MODE(screen) { return s + d - div255_accurate( s*d ); }
5879 BLEND_MODE(xor_) { return div255( s*inv(da) + d*inv(sa) ); }
5880 #endif
5881 #undef BLEND_MODE
5882
5883 // The same logic applied to color, and srcover for alpha.
5884 #define BLEND_MODE(name) \
5885 SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da); \
5886 LOWP_STAGE_PP(name, NoCtx) { \
5887 r = name##_channel(r,dr,a,da); \
5888 g = name##_channel(g,dg,a,da); \
5889 b = name##_channel(b,db,a,da); \
5890 a = a + div255( da*inv(a) ); \
5891 } \
5892 SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da)
5893
5894 BLEND_MODE(darken) { return s + d - div255( max(s*da, d*sa) ); }
5895 BLEND_MODE(lighten) { return s + d - div255( min(s*da, d*sa) ); }
5896 BLEND_MODE(difference) { return s + d - 2*div255( min(s*da, d*sa) ); }
5897 BLEND_MODE(exclusion) { return s + d - 2*div255( s*d ); }
5898
5899 BLEND_MODE(hardlight) {
5900 return div255( s*inv(da) + d*inv(sa) +
5901 if_then_else(2*s <= sa, 2*s*d, sa*da - 2*(sa-s)*(da-d)) );
5902 }
5903 BLEND_MODE(overlay) {
5904 return div255( s*inv(da) + d*inv(sa) +
5905 if_then_else(2*d <= da, 2*s*d, sa*da - 2*(sa-s)*(da-d)) );
5906 }
5907 #undef BLEND_MODE
5908
5909 // ~~~~~~ Helpers for interacting with memory ~~~~~~ //
5910
5911 template <typename T>
5912 SI T* ptr_at_xy(const SkRasterPipelineContexts::MemoryCtx* ctx, const size_t dx, const size_t dy) {
5913 return (T*)ctx->pixels + dy*ctx->stride + dx;
5914 }
5915
5916 template <typename T>
5917 SI U32 ix_and_ptr(T** ptr, const SkRasterPipelineContexts::GatherCtx* ctx, F x, F y) {
5918 // Exclusive -> inclusive.
5919 const F w = F_(sk_bit_cast<float>( sk_bit_cast<uint32_t>(ctx->width ) - 1)),
5920 h = F_(sk_bit_cast<float>( sk_bit_cast<uint32_t>(ctx->height) - 1));
5921
5922 const F z = F_(std::numeric_limits<float>::min());
5923
5924 x = min(max(z, x), w);
5925 y = min(max(z, y), h);
5926
5927 x = sk_bit_cast<F>(sk_bit_cast<U32>(x) - (uint32_t)ctx->roundDownAtInteger);
5928 y = sk_bit_cast<F>(sk_bit_cast<U32>(y) - (uint32_t)ctx->roundDownAtInteger);
5929
5930 *ptr = (const T*)ctx->pixels;
5931 return trunc_(y)*ctx->stride + trunc_(x);
5932 }
5933
5934 template <typename T>
5935 SI U32 ix_and_ptr(T** ptr, const SkRasterPipelineContexts::GatherCtx* ctx, I32 x, I32 y) {
5936 // This flag doesn't make sense when the coords are integers.
5937 SkASSERT(ctx->roundDownAtInteger == 0);
5938 // Exclusive -> inclusive.
5939 const I32 w = I32_( ctx->width - 1),
5940 h = I32_(ctx->height - 1);
5941
5942 U32 ax = cast<U32>(min(max(0, x), w)),
5943 ay = cast<U32>(min(max(0, y), h));
5944
5945 *ptr = (const T*)ctx->pixels;
5946 return ay * ctx->stride + ax;
5947 }
5948
5949 template <typename V, typename T>
5950 SI V load(const T* ptr) {
5951 V v;
5952 memcpy(&v, ptr, sizeof(v));
5953 return v;
5954 }
5955 template <typename V, typename T>
5956 SI void store(T* ptr, V v) {
5957 memcpy(ptr, &v, sizeof(v));
5958 }
5959
5960 #if defined(SKRP_CPU_SKX)
5961 template <typename V, typename T>
5962 SI V gather(const T* ptr, U32 ix) {
5963 return V{ ptr[ix[ 0]], ptr[ix[ 1]], ptr[ix[ 2]], ptr[ix[ 3]],
5964 ptr[ix[ 4]], ptr[ix[ 5]], ptr[ix[ 6]], ptr[ix[ 7]],
5965 ptr[ix[ 8]], ptr[ix[ 9]], ptr[ix[10]], ptr[ix[11]],
5966 ptr[ix[12]], ptr[ix[13]], ptr[ix[14]], ptr[ix[15]], };
5967 }
5968
5969 template<>
5970 F gather(const float* ptr, U32 ix) {
5971 return _mm512_i32gather_ps((__m512i)ix, ptr, 4);
5972 }
5973
5974 template<>
5975 U32 gather(const uint32_t* ptr, U32 ix) {
5976 return (U32)_mm512_i32gather_epi32((__m512i)ix, ptr, 4);
5977 }
5978
5979 template <typename V, typename T>
5980 SI V gather_unaligned(const T* ptr, U32 ix) {
5981 return gather<V, T>(ptr, ix);
5982 }
5983 #elif defined(SKRP_CPU_HSW)
5984 template <typename V, typename T>
5985 SI V gather(const T* ptr, U32 ix) {
5986 return V{ ptr[ix[ 0]], ptr[ix[ 1]], ptr[ix[ 2]], ptr[ix[ 3]],
5987 ptr[ix[ 4]], ptr[ix[ 5]], ptr[ix[ 6]], ptr[ix[ 7]],
5988 ptr[ix[ 8]], ptr[ix[ 9]], ptr[ix[10]], ptr[ix[11]],
5989 ptr[ix[12]], ptr[ix[13]], ptr[ix[14]], ptr[ix[15]], };
5990 }
5991
5992 template<>
5993 F gather(const float* ptr, U32 ix) {
5994 __m256i lo, hi;
5995 split(ix, &lo, &hi);
5996
5997 return join<F>(_mm256_i32gather_ps(ptr, lo, 4),
5998 _mm256_i32gather_ps(ptr, hi, 4));
5999 }
6000
6001 template<>
6002 U32 gather(const uint32_t* ptr, U32 ix) {
6003 __m256i lo, hi;
6004 split(ix, &lo, &hi);
6005
6006 return join<U32>(_mm256_i32gather_epi32((const int*)ptr, lo, 4),
6007 _mm256_i32gather_epi32((const int*)ptr, hi, 4));
6008 }
6009
6010 template <typename V, typename T>
6011 SI V gather_unaligned(const T* ptr, U32 ix) {
6012 return gather<V, T>(ptr, ix);
6013 }
6014 #elif defined(SKRP_CPU_LASX)
6015 template <typename V, typename T>
6016 SI V gather(const T* ptr, U32 ix) {
6017 return V{ ptr[ix[ 0]], ptr[ix[ 1]], ptr[ix[ 2]], ptr[ix[ 3]],
6018 ptr[ix[ 4]], ptr[ix[ 5]], ptr[ix[ 6]], ptr[ix[ 7]],
6019 ptr[ix[ 8]], ptr[ix[ 9]], ptr[ix[10]], ptr[ix[11]],
6020 ptr[ix[12]], ptr[ix[13]], ptr[ix[14]], ptr[ix[15]], };
6021 }
6022
6023 template <typename V, typename T>
6024 SI V gather_unaligned(const T* ptr, U32 ix) {
6025 return gather<V, T>(ptr, ix);
6026 }
6027 #elif defined(SKRP_CPU_NEON)
6028 template <typename V, typename T>
6029 SI V gather(const T* ptr, U32 ix) {
6030 // The compiler assumes ptr is aligned, which caused crashes on some
6031 // arm32 chips because a register was marked as "aligned to 32 bits"
6032 // incorrectly. skbug.com/409859319
6033 SkASSERTF(reinterpret_cast<uintptr_t>(ptr) % alignof(T) == 0,
6034 "Should use gather_unaligned");
6035 return V{ ptr[ix[ 0]], ptr[ix[ 1]], ptr[ix[ 2]], ptr[ix[ 3]],
6036 ptr[ix[ 4]], ptr[ix[ 5]], ptr[ix[ 6]], ptr[ix[ 7]], };
6037 }
6038
6039 template <typename V, typename T>
6040 SI V gather_unaligned(const T* ptr, U32 ix) {
6041 // This tells the compiler ptr might not be aligned appropriately, so
6042 // it generates better assembly.
6043 typedef T __attribute__ ((aligned (1))) unaligned_ptr;
6044 const unaligned_ptr* uptr = static_cast<const unaligned_ptr*>(ptr);
6045 return V{ uptr[ix[ 0]], uptr[ix[ 1]], uptr[ix[ 2]], uptr[ix[ 3]],
6046 uptr[ix[ 4]], uptr[ix[ 5]], uptr[ix[ 6]], uptr[ix[ 7]], };
6047 }
6048 #else
6049 template <typename V, typename T>
6050 SI V gather(const T* ptr, U32 ix) {
6051 return V{ ptr[ix[ 0]], ptr[ix[ 1]], ptr[ix[ 2]], ptr[ix[ 3]],
6052 ptr[ix[ 4]], ptr[ix[ 5]], ptr[ix[ 6]], ptr[ix[ 7]], };
6053 }
6054
6055 template <typename V, typename T>
6056 SI V gather_unaligned(const T* ptr, U32 ix) {
6057 return gather<V, T>(ptr, ix);
6058 }
6059 #endif
6060
6061 // ~~~~~~ 32-bit memory loads and stores ~~~~~~ //
6062
6063 SI void from_8888(U32 rgba, U16* r, U16* g, U16* b, U16* a) {
6064 #if defined(SKRP_CPU_SKX)
6065 rgba = (U32)_mm512_permutexvar_epi64(_mm512_setr_epi64(0,1,4,5,2,3,6,7), (__m512i)rgba);
6066 auto cast_U16 = [](U32 v) -> U16 {
6067 return (U16)_mm256_packus_epi32(_mm512_castsi512_si256((__m512i)v),
6068 _mm512_extracti64x4_epi64((__m512i)v, 1));
6069 };
6070 #elif defined(SKRP_CPU_HSW)
6071 // Swap the middle 128-bit lanes to make _mm256_packus_epi32() in cast_U16() work out nicely.
6072 __m256i _01,_23;
6073 split(rgba, &_01, &_23);
6074 __m256i _02 = _mm256_permute2x128_si256(_01,_23, 0x20),
6075 _13 = _mm256_permute2x128_si256(_01,_23, 0x31);
6076 rgba = join<U32>(_02, _13);
6077
6078 auto cast_U16 = [](U32 v) -> U16 {
6079 __m256i _02,_13;
6080 split(v, &_02,&_13);
6081 return (U16)_mm256_packus_epi32(_02,_13);
6082 };
6083 #elif defined(SKRP_CPU_LASX)
6084 __m256i _01, _23;
6085 split(rgba, &_01, &_23);
6086 __m256i _02 = __lasx_xvpermi_q(_01, _23, 0x02),
6087 _13 = __lasx_xvpermi_q(_01, _23, 0x13);
6088 rgba = join<U32>(_02, _13);
6089
6090 auto cast_U16 = [](U32 v) -> U16 {
6091 __m256i _02,_13;
6092 split(v, &_02,&_13);
6093 __m256i tmp0 = __lasx_xvsat_wu(_02, 15);
6094 __m256i tmp1 = __lasx_xvsat_wu(_13, 15);
6095 return __lasx_xvpickev_h(tmp1, tmp0);
6096 };
6097 #elif defined(SKRP_CPU_LSX)
6098 __m128i _01, _23, rg, ba;
6099 split(rgba, &_01, &_23);
6100 rg = __lsx_vpickev_h(_23, _01);
6101 ba = __lsx_vpickod_h(_23, _01);
6102
6103 __m128i mask_00ff = __lsx_vreplgr2vr_h(0xff);
6104
6105 *r = __lsx_vand_v(rg, mask_00ff);
6106 *g = __lsx_vsrli_h(rg, 8);
6107 *b = __lsx_vand_v(ba, mask_00ff);
6108 *a = __lsx_vsrli_h(ba, 8);
6109 #else
6110 auto cast_U16 = [](U32 v) -> U16 {
6111 return cast<U16>(v);
6112 };
6113 #endif
6114 #if !defined(SKRP_CPU_LSX)
6115 *r = cast_U16(rgba & 65535) & 255;
6116 *g = cast_U16(rgba & 65535) >> 8;
6117 *b = cast_U16(rgba >> 16) & 255;
6118 *a = cast_U16(rgba >> 16) >> 8;
6119 #endif
6120 }
6121
6122 SI void load_8888_(const uint32_t* ptr, U16* r, U16* g, U16* b, U16* a) {
6123 #if defined(SKRP_CPU_NEON)
6124 uint8x8x4_t rgba = vld4_u8((const uint8_t*)(ptr));
6125 *r = cast<U16>(rgba.val[0]);
6126 *g = cast<U16>(rgba.val[1]);
6127 *b = cast<U16>(rgba.val[2]);
6128 *a = cast<U16>(rgba.val[3]);
6129 #else
6130 from_8888(load<U32>(ptr), r,g,b,a);
6131 #endif
6132 }
6133 SI void store_8888_(uint32_t* ptr, U16 r, U16 g, U16 b, U16 a) {
6134 #if defined(SKRP_CPU_LSX)
6135 __m128i mask = __lsx_vreplgr2vr_h(255);
6136 r = __lsx_vmin_hu(r, mask);
6137 g = __lsx_vmin_hu(g, mask);
6138 b = __lsx_vmin_hu(b, mask);
6139 a = __lsx_vmin_hu(a, mask);
6140
6141 g = __lsx_vslli_h(g, 8);
6142 r = r | g;
6143 a = __lsx_vslli_h(a, 8);
6144 a = a | b;
6145
6146 __m128i r_lo = __lsx_vsllwil_wu_hu(r, 0);
6147 __m128i r_hi = __lsx_vexth_wu_hu(r);
6148 __m128i a_lo = __lsx_vsllwil_wu_hu(a, 0);
6149 __m128i a_hi = __lsx_vexth_wu_hu(a);
6150
6151 a_lo = __lsx_vslli_w(a_lo, 16);
6152 a_hi = __lsx_vslli_w(a_hi, 16);
6153
6154 r = r_lo | a_lo;
6155 a = r_hi | a_hi;
6156 store(ptr, join<U32>(r, a));
6157 #else
6158 r = min(r, 255);
6159 g = min(g, 255);
6160 b = min(b, 255);
6161 a = min(a, 255);
6162
6163 #if defined(SKRP_CPU_NEON)
6164 uint8x8x4_t rgba = {{
6165 cast<U8>(r),
6166 cast<U8>(g),
6167 cast<U8>(b),
6168 cast<U8>(a),
6169 }};
6170 vst4_u8((uint8_t*)(ptr), rgba);
6171 #else
6172 store(ptr, cast<U32>(r | (g<<8)) << 0
6173 | cast<U32>(b | (a<<8)) << 16);
6174 #endif
6175 #endif
6176 }
6177
6178 LOWP_STAGE_PP(load_8888, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6179 load_8888_(ptr_at_xy<const uint32_t>(ctx, dx,dy), &r,&g,&b,&a);
6180 }
6181 LOWP_STAGE_PP(load_8888_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6182 load_8888_(ptr_at_xy<const uint32_t>(ctx, dx,dy), &dr,&dg,&db,&da);
6183 }
6184 LOWP_STAGE_PP(store_8888, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6185 store_8888_(ptr_at_xy<uint32_t>(ctx, dx,dy), r,g,b,a);
6186 }
6187 LOWP_STAGE_GP(gather_8888, const SkRasterPipelineContexts::GatherCtx* ctx) {
6188 const uint32_t* ptr;
6189 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
6190 from_8888(gather_unaligned<U32>(ptr, ix), &r, &g, &b, &a);
6191 }
6192
6193 // ~~~~~~ 16-bit memory loads and stores ~~~~~~ //
6194
6195 SI void from_565(U16 rgb, U16* r, U16* g, U16* b) {
6196 // Format for 565 buffers: 15|rrrrr gggggg bbbbb|0
6197 U16 R = (rgb >> 11) & 31,
6198 G = (rgb >> 5) & 63,
6199 B = (rgb >> 0) & 31;
6200
6201 // These bit replications are the same as multiplying by 255/31 or 255/63 to scale to 8-bit.
6202 *r = (R << 3) | (R >> 2);
6203 *g = (G << 2) | (G >> 4);
6204 *b = (B << 3) | (B >> 2);
6205 }
6206 SI void load_565_(const uint16_t* ptr, U16* r, U16* g, U16* b) {
6207 from_565(load<U16>(ptr), r,g,b);
6208 }
6209 SI void store_565_(uint16_t* ptr, U16 r, U16 g, U16 b) {
6210 r = min(r, 255);
6211 g = min(g, 255);
6212 b = min(b, 255);
6213
6214 // Round from [0,255] to [0,31] or [0,63], as if x * (31/255.0f) + 0.5f.
6215 // (Don't feel like you need to find some fundamental truth in these...
6216 // they were brute-force searched.)
6217 U16 R = (r * 9 + 36) / 74, // 9/74 ≈ 31/255, plus 36/74, about half.
6218 G = (g * 21 + 42) / 85, // 21/85 = 63/255 exactly.
6219 B = (b * 9 + 36) / 74;
6220 // Pack them back into 15|rrrrr gggggg bbbbb|0.
6221 store(ptr, R << 11
6222 | G << 5
6223 | B << 0);
6224 }
6225
6226 LOWP_STAGE_PP(load_565, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6227 load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), &r,&g,&b);
6228 a = U16_255;
6229 }
6230 LOWP_STAGE_PP(load_565_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6231 load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), &dr,&dg,&db);
6232 da = U16_255;
6233 }
6234 LOWP_STAGE_PP(store_565, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6235 store_565_(ptr_at_xy<uint16_t>(ctx, dx,dy), r,g,b);
6236 }
6237 LOWP_STAGE_GP(gather_565, const SkRasterPipelineContexts::GatherCtx* ctx) {
6238 const uint16_t* ptr;
6239 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
6240 from_565(gather_unaligned<U16>(ptr, ix), &r, &g, &b);
6241 a = U16_255;
6242 }
6243
6244 SI void from_4444(U16 rgba, U16* r, U16* g, U16* b, U16* a) {
6245 // Format for 4444 buffers: 15|rrrr gggg bbbb aaaa|0.
6246 U16 R = (rgba >> 12) & 15,
6247 G = (rgba >> 8) & 15,
6248 B = (rgba >> 4) & 15,
6249 A = (rgba >> 0) & 15;
6250
6251 // Scale [0,15] to [0,255].
6252 *r = (R << 4) | R;
6253 *g = (G << 4) | G;
6254 *b = (B << 4) | B;
6255 *a = (A << 4) | A;
6256 }
6257 SI void load_4444_(const uint16_t* ptr, U16* r, U16* g, U16* b, U16* a) {
6258 from_4444(load<U16>(ptr), r,g,b,a);
6259 }
6260 SI void store_4444_(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
6261 r = min(r, 255);
6262 g = min(g, 255);
6263 b = min(b, 255);
6264 a = min(a, 255);
6265
6266 // Round from [0,255] to [0,15], producing the same value as (x*(15/255.0f) + 0.5f).
6267 U16 R = (r + 8) / 17,
6268 G = (g + 8) / 17,
6269 B = (b + 8) / 17,
6270 A = (a + 8) / 17;
6271 // Pack them back into 15|rrrr gggg bbbb aaaa|0.
6272 store(ptr, R << 12
6273 | G << 8
6274 | B << 4
6275 | A << 0);
6276 }
6277
6278 LOWP_STAGE_PP(load_4444, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6279 load_4444_(ptr_at_xy<const uint16_t>(ctx, dx,dy), &r,&g,&b,&a);
6280 }
6281 LOWP_STAGE_PP(load_4444_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6282 load_4444_(ptr_at_xy<const uint16_t>(ctx, dx,dy), &dr,&dg,&db,&da);
6283 }
6284 LOWP_STAGE_PP(store_4444, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6285 store_4444_(ptr_at_xy<uint16_t>(ctx, dx,dy), r,g,b,a);
6286 }
6287 LOWP_STAGE_GP(gather_4444, const SkRasterPipelineContexts::GatherCtx* ctx) {
6288 const uint16_t* ptr;
6289 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
6290 from_4444(gather_unaligned<U16>(ptr, ix), &r,&g,&b,&a);
6291 }
6292
6293 SI void from_88(U16 rg, U16* r, U16* g) {
6294 *r = (rg & 0xFF);
6295 *g = (rg >> 8);
6296 }
6297
6298 SI void load_88_(const uint16_t* ptr, U16* r, U16* g) {
6299 #if defined(SKRP_CPU_NEON)
6300 uint8x8x2_t rg = vld2_u8((const uint8_t*)(ptr));
6301 *r = cast<U16>(rg.val[0]);
6302 *g = cast<U16>(rg.val[1]);
6303 #else
6304 from_88(load<U16>(ptr), r,g);
6305 #endif
6306 }
6307
6308 SI void store_88_(uint16_t* ptr, U16 r, U16 g) {
6309 r = min(r, 255);
6310 g = min(g, 255);
6311
6312 #if defined(SKRP_CPU_NEON)
6313 uint8x8x2_t rg = {{
6314 cast<U8>(r),
6315 cast<U8>(g),
6316 }};
6317 vst2_u8((uint8_t*)(ptr), rg);
6318 #else
6319 store(ptr, cast<U16>(r | (g<<8)) << 0);
6320 #endif
6321 }
6322
6323 LOWP_STAGE_PP(load_rg88, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6324 load_88_(ptr_at_xy<const uint16_t>(ctx, dx, dy), &r, &g);
6325 b = U16_0;
6326 a = U16_255;
6327 }
6328 LOWP_STAGE_PP(load_rg88_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6329 load_88_(ptr_at_xy<const uint16_t>(ctx, dx, dy), &dr, &dg);
6330 db = U16_0;
6331 da = U16_255;
6332 }
6333 LOWP_STAGE_PP(store_rg88, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6334 store_88_(ptr_at_xy<uint16_t>(ctx, dx, dy), r, g);
6335 }
6336 LOWP_STAGE_GP(gather_rg88, const SkRasterPipelineContexts::GatherCtx* ctx) {
6337 const uint16_t* ptr;
6338 U32 ix = ix_and_ptr(&ptr, ctx, x, y);
6339 from_88(gather_unaligned<U16>(ptr, ix), &r, &g);
6340 b = U16_0;
6341 a = U16_255;
6342 }
6343
6344 // ~~~~~~ 8-bit memory loads and stores ~~~~~~ //
6345
6346 SI U16 load_8(const uint8_t* ptr) {
6347 return cast<U16>(load<U8>(ptr));
6348 }
6349 SI void store_8(uint8_t* ptr, U16 v) {
6350 v = min(v, 255);
6351 store(ptr, cast<U8>(v));
6352 }
6353
6354 LOWP_STAGE_PP(load_a8, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6355 r = g = b = U16_0;
6356 a = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy));
6357 }
6358 LOWP_STAGE_PP(load_a8_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6359 dr = dg = db = U16_0;
6360 da = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy));
6361 }
6362 LOWP_STAGE_PP(store_a8, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6363 store_8(ptr_at_xy<uint8_t>(ctx, dx,dy), a);
6364 }
6365 LOWP_STAGE_GP(gather_a8, const SkRasterPipelineContexts::GatherCtx* ctx) {
6366 const uint8_t* ptr;
6367 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
6368 r = g = b = U16_0;
6369 a = cast<U16>(gather<U8>(ptr, ix));
6370 }
6371 LOWP_STAGE_PP(store_r8, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6372 store_8(ptr_at_xy<uint8_t>(ctx, dx,dy), r);
6373 }
6374
6375 LOWP_STAGE_PP(alpha_to_gray, NoCtx) {
6376 r = g = b = a;
6377 a = U16_255;
6378 }
6379 LOWP_STAGE_PP(alpha_to_gray_dst, NoCtx) {
6380 dr = dg = db = da;
6381 da = U16_255;
6382 }
6383 LOWP_STAGE_PP(alpha_to_red, NoCtx) {
6384 r = a;
6385 a = U16_255;
6386 }
6387 LOWP_STAGE_PP(alpha_to_red_dst, NoCtx) {
6388 dr = da;
6389 da = U16_255;
6390 }
6391
6392 LOWP_STAGE_PP(bt709_luminance_or_luma_to_alpha, NoCtx) {
6393 a = (r*54 + g*183 + b*19)/256; // 0.2126, 0.7152, 0.0722 with 256 denominator.
6394 r = g = b = U16_0;
6395 }
6396 LOWP_STAGE_PP(bt709_luminance_or_luma_to_rgb, NoCtx) {
6397 r = g = b =(r*54 + g*183 + b*19)/256; // 0.2126, 0.7152, 0.0722 with 256 denominator.
6398 }
6399
6400 // ~~~~~~ Coverage scales / lerps ~~~~~~ //
6401
6402 LOWP_STAGE_PP(load_src, const uint16_t* ptr) {
6403 r = sk_unaligned_load<U16>(ptr + 0*N);
6404 g = sk_unaligned_load<U16>(ptr + 1*N);
6405 b = sk_unaligned_load<U16>(ptr + 2*N);
6406 a = sk_unaligned_load<U16>(ptr + 3*N);
6407 }
6408 LOWP_STAGE_PP(store_src, uint16_t* ptr) {
6409 sk_unaligned_store(ptr + 0*N, r);
6410 sk_unaligned_store(ptr + 1*N, g);
6411 sk_unaligned_store(ptr + 2*N, b);
6412 sk_unaligned_store(ptr + 3*N, a);
6413 }
6414 LOWP_STAGE_PP(store_src_a, uint16_t* ptr) {
6415 sk_unaligned_store(ptr, a);
6416 }
6417 LOWP_STAGE_PP(load_dst, const uint16_t* ptr) {
6418 dr = sk_unaligned_load<U16>(ptr + 0*N);
6419 dg = sk_unaligned_load<U16>(ptr + 1*N);
6420 db = sk_unaligned_load<U16>(ptr + 2*N);
6421 da = sk_unaligned_load<U16>(ptr + 3*N);
6422 }
6423 LOWP_STAGE_PP(store_dst, uint16_t* ptr) {
6424 sk_unaligned_store(ptr + 0*N, dr);
6425 sk_unaligned_store(ptr + 1*N, dg);
6426 sk_unaligned_store(ptr + 2*N, db);
6427 sk_unaligned_store(ptr + 3*N, da);
6428 }
6429
6430 // ~~~~~~ Coverage scales / lerps ~~~~~~ //
6431
6432 LOWP_STAGE_PP(scale_1_float, const float* f) {
6433 U16 c = from_float(*f);
6434 r = div255( r * c );
6435 g = div255( g * c );
6436 b = div255( b * c );
6437 a = div255( a * c );
6438 }
6439 LOWP_STAGE_PP(lerp_1_float, const float* f) {
6440 U16 c = from_float(*f);
6441 r = lerp(dr, r, c);
6442 g = lerp(dg, g, c);
6443 b = lerp(db, b, c);
6444 a = lerp(da, a, c);
6445 }
6446 LOWP_STAGE_PP(scale_native, const uint16_t scales[]) {
6447 auto c = sk_unaligned_load<U16>(scales);
6448 r = div255( r * c );
6449 g = div255( g * c );
6450 b = div255( b * c );
6451 a = div255( a * c );
6452 }
6453
6454 LOWP_STAGE_PP(lerp_native, const uint16_t scales[]) {
6455 auto c = sk_unaligned_load<U16>(scales);
6456 r = lerp(dr, r, c);
6457 g = lerp(dg, g, c);
6458 b = lerp(db, b, c);
6459 a = lerp(da, a, c);
6460 }
6461
6462 LOWP_STAGE_PP(scale_u8, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6463 U16 c = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy));
6464 r = div255( r * c );
6465 g = div255( g * c );
6466 b = div255( b * c );
6467 a = div255( a * c );
6468 }
6469 LOWP_STAGE_PP(lerp_u8, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6470 U16 c = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy));
6471 r = lerp(dr, r, c);
6472 g = lerp(dg, g, c);
6473 b = lerp(db, b, c);
6474 a = lerp(da, a, c);
6475 }
6476
6477 // Derive alpha's coverage from rgb coverage and the values of src and dst alpha.
6478 SI U16 alpha_coverage_from_rgb_coverage(U16 a, U16 da, U16 cr, U16 cg, U16 cb) {
6479 return if_then_else(a < da, min(cr, min(cg,cb))
6480 , max(cr, max(cg,cb)));
6481 }
6482 LOWP_STAGE_PP(scale_565, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6483 U16 cr,cg,cb;
6484 load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), &cr,&cg,&cb);
6485 U16 ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
6486
6487 r = div255( r * cr );
6488 g = div255( g * cg );
6489 b = div255( b * cb );
6490 a = div255( a * ca );
6491 }
6492 LOWP_STAGE_PP(lerp_565, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6493 U16 cr,cg,cb;
6494 load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), &cr,&cg,&cb);
6495 U16 ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
6496
6497 r = lerp(dr, r, cr);
6498 g = lerp(dg, g, cg);
6499 b = lerp(db, b, cb);
6500 a = lerp(da, a, ca);
6501 }
6502
6503 LOWP_STAGE_PP(emboss, const SkRasterPipelineContexts::EmbossCtx* ctx) {
6504 U16 mul = load_8(ptr_at_xy<const uint8_t>(&ctx->mul, dx,dy)),
6505 add = load_8(ptr_at_xy<const uint8_t>(&ctx->add, dx,dy));
6506
6507 r = min(div255(r*mul) + add, a);
6508 g = min(div255(g*mul) + add, a);
6509 b = min(div255(b*mul) + add, a);
6510 }
6511
6512 // ~~~~~~ Gradient stages ~~~~~~ //
6513
6514 // Clamp x to [0,1], both sides inclusive (think, gradients).
6515 // Even repeat and mirror funnel through a clamp to handle bad inputs like +Inf, NaN.
6516 SI F clamp_01_(F v) { return min(max(0, v), 1); }
6517
6518 LOWP_STAGE_GG(clamp_x_1 , NoCtx) { x = clamp_01_(x); }
6519 LOWP_STAGE_GG(repeat_x_1, NoCtx) { x = clamp_01_(x - floor_(x)); }
6520 LOWP_STAGE_GG(mirror_x_1, NoCtx) {
6521 auto two = [](F x){ return x+x; };
6522 x = clamp_01_(abs_( (x-1.0f) - two(floor_((x-1.0f)*0.5f)) - 1.0f ));
6523 }
6524
6525 SI I16 cond_to_mask_16(I32 cond) { return cast<I16>(cond); }
6526
6527 LOWP_STAGE_GG(decal_x, SkRasterPipelineContexts::DecalTileCtx* ctx) {
6528 auto w = ctx->limit_x;
6529 sk_unaligned_store(ctx->mask, cond_to_mask_16((0 <= x) & (x < w)));
6530 }
6531 LOWP_STAGE_GG(decal_y, SkRasterPipelineContexts::DecalTileCtx* ctx) {
6532 auto h = ctx->limit_y;
6533 sk_unaligned_store(ctx->mask, cond_to_mask_16((0 <= y) & (y < h)));
6534 }
6535 LOWP_STAGE_GG(decal_x_and_y, SkRasterPipelineContexts::DecalTileCtx* ctx) {
6536 auto w = ctx->limit_x;
6537 auto h = ctx->limit_y;
6538 sk_unaligned_store(ctx->mask, cond_to_mask_16((0 <= x) & (x < w) & (0 <= y) & (y < h)));
6539 }
6540 LOWP_STAGE_GG(clamp_x_and_y, SkRasterPipelineContexts::CoordClampCtx* ctx) {
6541 x = min(ctx->max_x, max(ctx->min_x, x));
6542 y = min(ctx->max_y, max(ctx->min_y, y));
6543 }
6544 LOWP_STAGE_PP(check_decal_mask, SkRasterPipelineContexts::DecalTileCtx* ctx) {
6545 auto mask = sk_unaligned_load<U16>(ctx->mask);
6546 r = r & mask;
6547 g = g & mask;
6548 b = b & mask;
6549 a = a & mask;
6550 }
6551
6552 SI void round_F_to_U16(F R, F G, F B, F A, U16* r, U16* g, U16* b, U16* a) {
6553 auto round_color = [](F x) { return cast<U16>(x * 255.0f + 0.5f); };
6554
6555 *r = round_color(min(max(0, R), 1));
6556 *g = round_color(min(max(0, G), 1));
6557 *b = round_color(min(max(0, B), 1));
6558 *a = round_color(A); // we assume alpha is already in [0,1].
6559 }
6560
6561 SI void gradient_lookup(const SkRasterPipelineContexts::GradientCtx* c,
6562 U32 idx,
6563 F t,
6564 U16* r,
6565 U16* g,
6566 U16* b,
6567 U16* a) {
6568 F fr, fg, fb, fa, br, bg, bb, ba;
6569 #if defined(SKRP_CPU_HSW)
6570 if (c->stopCount <=8) {
6571 __m256i lo, hi;
6572 split(idx, &lo, &hi);
6573
6574 fr = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->factors[0]), lo),
6575 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->factors[0]), hi));
6576 br = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->biases[0]), lo),
6577 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->biases[0]), hi));
6578 fg = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->factors[1]), lo),
6579 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->factors[1]), hi));
6580 bg = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->biases[1]), lo),
6581 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->biases[1]), hi));
6582 fb = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->factors[2]), lo),
6583 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->factors[2]), hi));
6584 bb = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->biases[2]), lo),
6585 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->biases[2]), hi));
6586 fa = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->factors[3]), lo),
6587 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->factors[3]), hi));
6588 ba = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->biases[3]), lo),
6589 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->biases[3]), hi));
6590 } else
6591 #elif defined(SKRP_CPU_LASX)
6592 if (c->stopCount <= 8) {
6593 __m256i lo, hi;
6594 split(idx, &lo, &hi);
6595
6596 fr = join<F>((__m256)__lasx_xvperm_w(__lasx_xvld(c->factors[0], 0), lo),
6597 (__m256)__lasx_xvperm_w(__lasx_xvld(c->factors[0], 0), hi));
6598 br = join<F>((__m256)__lasx_xvperm_w(__lasx_xvld(c->biases[0], 0), lo),
6599 (__m256)__lasx_xvperm_w(__lasx_xvld(c->biases[0], 0), hi));
6600 fg = join<F>((__m256)__lasx_xvperm_w(__lasx_xvld(c->factors[1], 0), lo),
6601 (__m256)__lasx_xvperm_w(__lasx_xvld(c->factors[1], 0), hi));
6602 bg = join<F>((__m256)__lasx_xvperm_w(__lasx_xvld(c->biases[1], 0), lo),
6603 (__m256)__lasx_xvperm_w(__lasx_xvld(c->biases[1], 0), hi));
6604 fb = join<F>((__m256)__lasx_xvperm_w(__lasx_xvld(c->factors[2], 0), lo),
6605 (__m256)__lasx_xvperm_w(__lasx_xvld(c->factors[2], 0), hi));
6606 bb = join<F>((__m256)__lasx_xvperm_w(__lasx_xvld(c->biases[2], 0), lo),
6607 (__m256)__lasx_xvperm_w(__lasx_xvld(c->biases[2], 0), hi));
6608 fa = join<F>((__m256)__lasx_xvperm_w(__lasx_xvld(c->factors[3], 0), lo),
6609 (__m256)__lasx_xvperm_w(__lasx_xvld(c->factors[3], 0), hi));
6610 ba = join<F>((__m256)__lasx_xvperm_w(__lasx_xvld(c->biases[3], 0), lo),
6611 (__m256)__lasx_xvperm_w(__lasx_xvld(c->biases[3], 0), hi));
6612 } else
6613 #elif defined(SKRP_CPU_LSX)
6614 if (c->stopCount <= 4) {
6615 __m128i lo, hi;
6616 split(idx, &lo, &hi);
6617 __m128i zero = __lsx_vldi(0);
6618 fr = join<F>((__m128)__lsx_vshuf_w(lo, zero, __lsx_vld(c->factors[0], 0)),
6619 (__m128)__lsx_vshuf_w(hi, zero, __lsx_vld(c->factors[0], 0)));
6620 br = join<F>((__m128)__lsx_vshuf_w(lo, zero, __lsx_vld(c->biases[0], 0)),
6621 (__m128)__lsx_vshuf_w(hi, zero, __lsx_vld(c->biases[0], 0)));
6622 fg = join<F>((__m128)__lsx_vshuf_w(lo, zero, __lsx_vld(c->factors[1], 0)),
6623 (__m128)__lsx_vshuf_w(hi, zero, __lsx_vld(c->factors[1], 0)));
6624 bg = join<F>((__m128)__lsx_vshuf_w(lo, zero, __lsx_vld(c->biases[1], 0)),
6625 (__m128)__lsx_vshuf_w(hi, zero, __lsx_vld(c->biases[1], 0)));
6626 fb = join<F>((__m128)__lsx_vshuf_w(lo, zero, __lsx_vld(c->factors[2], 0)),
6627 (__m128)__lsx_vshuf_w(hi, zero, __lsx_vld(c->factors[2], 0)));
6628 bb = join<F>((__m128)__lsx_vshuf_w(lo, zero, __lsx_vld(c->biases[2], 0)),
6629 (__m128)__lsx_vshuf_w(hi, zero, __lsx_vld(c->biases[2], 0)));
6630 fa = join<F>((__m128)__lsx_vshuf_w(lo, zero, __lsx_vld(c->factors[3], 0)),
6631 (__m128)__lsx_vshuf_w(hi, zero, __lsx_vld(c->factors[3], 0)));
6632 ba = join<F>((__m128)__lsx_vshuf_w(lo, zero, __lsx_vld(c->biases[3], 0)),
6633 (__m128)__lsx_vshuf_w(hi, zero, __lsx_vld(c->biases[3], 0)));
6634 } else
6635 #endif
6636 {
6637 fr = gather<F>(c->factors[0], idx);
6638 fg = gather<F>(c->factors[1], idx);
6639 fb = gather<F>(c->factors[2], idx);
6640 fa = gather<F>(c->factors[3], idx);
6641 br = gather<F>(c->biases[0], idx);
6642 bg = gather<F>(c->biases[1], idx);
6643 bb = gather<F>(c->biases[2], idx);
6644 ba = gather<F>(c->biases[3], idx);
6645 }
6646 round_F_to_U16(mad(t, fr, br),
6647 mad(t, fg, bg),
6648 mad(t, fb, bb),
6649 mad(t, fa, ba),
6650 r,g,b,a);
6651 }
6652
6653 LOWP_STAGE_GP(gradient, const SkRasterPipelineContexts::GradientCtx* c) {
6654 auto t = x;
6655 U32 idx = U32_(0);
6656
6657 // N.B. The loop starts at 1 because idx 0 is the color to use before the first stop.
6658 for (size_t i = 1; i < c->stopCount; i++) {
6659 idx += if_then_else(t >= c->ts[i], U32_(1), U32_(0));
6660 }
6661
6662 gradient_lookup(c, idx, t, &r, &g, &b, &a);
6663 }
6664
6665 LOWP_STAGE_GP(evenly_spaced_gradient, const SkRasterPipelineContexts::GradientCtx* c) {
6666 auto t = x;
6667 auto idx = trunc_(t * static_cast<float>(c->stopCount-1));
6668 gradient_lookup(c, idx, t, &r, &g, &b, &a);
6669 }
6670
6671 LOWP_STAGE_GP(evenly_spaced_2_stop_gradient,
6672 const SkRasterPipelineContexts::EvenlySpaced2StopGradientCtx* c) {
6673 auto t = x;
6674 round_F_to_U16(mad(t, c->factor[0], c->bias[0]),
6675 mad(t, c->factor[1], c->bias[1]),
6676 mad(t, c->factor[2], c->bias[2]),
6677 mad(t, c->factor[3], c->bias[3]),
6678 &r,&g,&b,&a);
6679 }
6680
6681 LOWP_STAGE_GP(bilerp_clamp_8888, const SkRasterPipelineContexts::GatherCtx* ctx) {
6682 // Quantize sample point and transform into lerp coordinates converting them to 16.16 fixed
6683 // point number.
6684 #if defined(SKRP_CPU_LSX)
6685 __m128 _01, _23, _45, _67;
6686 v4f32 v_tmp1 = {0.5f, 0.5f, 0.5f, 0.5f};
6687 v4f32 v_tmp2 = {65536.0f, 65536.0f, 65536.0f, 65536.0f};
6688 split(x, &_01,&_23);
6689 split(y, &_45,&_67);
6690 __m128 val1 = __lsx_vfmadd_s((__m128)v_tmp2, _01, (__m128)v_tmp1);
6691 __m128 val2 = __lsx_vfmadd_s((__m128)v_tmp2, _23, (__m128)v_tmp1);
6692 __m128 val3 = __lsx_vfmadd_s((__m128)v_tmp2, _45, (__m128)v_tmp1);
6693 __m128 val4 = __lsx_vfmadd_s((__m128)v_tmp2, _67, (__m128)v_tmp1);
6694 I32 qx = cast<I32>((join<F>(__lsx_vfrintrm_s(val1), __lsx_vfrintrm_s(val2)))) - 32768,
6695 qy = cast<I32>((join<F>(__lsx_vfrintrm_s(val3), __lsx_vfrintrm_s(val4)))) - 32768;
6696 #else
6697 I32 qx = cast<I32>(floor_(65536.0f * x + 0.5f)) - 32768,
6698 qy = cast<I32>(floor_(65536.0f * y + 0.5f)) - 32768;
6699 #endif
6700
6701 // Calculate screen coordinates sx & sy by flooring qx and qy.
6702 I32 sx = qx >> 16,
6703 sy = qy >> 16;
6704
6705 // We are going to perform a change of parameters for qx on [0, 1) to tx on [-1, 1).
6706 // This will put tx in Q15 format for use with q_mult.
6707 // Calculate tx and ty on the interval of [-1, 1). Give {qx} and {qy} are on the interval
6708 // [0, 1), where {v} is fract(v), we can transform to tx in the following manner ty follows
6709 // the same math:
6710 // tx = 2 * {qx} - 1, so
6711 // {qx} = (tx + 1) / 2.
6712 // Calculate {qx} - 1 and {qy} - 1 where the {} operation is handled by the cast, and the - 1
6713 // is handled by the ^ 0x8000, dividing by 2 is deferred and handled in lerpX and lerpY in
6714 // order to use the full 16-bit resolution.
6715 #if defined(SKRP_CPU_LSX)
6716 __m128i qx_lo, qx_hi, qy_lo, qy_hi;
6717 split(qx, &qx_lo, &qx_hi);
6718 split(qy, &qy_lo, &qy_hi);
6719 __m128i temp = __lsx_vreplgr2vr_w(0x8000);
6720 qx_lo = __lsx_vxor_v(qx_lo, temp);
6721 qx_hi = __lsx_vxor_v(qx_hi, temp);
6722 qy_lo = __lsx_vxor_v(qy_lo, temp);
6723 qy_hi = __lsx_vxor_v(qy_hi, temp);
6724
6725 I16 tx = __lsx_vpickev_h(qx_hi, qx_lo);
6726 I16 ty = __lsx_vpickev_h(qy_hi, qy_lo);
6727 #else
6728 I16 tx = cast<I16>(qx ^ 0x8000),
6729 ty = cast<I16>(qy ^ 0x8000);
6730 #endif
6731
6732 // Substituting the {qx} by the equation for tx from above into the lerp equation where v is
6733 // the lerped value:
6734 // v = {qx}*(R - L) + L,
6735 // v = 1/2*(tx + 1)*(R - L) + L
6736 // 2 * v = (tx + 1)*(R - L) + 2*L
6737 // = tx*R - tx*L + R - L + 2*L
6738 // = tx*(R - L) + (R + L).
6739 // Since R and L are on [0, 255] we need them on the interval [0, 1/2] to get them into form
6740 // for Q15_mult. If L and R where in 16.16 format, this would be done by dividing by 2^9. In
6741 // code, we can multiply by 2^7 to get the value directly.
6742 // 2 * v = tx*(R - L) + (R + L)
6743 // 2^-9 * 2 * v = tx*(R - L)*2^-9 + (R + L)*2^-9
6744 // 2^-8 * v = 2^-9 * (tx*(R - L) + (R + L))
6745 // v = 1/2 * (tx*(R - L) + (R + L))
6746 auto lerpX = [&](U16 left, U16 right) -> U16 {
6747 I16 width = (I16)(right - left) << 7;
6748 U16 middle = (right + left) << 7;
6749 // The constrained_add is the most subtle part of lerp. The first term is on the interval
6750 // [-1, 1), and the second term is on the interval is on the interval [0, 1) because
6751 // both terms are too high by a factor of 2 which will be handled below. (Both R and L are
6752 // on [0, 1/2), but the sum R + L is on the interval [0, 1).) Generally, the sum below
6753 // should overflow, but because we know that sum produces an output on the
6754 // interval [0, 1) we know that the extra bit that would be needed will always be 0. So
6755 // we need to be careful to treat this sum as an unsigned positive number in the divide
6756 // by 2 below. Add +1 for rounding.
6757 U16 v2 = constrained_add(scaled_mult(tx, width), middle) + 1;
6758 // Divide by 2 to calculate v and at the same time bring the intermediate value onto the
6759 // interval [0, 1/2] to set up for the lerpY.
6760 return v2 >> 1;
6761 };
6762
6763 const uint32_t* ptr;
6764 U32 ix = ix_and_ptr(&ptr, ctx, sx, sy);
6765 U16 leftR, leftG, leftB, leftA;
6766 from_8888(gather_unaligned<U32>(ptr, ix), &leftR,&leftG,&leftB,&leftA);
6767
6768 ix = ix_and_ptr(&ptr, ctx, sx+1, sy);
6769 U16 rightR, rightG, rightB, rightA;
6770 from_8888(gather_unaligned<U32>(ptr, ix), &rightR,&rightG,&rightB,&rightA);
6771
6772 U16 topR = lerpX(leftR, rightR),
6773 topG = lerpX(leftG, rightG),
6774 topB = lerpX(leftB, rightB),
6775 topA = lerpX(leftA, rightA);
6776
6777 ix = ix_and_ptr(&ptr, ctx, sx, sy+1);
6778 from_8888(gather_unaligned<U32>(ptr, ix), &leftR,&leftG,&leftB,&leftA);
6779
6780 ix = ix_and_ptr(&ptr, ctx, sx+1, sy+1);
6781 from_8888(gather_unaligned<U32>(ptr, ix), &rightR,&rightG,&rightB,&rightA);
6782
6783 U16 bottomR = lerpX(leftR, rightR),
6784 bottomG = lerpX(leftG, rightG),
6785 bottomB = lerpX(leftB, rightB),
6786 bottomA = lerpX(leftA, rightA);
6787
6788 // lerpY plays the same mathematical tricks as lerpX, but the final divide is by 256 resulting
6789 // in a value on [0, 255].
6790 auto lerpY = [&](U16 top, U16 bottom) -> U16 {
6791 I16 width = (I16)bottom - (I16)top;
6792 U16 middle = bottom + top;
6793 // Add + 0x80 for rounding.
6794 U16 blend = constrained_add(scaled_mult(ty, width), middle) + 0x80;
6795
6796 return blend >> 8;
6797 };
6798
6799 r = lerpY(topR, bottomR);
6800 g = lerpY(topG, bottomG);
6801 b = lerpY(topB, bottomB);
6802 a = lerpY(topA, bottomA);
6803 }
6804
6805 LOWP_STAGE_GG(xy_to_unit_angle, NoCtx) {
6806 F xabs = abs_(x),
6807 yabs = abs_(y);
6808
6809 F slope = min(xabs, yabs)/max(xabs, yabs);
6810 F s = slope * slope;
6811
6812 // Use a 7th degree polynomial to approximate atan.
6813 // This was generated using sollya.gforge.inria.fr.
6814 // A float optimized polynomial was generated using the following command.
6815 // P1 = fpminimax((1/(2*Pi))*atan(x),[|1,3,5,7|],[|24...|],[2^(-40),1],relative);
6816 F phi = slope
6817 * (0.15912117063999176025390625f + s
6818 * (-5.185396969318389892578125e-2f + s
6819 * (2.476101927459239959716796875e-2f + s
6820 * (-7.0547382347285747528076171875e-3f))));
6821
6822 phi = if_then_else(xabs < yabs, 1.0f/4.0f - phi, phi);
6823 phi = if_then_else(x < 0.0f , 1.0f/2.0f - phi, phi);
6824 phi = if_then_else(y < 0.0f , 1.0f - phi , phi);
6825 phi = if_then_else(phi != phi , 0 , phi); // Check for NaN.
6826 x = phi;
6827 }
6828 LOWP_STAGE_GG(xy_to_radius, NoCtx) {
6829 x = sqrt_(x*x + y*y);
6830 }
6831
6832 // ~~~~~~ Compound stages ~~~~~~ //
6833
6834 LOWP_STAGE_PP(srcover_rgba_8888, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6835 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
6836
6837 load_8888_(ptr, &dr,&dg,&db,&da);
6838 r = r + div255( dr*inv(a) );
6839 g = g + div255( dg*inv(a) );
6840 b = b + div255( db*inv(a) );
6841 a = a + div255( da*inv(a) );
6842 store_8888_(ptr, r,g,b,a);
6843 }
6844
6845 // ~~~~~~ skgpu::Swizzle stage ~~~~~~ //
6846
6847 LOWP_STAGE_PP(swizzle, void* ctx) {
6848 auto ir = r, ig = g, ib = b, ia = a;
6849 U16* o[] = {&r, &g, &b, &a};
6850 char swiz[4];
6851 memcpy(swiz, &ctx, sizeof(swiz));
6852
6853 for (int i = 0; i < 4; ++i) {
6854 switch (swiz[i]) {
6855 case 'r': *o[i] = ir; break;
6856 case 'g': *o[i] = ig; break;
6857 case 'b': *o[i] = ib; break;
6858 case 'a': *o[i] = ia; break;
6859 case '0': *o[i] = U16_0; break;
6860 case '1': *o[i] = U16_255; break;
6861 default: break;
6862 }
6863 }
6864 }
6865
6866 // These debug stages are meant to be used with SkRasterPipelineVisualizer functions
6867 // to look at certain lanes in a pipeline at various times.
6868 // There are 3 flavors of functions for lowp, 2 for highp
6869 // debug_L_255: clamps the value of the lane to [0, 255] and puts it into
6870 // a channel of an appropriate color to visualize that component.
6871 // debug_L: Converts the value of the lane to 12.8 fixed point such that it is
6872 // easier to understand when using Viewer's debugger. A number like
6873 // 532.5 will be displayed as 0x02 0x14 0x80 0xFF. 532 -> hex is 214
6874 // 0.5 is 0x80 / 0x100 and 0xFF indicates positive (0x00 is negative).
6875 // Note on lowp there will be no fractional components nor negative numbers.
6876 // debug_x/y: (lowp only) visualizes the lane, which is a 32 bit float (combined from
6877 // rg or ba) as 12.8 fixed point (outlined above).
6878
6879 LOWP_STAGE_PP(debug_r_255, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6880 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
6881
6882 auto px = cast<U32>(min(r, 255));
6883 px |= 0xFF000000; // make opaque
6884 store(ptr, px);
6885 }
6886 LOWP_STAGE_PP(debug_g_255, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6887 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
6888
6889 auto px = cast<U32>(min(g, 255)) << 8;
6890 px |= 0xFF000000; // make opaque
6891 store(ptr, px);
6892 }
6893 LOWP_STAGE_PP(debug_b_255, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6894 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
6895
6896 auto px = cast<U32>(min(b, 255)) << 16;
6897 px |= 0xFF000000; // make opaque
6898 store(ptr, px);
6899 }
6900 LOWP_STAGE_PP(debug_a_255, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6901 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
6902
6903 auto px = cast<U32>(min(a, 255));
6904 // Render alpha as greyscale
6905 store(ptr, px | px << 8 | px << 16 | px << 24);
6906 }
6907
6908 SI void lowp_fixed_point(const SkRasterPipelineContexts::MemoryCtx* ctx,
6909 const size_t dx, const size_t dy,
6910 const F lane) {
6911 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
6912
6913 auto r2 = cast<U32>(abs_(lane) / 256) & 0xFF;
6914 auto g2 = cast<U32>(abs_(lane)) & 0xFF;
6915 auto b2 = cast<U32>(abs_(lane) * 256) & 0xFF;
6916 auto a2 = cast<U32>(min(max(lane * -256 * 256, 0), 0xFF));
6917 store(ptr, r2 | (g2 << 8) | (b2 << 16) | (a2 << 24));
6918 }
6919
6920 LOWP_STAGE_GG(debug_x, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6921 lowp_fixed_point(ctx, dx, dy, x);
6922 }
6923 LOWP_STAGE_GG(debug_y, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6924 lowp_fixed_point(ctx, dx, dy, y);
6925 }
6926
6927 // Note that there won't be negative numbers nor fractional points.
6928 SI void lowp_fixed_point(const SkRasterPipelineContexts::MemoryCtx* ctx,
6929 const size_t dx, const size_t dy,
6930 const U16 lane) {
6931 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
6932 // Flip the byte ordering so it aligns with the fixed point used above
6933 auto px = cast<U32>((lane & 0xFF00) >> 8 |
6934 (lane & 0x00FF) << 8);
6935 store(ptr, px);
6936 }
6937
6938 LOWP_STAGE_PP(debug_r, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6939 lowp_fixed_point(ctx, dx, dy, r);
6940 }
6941 LOWP_STAGE_PP(debug_g, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6942 lowp_fixed_point(ctx, dx, dy, g);
6943 }
6944 LOWP_STAGE_PP(debug_b, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6945 lowp_fixed_point(ctx, dx, dy, b);
6946 }
6947 LOWP_STAGE_PP(debug_a, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6948 lowp_fixed_point(ctx, dx, dy, a);
6949 }
6950
6951 #endif//defined(SKRP_CPU_SCALAR) controlling whether we build lowp stages
6952 } // namespace lowp
6953
6954 HIGHP_STAGE(debug_r_255, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6955 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
6956
6957 auto px = to_unorm(r, 255);
6958 px |= 0xFF000000; // make opaque
6959 store(ptr, px);
6960 }
6961 HIGHP_STAGE(debug_g_255, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6962 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
6963
6964 auto px = to_unorm(g, 255) << 8;
6965 px |= 0xFF000000; // make opaque
6966 store(ptr, px);
6967 }
6968 HIGHP_STAGE(debug_b_255, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6969 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
6970
6971 auto px = to_unorm(b, 255) << 16;
6972 px |= 0xFF000000; // make opaque
6973 store(ptr, px);
6974 }
6975 HIGHP_STAGE(debug_a_255, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6976 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
6977
6978 auto px = to_unorm(a, 255);
6979 // Render alpha as greyscale
6980 store(ptr, px | px << 8 | px << 16 | px << 24);
6981 }
6982
6983 SI void highp_fixed_point(const SkRasterPipelineContexts::MemoryCtx* ctx,
6984 const size_t dx, const size_t dy,
6985 const F lane) {
6986 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
6987
6988 auto r2 = trunc_(abs_(lane) / 256) & 0xFF;
6989 auto g2 = trunc_(abs_(lane)) & 0xFF;
6990 auto b2 = trunc_(abs_(lane) * 256) & 0xFF;
6991 auto a2 = to_unorm(lane * -256 * 256, 255);
6992 store(ptr, r2 | (g2 << 8) | (b2 << 16) | (a2 << 24));
6993 }
6994
6995 HIGHP_STAGE(debug_r, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6996 highp_fixed_point(ctx, dx, dy, r);
6997 }
6998 HIGHP_STAGE(debug_g, const SkRasterPipelineContexts::MemoryCtx* ctx) {
6999 highp_fixed_point(ctx, dx, dy, g);
7000 }
7001 HIGHP_STAGE(debug_b, const SkRasterPipelineContexts::MemoryCtx* ctx) {
7002 highp_fixed_point(ctx, dx, dy, b);
7003 }
7004 HIGHP_STAGE(debug_a, const SkRasterPipelineContexts::MemoryCtx* ctx) {
7005 highp_fixed_point(ctx, dx, dy, a);
7006 }
7007 // The special handling of x and y only make sense in lowp mode. If they are
7008 // called in highp mode (e.g. someone made a lowp pipelin and then added something
7009 // which required highp), we'll just treat x and y as r and g respectively (we have
7010 // to define *some* behavior or this won't link).
7011 HIGHP_STAGE(debug_x, const SkRasterPipelineContexts::MemoryCtx* ctx) {
7012 highp_fixed_point(ctx, dx, dy, r);
7013 }
7014 HIGHP_STAGE(debug_y, const SkRasterPipelineContexts::MemoryCtx* ctx) {
7015 highp_fixed_point(ctx, dx, dy, g);
7016 }
7017
7018 /* This gives us SK_OPTS::lowp::N if lowp::N has been set, or SK_OPTS::N if it hasn't. */
7019 namespace lowp { static constexpr size_t lowp_N = N; }
7020
7021 /** Allow outside code to access the Raster Pipeline pixel stride. */
7022 constexpr size_t raster_pipeline_lowp_stride() { return lowp::lowp_N; }
7023 constexpr size_t raster_pipeline_highp_stride() { return N; }
7024
7025 } // namespace SK_OPTS_NS
7026
7027 #undef SI
7028
7029 #endif//SkRasterPipeline_opts_DEFINED