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