Coverage Report

Created: 2026-07-25 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/astc-encoder/Source/astcenc_decompress_symbolic.cpp
Line
Count
Source
1
// SPDX-License-Identifier: Apache-2.0
2
// ----------------------------------------------------------------------------
3
// Copyright 2011-2025 Arm Limited
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
// use this file except in compliance with the License. You may obtain a copy
7
// of the License at:
8
//
9
//     http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
// License for the specific language governing permissions and limitations
15
// under the License.
16
// ----------------------------------------------------------------------------
17
18
/**
19
 * @brief Functions to decompress a symbolic block.
20
 */
21
22
#include "astcenc_internal.h"
23
24
#include <stdio.h>
25
#include <assert.h>
26
27
/**
28
 * @brief Compute the integer linear interpolation of two color endpoints.
29
 *
30
 * @param u8_mask       The mask for lanes using decode_unorm8 rather than decode_f16.
31
 * @param color0        The endpoint0 color.
32
 * @param color1        The endpoint1 color.
33
 * @param weights       The interpolation weight (between 0 and 64).
34
 *
35
 * @return The interpolated color.
36
 */
37
static vint4 lerp_color_int(
38
  vmask4 u8_mask,
39
  vint4 color0,
40
  vint4 color1,
41
  vint4 weights
42
1.48M
) {
43
1.48M
  vint4 weight1 = weights;
44
1.48M
  vint4 weight0 = vint4(64) - weight1;
45
46
1.48M
  vint4 color = (color0 * weight0) + (color1 * weight1) + vint4(32);
47
1.48M
  color = asr<6>(color);
48
49
  // For decode_unorm8 values force the codec to bit replicate. This allows the
50
  // rest of the codec to assume the full 0xFFFF range for everything and ignore
51
  // the decode_mode setting
52
1.48M
  vint4 color_u8 = asr<8>(color) * vint4(257);
53
1.48M
  color = select(color, color_u8, u8_mask);
54
55
1.48M
  return color;
56
1.48M
}
57
58
/**
59
 * @brief Convert integer color value into a float value for the decoder.
60
 *
61
 * @param data       The integer color value post-interpolation.
62
 * @param lns_mask   If set treat lane as HDR (LNS) else LDR (unorm16).
63
 *
64
 * @return The float color value.
65
 */
