Skip to content

Commit 5774306

Browse files
committed
Make more use of unsigned ints
1 parent af59d8a commit 5774306

11 files changed

Lines changed: 156 additions & 155 deletions

Source/astcenc_block_sizes.cpp

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,11 @@ static bool decode_block_mode_2d(
124124
}
125125
}
126126

127-
int weight_count = x_weights * y_weights * (D + 1);
127+
unsigned int weight_count = x_weights * y_weights * (D + 1);
128128
quant_mode = (base_quant_mode - 2) + 6 * H;
129129
is_dual_plane = D != 0;
130130

131-
int weight_bits = get_ise_sequence_bitcount(weight_count, (quant_method)quant_mode);
131+
unsigned int weight_bits = get_ise_sequence_bitcount(weight_count, (quant_method)quant_mode);
132132
return (weight_count <= MAX_WEIGHTS_PER_BLOCK &&
133133
weight_bits >= MIN_WEIGHT_BITS_PER_BLOCK &&
134134
weight_bits <= MAX_WEIGHT_BITS_PER_BLOCK);
@@ -225,11 +225,11 @@ static int decode_block_mode_3d(
225225
}
226226
}
227227

228-
int weight_count = x_weights * y_weights * z_weights * (D + 1);
228+
unsigned int weight_count = x_weights * y_weights * z_weights * (D + 1);
229229
quant_mode = (base_quant_mode - 2) + 6 * H;
230230
is_dual_plane = D != 0;
231231

