astcenc_weight_align.cpp 24 KB

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