| 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkRasterPipeline_opts_DEFINED |
| 9 | #define SkRasterPipeline_opts_DEFINED |
| 10 | |
| 11 | #include "include/core/SkData.h" |
| 12 | #include "include/core/SkTypes.h" |
| 13 | #include "include/private/base/SkMalloc.h" |
| 14 | #include "modules/skcms/skcms.h" |
| 15 | #include "src/base/SkUtils.h" // unaligned_{load,store} |
| 16 | #include "src/core/SkRasterPipeline.h" |
| 17 | #include "src/core/SkRasterPipelineContextUtils.h" |
| 18 | #include "src/sksl/tracing/SkSLTraceHook.h" |
| 19 | |
| 20 | #include <cstdint> |
| 21 | #include <type_traits> |
| 22 | |
| 23 | // Every function in this file should be marked static and inline using SI. |
| 24 | #if defined(__clang__) |
| 25 | #define SI __attribute__((always_inline)) static inline |
| 26 | #else |
| 27 | #define SI static inline |
| 28 | #endif |
| 29 | |
| 30 | #if defined(__clang__) |
| 31 | #define SK_UNROLL _Pragma("unroll") |
| 32 | #else |
| 33 | #define SK_UNROLL |
| 34 | #endif |
| 35 | |
| 36 | template <typename Dst, typename Src> |
| 37 | SI Dst widen_cast(const Src& src) { |
| 38 | static_assert(sizeof(Dst) > sizeof(Src)); |
| 39 | static_assert(std::is_trivially_copyable<Dst>::value); |
| 40 | static_assert(std::is_trivially_copyable<Src>::value); |
| 41 | Dst dst; |
| 42 | memcpy(&dst, &src, sizeof(Src)); |
| 43 | return dst; |
| 44 | } |
| 45 | |
| 46 | struct Ctx { |
| 47 | SkRasterPipelineStage* fStage; |
| 48 | |
| 49 | template <typename T> |
| 50 | operator T*() { |
| 51 | return (T*)fStage->ctx; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | using NoCtx = const void*; |
| 56 | |
| 57 | #if !defined(__clang__) |
| 58 | #define JUMPER_IS_SCALAR |
| 59 | #elif defined(SK_ARM_HAS_NEON) |
| 60 | #define JUMPER_IS_NEON |
| 61 | #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2 |
| 62 | #define JUMPER_IS_HSW |
| 63 | #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX |
| 64 | #define JUMPER_IS_AVX |
| 65 | #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41 |
| 66 | #define JUMPER_IS_SSE41 |
| 67 | #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 |
| 68 | #define JUMPER_IS_SSE2 |
| 69 | #else |
| 70 | #define JUMPER_IS_SCALAR |
| 71 | #endif |
| 72 | |
| 73 | // Older Clangs seem to crash when generating non-optimized NEON code for ARMv7. |
| 74 | #if defined(__clang__) && !defined(__OPTIMIZE__) && defined(SK_CPU_ARM32) |
| 75 | // Apple Clang 9 and vanilla Clang 5 are fine, and may even be conservative. |
| 76 | #if defined(__apple_build_version__) && __clang_major__ < 9 |
| 77 | #define JUMPER_IS_SCALAR |
| 78 | #elif __clang_major__ < 5 |
| 79 | #define JUMPER_IS_SCALAR |
| 80 | #endif |
| 81 | |
| 82 | #if defined(JUMPER_IS_NEON) && defined(JUMPER_IS_SCALAR) |
| 83 | #undef JUMPER_IS_NEON |
| 84 | #endif |
| 85 | #endif |
| 86 | |
| 87 | #if defined(JUMPER_IS_SCALAR) |
| 88 | #include <math.h> |
| 89 | #elif defined(JUMPER_IS_NEON) |
| 90 | #include <arm_neon.h> |
| 91 | #else |
| 92 | #include <immintrin.h> |
| 93 | #endif |
| 94 | |
| 95 | // Notes: |
| 96 | // * rcp_fast and rcp_precise both produce a reciprocal, but rcp_fast is an estimate with at least |
| 97 | // 12 bits of precision while rcp_precise should be accurate for float size. For ARM rcp_precise |
| 98 | // requires 2 Newton-Raphson refinement steps because its estimate has 8 bit precision, and for |
| 99 | // Intel this requires one additional step because its estimate has 12 bit precision. |
| 100 | |
| 101 | namespace SK_OPTS_NS { |
| 102 | #if defined(JUMPER_IS_SCALAR) |
| 103 | // This path should lead to portable scalar code. |
| 104 | using F = float ; |
| 105 | using I32 = int32_t; |
| 106 | using U64 = uint64_t; |
| 107 | using U32 = uint32_t; |
| 108 | using U16 = uint16_t; |
| 109 | using U8 = uint8_t ; |
| 110 | |
| 111 | SI F min(F a, F b) { return fminf(a,b); } |
| 112 | SI I32 min(I32 a, I32 b) { return a < b ? a : b; } |
| 113 | SI U32 min(U32 a, U32 b) { return a < b ? a : b; } |
| 114 | SI F max(F a, F b) { return fmaxf(a,b); } |
| 115 | SI I32 max(I32 a, I32 b) { return a > b ? a : b; } |
| 116 | SI U32 max(U32 a, U32 b) { return a > b ? a : b; } |
| 117 | |
| 118 | SI F mad(F f, F m, F a) { return f*m+a; } |
| 119 | SI F abs_ (F v) { return fabsf(v); } |
| 120 | SI I32 abs_ (I32 v) { return v < 0 ? -v : v; } |
| 121 | SI F floor_(F v) { return floorf(v); } |
| 122 | SI F ceil_(F v) { return ceilf(v); } |
| 123 | SI F rcp_fast(F v) { return 1.0f / v; } |
| 124 | SI F rsqrt (F v) { return 1.0f / sqrtf(v); } |
| 125 | SI F sqrt_ (F v) { return sqrtf(v); } |
| 126 | SI F rcp_precise (F v) { return 1.0f / v; } |
| 127 | |
| 128 | SI U32 round(F v) { return (uint32_t)(v + 0.5f); } |
| 129 | SI U32 round(F v, F scale) { return (uint32_t)(v*scale + 0.5f); } |
| 130 | SI U16 pack(U32 v) { return (U16)v; } |
| 131 | SI U8 pack(U16 v) { return (U8)v; } |
| 132 | |
| 133 | SI F if_then_else(I32 c, F t, F e) { return c ? t : e; } |
| 134 | SI bool any(I32 c) { return c != 0; } |
| 135 | SI bool all(I32 c) { return c != 0; } |
| 136 | |
| 137 | template <typename T> |
| 138 | SI T gather(const T* p, U32 ix) { return p[ix]; } |
| 139 | |
| 140 | template <typename T> |
| 141 | SI void scatter_masked(T src, T* dst, U32 ix, I32 mask) { |
| 142 | dst[ix] = mask ? src : dst[ix]; |
| 143 | } |
| 144 | |
| 145 | SI void load2(const uint16_t* ptr, size_t tail, U16* r, U16* g) { |
| 146 | *r = ptr[0]; |
| 147 | *g = ptr[1]; |
| 148 | } |
| 149 | SI void store2(uint16_t* ptr, size_t tail, U16 r, U16 g) { |
| 150 | ptr[0] = r; |
| 151 | ptr[1] = g; |
| 152 | } |
| 153 | SI void load3(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b) { |
| 154 | *r = ptr[0]; |
| 155 | *g = ptr[1]; |
| 156 | *b = ptr[2]; |
| 157 | } |
| 158 | SI void load4(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b, U16* a) { |
| 159 | *r = ptr[0]; |
| 160 | *g = ptr[1]; |
| 161 | *b = ptr[2]; |
| 162 | *a = ptr[3]; |
| 163 | } |
| 164 | SI void store4(uint16_t* ptr, size_t tail, U16 r, U16 g, U16 b, U16 a) { |
| 165 | ptr[0] = r; |
| 166 | ptr[1] = g; |
| 167 | ptr[2] = b; |
| 168 | ptr[3] = a; |
| 169 | } |
| 170 | |
| 171 | SI void load2(const float* ptr, size_t tail, F* r, F* g) { |
| 172 | *r = ptr[0]; |
| 173 | *g = ptr[1]; |
| 174 | } |
| 175 | SI void store2(float* ptr, size_t tail, F r, F g) { |
| 176 | ptr[0] = r; |
| 177 | ptr[1] = g; |
| 178 | } |
| 179 | SI void load4(const float* ptr, size_t tail, F* r, F* g, F* b, F* a) { |
| 180 | *r = ptr[0]; |
| 181 | *g = ptr[1]; |
| 182 | *b = ptr[2]; |
| 183 | *a = ptr[3]; |
| 184 | } |
| 185 | SI void store4(float* ptr, size_t tail, F r, F g, F b, F a) { |
| 186 | ptr[0] = r; |
| 187 | ptr[1] = g; |
| 188 | ptr[2] = b; |
| 189 | ptr[3] = a; |
| 190 | } |
| 191 | |
| 192 | #elif defined(JUMPER_IS_NEON) |
| 193 | // Since we know we're using Clang, we can use its vector extensions. |
| 194 | template <typename T> using V = T __attribute__((ext_vector_type(4))); |
| 195 | using F = V<float >; |
| 196 | using I32 = V< int32_t>; |
| 197 | using U64 = V<uint64_t>; |
| 198 | using U32 = V<uint32_t>; |
| 199 | using U16 = V<uint16_t>; |
| 200 | using U8 = V<uint8_t >; |
| 201 | |
| 202 | // We polyfill a few routines that Clang doesn't build into ext_vector_types. |
| 203 | SI F min(F a, F b) { return vminq_f32(a,b); } |
| 204 | SI I32 min(I32 a, I32 b) { return vminq_s32(a,b); } |
| 205 | SI U32 min(U32 a, U32 b) { return vminq_u32(a,b); } |
| 206 | SI F max(F a, F b) { return vmaxq_f32(a,b); } |
| 207 | SI I32 max(I32 a, I32 b) { return vmaxq_s32(a,b); } |
| 208 | SI U32 max(U32 a, U32 b) { return vmaxq_u32(a,b); } |
| 209 | |
| 210 | SI F abs_ (F v) { return vabsq_f32(v); } |
| 211 | SI I32 abs_ (I32 v) { return vabsq_s32(v); } |
| 212 | SI F rcp_fast(F v) { auto e = vrecpeq_f32 (v); return vrecpsq_f32 (v,e ) * e; } |
| 213 | SI F rcp_precise (F v) { auto e = rcp_fast(v); return vrecpsq_f32 (v,e ) * e; } |
| 214 | SI F rsqrt (F v) { auto e = vrsqrteq_f32(v); return vrsqrtsq_f32(v,e*e) * e; } |
| 215 | |
| 216 | SI U16 pack(U32 v) { return __builtin_convertvector(v, U16); } |
| 217 | SI U8 pack(U16 v) { return __builtin_convertvector(v, U8); } |
| 218 | |
| 219 | SI F if_then_else(I32 c, F t, F e) { return vbslq_f32((U32)c,t,e); } |
| 220 | |
| 221 | #if defined(SK_CPU_ARM64) |
| 222 | SI bool any(I32 c) { return vmaxvq_u32((U32)c) != 0; } |
| 223 | SI bool all(I32 c) { return vminvq_u32((U32)c) != 0; } |
| 224 | |
| 225 | SI F mad(F f, F m, F a) { return vfmaq_f32(a,f,m); } |
| 226 | SI F floor_(F v) { return vrndmq_f32(v); } |
| 227 | SI F ceil_(F v) { return vrndpq_f32(v); } |
| 228 | SI F sqrt_(F v) { return vsqrtq_f32(v); } |
| 229 | SI U32 round(F v) { return vcvtnq_u32_f32(v); } |
| 230 | SI U32 round(F v, F scale) { return vcvtnq_u32_f32(v*scale); } |
| 231 | #else |
| 232 | SI bool any(I32 c) { return c[0] | c[1] | c[2] | c[3]; } |
| 233 | SI bool all(I32 c) { return c[0] & c[1] & c[2] & c[3]; } |
| 234 | |
| 235 | SI F mad(F f, F m, F a) { return vmlaq_f32(a,f,m); } |
| 236 | SI F floor_(F v) { |
| 237 | F roundtrip = vcvtq_f32_s32(vcvtq_s32_f32(v)); |
| 238 | return roundtrip - if_then_else(roundtrip > v, 1, 0); |
| 239 | } |
| 240 | |
| 241 | SI F ceil_(F v) { |
| 242 | F roundtrip = vcvtq_f32_s32(vcvtq_s32_f32(v)); |
| 243 | return roundtrip + if_then_else(roundtrip < v, 1, 0); |
| 244 | } |
| 245 | |
| 246 | SI F sqrt_(F v) { |
| 247 | auto e = vrsqrteq_f32(v); // Estimate and two refinement steps for e = rsqrt(v). |
| 248 | e *= vrsqrtsq_f32(v,e*e); |
| 249 | e *= vrsqrtsq_f32(v,e*e); |
| 250 | return v*e; // sqrt(v) == v*rsqrt(v). |
| 251 | } |
| 252 | |
| 253 | SI U32 round(F v) { |
| 254 | return vcvtq_u32_f32(v + 0.5f); |
| 255 | } |
| 256 | |
| 257 | SI U32 round(F v, F scale) { |
| 258 | return vcvtq_u32_f32(mad(v,scale,0.5f)); |
| 259 | } |
| 260 | #endif |
| 261 | |
| 262 | template <typename T> |
| 263 | SI V<T> gather(const T* p, U32 ix) { |
| 264 | return {p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]]}; |
| 265 | } |
| 266 | template <typename V, typename S> |
| 267 | SI void scatter_masked(V src, S* dst, U32 ix, I32 mask) { |
| 268 | V before = gather(dst, ix); |
| 269 | V after = if_then_else(mask, src, before); |
| 270 | dst[ix[0]] = after[0]; |
| 271 | dst[ix[1]] = after[1]; |
| 272 | dst[ix[2]] = after[2]; |
| 273 | dst[ix[3]] = after[3]; |
| 274 | } |
| 275 | SI void load2(const uint16_t* ptr, size_t tail, U16* r, U16* g) { |
| 276 | uint16x4x2_t rg; |
| 277 | if (__builtin_expect(tail,0)) { |
| 278 | if ( true ) { rg = vld2_lane_u16(ptr + 0, rg, 0); } |
| 279 | if (tail > 1) { rg = vld2_lane_u16(ptr + 2, rg, 1); } |
| 280 | if (tail > 2) { rg = vld2_lane_u16(ptr + 4, rg, 2); } |
| 281 | } else { |
| 282 | rg = vld2_u16(ptr); |
| 283 | } |
| 284 | *r = rg.val[0]; |
| 285 | *g = rg.val[1]; |
| 286 | } |
| 287 | SI void store2(uint16_t* ptr, size_t tail, U16 r, U16 g) { |
| 288 | if (__builtin_expect(tail,0)) { |
| 289 | if ( true ) { vst2_lane_u16(ptr + 0, (uint16x4x2_t{{r,g}}), 0); } |
| 290 | if (tail > 1) { vst2_lane_u16(ptr + 2, (uint16x4x2_t{{r,g}}), 1); } |
| 291 | if (tail > 2) { vst2_lane_u16(ptr + 4, (uint16x4x2_t{{r,g}}), 2); } |
| 292 | } else { |
| 293 | vst2_u16(ptr, (uint16x4x2_t{{r,g}})); |
| 294 | } |
| 295 | } |
| 296 | SI void load3(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b) { |
| 297 | uint16x4x3_t rgb; |
| 298 | if (__builtin_expect(tail,0)) { |
| 299 | if ( true ) { rgb = vld3_lane_u16(ptr + 0, rgb, 0); } |
| 300 | if (tail > 1) { rgb = vld3_lane_u16(ptr + 3, rgb, 1); } |
| 301 | if (tail > 2) { rgb = vld3_lane_u16(ptr + 6, rgb, 2); } |
| 302 | } else { |
| 303 | rgb = vld3_u16(ptr); |
| 304 | } |
| 305 | *r = rgb.val[0]; |
| 306 | *g = rgb.val[1]; |
| 307 | *b = rgb.val[2]; |
| 308 | } |
| 309 | SI void load4(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b, U16* a) { |
| 310 | uint16x4x4_t rgba; |
| 311 | if (__builtin_expect(tail,0)) { |
| 312 | if ( true ) { rgba = vld4_lane_u16(ptr + 0, rgba, 0); } |
| 313 | if (tail > 1) { rgba = vld4_lane_u16(ptr + 4, rgba, 1); } |
| 314 | if (tail > 2) { rgba = vld4_lane_u16(ptr + 8, rgba, 2); } |
| 315 | } else { |
| 316 | rgba = vld4_u16(ptr); |
| 317 | } |
| 318 | *r = rgba.val[0]; |
| 319 | *g = rgba.val[1]; |
| 320 | *b = rgba.val[2]; |
| 321 | *a = rgba.val[3]; |
| 322 | } |
| 323 | |
| 324 | SI void store4(uint16_t* ptr, size_t tail, U16 r, U16 g, U16 b, U16 a) { |
| 325 | if (__builtin_expect(tail,0)) { |
| 326 | if ( true ) { vst4_lane_u16(ptr + 0, (uint16x4x4_t{{r,g,b,a}}), 0); } |
| 327 | if (tail > 1) { vst4_lane_u16(ptr + 4, (uint16x4x4_t{{r,g,b,a}}), 1); } |
| 328 | if (tail > 2) { vst4_lane_u16(ptr + 8, (uint16x4x4_t{{r,g,b,a}}), 2); } |
| 329 | } else { |
| 330 | vst4_u16(ptr, (uint16x4x4_t{{r,g,b,a}})); |
| 331 | } |
| 332 | } |
| 333 | SI void load2(const float* ptr, size_t tail, F* r, F* g) { |
| 334 | float32x4x2_t rg; |
| 335 | if (__builtin_expect(tail,0)) { |
| 336 | if ( true ) { rg = vld2q_lane_f32(ptr + 0, rg, 0); } |
| 337 | if (tail > 1) { rg = vld2q_lane_f32(ptr + 2, rg, 1); } |
| 338 | if (tail > 2) { rg = vld2q_lane_f32(ptr + 4, rg, 2); } |
| 339 | } else { |
| 340 | rg = vld2q_f32(ptr); |
| 341 | } |
| 342 | *r = rg.val[0]; |
| 343 | *g = rg.val[1]; |
| 344 | } |
| 345 | SI void store2(float* ptr, size_t tail, F r, F g) { |
| 346 | if (__builtin_expect(tail,0)) { |
| 347 | if ( true ) { vst2q_lane_f32(ptr + 0, (float32x4x2_t{{r,g}}), 0); } |
| 348 | if (tail > 1) { vst2q_lane_f32(ptr + 2, (float32x4x2_t{{r,g}}), 1); } |
| 349 | if (tail > 2) { vst2q_lane_f32(ptr + 4, (float32x4x2_t{{r,g}}), 2); } |
| 350 | } else { |
| 351 | vst2q_f32(ptr, (float32x4x2_t{{r,g}})); |
| 352 | } |
| 353 | } |
| 354 | SI void load4(const float* ptr, size_t tail, F* r, F* g, F* b, F* a) { |
| 355 | float32x4x4_t rgba; |
| 356 | if (__builtin_expect(tail,0)) { |
| 357 | if ( true ) { rgba = vld4q_lane_f32(ptr + 0, rgba, 0); } |
| 358 | if (tail > 1) { rgba = vld4q_lane_f32(ptr + 4, rgba, 1); } |
| 359 | if (tail > 2) { rgba = vld4q_lane_f32(ptr + 8, rgba, 2); } |
| 360 | } else { |
| 361 | rgba = vld4q_f32(ptr); |
| 362 | } |
| 363 | *r = rgba.val[0]; |
| 364 | *g = rgba.val[1]; |
| 365 | *b = rgba.val[2]; |
| 366 | *a = rgba.val[3]; |
| 367 | } |
| 368 | SI void store4(float* ptr, size_t tail, F r, F g, F b, F a) { |
| 369 | if (__builtin_expect(tail,0)) { |
| 370 | if ( true ) { vst4q_lane_f32(ptr + 0, (float32x4x4_t{{r,g,b,a}}), 0); } |
| 371 | if (tail > 1) { vst4q_lane_f32(ptr + 4, (float32x4x4_t{{r,g,b,a}}), 1); } |
| 372 | if (tail > 2) { vst4q_lane_f32(ptr + 8, (float32x4x4_t{{r,g,b,a}}), 2); } |
| 373 | } else { |
| 374 | vst4q_f32(ptr, (float32x4x4_t{{r,g,b,a}})); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | #elif defined(JUMPER_IS_HSW) |
| 379 | // These are __m256 and __m256i, but friendlier and strongly-typed. |
| 380 | template <typename T> using V = T __attribute__((ext_vector_type(8))); |
| 381 | using F = V<float >; |
| 382 | using I32 = V< int32_t>; |
| 383 | using U64 = V<uint64_t>; |
| 384 | using U32 = V<uint32_t>; |
| 385 | using U16 = V<uint16_t>; |
| 386 | using U8 = V<uint8_t >; |
| 387 | |
| 388 | SI F mad(F f, F m, F a) { return _mm256_fmadd_ps(f, m, a); } |
| 389 | |
| 390 | SI F min(F a, F b) { return _mm256_min_ps(a,b); } |
| 391 | SI I32 min(I32 a, I32 b) { return _mm256_min_epi32(a,b); } |
| 392 | SI U32 min(U32 a, U32 b) { return _mm256_min_epu32(a,b); } |
| 393 | SI F max(F a, F b) { return _mm256_max_ps(a,b); } |
| 394 | SI I32 max(I32 a, I32 b) { return _mm256_max_epi32(a,b); } |
| 395 | SI U32 max(U32 a, U32 b) { return _mm256_max_epu32(a,b); } |
| 396 | |
| 397 | SI F abs_ (F v) { return _mm256_and_ps(v, 0-v); } |
| 398 | SI I32 abs_ (I32 v) { return _mm256_abs_epi32(v); } |
| 399 | SI F floor_(F v) { return _mm256_floor_ps(v); } |
| 400 | SI F ceil_(F v) { return _mm256_ceil_ps(v); } |
| 401 | SI F rcp_fast(F v) { return _mm256_rcp_ps (v); } |
| 402 | SI F rsqrt (F v) { return _mm256_rsqrt_ps(v); } |
| 403 | SI F sqrt_ (F v) { return _mm256_sqrt_ps (v); } |
| 404 | SI F rcp_precise (F v) { |
| 405 | F e = rcp_fast(v); |
| 406 | return _mm256_fnmadd_ps(v, e, _mm256_set1_ps(2.0f)) * e; |
| 407 | } |
| 408 | |
| 409 | SI U32 round(F v) { return _mm256_cvtps_epi32(v); } |
| 410 | SI U32 round(F v, F scale) { return _mm256_cvtps_epi32(v*scale); } |
| 411 | SI U16 pack(U32 v) { |
| 412 | return _mm_packus_epi32(_mm256_extractf128_si256(v, 0), |
| 413 | _mm256_extractf128_si256(v, 1)); |
| 414 | } |
| 415 | SI U8 pack(U16 v) { |
| 416 | auto r = _mm_packus_epi16(v,v); |
| 417 | return sk_unaligned_load<U8>(&r); |
| 418 | } |
| 419 | |
| 420 | SI F if_then_else(I32 c, F t, F e) { return _mm256_blendv_ps(e,t,c); } |
| 421 | // NOTE: This version of 'all' only works with mask values (true == all bits set) |
| 422 | SI bool any(I32 c) { return !_mm256_testz_si256(c, _mm256_set1_epi32(-1)); } |
| 423 | SI bool all(I32 c) { return _mm256_testc_si256(c, _mm256_set1_epi32(-1)); } |
| 424 | |
| 425 | template <typename T> |
| 426 | SI V<T> gather(const T* p, U32 ix) { |
| 427 | return { p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]], |
| 428 | p[ix[4]], p[ix[5]], p[ix[6]], p[ix[7]], }; |
| 429 | } |
| 430 | SI F gather(const float* p, U32 ix) { return _mm256_i32gather_ps (p, ix, 4); } |
| 431 | SI U32 gather(const uint32_t* p, U32 ix) { return _mm256_i32gather_epi32(p, ix, 4); } |
| 432 | SI U64 gather(const uint64_t* p, U32 ix) { |
| 433 | __m256i parts[] = { |
| 434 | _mm256_i32gather_epi64(p, _mm256_extracti128_si256(ix,0), 8), |
| 435 | _mm256_i32gather_epi64(p, _mm256_extracti128_si256(ix,1), 8), |
| 436 | }; |
| 437 | return sk_bit_cast<U64>(parts); |
| 438 | } |
| 439 | template <typename V, typename S> |
| 440 | SI void scatter_masked(V src, S* dst, U32 ix, I32 mask) { |
| 441 | V before = gather(dst, ix); |
| 442 | V after = if_then_else(mask, src, before); |
| 443 | dst[ix[0]] = after[0]; |
| 444 | dst[ix[1]] = after[1]; |
| 445 | dst[ix[2]] = after[2]; |
| 446 | dst[ix[3]] = after[3]; |
| 447 | dst[ix[4]] = after[4]; |
| 448 | dst[ix[5]] = after[5]; |
| 449 | dst[ix[6]] = after[6]; |
| 450 | dst[ix[7]] = after[7]; |
| 451 | } |
| 452 | |
| 453 | SI void load2(const uint16_t* ptr, size_t tail, U16* r, U16* g) { |
| 454 | U16 _0123, _4567; |
| 455 | if (__builtin_expect(tail,0)) { |
| 456 | _0123 = _4567 = _mm_setzero_si128(); |
| 457 | auto* d = &_0123; |
| 458 | if (tail > 3) { |
| 459 | *d = _mm_loadu_si128(((__m128i*)ptr) + 0); |
| 460 | tail -= 4; |
| 461 | ptr += 8; |
| 462 | d = &_4567; |
| 463 | } |
| 464 | bool high = false; |
| 465 | if (tail > 1) { |
| 466 | *d = _mm_loadu_si64(ptr); |
| 467 | tail -= 2; |
| 468 | ptr += 4; |
| 469 | high = true; |
| 470 | } |
| 471 | if (tail > 0) { |
| 472 | (*d)[high ? 4 : 0] = *(ptr + 0); |
| 473 | (*d)[high ? 5 : 1] = *(ptr + 1); |
| 474 | } |
| 475 | } else { |
| 476 | _0123 = _mm_loadu_si128(((__m128i*)ptr) + 0); |
| 477 | _4567 = _mm_loadu_si128(((__m128i*)ptr) + 1); |
| 478 | } |
| 479 | *r = _mm_packs_epi32(_mm_srai_epi32(_mm_slli_epi32(_0123, 16), 16), |
| 480 | _mm_srai_epi32(_mm_slli_epi32(_4567, 16), 16)); |
| 481 | *g = _mm_packs_epi32(_mm_srai_epi32(_0123, 16), |
| 482 | _mm_srai_epi32(_4567, 16)); |
| 483 | } |
| 484 | SI void store2(uint16_t* ptr, size_t tail, U16 r, U16 g) { |
| 485 | auto _0123 = _mm_unpacklo_epi16(r, g), |
| 486 | _4567 = _mm_unpackhi_epi16(r, g); |
| 487 | if (__builtin_expect(tail,0)) { |
| 488 | const auto* s = &_0123; |
| 489 | if (tail > 3) { |
| 490 | _mm_storeu_si128((__m128i*)ptr, *s); |
| 491 | s = &_4567; |
| 492 | tail -= 4; |
| 493 | ptr += 8; |
| 494 | } |
| 495 | bool high = false; |
| 496 | if (tail > 1) { |
| 497 | _mm_storel_epi64((__m128i*)ptr, *s); |
| 498 | ptr += 4; |
| 499 | tail -= 2; |
| 500 | high = true; |
| 501 | } |
| 502 | if (tail > 0) { |
| 503 | if (high) { |
| 504 | *(int32_t*)ptr = _mm_extract_epi32(*s, 2); |
| 505 | } else { |
| 506 | *(int32_t*)ptr = _mm_cvtsi128_si32(*s); |
| 507 | } |
| 508 | } |
| 509 | } else { |
| 510 | _mm_storeu_si128((__m128i*)ptr + 0, _0123); |
| 511 | _mm_storeu_si128((__m128i*)ptr + 1, _4567); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | SI void load3(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b) { |
| 516 | __m128i _0,_1,_2,_3,_4,_5,_6,_7; |
| 517 | if (__builtin_expect(tail,0)) { |
| 518 | auto load_rgb = [](const uint16_t* src) { |
| 519 | auto v = _mm_cvtsi32_si128(*(const uint32_t*)src); |
| 520 | return _mm_insert_epi16(v, src[2], 2); |
| 521 | }; |
| 522 | _1 = _2 = _3 = _4 = _5 = _6 = _7 = _mm_setzero_si128(); |
| 523 | if ( true ) { _0 = load_rgb(ptr + 0); } |
| 524 | if (tail > 1) { _1 = load_rgb(ptr + 3); } |
| 525 | if (tail > 2) { _2 = load_rgb(ptr + 6); } |
| 526 | if (tail > 3) { _3 = load_rgb(ptr + 9); } |
| 527 | if (tail > 4) { _4 = load_rgb(ptr + 12); } |
| 528 | if (tail > 5) { _5 = load_rgb(ptr + 15); } |
| 529 | if (tail > 6) { _6 = load_rgb(ptr + 18); } |
| 530 | } else { |
| 531 | // Load 0+1, 2+3, 4+5 normally, and 6+7 backed up 4 bytes so we don't run over. |
| 532 | auto _01 = _mm_loadu_si128((const __m128i*)(ptr + 0)) ; |
| 533 | auto _23 = _mm_loadu_si128((const __m128i*)(ptr + 6)) ; |
| 534 | auto _45 = _mm_loadu_si128((const __m128i*)(ptr + 12)) ; |
| 535 | auto _67 = _mm_srli_si128(_mm_loadu_si128((const __m128i*)(ptr + 16)), 4); |
| 536 | _0 = _01; _1 = _mm_srli_si128(_01, 6); |
| 537 | _2 = _23; _3 = _mm_srli_si128(_23, 6); |
| 538 | _4 = _45; _5 = _mm_srli_si128(_45, 6); |
| 539 | _6 = _67; _7 = _mm_srli_si128(_67, 6); |
| 540 | } |
| 541 | |
| 542 | auto _02 = _mm_unpacklo_epi16(_0, _2), // r0 r2 g0 g2 b0 b2 xx xx |
| 543 | _13 = _mm_unpacklo_epi16(_1, _3), |
| 544 | _46 = _mm_unpacklo_epi16(_4, _6), |
| 545 | _57 = _mm_unpacklo_epi16(_5, _7); |
| 546 | |
| 547 | auto rg0123 = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3 |
| 548 | bx0123 = _mm_unpackhi_epi16(_02, _13), // b0 b1 b2 b3 xx xx xx xx |
| 549 | rg4567 = _mm_unpacklo_epi16(_46, _57), |
| 550 | bx4567 = _mm_unpackhi_epi16(_46, _57); |
| 551 | |
| 552 | *r = _mm_unpacklo_epi64(rg0123, rg4567); |
| 553 | *g = _mm_unpackhi_epi64(rg0123, rg4567); |
| 554 | *b = _mm_unpacklo_epi64(bx0123, bx4567); |
| 555 | } |
| 556 | SI void load4(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b, U16* a) { |
| 557 | __m128i _01, _23, _45, _67; |
| 558 | if (__builtin_expect(tail,0)) { |
| 559 | auto src = (const double*)ptr; |
| 560 | _01 = _23 = _45 = _67 = _mm_setzero_si128(); |
| 561 | if (tail > 0) { _01 = _mm_loadl_pd(_01, src+0); } |
| 562 | if (tail > 1) { _01 = _mm_loadh_pd(_01, src+1); } |
| 563 | if (tail > 2) { _23 = _mm_loadl_pd(_23, src+2); } |
| 564 | if (tail > 3) { _23 = _mm_loadh_pd(_23, src+3); } |
| 565 | if (tail > 4) { _45 = _mm_loadl_pd(_45, src+4); } |
| 566 | if (tail > 5) { _45 = _mm_loadh_pd(_45, src+5); } |
| 567 | if (tail > 6) { _67 = _mm_loadl_pd(_67, src+6); } |
| 568 | } else { |
| 569 | _01 = _mm_loadu_si128(((__m128i*)ptr) + 0); |
| 570 | _23 = _mm_loadu_si128(((__m128i*)ptr) + 1); |
| 571 | _45 = _mm_loadu_si128(((__m128i*)ptr) + 2); |
| 572 | _67 = _mm_loadu_si128(((__m128i*)ptr) + 3); |
| 573 | } |
| 574 | |
| 575 | auto _02 = _mm_unpacklo_epi16(_01, _23), // r0 r2 g0 g2 b0 b2 a0 a2 |
| 576 | _13 = _mm_unpackhi_epi16(_01, _23), // r1 r3 g1 g3 b1 b3 a1 a3 |
| 577 | _46 = _mm_unpacklo_epi16(_45, _67), |
| 578 | _57 = _mm_unpackhi_epi16(_45, _67); |
| 579 | |
| 580 | auto rg0123 = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3 |
| 581 | ba0123 = _mm_unpackhi_epi16(_02, _13), // b0 b1 b2 b3 a0 a1 a2 a3 |
| 582 | rg4567 = _mm_unpacklo_epi16(_46, _57), |
| 583 | ba4567 = _mm_unpackhi_epi16(_46, _57); |
| 584 | |
| 585 | *r = _mm_unpacklo_epi64(rg0123, rg4567); |
| 586 | *g = _mm_unpackhi_epi64(rg0123, rg4567); |
| 587 | *b = _mm_unpacklo_epi64(ba0123, ba4567); |
| 588 | *a = _mm_unpackhi_epi64(ba0123, ba4567); |
| 589 | } |
| 590 | SI void store4(uint16_t* ptr, size_t tail, U16 r, U16 g, U16 b, U16 a) { |
| 591 | auto rg0123 = _mm_unpacklo_epi16(r, g), // r0 g0 r1 g1 r2 g2 r3 g3 |
| 592 | rg4567 = _mm_unpackhi_epi16(r, g), // r4 g4 r5 g5 r6 g6 r7 g7 |
| 593 | ba0123 = _mm_unpacklo_epi16(b, a), |
| 594 | ba4567 = _mm_unpackhi_epi16(b, a); |
| 595 | |
| 596 | auto _01 = _mm_unpacklo_epi32(rg0123, ba0123), |
| 597 | _23 = _mm_unpackhi_epi32(rg0123, ba0123), |
| 598 | _45 = _mm_unpacklo_epi32(rg4567, ba4567), |
| 599 | _67 = _mm_unpackhi_epi32(rg4567, ba4567); |
| 600 | |
| 601 | if (__builtin_expect(tail,0)) { |
| 602 | auto dst = (double*)ptr; |
| 603 | if (tail > 0) { _mm_storel_pd(dst+0, _01); } |
| 604 | if (tail > 1) { _mm_storeh_pd(dst+1, _01); } |
| 605 | if (tail > 2) { _mm_storel_pd(dst+2, _23); } |
| 606 | if (tail > 3) { _mm_storeh_pd(dst+3, _23); } |
| 607 | if (tail > 4) { _mm_storel_pd(dst+4, _45); } |
| 608 | if (tail > 5) { _mm_storeh_pd(dst+5, _45); } |
| 609 | if (tail > 6) { _mm_storel_pd(dst+6, _67); } |
| 610 | } else { |
| 611 | _mm_storeu_si128((__m128i*)ptr + 0, _01); |
| 612 | _mm_storeu_si128((__m128i*)ptr + 1, _23); |
| 613 | _mm_storeu_si128((__m128i*)ptr + 2, _45); |
| 614 | _mm_storeu_si128((__m128i*)ptr + 3, _67); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | SI void load2(const float* ptr, size_t tail, F* r, F* g) { |
| 619 | F _0123, _4567; |
| 620 | if (__builtin_expect(tail, 0)) { |
| 621 | _0123 = _4567 = _mm256_setzero_ps(); |
| 622 | F* d = &_0123; |
| 623 | if (tail > 3) { |
| 624 | *d = _mm256_loadu_ps(ptr); |
| 625 | ptr += 8; |
| 626 | tail -= 4; |
| 627 | d = &_4567; |
| 628 | } |
| 629 | bool high = false; |
| 630 | if (tail > 1) { |
| 631 | *d = _mm256_castps128_ps256(_mm_loadu_ps(ptr)); |
| 632 | ptr += 4; |
| 633 | tail -= 2; |
| 634 | high = true; |
| 635 | } |
| 636 | if (tail > 0) { |
| 637 | *d = high ? _mm256_insertf128_ps(*d, _mm_loadu_si64(ptr), 1) |
| 638 | : _mm256_insertf128_ps(*d, _mm_loadu_si64(ptr), 0); |
| 639 | } |
| 640 | } else { |
| 641 | _0123 = _mm256_loadu_ps(ptr + 0); |
| 642 | _4567 = _mm256_loadu_ps(ptr + 8); |
| 643 | } |
| 644 | |
| 645 | F _0145 = _mm256_permute2f128_pd(_0123, _4567, 0x20), |
| 646 | _2367 = _mm256_permute2f128_pd(_0123, _4567, 0x31); |
| 647 | |
| 648 | *r = _mm256_shuffle_ps(_0145, _2367, 0x88); |
| 649 | *g = _mm256_shuffle_ps(_0145, _2367, 0xDD); |
| 650 | } |
| 651 | SI void store2(float* ptr, size_t tail, F r, F g) { |
| 652 | F _0145 = _mm256_unpacklo_ps(r, g), |
| 653 | _2367 = _mm256_unpackhi_ps(r, g); |
| 654 | F _0123 = _mm256_permute2f128_pd(_0145, _2367, 0x20), |
| 655 | _4567 = _mm256_permute2f128_pd(_0145, _2367, 0x31); |
| 656 | |
| 657 | if (__builtin_expect(tail, 0)) { |
| 658 | const __m256* s = &_0123; |
| 659 | if (tail > 3) { |
| 660 | _mm256_storeu_ps(ptr, *s); |
| 661 | s = &_4567; |
| 662 | tail -= 4; |
| 663 | ptr += 8; |
| 664 | } |
| 665 | bool high = false; |
| 666 | if (tail > 1) { |
| 667 | _mm_storeu_ps(ptr, _mm256_extractf128_ps(*s, 0)); |
| 668 | ptr += 4; |
| 669 | tail -= 2; |
| 670 | high = true; |
| 671 | } |
| 672 | if (tail > 0) { |
| 673 | *(ptr + 0) = (*s)[ high ? 4 : 0]; |
| 674 | *(ptr + 1) = (*s)[ high ? 5 : 1]; |
| 675 | } |
| 676 | } else { |
| 677 | _mm256_storeu_ps(ptr + 0, _0123); |
| 678 | _mm256_storeu_ps(ptr + 8, _4567); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | SI void load4(const float* ptr, size_t tail, F* r, F* g, F* b, F* a) { |
| 683 | F _04, _15, _26, _37; |
| 684 | _04 = _15 = _26 = _37 = 0; |
| 685 | switch (tail) { |
| 686 | case 0: _37 = _mm256_insertf128_ps(_37, _mm_loadu_ps(ptr+28), 1); [[fallthrough]]; |
| 687 | case 7: _26 = _mm256_insertf128_ps(_26, _mm_loadu_ps(ptr+24), 1); [[fallthrough]]; |
| 688 | case 6: _15 = _mm256_insertf128_ps(_15, _mm_loadu_ps(ptr+20), 1); [[fallthrough]]; |
| 689 | case 5: _04 = _mm256_insertf128_ps(_04, _mm_loadu_ps(ptr+16), 1); [[fallthrough]]; |
| 690 | case 4: _37 = _mm256_insertf128_ps(_37, _mm_loadu_ps(ptr+12), 0); [[fallthrough]]; |
| 691 | case 3: _26 = _mm256_insertf128_ps(_26, _mm_loadu_ps(ptr+ 8), 0); [[fallthrough]]; |
| 692 | case 2: _15 = _mm256_insertf128_ps(_15, _mm_loadu_ps(ptr+ 4), 0); [[fallthrough]]; |
| 693 | case 1: _04 = _mm256_insertf128_ps(_04, _mm_loadu_ps(ptr+ 0), 0); |
| 694 | } |
| 695 | |
| 696 | F rg0145 = _mm256_unpacklo_ps(_04,_15), // r0 r1 g0 g1 | r4 r5 g4 g5 |
| 697 | ba0145 = _mm256_unpackhi_ps(_04,_15), |
| 698 | rg2367 = _mm256_unpacklo_ps(_26,_37), |
| 699 | ba2367 = _mm256_unpackhi_ps(_26,_37); |
| 700 | |
| 701 | *r = _mm256_unpacklo_pd(rg0145, rg2367); |
| 702 | *g = _mm256_unpackhi_pd(rg0145, rg2367); |
| 703 | *b = _mm256_unpacklo_pd(ba0145, ba2367); |
| 704 | *a = _mm256_unpackhi_pd(ba0145, ba2367); |
| 705 | } |
| 706 | SI void store4(float* ptr, size_t tail, F r, F g, F b, F a) { |
| 707 | F rg0145 = _mm256_unpacklo_ps(r, g), // r0 g0 r1 g1 | r4 g4 r5 g5 |
| 708 | rg2367 = _mm256_unpackhi_ps(r, g), // r2 ... | r6 ... |
| 709 | ba0145 = _mm256_unpacklo_ps(b, a), // b0 a0 b1 a1 | b4 a4 b5 a5 |
| 710 | ba2367 = _mm256_unpackhi_ps(b, a); // b2 ... | b6 ... |
| 711 | |
| 712 | F _04 = _mm256_unpacklo_pd(rg0145, ba0145), // r0 g0 b0 a0 | r4 g4 b4 a4 |
| 713 | _15 = _mm256_unpackhi_pd(rg0145, ba0145), // r1 ... | r5 ... |
| 714 | _26 = _mm256_unpacklo_pd(rg2367, ba2367), // r2 ... | r6 ... |
| 715 | _37 = _mm256_unpackhi_pd(rg2367, ba2367); // r3 ... | r7 ... |
| 716 | |
| 717 | if (__builtin_expect(tail, 0)) { |
| 718 | if (tail > 0) { _mm_storeu_ps(ptr+ 0, _mm256_extractf128_ps(_04, 0)); } |
| 719 | if (tail > 1) { _mm_storeu_ps(ptr+ 4, _mm256_extractf128_ps(_15, 0)); } |
| 720 | if (tail > 2) { _mm_storeu_ps(ptr+ 8, _mm256_extractf128_ps(_26, 0)); } |
| 721 | if (tail > 3) { _mm_storeu_ps(ptr+12, _mm256_extractf128_ps(_37, 0)); } |
| 722 | if (tail > 4) { _mm_storeu_ps(ptr+16, _mm256_extractf128_ps(_04, 1)); } |
| 723 | if (tail > 5) { _mm_storeu_ps(ptr+20, _mm256_extractf128_ps(_15, 1)); } |
| 724 | if (tail > 6) { _mm_storeu_ps(ptr+24, _mm256_extractf128_ps(_26, 1)); } |
| 725 | } else { |
| 726 | F _01 = _mm256_permute2f128_ps(_04, _15, 32), // 32 == 0010 0000 == lo, lo |
| 727 | _23 = _mm256_permute2f128_ps(_26, _37, 32), |
| 728 | _45 = _mm256_permute2f128_ps(_04, _15, 49), // 49 == 0011 0001 == hi, hi |
| 729 | _67 = _mm256_permute2f128_ps(_26, _37, 49); |
| 730 | _mm256_storeu_ps(ptr+ 0, _01); |
| 731 | _mm256_storeu_ps(ptr+ 8, _23); |
| 732 | _mm256_storeu_ps(ptr+16, _45); |
| 733 | _mm256_storeu_ps(ptr+24, _67); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | #elif defined(JUMPER_IS_SSE2) || defined(JUMPER_IS_SSE41) || defined(JUMPER_IS_AVX) |
| 738 | template <typename T> using V = T __attribute__((ext_vector_type(4))); |
| 739 | using F = V<float >; |
| 740 | using I32 = V< int32_t>; |
| 741 | using U64 = V<uint64_t>; |
| 742 | using U32 = V<uint32_t>; |
| 743 | using U16 = V<uint16_t>; |
| 744 | using U8 = V<uint8_t >; |
| 745 | |
| 746 | SI F if_then_else(I32 c, F t, F e) { |
| 747 | return _mm_or_ps(a: _mm_and_ps(a: c, b: t), b: _mm_andnot_ps(a: c, b: e)); |
| 748 | } |
| 749 | |
| 750 | SI F min(F a, F b) { return _mm_min_ps(a: a,b: b); } |
| 751 | SI F max(F a, F b) { return _mm_max_ps(a: a,b: b); } |
| 752 | #if defined(JUMPER_IS_SSE41) || defined(JUMPER_IS_AVX) |
| 753 | SI I32 min(I32 a, I32 b) { return _mm_min_epi32(a,b); } |
| 754 | SI U32 min(U32 a, U32 b) { return _mm_min_epu32(a,b); } |
| 755 | SI I32 max(I32 a, I32 b) { return _mm_max_epi32(a,b); } |
| 756 | SI U32 max(U32 a, U32 b) { return _mm_max_epu32(a,b); } |
| 757 | #else |
| 758 | SI I32 min(I32 a, I32 b) { |
| 759 | return sk_bit_cast<I32>(src: if_then_else(c: a < b, t: sk_bit_cast<F>(src: a), e: sk_bit_cast<F>(src: b))); |
| 760 | } |
| 761 | SI U32 min(U32 a, U32 b) { |
| 762 | return sk_bit_cast<U32>(src: if_then_else(c: a < b, t: sk_bit_cast<F>(src: a), e: sk_bit_cast<F>(src: b))); |
| 763 | } |
| 764 | SI I32 max(I32 a, I32 b) { |
| 765 | return sk_bit_cast<I32>(src: if_then_else(c: a > b, t: sk_bit_cast<F>(src: a), e: sk_bit_cast<F>(src: b))); |
| 766 | } |
| 767 | SI U32 max(U32 a, U32 b) { |
| 768 | return sk_bit_cast<U32>(src: if_then_else(c: a > b, t: sk_bit_cast<F>(src: a), e: sk_bit_cast<F>(src: b))); |
| 769 | } |
| 770 | #endif |
| 771 | |
| 772 | SI F mad(F f, F m, F a) { return f*m+a; } |
| 773 | SI F abs_(F v) { return _mm_and_ps(a: v, b: 0-v); } |
| 774 | #if defined(JUMPER_IS_SSE41) || defined(JUMPER_IS_AVX) |
| 775 | SI I32 abs_(I32 v) { return _mm_abs_epi32(v); } |
| 776 | #else |
| 777 | SI I32 abs_(I32 v) { return max(a: v, b: -v); } |
| 778 | #endif |
| 779 | SI F rcp_fast(F v) { return _mm_rcp_ps (a: v); } |
| 780 | SI F rcp_precise (F v) { F e = rcp_fast(v); return e * (2.0f - v * e); } |
| 781 | SI F rsqrt (F v) { return _mm_rsqrt_ps(a: v); } |
| 782 | SI F sqrt_(F v) { return _mm_sqrt_ps (a: v); } |
| 783 | |
| 784 | SI U32 round(F v) { return _mm_cvtps_epi32(a: v); } |
| 785 | SI U32 round(F v, F scale) { return _mm_cvtps_epi32(a: v*scale); } |
| 786 | |
| 787 | SI U16 pack(U32 v) { |
| 788 | #if defined(JUMPER_IS_SSE41) || defined(JUMPER_IS_AVX) |
| 789 | auto p = _mm_packus_epi32(v,v); |
| 790 | #else |
| 791 | // Sign extend so that _mm_packs_epi32() does the pack we want. |
| 792 | auto p = _mm_srai_epi32(a: _mm_slli_epi32(a: v, count: 16), count: 16); |
| 793 | p = _mm_packs_epi32(a: p,b: p); |
| 794 | #endif |
| 795 | return sk_unaligned_load<U16>(ptr: &p); // We have two copies. Return (the lower) one. |
| 796 | } |
| 797 | SI U8 pack(U16 v) { |
| 798 | auto r = widen_cast<__m128i>(src: v); |
| 799 | r = _mm_packus_epi16(a: r,b: r); |
| 800 | return sk_unaligned_load<U8>(ptr: &r); |
| 801 | } |
| 802 | |
| 803 | // NOTE: This only checks the top bit of each lane, and is incorrect with non-mask values. |
| 804 | SI bool any(I32 c) { return _mm_movemask_ps(a: c) != 0b0000; } |
| 805 | SI bool all(I32 c) { return _mm_movemask_ps(a: c) == 0b1111; } |
| 806 | |
| 807 | SI F floor_(F v) { |
| 808 | #if defined(JUMPER_IS_SSE41) || defined(JUMPER_IS_AVX) |
| 809 | return _mm_floor_ps(v); |
| 810 | #else |
| 811 | F roundtrip = _mm_cvtepi32_ps(a: _mm_cvttps_epi32(a: v)); |
| 812 | return roundtrip - if_then_else(c: roundtrip > v, t: 1, e: 0); |
| 813 | #endif |
| 814 | } |
| 815 | |
| 816 | SI F ceil_(F v) { |
| 817 | #if defined(JUMPER_IS_SSE41) || defined(JUMPER_IS_AVX) |
| 818 | return _mm_ceil_ps(v); |
| 819 | #else |
| 820 | F roundtrip = _mm_cvtepi32_ps(a: _mm_cvttps_epi32(a: v)); |
| 821 | return roundtrip + if_then_else(c: roundtrip < v, t: 1, e: 0); |
| 822 | #endif |
| 823 | } |
| 824 | |
| 825 | template <typename T> |
| 826 | SI V<T> gather(const T* p, U32 ix) { |
| 827 | return {p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]]}; |
| 828 | } |
| 829 | template <typename V, typename S> |
| 830 | SI void scatter_masked(V src, S* dst, U32 ix, I32 mask) { |
| 831 | V before = gather(dst, ix); |
| 832 | V after = if_then_else(mask, src, before); |
| 833 | dst[ix[0]] = after[0]; |
| 834 | dst[ix[1]] = after[1]; |
| 835 | dst[ix[2]] = after[2]; |
| 836 | dst[ix[3]] = after[3]; |
| 837 | } |
| 838 | SI void load2(const uint16_t* ptr, size_t tail, U16* r, U16* g) { |
| 839 | __m128i _01; |
| 840 | if (__builtin_expect(tail,0)) { |
| 841 | _01 = _mm_setzero_si128(); |
| 842 | if (tail > 1) { |
| 843 | _01 = _mm_loadl_pd(a: _01, dp: (const double*)ptr); // r0 g0 r1 g1 00 00 00 00 |
| 844 | if (tail > 2) { |
| 845 | _01 = _mm_insert_epi16(_01, *(ptr+4), 4); // r0 g0 r1 g1 r2 00 00 00 |
| 846 | _01 = _mm_insert_epi16(_01, *(ptr+5), 5); // r0 g0 r1 g1 r2 g2 00 00 |
| 847 | } |
| 848 | } else { |
| 849 | _01 = _mm_cvtsi32_si128(a: *(const uint32_t*)ptr); // r0 g0 00 00 00 00 00 00 |
| 850 | } |
| 851 | } else { |
| 852 | _01 = _mm_loadu_si128(p: ((__m128i*)ptr) + 0); // r0 g0 r1 g1 r2 g2 r3 g3 |
| 853 | } |
| 854 | auto rg01_23 = _mm_shufflelo_epi16(_01, 0xD8); // r0 r1 g0 g1 r2 g2 r3 g3 |
| 855 | auto rg = _mm_shufflehi_epi16(rg01_23, 0xD8); // r0 r1 g0 g1 r2 r3 g2 g3 |
| 856 | |
| 857 | auto R = _mm_shuffle_epi32(rg, 0x88); // r0 r1 r2 r3 r0 r1 r2 r3 |
| 858 | auto G = _mm_shuffle_epi32(rg, 0xDD); // g0 g1 g2 g3 g0 g1 g2 g3 |
| 859 | *r = sk_unaligned_load<U16>(ptr: &R); |
| 860 | *g = sk_unaligned_load<U16>(ptr: &G); |
| 861 | } |
| 862 | SI void store2(uint16_t* ptr, size_t tail, U16 r, U16 g) { |
| 863 | U32 rg = _mm_unpacklo_epi16(a: widen_cast<__m128i>(src: r), b: widen_cast<__m128i>(src: g)); |
| 864 | if (__builtin_expect(tail, 0)) { |
| 865 | if (tail > 1) { |
| 866 | _mm_storel_epi64(p: (__m128i*)ptr, a: rg); |
| 867 | if (tail > 2) { |
| 868 | int32_t rgpair = rg[2]; |
| 869 | memcpy(dest: ptr + 4, src: &rgpair, n: sizeof(rgpair)); |
| 870 | } |
| 871 | } else { |
| 872 | int32_t rgpair = rg[0]; |
| 873 | memcpy(dest: ptr, src: &rgpair, n: sizeof(rgpair)); |
| 874 | } |
| 875 | } else { |
| 876 | _mm_storeu_si128(p: (__m128i*)ptr + 0, b: rg); |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | SI void load3(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b) { |
| 881 | __m128i _0, _1, _2, _3; |
| 882 | if (__builtin_expect(tail,0)) { |
| 883 | _1 = _2 = _3 = _mm_setzero_si128(); |
| 884 | auto load_rgb = [](const uint16_t* src) { |
| 885 | auto v = _mm_cvtsi32_si128(a: *(const uint32_t*)src); |
| 886 | return _mm_insert_epi16(v, src[2], 2); |
| 887 | }; |
| 888 | if ( true ) { _0 = load_rgb(ptr + 0); } |
| 889 | if (tail > 1) { _1 = load_rgb(ptr + 3); } |
| 890 | if (tail > 2) { _2 = load_rgb(ptr + 6); } |
| 891 | } else { |
| 892 | // Load slightly weirdly to make sure we don't load past the end of 4x48 bits. |
| 893 | auto _01 = _mm_loadu_si128(p: (const __m128i*)(ptr + 0)) , |
| 894 | _23 = _mm_srli_si128(_mm_loadu_si128((const __m128i*)(ptr + 4)), 4); |
| 895 | |
| 896 | // Each _N holds R,G,B for pixel N in its lower 3 lanes (upper 5 are ignored). |
| 897 | _0 = _01; |
| 898 | _1 = _mm_srli_si128(_01, 6); |
| 899 | _2 = _23; |
| 900 | _3 = _mm_srli_si128(_23, 6); |
| 901 | } |
| 902 | |
| 903 | // De-interlace to R,G,B. |
| 904 | auto _02 = _mm_unpacklo_epi16(a: _0, b: _2), // r0 r2 g0 g2 b0 b2 xx xx |
| 905 | _13 = _mm_unpacklo_epi16(a: _1, b: _3); // r1 r3 g1 g3 b1 b3 xx xx |
| 906 | |
| 907 | auto R = _mm_unpacklo_epi16(a: _02, b: _13), // r0 r1 r2 r3 g0 g1 g2 g3 |
| 908 | G = _mm_srli_si128(R, 8), |
| 909 | B = _mm_unpackhi_epi16(a: _02, b: _13); // b0 b1 b2 b3 xx xx xx xx |
| 910 | |
| 911 | *r = sk_unaligned_load<U16>(ptr: &R); |
| 912 | *g = sk_unaligned_load<U16>(ptr: &G); |
| 913 | *b = sk_unaligned_load<U16>(ptr: &B); |
| 914 | } |
| 915 | |
| 916 | SI void load4(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b, U16* a) { |
| 917 | __m128i _01, _23; |
| 918 | if (__builtin_expect(tail,0)) { |
| 919 | _01 = _23 = _mm_setzero_si128(); |
| 920 | auto src = (const double*)ptr; |
| 921 | if ( true ) { _01 = _mm_loadl_pd(a: _01, dp: src + 0); } // r0 g0 b0 a0 00 00 00 00 |
| 922 | if (tail > 1) { _01 = _mm_loadh_pd(a: _01, dp: src + 1); } // r0 g0 b0 a0 r1 g1 b1 a1 |
| 923 | if (tail > 2) { _23 = _mm_loadl_pd(a: _23, dp: src + 2); } // r2 g2 b2 a2 00 00 00 00 |
| 924 | } else { |
| 925 | _01 = _mm_loadu_si128(p: ((__m128i*)ptr) + 0); // r0 g0 b0 a0 r1 g1 b1 a1 |
| 926 | _23 = _mm_loadu_si128(p: ((__m128i*)ptr) + 1); // r2 g2 b2 a2 r3 g3 b3 a3 |
| 927 | } |
| 928 | |
| 929 | auto _02 = _mm_unpacklo_epi16(a: _01, b: _23), // r0 r2 g0 g2 b0 b2 a0 a2 |
| 930 | _13 = _mm_unpackhi_epi16(a: _01, b: _23); // r1 r3 g1 g3 b1 b3 a1 a3 |
| 931 | |
| 932 | auto rg = _mm_unpacklo_epi16(a: _02, b: _13), // r0 r1 r2 r3 g0 g1 g2 g3 |
| 933 | ba = _mm_unpackhi_epi16(a: _02, b: _13); // b0 b1 b2 b3 a0 a1 a2 a3 |
| 934 | |
| 935 | *r = sk_unaligned_load<U16>(ptr: (uint16_t*)&rg + 0); |
| 936 | *g = sk_unaligned_load<U16>(ptr: (uint16_t*)&rg + 4); |
| 937 | *b = sk_unaligned_load<U16>(ptr: (uint16_t*)&ba + 0); |
| 938 | *a = sk_unaligned_load<U16>(ptr: (uint16_t*)&ba + 4); |
| 939 | } |
| 940 | |
| 941 | SI void store4(uint16_t* ptr, size_t tail, U16 r, U16 g, U16 b, U16 a) { |
| 942 | auto rg = _mm_unpacklo_epi16(a: widen_cast<__m128i>(src: r), b: widen_cast<__m128i>(src: g)), |
| 943 | ba = _mm_unpacklo_epi16(a: widen_cast<__m128i>(src: b), b: widen_cast<__m128i>(src: a)); |
| 944 | |
| 945 | if (__builtin_expect(tail, 0)) { |
| 946 | auto dst = (double*)ptr; |
| 947 | if ( true ) { _mm_storel_pd(dp: dst + 0, a: _mm_unpacklo_epi32(a: rg, b: ba)); } |
| 948 | if (tail > 1) { _mm_storeh_pd(dp: dst + 1, a: _mm_unpacklo_epi32(a: rg, b: ba)); } |
| 949 | if (tail > 2) { _mm_storel_pd(dp: dst + 2, a: _mm_unpackhi_epi32(a: rg, b: ba)); } |
| 950 | } else { |
| 951 | _mm_storeu_si128(p: (__m128i*)ptr + 0, b: _mm_unpacklo_epi32(a: rg, b: ba)); |
| 952 | _mm_storeu_si128(p: (__m128i*)ptr + 1, b: _mm_unpackhi_epi32(a: rg, b: ba)); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | SI void load2(const float* ptr, size_t tail, F* r, F* g) { |
| 957 | F _01, _23; |
| 958 | if (__builtin_expect(tail, 0)) { |
| 959 | _01 = _23 = _mm_setzero_si128(); |
| 960 | if ( true ) { _01 = _mm_loadl_pi(a: _01, p: (__m64 const*)(ptr + 0)); } |
| 961 | if (tail > 1) { _01 = _mm_loadh_pi(a: _01, p: (__m64 const*)(ptr + 2)); } |
| 962 | if (tail > 2) { _23 = _mm_loadl_pi(a: _23, p: (__m64 const*)(ptr + 4)); } |
| 963 | } else { |
| 964 | _01 = _mm_loadu_ps(p: ptr + 0); |
| 965 | _23 = _mm_loadu_ps(p: ptr + 4); |
| 966 | } |
| 967 | *r = _mm_shuffle_ps(_01, _23, 0x88); |
| 968 | *g = _mm_shuffle_ps(_01, _23, 0xDD); |
| 969 | } |
| 970 | SI void store2(float* ptr, size_t tail, F r, F g) { |
| 971 | F _01 = _mm_unpacklo_ps(a: r, b: g), |
| 972 | _23 = _mm_unpackhi_ps(a: r, b: g); |
| 973 | if (__builtin_expect(tail, 0)) { |
| 974 | if ( true ) { _mm_storel_pi(p: (__m64*)(ptr + 0), a: _01); } |
| 975 | if (tail > 1) { _mm_storeh_pi(p: (__m64*)(ptr + 2), a: _01); } |
| 976 | if (tail > 2) { _mm_storel_pi(p: (__m64*)(ptr + 4), a: _23); } |
| 977 | } else { |
| 978 | _mm_storeu_ps(p: ptr + 0, a: _01); |
| 979 | _mm_storeu_ps(p: ptr + 4, a: _23); |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | SI void load4(const float* ptr, size_t tail, F* r, F* g, F* b, F* a) { |
| 984 | F _0, _1, _2, _3; |
| 985 | if (__builtin_expect(tail, 0)) { |
| 986 | _1 = _2 = _3 = _mm_setzero_si128(); |
| 987 | if ( true ) { _0 = _mm_loadu_ps(p: ptr + 0); } |
| 988 | if (tail > 1) { _1 = _mm_loadu_ps(p: ptr + 4); } |
| 989 | if (tail > 2) { _2 = _mm_loadu_ps(p: ptr + 8); } |
| 990 | } else { |
| 991 | _0 = _mm_loadu_ps(p: ptr + 0); |
| 992 | _1 = _mm_loadu_ps(p: ptr + 4); |
| 993 | _2 = _mm_loadu_ps(p: ptr + 8); |
| 994 | _3 = _mm_loadu_ps(p: ptr +12); |
| 995 | } |
| 996 | _MM_TRANSPOSE4_PS(_0,_1,_2,_3); |
| 997 | *r = _0; |
| 998 | *g = _1; |
| 999 | *b = _2; |
| 1000 | *a = _3; |
| 1001 | } |
| 1002 | |
| 1003 | SI void store4(float* ptr, size_t tail, F r, F g, F b, F a) { |
| 1004 | _MM_TRANSPOSE4_PS(r,g,b,a); |
| 1005 | if (__builtin_expect(tail, 0)) { |
| 1006 | if ( true ) { _mm_storeu_ps(p: ptr + 0, a: r); } |
| 1007 | if (tail > 1) { _mm_storeu_ps(p: ptr + 4, a: g); } |
| 1008 | if (tail > 2) { _mm_storeu_ps(p: ptr + 8, a: b); } |
| 1009 | } else { |
| 1010 | _mm_storeu_ps(p: ptr + 0, a: r); |
| 1011 | _mm_storeu_ps(p: ptr + 4, a: g); |
| 1012 | _mm_storeu_ps(p: ptr + 8, a: b); |
| 1013 | _mm_storeu_ps(p: ptr +12, a: a); |
| 1014 | } |
| 1015 | } |
| 1016 | #endif |
| 1017 | |
| 1018 | // We need to be a careful with casts. |
| 1019 | // (F)x means cast x to float in the portable path, but bit_cast x to float in the others. |
| 1020 | // These named casts and bit_cast() are always what they seem to be. |
| 1021 | #if defined(JUMPER_IS_SCALAR) |
| 1022 | SI F cast (U32 v) { return (F)v; } |
| 1023 | SI F cast64(U64 v) { return (F)v; } |
| 1024 | SI U32 trunc_(F v) { return (U32)v; } |
| 1025 | SI U32 expand(U16 v) { return (U32)v; } |
| 1026 | SI U32 expand(U8 v) { return (U32)v; } |
| 1027 | #else |
| 1028 | SI F cast (U32 v) { return __builtin_convertvector((I32)v, F); } |
| 1029 | SI F cast64(U64 v) { return __builtin_convertvector( v, F); } |
| 1030 | SI U32 trunc_(F v) { return (U32)__builtin_convertvector( v, I32); } |
| 1031 | SI U32 expand(U16 v) { return __builtin_convertvector( v, U32); } |
| 1032 | SI U32 expand(U8 v) { return __builtin_convertvector( v, U32); } |
| 1033 | #endif |
| 1034 | |
| 1035 | template <typename V> |
| 1036 | SI V if_then_else(I32 c, V t, V e) { |
| 1037 | return sk_bit_cast<V>(if_then_else(c, sk_bit_cast<F>(t), sk_bit_cast<F>(e))); |
| 1038 | } |
| 1039 | |
| 1040 | SI U16 bswap(U16 x) { |
| 1041 | #if defined(JUMPER_IS_SSE2) || defined(JUMPER_IS_SSE41) |
| 1042 | // Somewhat inexplicably Clang decides to do (x<<8) | (x>>8) in 32-bit lanes |
| 1043 | // when generating code for SSE2 and SSE4.1. We'll do it manually... |
| 1044 | auto v = widen_cast<__m128i>(src: x); |
| 1045 | v = _mm_slli_epi16(a: v,count: 8) | _mm_srli_epi16(a: v,count: 8); |
| 1046 | return sk_unaligned_load<U16>(ptr: &v); |
| 1047 | #else |
| 1048 | return (x<<8) | (x>>8); |
| 1049 | #endif |
| 1050 | } |
| 1051 | |
| 1052 | SI F fract(F v) { return v - floor_(v); } |
| 1053 | |
| 1054 | // See http://www.machinedlearnings.com/2011/06/fast-approximate-logarithm-exponential.html |
| 1055 | SI F approx_log2(F x) { |
| 1056 | // e - 127 is a fair approximation of log2(x) in its own right... |
| 1057 | F e = cast(v: sk_bit_cast<U32>(src: x)) * (1.0f / (1<<23)); |
| 1058 | |
| 1059 | // ... but using the mantissa to refine its error is _much_ better. |
| 1060 | F m = sk_bit_cast<F>(src: (sk_bit_cast<U32>(src: x) & 0x007fffff) | 0x3f000000); |
| 1061 | return e |
| 1062 | - 124.225514990f |
| 1063 | - 1.498030302f * m |
| 1064 | - 1.725879990f / (0.3520887068f + m); |
| 1065 | } |
| 1066 | |
| 1067 | SI F approx_log(F x) { |
| 1068 | const float ln2 = 0.69314718f; |
| 1069 | return ln2 * approx_log2(x); |
| 1070 | } |
| 1071 | |
| 1072 | SI F approx_pow2(F x) { |
| 1073 | constexpr float kInfinityBits = 0x7f800000; |
| 1074 | |
| 1075 | F f = fract(v: x); |
| 1076 | F approx = x + 121.274057500f; |
| 1077 | approx -= f * 1.490129070f; |
| 1078 | approx += 27.728023300f / (4.84252568f - f); |
| 1079 | approx *= 1.0f * (1<<23); |
| 1080 | approx = min(a: max(a: approx, b: F(0)), b: kInfinityBits); // guard against underflow/overflow |
| 1081 | |
| 1082 | return sk_bit_cast<F>(src: round(v: approx)); |
| 1083 | } |
| 1084 | |
| 1085 | SI F approx_exp(F x) { |
| 1086 | const float log2_e = 1.4426950408889634074f; |
| 1087 | return approx_pow2(x: log2_e * x); |
| 1088 | } |
| 1089 | |
| 1090 | SI F approx_powf(F x, F y) { |
| 1091 | return if_then_else(c: (x == 0)|(x == 1), t: x |
| 1092 | , e: approx_pow2(x: approx_log2(x) * y)); |
| 1093 | } |
| 1094 | |
| 1095 | SI F from_half(U16 h) { |
| 1096 | #if defined(JUMPER_IS_NEON) && defined(SK_CPU_ARM64) \ |
| 1097 | && !defined(SK_BUILD_FOR_GOOGLE3) // Temporary workaround for some Google3 builds. |
| 1098 | return vcvt_f32_f16(h); |
| 1099 | |
| 1100 | #elif defined(JUMPER_IS_HSW) |
| 1101 | return _mm256_cvtph_ps(h); |
| 1102 | |
| 1103 | #else |
| 1104 | // Remember, a half is 1-5-10 (sign-exponent-mantissa) with 15 exponent bias. |
| 1105 | U32 sem = expand(v: h), |
| 1106 | s = sem & 0x8000, |
| 1107 | em = sem ^ s; |
| 1108 | |
| 1109 | // Convert to 1-8-23 float with 127 bias, flushing denorm halfs (including zero) to zero. |
| 1110 | auto denorm = (I32)em < 0x0400; // I32 comparison is often quicker, and always safe here. |
| 1111 | return if_then_else(c: denorm, t: F(0) |
| 1112 | , e: sk_bit_cast<F>( src: (s<<16) + (em<<13) + ((127-15)<<23) )); |
| 1113 | #endif |
| 1114 | } |
| 1115 | |
| 1116 | SI U16 to_half(F f) { |
| 1117 | #if defined(JUMPER_IS_NEON) && defined(SK_CPU_ARM64) \ |
| 1118 | && !defined(SK_BUILD_FOR_GOOGLE3) // Temporary workaround for some Google3 builds. |
| 1119 | return vcvt_f16_f32(f); |
| 1120 | |
| 1121 | #elif defined(JUMPER_IS_HSW) |
| 1122 | return _mm256_cvtps_ph(f, _MM_FROUND_CUR_DIRECTION); |
| 1123 | |
| 1124 | #else |
| 1125 | // Remember, a float is 1-8-23 (sign-exponent-mantissa) with 127 exponent bias. |
| 1126 | U32 sem = sk_bit_cast<U32>(src: f), |
| 1127 | s = sem & 0x80000000, |
| 1128 | em = sem ^ s; |
| 1129 | |
| 1130 | // Convert to 1-5-10 half with 15 bias, flushing denorm halfs (including zero) to zero. |
| 1131 | auto denorm = (I32)em < 0x38800000; // I32 comparison is often quicker, and always safe here. |
| 1132 | return pack(v: if_then_else(c: denorm, t: U32(0) |
| 1133 | , e: (s>>16) + (em>>13) - ((127-15)<<10))); |
| 1134 | #endif |
| 1135 | } |
| 1136 | |
| 1137 | // Our fundamental vector depth is our pixel stride. |
| 1138 | static constexpr size_t N = sizeof(F) / sizeof(float); |
| 1139 | |
| 1140 | // We're finally going to get to what a Stage function looks like! |
| 1141 | // tail == 0 ~~> work on a full N pixels |
| 1142 | // tail != 0 ~~> work on only the first tail pixels |
| 1143 | // tail is always < N. |
| 1144 | |
| 1145 | // Any custom ABI to use for all (non-externally-facing) stage functions? |
| 1146 | // Also decide here whether to use narrow (compromise) or wide (ideal) stages. |
| 1147 | #if defined(SK_CPU_ARM32) && defined(JUMPER_IS_NEON) |
| 1148 | // This lets us pass vectors more efficiently on 32-bit ARM. |
| 1149 | // We can still only pass 16 floats, so best as 4x {r,g,b,a}. |
| 1150 | #define ABI __attribute__((pcs("aapcs-vfp"))) |
| 1151 | #define JUMPER_NARROW_STAGES 1 |
| 1152 | #elif defined(_MSC_VER) |
| 1153 | // Even if not vectorized, this lets us pass {r,g,b,a} as registers, |
| 1154 | // instead of {b,a} on the stack. Narrow stages work best for __vectorcall. |
| 1155 | #define ABI __vectorcall |
| 1156 | #define JUMPER_NARROW_STAGES 1 |
| 1157 | #elif defined(__x86_64__) || defined(SK_CPU_ARM64) |
| 1158 | // These platforms are ideal for wider stages, and their default ABI is ideal. |
| 1159 | #define ABI |
| 1160 | #define JUMPER_NARROW_STAGES 0 |
| 1161 | #else |
| 1162 | // 32-bit or unknown... shunt them down the narrow path. |
| 1163 | // Odds are these have few registers and are better off there. |
| 1164 | #define ABI |
| 1165 | #define JUMPER_NARROW_STAGES 1 |
| 1166 | #endif |
| 1167 | |
| 1168 | #if JUMPER_NARROW_STAGES |
| 1169 | struct Params { |
| 1170 | size_t dx, dy, tail; |
| 1171 | std::byte* base; |
| 1172 | F dr,dg,db,da; |
| 1173 | }; |
| 1174 | using Stage = void(ABI*)(Params*, SkRasterPipelineStage* program, F r, F g, F b, F a); |
| 1175 | #else |
| 1176 | using Stage = void(ABI*)(size_t tail, SkRasterPipelineStage* program, size_t dx, size_t dy, |
| 1177 | std::byte* base, F,F,F,F, F,F,F,F); |
| 1178 | #endif |
| 1179 | |
| 1180 | static void start_pipeline(size_t dx, size_t dy, |
| 1181 | size_t xlimit, size_t ylimit, |
| 1182 | SkRasterPipelineStage* program) { |
| 1183 | auto start = (Stage)program->fn; |
| 1184 | const size_t x0 = dx; |
| 1185 | std::byte* const base = nullptr; |
| 1186 | for (; dy < ylimit; dy++) { |
| 1187 | #if JUMPER_NARROW_STAGES |
| 1188 | Params params = { x0,dy,0,base, 0,0,0,0 }; |
| 1189 | while (params.dx + N <= xlimit) { |
| 1190 | start(¶ms,program, 0,0,0,0); |
| 1191 | params.dx += N; |
| 1192 | } |
| 1193 | if (size_t tail = xlimit - params.dx) { |
| 1194 | params.tail = tail; |
| 1195 | start(¶ms,program, 0,0,0,0); |
| 1196 | } |
| 1197 | #else |
| 1198 | dx = x0; |
| 1199 | while (dx + N <= xlimit) { |
| 1200 | start(0,program,dx,dy,base, 0,0,0,0, 0,0,0,0); |
| 1201 | dx += N; |
| 1202 | } |
| 1203 | if (size_t tail = xlimit - dx) { |
| 1204 | start(tail,program,dx,dy,base, 0,0,0,0, 0,0,0,0); |
| 1205 | } |
| 1206 | #endif |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | #if SK_HAS_MUSTTAIL |
| 1211 | #define JUMPER_MUSTTAIL [[clang::musttail]] |
| 1212 | #else |
| 1213 | #define JUMPER_MUSTTAIL |
| 1214 | #endif |
| 1215 | |
| 1216 | #if JUMPER_NARROW_STAGES |
| 1217 | #define DECLARE_STAGE(name, ARG, STAGE_RET, INC, OFFSET, MUSTTAIL) \ |
| 1218 | SI STAGE_RET name##_k(ARG, size_t dx, size_t dy, size_t tail, std::byte*& base, \ |
| 1219 | F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da); \ |
| 1220 | static void ABI name(Params* params, SkRasterPipelineStage* program, \ |
| 1221 | F r, F g, F b, F a) { \ |
| 1222 | OFFSET name##_k(Ctx{program}, params->dx,params->dy,params->tail,params->base, \ |
| 1223 | r,g,b,a, params->dr, params->dg, params->db, params->da); \ |
| 1224 | INC; \ |
| 1225 | auto fn = (Stage)program->fn; \ |
| 1226 | MUSTTAIL return fn(params, program, r,g,b,a); \ |
| 1227 | } \ |
| 1228 | SI STAGE_RET name##_k(ARG, size_t dx, size_t dy, size_t tail, std::byte*& base, \ |
| 1229 | F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da) |
| 1230 | #else |
| 1231 | #define DECLARE_STAGE(name, ARG, STAGE_RET, INC, OFFSET, MUSTTAIL) \ |
| 1232 | SI STAGE_RET name##_k(ARG, size_t |
|---|