Debian Codegraph @

modules/skcms/src/Transform_inl.h

raw
1 /*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 // Intentionally NO #pragma once... included multiple times.
9
10 // This file is included from skcms.cc in a namespace with some pre-defines:
11 // - N: SIMD width of all vectors; 1, 4, 8 or 16 (preprocessor define)
12 // - V<T>: a template to create a vector of N T's.
13
14 using F = V<float>;
15 using I32 = V<int32_t>;
16 using U64 = V<uint64_t>;
17 using U32 = V<uint32_t>;
18 using U16 = V<uint16_t>;
19 using U8 = V<uint8_t>;
20
21 #if defined(__GNUC__) && !defined(__clang__)
22 // GCC is kind of weird, not allowing vector = scalar directly.
23 static constexpr F F0 = F() + 0.0f,
24 F1 = F() + 1.0f,
25 FInfBits = F() + 0x7f800000; // equals 2139095040, the bit pattern of +Inf
26 #else
27 static constexpr F F0 = 0.0f,
28 F1 = 1.0f,
29 FInfBits = 0x7f800000; // equals 2139095040, the bit pattern of +Inf
30 #endif
31
32 // Instead of checking __AVX__ below, we'll check USING_AVX.
33 // This lets skcms.cc set USING_AVX to force us in even if the compiler's not set that way.
34 // Same deal for __F16C__ and __AVX2__ ~~~> USING_AVX_F16C, USING_AVX2.
35
36 #if !defined(USING_AVX) && N == 8 && defined(__AVX__)
37 #define USING_AVX
38 #endif
39 #if !defined(USING_AVX_F16C) && defined(USING_AVX) && defined(__F16C__)
40 #define USING_AVX_F16C
41 #endif
42 #if !defined(USING_AVX2) && defined(USING_AVX) && defined(__AVX2__)
43 #define USING_AVX2
44 #endif
45 #if !defined(USING_AVX512F) && N == 16 && defined(__AVX512F__) && defined(__AVX512DQ__)
46 #define USING_AVX512F
47 #endif
48
49 // Similar to the AVX+ features, we define USING_NEON and USING_NEON_F16C.
50 // This is more for organizational clarity... skcms.cc doesn't force these.
51 #if N > 1 && defined(__ARM_NEON)
52 #define USING_NEON
53
54 // We have to use two different mechanisms to enable the f16 conversion intrinsics:
55 #if defined(__clang__)
56 // Clang's arm_neon.h guards them with the FP hardware bit:
57 #if __ARM_FP & 2
58 #define USING_NEON_F16C
59 #endif
60 #elif defined(__GNUC__)
61 // GCC's arm_neon.h guards them with the FP16 format macros (IEEE and ALTERNATIVE).
62 // We don't actually want the alternative format - we're reading/writing IEEE f16 values.
63 #if defined(__ARM_FP16_FORMAT_IEEE)
64 #define USING_NEON_F16C
65 #endif
66 #endif
67 #endif
68
69 // These -Wvector-conversion warnings seem to trigger in very bogus situations,
70 // like vst3q_f32() expecting a 16x char rather than a 4x float vector. :/
71 #if defined(USING_NEON) && defined(__clang__)
72 #pragma clang diagnostic ignored "-Wvector-conversion"
73 #endif
74
75 // GCC & Clang (but not clang-cl) warn returning U64 on x86 is larger than a register.
76 // You'd see warnings like, "using AVX even though AVX is not enabled".
77 // We stifle these warnings; our helpers that return U64 are always inlined.
78 #if defined(__SSE__) && defined(__GNUC__)
79 #if !defined(__has_warning)
80 #pragma GCC diagnostic ignored "-Wpsabi"
81 #elif __has_warning("-Wpsabi")
82 #pragma GCC diagnostic ignored "-Wpsabi"
83 #endif
84 #endif
85
86 // We tag most helper functions as SI, to enforce good code generation
87 // but also work around what we think is a bug in GCC: when targeting 32-bit
88 // x86, GCC tends to pass U16 (4x uint16_t vector) function arguments in the
89 // MMX mm0 register, which seems to mess with unrelated code that later uses
90 // x87 FP instructions (MMX's mm0 is an alias for x87's st0 register).
91 #if defined(__clang__) || defined(__GNUC__)
92 #define SI static inline __attribute__((always_inline))
93 #else
94 #define SI static inline
95 #endif
96
97 template <typename T, typename P>
98 SI T load(const P* ptr) {
99 T val;
100 memcpy(&val, ptr, sizeof(val));
101 return val;
102 }
103 template <typename T, typename P>
104 SI void store(P* ptr, const T& val) {
105 memcpy(ptr, &val, sizeof(val));
106 }
107
108 // (T)v is a cast when N == 1 and a bit-pun when N>1,
109 // so we use cast<T>(v) to actually cast or bit_pun<T>(v) to bit-pun.
110 template <typename D, typename S>
111 SI D cast(const S& v) {
112 #if N == 1
113 return (D)v;
114 #else
115 return __builtin_convertvector(v, D);
116
117 #endif
118 }
119
120 template <typename D, typename S>
121 SI D bit_pun(const S& v) {
122 static_assert(sizeof(D) == sizeof(v), "");
123 return load<D>(&v);
124 }
125
126 // When we convert from float to fixed point, it's very common to want to round,
127 // and for some reason compilers generate better code when converting to int32_t.
128 // To serve both those ends, we use this function to_fixed() instead of direct cast().
129 SI U32 to_fixed(F f) { return (U32)cast<I32>(f + 0.5f); }
130
131 // Sometimes we do something crazy on one branch of a conditonal,
132 // like divide by zero or convert a huge float to an integer,
133 // but then harmlessly select the other side. That trips up N==1
134 // sanitizer builds, so we make if_then_else() a macro to avoid
135 // evaluating the unused side.
136
137 #if N == 1
138 #define if_then_else(cond, t, e) ((cond) ? (t) : (e))
139 #else
140 template <typename C, typename T>
141 SI T if_then_else(C cond, T t, T e) {
142 return bit_pun<T>( ( cond & bit_pun<C>(t)) |
143 (~cond & bit_pun<C>(e)) );
144 }
145 #endif
146
147
148 SI F F_from_Half(U16 half) {
149 #if defined(USING_NEON_F16C)
150 return vcvt_f32_f16((float16x4_t)half);
151 #elif defined(USING_AVX512F)
152 return (F)_mm512_cvtph_ps((__m256i)half);
153 #elif defined(USING_AVX_F16C)
154 #if defined(__clang__) && __clang_major__ >= 15 // for _Float16 support
155 typedef _Float16 __attribute__((vector_size(16))) F16;
156 return __builtin_convertvector((F16)half, F);
157 #else
158 typedef int16_t __attribute__((vector_size(16))) I16;
159 return __builtin_ia32_vcvtph2ps256((I16)half);
160 #endif // defined(__clang))
161 #else
162 U32 wide = cast<U32>(half);
163 // A half is 1-5-10 sign-exponent-mantissa, with 15 exponent bias.
164 U32 s = wide & 0x8000,
165 em = wide ^ s;
166
167 // Constructing the float is easy if the half is not denormalized.
168 F norm = bit_pun<F>( (s<<16) + (em<<13) + ((127-15)<<23) );
169
170 // Simply flush all denorm half floats to zero.
171 return if_then_else(em < 0x0400, F0, norm);
172 #endif
173 }
174
175 #if defined(__clang__)
176 // The -((127-15)<<10) underflows that side of the math when
177 // we pass a denorm half float. It's harmless... we'll take the 0 side anyway.
178 __attribute__((no_sanitize("unsigned-integer-overflow")))
179 #endif
180 SI U16 Half_from_F(F f) {
181 #if defined(USING_NEON_F16C)
182 return (U16)vcvt_f16_f32(f);
183 #elif defined(USING_AVX512F)
184 return (U16)_mm512_cvtps_ph((__m512 )f, _MM_FROUND_CUR_DIRECTION );
185 #elif defined(USING_AVX_F16C)
186 return (U16)__builtin_ia32_vcvtps2ph256(f, 0x04/*_MM_FROUND_CUR_DIRECTION*/);
187 #else
188 // A float is 1-8-23 sign-exponent-mantissa, with 127 exponent bias.
189 U32 sem = bit_pun<U32>(f),
190 s = sem & 0x80000000,
191 em = sem ^ s;
192
193 // For simplicity we flush denorm half floats (including all denorm floats) to zero.
194 return cast<U16>(if_then_else(em < 0x38800000, (U32)F0
195 , (s>>16) + (em>>13) - ((127-15)<<10)));
196 #endif
197 }
198
199 // Swap high and low bytes of 16-bit lanes, converting between big-endian and little-endian.
200 #if defined(USING_NEON)
201 SI U16 swap_endian_16(U16 v) {
202 return (U16)vrev16_u8((uint8x8_t) v);
203 }
204 #endif
205
206 SI U64 swap_endian_16x4(const U64& rgba) {
207 return (rgba & 0x00ff00ff00ff00ff) << 8
208 | (rgba & 0xff00ff00ff00ff00) >> 8;
209 }
210
211 #if defined(USING_NEON)
212 SI F min_(F x, F y) { return (F)vminq_f32((float32x4_t)x, (float32x4_t)y); }
213 SI F max_(F x, F y) { return (F)vmaxq_f32((float32x4_t)x, (float32x4_t)y); }
214 #elif defined(__loongarch_sx)
215 SI F min_(F x, F y) { return (F)__lsx_vfmin_s(x, y); }
216 SI F max_(F x, F y) { return (F)__lsx_vfmax_s(x, y); }
217 #else
218 SI F min_(F x, F y) { return if_then_else(x > y, y, x); }
219 SI F max_(F x, F y) { return if_then_else(x < y, y, x); }
220 #endif
221
222 SI F floor_(F x) {
223 #if N == 1
224 return floorf_(x);
225 #elif defined(__aarch64__)
226 return vrndmq_f32(x);
227 #elif defined(USING_AVX512F)
228 // Clang's _mm512_floor_ps() passes its mask as -1, not (__mmask16)-1,
229 // and integer santizer catches that this implicit cast changes the
230 // value from -1 to 65535. We'll cast manually to work around it.
231 // Read this as `return _mm512_floor_ps(x)`.
232 return _mm512_mask_floor_ps(x, (__mmask16)-1, x);
233 #elif defined(USING_AVX)
234 return __builtin_ia32_roundps256(x, 0x01/*_MM_FROUND_FLOOR*/);
235 #elif defined(__SSE4_1__)
236 return _mm_floor_ps(x);
237 #elif defined(__loongarch_sx)
238 return __lsx_vfrintrm_s((__m128)x);
239 #else
240 // Round trip through integers with a truncating cast.
241 F roundtrip = cast<F>(cast<I32>(x));
242 // If x is negative, truncating gives the ceiling instead of the floor.
243 return roundtrip - if_then_else(roundtrip > x, F1, F0);
244
245 // This implementation fails for values of x that are outside
246 // the range an integer can represent. We expect most x to be small.
247 #endif
248 }
249
250 SI F approx_log2(F x) {
251 // The first approximation of log2(x) is its exponent 'e', minus 127.
252 I32 bits = bit_pun<I32>(x);
253
254 F e = cast<F>(bits) * (1.0f / (1<<23));
255
256 // If we use the mantissa too we can refine the error signficantly.
257 F m = bit_pun<F>( (bits & 0x007fffff) | 0x3f000000 );
258
259 return e - 124.225514990f
260 - 1.498030302f*m
261 - 1.725879990f/(0.3520887068f + m);
262 }
263
264 SI F approx_log(F x) {
265 const float ln2 = 0.69314718f;
266 return ln2 * approx_log2(x);
267 }
268
269 SI F approx_exp2(F x) {
270 F fract = x - floor_(x);
271
272 F fbits = (1.0f * (1<<23)) * (x + 121.274057500f
273 - 1.490129070f*fract
274 + 27.728023300f/(4.84252568f - fract));
275 I32 bits = cast<I32>(min_(max_(fbits, F0), FInfBits));
276
277 return bit_pun<F>(bits);
278 }
279
280 SI F approx_pow(F x, float y) {
281 return if_then_else((x == F0) | (x == F1), x
282 , approx_exp2(approx_log2(x) * y));
283 }
284
285 SI F approx_exp(F x) {
286 const float log2_e = 1.4426950408889634074f;
287 return approx_exp2(log2_e * x);
288 }
289
290 SI F strip_sign(F x, U32* sign) {
291 U32 bits = bit_pun<U32>(x);
292 *sign = bits & 0x80000000;
293 return bit_pun<F>(bits ^ *sign);
294 }
295
296 SI F apply_sign(F x, U32 sign) {
297 return bit_pun<F>(sign | bit_pun<U32>(x));
298 }
299
300 // Return tf(x).
301 SI F apply_tf(const skcms_TransferFunction* tf, F x) {
302 // Peel off the sign bit and set x = |x|.
303 U32 sign;
304 x = strip_sign(x, &sign);
305
306 // The transfer function has a linear part up to d, exponential at d and after.
307 F v = if_then_else(x < tf->d, tf->c*x + tf->f
308 , approx_pow(tf->a*x + tf->b, tf->g) + tf->e);
309
310 // Tack the sign bit back on.
311 return apply_sign(v, sign);
312 }
313
314 // Return the gamma function (|x|^G with the original sign re-applied to x).
315 SI F apply_gamma(const skcms_TransferFunction* tf, F x) {
316 U32 sign;
317 x = strip_sign(x, &sign);
318 return apply_sign(approx_pow(x, tf->g), sign);
319 }
320
321 SI F apply_pq(const skcms_TransferFunction* tf, F x) {
322 U32 bits = bit_pun<U32>(x),
323 sign = bits & 0x80000000;
324 x = bit_pun<F>(bits ^ sign);
325
326 F v = approx_pow(max_(tf->a + tf->b * approx_pow(x, tf->c), F0)
327 / (tf->d + tf->e * approx_pow(x, tf->c)),
328 tf->f);
329
330 return bit_pun<F>(sign | bit_pun<U32>(v));
331 }
332
333 SI F apply_hlg(const skcms_TransferFunction* tf, F x) {
334 const float R = tf->a, G = tf->b,
335 a = tf->c, b = tf->d, c = tf->e,
336 K = tf->f + 1;
337 U32 bits = bit_pun<U32>(x),
338 sign = bits & 0x80000000;
339 x = bit_pun<F>(bits ^ sign);
340
341 F v = if_then_else(x*R <= 1, approx_pow(x*R, G)
342 , approx_exp((x-c)*a) + b);
343
344 return K*bit_pun<F>(sign | bit_pun<U32>(v));
345 }
346
347 SI F apply_hlginv(const skcms_TransferFunction* tf, F x) {
348 const float R = tf->a, G = tf->b,
349 a = tf->c, b = tf->d, c = tf->e,
350 K = tf->f + 1;
351 U32 bits = bit_pun<U32>(x),
352 sign = bits & 0x80000000;
353 x = bit_pun<F>(bits ^ sign);
354 x /= K;
355
356 F v = if_then_else(x <= 1, R * approx_pow(x, G)
357 , a * approx_log(x - b) + c);
358
359 return bit_pun<F>(sign | bit_pun<U32>(v));
360 }
361
362 // Compute the luminance Y used in the HLG OOTF. This is equivalent to computing the dot product
363 // with the vector [0.2627 0.678 0.0593] in Rec2020 primaries, but is performed in the XYZD50
364 // space to simplify the pipeline.
365 SI F compute_Y_in_xyzd50(F x, F y, F z) {
366 return -0.02831655f * x +
367 1.00995452f * y +
368 0.02102382f * z;
369 }
370
371 // Strided loads and stores of N values, starting from p.
372 template <typename T, typename P>
373 SI T load_3(const P* p) {
374 #if N == 1
375 return (T)p[0];
376 #elif N == 4
377 return T{p[ 0],p[ 3],p[ 6],p[ 9]};
378 #elif N == 8
379 return T{p[ 0],p[ 3],p[ 6],p[ 9], p[12],p[15],p[18],p[21]};
380 #elif N == 16
381 return T{p[ 0],p[ 3],p[ 6],p[ 9], p[12],p[15],p[18],p[21],
382 p[24],p[27],p[30],p[33], p[36],p[39],p[42],p[45]};
383 #endif
384 }
385
386 template <typename T, typename P>
387 SI T load_4(const P* p) {
388 #if N == 1
389 return (T)p[0];
390 #elif N == 4
391 return T{p[ 0],p[ 4],p[ 8],p[12]};
392 #elif N == 8
393 return T{p[ 0],p[ 4],p[ 8],p[12], p[16],p[20],p[24],p[28]};
394 #elif N == 16
395 return T{p[ 0],p[ 4],p[ 8],p[12], p[16],p[20],p[24],p[28],
396 p[32],p[36],p[40],p[44], p[48],p[52],p[56],p[60]};
397 #endif
398 }
399
400 template <typename T, typename P>
401 SI void store_3(P* p, const T& v) {
402 #if N == 1
403 p[0] = v;
404 #elif N == 4
405 p[ 0] = v[ 0]; p[ 3] = v[ 1]; p[ 6] = v[ 2]; p[ 9] = v[ 3];
406 #elif N == 8
407 p[ 0] = v[ 0]; p[ 3] = v[ 1]; p[ 6] = v[ 2]; p[ 9] = v[ 3];
408 p[12] = v[ 4]; p[15] = v[ 5]; p[18] = v[ 6]; p[21] = v[ 7];
409 #elif N == 16
410 p[ 0] = v[ 0]; p[ 3] = v[ 1]; p[ 6] = v[ 2]; p[ 9] = v[ 3];
411 p[12] = v[ 4]; p[15] = v[ 5]; p[18] = v[ 6]; p[21] = v[ 7];
412 p[24] = v[ 8]; p[27] = v[ 9]; p[30] = v[10]; p[33] = v[11];
413 p[36] = v[12]; p[39] = v[13]; p[42] = v[14]; p[45] = v[15];
414 #endif
415 }
416
417 template <typename T, typename P>
418 SI void store_4(P* p, const T& v) {
419 #if N == 1
420 p[0] = v;
421 #elif N == 4
422 p[ 0] = v[ 0]; p[ 4] = v[ 1]; p[ 8] = v[ 2]; p[12] = v[ 3];
423 #elif N == 8
424 p[ 0] = v[ 0]; p[ 4] = v[ 1]; p[ 8] = v[ 2]; p[12] = v[ 3];
425 p[16] = v[ 4]; p[20] = v[ 5]; p[24] = v[ 6]; p[28] = v[ 7];
426 #elif N == 16
427 p[ 0] = v[ 0]; p[ 4] = v[ 1]; p[ 8] = v[ 2]; p[12] = v[ 3];
428 p[16] = v[ 4]; p[20] = v[ 5]; p[24] = v[ 6]; p[28] = v[ 7];
429 p[32] = v[ 8]; p[36] = v[ 9]; p[40] = v[10]; p[44] = v[11];
430 p[48] = v[12]; p[52] = v[13]; p[56] = v[14]; p[60] = v[15];
431 #endif
432 }
433
434
435 SI U8 gather_8(const uint8_t* p, I32 ix) {
436 #if N == 1
437 U8 v = p[ix];
438 #elif N == 4
439 U8 v = { p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]] };
440 #elif N == 8
441 U8 v = { p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]],
442 p[ix[4]], p[ix[5]], p[ix[6]], p[ix[7]] };
443 #elif N == 16
444 U8 v = { p[ix[ 0]], p[ix[ 1]], p[ix[ 2]], p[ix[ 3]],
445 p[ix[ 4]], p[ix[ 5]], p[ix[ 6]], p[ix[ 7]],
446 p[ix[ 8]], p[ix[ 9]], p[ix[10]], p[ix[11]],
447 p[ix[12]], p[ix[13]], p[ix[14]], p[ix[15]] };
448 #endif
449 return v;
450 }
451
452 SI U16 gather_16(const uint8_t* p, I32 ix) {
453 // Load the i'th 16-bit value from p.
454 auto load_16 = [p](int i) {
455 return load<uint16_t>(p + 2*i);
456 };
457 #if N == 1
458 U16 v = load_16(ix);
459 #elif N == 4
460 U16 v = { load_16(ix[0]), load_16(ix[1]), load_16(ix[2]), load_16(ix[3]) };
461 #elif N == 8
462 U16 v = { load_16(ix[0]), load_16(ix[1]), load_16(ix[2]), load_16(ix[3]),
463 load_16(ix[4]), load_16(ix[5]), load_16(ix[6]), load_16(ix[7]) };
464 #elif N == 16
465 U16 v = { load_16(ix[ 0]), load_16(ix[ 1]), load_16(ix[ 2]), load_16(ix[ 3]),
466 load_16(ix[ 4]), load_16(ix[ 5]), load_16(ix[ 6]), load_16(ix[ 7]),
467 load_16(ix[ 8]), load_16(ix[ 9]), load_16(ix[10]), load_16(ix[11]),
468 load_16(ix[12]), load_16(ix[13]), load_16(ix[14]), load_16(ix[15]) };
469 #endif
470 return v;
471 }
472
473 SI U32 gather_32(const uint8_t* p, I32 ix) {
474 // Load the i'th 32-bit value from p.
475 auto load_32 = [p](int i) {
476 return load<uint32_t>(p + 4*i);
477 };
478 #if N == 1
479 U32 v = load_32(ix);
480 #elif N == 4
481 U32 v = { load_32(ix[0]), load_32(ix[1]), load_32(ix[2]), load_32(ix[3]) };
482 #elif N == 8
483 U32 v = { load_32(ix[0]), load_32(ix[1]), load_32(ix[2]), load_32(ix[3]),
484 load_32(ix[4]), load_32(ix[5]), load_32(ix[6]), load_32(ix[7]) };
485 #elif N == 16
486 U32 v = { load_32(ix[ 0]), load_32(ix[ 1]), load_32(ix[ 2]), load_32(ix[ 3]),
487 load_32(ix[ 4]), load_32(ix[ 5]), load_32(ix[ 6]), load_32(ix[ 7]),
488 load_32(ix[ 8]), load_32(ix[ 9]), load_32(ix[10]), load_32(ix[11]),
489 load_32(ix[12]), load_32(ix[13]), load_32(ix[14]), load_32(ix[15]) };
490 #endif
491 // TODO: AVX2 and AVX-512 gathers (c.f. gather_24).
492 return v;
493 }
494
495 SI U32 gather_24(const uint8_t* p, I32 ix) {
496 // First, back up a byte. Any place we're gathering from has a safe junk byte to read
497 // in front of it, either a previous table value, or some tag metadata.
498 p -= 1;
499
500 // Load the i'th 24-bit value from p, and 1 extra byte.
501 auto load_24_32 = [p](int i) {
502 return load<uint32_t>(p + 3*i);
503 };
504
505 // Now load multiples of 4 bytes (a junk byte, then r,g,b).
506 #if N == 1
507 U32 v = load_24_32(ix);
508 #elif N == 4
509 U32 v = { load_24_32(ix[0]), load_24_32(ix[1]), load_24_32(ix[2]), load_24_32(ix[3]) };
510 #elif N == 8 && !defined(USING_AVX2)
511 U32 v = { load_24_32(ix[0]), load_24_32(ix[1]), load_24_32(ix[2]), load_24_32(ix[3]),
512 load_24_32(ix[4]), load_24_32(ix[5]), load_24_32(ix[6]), load_24_32(ix[7]) };
513 #elif N == 8
514 (void)load_24_32;
515 // The gather instruction here doesn't need any particular alignment,
516 // but the intrinsic takes a const int*.
517 const int* p4 = bit_pun<const int*>(p);
518 I32 zero = { 0, 0, 0, 0, 0, 0, 0, 0},
519 mask = {-1,-1,-1,-1, -1,-1,-1,-1};
520 #if defined(__clang__)
521 U32 v = (U32)__builtin_ia32_gatherd_d256(zero, p4, 3*ix, mask, 1);
522 #elif defined(__GNUC__)
523 U32 v = (U32)__builtin_ia32_gathersiv8si(zero, p4, 3*ix, mask, 1);
524 #endif
525 #elif N == 16
526 (void)load_24_32;
527 // The intrinsic is supposed to take const void* now, but it takes const int*, just like AVX2.
528 // And AVX-512 swapped the order of arguments. :/
529 const int* p4 = bit_pun<const int*>(p);
530 U32 v = (U32)_mm512_i32gather_epi32((__m512i)(3*ix), p4, 1);
531 #endif
532
533 // Shift off the junk byte, leaving r,g,b in low 24 bits (and zero in the top 8).
534 return v >> 8;
535 }
536
537 #if !defined(__arm__)
538 SI void gather_48(const uint8_t* p, I32 ix, U64* v) {
539 // As in gather_24(), with everything doubled.
540 p -= 2;
541
542 // Load the i'th 48-bit value from p, and 2 extra bytes.
543 auto load_48_64 = [p](int i) {
544 return load<uint64_t>(p + 6*i);
545 };
546
547 #if N == 1
548 *v = load_48_64(ix);
549 #elif N == 4
550 *v = U64{
551 load_48_64(ix[0]), load_48_64(ix[1]), load_48_64(ix[2]), load_48_64(ix[3]),
552 };
553 #elif N == 8 && !defined(USING_AVX2)
554 *v = U64{
555 load_48_64(ix[0]), load_48_64(ix[1]), load_48_64(ix[2]), load_48_64(ix[3]),
556 load_48_64(ix[4]), load_48_64(ix[5]), load_48_64(ix[6]), load_48_64(ix[7]),
557 };
558 #elif N == 8
559 (void)load_48_64;
560 typedef int32_t __attribute__((vector_size(16))) Half_I32;
561 typedef long long __attribute__((vector_size(32))) Half_I64;
562
563 // The gather instruction here doesn't need any particular alignment,
564 // but the intrinsic takes a const long long*.
565 const long long int* p8 = bit_pun<const long long int*>(p);
566
567 Half_I64 zero = { 0, 0, 0, 0},
568 mask = {-1,-1,-1,-1};
569
570 ix *= 6;
571 Half_I32 ix_lo = { ix[0], ix[1], ix[2], ix[3] },
572 ix_hi = { ix[4], ix[5], ix[6], ix[7] };
573
574 #if defined(__clang__)
575 Half_I64 lo = (Half_I64)__builtin_ia32_gatherd_q256(zero, p8, ix_lo, mask, 1),
576 hi = (Half_I64)__builtin_ia32_gatherd_q256(zero, p8, ix_hi, mask, 1);
577 #elif defined(__GNUC__)
578 Half_I64 lo = (Half_I64)__builtin_ia32_gathersiv4di(zero, p8, ix_lo, mask, 1),
579 hi = (Half_I64)__builtin_ia32_gathersiv4di(zero, p8, ix_hi, mask, 1);
580 #endif
581 store((char*)v + 0, lo);
582 store((char*)v + 32, hi);
583 #elif N == 16
584 (void)load_48_64;
585 const long long int* p8 = bit_pun<const long long int*>(p);
586 __m512i lo = _mm512_i32gather_epi64(_mm512_extracti32x8_epi32((__m512i)(6*ix), 0), p8, 1),
587 hi = _mm512_i32gather_epi64(_mm512_extracti32x8_epi32((__m512i)(6*ix), 1), p8, 1);
588 store((char*)v + 0, lo);
589 store((char*)v + 64, hi);
590 #endif
591
592 *v >>= 16;
593 }
594 #endif
595
596 SI F F_from_U8(U8 v) {
597 return cast<F>(v) * (1/255.0f);
598 }
599
600 SI F F_from_U16_BE(U16 v) {
601 // All 16-bit ICC values are big-endian, so we byte swap before converting to float.
602 // MSVC catches the "loss" of data here in the portable path, so we also make sure to mask.
603 U16 lo = (v >> 8),
604 hi = (v << 8) & 0xffff;
605 return cast<F>(lo|hi) * (1/65535.0f);
606 }
607
608 SI U16 U16_from_F(F v) {
609 // 65535 == inf in FP16, so promote to FP32 before converting.
610 return cast<U16>(cast<V<float>>(v) * 65535 + 0.5f);
611 }
612
613 SI F minus_1_ulp(F v) {
614 return bit_pun<F>( bit_pun<U32>(v) - 1 );
615 }
616
617 SI F table(const skcms_Curve* curve, F v) {
618 // Clamp the input to [0,1], then scale to a table index.
619 F ix = max_(F0, min_(v, F1)) * (float)(curve->table_entries - 1);
620
621 // We'll look up (equal or adjacent) entries at lo and hi, then lerp by t between the two.
622 I32 lo = cast<I32>( ix ),
623 hi = cast<I32>(minus_1_ulp(ix+1.0f));
624 F t = ix - cast<F>(lo); // i.e. the fractional part of ix.
625
626 // TODO: can we load l and h simultaneously? Each entry in 'h' is either
627 // the same as in 'l' or adjacent. We have a rough idea that's it'd always be safe
628 // to read adjacent entries and perhaps underflow the table by a byte or two
629 // (it'd be junk, but always safe to read). Not sure how to lerp yet.
630 F l,h;
631 if (curve->table_8) {
632 l = F_from_U8(gather_8(curve->table_8, lo));
633 h = F_from_U8(gather_8(curve->table_8, hi));
634 } else {
635 l = F_from_U16_BE(gather_16(curve->table_16, lo));
636 h = F_from_U16_BE(gather_16(curve->table_16, hi));
637 }
638 return l + (h-l)*t;
639 }
640
641 SI void sample_clut_8(const uint8_t* grid_8, I32 ix, F* r, F* g, F* b) {
642 U32 rgb = gather_24(grid_8, ix);
643
644 *r = cast<F>((rgb >> 0) & 0xff) * (1/255.0f);
645 *g = cast<F>((rgb >> 8) & 0xff) * (1/255.0f);
646 *b = cast<F>((rgb >> 16) & 0xff) * (1/255.0f);
647 }
648
649 SI void sample_clut_8(const uint8_t* grid_8, I32 ix, F* r, F* g, F* b, F* a) {
650 // TODO: don't forget to optimize gather_32().
651 U32 rgba = gather_32(grid_8, ix);
652
653 *r = cast<F>((rgba >> 0) & 0xff) * (1/255.0f);
654 *g = cast<F>((rgba >> 8) & 0xff) * (1/255.0f);
655 *b = cast<F>((rgba >> 16) & 0xff) * (1/255.0f);
656 *a = cast<F>((rgba >> 24) & 0xff) * (1/255.0f);
657 }
658
659 SI void sample_clut_16(const uint8_t* grid_16, I32 ix, F* r, F* g, F* b) {
660 #if defined(__arm__) || defined(__loongarch_sx)
661 // This is up to 2x faster on 32-bit ARM than the #else-case fast path.
662 *r = F_from_U16_BE(gather_16(grid_16, 3*ix+0));
663 *g = F_from_U16_BE(gather_16(grid_16, 3*ix+1));
664 *b = F_from_U16_BE(gather_16(grid_16, 3*ix+2));
665 #else
666 // This strategy is much faster for 64-bit builds, and fine for 32-bit x86 too.
667 U64 rgb;
668 gather_48(grid_16, ix, &rgb);
669 rgb = swap_endian_16x4(rgb);
670
671 *r = cast<F>((rgb >> 0) & 0xffff) * (1/65535.0f);
672 *g = cast<F>((rgb >> 16) & 0xffff) * (1/65535.0f);
673 *b = cast<F>((rgb >> 32) & 0xffff) * (1/65535.0f);
674 #endif
675 }
676
677 SI void sample_clut_16(const uint8_t* grid_16, I32 ix, F* r, F* g, F* b, F* a) {
678 // TODO: gather_64()-based fast path?
679 *r = F_from_U16_BE(gather_16(grid_16, 4*ix+0));
680 *g = F_from_U16_BE(gather_16(grid_16, 4*ix+1));
681 *b = F_from_U16_BE(gather_16(grid_16, 4*ix+2));
682 *a = F_from_U16_BE(gather_16(grid_16, 4*ix+3));
683 }
684
685 static void clut(uint32_t input_channels, uint32_t output_channels,
686 const uint8_t grid_points[4], const uint8_t* grid_8, const uint8_t* grid_16,
687 F* r, F* g, F* b, F* a) {
688
689 const int dim = (int)input_channels;
690 assert (0 < dim && dim <= 4);
691 assert (output_channels == 3 ||
692 output_channels == 4);
693
694 // For each of these arrays, think foo[2*dim], but we use foo[8] since we know dim <= 4.
695 I32 index [8]; // Index contribution by dimension, first low from 0, then high from 4.
696 F weight[8]; // Weight for each contribution, again first low, then high.
697
698 // O(dim) work first: calculate index,weight from r,g,b,a.
699 const F inputs[] = { *r,*g,*b,*a };
700 for (int i = dim-1, stride = 1; i >= 0; i--) {
701 // x is where we logically want to sample the grid in the i-th dimension.
702 F x = inputs[i] * (float)(grid_points[i] - 1);
703
704 // But we can't index at floats. lo and hi are the two integer grid points surrounding x.
705 I32 lo = cast<I32>( x ), // i.e. trunc(x) == floor(x) here.
706 hi = cast<I32>(minus_1_ulp(x+1.0f));
707 // Notice how we fold in the accumulated stride across previous dimensions here.
708 index[i+0] = lo * stride;
709 index[i+4] = hi * stride;
710 stride *= grid_points[i];
711
712 // We'll interpolate between those two integer grid points by t.
713 F t = x - cast<F>(lo); // i.e. fract(x)
714 weight[i+0] = 1-t;
715 weight[i+4] = t;
716 }
717
718 *r = *g = *b = F0;
719 if (output_channels == 4) {
720 *a = F0;
721 }
722
723 // We'll sample 2^dim == 1<<dim table entries per pixel,
724 // in all combinations of low and high in each dimension.
725 for (int combo = 0; combo < (1<<dim); combo++) { // This loop can be done in any order.
726
727 // Each of these upcoming (combo&N)*K expressions here evaluates to 0 or 4,
728 // where 0 selects the low index contribution and its weight 1-t,
729 // or 4 the high index contribution and its weight t.
730
731 // Since 0<dim≤4, we can always just start off with the 0-th channel,
732 // then handle the others conditionally.
733 I32 ix = index [0 + (combo&1)*4];
734 F w = weight[0 + (combo&1)*4];
735
736 switch ((dim-1)&3) { // This lets the compiler know there are no other cases to handle.
737 case 3: ix += index [3 + (combo&8)/2];
738 w *= weight[3 + (combo&8)/2];
739 SKCMS_FALLTHROUGH;
740 // fall through
741
742 case 2: ix += index [2 + (combo&4)*1];
743 w *= weight[2 + (combo&4)*1];
744 SKCMS_FALLTHROUGH;
745 // fall through
746
747 case 1: ix += index [1 + (combo&2)*2];
748 w *= weight[1 + (combo&2)*2];
749 }
750
751 F R,G,B,A=F0;
752 if (output_channels == 3) {
753 if (grid_8) { sample_clut_8 (grid_8 ,ix, &R,&G,&B); }
754 else { sample_clut_16(grid_16,ix, &R,&G,&B); }
755 } else {
756 if (grid_8) { sample_clut_8 (grid_8 ,ix, &R,&G,&B,&A); }
757 else { sample_clut_16(grid_16,ix, &R,&G,&B,&A); }
758 }
759 *r += w*R;
760 *g += w*G;
761 *b += w*B;
762 *a += w*A;
763 }
764 }
765
766 static void clut(const skcms_A2B* a2b, F* r, F* g, F* b, F a) {
767 clut(a2b->input_channels, a2b->output_channels,
768 a2b->grid_points, a2b->grid_8, a2b->grid_16,
769 r,g,b,&a);
770 }
771 static void clut(const skcms_B2A* b2a, F* r, F* g, F* b, F* a) {
772 clut(b2a->input_channels, b2a->output_channels,
773 b2a->grid_points, b2a->grid_8, b2a->grid_16,
774 r,g,b,a);
775 }
776
777 struct NoCtx {};
778
779 struct Ctx {
780 const void* fArg;
781 operator NoCtx() { return NoCtx{}; }
782 template <typename T> operator T*() { return (const T*)fArg; }
783 };
784
785 #define STAGE_PARAMS(MAYBE_REF) SKCMS_MAYBE_UNUSED const char* src, \
786 SKCMS_MAYBE_UNUSED char* dst, \
787 SKCMS_MAYBE_UNUSED F MAYBE_REF r, \
788 SKCMS_MAYBE_UNUSED F MAYBE_REF g, \
789 SKCMS_MAYBE_UNUSED F MAYBE_REF b, \
790 SKCMS_MAYBE_UNUSED F MAYBE_REF a, \
791 SKCMS_MAYBE_UNUSED int i
792
793 #if SKCMS_HAS_MUSTTAIL
794
795 // Stages take a stage list, and each stage is responsible for tail-calling the next one.
796 //
797 // Unfortunately, we can't declare a StageFn as a function pointer which takes a pointer to
798 // another StageFn; declaring this leads to a circular dependency. To avoid this, StageFn is
799 // wrapped in a single-element `struct StageList` which we are able to forward-declare.
800 struct StageList;
801 using StageFn = void (*)(StageList stages, const void** ctx, STAGE_PARAMS());
802 struct StageList {
803 const StageFn* fn;
804 };
805
806 #define DECLARE_STAGE(name, arg, CALL_NEXT) \
807 SI void Exec_##name##_k(arg, STAGE_PARAMS(&)); \
808 \
809 SI void Exec_##name(StageList list, const void** ctx, STAGE_PARAMS()) { \
810 Exec_##name##_k(Ctx{*ctx}, src, dst, r, g, b, a, i); \
811 ++list.fn; ++ctx; \
812 CALL_NEXT; \
813 } \
814 \
815 SI void Exec_##name##_k(arg, STAGE_PARAMS(&))
816
817 #define STAGE(name, arg) \
818 DECLARE_STAGE(name, arg, [[clang::musttail]] return (*list.fn)(list, ctx, src, dst, \
819 r, g, b, a, i))
820
821 #define FINAL_STAGE(name, arg) \
822 DECLARE_STAGE(name, arg, /* Stop executing stages and return to the caller. */)
823
824 #else
825
826 #define DECLARE_STAGE(name, arg) \
827 SI void Exec_##name##_k(arg, STAGE_PARAMS(&)); \
828 \
829 SI void Exec_##name(const void* ctx, STAGE_PARAMS(&)) { \
830 Exec_##name##_k(Ctx{ctx}, src, dst, r, g, b, a, i); \
831 } \
832 \
833 SI void Exec_##name##_k(arg, STAGE_PARAMS(&))
834
835 #define STAGE(name, arg) DECLARE_STAGE(name, arg)
836 #define FINAL_STAGE(name, arg) DECLARE_STAGE(name, arg)
837
838 #endif
839
840 STAGE(load_a8, NoCtx) {
841 a = F_from_U8(load<U8>(src + 1*i));
842 }
843
844 STAGE(load_g8, NoCtx) {
845 r = g = b = F_from_U8(load<U8>(src + 1*i));
846 }
847
848 STAGE(load_ga88, NoCtx) {
849 U16 u16 = load<U16>(src + 2 * i);
850 r = g = b = cast<F>((u16 >> 0) & 0xff) * (1 / 255.0f);
851 a = cast<F>((u16 >> 8) & 0xff) * (1 / 255.0f);
852 }
853
854 STAGE(load_4444, NoCtx) {
855 U16 abgr = load<U16>(src + 2*i);
856
857 r = cast<F>((abgr >> 12) & 0xf) * (1/15.0f);
858 g = cast<F>((abgr >> 8) & 0xf) * (1/15.0f);
859 b = cast<F>((abgr >> 4) & 0xf) * (1/15.0f);
860 a = cast<F>((abgr >> 0) & 0xf) * (1/15.0f);
861 }
862
863 STAGE(load_565, NoCtx) {
864 U16 rgb = load<U16>(src + 2*i);
865
866 r = cast<F>(rgb & (uint16_t)(31<< 0)) * (1.0f / (31<< 0));
867 g = cast<F>(rgb & (uint16_t)(63<< 5)) * (1.0f / (63<< 5));
868 b = cast<F>(rgb & (uint16_t)(31<<11)) * (1.0f / (31<<11));
869 }
870
871 STAGE(load_888, NoCtx) {
872 const uint8_t* rgb = (const uint8_t*)(src + 3*i);
873 #if defined(USING_NEON)
874 // There's no uint8x4x3_t or vld3 load for it, so we'll load each rgb pixel one at
875 // a time. Since we're doing that, we might as well load them into 16-bit lanes.
876 // (We'd even load into 32-bit lanes, but that's not possible on ARMv7.)
877 uint8x8x3_t v = {{ vdup_n_u8(0), vdup_n_u8(0), vdup_n_u8(0) }};
878 v = vld3_lane_u8(rgb+0, v, 0);
879 v = vld3_lane_u8(rgb+3, v, 2);
880 v = vld3_lane_u8(rgb+6, v, 4);
881 v = vld3_lane_u8(rgb+9, v, 6);
882
883 // Now if we squint, those 3 uint8x8_t we constructed are really U16s, easy to
884 // convert to F. (Again, U32 would be even better here if drop ARMv7 or split
885 // ARMv7 and ARMv8 impls.)
886 r = cast<F>((U16)v.val[0]) * (1/255.0f);
887 g = cast<F>((U16)v.val[1]) * (1/255.0f);
888 b = cast<F>((U16)v.val[2]) * (1/255.0f);
889 #else
890 r = cast<F>(load_3<U32>(rgb+0) ) * (1/255.0f);
891 g = cast<F>(load_3<U32>(rgb+1) ) * (1/255.0f);
892 b = cast<F>(load_3<U32>(rgb+2) ) * (1/255.0f);
893 #endif
894 }
895
896 STAGE(load_8888, NoCtx) {
897 U32 rgba = load<U32>(src + 4*i);
898
899 r = cast<F>((rgba >> 0) & 0xff) * (1/255.0f);
900 g = cast<F>((rgba >> 8) & 0xff) * (1/255.0f);
901 b = cast<F>((rgba >> 16) & 0xff) * (1/255.0f);
902 a = cast<F>((rgba >> 24) & 0xff) * (1/255.0f);
903 }
904
905 STAGE(load_1010102, NoCtx) {
906 U32 rgba = load<U32>(src + 4*i);
907
908 r = cast<F>((rgba >> 0) & 0x3ff) * (1/1023.0f);
909 g = cast<F>((rgba >> 10) & 0x3ff) * (1/1023.0f);
910 b = cast<F>((rgba >> 20) & 0x3ff) * (1/1023.0f);
911 a = cast<F>((rgba >> 30) & 0x3 ) * (1/ 3.0f);
912 }
913
914 STAGE(load_101010x_XR, NoCtx) {
915 U32 rgba = load<U32>(src + 4*i);
916 r = cast<F>(((rgba >> 0) & 0x3ff) - 384) / 510.0f;
917 g = cast<F>(((rgba >> 10) & 0x3ff) - 384) / 510.0f;
918 b = cast<F>(((rgba >> 20) & 0x3ff) - 384) / 510.0f;
919 }
920
921 STAGE(load_10101010_XR, NoCtx) {
922 U64 rgba = load<U64>(src + 8 * i);
923 // Each channel is 16 bits, where the 6 low bits are padding.
924 r = cast<F>(((rgba >> ( 0+6)) & 0x3ff) - 384) / 510.0f;
925 g = cast<F>(((rgba >> (16+6)) & 0x3ff) - 384) / 510.0f;
926 b = cast<F>(((rgba >> (32+6)) & 0x3ff) - 384) / 510.0f;
927 a = cast<F>(((rgba >> (48+6)) & 0x3ff) - 384) / 510.0f;
928 }
929
930 STAGE(load_161616LE, NoCtx) {
931 uintptr_t ptr = (uintptr_t)(src + 6*i);
932 assert( (ptr & 1) == 0 ); // src must be 2-byte aligned for this
933 const uint16_t* rgb = (const uint16_t*)ptr; // cast to const uint16_t* to be safe.
934 #if defined(USING_NEON)
935 uint16x4x3_t v = vld3_u16(rgb);
936 r = cast<F>((U16)v.val[0]) * (1/65535.0f);
937 g = cast<F>((U16)v.val[1]) * (1/65535.0f);
938 b = cast<F>((U16)v.val[2]) * (1/65535.0f);
939 #else
940 r = cast<F>(load_3<U32>(rgb+0)) * (1/65535.0f);
941 g = cast<F>(load_3<U32>(rgb+1)) * (1/65535.0f);
942 b = cast<F>(load_3<U32>(rgb+2)) * (1/65535.0f);
943 #endif
944 }
945
946 STAGE(load_16161616LE, NoCtx) {
947 uintptr_t ptr = (uintptr_t)(src + 8*i);
948 assert( (ptr & 1) == 0 ); // src must be 2-byte aligned for this
949 const uint16_t* rgba = (const uint16_t*)ptr; // cast to const uint16_t* to be safe.
950 #if defined(USING_NEON)
951 uint16x4x4_t v = vld4_u16(rgba);
952 r = cast<F>((U16)v.val[0]) * (1/65535.0f);
953 g = cast<F>((U16)v.val[1]) * (1/65535.0f);
954 b = cast<F>((U16)v.val[2]) * (1/65535.0f);
955 a = cast<F>((U16)v.val[3]) * (1/65535.0f);
956 #else
957 U64 px = load<U64>(rgba);
958
959 r = cast<F>((px >> 0) & 0xffff) * (1/65535.0f);
960 g = cast<F>((px >> 16) & 0xffff) * (1/65535.0f);
961 b = cast<F>((px >> 32) & 0xffff) * (1/65535.0f);
962 a = cast<F>((px >> 48) & 0xffff) * (1/65535.0f);
963 #endif
964 }
965
966 STAGE(load_161616BE, NoCtx) {
967 uintptr_t ptr = (uintptr_t)(src + 6*i);
968 assert( (ptr & 1) == 0 ); // src must be 2-byte aligned for this
969 const uint16_t* rgb = (const uint16_t*)ptr; // cast to const uint16_t* to be safe.
970 #if defined(USING_NEON)
971 uint16x4x3_t v = vld3_u16(rgb);
972 r = cast<F>(swap_endian_16((U16)v.val[0])) * (1/65535.0f);
973 g = cast<F>(swap_endian_16((U16)v.val[1])) * (1/65535.0f);
974 b = cast<F>(swap_endian_16((U16)v.val[2])) * (1/65535.0f);
975 #else
976 U32 R = load_3<U32>(rgb+0),
977 G = load_3<U32>(rgb+1),
978 B = load_3<U32>(rgb+2);
979 // R,G,B are big-endian 16-bit, so byte swap them before converting to float.
980 r = cast<F>((R & 0x00ff)<<8 | (R & 0xff00)>>8) * (1/65535.0f);
981 g = cast<F>((G & 0x00ff)<<8 | (G & 0xff00)>>8) * (1/65535.0f);
982 b = cast<F>((B & 0x00ff)<<8 | (B & 0xff00)>>8) * (1/65535.0f);
983 #endif
984 }
985
986 STAGE(load_16161616BE, NoCtx) {
987 uintptr_t ptr = (uintptr_t)(src + 8*i);
988 assert( (ptr & 1) == 0 ); // src must be 2-byte aligned for this
989 const uint16_t* rgba = (const uint16_t*)ptr; // cast to const uint16_t* to be safe.
990 #if defined(USING_NEON)
991 uint16x4x4_t v = vld4_u16(rgba);
992 r = cast<F>(swap_endian_16((U16)v.val[0])) * (1/65535.0f);
993 g = cast<F>(swap_endian_16((U16)v.val[1])) * (1/65535.0f);
994 b = cast<F>(swap_endian_16((U16)v.val[2])) * (1/65535.0f);
995 a = cast<F>(swap_endian_16((U16)v.val[3])) * (1/65535.0f);
996 #else
997 U64 px = swap_endian_16x4(load<U64>(rgba));
998
999 r = cast<F>((px >> 0) & 0xffff) * (1/65535.0f);
1000 g = cast<F>((px >> 16) & 0xffff) * (1/65535.0f);
1001 b = cast<F>((px >> 32) & 0xffff) * (1/65535.0f);
1002 a = cast<F>((px >> 48) & 0xffff) * (1/65535.0f);
1003 #endif
1004 }
1005
1006 STAGE(load_hhh, NoCtx) {
1007 uintptr_t ptr = (uintptr_t)(src + 6*i);
1008 assert( (ptr & 1) == 0 ); // src must be 2-byte aligned for this
1009 const uint16_t* rgb = (const uint16_t*)ptr; // cast to const uint16_t* to be safe.
1010 #if defined(USING_NEON)
1011 uint16x4x3_t v = vld3_u16(rgb);
1012 U16 R = (U16)v.val[0],
1013 G = (U16)v.val[1],
1014 B = (U16)v.val[2];
1015 #else
1016 U16 R = load_3<U16>(rgb+0),
1017 G = load_3<U16>(rgb+1),
1018 B = load_3<U16>(rgb+2);
1019 #endif
1020 r = F_from_Half(R);
1021 g = F_from_Half(G);
1022 b = F_from_Half(B);
1023 }
1024
1025 STAGE(load_hhhh, NoCtx) {
1026 uintptr_t ptr = (uintptr_t)(src + 8*i);
1027 assert( (ptr & 1) == 0 ); // src must be 2-byte aligned for this
1028 const uint16_t* rgba = (const uint16_t*)ptr; // cast to const uint16_t* to be safe.
1029 #if defined(USING_NEON)
1030 uint16x4x4_t v = vld4_u16(rgba);
1031 U16 R = (U16)v.val[0],
1032 G = (U16)v.val[1],
1033 B = (U16)v.val[2],
1034 A = (U16)v.val[3];
1035 #else
1036 U64 px = load<U64>(rgba);
1037 U16 R = cast<U16>((px >> 0) & 0xffff),
1038 G = cast<U16>((px >> 16) & 0xffff),
1039 B = cast<U16>((px >> 32) & 0xffff),
1040 A = cast<U16>((px >> 48) & 0xffff);
1041 #endif
1042 r = F_from_Half(R);
1043 g = F_from_Half(G);
1044 b = F_from_Half(B);
1045 a = F_from_Half(A);
1046 }
1047
1048 STAGE(load_fff, NoCtx) {
1049 uintptr_t ptr = (uintptr_t)(src + 12*i);
1050 assert( (ptr & 3) == 0 ); // src must be 4-byte aligned for this
1051 const float* rgb = (const float*)ptr; // cast to const float* to be safe.
1052 #if defined(USING_NEON)
1053 float32x4x3_t v = vld3q_f32(rgb);
1054 r = (F)v.val[0];
1055 g = (F)v.val[1];
1056 b = (F)v.val[2];
1057 #else
1058 r = load_3<F>(rgb+0);
1059 g = load_3<F>(rgb+1);
1060 b = load_3<F>(rgb+2);
1061 #endif
1062 }
1063
1064 STAGE(load_ffff, NoCtx) {
1065 uintptr_t ptr = (uintptr_t)(src + 16*i);
1066 assert( (ptr & 3) == 0 ); // src must be 4-byte aligned for this
1067 const float* rgba = (const float*)ptr; // cast to const float* to be safe.
1068 #if defined(USING_NEON)
1069 float32x4x4_t v = vld4q_f32(rgba);
1070 r = (F)v.val[0];
1071 g = (F)v.val[1];
1072 b = (F)v.val[2];
1073 a = (F)v.val[3];
1074 #else
1075 r = load_4<F>(rgba+0);
1076 g = load_4<F>(rgba+1);
1077 b = load_4<F>(rgba+2);
1078 a = load_4<F>(rgba+3);
1079 #endif
1080 }
1081
1082 STAGE(swap_rb, NoCtx) {
1083 F t = r;
1084 r = b;
1085 b = t;
1086 }
1087
1088 STAGE(clamp, NoCtx) {
1089 r = max_(F0, min_(r, F1));
1090 g = max_(F0, min_(g, F1));
1091 b = max_(F0, min_(b, F1));
1092 a = max_(F0, min_(a, F1));
1093 }
1094
1095 STAGE(invert, NoCtx) {
1096 r = F1 - r;
1097 g = F1 - g;
1098 b = F1 - b;
1099 a = F1 - a;
1100 }
1101
1102 STAGE(force_opaque, NoCtx) {
1103 a = F1;
1104 }
1105
1106 STAGE(premul, NoCtx) {
1107 r *= a;
1108 g *= a;
1109 b *= a;
1110 }
1111
1112 STAGE(unpremul, NoCtx) {
1113 F scale = if_then_else(F1 / a < INFINITY_, F1 / a, F0);
1114 r *= scale;
1115 g *= scale;
1116 b *= scale;
1117 }
1118
1119 STAGE(matrix_3x3, const skcms_Matrix3x3* matrix) {
1120 const float* m = &matrix->vals[0][0];
1121
1122 F R = m[0]*r + m[1]*g + m[2]*b,
1123 G = m[3]*r + m[4]*g + m[5]*b,
1124 B = m[6]*r + m[7]*g + m[8]*b;
1125
1126 r = R;
1127 g = G;
1128 b = B;
1129 }
1130
1131 STAGE(matrix_3x4, const skcms_Matrix3x4* matrix) {
1132 const float* m = &matrix->vals[0][0];
1133
1134 F R = m[0]*r + m[1]*g + m[ 2]*b + m[ 3],
1135 G = m[4]*r + m[5]*g + m[ 6]*b + m[ 7],
1136 B = m[8]*r + m[9]*g + m[10]*b + m[11];
1137
1138 r = R;
1139 g = G;
1140 b = B;
1141 }
1142
1143 STAGE(lab_to_xyz, NoCtx) {
1144 // The L*a*b values are in r,g,b, but normalized to [0,1]. Reconstruct them:
1145 F L = r * 100.0f,
1146 A = g * 255.0f - 128.0f,
1147 B = b * 255.0f - 128.0f;
1148
1149 // Convert to CIE XYZ.
1150 F Y = (L + 16.0f) * (1/116.0f),
1151 X = Y + A*(1/500.0f),
1152 Z = Y - B*(1/200.0f);
1153
1154 X = if_then_else(X*X*X > 0.008856f, X*X*X, (X - (16/116.0f)) * (1/7.787f));
1155 Y = if_then_else(Y*Y*Y > 0.008856f, Y*Y*Y, (Y - (16/116.0f)) * (1/7.787f));
1156 Z = if_then_else(Z*Z*Z > 0.008856f, Z*Z*Z, (Z - (16/116.0f)) * (1/7.787f));
1157
1158 // Adjust to XYZD50 illuminant, and stuff back into r,g,b for the next op.
1159 r = X * 0.9642f;
1160 g = Y ;
1161 b = Z * 0.8249f;
1162 }
1163
1164 // As above, in reverse.
1165 STAGE(xyz_to_lab, NoCtx) {
1166 F X = r * (1/0.9642f),
1167 Y = g,
1168 Z = b * (1/0.8249f);
1169
1170 X = if_then_else(X > 0.008856f, approx_pow(X, 1/3.0f), X*7.787f + (16/116.0f));
1171 Y = if_then_else(Y > 0.008856f, approx_pow(Y, 1/3.0f), Y*7.787f + (16/116.0f));
1172 Z = if_then_else(Z > 0.008856f, approx_pow(Z, 1/3.0f), Z*7.787f + (16/116.0f));
1173
1174 F L = Y*116.0f - 16.0f,
1175 A = (X-Y)*500.0f,
1176 B = (Y-Z)*200.0f;
1177
1178 r = L * (1/100.f);
1179 g = (A + 128.0f) * (1/255.0f);
1180 b = (B + 128.0f) * (1/255.0f);
1181 }
1182
1183 STAGE(gamma_r, const skcms_TransferFunction* tf) { r = apply_gamma(tf, r); }
1184 STAGE(gamma_g, const skcms_TransferFunction* tf) { g = apply_gamma(tf, g); }
1185 STAGE(gamma_b, const skcms_TransferFunction* tf) { b = apply_gamma(tf, b); }
1186 STAGE(gamma_a, const skcms_TransferFunction* tf) { a = apply_gamma(tf, a); }
1187
1188 STAGE(gamma_rgb, const skcms_TransferFunction* tf) {
1189 r = apply_gamma(tf, r);
1190 g = apply_gamma(tf, g);
1191 b = apply_gamma(tf, b);
1192 }
1193
1194 STAGE(tf_r, const skcms_TransferFunction* tf) { r = apply_tf(tf, r); }
1195 STAGE(tf_g, const skcms_TransferFunction* tf) { g = apply_tf(tf, g); }
1196 STAGE(tf_b, const skcms_TransferFunction* tf) { b = apply_tf(tf, b); }
1197 STAGE(tf_a, const skcms_TransferFunction* tf) { a = apply_tf(tf, a); }
1198
1199 STAGE(tf_rgb, const skcms_TransferFunction* tf) {
1200 r = apply_tf(tf, r);
1201 g = apply_tf(tf, g);
1202 b = apply_tf(tf, b);
1203 }
1204
1205 STAGE(pq_r, const skcms_TransferFunction* tf) { r = apply_pq(tf, r); }
1206 STAGE(pq_g, const skcms_TransferFunction* tf) { g = apply_pq(tf, g); }
1207 STAGE(pq_b, const skcms_TransferFunction* tf) { b = apply_pq(tf, b); }
1208 STAGE(pq_a, const skcms_TransferFunction* tf) { a = apply_pq(tf, a); }
1209
1210 STAGE(pq_rgb, const skcms_TransferFunction* tf) {
1211 r = apply_pq(tf, r);
1212 g = apply_pq(tf, g);
1213 b = apply_pq(tf, b);
1214 }
1215
1216 STAGE(hlg_r, const skcms_TransferFunction* tf) { r = apply_hlg(tf, r); }
1217 STAGE(hlg_g, const skcms_TransferFunction* tf) { g = apply_hlg(tf, g); }
1218 STAGE(hlg_b, const skcms_TransferFunction* tf) { b = apply_hlg(tf, b); }
1219 STAGE(hlg_a, const skcms_TransferFunction* tf) { a = apply_hlg(tf, a); }
1220
1221 STAGE(hlg_rgb, const skcms_TransferFunction* tf) {
1222 r = apply_hlg(tf, r);
1223 g = apply_hlg(tf, g);
1224 b = apply_hlg(tf, b);
1225 }
1226
1227 // Apply the HLG Reference OOTF, as described in ITU-R BT.2100-3 Table 5.
1228 STAGE(hlg_ootf_scale, const void*) {
1229 // Compute Y in the XYZD50 primaries.
1230 F Y = compute_Y_in_xyzd50(r, g, b);
1231
1232 // Apply the gamma of 1.2.
1233 const float gamma_minus_1 = 0.2f;
1234 U32 sign;
1235 Y = strip_sign(Y, &sign);
1236 F Y_to_gamma_minus1 = apply_sign(approx_pow(Y, gamma_minus_1), sign);
1237 r = r * Y_to_gamma_minus1;
1238 g = g * Y_to_gamma_minus1;
1239 b = b * Y_to_gamma_minus1;
1240
1241 // Scale to the reference peak white (1000 nits) to get display luminance. Then divide by the
1242 // HDR reference white (203 nits), to get a value in relative linear color space.
1243 r *= 1000.0f / 203.0f;
1244 g *= 1000.0f / 203.0f;
1245 b *= 1000.0f / 203.0f;
1246 }
1247
1248 STAGE(hlginv_r, const skcms_TransferFunction* tf) { r = apply_hlginv(tf, r); }
1249 STAGE(hlginv_g, const skcms_TransferFunction* tf) { g = apply_hlginv(tf, g); }
1250 STAGE(hlginv_b, const skcms_TransferFunction* tf) { b = apply_hlginv(tf, b); }
1251 STAGE(hlginv_a, const skcms_TransferFunction* tf) { a = apply_hlginv(tf, a); }
1252
1253 STAGE(hlginv_rgb, const skcms_TransferFunction* tf) {
1254 r = apply_hlginv(tf, r);
1255 g = apply_hlginv(tf, g);
1256 b = apply_hlginv(tf, b);
1257 }
1258
1259 // Perform the inverse of the operation in hlg_ootf_scale.
1260 STAGE(hlginv_ootf_scale, const void*) {
1261 r *= (203.f / 1000.0f);
1262 g *= (203.f / 1000.0f);
1263 b *= (203.f / 1000.0f);
1264
1265 const float gamma_inv_minus_1 = 1.0f / 1.2f - 1.0f;
1266 F Y = compute_Y_in_xyzd50(r, g, b);
1267 U32 sign;
1268 Y = strip_sign(Y, &sign);
1269 F Y_to_gamma_minus1 = apply_sign(approx_pow(Y, gamma_inv_minus_1), sign);
1270
1271 r = r * Y_to_gamma_minus1;
1272 g = g * Y_to_gamma_minus1;
1273 b = b * Y_to_gamma_minus1;
1274 }
1275
1276 STAGE(table_r, const skcms_Curve* curve) { r = table(curve, r); }
1277 STAGE(table_g, const skcms_Curve* curve) { g = table(curve, g); }
1278 STAGE(table_b, const skcms_Curve* curve) { b = table(curve, b); }
1279 STAGE(table_a, const skcms_Curve* curve) { a = table(curve, a); }
1280
1281 STAGE(clut_A2B, const skcms_A2B* a2b) {
1282 clut(a2b, &r,&g,&b,a);
1283
1284 if (a2b->input_channels == 4) {
1285 // CMYK is opaque.
1286 a = F1;
1287 }
1288 }
1289
1290 STAGE(clut_B2A, const skcms_B2A* b2a) {
1291 clut(b2a, &r,&g,&b,&a);
1292 }
1293
1294 // From here on down, the store_ ops are all "final stages," terminating processing of this group.
1295
1296 FINAL_STAGE(store_a8, NoCtx) {
1297 store(dst + 1*i, cast<U8>(to_fixed(a * 255)));
1298 }
1299
1300 FINAL_STAGE(store_g8, NoCtx) {
1301 // g should be holding luminance (Y) (r,g,b ~~~> X,Y,Z)
1302 store(dst + 1*i, cast<U8>(to_fixed(g * 255)));
1303 }
1304
1305 FINAL_STAGE(store_ga88, NoCtx) {
1306 // g should be holding luminance (Y) (r,g,b ~~~> X,Y,Z)
1307 store<U16>(dst + 2*i, cast<U16>(to_fixed(g * 255) << 0 )
1308 | cast<U16>(to_fixed(a * 255) << 8 ));
1309 }
1310
1311 FINAL_STAGE(store_4444, NoCtx) {
1312 store<U16>(dst + 2*i, cast<U16>(to_fixed(r * 15) << 12)
1313 | cast<U16>(to_fixed(g * 15) << 8)
1314 | cast<U16>(to_fixed(b * 15) << 4)
1315 | cast<U16>(to_fixed(a * 15) << 0));
1316 }
1317
1318 FINAL_STAGE(store_565, NoCtx) {
1319 store<U16>(dst + 2*i, cast<U16>(to_fixed(r * 31) << 0 )
1320 | cast<U16>(to_fixed(g * 63) << 5 )
1321 | cast<U16>(to_fixed(b * 31) << 11 ));
1322 }
1323
1324 FINAL_STAGE(store_888, NoCtx) {
1325 uint8_t* rgb = (uint8_t*)dst + 3*i;
1326 #if defined(USING_NEON)
1327 // Same deal as load_888 but in reverse... we'll store using uint8x8x3_t, but
1328 // get there via U16 to save some instructions converting to float. And just
1329 // like load_888, we'd prefer to go via U32 but for ARMv7 support.
1330 U16 R = cast<U16>(to_fixed(r * 255)),
1331 G = cast<U16>(to_fixed(g * 255)),
1332 B = cast<U16>(to_fixed(b * 255));
1333
1334 uint8x8x3_t v = {{ (uint8x8_t)R, (uint8x8_t)G, (uint8x8_t)B }};
1335 vst3_lane_u8(rgb+0, v, 0);
1336 vst3_lane_u8(rgb+3, v, 2);
1337 vst3_lane_u8(rgb+6, v, 4);
1338 vst3_lane_u8(rgb+9, v, 6);
1339 #else
1340 store_3(rgb+0, cast<U8>(to_fixed(r * 255)) );
1341 store_3(rgb+1, cast<U8>(to_fixed(g * 255)) );
1342 store_3(rgb+2, cast<U8>(to_fixed(b * 255)) );
1343 #endif
1344 }
1345
1346 FINAL_STAGE(store_8888, NoCtx) {
1347 store(dst + 4*i, cast<U32>(to_fixed(r * 255)) << 0
1348 | cast<U32>(to_fixed(g * 255)) << 8
1349 | cast<U32>(to_fixed(b * 255)) << 16
1350 | cast<U32>(to_fixed(a * 255)) << 24);
1351 }
1352
1353 FINAL_STAGE(store_101010x_XR, NoCtx) {
1354 store(dst + 4*i, cast<U32>(to_fixed((r * 510) + 384)) << 0
1355 | cast<U32>(to_fixed((g * 510) + 384)) << 10
1356 | cast<U32>(to_fixed((b * 510) + 384)) << 20);
1357 }
1358
1359 FINAL_STAGE(store_10101010_XR, NoCtx) {
1360 // Each channel is 16 bits, where the 6 low bits are padding.
1361 store(dst + 8*i, cast<U64>(to_fixed((r * 510) + 384)) << ( 0+6)
1362 | cast<U64>(to_fixed((g * 510) + 384)) << (16+6)
1363 | cast<U64>(to_fixed((b * 510) + 384)) << (32+6)
1364 | cast<U64>(to_fixed((a * 510) + 384)) << (48+6));
1365 }
1366
1367 FINAL_STAGE(store_1010102, NoCtx) {
1368 store(dst + 4*i, cast<U32>(to_fixed(r * 1023)) << 0
1369 | cast<U32>(to_fixed(g * 1023)) << 10
1370 | cast<U32>(to_fixed(b * 1023)) << 20
1371 | cast<U32>(to_fixed(a * 3)) << 30);
1372 }
1373
1374 FINAL_STAGE(store_161616LE, NoCtx) {
1375 uintptr_t ptr = (uintptr_t)(dst + 6*i);
1376 assert( (ptr & 1) == 0 ); // The dst pointer must be 2-byte aligned
1377 uint16_t* rgb = (uint16_t*)ptr; // for this cast to uint16_t* to be safe.
1378 #if defined(USING_NEON)
1379 uint16x4x3_t v = {{
1380 (uint16x4_t)U16_from_F(r),
1381 (uint16x4_t)U16_from_F(g),
1382 (uint16x4_t)U16_from_F(b),
1383 }};
1384 vst3_u16(rgb, v);
1385 #else
1386 store_3(rgb+0, U16_from_F(r));
1387 store_3(rgb+1, U16_from_F(g));
1388 store_3(rgb+2, U16_from_F(b));
1389 #endif
1390
1391 }
1392
1393 FINAL_STAGE(store_16161616LE, NoCtx) {
1394 uintptr_t ptr = (uintptr_t)(dst + 8*i);
1395 assert( (ptr & 1) == 0 ); // The dst pointer must be 2-byte aligned
1396 uint16_t* rgba = (uint16_t*)ptr; // for this cast to uint16_t* to be safe.
1397 #if defined(USING_NEON)
1398 uint16x4x4_t v = {{
1399 (uint16x4_t)U16_from_F(r),
1400 (uint16x4_t)U16_from_F(g),
1401 (uint16x4_t)U16_from_F(b),
1402 (uint16x4_t)U16_from_F(a),
1403 }};
1404 vst4_u16(rgba, v);
1405 #else
1406 U64 px = cast<U64>(to_fixed(r * 65535)) << 0
1407 | cast<U64>(to_fixed(g * 65535)) << 16
1408 | cast<U64>(to_fixed(b * 65535)) << 32
1409 | cast<U64>(to_fixed(a * 65535)) << 48;
1410 store(rgba, px);
1411 #endif
1412 }
1413
1414 FINAL_STAGE(store_161616BE, NoCtx) {
1415 uintptr_t ptr = (uintptr_t)(dst + 6*i);
1416 assert( (ptr & 1) == 0 ); // The dst pointer must be 2-byte aligned
1417 uint16_t* rgb = (uint16_t*)ptr; // for this cast to uint16_t* to be safe.
1418 #if defined(USING_NEON)
1419 uint16x4x3_t v = {{
1420 (uint16x4_t)swap_endian_16(cast<U16>(U16_from_F(r))),
1421 (uint16x4_t)swap_endian_16(cast<U16>(U16_from_F(g))),
1422 (uint16x4_t)swap_endian_16(cast<U16>(U16_from_F(b))),
1423 }};
1424 vst3_u16(rgb, v);
1425 #else
1426 U32 R = to_fixed(r * 65535),
1427 G = to_fixed(g * 65535),
1428 B = to_fixed(b * 65535);
1429 store_3(rgb+0, cast<U16>((R & 0x00ff) << 8 | (R & 0xff00) >> 8) );
1430 store_3(rgb+1, cast<U16>((G & 0x00ff) << 8 | (G & 0xff00) >> 8) );
1431 store_3(rgb+2, cast<U16>((B & 0x00ff) << 8 | (B & 0xff00) >> 8) );
1432 #endif
1433
1434 }
1435
1436 FINAL_STAGE(store_16161616BE, NoCtx) {
1437 uintptr_t ptr = (uintptr_t)(dst + 8*i);
1438 assert( (ptr & 1) == 0 ); // The dst pointer must be 2-byte aligned
1439 uint16_t* rgba = (uint16_t*)ptr; // for this cast to uint16_t* to be safe.
1440 #if defined(USING_NEON)
1441 uint16x4x4_t v = {{
1442 (uint16x4_t)swap_endian_16(cast<U16>(U16_from_F(r))),
1443 (uint16x4_t)swap_endian_16(cast<U16>(U16_from_F(g))),
1444 (uint16x4_t)swap_endian_16(cast<U16>(U16_from_F(b))),
1445 (uint16x4_t)swap_endian_16(cast<U16>(U16_from_F(a))),
1446 }};
1447 vst4_u16(rgba, v);
1448 #else
1449 U64 px = cast<U64>(to_fixed(r * 65535)) << 0
1450 | cast<U64>(to_fixed(g * 65535)) << 16
1451 | cast<U64>(to_fixed(b * 65535)) << 32
1452 | cast<U64>(to_fixed(a * 65535)) << 48;
1453 store(rgba, swap_endian_16x4(px));
1454 #endif
1455 }
1456
1457 FINAL_STAGE(store_hhh, NoCtx) {
1458 uintptr_t ptr = (uintptr_t)(dst + 6*i);
1459 assert( (ptr & 1) == 0 ); // The dst pointer must be 2-byte aligned
1460 uint16_t* rgb = (uint16_t*)ptr; // for this cast to uint16_t* to be safe.
1461
1462 U16 R = Half_from_F(r),
1463 G = Half_from_F(g),
1464 B = Half_from_F(b);
1465 #if defined(USING_NEON)
1466 uint16x4x3_t v = {{
1467 (uint16x4_t)R,
1468 (uint16x4_t)G,
1469 (uint16x4_t)B,
1470 }};
1471 vst3_u16(rgb, v);
1472 #else
1473 store_3(rgb+0, R);
1474 store_3(rgb+1, G);
1475 store_3(rgb+2, B);
1476 #endif
1477 }
1478
1479 FINAL_STAGE(store_hhhh, NoCtx) {
1480 uintptr_t ptr = (uintptr_t)(dst + 8*i);
1481 assert( (ptr & 1) == 0 ); // The dst pointer must be 2-byte aligned
1482 uint16_t* rgba = (uint16_t*)ptr; // for this cast to uint16_t* to be safe.
1483
1484 U16 R = Half_from_F(r),
1485 G = Half_from_F(g),
1486 B = Half_from_F(b),
1487 A = Half_from_F(a);
1488 #if defined(USING_NEON)
1489 uint16x4x4_t v = {{
1490 (uint16x4_t)R,
1491 (uint16x4_t)G,
1492 (uint16x4_t)B,
1493 (uint16x4_t)A,
1494 }};
1495 vst4_u16(rgba, v);
1496 #else
1497 store(rgba, cast<U64>(R) << 0
1498 | cast<U64>(G) << 16
1499 | cast<U64>(B) << 32
1500 | cast<U64>(A) << 48);
1501 #endif
1502 }
1503
1504 FINAL_STAGE(store_fff, NoCtx) {
1505 uintptr_t ptr = (uintptr_t)(dst + 12*i);
1506 assert( (ptr & 3) == 0 ); // The dst pointer must be 4-byte aligned
1507 float* rgb = (float*)ptr; // for this cast to float* to be safe.
1508 #if defined(USING_NEON)
1509 float32x4x3_t v = {{
1510 (float32x4_t)r,
1511 (float32x4_t)g,
1512 (float32x4_t)b,
1513 }};
1514 vst3q_f32(rgb, v);
1515 #else
1516 store_3(rgb+0, r);
1517 store_3(rgb+1, g);
1518 store_3(rgb+2, b);
1519 #endif
1520 }
1521
1522 FINAL_STAGE(store_ffff, NoCtx) {
1523 uintptr_t ptr = (uintptr_t)(dst + 16*i);
1524 assert( (ptr & 3) == 0 ); // The dst pointer must be 4-byte aligned
1525 float* rgba = (float*)ptr; // for this cast to float* to be safe.
1526 #if defined(USING_NEON)
1527 float32x4x4_t v = {{
1528 (float32x4_t)r,
1529 (float32x4_t)g,
1530 (float32x4_t)b,
1531 (float32x4_t)a,
1532 }};
1533 vst4q_f32(rgba, v);
1534 #else
1535 store_4(rgba+0, r);
1536 store_4(rgba+1, g);
1537 store_4(rgba+2, b);
1538 store_4(rgba+3, a);
1539 #endif
1540 }
1541
1542 #if SKCMS_HAS_MUSTTAIL
1543
1544 SI void exec_stages(StageFn* stages, const void** contexts, const char* src, char* dst, int i) {
1545 (*stages)({stages}, contexts, src, dst, F0, F0, F0, F1, i);
1546 }
1547
1548 #else
1549
1550 static void exec_stages(const Op* ops, const void** contexts,
1551 const char* src, char* dst, int i) {
1552 F r = F0, g = F0, b = F0, a = F1;
1553 while (true) {
1554 switch (*ops++) {
1555 #define M(name) case Op::name: Exec_##name(*contexts++, src, dst, r, g, b, a, i); break;
1556 SKCMS_WORK_OPS(M)
1557 #undef M
1558 #define M(name) case Op::name: Exec_##name(*contexts++, src, dst, r, g, b, a, i); return;
1559 SKCMS_STORE_OPS(M)
1560 #undef M
1561 }
1562 }
1563 }
1564
1565 #endif
1566
1567 // NOLINTNEXTLINE(misc-definitions-in-headers)
1568 void run_program(const Op* program, const void** contexts, SKCMS_MAYBE_UNUSED ptrdiff_t programSize,
1569 const char* src, char* dst, int n,
1570 size_t src_bpp, size_t dst_bpp) {
1571 #if SKCMS_HAS_MUSTTAIL
1572 // Convert the program into an array of tailcall stages.
1573 StageFn stages[32];
1574 assert(programSize <= ARRAY_COUNT(stages));
1575
1576 static constexpr StageFn kStageFns[] = {
1577 #define M(name) &Exec_##name,
1578 SKCMS_WORK_OPS(M)
1579 SKCMS_STORE_OPS(M)
1580 #undef M
1581 };
1582
1583 for (ptrdiff_t index = 0; index < programSize; ++index) {
1584 stages[index] = kStageFns[(int)program[index]];
1585 }
1586 #else
1587 // Use the op array as-is.
1588 const Op* stages = program;
1589 #endif
1590
1591 int i = 0;
1592 while (n >= N) {
1593 exec_stages(stages, contexts, src, dst, i);
1594 i += N;
1595 n -= N;
1596 }
1597 if (n > 0) {
1598 char tmp[4*4*N] = {0};
1599
1600 memcpy(tmp, (const char*)src + (size_t)i*src_bpp, (size_t)n*src_bpp);
1601 exec_stages(stages, contexts, tmp, tmp, 0);
1602 memcpy((char*)dst + (size_t)i*dst_bpp, tmp, (size_t)n*dst_bpp);
1603 }
1604 }