66
static inline vfloat4 decode_texel(
67
  vint4 data,
68
  vmask4 lns_mask
69
192k
) {
70
192k
  vint4 color_lns = vint4::zero();
71
192k
  vint4 color_unorm = vint4::zero();
72
73
192k
  if (any(lns_mask))
74
40.2k
  {
75
40.2k
    color_lns = lns_to_sf16(data);
76
40.2k
  }
77
78
192k
  if (!all(lns_mask))
79
168k
  {
80
168k
    color_unorm = unorm16_to_sf16(data);
81
168k
  }
82
83
  // Pick components and then convert to FP16
84
192k
  vint4 datai = select(color_unorm, color_lns, lns_mask);
85
192k
  return float16_to_float(datai);
86
192k
}
87
88
/* See header for documentation. */
89
void unpack_weights(
90
  const block_size_descriptor& bsd,
91
  const symbolic_compressed_block& scb,
92
  const decimation_info& di,
93
  bool is_dual_plane,
94
  int weights_plane1[BLOCK_MAX_TEXELS],
95
  int weights_plane2[BLOCK_MAX_TEXELS]
96
84.4k
) {
97
  // Safe to overshoot as all arrays are allocated to full size
98
84.4k
  if (!is_dual_plane)
99
55.8k
  {
100
    // Build full 64-entry weight lookup table
101
55.8k
    vtable_64x8 table;
102
55.8k
    vtable_prepare(table, scb.weights);
103
104
432k
    for (unsigned int i = 0; i < bsd.texel_count; i += ASTCENC_SIMD_WIDTH)
105
376k
    {
106
376k
      vint summed_value(8);
107
376k
      vint weight_count(di.texel_weight_count + i);
108
376k
      int max_weight_count = hmax_s(weight_count);
109
110
376k
      promise(max_weight_count > 0);
111
1.01M
      for (int j = 0; j < max_weight_count; j++)
112
638k
      {
113
638k
        vint texel_weights(di.texel_weights_tr[j] + i);
114
638k
        vint texel_weights_int(di.texel_weight_contribs_int_tr[j] + i);
115
116
638k
        summed_value += vtable_lookup_32bit(table, texel_weights) * texel_weights_int;
117
638k
      }
118
119
376k
      store(lsr<4>(summed_value), weights_plane1 + i);
120
376k
    }
121
55.8k
  }
122
28.5k
  else
123
28.5k
  {
124
    // Build a 32-entry weight lookup table per plane
125
    // Plane 1
126
28.5k
    vtable_32x8 tab_plane1;
127
28.5k
    vtable_prepare(tab_plane1, scb.weights);
128
129
    // Plane 2
130
28.5k
    vtable_32x8 tab_plane2;
131
28.5k
    vtable_prepare(tab_plane2, scb.weights + 32);
132
133
215k
    for (unsigned int i = 0; i < bsd.texel_count; i += ASTCENC_SIMD_WIDTH)
134
186k
    {
135
186k
      vint sum_plane1(8);
136
186k
      vint sum_plane2(8);
137
138
186k
      vint weight_count(di.texel_weight_count + i);
139
186k
      int max_weight_count = hmax_s(weight_count);
140
141
186k
      promise(max_weight_count > 0);
142
614k
      for (int j = 0; j < max_weight_count; j++)
143
427k
      {
144
427k
        vint texel_weights(di.texel_weights_tr[j] + i);
145
427k
        vint texel_weights_int(di.texel_weight_contribs_int_tr[j] + i);
146
147
427k
        sum_plane1 += vtable_lookup_32bit(tab_plane1, texel_weights) * texel_weights_int;
148
427k
        sum_plane2 += vtable_lookup_32bit(tab_plane2, texel_weights) * texel_weights_int;
149
427k
      }
150
151
186k
      store(lsr<4>(sum_plane1), weights_plane1 + i);
152
186k
      store(lsr<4>(sum_plane2), weights_plane2 + i);
153
186k
    }
154
28.5k
  }
155
84.4k
}
156
157
/**
158
 * @brief Return an FP32 NaN value for use in error colors.
159
 *
160
 * This NaN encoding will turn into 0xFFFF when converted to an FP16 NaN.
161
 *
162
 * @return The float color value.
163
 */