232-
int weight_bits = get_ise_sequence_bitcount(weight_count, (quant_method)quant_mode);
232+
unsigned int weight_bits = get_ise_sequence_bitcount(weight_count, (quant_method)quant_mode);
233233
return (weight_count <= MAX_WEIGHTS_PER_BLOCK &&
234234
weight_bits >= MIN_WEIGHT_BITS_PER_BLOCK &&
235235
weight_bits <= MAX_WEIGHT_BITS_PER_BLOCK);
@@ -245,14 +245,14 @@ static int decode_block_mode_3d(
245245
* @param[out] di The decimation info structure to populate.
246246
*/
247247
static void init_decimation_info_2d(
248-
int x_texels,
249-
int y_texels,
250-
int x_weights,
251-
int y_weights,
248+
unsigned int x_texels,
249+
unsigned int y_texels,
250+
unsigned int x_weights,
251+
unsigned int y_weights,
252252
decimation_info& di
253253
) {
254-
int texels_per_block = x_texels * y_texels;
255-
int weights_per_block = x_weights * y_weights;
254+
unsigned int texels_per_block = x_texels * y_texels;
255+
unsigned int weights_per_block = x_weights * y_weights;
256256

257257
uint8_t weight_count_of_texel[MAX_TEXELS_PER_BLOCK];
258258
uint8_t grid_weights_of_texel[MAX_TEXELS_PER_BLOCK][4];
@@ -263,45 +263,46 @@ static void init_decimation_info_2d(
263263
uint8_t texels_of_weight[MAX_WEIGHTS_PER_BLOCK][MAX_TEXELS_PER_BLOCK];
264264
int texel_weights_of_weight[MAX_WEIGHTS_PER_BLOCK][MAX_TEXELS_PER_BLOCK];
265265

266-
for (int i = 0; i < weights_per_block; i++)
266+
for (unsigned int i = 0; i < weights_per_block; i++)
267267
{
268268
texel_count_of_weight[i] = 0;
269269
}
270270

271-
for (int i = 0; i < texels_per_block; i++)
271+
for (unsigned int i = 0; i < texels_per_block; i++)
272272
{
273273
weight_count_of_texel[i] = 0;
274274
}
275275

276-
for (int y = 0; y < y_texels; y++)
276+
for (unsigned int y = 0; y < y_texels; y++)
277277
{
278-
for (int x = 0; x < x_texels; x++)
278+
for (unsigned int x = 0; x < x_texels; x++)
279279
{
280-
int texel = y * x_texels + x;
280+
unsigned int texel = y * x_texels + x;
281+
282+
unsigned int x_weight = (((1024 + x_texels / 2) / (x_texels - 1)) * x * (x_weights - 1) + 32) >> 6;
283+
unsigned int y_weight = (((1024 + y_texels / 2) / (y_texels - 1)) * y * (y_weights - 1) + 32) >> 6;
281284

282-
int x_weight = (((1024 + x_texels / 2) / (x_texels - 1)) * x * (x_weights - 1) + 32) >> 6;
283-
int y_weight = (((1024 + y_texels / 2) / (y_texels - 1)) * y * (y_weights - 1) + 32) >> 6;
285+
unsigned int x_weight_frac = x_weight & 0xF;
286+
unsigned int y_weight_frac = y_weight & 0xF;
287+
unsigned int x_weight_int = x_weight >> 4;
288+
unsigned int y_weight_int = y_weight >> 4;
284289

285-
int x_weight_frac = x_weight & 0xF;
286-
int y_weight_frac = y_weight & 0xF;
287-
int x_weight_int = x_weight >> 4;
288-
int y_weight_int = y_weight >> 4;
289-
int qweight[4];
290+
unsigned int qweight[4];
290291
qweight[0] = x_weight_int + y_weight_int * x_weights;
291292
qweight[1] = qweight[0] + 1;
292293
qweight[2] = qweight[0] + x_weights;
293294
qweight[3] = qweight[2] + 1;
294295

295296
// Truncated-precision bilinear interpolation
296-
int prod = x_weight_frac * y_weight_frac;
297+
unsigned int prod = x_weight_frac * y_weight_frac;
297298

298-
int weight[4];
299+
unsigned int weight[4];
299300
weight[3] = (prod + 8) >> 4;
300301
weight[1] = x_weight_frac - weight[3];
301302
weight[2] = y_weight_frac - weight[3];
302303
weight[0] = 16 - x_weight_frac - y_weight_frac + weight[3];
303304

304-
for (int i = 0; i < 4; i++)
305+
for (unsigned int i = 0; i < 4; i++)
305306
{
306307
if (weight[i] != 0)
307308
{
@@ -317,7 +318,7 @@ static void init_decimation_info_2d(
317318
}
318319
}
319320

320-
for (int i = 0; i < texels_per_block; i++)
321+
for (unsigned int i = 0; i < texels_per_block; i++)
321322
{
322323
di.texel_weight_count[i] = weight_count_of_texel[i];
323324

@@ -329,20 +330,20 @@ static void init_decimation_info_2d(
329330
}
330331

331332
// Init all 4 entries so we can rely on zeros for vectorization
332-
for (int j = weight_count_of_texel[i]; j < 4; j++)
333+
for (unsigned int j = weight_count_of_texel[i]; j < 4; j++)
333334
{
334335
di.texel_weights_int_4t[j][i] = 0;
335336
di.texel_weights_float_4t[j][i] = 0.0f;
336337
di.texel_weights_4t[j][i] = 0;
337338
}
338339
}
339340

340-
for (int i = 0; i < weights_per_block; i++)
341+
for (unsigned int i = 0; i < weights_per_block; i++)
341342
{
342-
int texel_count_wt = texel_count_of_weight[i];
343+
unsigned int texel_count_wt = texel_count_of_weight[i];
343344
di.weight_texel_count[i] = (uint8_t)texel_count_wt;
344345

345-
for (int j = 0; j < texel_count_wt; j++)
346+
for (unsigned int j = 0; j < texel_count_wt; j++)
346347
{
347348
uint8_t texel = texels_of_weight[i][j];
348349

@@ -380,16 +381,16 @@ static void init_decimation_info_2d(
380381
// Initialize array tail so we can over-fetch with SIMD later to avoid loop tails
381382
// Match last texel in active lane in SIMD group, for better gathers
382383
uint8_t last_texel = di.weight_texel[texel_count_wt - 1][i];
383-
for (int j = texel_count_wt; j < max_texel_count_of_weight; j++)
384+
for (unsigned int j = texel_count_wt; j < max_texel_count_of_weight; j++)
384385
{
385386
di.weight_texel[j][i] = last_texel;
386387
di.weights_flt[j][i] = 0.0f;
387388
}
388389
}
389390

390391
// Initialize array tail so we can over-fetch with SIMD later to avoid loop tails
391-
int texels_per_block_simd = round_up_to_simd_multiple_vla(texels_per_block);
392-
for (int i = texels_per_block; i < texels_per_block_simd; i++)
392+
unsigned int texels_per_block_simd = round_up_to_simd_multiple_vla(texels_per_block);
393+
for (unsigned int i = texels_per_block; i < texels_per_block_simd; i++)
393394
{
394395
di.texel_weight_count[i] = 0;
395396

@@ -706,7 +707,7 @@ static void assign_kmeans_texels(
706707
// Use all texels for kmeans on a small block
707708
if (bsd.texel_count <= MAX_KMEANS_TEXELS)
708709
{
709-
for (int i = 0; i < bsd.texel_count; i++)
710+
for (unsigned int i = 0; i < bsd.texel_count; i++)
710711
{
711712
bsd.kmeans_texels[i] = i;
712713
}
@@ -721,13 +722,13 @@ static void assign_kmeans_texels(
721722

722723
// Initialize array used for tracking used indices
723724
bool seen[MAX_TEXELS_PER_BLOCK];
724-
for (int i = 0; i < bsd.texel_count; i++)
725+
for (unsigned int i = 0; i < bsd.texel_count; i++)
725726
{
726727
seen[i] = false;
727728
}
728729

729730
// Assign 64 random indices, retrying if we see repeats
730-
int arr_elements_set = 0;
731+
unsigned int arr_elements_set = 0;
731732
while (arr_elements_set < MAX_KMEANS_TEXELS)
732733
{
733734
unsigned int texel = (unsigned int)astc::rand(rng_state);
@@ -760,7 +761,7 @@ static int construct_dt_entry_2d(
760761
block_size_descriptor& bsd
761762
) {
762763
int dm_index = bsd.decimation_mode_count;
763-
int weight_count = x_weights * y_weights;
764+
unsigned int weight_count = x_weights * y_weights;
764765
assert(weight_count <= MAX_WEIGHTS_PER_BLOCK);
765766

766767
bool try_2planes = (2 * weight_count) <= MAX_WEIGHTS_PER_BLOCK;
@@ -772,15 +773,15 @@ static int construct_dt_entry_2d(
772773
int maxprec_2planes = -1;
773774
for (int i = 0; i < 12; i++)
774775
{
775-
int bits_1plane = get_ise_sequence_bitcount(weight_count, (quant_method)i);
776+
unsigned int bits_1plane = get_ise_sequence_bitcount(weight_count, (quant_method)i);
776777
if (bits_1plane >= MIN_WEIGHT_BITS_PER_BLOCK && bits_1plane <= MAX_WEIGHT_BITS_PER_BLOCK)
777778
{
778779
maxprec_1plane = i;
779780
}
780781

781782
if (try_2planes)
782783
{
783-
int bits_2planes = get_ise_sequence_bitcount(2 * weight_count, (quant_method)i);
784+
unsigned int bits_2planes = get_ise_sequence_bitcount(2 * weight_count, (quant_method)i);
784785
if (bits_2planes >= MIN_WEIGHT_BITS_PER_BLOCK && bits_2planes <= MAX_WEIGHT_BITS_PER_BLOCK)
785786
{
786787
maxprec_2planes = i;
@@ -976,7 +977,7 @@ static void construct_block_size_descriptor_3d(
976977
{
977978
for (int z_weights = 2; z_weights <= z_texels; z_weights++)
978979
{
979-
int weight_count = x_weights * y_weights * z_weights;
980+
unsigned int weight_count = x_weights * y_weights * z_weights;
980981
if (weight_count > MAX_WEIGHTS_PER_BLOCK)
981982
{
982983
continue;
@@ -990,14 +991,13 @@ static void construct_block_size_descriptor_3d(
990991
int maxprec_2planes = -1;
991992
for (int i = 0; i < 12; i++)
992993
{
993-
int bits_1plane = get_ise_sequence_bitcount(weight_count, (quant_method)i);
994-
int bits_2planes = get_ise_sequence_bitcount(2 * weight_count, (quant_method)i);
995-
994+
unsigned int bits_1plane = get_ise_sequence_bitcount(weight_count, (quant_method)i);
996995
if (bits_1plane >= MIN_WEIGHT_BITS_PER_BLOCK && bits_1plane <= MAX_WEIGHT_BITS_PER_BLOCK)
997996
{
998997
maxprec_1plane = i;
999998
}
1000999

1000+
unsigned int bits_2planes = get_ise_sequence_bitcount(2 * weight_count, (quant_method)i);
10011001
if (bits_2planes >= MIN_WEIGHT_BITS_PER_BLOCK && bits_2planes <= MAX_WEIGHT_BITS_PER_BLOCK)
10021002
{
10031003
maxprec_2planes = i;
@@ -1082,9 +1082,9 @@ static void construct_block_size_descriptor_3d(
10821082

10831083
/* See header for documentation. */
10841084
void init_block_size_descriptor(
1085-
int x_texels,
1086-
int y_texels,
1087-
int z_texels,
1085+
unsigned int x_texels,
1086+
unsigned int y_texels,
1087+
unsigned int z_texels,
10881088
bool can_omit_modes,
10891089
float mode_cutoff,
10901090
block_size_descriptor& bsd
@@ -1105,7 +1105,7 @@ void init_block_size_descriptor(
11051105
void term_block_size_descriptor(
11061106
block_size_descriptor& bsd
11071107
) {
1108-
for (int i = 0; i < bsd.decimation_mode_count; i++)
1108+
for (unsigned int i = 0; i < bsd.decimation_mode_count; i++)
11091109
{
11101110
aligned_free<const decimation_info>(bsd.decimation_tables[i]);
11111111
}

Source/astcenc_compress_symbolic.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ static float compress_symbolic_block_for_partition_1plane(
271271
uint8_t *u8_quantized_decimated_quantized_weights = tmpbuf.u8_quantized_decimated_quantized_weights;
272272

273273
// For each decimation mode, compute an ideal set of weights with no quantization
274-
for (int i = 0; i < bsd.decimation_mode_count; i++)
274+
for (unsigned int i = 0; i < bsd.decimation_mode_count; i++)
275275
{
276276
const decimation_mode& dm = bsd.decimation_modes[i];
277277
if (dm.maxprec_1plane < 0 || (only_always && !dm.percentile_always) || !dm.percentile_hit)
@@ -317,7 +317,7 @@ static float compress_symbolic_block_for_partition_1plane(
317317
int qwt_bitcounts[MAX_WEIGHT_MODES];
318318
float qwt_errors[MAX_WEIGHT_MODES];
319319

320-
for (int i = 0; i < bsd.block_mode_count; ++i)
320+
for (unsigned int i = 0; i < bsd.block_mode_count; ++i)
321321
{
322322
const block_mode& bm = bsd.block_modes[i];
323323
if (bm.is_dual_plane || (only_always && !bm.percentile_always) || !bm.percentile_hit)
@@ -633,7 +633,7 @@ static float compress_symbolic_block_for_partition_2planes(
633633
uint8_t *u8_quantized_decimated_quantized_weights = tmpbuf.u8_quantized_decimated_quantized_weights;
634634

635635
// For each decimation mode, compute an ideal set of weights with no quantization
636-
for (int i = 0; i < bsd.decimation_mode_count; i++)
636+
for (unsigned int i = 0; i < bsd.decimation_mode_count; i++)
637637
{
638638
const decimation_mode& dm = bsd.decimation_modes[i];
639639
if (dm.maxprec_2planes < 0 || !dm.percentile_hit)
@@ -699,7 +699,7 @@ static float compress_symbolic_block_for_partition_2planes(
699699

700700
int qwt_bitcounts[MAX_WEIGHT_MODES];
701701
float qwt_errors[MAX_WEIGHT_MODES];
702-
for (int i = 0; i < bsd.block_mode_count; ++i)
702+
for (unsigned int i = 0; i < bsd.block_mode_count; ++i)
703703
{
704704
const block_mode& bm = bsd.block_modes[i];
705705
if (!bm.is_dual_plane || !bm.percentile_hit)
@@ -708,7 +708,7 @@ static float compress_symbolic_block_for_partition_2planes(
708708
continue;
709709
}
710710

711-
int decimation_mode = bm.decimation_mode;
711+
unsigned int decimation_mode = bm.decimation_mode;
712712

713713
if (weight_high_value1[i] > 1.02f * min_wt_cutoff1)
714714
{
@@ -1067,11 +1067,11 @@ static float prepare_error_weight_block(
10671067
promise(bsd.ydim > 0);
10681068
promise(bsd.zdim > 0);
10691069

1070-
for (int z = 0; z < bsd.zdim; z++)
1070+
for (unsigned int z = 0; z < bsd.zdim; z++)
10711071
{
1072-
for (int y = 0; y < bsd.ydim; y++)
1072+
for (unsigned int y = 0; y < bsd.ydim; y++)
10731073
{
1074-
for (int x = 0; x < bsd.xdim; x++)
1074+
for (unsigned int x = 0; x < bsd.xdim; x++)
10751075
{
10761076
unsigned int xpos = x + blk.xpos;
10771077
unsigned int ypos = y + blk.ypos;

Source/astcenc_decompress_symbolic.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void unpack_weights(
151151
uq_plane1_weights[i] = qat->unquantized_value[scb.weights[i]];
152152
}
153153

154-
for (int i = 0; i < bsd.texel_count; i += ASTCENC_SIMD_WIDTH)
154+
for (unsigned int i = 0; i < bsd.texel_count; i += ASTCENC_SIMD_WIDTH)
155155
{
156156
store(compute_value_of_texel_weight_int_vla(i, di, uq_plane1_weights), weights_plane1 + i);
157157
}
@@ -164,7 +164,7 @@ void unpack_weights(
164164
uq_plane2_weights[i] = qat->unquantized_value[scb.weights[i + PLANE2_WEIGHTS_OFFSET]];
165165
}
166166

167-
for (int i = 0; i < bsd.texel_count; i += ASTCENC_SIMD_WIDTH)
167+
for (unsigned int i = 0; i < bsd.texel_count; i += ASTCENC_SIMD_WIDTH)
168168
{
169169
store(compute_value_of_texel_weight_int_vla(i, di, uq_plane1_weights), weights_plane1 + i);
170170
store(compute_value_of_texel_weight_int_vla(i, di, uq_plane2_weights), weights_plane2 + i);
@@ -193,7 +193,7 @@ void decompress_symbolic_block(
193193
// If we detected an error-block, blow up immediately.
194194
if (scb.error_block)
195195
{
196-
for (int i = 0; i < bsd.texel_count; i++)
196+
for (unsigned int i = 0; i < bsd.texel_count; i++)
197197
{
198198
blk.data_r[i] = std::numeric_limits<float>::quiet_NaN();
199199
blk.data_g[i] = std::numeric_limits<float>::quiet_NaN();
@@ -243,7 +243,7 @@ void decompress_symbolic_block(
243243
}
244244

245245
// TODO: Skip this and add constant color transfer to img block?
246-
for (int i = 0; i < bsd.texel_count; i++)
246+
for (unsigned int i = 0; i < bsd.texel_count; i++)
247247
{
248248
blk.data_r[i] = color.lane<0>();
249249
blk.data_g[i] = color.lane<1>();

Source/astcenc_entry.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ astcenc_error astcenc_get_block_info(
12821282
int weight_plane2[MAX_TEXELS_PER_BLOCK];
12831283

12841284
unpack_weights(bsd, scb, di, bm.is_dual_plane, bm.quant_mode, weight_plane1, weight_plane2);
1285-
for (int i = 0; i < bsd.texel_count; i++)
1285+
for (unsigned int i = 0; i < bsd.texel_count; i++)
12861286
{
12871287
info->weight_values_plane1[i] = (float)weight_plane1[i] / (float)TEXEL_WEIGHT_SUM;
12881288
if (info->is_dual_plane_block)
@@ -1292,7 +1292,7 @@ astcenc_error astcenc_get_block_info(
12921292
}
12931293

12941294
// Unpack partition assignments for each texel
1295-
for (int i = 0; i < bsd.texel_count; i++)
1295+
for (unsigned int i = 0; i < bsd.texel_count; i++)
12961296
{
12971297
info->partition_assignment[i] = pt->partition_of_texel[i];
12981298
}

0 commit comments

Comments
 (0)