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