164
static float error_color_nan()
165
271k
{
166
271k
  return astc::uint_as_float(0xFFFFE000u);
167
271k
}
168
169
/* See header for documentation. */
170
void decompress_symbolic_block(
171
  astcenc_profile decode_mode,
172
  const block_size_descriptor& bsd,
173
  size_t pos_x,
174
  size_t pos_y,
175
  size_t pos_z,
176
  const symbolic_compressed_block& scb,
177
  image_block& blk
178
3.84k
) {
179
3.84k
  blk.pos_x = pos_x;
180
3.84k
  blk.pos_y = pos_y;
181
3.84k
  blk.pos_z = pos_z;
182
183
3.84k
  blk.data_min = vfloat4::zero();
184
3.84k
  blk.data_mean = vfloat4::zero();
185
3.84k
  blk.data_max = vfloat4::zero();
186
3.84k
  blk.grayscale = false;
187
188
  // If we detected an error-block, blow up immediately.
189
3.84k
  if (scb.block_type == SYM_BTYPE_ERROR)
190
1.00k
  {
191
68.7k
    for (unsigned int i = 0; i < bsd.texel_count; i++)
192
67.7k
    {
193
67.7k
      blk.data_r[i] = error_color_nan();
194
67.7k
      blk.data_g[i] = error_color_nan();
195
67.7k
      blk.data_b[i] = error_color_nan();
196
67.7k
      blk.data_a[i] = error_color_nan();
197
67.7k
      blk.rgb_lns[i] = 0;
198
67.7k
      blk.alpha_lns[i] = 0;
199
67.7k
    }
200
201
1.00k
    return;
202
1.00k
  }
203
204
2.84k
  if ((scb.block_type == SYM_BTYPE_CONST_F16) ||
205
2.73k
      (scb.block_type == SYM_BTYPE_CONST_U16))
206
345
  {
207
345
    vfloat4 color;
208
345
    uint8_t use_lns = 0;
209
210
    // UNORM16 constant color block
211
345
    if (scb.block_type == SYM_BTYPE_CONST_U16)
212
237
    {
213
237
      vint4 colori(scb.constant_color);
214
215
      // Determine the UNORM8 rounding on the decode
216
237
      vmask4 u8_mask = get_u8_component_mask(decode_mode, blk);
217
218
      // The real decoder would just use the top 8 bits, but we rescale
219
      // into a 16-bit value that rounds correctly.
220
237
      vint4 colori_u8 = asr<8>(colori) * 257;
221
237
      colori = select(colori, colori_u8, u8_mask);
222
223
237
      vint4 colorf16 = unorm16_to_sf16(colori);
224
237
      color = float16_to_float(colorf16);
225
237
    }
226
    // FLOAT16 constant color block
227
108
    else
228
108
    {
229
108
      switch (decode_mode)
230
108
      {
231
31
      case ASTCENC_PRF_LDR_SRGB:
232
33
      case ASTCENC_PRF_LDR:
233
33
        color = vfloat4(error_color_nan());
234
33
        break;
235
12
      case ASTCENC_PRF_HDR_RGB_LDR_A:
236
75
      case ASTCENC_PRF_HDR:
237
        // Constant-color block; unpack from FP16 to FP32.
238
75
        color = float16_to_float(vint4(scb.constant_color));
239
75
        use_lns = 1;
240
75
        break;
241
108
      }
242
108
    }
243
244
21.5k
    for (unsigned int i = 0; i < bsd.texel_count; i++)
245
21.1k
    {
246
21.1k
      blk.data_r[i] = color.lane<0>();
247
21.1k
      blk.data_g[i] = color.lane<1>();
248
21.1k
      blk.data_b[i] = color.lane<2>();
249
21.1k
      blk.data_a[i] = color.lane<3>();
250
21.1k
      blk.rgb_lns[i] = use_lns;
251
21.1k
      blk.alpha_lns[i] = use_lns;
252
21.1k
    }
253
254
345
    return;
255
345
  }
256
257
  // Get the appropriate partition-table entry
258
2.49k
  int partition_count = scb.partition_count;
259
2.49k
  const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index);
260
261
  // Get the appropriate block descriptors
262
2.49k
  const auto& bm = bsd.get_block_mode(scb.block_mode);
263
2.49k
  const auto& di = bsd.get_decimation_info(bm.decimation_mode);
264
265
2.49k
  bool is_dual_plane = static_cast<bool>(bm.is_dual_plane);
266
267
  // Unquantize and undecimate the weights
268
2.49k
  int plane1_weights[BLOCK_MAX_TEXELS];
269
2.49k
  int plane2_weights[BLOCK_MAX_TEXELS];
270
2.49k
  unpack_weights(bsd, scb, di, is_dual_plane, plane1_weights, plane2_weights);
271
272
  // Now that we have endpoint colors and weights, we can unpack texel colors
273
2.49k
  int plane2_component = scb.plane2_component;
274
2.49k
  vmask4 plane2_mask = vint4::lane_id() == vint4(plane2_component);
275
276
2.49k
  vmask4 u8_mask = get_u8_component_mask(decode_mode, blk);
277
278
9.37k
  for (int i = 0; i < partition_count; i++)
279
6.87k
  {
280
    // Decode the color endpoints for this partition
281
6.87k
    vint4 ep0;
282
6.87k
    vint4 ep1;
283
6.87k
    bool rgb_lns;
284
6.87k
    bool a_lns;
285
286
6.87k
    unpack_color_endpoints(decode_mode,
287
6.87k
                           scb.color_formats[i],
288
6.87k
                           scb.color_values[i],
289
6.87k
                           rgb_lns, a_lns,
290
6.87k
                           ep0, ep1);
291
292
6.87k
    vmask4 lns_mask(rgb_lns, rgb_lns, rgb_lns, a_lns);
293
294
6.87k
    int texel_count = pi.partition_texel_count[i];
295
199k
    for (int j = 0; j < texel_count; j++)
296
192k
    {
297
192k
      int tix = pi.texels_of_partition[i][j];
298
192k
      vint4 weight = select(vint4(plane1_weights[tix]), vint4(plane2_weights[tix]), plane2_mask);
299
192k
      vint4 color = lerp_color_int(u8_mask, ep0, ep1, weight);
300
192k
      vfloat4 colorf = decode_texel(color, lns_mask);
301
302
192k
      blk.data_r[tix] = colorf.lane<0>();
303
192k
      blk.data_g[tix] = colorf.lane<1>();
304
192k
      blk.data_b[tix] = colorf.lane<2>();
305
192k
      blk.data_a[tix] = colorf.lane<3>();
306
192k
    }
307
6.87k
  }
