• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: Apache-2.0
2 // ----------------------------------------------------------------------------
3 // Copyright 2011-2022 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 #if !defined(ASTCENC_DECOMPRESS_ONLY)
19 
20 /**
21  * @brief Functions for angular-sum algorithm for weight alignment.
22  *
23  * This algorithm works as follows:
24  * - we compute a complex number P as (cos s*i, sin s*i) for each weight,
25  *   where i is the input value and s is a scaling factor based on the spacing between the weights.
26  * - we then add together complex numbers for all the weights.
27  * - we then compute the length and angle of the resulting sum.
28  *
29  * This should produce the following results:
30  * - perfect alignment results in a vector whose length is equal to the sum of lengths of all inputs
31  * - even distribution results in a vector of length 0.
32  * - all samples identical results in perfect alignment for every scaling.
33  *
34  * For each scaling factor within a given set, we compute an alignment factor from 0 to 1. This
35  * should then result in some scalings standing out as having particularly good alignment factors;
36  * we can use this to produce a set of candidate scale/shift values for various quantization levels;
37  * we should then actually try them and see what happens.
38  */
39 
40 #include "astcenc_internal.h"
41 #include "astcenc_vecmathlib.h"
42 
43 #include <stdio.h>
44 #include <cassert>
45 #include <cstring>
46 
47 
48 static constexpr unsigned int ANGULAR_STEPS { 40 };
49 
50 // Store a reduced sin/cos table for 64 possible weight values; this causes slight quality loss
51 // compared to using sin() and cos() directly. Must be 2^N.
52 static constexpr unsigned int SINCOS_STEPS { 64 };
53 
54 static_assert((ANGULAR_STEPS % ASTCENC_SIMD_WIDTH) == 0,
55               "ANGULAR_STEPS must be multiple of ASTCENC_SIMD_WIDTH");
56 
57 static uint8_t max_angular_steps_needed_for_quant_level[13];
58 
59 // The next-to-last entry is supposed to have the value 33. This because the 32-weight mode leaves a
60 // double-sized hole in the middle of the weight space, so we are better off matching 33 weights.
61 static const uint8_t quantization_steps_for_level[13] {
62 	2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 24, 33, 36
63 };
64 
65 alignas(ASTCENC_VECALIGN) static float sin_table[SINCOS_STEPS][ANGULAR_STEPS];
66 alignas(ASTCENC_VECALIGN) static float cos_table[SINCOS_STEPS][ANGULAR_STEPS];
67 
68 #if defined(ASTCENC_DIAGNOSTICS)
69 	static bool print_once { true };
70 #endif
71 
72 /* See header for documentation. */
prepare_angular_tables()73 void prepare_angular_tables()
74 {
75 	unsigned int max_angular_steps_needed_for_quant_steps[ANGULAR_STEPS + 1];
76 	for (unsigned int i = 0; i < ANGULAR_STEPS; i++)
77 	{
78 		float angle_step = static_cast<float>(i + 1);
79 
80 		for (unsigned int j = 0; j < SINCOS_STEPS; j++)
81 		{
82 			sin_table[j][i] = static_cast<float>(sinf((2.0f * astc::PI / (SINCOS_STEPS - 1.0f)) * angle_step * static_cast<float>(j)));
83 			cos_table[j][i] = static_cast<float>(cosf((2.0f * astc::PI / (SINCOS_STEPS - 1.0f)) * angle_step * static_cast<float>(j)));
84 		}
85 
86 		max_angular_steps_needed_for_quant_steps[i + 1] = astc::min(i + 1, ANGULAR_STEPS - 1);
87 	}
88 
89 	for (unsigned int i = 0; i < 13; i++)
90 	{
91 		max_angular_steps_needed_for_quant_level[i] = max_angular_steps_needed_for_quant_steps[quantization_steps_for_level[i]];
92 	}
93 }
94 
95 /**
96  * @brief Compute the angular alignment factors and offsets.
97  *
98  * @param      weight_count              The number of (decimated) weights.
99  * @param      dec_weight_ideal_value    The ideal decimated unquantized weight values.
100  * @param      max_angular_steps         The maximum number of steps to be tested.
101  * @param[out] offsets                   The output angular offsets array.
102  */
compute_angular_offsets(unsigned int weight_count,const float * dec_weight_ideal_value,unsigned int max_angular_steps,float * offsets)103 static void compute_angular_offsets(
104 	unsigned int weight_count,
105 	const float* dec_weight_ideal_value,
106 	unsigned int max_angular_steps,
107 	float* offsets
108 ) {
109 	promise(weight_count > 0);
110 	promise(max_angular_steps > 0);
111 
112 	alignas(ASTCENC_VECALIGN) int isamplev[BLOCK_MAX_WEIGHTS];
113 
114 	// Precompute isample; arrays are always allocated 64 elements long
115 	for (unsigned int i = 0; i < weight_count; i += ASTCENC_SIMD_WIDTH)
116 	{
117 		// Add 2^23 and interpreting bits extracts round-to-nearest int
118 		vfloat sample = loada(dec_weight_ideal_value + i) * (SINCOS_STEPS - 1.0f) + vfloat(12582912.0f);
119 		vint isample = float_as_int(sample) & vint((SINCOS_STEPS - 1));
120 		storea(isample, isamplev + i);
121 	}
122 
123 	// Arrays are multiple of SIMD width (ANGULAR_STEPS), safe to overshoot max
124 	vfloat mult = vfloat(1.0f / (2.0f * astc::PI));
125 
126 	for (unsigned int i = 0; i < max_angular_steps; i += ASTCENC_SIMD_WIDTH)
127 	{
128 		vfloat anglesum_x = vfloat::zero();
129 		vfloat anglesum_y = vfloat::zero();
130 
131 		for (unsigned int j = 0; j < weight_count; j++)
132 		{
133 			int isample = isamplev[j];
134 			anglesum_x += loada(cos_table[isample] + i);
135 			anglesum_y += loada(sin_table[isample] + i);
136 		}
137 
138 		vfloat angle = atan2(anglesum_y, anglesum_x);
139 		vfloat ofs = angle * mult;
140 		storea(ofs, offsets + i);
141 	}
142 }
143 
144 /**
145  * @brief For a given step size compute the lowest and highest weight.
146  *
147  * Compute the lowest and highest weight that results from quantizing using the given stepsize and
148  * offset, and then compute the resulting error. The cut errors indicate the error that results from
149  * forcing samples that should have had one weight value one step up or down.
150  *
151  * @param      weight_count              The number of (decimated) weights.
152  * @param      dec_weight_ideal_value    The ideal decimated unquantized weight values.
153  * @param      max_angular_steps         The maximum number of steps to be tested.
154  * @param      max_quant_steps           The maximum quantization level to be tested.
155  * @param      offsets                   The angular offsets array.
156  * @param[out] lowest_weight             Per angular step, the lowest weight.
157  * @param[out] weight_span               Per angular step, the span between lowest and highest weight.
158  * @param[out] error                     Per angular step, the error.
159  * @param[out] cut_low_weight_error      Per angular step, the low weight cut error.
160  * @param[out] cut_high_weight_error     Per angular step, the high weight cut error.
161  */
compute_lowest_and_highest_weight(unsigned int weight_count,const float * dec_weight_ideal_value,unsigned int max_angular_steps,unsigned int max_quant_steps,const float * offsets,int * lowest_weight,int * weight_span,float * error,float * cut_low_weight_error,float * cut_high_weight_error)162 static void compute_lowest_and_highest_weight(
163 	unsigned int weight_count,
164 	const float* dec_weight_ideal_value,
165 	unsigned int max_angular_steps,
166 	unsigned int max_quant_steps,
167 	const float* offsets,
168 	int* lowest_weight,
169 	int* weight_span,
170 	float* error,
171 	float* cut_low_weight_error,
172 	float* cut_high_weight_error
173 ) {
174 	promise(weight_count > 0);
175 	promise(max_angular_steps > 0);
176 
177 	vfloat rcp_stepsize = vfloat::lane_id() + vfloat(1.0f);
178 
179 	// Arrays are ANGULAR_STEPS long, so always safe to run full vectors
180 	for (unsigned int sp = 0; sp < max_angular_steps; sp += ASTCENC_SIMD_WIDTH)
181 	{
182 		vfloat minidx(128.0f);
183 		vfloat maxidx(-128.0f);
184 		vfloat errval = vfloat::zero();
185 		vfloat cut_low_weight_err = vfloat::zero();
186 		vfloat cut_high_weight_err = vfloat::zero();
187 		vfloat offset = loada(&offsets[sp]);
188 
189 		for (unsigned int j = 0; j < weight_count; ++j)
190 		{
191 			vfloat sval = load1(&dec_weight_ideal_value[j]) * rcp_stepsize - offset;
192 			vfloat svalrte = round(sval);
193 			vfloat diff = sval - svalrte;
194 			errval += diff * diff;
195 
196 			// Reset tracker on min hit
197 			vmask mask = svalrte < minidx;
198 			minidx = select(minidx, svalrte, mask);
199 			cut_low_weight_err = select(cut_low_weight_err, vfloat::zero(), mask);
200 
201 			// Accumulate on min hit
202 			mask = svalrte == minidx;
203 			vfloat accum = cut_low_weight_err + vfloat(1.0f) - vfloat(2.0f) * diff;
204 			cut_low_weight_err = select(cut_low_weight_err, accum, mask);
205 
206 			// Reset tracker on max hit
207 			mask = svalrte > maxidx;
208 			maxidx = select(maxidx, svalrte, mask);
209 			cut_high_weight_err = select(cut_high_weight_err, vfloat::zero(), mask);
210 
211 			// Accumulate on max hit
212 			mask = svalrte == maxidx;
213 			accum = cut_high_weight_err + vfloat(1.0f) + vfloat(2.0f) * diff;
214 			cut_high_weight_err = select(cut_high_weight_err, accum, mask);
215 		}
216 
217 		// Write out min weight and weight span; clamp span to a usable range
218 		vint span = float_to_int(maxidx - minidx + vfloat(1));
219 		span = min(span, vint(max_quant_steps + 3));
220 		span = max(span, vint(2));
221 		storea(float_to_int(minidx), &lowest_weight[sp]);
222 		storea(span, &weight_span[sp]);
223 
224 		// The cut_(lowest/highest)_weight_error indicate the error that results from  forcing
225 		// samples that should have had the weight value one step (up/down).
226 		vfloat ssize = 1.0f / rcp_stepsize;
227 		vfloat errscale = ssize * ssize;
228 		storea(errval * errscale, &error[sp]);
229 		storea(cut_low_weight_err * errscale, &cut_low_weight_error[sp]);
230 		storea(cut_high_weight_err * errscale, &cut_high_weight_error[sp]);
231 
232 		rcp_stepsize = rcp_stepsize + vfloat(ASTCENC_SIMD_WIDTH);
233 	}
234 }
235 
236 /**
237  * @brief The main function for the angular algorithm.
238  *
239  * @param      weight_count              The number of (decimated) weights.
240  * @param      dec_weight_ideal_value    The ideal decimated unquantized weight values.
241  * @param      max_quant_level           The maximum quantization level to be tested.
242  * @param[out] low_value                 Per angular step, the lowest weight value.
243  * @param[out] high_value                Per angular step, the highest weight value.
244  */
compute_angular_endpoints_for_quant_levels(unsigned int weight_count,const float * dec_weight_ideal_value,unsigned int max_quant_level,float low_value[12],float high_value[12])245 static void compute_angular_endpoints_for_quant_levels(
246 	unsigned int weight_count,
247 	const float* dec_weight_ideal_value,
248 	unsigned int max_quant_level,
249 	float low_value[12],
250 	float high_value[12]
251 ) {
252 	unsigned int max_quant_steps = quantization_steps_for_level[max_quant_level];
253 
254 	alignas(ASTCENC_VECALIGN) float angular_offsets[ANGULAR_STEPS];
255 	unsigned int max_angular_steps = max_angular_steps_needed_for_quant_level[max_quant_level];
256 	compute_angular_offsets(weight_count, dec_weight_ideal_value,
257 	                        max_angular_steps, angular_offsets);
258 
259 	alignas(ASTCENC_VECALIGN) int32_t lowest_weight[ANGULAR_STEPS];
260 	alignas(ASTCENC_VECALIGN) int32_t weight_span[ANGULAR_STEPS];
261 	alignas(ASTCENC_VECALIGN) float error[ANGULAR_STEPS];
262 	alignas(ASTCENC_VECALIGN) float cut_low_weight_error[ANGULAR_STEPS];
263 	alignas(ASTCENC_VECALIGN) float cut_high_weight_error[ANGULAR_STEPS];
264 
265 	compute_lowest_and_highest_weight(weight_count, dec_weight_ideal_value,
266 	                                  max_angular_steps, max_quant_steps,
267 	                                  angular_offsets, lowest_weight, weight_span, error,
268 	                                  cut_low_weight_error, cut_high_weight_error);
269 
270 	// For each quantization level, find the best error terms. Use packed vectors so data-dependent
271 	// branches can become selects. This involves some integer to float casts, but the values are
272 	// small enough so they never round the wrong way.
273 	vfloat4 best_results[40];
274 
275 	// Initialize the array to some safe defaults
276 	promise(max_quant_steps > 0);
277 	for (unsigned int i = 0; i < (max_quant_steps + 4); i++)
278 	{
279 		// Lane<0> = Best error
280 		// Lane<1> = Best scale; -1 indicates no solution found
281 		// Lane<2> = Cut low weight
282 		best_results[i] = vfloat4(ERROR_CALC_DEFAULT, -1.0f, 0.0f, 0.0f);
283 	}
284 
285 	promise(max_angular_steps > 0);
286 	for (unsigned int i = 0; i < max_angular_steps; i++)
287 	{
288 		float i_flt = static_cast<float>(i);
289 
290 		int idx_span = weight_span[i];
291 
292 		float error_cut_low = error[i] + cut_low_weight_error[i];
293 		float error_cut_high = error[i] + cut_high_weight_error[i];
294 		float error_cut_low_high = error[i] + cut_low_weight_error[i] + cut_high_weight_error[i];
295 
296 		// Check best error against record N
297 		vfloat4 best_result = best_results[idx_span];
298 		vfloat4 new_result = vfloat4(error[i], i_flt, 0.0f, 0.0f);
299 		vmask4 mask1(best_result.lane<0>() > error[i]);
300 		best_results[idx_span] = select(best_result, new_result, mask1);
301 
302 		// Check best error against record N-1 with either cut low or cut high
303 		best_result = best_results[idx_span - 1];
304 
305 		new_result = vfloat4(error_cut_low, i_flt, 1.0f, 0.0f);
306 		vmask4 mask2(best_result.lane<0>() > error_cut_low);
307 		best_result = select(best_result, new_result, mask2);
308 
309 		new_result = vfloat4(error_cut_high, i_flt, 0.0f, 0.0f);
310 		vmask4 mask3(best_result.lane<0>() > error_cut_high);
311 		best_results[idx_span - 1] = select(best_result, new_result, mask3);
312 
313 		// Check best error against record N-2 with both cut low and high
314 		best_result = best_results[idx_span - 2];
315 		new_result = vfloat4(error_cut_low_high, i_flt, 1.0f, 0.0f);
316 		vmask4 mask4(best_result.lane<0>() > error_cut_low_high);
317 		best_results[idx_span - 2] = select(best_result, new_result, mask4);
318 	}
319 
320 	for (unsigned int i = 0; i <= max_quant_level; i++)
321 	{
322 		unsigned int q = quantization_steps_for_level[i];
323 		int bsi = static_cast<int>(best_results[q].lane<1>());
324 
325 		// Did we find anything?
326 #if defined(ASTCENC_DIAGNOSTICS)
327 		if ((bsi < 0) && print_once)
328 		{
329 			print_once = false;
330 			printf("INFO: Unable to find full encoding within search error limit.\n\n");
331 		}
332 #endif
333 
334 		bsi = astc::max(0, bsi);
335 
336 		float stepsize = 1.0f / (1.0f + static_cast<float>(bsi));
337 		int lwi = lowest_weight[bsi] + static_cast<int>(best_results[q].lane<2>());
338 		int hwi = lwi + q - 1;
339 
340 		float offset = angular_offsets[bsi] * stepsize;
341 		low_value[i] = offset + static_cast<float>(lwi) * stepsize;
342 		high_value[i] = offset + static_cast<float>(hwi) * stepsize;
343 	}
344 }
345 
346 /**
347  * @brief For a given step size compute the lowest and highest weight, variant for low weight count.
348  *
349  * Compute the lowest and highest weight that results from quantizing using the given stepsize and
350  * offset, and then compute the resulting error. The cut errors indicate the error that results from
351  * forcing samples that should have had one weight value one step up or down.
352  *
353  * @param      weight_count              The number of (decimated) weights.
354  * @param      dec_weight_quant_uvalue   The decimated and quantized weight values.
355  * @param      max_angular_steps         The maximum number of steps to be tested.
356  * @param      max_quant_steps           The maximum quantization level to be tested.
357  * @param      offsets                   The angular offsets array.
358  * @param[out] lowest_weight             Per angular step, the lowest weight.
359  * @param[out] weight_span               Per angular step, the span between lowest and highest weight.
360  * @param[out] error                     Per angular step, the error.
361  */
compute_lowest_and_highest_weight_lwc(unsigned int weight_count,const float * dec_weight_quant_uvalue,unsigned int max_angular_steps,unsigned int max_quant_steps,const float * offsets,int * lowest_weight,int * weight_span,float * error)362 static void compute_lowest_and_highest_weight_lwc(
363 	unsigned int weight_count,
364 	const float* dec_weight_quant_uvalue,
365 	unsigned int max_angular_steps,
366 	unsigned int max_quant_steps,
367 	const float* offsets,
368 	int* lowest_weight,
369 	int* weight_span,
370 	float* error
371 ) {
372 	promise(weight_count > 0);
373 	promise(max_angular_steps > 0);
374 
375 	vfloat rcp_stepsize = vfloat::lane_id() + vfloat(1.0f);
376 
377 	// Arrays are ANGULAR_STEPS long, so always safe to run full vectors
378 	for (unsigned int sp = 0; sp < max_angular_steps; sp += ASTCENC_SIMD_WIDTH)
379 	{
380 		vfloat minidx(128.0f);
381 		vfloat maxidx(-128.0f);
382 		vfloat errval = vfloat::zero();
383 		vfloat offset = loada(&offsets[sp]);
384 
385 		for (unsigned int j = 0; j < weight_count; ++j)
386 		{
387 			vfloat sval = load1(&dec_weight_quant_uvalue[j]) * rcp_stepsize - offset;
388 			vfloat svalrte = round(sval);
389 			vfloat diff = sval - svalrte;
390 			errval += diff * diff;
391 
392 			// Reset tracker on min hit
393 			vmask mask = svalrte < minidx;
394 			minidx = select(minidx, svalrte, mask);
395 
396 			// Reset tracker on max hit
397 			mask = svalrte > maxidx;
398 			maxidx = select(maxidx, svalrte, mask);
399 		}
400 
401 		// Write out min weight and weight span; clamp span to a usable range
402 		vint span = float_to_int(maxidx - minidx + vfloat(1.0f));
403 		span = min(span, vint(max_quant_steps + 3));
404 		span = max(span, vint(2));
405 		storea(float_to_int(minidx), &lowest_weight[sp]);
406 		storea(span, &weight_span[sp]);
407 
408 		// The cut_(lowest/highest)_weight_error indicate the error that results from  forcing
409 		// samples that should have had the weight value one step (up/down).
410 		vfloat ssize = 1.0f / rcp_stepsize;
411 		vfloat errscale = ssize * ssize;
412 		storea(errval * errscale, &error[sp]);
413 
414 		rcp_stepsize = rcp_stepsize + vfloat(ASTCENC_SIMD_WIDTH);
415 	}
416 }
417 
418 /**
419  * @brief The main function for the angular algorithm, variant for low weight count.
420  *
421  * @param      weight_count              The number of (decimated) weights.
422  * @param      dec_weight_ideal_value    The ideal decimated unquantized weight values.
423  * @param      max_quant_level           The maximum quantization level to be tested.
424  * @param[out] low_value                 Per angular step, the lowest weight value.
425  * @param[out] high_value                Per angular step, the highest weight value.
426  */
compute_angular_endpoints_for_quant_levels_lwc(unsigned int weight_count,const float * dec_weight_ideal_value,unsigned int max_quant_level,float low_value[12],float high_value[12])427 static void compute_angular_endpoints_for_quant_levels_lwc(
428 	unsigned int weight_count,
429 	const float* dec_weight_ideal_value,
430 	unsigned int max_quant_level,
431 	float low_value[12],
432 	float high_value[12]
433 ) {
434 	unsigned int max_quant_steps = quantization_steps_for_level[max_quant_level];
435 	unsigned int max_angular_steps = max_angular_steps_needed_for_quant_level[max_quant_level];
436 
437 	alignas(ASTCENC_VECALIGN) float angular_offsets[ANGULAR_STEPS];
438 	alignas(ASTCENC_VECALIGN) int32_t lowest_weight[ANGULAR_STEPS];
439 	alignas(ASTCENC_VECALIGN) int32_t weight_span[ANGULAR_STEPS];
440 	alignas(ASTCENC_VECALIGN) float error[ANGULAR_STEPS];
441 
442 	compute_angular_offsets(weight_count, dec_weight_ideal_value,
443 	                        max_angular_steps, angular_offsets);
444 
445 
446 	compute_lowest_and_highest_weight_lwc(weight_count, dec_weight_ideal_value,
447 	                                      max_angular_steps, max_quant_steps,
448 	                                      angular_offsets, lowest_weight, weight_span, error);
449 
450 	// For each quantization level, find the best error terms. Use packed vectors so data-dependent
451 	// branches can become selects. This involves some integer to float casts, but the values are
452 	// small enough so they never round the wrong way.
453 	vfloat4 best_results[ANGULAR_STEPS];
454 
455 	// Initialize the array to some safe defaults
456 	promise(max_quant_steps > 0);
457 	for (unsigned int i = 0; i < (max_quant_steps + 4); i++)
458 	{
459 		best_results[i] = vfloat4(ERROR_CALC_DEFAULT, -1.0f, 0.0f, 0.0f);
460 	}
461 
462 	promise(max_angular_steps > 0);
463 	for (unsigned int i = 0; i < max_angular_steps; i++)
464 	{
465 		int idx_span = weight_span[i];
466 
467 		// Check best error against record N
468 		vfloat4 current_best = best_results[idx_span];
469 		vfloat4 candidate = vfloat4(error[i], static_cast<float>(i), 0.0f, 0.0f);
470 		vmask4 mask(current_best.lane<0>() > error[i]);
471 		best_results[idx_span] = select(current_best, candidate, mask);
472 	}
473 
474 	for (unsigned int i = 0; i <= max_quant_level; i++)
475 	{
476 		unsigned int q = quantization_steps_for_level[i];
477 		int bsi = static_cast<int>(best_results[q].lane<1>());
478 
479 		// Did we find anything?
480 #if defined(ASTCENC_DIAGNOSTICS)
481 		if ((bsi < 0) && print_once)
482 		{
483 			print_once = false;
484 			printf("INFO: Unable to find low weight encoding within search error limit.\n\n");
485 		}
486 #endif
487 
488 		bsi = astc::max(0, bsi);
489 
490 		int lwi = lowest_weight[bsi];
491 		int hwi = lwi + q - 1;
492 
493 		low_value[i]  = (angular_offsets[bsi] + static_cast<float>(lwi)) / (1.0f + static_cast<float>(bsi));
494 		high_value[i] = (angular_offsets[bsi] + static_cast<float>(hwi)) / (1.0f + static_cast<float>(bsi));
495 	}
496 }
497 
498 /* See header for documentation. */
compute_angular_endpoints_1plane(unsigned int tune_low_weight_limit,bool only_always,const block_size_descriptor & bsd,const float * dec_weight_ideal_value,compression_working_buffers & tmpbuf)499 void compute_angular_endpoints_1plane(
500 	unsigned int tune_low_weight_limit,
501 	bool only_always,
502 	const block_size_descriptor& bsd,
503 	const float* dec_weight_ideal_value,
504 	compression_working_buffers& tmpbuf
505 ) {
506 	float (&low_value)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_low_value1;
507 	float (&high_value)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_high_value1;
508 
509 	float (&low_values)[WEIGHTS_MAX_DECIMATION_MODES][12] = tmpbuf.weight_low_values1;
510 	float (&high_values)[WEIGHTS_MAX_DECIMATION_MODES][12] = tmpbuf.weight_high_values1;
511 
512 	unsigned int max_decimation_modes = only_always ? bsd.decimation_mode_count_always
513 	                                                : bsd.decimation_mode_count_selected;
514 	promise(max_decimation_modes > 0);
515 	for (unsigned int i = 0; i < max_decimation_modes; i++)
516 	{
517 		const decimation_mode& dm = bsd.decimation_modes[i];
518 		if (!dm.ref_1_plane)
519 		{
520 			continue;
521 		}
522 
523 		unsigned int weight_count = bsd.get_decimation_info(i).weight_count;
524 
525 		if (weight_count < tune_low_weight_limit)
526 		{
527 			compute_angular_endpoints_for_quant_levels_lwc(
528 				weight_count,
529 				dec_weight_ideal_value + i * BLOCK_MAX_WEIGHTS,
530 				dm.maxprec_1plane, low_values[i], high_values[i]);
531 		}
532 		else
533 		{
534 			compute_angular_endpoints_for_quant_levels(
535 				weight_count,
536 				dec_weight_ideal_value + i * BLOCK_MAX_WEIGHTS,
537 				dm.maxprec_1plane, low_values[i], high_values[i]);
538 		}
539 	}
540 
541 	unsigned int max_block_modes = only_always ? bsd.block_mode_count_1plane_always
542 	                                           : bsd.block_mode_count_1plane_selected;
543 	promise(max_block_modes > 0);
544 	for (unsigned int i = 0; i < max_block_modes; ++i)
545 	{
546 		const block_mode& bm = bsd.block_modes[i];
547 		assert(!bm.is_dual_plane);
548 
549 		unsigned int quant_mode = bm.quant_mode;
550 		unsigned int decim_mode = bm.decimation_mode;
551 
552 		low_value[i] = low_values[decim_mode][quant_mode];
553 		high_value[i] = high_values[decim_mode][quant_mode];
554 	}
555 }
556 
557 /* See header for documentation. */
compute_angular_endpoints_2planes(unsigned int tune_low_weight_limit,const block_size_descriptor & bsd,const float * dec_weight_ideal_value,compression_working_buffers & tmpbuf)558 void compute_angular_endpoints_2planes(
559 	unsigned int tune_low_weight_limit,
560 	const block_size_descriptor& bsd,
561 	const float* dec_weight_ideal_value,
562 	compression_working_buffers& tmpbuf
563 ) {
564 	float (&low_value1)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_low_value1;
565 	float (&high_value1)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_high_value1;
566 	float (&low_value2)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_low_value2;
567 	float (&high_value2)[WEIGHTS_MAX_BLOCK_MODES] = tmpbuf.weight_high_value2;
568 
569 	float (&low_values1)[WEIGHTS_MAX_DECIMATION_MODES][12] = tmpbuf.weight_low_values1;
570 	float (&high_values1)[WEIGHTS_MAX_DECIMATION_MODES][12] = tmpbuf.weight_high_values1;
571 	float (&low_values2)[WEIGHTS_MAX_DECIMATION_MODES][12] = tmpbuf.weight_low_values2;
572 	float (&high_values2)[WEIGHTS_MAX_DECIMATION_MODES][12] = tmpbuf.weight_high_values2;
573 
574 	promise(bsd.decimation_mode_count_selected > 0);
575 	for (unsigned int i = 0; i < bsd.decimation_mode_count_selected; i++)
576 	{
577 		const decimation_mode& dm = bsd.decimation_modes[i];
578 		if (!dm.ref_2_planes)
579 		{
580 			continue;
581 		}
582 
583 		unsigned int weight_count = bsd.get_decimation_info(i).weight_count;
584 
585 		if (weight_count < tune_low_weight_limit)
586 		{
587 			compute_angular_endpoints_for_quant_levels_lwc(
588 				weight_count,
589 				dec_weight_ideal_value + i * BLOCK_MAX_WEIGHTS,
590 				dm.maxprec_2planes, low_values1[i], high_values1[i]);
591 
592 			compute_angular_endpoints_for_quant_levels_lwc(
593 				weight_count,
594 				dec_weight_ideal_value + i * BLOCK_MAX_WEIGHTS + WEIGHTS_PLANE2_OFFSET,
595 				dm.maxprec_2planes, low_values2[i], high_values2[i]);
596 		}
597 		else
598 		{
599 			compute_angular_endpoints_for_quant_levels(
600 				weight_count,
601 				dec_weight_ideal_value + i * BLOCK_MAX_WEIGHTS,
602 				dm.maxprec_2planes, low_values1[i], high_values1[i]);
603 
604 			compute_angular_endpoints_for_quant_levels(
605 				weight_count,
606 				dec_weight_ideal_value + i * BLOCK_MAX_WEIGHTS + WEIGHTS_PLANE2_OFFSET,
607 				dm.maxprec_2planes, low_values2[i], high_values2[i]);
608 		}
609 	}
610 
611 	unsigned int start = bsd.block_mode_count_1plane_selected;
612 	unsigned int end = bsd.block_mode_count_1plane_2plane_selected;
613 	for (unsigned int i = start; i < end; i++)
614 	{
615 		const block_mode& bm = bsd.block_modes[i];
616 		unsigned int quant_mode = bm.quant_mode;
617 		unsigned int decim_mode = bm.decimation_mode;
618 
619 		low_value1[i] = low_values1[decim_mode][quant_mode];
620 		high_value1[i] = high_values1[decim_mode][quant_mode];
621 		low_value2[i] = low_values2[decim_mode][quant_mode];
622 		high_value2[i] = high_values2[decim_mode][quant_mode];
623 	}
624 }
625 
626 #endif
627