308
2.49k
}
309
310
#if !defined(ASTCENC_DECOMPRESS_ONLY)
311
312
/* See header for documentation. */
313
float compute_symbolic_block_difference_2plane(
314
  const astcenc_config& config,
315
  const block_size_descriptor& bsd,
316
  const symbolic_compressed_block& scb,
317
  const image_block& blk
318
32.4k
) {
319
  // If we detected an error-block, blow up immediately.
320
32.4k
  if (scb.block_type == SYM_BTYPE_ERROR)
321
4.53k
  {
322
4.53k
    return ERROR_CALC_DEFAULT;
323
4.53k
  }
324
325
32.4k
  assert(scb.block_mode >= 0);
326
27.9k
  assert(scb.partition_count == 1);
327
27.9k
  assert(bsd.get_block_mode(scb.block_mode).is_dual_plane == 1);
328
329
  // Get the appropriate block descriptor
330
27.9k
  const block_mode& bm = bsd.get_block_mode(scb.block_mode);
331
27.9k
  const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode);
332
333
  // Unquantize and undecimate the weights
334
27.9k
  int plane1_weights[BLOCK_MAX_TEXELS];
335
27.9k
  int plane2_weights[BLOCK_MAX_TEXELS];
336
27.9k
  unpack_weights(bsd, scb, di, true, plane1_weights, plane2_weights);
337
338
27.9k
  vmask4 plane2_mask = vint4::lane_id() == vint4(scb.plane2_component);
339
340
27.9k
  vfloat4 summa = vfloat4::zero();
341
342
  // Decode the color endpoints for this partition
343
27.9k
  vint4 ep0;
344
27.9k
  vint4 ep1;
345
27.9k
  bool rgb_lns;
346
27.9k
  bool a_lns;
347
348
27.9k
  unpack_color_endpoints(config.profile,
349
27.9k
                         scb.color_formats[0],
350
27.9k
                         scb.color_values[0],
351
27.9k
                         rgb_lns, a_lns,
352
27.9k
                         ep0, ep1);
353
354
27.9k
  vmask4 u8_mask = get_u8_component_mask(config.profile, blk);
355
356
  // Unpack and compute error for each texel in the partition
357
27.9k
  unsigned int texel_count = bsd.texel_count;
358
478k
  for (unsigned int i = 0; i < texel_count; i++)
359
461k
  {
360
461k
    vint4 weight = select(vint4(plane1_weights[i]), vint4(plane2_weights[i]), plane2_mask);
361
461k
    vint4 colori = lerp_color_int(u8_mask, ep0, ep1, weight);
362
363
461k
    vfloat4 color = int_to_float(colori);
364
461k
    vfloat4 oldColor = blk.texel(i);
365
366
    // Compare error using a perceptual decode metric for RGBM textures
367
461k
    if (config.flags & ASTCENC_FLG_MAP_RGBM)
368
85.5k
    {
369
      // Fail encodings that result in zero weight M pixels. Note that this can cause
370
      // "interesting" artifacts if we reject all useful encodings - we typically get max
371
      // brightness encodings instead which look just as bad. We recommend users apply a
372
      // bias to their stored M value, limiting the lower value to 16 or 32 to avoid
373
      // getting small M values post-quantization, but we can't prove it would never
374
      // happen, especially at low bit rates ...
375
85.5k
      if (color.lane<3>() == 0.0f)
376
11.2k
      {
377
11.2k
        return -ERROR_CALC_DEFAULT;
378
11.2k
      }
379
380
      // Compute error based on decoded RGBM color
381
74.3k
      color = vfloat4(
382
74.3k
        color.lane<0>() * color.lane<3>() * config.rgbm_m_scale,
383
74.3k
        color.lane<1>() * color.lane<3>() * config.rgbm_m_scale,
384
74.3k
        color.lane<2>() * color.lane<3>() * config.rgbm_m_scale,
385
74.3k
        1.0f
386
74.3k
      );
387
388
74.3k
      oldColor = vfloat4(
389
74.3k
        oldColor.lane<0>() * oldColor.lane<3>() * config.rgbm_m_scale,
390
74.3k
        oldColor.lane<1>() * oldColor.lane<3>() * config.rgbm_m_scale,
391
74.3k
        oldColor.lane<2>() * oldColor.lane<3>() * config.rgbm_m_scale,
392
74.3k
        1.0f
393
74.3k
      );
394
74.3k
    }
395
396
450k
    vfloat4 error = oldColor - color;
397
450k
    error = min(abs(error), 1e15f);
398
450k
    error = error * error;
399
400
450k
    summa += min(dot(error, blk.channel_weight), ERROR_CALC_DEFAULT);
401
450k
  }
402
403
16.7k
  return summa.lane<0>();
404
27.9k
}
405
406
/* See header for documentation. */
407
float compute_symbolic_block_difference_1plane(
408
  const astcenc_config& config,
409
  const block_size_descriptor& bsd,
410
  const symbolic_compressed_block& scb,
411
  const image_block& blk
412
48.1k
) {
413
48.1k
  assert(bsd.get_block_mode(scb.block_mode).is_dual_plane == 0);
414
415
  // If we detected an error-block, blow up immediately.
416
48.1k
  if (scb.block_type == SYM_BTYPE_ERROR)
417
4.65k
  {
418
4.65k
    return ERROR_CALC_DEFAULT;
419
4.65k
  }
420
421
48.1k
  assert(scb.block_mode >= 0);
422
423
  // Get the appropriate partition-table entry
424
43.4k
  unsigned int partition_count = scb.partition_count;
425
43.4k
  const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index);
426
427
  // Get the appropriate block descriptor
428
43.4k
  const block_mode& bm = bsd.get_block_mode(scb.block_mode);
429
43.4k
  const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode);
430
431
  // Unquantize and undecimate the weights
432
43.4k
  int plane1_weights[BLOCK_MAX_TEXELS];
433
43.4k
  unpack_weights(bsd, scb, di, false, plane1_weights, nullptr);
434
435
43.4k
  vmask4 u8_mask = get_u8_component_mask(config.profile, blk);
436
437
43.4k
  vfloat4 summa = vfloat4::zero();
438
129k
  for (unsigned int i = 0; i < partition_count; i++)
439
97.5k
  {
440
    // Decode the color endpoints for this partition
441
97.5k
    vint4 ep0;
442
97.5k
    vint4 ep1;
443
97.5k
    bool rgb_lns;
444
97.5k
    bool a_lns;
445
446
97.5k
    unpack_color_endpoints(config.profile,
447
97.5k
                           scb.color_formats[i],
448
97.5k
                           scb.color_values[i],
449
97.5k
                           rgb_lns, a_lns,
450
97.5k
                           ep0, ep1);
451
452
    // Unpack and compute error for each texel in the partition
453
97.5k
    unsigned int texel_count = pi.partition_texel_count[i];
454
911k
    for (unsigned int j = 0; j < texel_count; j++)
455
826k
    {
456
826k
      unsigned int tix = pi.texels_of_partition[i][j];
457
826k
      vint4 colori = lerp_color_int(u8_mask, ep0, ep1,
458
826k
                                    vint4(plane1_weights[tix]));
459
460
826k
      vfloat4 color = int_to_float(colori);
461
826k
      vfloat4 oldColor = blk.texel(tix);
462
463
      // Compare error using a perceptual decode metric for RGBM textures
464
826k
      if (config.flags & ASTCENC_FLG_MAP_RGBM)
465
287k
      {
466
        // Fail encodings that result in zero weight M pixels. Note that this can cause
467
        // "interesting" artifacts if we reject all useful encodings - we typically get max
468
        // brightness encodings instead which look just as bad. We recommend users apply a
469
        // bias to their stored M value, limiting the lower value to 16 or 32 to avoid
470
        // getting small M values post-quantization, but we can't prove it would never
471
        // happen, especially at low bit rates ...
472
287k
        if (color.lane<3>() == 0.0f)
473
11.9k
        {
474
11.9k
          return -ERROR_CALC_DEFAULT;
475
11.9k
        }
476
477
        // Compute error based on decoded RGBM color
478
275k
        color = vfloat4(
479
275k
          color.lane<0>() * color.lane<3>() * config.rgbm_m_scale,
480
275k
          color.lane<1>() * color.lane<3>() * config.rgbm_m_scale,
481
275k
          color.lane<2>() * color.lane<3>() * config.rgbm_m_scale,
482
275k
          1.0f
483
275k
        );
484
485
275k
        oldColor = vfloat4(
486
275k
          oldColor.lane<0>() * oldColor.lane<3>() * config.rgbm_m_scale,
487
275k
          oldColor.lane<1>() * oldColor.lane<3>() * config.rgbm_m_scale,
488
275k
          oldColor.lane<2>() * oldColor.lane<3>() * config.rgbm_m_scale,
489
275k
          1.0f
490
275k
        );
491
275k
      }
492
493
814k
      vfloat4 error = oldColor - color;
494
814k
      error = min(abs(error), 1e15f);
495
814k
      error = error * error;
496
497
814k
      summa += min(dot(error, blk.channel_weight), ERROR_CALC_DEFAULT);
498
814k
    }
499
97.5k
  }
500
501
31.5k
  return summa.lane<0>();
502
43.4k
}
503
504
/* See header for documentation. */
505
float compute_symbolic_block_difference_1plane_1partition(
506
  const astcenc_config& config,
507
  const block_size_descriptor& bsd,
508
  const symbolic_compressed_block& scb,
509
  const image_block& blk
510
10.5k
) {
511
  // If we detected an error-block, blow up immediately.
512
10.5k
  if (scb.block_type == SYM_BTYPE_ERROR)
513
0
  {
514
0
    return ERROR_CALC_DEFAULT;
515
0
  }
516
517
10.5k
  assert(scb.block_mode >= 0);
518
10.5k
  assert(bsd.get_partition_info(scb.partition_count, scb.partition_index).partition_count == 1);
519
520
  // Get the appropriate block descriptor
521
10.5k
  const block_mode& bm = bsd.get_block_mode(scb.block_mode);
522
10.5k
  const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode);
523
524
  // Unquantize and undecimate the weights
525
10.5k
  ASTCENC_ALIGNAS int plane1_weights[BLOCK_MAX_TEXELS];
526
10.5k
  unpack_weights(bsd, scb, di, false, plane1_weights, nullptr);
527
528
  // Decode the color endpoints for this partition
529
10.5k
  vint4 ep0;
530
10.5k
  vint4 ep1;
531
10.5k
  bool rgb_lns;
532
10.5k
  bool a_lns;
533
534
10.5k
  unpack_color_endpoints(config.profile,
535
10.5k
                         scb.color_formats[0],
536
10.5k
                         scb.color_values[0],
537
10.5k
                         rgb_lns, a_lns,
538
10.5k
                         ep0, ep1);
539
540
10.5k
  vmask4 u8_mask = get_u8_component_mask(config.profile, blk);
541
542
  // Unpack and compute error for each texel in the partition
543
10.5k
  vfloatacc summav = vfloatacc::zero();
544
545
10.5k
  vint lane_id = vint::lane_id();
546
547
10.5k
  unsigned int texel_count = bsd.texel_count;
548
88.4k
  for (unsigned int i = 0; i < texel_count; i += ASTCENC_SIMD_WIDTH)
549
77.9k
  {
550
    // Compute EP1 contribution
551
77.9k
    vint weight1 = vint::loada(plane1_weights + i);
552
77.9k
    vint ep1_r = vint(ep1.lane<0>()) * weight1;
553
77.9k
    vint ep1_g = vint(ep1.lane<1>()) * weight1;
554
77.9k
    vint ep1_b = vint(ep1.lane<2>()) * weight1;
555
77.9k
    vint ep1_a = vint(ep1.lane<3>()) * weight1;
556
557
    // Compute EP0 contribution
558
77.9k
    vint weight0 = vint(64) - weight1;
559
77.9k
    vint ep0_r = vint(ep0.lane<0>()) * weight0;
560
77.9k
    vint ep0_g = vint(ep0.lane<1>()) * weight0;
561
77.9k
    vint ep0_b = vint(ep0.lane<2>()) * weight0;
562
77.9k
    vint ep0_a = vint(ep0.lane<3>()) * weight0;
563
564
    // Combine contributions
565
77.9k
    vint colori_r = asr<6>(ep0_r + ep1_r + vint(32));
566
77.9k
    vint colori_g = asr<6>(ep0_g + ep1_g + vint(32));
567
77.9k
    vint colori_b = asr<6>(ep0_b + ep1_b + vint(32));
568
77.9k
    vint colori_a = asr<6>(ep0_a + ep1_a + vint(32));
569
570
    // If using a U8 decode mode bit replicate top 8 bits
571
    // so rest of codec can assume 0xFFFF max range everywhere
572
77.9k
    vint colori_r8 = asr<8>(colori_r) * vint(257);
573
77.9k
    colori_r = select(colori_r, colori_r8, vmask(u8_mask.lane<0>()));
574
575
77.9k
    vint colori_g8 = asr<8>(colori_g) * vint(257);
576
77.9k
    colori_g = select(colori_g, colori_g8, vmask(u8_mask.lane<1>()));
577
578
77.9k
    vint colori_b8 = asr<8>(colori_b) * vint(257);
579
77.9k
    colori_b = select(colori_b, colori_b8, vmask(u8_mask.lane<2>()));
580
581
77.9k
    vint colori_a8 = asr<8>(colori_a) * vint(257);
582
77.9k
    colori_a = select(colori_a, colori_a8, vmask(u8_mask.lane<3>()));
583
584
    // Compute color diff
585
77.9k
    vfloat color_r = int_to_float(colori_r);
586
77.9k
    vfloat color_g = int_to_float(colori_g);
587
77.9k
    vfloat color_b = int_to_float(colori_b);
588
77.9k
    vfloat color_a = int_to_float(colori_a);
589
590
77.9k
    vfloat color_orig_r = loada(blk.data_r + i);
591
77.9k
    vfloat color_orig_g = loada(blk.data_g + i);
592
77.9k
    vfloat color_orig_b = loada(blk.data_b + i);
593
77.9k
    vfloat color_orig_a = loada(blk.data_a + i);
594
595
77.9k
    vfloat color_error_r = min(abs(color_orig_r - color_r), vfloat(1e15f));
596
77.9k
    vfloat color_error_g = min(abs(color_orig_g - color_g), vfloat(1e15f));
597
77.9k
    vfloat color_error_b = min(abs(color_orig_b - color_b), vfloat(1e15f));
598
77.9k
    vfloat color_error_a = min(abs(color_orig_a - color_a), vfloat(1e15f));
599
600
    // Compute squared error metric
601
77.9k
    color_error_r = color_error_r * color_error_r;
602
77.9k
    color_error_g = color_error_g * color_error_g;
603
77.9k
    color_error_b = color_error_b * color_error_b;
604
77.9k
    color_error_a = color_error_a * color_error_a;
605
606
77.9k
    vfloat metric = color_error_r * blk.channel_weight.lane<0>()
607
77.9k
                  + color_error_g * blk.channel_weight.lane<1>()
608
77.9k
                  + color_error_b * blk.channel_weight.lane<2>()
609
77.9k
                  + color_error_a * blk.channel_weight.lane<3>();
610
611
    // Mask off bad lanes
612
77.9k
    vmask mask = lane_id < vint(texel_count);
613
77.9k
    lane_id += vint(ASTCENC_SIMD_WIDTH);
614
77.9k
    haccumulate(summav, metric, mask);
615
77.9k
  }
616
617
10.5k
  return hadd_s(summav);
618
10.5k
}
619
620
#endif